INNER CODE UNIT · TypeScript
topScore
ekzhang/setwithfriends · functions/src/index.ts:125
const topScore = Math.max(...Object.values(scores));
// Update statistics for all users
const userStats: Record<string, any> = {};
await Promise.all(
players.map(async (player) => {
const result: TransactionResult = await getDatabase()
.ref(`userStats/${player}/${gameMode}/${variant}`)
.transaction((stats) => {
stats ??= {}; // tslint:disable-line no-parameter-reassignment
stats.finishedGames = (stats.finishedGames ?? 0) + 1;
stats.totalSets = (stats.totalSets ?? 0) + scores[player];
if (scores[player] === topScore) {
stats.wonGames = (stats.wonGames ?? 0) + 1;
stats.fastestTime = Math.min(stats.fastestTime ?? Infinity, time);
}
stats.totalTime = (stats.totalTime ?? 0) + time;
return stats;