gistfile1.txt
· 2.1 KiB · Text
Неформатований
/**
* Submit a score for a character.
* Handles W.Rank snapshot at submission time.
*/
async submit({ character, borrowedFrom, pts, k, d, atk, def, heal, slot, submittedByOfficer }: {
character: Character;
borrowedFrom?: UserKey;
pts: number;
k?: number;
d?: number;
atk?: number;
def?: number;
heal?: number;
slot: SlotHour;
submittedByOfficer?: boolean;
}): Promise<void> {
const date = new Date().toISOString().slice(0, 10);
const historyKey = TGKey.current({ slot });
const history = loadHistory(historyKey);
// Snapshot W.Rank before recording score
const existingEntry = WRank.entry(character.name, character.nation);
const wRankAtSubmission = existingEntry ? {
rank: existingEntry.currentRank,
delta: existingEntry.currentRank - (existingEntry.previousRank ?? existingEntry.currentRank),
} : undefined;
const score: TGScore = {
userKey: character.ownerKey,
playedBy: borrowedFrom,
characterName: character.name,
class: character.class.key,
nation: character.nation,
pts,
k,
d,
atk,
def,
heal,
submittedAt: new Date().toISOString(),
slot,
date,
submittedByOfficer: submittedByOfficer ?? false,
wRankAtSubmission,
};
// Upsert — replace existing score for same character/slot
history.scores = history.scores.filter(
(s) => !(s.userKey === character.ownerKey &&
s.characterName === character.name &&
s.slot === slot &&
s.date === date)
);
history.scores.push(score);
saveHistory(historyKey, history);
// Record in W.Rank
WRank.recordScore(
character.ownerKey,
character.name,
character.class.key,
character.nation,
pts,
historyKey
);
await RuntimeEvents.emit("scoreSubmitted", { historyKey, character });
},
| 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 | }, |