-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
751 lines (731 loc) · 24 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
const { wrappedMain, wrappedRun } = require("./entryPoint");
const rp = require("request-promise");
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const moment = require("moment");
const _ = require("lodash");
const compareVersion = require("node-version-compare");
const { DataStorage } = require("./storage");
const { createMajsoulConnection, fetchLatestDataDefinition } = require("./majsoul");
const { CouchStorage, MODE_GAME } = require("./couchStorage");
const { iterateLocalData, watchLiveData, DEFAULT_BASE } = require("./localData");
const { calcShanten } = require("./shanten");
const { MajsoulGameAnalyzer } = require("./gameAnalyzer");
CouchStorage.DEFAULT_MODE = MODE_GAME;
function convertGameInfo(raw) {
return {
modeId: raw.game_config.meta.mode_id,
uuid: raw.uuid,
startTime: raw.start_time,
players: raw.seat_list.map((accountId) => {
const player = raw.players.filter((x) => x.account_id === accountId)[0];
return {
accountId,
nickname: player.nickname,
level: player.level.id,
};
}),
};
}
function convertGameRecord(raw) {
return {
// modeId: raw.config.meta.mode_id,
uuid: raw.uuid,
/*
startTime: raw.start_time,
endTime: raw.end_time,
players: raw.accounts.map((account) => {
return {
accountId: account.account_id,
nickname: account.nickname,
level: account.level.id,
score: raw.result.players.filter(x => x.seat === account.seat)[0].part_point_1,
};
}),*/
};
}
async function fetchLiveGames(conn) {
const game216 = await conn.rpcCall(".lq.Lobby.fetchGameLiveList", {
filter_id: 216,
});
const game212 = await conn.rpcCall(".lq.Lobby.fetchGameLiveList", {
filter_id: 212,
});
return game216.live_list.concat(game212.live_list).map(convertGameInfo);
}
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
async function withRetry(func, num = 20, retryInterval = 30000) {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
return await func();
} catch (e) {
if (num <= 0 || e.status === 403 || e.noRetry) {
throw e;
}
console.log(e);
console.log(`Retrying (${num})`);
await new Promise((r) => setTimeout(r, Math.random() * retryInterval));
}
num--;
}
}
function buildRecordData({ data, dataDefinition, game }) {
const root = require("protobufjs").Root.fromJSON(dataDefinition);
const wrapper = root.nested.lq.Wrapper;
let msg, typeObj, payload;
try {
msg = wrapper.decode(data);
typeObj = root.lookupType(msg.name);
if (!typeObj || !typeObj.decode) {
console.log(game, msg, typeObj);
return null;
}
payload = typeObj.decode(msg.data);
} catch (e) {
console.log(game, msg, typeObj);
console.error(e);
return null;
}
const records = payload.version >= 210715 ? [] : payload.records;
if (payload.version >= 210715) {
for (const action of payload.actions) {
if (!action.result || !action.result.length) {
continue;
}
records.push(action.result);
}
}
assert(records.length);
const rounds = [];
let 振听 = null;
let numDiscarded = null;
let lastDiscardSeat = null;
let analyzer = null;
for (const itemBuf of records) {
let item;
let itemType;
let itemPayload;
try {
item = wrapper.decode(itemBuf);
itemType = root.lookupType(item.name);
itemPayload = itemType.decode(item.data);
} catch (e) {
console.log(game, item, itemType);
console.error(e);
return null;
}
if (item.name !== ".lq.RecordNewRound") {
assert(analyzer);
analyzer.processRecord(item.name, itemPayload);
}
if ([".lq.RecordDealTile"].includes(item.name)) {
continue;
}
if (item.name === ".lq.RecordNewRound") {
analyzer = new MajsoulGameAnalyzer(itemPayload);
assert([3, 4].includes(itemPayload.scores.length));
rounds.push(
[0, 1, 2, 3].slice(0, itemPayload.scores.length).map((seat) => ({
...(itemPayload[`tiles${seat}`].length === 14
? {
亲: true,
牌山: itemPayload.paishan,
}
: {}),
手牌: itemPayload[`tiles${seat}`],
起手向听: calcShanten(itemPayload[`tiles${seat}`]),
}))
);
振听 = Array(rounds[rounds.length - 1].length).fill(false);
numDiscarded = 0;
lastDiscardSeat = null;
assert(rounds[rounds.length - 1].filter((x) => x.亲).length === 1);
assert([3, 4].includes(rounds[rounds.length - 1].length));
continue;
}
const curRound = rounds[rounds.length - 1];
assert(curRound);
const numPlayers = curRound.length;
assert([3, 4].includes(numPlayers));
switch (item.name) {
case ".lq.RecordChiPengGang":
curRound[itemPayload.seat].副露 = (curRound[itemPayload.seat].副露 || 0) + 1;
break;
case ".lq.RecordDiscardTile":
// console.log(itemPayload);
lastDiscardSeat = itemPayload.seat;
振听 = itemPayload.zhenting; // Array of all players' status
if (!curRound[itemPayload.seat].立直 && (itemPayload.is_liqi || itemPayload.is_wliqi)) {
curRound[itemPayload.seat].立直 = numDiscarded / numPlayers + 1;
if (振听[itemPayload.seat]) {
curRound[itemPayload.seat].振听立直 = true;
}
if (itemPayload.tingpais && itemPayload.tingpais.length) {
curRound[itemPayload.seat].立直听牌 = itemPayload.tingpais.map((x) => x.tile);
curRound[itemPayload.seat].立直听牌残枚 = analyzer.getRemainingNumTiles(
itemPayload.seat,
itemPayload.tingpais.map((x) => x.tile)
);
}
}
if (itemPayload.is_wliqi) {
curRound[itemPayload.seat].W立直 = true;
}
numDiscarded++;
break;
case ".lq.RecordNoTile":
if (itemPayload.liujumanguan) {
itemPayload.scores.forEach((x) => (curRound[x.seat].流满 = true));
}
itemPayload.players.forEach((x, seat) => {
curRound[seat].流听 = x.tingpai;
});
break;
case ".lq.RecordHule":
itemPayload.hules.forEach((x) => {
curRound[x.seat].和 = [
itemPayload.delta_scores[x.seat] - (x.liqi ? 1000 : 0),
_.flatten(x.fans.map((x) => Array(x.val).fill(x.id))),
numDiscarded / numPlayers + 1,
];
if (!x.zimo && curRound[x.seat].和[0] < Math.max(0, x.point_rong - 1500)) {
// 一炮多响 + 包牌
console.log(itemPayload, game.uuid);
assert(itemPayload.hules.length >= 2);
const info = itemPayload.hules.filter((other) => other.yiman && other.seat !== x.seat)[0];
assert(info);
curRound[x.seat].和[0] += info.point_rong / 2;
curRound[x.seat].包牌 = info.point_rong / 2;
}
const numLosingPlayers = itemPayload.delta_scores.filter((x) => x < 0).length;
if (x.zimo) {
assert(itemPayload.hules.length === 1);
assert(numLosingPlayers === numPlayers - 1 || itemPayload.hules[0].yiman);
curRound[x.seat].自摸 = true;
if (振听[x.seat]) {
curRound[x.seat].振听自摸 = true;
}
if (numLosingPlayers === 1) {
itemPayload.delta_scores.forEach((score, seat) => {
if (score < 0) {
curRound[seat].包牌 = Math.abs(score);
}
});
}
} else {
assert([1, 2].includes(numLosingPlayers));
itemPayload.delta_scores.forEach((score, seat) => {
if (score < 0) {
if (numLosingPlayers === 1) {
assert(seat === lastDiscardSeat);
} else {
assert(itemPayload.hules.some((x) => x.yiman));
}
curRound[seat][seat === lastDiscardSeat ? "放铳" : "包牌"] = Math.abs(score);
}
});
}
});
break;
case ".lq.RecordBaBei":
case ".lq.RecordAnGangAddGang":
lastDiscardSeat = itemPayload.seat;
break;
case ".lq.RecordLiuJu":
curRound.forEach((x) => (x.途中流局 = itemPayload.type));
break;
default:
console.log(game.uuid);
console.log(item.name);
delete itemPayload.operation;
console.log(itemPayload);
assert(false);
}
}
return rounds;
}
async function processRecordDataForGameId(store, uuid, recordData, gameData, batch) {
const rawRecordInfo = {
...(gameData || (await withRetry(() => store.getRecordData(uuid)))),
data: recordData,
};
const rounds = buildRecordData(rawRecordInfo);
if (!rounds) {
console.error(`Corrupted data: ${uuid}`);
fs.mkdirSync(path.join(DEFAULT_BASE, "210101"), { recursive: true });
fs.writeFileSync(path.join(DEFAULT_BASE, "210101", uuid + ".json"), "");
fs.utimesSync(path.join(DEFAULT_BASE, "210101", uuid + ".json"), 1, 1);
const e = new Error(`Corrupted data: ${uuid}`);
e.noRetry = true;
throw e;
}
// console.log(rawRecordInfo.game.uuid);
await withRetry(() => store.saveRoundData(rawRecordInfo.game, rounds, batch));
}
async function processGames(conn, ids, storageParams = {}, gamePostprocess = (game) => game) {
const store = new CouchStorage(storageParams);
let filteredIds = [];
while (ids.length) {
filteredIds = filteredIds.concat(await store.findNonExistentRecords(ids.slice(0, 100)));
ids = ids.slice(100);
}
ids = filteredIds;
if (!ids.length) {
return;
}
ids.sort();
for (const id of ids) {
console.log(id);
let resp;
try {
resp = await conn.rpcCall(".lq.Lobby.fetchGameRecord", {
game_uuid: id,
client_version_string: conn.clientVersionString,
});
} catch (e) {
console.log(e);
console.log("Reconnecting");
await new Promise((r) => setTimeout(r, 1000));
// eslint-disable-next-line require-atomic-updates
conn.reconnect();
await conn.waitForReady();
resp = await conn.rpcCall(".lq.Lobby.fetchGameRecord", {
game_uuid: id,
client_version_string: conn.clientVersionString,
});
}
if (!resp.data_url && !(resp.data && resp.data.length)) {
console.log(`No data in response: ${id}`);
continue;
}
const recordData = resp.data_url
? await withRetry(() => rp({ uri: resp.data_url, encoding: null, timeout: 5000 }))
: resp.data;
console.log("Saving");
// const compressedRecordData = await store.compressData(recordData);
// await withRetry(() => dataStore.setRaw(`recordData/${id}.lzma2`, compressedRecordData));
const game = gamePostprocess(resp.head);
assert(game);
await withRetry(() => store.saveGame(game, conn._codec.version));
await withRetry(() => store.ensureDataDefinition(conn._codec.version, conn._codec.rawDefinition));
await withRetry(() =>
processRecordDataForGameId(store, id, recordData, { game: game, dataDefinition: conn._codec.rawDefinition })
);
await new Promise((r) => setTimeout(r, 1000));
}
await store.triggerViewRefresh();
}
async function syncToCouchDb() {
const storage = new DataStorage();
const store = new CouchStorage();
const latestDoc = await store.getLatestRecord();
let date = moment.unix(latestDoc.start_time).subtract(36, "hours").startOf("day");
const conn = await createMajsoulConnection();
if (!conn) {
return;
}
try {
while (date.isSameOrBefore(moment())) {
const ids = Object.keys(await storage.get(`records/${date.format("YYMMDD")}.json`));
await processGames(conn, ids);
date = date.add(1, "day");
}
} finally {
conn.close();
}
await store.triggerViewRefresh();
}
async function loadLocalData() {
const store = new CouchStorage();
const dataDefs = await store._db.allDocs({
include_docs: true,
startkey: "dataDefinition-",
endkey: "dataDefinition-\uffff",
});
const ver = dataDefs.rows
.map((x) => x.doc.version)
.sort(compareVersion)
.reverse()[0];
const dataDefinition = dataDefs.rows.filter((x) => x.doc.version === ver)[0].doc.defintion;
const groups = {
normal: {
store,
items: [],
},
gold: {
store: new CouchStorage({ suffix: "_gold" }),
items: [],
},
sanma: {
store: new CouchStorage({ suffix: "_sanma" }),
items: [],
},
e4: {
store: new CouchStorage({ suffix: "_e4" }),
items: [],
},
e3: {
store: new CouchStorage({ suffix: "_e3" }),
items: [],
},
};
const processLoadedData = async function () {
for (const group of Object.values(groups)) {
let items = group.items;
group.items = [];
const itemStore = group.store;
const filteredItems = [];
while (items.length) {
const chunk = items.slice(0, 100);
items = items.slice(100);
const filteredIds = process.env.FORCE_LOAD
? new Set(chunk.map((x) => x.data.uuid))
: new Set((await itemStore.findNonExistentRecordsFast(chunk.map((x) => x.data))).map((x) => x.uuid));
for (const item of chunk) {
if (filteredIds.has(item.data.uuid)) {
filteredItems.push(item);
}
}
}
if (!filteredItems.length) {
continue;
}
for (const item of filteredItems) {
if (item.id === "200207-56f99098-5bae-4d19-a8e6-0ce03246e02a") {
// Skip buggy game
continue;
}
console.log(`Saving ${item.id}`);
const recordData = item.getRecordData();
await withRetry(() => itemStore.saveGame(item.data, ver, true));
await withRetry(() =>
processRecordDataForGameId(itemStore, item.id, recordData, { game: item.data, dataDefinition }, true)
);
}
await itemStore.triggerViewRefresh();
}
};
await iterateLocalData(async function (item) {
try {
item.data = item.getData();
} catch (e) {
console.error(`Failed to parse ${item.id}:`, e);
return;
}
if (item.data.config.category !== 2) {
return;
}
if ([12, 16].includes(item.data.config.meta.mode_id)) {
groups.normal.items.push(item);
} else if ([9].includes(item.data.config.meta.mode_id)) {
groups.gold.items.push(item);
} else if ([22, 24, 26].includes(item.data.config.meta.mode_id)) {
groups.sanma.items.push(item);
} else if ([15, 11, 8].includes(item.data.config.meta.mode_id)) {
groups.e4.items.push(item);
} else if ([25, 23, 21].includes(item.data.config.meta.mode_id)) {
groups.e3.items.push(item);
} else {
console.log(`Unknown mode ${item.data.config.meta.mode_id}, skipping ${item.id}`);
}
if (Object.values(groups).some((x) => x.items.length > 1000)) {
await processLoadedData();
}
});
await new Promise((res) => setTimeout(res, 3000));
await processLoadedData();
}
async function loadLiveData() {
const store = new CouchStorage();
const dataDefs = await store._db.allDocs({
include_docs: true,
startkey: "dataDefinition-",
endkey: "dataDefinition-\uffff",
});
const ver = dataDefs.rows
.map((x) => x.doc.version)
.sort(compareVersion)
.reverse()[0];
let dataDefinition = dataDefs.rows.filter((x) => x.doc.version === ver)[0].doc.defintion;
let dataDefinitionVersion = ver;
async function updateDataDefintion() {
const result = await fetchLatestDataDefinition();
dataDefinition = result.dataDefinition;
dataDefinitionVersion = result.version;
}
updateDataDefintion().catch((e) => console.error(e));
setInterval(() => updateDataDefintion().catch((e) => console.error(e)), 1000 * 60 * 60);
const groups = {
normal: {
store,
},
gold: {
store: new CouchStorage({ suffix: "_gold" }),
},
sanma: {
store: new CouchStorage({ suffix: "_sanma" }),
},
e4: {
store: new CouchStorage({ suffix: "_e4" }),
},
e3: {
store: new CouchStorage({ suffix: "_e3" }),
},
};
let i = 0;
await watchLiveData(async function (item) {
(async function () {
try {
item.data = item.getData();
} catch (e) {
console.error(`Failed to parse ${item.id}:`, e);
return;
}
if (item.data.config.category !== 2) {
return;
}
let itemStore;
if ([12, 16].includes(item.data.config.meta.mode_id)) {
itemStore = groups.normal.store;
} else if ([9].includes(item.data.config.meta.mode_id)) {
itemStore = groups.gold.store;
} else if ([22, 24, 26].includes(item.data.config.meta.mode_id)) {
itemStore = groups.sanma.store;
} else if ([15, 11, 8].includes(item.data.config.meta.mode_id)) {
itemStore = groups.e4.store;
} else if ([25, 23, 21].includes(item.data.config.meta.mode_id)) {
itemStore = groups.e3.store;
}
if (!itemStore) {
console.log(`Unknown mode ${item.data.config.meta.mode_id}, skipping ${item.id}`);
return;
}
console.log(`Saving ${item.data.config.meta.mode_id} ${item.id}`);
const recordData = item.getRecordData();
await withRetry(() => itemStore.ensureDataDefinition(dataDefinitionVersion, dataDefinition));
await withRetry(() => itemStore.saveGame(item.data, ver, true));
await withRetry(() =>
processRecordDataForGameId(itemStore, item.id, recordData, { game: item.data, dataDefinition }, true)
);
if (i > 100) {
i = 0;
if (global.gc) {
global.gc();
}
}
})().catch((e) => console.error(item.id, e));
});
}
async function syncContest(contestId, dbSuffix) {
const conn = await createMajsoulConnection();
if (!conn) {
return;
}
try {
let resp = await conn.rpcCall(".lq.Lobby.fetchCustomizedContestByContestId", {
contest_id: contestId,
});
const realId = resp.contest_info.unique_id;
let nextIndex = undefined;
console.log(`${contestId} ${resp.contest_info.unique_id} ${resp.contest_info.contest_name}`);
const idLog = {};
while (true) {
resp = await conn.rpcCall(".lq.Lobby.fetchCustomizedContestGameRecords", {
unique_id: realId,
last_index: nextIndex,
});
for (const game of resp.record_list) {
if (game.result.players.length < 4) {
continue;
}
idLog[game.uuid] = true;
}
if (!resp.next_index || !resp.record_list.length) {
break;
}
nextIndex = resp.next_index;
}
await processGames(conn, Object.keys(idLog), { suffix: dbSuffix }, (game) => {
for (let i = 0; i < 4; i++) {
if (!game.accounts.some((x) => x.seat === i)) {
console.log(`${game.uuid} ${i} Computer`);
game.accounts.push({
seat: i,
nickname: "电脑",
account_id: 1,
level: {
id: 10301,
score: 1,
},
level3: {
id: 20301,
score: 1,
},
});
}
game.accounts.sort((a, b) => a.seat - b.seat);
}
return game;
});
} finally {
conn.close();
}
}
async function syncContest2() {
const dbSuffix = "_kanraku";
const conn = await createMajsoulConnection();
if (!conn) {
return;
}
try {
const ids = [];
await processGames(conn, ids, { suffix: dbSuffix }, (game) => {
for (let i = 0; i < 4; i++) {
if (!game.accounts.some((x) => x.seat === i)) {
console.log(`${game.uuid} ${i} Computer`);
game.accounts.push({
seat: i,
nickname: "电脑",
account_id: 1,
level: {
id: 10301,
score: 1,
},
level3: {
id: 20301,
score: 1,
},
});
}
game.accounts.sort((a, b) => a.seat - b.seat);
}
return game;
});
} finally {
conn.close();
}
}
async function main() {
if (process.env.EXTERNAL_AGGREGATION) {
throw new Error("Moved to separate script");
}
if (process.env.SYNC_COUCHDB) {
return await syncToCouchDb();
}
if (process.env.LOAD_LOCAL_DATA) {
return await loadLocalData();
}
if (process.env.LOAD_LIVE_DATA) {
return await loadLiveData();
}
if (process.env.SYNC_CONTEST) {
// return await syncContest(511652, "_jinja");
/*
await syncContest(903356, "_jinja");
await syncContest(525988, "_dd");
await syncContest(525988, "_t1");
await syncContest(251710, "_dd");
await syncContest(251710, "_t2");
await syncContest(536020, "_dd");
await syncContest(536020, "_crt");*/
await syncContest(605833, "_jinja");
await syncContest(941168, "_s5");
await syncContest(601462, "_s5sec");
// await syncContest(550658, "_sisousen");
// await syncContest(609440, "_sisousen");
// await syncContest(251630, "_souseisen");
// await syncContest(943107, "_tenten");
// await syncContest(792848, "_oshoji");
// await syncContest(364951, "_ein");
// await syncContest(461591, "_s4");
// await syncContest(960829, "_s4sec");
// await syncContest(525988, "_s3");
// await syncContest(222713, "_s3sec");
// await syncContest(689169, "_u");
await syncContest(205542, "_u");
// await syncContest(831675, "_xjtu");
// await syncContest(483861, "_xjtu");
await syncContest(672376, "_xjtu");
// await syncContest(570924, "_throne");
// await syncContest(575549, "_kanraku");
// await syncContest2();
return;
}
if (process.env.UPDATE_AGV) {
throw new Error("Moved to separate script");
}
const storage = new DataStorage();
const oldLiveGames = await storage.get("live.json");
let pendingIds = await storage.get("pending_ids.json", []);
const conn = await createMajsoulConnection();
if (!conn) {
return;
}
try {
/*
resp = await conn.rpcCall(".lq.Lobby.fetchGameRecord", {
game_uuid: "190823-64f22e47-7a34-4720-977f-2767eae35700",
});
const details = proto.decodeDataMessage(resp.data);
const lastOp = details.payload.records[details.payload.records.length - 1];
resp = await conn.rpcCall(".lq.Lobby.fetchGameRecordsDetail", {
uuid_list: ["190823-64f22e47-7a34-4720-977f-2767eae35700"],
});
*/
const liveGames = await fetchLiveGames(conn);
const newGameIds = new Set(liveGames.map((x) => x.uuid));
const finishedGameIds = oldLiveGames.map((x) => x.uuid).filter((x) => !newGameIds.has(x));
pendingIds = Array.from(new Set(finishedGameIds.concat(pendingIds)));
await storage.set("pending_ids.json", pendingIds);
let gameRecords = [];
if (pendingIds.length > 0) {
const resp = await conn.rpcCall(".lq.Lobby.fetchGameRecordsDetail", {
uuid_list: pendingIds,
});
gameRecords = resp.record_list.map(convertGameRecord);
const groupedRecords = groupBy(gameRecords, (x) => x.uuid.split("-")[0]);
for (const [time, records] of groupedRecords) {
const fileName = `records/${time}.json`;
const existingRecords = await storage.get(fileName, {});
for (const record of records) {
existingRecords[record.uuid] = record;
}
await storage.set(fileName, existingRecords);
}
}
const newRecordsIdSet = new Set(gameRecords.map((x) => x.uuid));
pendingIds.sort();
await storage.set(
"pending_ids.json",
pendingIds.filter((x) => !newRecordsIdSet.has(x))
);
await storage.set("live.json", liveGames);
if (newRecordsIdSet.size > 0) {
await processGames(conn, Array.from(newRecordsIdSet));
}
return `${gameRecords.length} records saved`;
} finally {
conn.close();
}
}
if (require.main === module) {
wrappedRun(main);
} else {
exports["amae-koromo"] = wrappedMain(main);
exports.processRecordDataForGameId = processRecordDataForGameId;
}
// vim: sw=2:ts=2:expandtab:fdm=syntax