Last active 3 weeks ago

nuno revised this gist 3 weeks ago. Go to revision

1 file changed, 62 insertions

gistfile1.txt(file created)

@@ -0,0 +1,62 @@
1 + submit({ character, borrowedFrom, pts, k, d, atk, def, heal, slot, submittedByOfficer }: {
2 + character: Character;
3 + borrowedFrom?: UserKey;
4 + pts: number;
5 + k?: number;
6 + d?: number;
7 + atk?: number;
8 + def?: number;
9 + heal?: number;
10 + slot: SlotHour;
11 + submittedByOfficer?: boolean;
12 + }): void {
13 + const date = new Date().toISOString().slice(0, 10);
14 + const historyKey = `${date}-${slot}` as HistoryKey;
15 + const history = loadHistory(historyKey);
16 +
17 + // Snapshot W.Rank before recording score
18 + const existingEntry = WRank.entry(character.name, character.nation);
19 + const wRankAtSubmission = existingEntry ? {
20 + rank: existingEntry.currentRank,
21 + delta: existingEntry.currentRank - (existingEntry.previousRank ?? existingEntry.currentRank),
22 + } : undefined;
23 +
24 + const score: TGScore = {
25 + userKey: character.ownerKey,
26 + playedBy: borrowedFrom,
27 + characterName: character.name,
28 + class: character.class.key,
29 + nation: character.nation,
30 + pts,
31 + k,
32 + d,
33 + atk,
34 + def,
35 + heal,
36 + submittedAt: new Date().toISOString(),
37 + slot,
38 + date,
39 + submittedByOfficer: submittedByOfficer ?? false,
40 + wRankAtSubmission,
41 + };
42 +
43 + // Upsert — replace existing score for same character/slot
44 + history.scores = history.scores.filter(
45 + (s) => !(s.userKey === character.ownerKey &&
46 + s.characterName === character.name &&
47 + s.slot === slot &&
48 + s.date === date)
49 + );
50 + history.scores.push(score);
51 + saveHistory(historyKey, history);
52 +
53 + // Record in W.Rank
54 + WRank.recordScore(
55 + character.ownerKey,
56 + character.name,
57 + character.class.key,
58 + character.nation,
59 + pts,
60 + historyKey
61 + );
62 + },
Newer Older