gistfile1.txt
· 2.7 KiB · Text
Неформатований
function buildRows(historyKey: TGKey): ResultRow[] {
const { slot, date } = TGKey.parse(historyKey);
let players: UserKey[] = Attendance.players(historyKey);
if (players.length === 0) {
const history = Store.read<{ scores: TGScore[] }>(TGKey.toHistoryPath(historyKey));
if (history?.scores) {
players = [...new Set(history.scores.map((s: TGScore) => s.userKey))];
}
}
const weekKey = WRank.weekKey(new Date(date));
const addedUsers = new Set<UserKey>();
const rows: ResultRow[] = [];
for (const userKey of players) {
const chars = CharacterRegistry.forUser(userKey);
for (const char of chars) {
const score = Score.get({ character: char, slot, historyKey });
if (!score) continue;
const wrEntry = WRank.entry(char.name, char.nation, weekKey);
const position = score.wRankAtSubmission
? {
currentRank: score.wRankAtSubmission.rank,
previousRank: score.wRankAtSubmission.rank - score.wRankAtSubmission.delta,
}
: wrEntry
? { currentRank: wrEntry.currentRank, previousRank: wrEntry.previousRank }
: undefined;
rows.push({
character: char,
score,
position,
leavesCount: Leaves.countForChar({ characterName: char.name }),
});
addedUsers.add(userKey);
break;
}
if (!addedUsers.has(userKey)) {
const history = Store.read<{ scores: TGScore[] }>(TGKey.toHistoryPath(historyKey));
const score = history?.scores.find((s: TGScore) => s.userKey === userKey);
if (score) {
const foundChar = CharacterRegistry.find(score.characterName);
const classKey = (typeof score.class === "object"
? (score.class as any)?.key
: score.class) as ClassKey;
const char: Character = foundChar ?? {
name: score.characterName,
class: CLASSES[classKey] ?? { key: classKey, name: classKey, shortName: classKey },
level: 0,
nation: score.nation,
ownerKey: score.userKey,
};
const wrEntry = WRank.entry(char.name, char.nation, weekKey);
const position = score.wRankAtSubmission
? {
currentRank: score.wRankAtSubmission.rank,
previousRank: score.wRankAtSubmission.rank - score.wRankAtSubmission.delta,
}
: wrEntry
? { currentRank: wrEntry.currentRank, previousRank: wrEntry.previousRank }
: undefined;
rows.push({ character: char, score, position, leavesCount: 0 });
addedUsers.add(userKey);
}
}
}
return rows;
}
| 1 | function buildRows(historyKey: TGKey): ResultRow[] { |
| 2 | const { slot, date } = TGKey.parse(historyKey); |
| 3 | let players: UserKey[] = Attendance.players(historyKey); |
| 4 | |
| 5 | if (players.length === 0) { |
| 6 | const history = Store.read<{ scores: TGScore[] }>(TGKey.toHistoryPath(historyKey)); |
| 7 | if (history?.scores) { |
| 8 | players = [...new Set(history.scores.map((s: TGScore) => s.userKey))]; |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | const weekKey = WRank.weekKey(new Date(date)); |
| 13 | const addedUsers = new Set<UserKey>(); |
| 14 | const rows: ResultRow[] = []; |
| 15 | |
| 16 | for (const userKey of players) { |
| 17 | const chars = CharacterRegistry.forUser(userKey); |
| 18 | |
| 19 | for (const char of chars) { |
| 20 | const score = Score.get({ character: char, slot, historyKey }); |
| 21 | if (!score) continue; |
| 22 | |
| 23 | const wrEntry = WRank.entry(char.name, char.nation, weekKey); |
| 24 | const position = score.wRankAtSubmission |
| 25 | ? { |
| 26 | currentRank: score.wRankAtSubmission.rank, |
| 27 | previousRank: score.wRankAtSubmission.rank - score.wRankAtSubmission.delta, |
| 28 | } |
| 29 | : wrEntry |
| 30 | ? { currentRank: wrEntry.currentRank, previousRank: wrEntry.previousRank } |
| 31 | : undefined; |
| 32 | |
| 33 | rows.push({ |
| 34 | character: char, |
| 35 | score, |
| 36 | position, |
| 37 | leavesCount: Leaves.countForChar({ characterName: char.name }), |
| 38 | }); |
| 39 | addedUsers.add(userKey); |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | if (!addedUsers.has(userKey)) { |
| 44 | const history = Store.read<{ scores: TGScore[] }>(TGKey.toHistoryPath(historyKey)); |
| 45 | const score = history?.scores.find((s: TGScore) => s.userKey === userKey); |
| 46 | if (score) { |
| 47 | const foundChar = CharacterRegistry.find(score.characterName); |
| 48 | const classKey = (typeof score.class === "object" |
| 49 | ? (score.class as any)?.key |
| 50 | : score.class) as ClassKey; |
| 51 | const char: Character = foundChar ?? { |
| 52 | name: score.characterName, |
| 53 | class: CLASSES[classKey] ?? { key: classKey, name: classKey, shortName: classKey }, |
| 54 | level: 0, |
| 55 | nation: score.nation, |
| 56 | ownerKey: score.userKey, |
| 57 | }; |
| 58 | const wrEntry = WRank.entry(char.name, char.nation, weekKey); |
| 59 | const position = score.wRankAtSubmission |
| 60 | ? { |
| 61 | currentRank: score.wRankAtSubmission.rank, |
| 62 | previousRank: score.wRankAtSubmission.rank - score.wRankAtSubmission.delta, |
| 63 | } |
| 64 | : wrEntry |
| 65 | ? { currentRank: wrEntry.currentRank, previousRank: wrEntry.previousRank } |
| 66 | : undefined; |
| 67 | |
| 68 | rows.push({ character: char, score, position, leavesCount: 0 }); |
| 69 | addedUsers.add(userKey); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return rows; |
| 75 | } |