-
Notifications
You must be signed in to change notification settings - Fork 7
/
logGames.js
282 lines (275 loc) · 9.86 KB
/
logGames.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
272
273
274
275
276
277
278
279
280
281
282
const { wrappedRun } = require("./entryPoint");
const fs = require("fs");
const path = require("path");
const rp = require("request-promise");
const { createMajsoulConnection } = require("./majsoul");
const MODES = [216, 215, 225, 226, 224, 223, 212, 211, 208, 209, 221, 222];
const OUTPUT_DIR = process.env.OUTPUT_DIR || path.resolve("livegames");
function writeFile(fileNameParts, data) {
const target = path.join(OUTPUT_DIR, ...fileNameParts);
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(path.join(target), data);
}
/**
* * Shuffles array in place.
* * @param {Array} a items An array containing the items.
* */
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
const DEFAULT_MTIME_CUTOFF = 130000;
async function main() {
let deadline = new Date().getTime() + 1000 * 230;
let timeoutToken = null;
const resetWatchdog = function () {
if (timeoutToken) {
clearTimeout(timeoutToken);
}
if (new Date().getTime() > deadline) {
console.warn("Deadline exceeded");
process.exit(1);
}
timeoutToken = setTimeout(() => {
console.warn("Unexpected timeout");
process.exit(1);
}, 25000);
};
resetWatchdog();
const conn = await createMajsoulConnection().catch((e) => {
clearTimeout(timeoutToken);
console.warn("Exiting in 500ms");
setTimeout(() => process.exit(1), 500);
return Promise.reject(e);
});
if (!conn) {
clearTimeout(timeoutToken);
// process.exit(0);
return;
}
const liveGames = {};
try {
if (process.env.LIST_ONLY) {
while (true) {
const startTime = Date.now();
for (const mode of shuffle(MODES)) {
resetWatchdog();
const resp = await conn.rpcCall(".lq.Lobby.fetchGameLiveList", {
filter_id: mode,
});
console.log(`Mode: ${mode}, Live: ${resp.live_list.length}`);
for (const game of resp.live_list) {
if (game.start_time < new Date().getTime() / 1000 - 60 * 60 * 5) {
// console.log(game.uuid, game.start_time, (new Date()).getTime() / 1000 - 60 * 60 * 5);
if (Math.random() > /*0.05*/ 0) {
continue;
}
}
liveGames[game.uuid] = game;
writeFile([game.uuid.split("-")[0], mode.toString(), game.uuid + ".json"], JSON.stringify(game));
}
}
if (!process.env.LIST_ONLY) {
break;
} else {
deadline += 60000;
}
const elapsed = Date.now() - startTime;
if (elapsed < 15000) {
const sleepTime = 15000 - elapsed;
console.log(`Sleeping for ${sleepTime}ms`);
await new Promise((resolve) => setTimeout(resolve, sleepTime));
}
/*
if (process.env.LIST_ONLY) {
// process.exit(0);
return;
}*/
}
}
const pendingPromises = [];
const recurseFillData = async function (dir) {
if (dir !== OUTPUT_DIR && !/^\d+$/.test(path.basename(dir))) {
return;
}
const entries = fs.readdirSync(dir, { withFileTypes: true });
const ts = new Date().getTime();
for (const ent of entries) {
try {
ent.mtimeDelta = fs.statSync(path.join(dir, ent.name)).mtimeMs - ts;
} catch (e) {
// Probably deleted by other process, this will be ignored below
ent.mtimeDelta = 0;
}
if (ent.isDirectory()) {
ent.sortKey = Math.random();
} else {
// Favor older games up to 3h, then sort randomly
ent.sortKey = Math.max(ent.mtimeDelta, -1000 * 60 * 60 * 3) + Math.random() * 10000;
}
}
// entries.sort((a, b) => a.sortKey - b.sortKey);
shuffle(entries);
const MTIME_CUTOFF = (parseInt(process.env.MTIME_CUTOFF, 10) || DEFAULT_MTIME_CUTOFF) * -1;
for (const ent of entries) {
if (ent.isDirectory()) {
await recurseFillData(path.join(dir, ent.name));
continue;
}
if (path.extname(ent.name) === ".json") {
const id = path.parse(ent.name).name;
if (id in liveGames) {
continue;
}
try {
ent.mtimeDelta = fs.statSync(path.join(dir, ent.name)).mtimeMs - ts;
} catch (e) {
// Probably deleted by other process
continue;
}
if (ent.mtimeDelta > MTIME_CUTOFF) {
// console.log("MTIME_CUTOFF", ent.name, ent.mtimeDelta, MTIME_CUTOFF);
if (MTIME_CUTOFF * -1 > DEFAULT_MTIME_CUTOFF) {
const resp = await conn.rpcCall(".lq.Lobby.fetchGameLiveInfo", {
game_uuid: id,
});
resetWatchdog();
if (resp.live_head) {
// console.log("Still running:", id);
try {
fs.statSync(path.join(dir, ent.name));
} catch (e) {
// Probably deleted by other process
continue;
}
fs.writeFileSync(path.join(dir, ent.name), JSON.stringify(resp.live_head));
process.stdout.write(".");
}
}
continue;
}
resetWatchdog();
try {
fs.statSync(path.join(dir, ent.name));
} catch (e) {
// Probably deleted by other process
continue;
}
const startTime = Date.now();
async function throttle() {
if (Date.now() - startTime < 1000) {
await new Promise((resolve) => setTimeout(resolve, 1000 - (Date.now() - startTime)));
}
}
const resp = await conn.rpcCall(".lq.Lobby.fetchGameRecord", {
game_uuid: id,
client_version_string: conn.clientVersionString,
});
if ((!resp.data && !resp.data_url) || !resp.head) {
let resp = await conn.rpcCall(".lq.Lobby.fetchOBToken", { uuid: id });
if (resp.token && resp.create_time && resp.create_time > Date.now() / 1000 - 60 * 60 * 6) {
console.log("OB:", id);
fs.utimesSync(path.join(dir, ent.name), new Date(), new Date(Date.now() + 1000 * 60 * 30));
await throttle();
continue;
}
resp = await conn.rpcCall(".lq.Lobby.fetchGameLiveInfo", { game_uuid: id });
if (resp.live_head) {
console.log("Still running:", id);
fs.writeFileSync(path.join(dir, ent.name), JSON.stringify(resp.live_head));
fs.utimesSync(path.join(dir, ent.name), new Date(), new Date(Date.now() + 1000 * 60 * 30));
} else {
let mtimeDeltaAdjusted = ent.mtimeDelta;
try {
const liveInfo = JSON.parse(fs.readFileSync(path.join(dir, ent.name), { encoding: "utf8" }));
if (liveInfo.start_time) {
mtimeDeltaAdjusted = liveInfo.start_time * 1000 - ts;
fs.utimesSync(
path.join(dir, ent.name),
new Date(),
new Date(
Date.now() + Math.min(Math.max(Math.abs(mtimeDeltaAdjusted), 1000 * 60 * 15), 1000 * 60 * 60)
)
);
}
} catch (e) {
// Ignore
}
console.log(resp.live_head, resp.error.code || resp.error, Math.round(mtimeDeltaAdjusted / 1000), id);
if (resp.error && resp.error.code === 1801 && mtimeDeltaAdjusted < -1000 * 60 * 60 * 24 * 3) {
console.log("Deleting...");
fs.unlink(path.join(dir, ent.name), () => {});
}
}
await throttle();
continue;
}
// pendingPromises.push(
await (async function ({ id, ent, resp, dir }) {
const recordData =
resp.data_url && (!resp.data || !resp.data.length)
? await rp({ uri: resp.data_url, encoding: null, timeout: 5000 }).catch(() =>
console.warn(`Failed to download data for ${id}:`, resp)
)
: resp.data;
if (!recordData || !recordData.length) {
console.log("No data:", id);
return;
}
console.log(resp.head.uuid);
const game = resp.head;
writeFile(
[
"records",
game.uuid.split("-")[0],
game.config.mode.mode.toString(),
(game.config.meta.mode_id || game.config.meta.contest_uid || game.config.meta.room_id).toString(),
game.uuid + ".json",
],
JSON.stringify(resp.head)
);
writeFile(
[
"records",
game.uuid.split("-")[0],
game.config.mode.mode.toString(),
(game.config.meta.mode_id || game.config.meta.contest_uid || game.config.meta.room_id).toString(),
game.uuid + ".recordData",
],
recordData
);
try {
fs.unlinkSync(path.join(dir, ent.name));
} catch (e) {
// console.warn("Error when deleting file: ", e);
}
})({ id, ent, resp, dir });
await throttle();
}
}
};
await recurseFillData(OUTPUT_DIR);
resetWatchdog();
if (pendingPromises.length > 0) {
await Promise.all(pendingPromises);
}
resetWatchdog();
deadline += 60000;
console.log("Sleeping...");
await new Promise((resolve) => setTimeout(resolve, 19000));
} finally {
conn.close();
clearTimeout(timeoutToken);
}
// process.exit(0);
}
if (require.main === module) {
wrappedRun(main);
}
// vim: sw=2:ts=2:expandtab:fdm=syntax