最后活跃于 1 week ago

gistfile1.txt 原始文件
1 /**
2 * Submit a score for a character.
3 * Handles W.Rank snapshot at submission time.
4 */
5 async submit({ character, borrowedFrom, pts, k, d, atk, def, heal, slot, submittedByOfficer }: {
6 character: Character;
7 borrowedFrom?: UserKey;
8 pts: number;
9 k?: number;
10 d?: number;
11 atk?: number;
12 def?: number;
13 heal?: number;
14 slot: SlotHour;
15 submittedByOfficer?: boolean;
16 }): Promise<void> {
17 const date = new Date().toISOString().slice(0, 10);
18 const historyKey = TGKey.current({ slot });
19 const history = loadHistory(historyKey);
20
21 // Snapshot W.Rank before recording score
22 const existingEntry = WRank.entry(character.name, character.nation);
23 const wRankAtSubmission = existingEntry ? {
24 rank: existingEntry.currentRank,
25 delta: existingEntry.currentRank - (existingEntry.previousRank ?? existingEntry.currentRank),
26 } : undefined;
27
28 const score: TGScore = {
29 userKey: character.ownerKey,
30 playedBy: borrowedFrom,
31 characterName: character.name,
32 class: character.class.key,
33 nation: character.nation,
34 pts,
35 k,
36 d,
37 atk,
38 def,
39 heal,
40 submittedAt: new Date().toISOString(),
41 slot,
42 date,
43 submittedByOfficer: submittedByOfficer ?? false,
44 wRankAtSubmission,
45 };
46
47 // Upsert — replace existing score for same character/slot
48 history.scores = history.scores.filter(
49 (s) => !(s.userKey === character.ownerKey &&
50 s.characterName === character.name &&
51 s.slot === slot &&
52 s.date === date)
53 );
54 history.scores.push(score);
55 saveHistory(historyKey, history);
56
57 // Record in W.Rank
58 WRank.recordScore(
59 character.ownerKey,
60 character.name,
61 character.class.key,
62 character.nation,
63 pts,
64 historyKey
65 );
66 await RuntimeEvents.emit("scoreSubmitted", { historyKey, character });
67 },