Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 48 additions & 57 deletions src/database/games.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Temporal } from '@js-temporal/polyfill';
import mongoose, { type HydratedDocument } from 'mongoose';
import { pokedex } from 'ps-client/data';

import { IS_ENABLED } from '@/enabled';
import { ScrabbleMods } from '@/ps/games/scrabble/constants';
import { GamesList } from '@/ps/games/types';
import { UGO_2025_END, UGO_2025_START } from '@/ps/ugo/constants';
import { instantInRange } from '@/utils/timeInRange';
import { toId } from '@/utils/toId';

import type { Log as ScrabbleLog } from '@/ps/games/scrabble/logs';
Expand Down Expand Up @@ -116,59 +113,53 @@ export type ScrabbleDexEntry = {
export async function getScrabbleDex(): Promise<ScrabbleDexEntry[] | null> {
if (!IS_ENABLED.DB) return null;
const scrabbleGames = await model.find({ game: GamesList.Scrabble, mod: [ScrabbleMods.CRAZYMONS, ScrabbleMods.POKEMON] }).lean();
return scrabbleGames
.filter(game => {
const time = Temporal.Instant.fromEpochMilliseconds(game.created.getTime());
return instantInRange(time, [UGO_2025_START, UGO_2025_END]);
})
.flatMap(game => {
const baseCtx = { gameId: game.id, mod: game.mod! };
const winCtx = game.winCtx as ScrabbleWinCtx | undefined;
const winners = winCtx?.type === 'win' ? winCtx.winnerIds : [];
const logs = game.log.map<ScrabbleLog>(log => JSON.parse(log));
if (winCtx?.type === 'dq' || winCtx?.type === 'regular') {
const leftUsers = logs.filter(log => log.action === 'dq' || log.action === 'forfeit').map(log => log.turn);
if (winCtx.type === 'dq')
winners.push(
...Object.values(game.players)
.map(player => player.turn)
.filter(player => !leftUsers.includes(player))
);
else if (winCtx.type === 'regular' && logs.filter(log => log.action === 'play').length > 20) {
const points: Record<string, number> = {};
logs.forEach(log => {
if (log.action === 'play') {
points[log.turn] ??= 0;
points[log.turn] += log.ctx.points.total;
}
});
const players = Object.entries(points).filter(([player]) => !leftUsers.includes(player));
const maxPoints = Math.max(...players.map(([_player, score]) => score));
winners.push(...players.filter(([_player, score]) => score === maxPoints).map(([player]) => player));
}
return scrabbleGames.flatMap(game => {
const baseCtx = { gameId: game.id, mod: game.mod! };
const winCtx = game.winCtx as ScrabbleWinCtx | undefined;
const winners = winCtx?.type === 'win' ? winCtx.winnerIds : [];
const logs = game.log.map<ScrabbleLog>(log => JSON.parse(log));
if (winCtx?.type === 'dq' || winCtx?.type === 'regular') {
const leftUsers = logs.filter(log => log.action === 'dq' || log.action === 'forfeit').map(log => log.turn);
if (winCtx.type === 'dq')
winners.push(
...Object.values(game.players)
.map(player => player.turn)
.filter(player => !leftUsers.includes(player))
);
else if (winCtx.type === 'regular' && logs.filter(log => log.action === 'play').length > 20) {
const points: Record<string, number> = {};
logs.forEach(log => {
if (log.action === 'play') {
points[log.turn] ??= 0;
points[log.turn] += log.ctx.points.total;
}
});
const players = Object.entries(points).filter(([player]) => !leftUsers.includes(player));
const maxPoints = Math.max(...players.map(([_player, score]) => score));
winners.push(...players.filter(([_player, score]) => score === maxPoints).map(([player]) => player));
}
return logs
.filterMap<ScrabbleDexEntry[]>(log => {
if (log.action !== 'play') return;
const words = Object.keys(log.ctx.words).map(toId).unique();
return words.filterMap<ScrabbleDexEntry>(word => {
if (!(word in pokedex)) return;
let mon = pokedex[word];
if (mon.baseSpecies) mon = pokedex[toId(mon.baseSpecies)];
if (mon.num <= 0) return;
return {
...baseCtx,
pokemon: word,
pokemonName: mon.name,
num: mon.num,
by: log.turn,
byName: game.players[log.turn]?.name ?? null,
at: log.time,
won: winners.includes(log.turn),
};
});
})
.flat();
})
.filter(entry => entry.won);
}
return logs
.filterMap<ScrabbleDexEntry[]>(log => {
if (log.action !== 'play') return;
const words = Object.keys(log.ctx.words).map(toId).unique();
return words.filterMap<ScrabbleDexEntry>(word => {
if (!(word in pokedex)) return;
let mon = pokedex[word];
if (mon.baseSpecies) mon = pokedex[toId(mon.baseSpecies)];
if (mon.num <= 0) return;
return {
...baseCtx,
pokemon: word,
pokemonName: mon.name,
num: mon.num,
by: log.turn,
byName: game.players[log.turn]?.name ?? null,
at: log.time,
won: winners.includes(log.turn),
};
});
})
.flat();
});
}
Loading