-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·272 lines (208 loc) · 7.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const {menubar} = require("menubar");
const {readJsonFileSync} = require("./util/FileUtil");
const {app, BrowserWindow, ipcRenderer, ipcMain, contextBridge, dialog} = require('electron');
const path = require("path");
const fs = require('fs');
const dbFileName = 'nba.db';
const asarDbPath = path.join(__dirname, dbFileName);
const userDataPath = app.getPath('userData');
const localDbPath = path.join(userDataPath, dbFileName);
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: localDbPath,
},
})
// 监听来自渲染进程的消息
ipcMain.on('confirmStarTeam', async (event, arg) => {
console.log('Received from renderer:', arg);
await knex('config').insert({
type: 'starTeam',
val: arg
}).onConflict('type').merge()
starTeam = arg
// 发送响应给渲染进程
event.reply('message-from-main', {message: 'Response from main process'});
await startApp()
});
ipcMain.on('enableLive', async (event, arg) => {
await knex('config').insert({
type: 'enableLive',
val: arg
}).onConflict('type').merge()
enableLive = arg
await startApp()
});
ipcMain.on('exit', (event, arg) => {
app.quit()
});
const axios = require('axios');
async function httpRequest(url) {
try {
// GET 请求
return await axios.get(url);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
}
const mb = menubar({
browserWindow: {
transparent: false,
width: 350,
preloadWindow: true,
height: 300,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true, // 推荐开启
nodeIntegration: false // 推荐关闭
}
},
index: "file://" + path.join(__dirname, 'web', 'dist', 'index.html'),
icon: "./build/16x16.png",
alwaysOnTop: true,
showOnRightClick: true
});
let playTime = 30;
let gameTimer = null;
let currentIndex = 0;
let logoJson = null
const {JSDOM} = require('jsdom')
let gameInfo = []
let starTeam = '勇士'
let enableLive = false
mb.on("ready", async () => {
// 初始化数据库
await setupDatabase()
logoJson = readJsonFileSync(path.join(__dirname, "resources", "logo.json"))
await startApp()
})
async function setupDatabase() {
console.info("localDbPath:", localDbPath)
console.info("asarDbPath:", asarDbPath)
if (!fs.existsSync(localDbPath)) {
// 确保用户数据目录存在
if (!fs.existsSync(userDataPath)) {
fs.mkdirSync(userDataPath, {recursive: true});
}
// 复制数据库文件到用户数据目录
const readStream = fs.createReadStream(asarDbPath);
const writeStream = fs.createWriteStream(localDbPath);
writeStream.on('error', (err) => console.error('Failed to write to user data:', err));
writeStream.on('finish', () => {
});
readStream.pipe(writeStream);
}
await knex.schema.createTableIfNotExists('config', (table) => {
table.increments('id');
table.string('type');
table.string('val');
})
}
mb.on("after-create-window", async () => {
logoJson = readJsonFileSync(path.join(__dirname, "resources", "logo.json"))
const teamNamesZh = logoJson.map(team => team['teamNameZh'])
mb.window.webContents.send("receiveFromElectron", {
"type": "teamInfo",
"message": teamNamesZh
})
const selectedRows = await knex('config').select('*')
enableLive = selectedRows.find(item => item.type === 'enableLive').val === '1'
starTeam = selectedRows.find(item => item.type === 'starTeam').val
mb.window.webContents.send("receiveFromElectron", {
"type": "enableLive",
"message": enableLive
})
mb.window.webContents.send("receiveFromElectron", {
"type": "starTeam",
"message": starTeam
})
});
async function startApp() {
gameFlag = null
mb.tray.setTitle("")
const selectedRows = await knex('config').select('*')
enableLive = selectedRows.find(item => item.type === 'enableLive').val === '1'
starTeam = selectedRows.find(item => item.type === 'starTeam').val
if (gameTimer != null) {
clearInterval(gameTimer)
}
resetLogo()
if (enableLive) {
getNbaInfo()
gameTimer = setInterval(getNbaInfo, 10000);
} else {
gameTimer = setInterval(function () {
playLogo(logoJson[currentIndex])
if (currentIndex >= 29) {
currentIndex = 0
} else {
currentIndex = currentIndex + 1
}
}, 10000);
}
}
function getNbaInfo() {
console.info("start look game")
httpRequest('https://nba.hupu.com/').then(resp => {
const dom = new JSDOM(resp.data);
const document = dom.window.document;
const containers = document.getElementsByClassName('MainSchedule-list-item');
// 清空
gameInfo = []
for (let i = 0; i < containers.length; i++) {
let spans = containers[i].querySelectorAll('span')
gameInfo.push({
"guestTeamName": spans[0].textContent,
"guestScore": spans[1].textContent,
"homeTeamName": spans[5].textContent,
"homeScore": spans[3].textContent,
})
}
// 匹配关注球队
let starInfo = gameInfo.find(item => item.guestTeamName === starTeam || item.homeTeamName === starTeam);
playGame(starInfo)
})
}
function resetLogo() {
let file = path.join(__dirname, "images", "24x24.png");
mb.tray.setImage(file)
}
let gameFlag = null
function playGame(starInfo) {
if (!starInfo) {
// 没有比赛
return;
}
let homeTeam = logoJson.find(item => item['teamNameZh'] === starInfo.homeTeamName)
let guestTeam = logoJson.find(item => item['teamNameZh'] === starInfo.guestTeamName)
if (gameFlag == null) {
gameFlag = starInfo
playScore(starInfo)
return;
}
if (gameFlag.homeScore < starInfo.homeScore) {
playLogo(homeTeam)
playScore(starInfo)
gameFlag = starInfo
return
}
if (gameFlag.guestScore < starInfo.guestScore) {
gameFlag = starInfo
playLogo(guestTeam)
playScore(starInfo)
}
}
function playScore(starInfo) {
let guestShortName = logoJson.find(item => item['teamNameZh'] === starInfo['guestTeamName'])['shortName'];
let homeShortName = logoJson.find(item => item['teamNameZh'] === starInfo['homeTeamName'])['shortName'];
mb.tray.setTitle(guestShortName + " " + starInfo['guestScore'] + " " + homeShortName + " " + starInfo['homeScore'])
}
function playLogo(teamInfo) {
let finalI = teamInfo['espnAnimSize']
for (let i = 1; i <= finalI; i++) {
let file = path.join(__dirname, "nba", teamInfo['teamName'],
teamInfo['teamName'] + "_" + `${i}`.padStart(3, "0") + ".png");
setTimeout(() => mb.tray.setImage(file), playTime * (i));
}
setTimeout(() => resetLogo(), playTime * (finalI + 1));
}