Skip to content

Commit 0c76d5a

Browse files
committed
games: Allow games to count for ScrabbleDex if it ended by passing but at least 20 words were played
1 parent 82e839c commit 0c76d5a

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/database/games.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,26 @@ export async function getScrabbleDex(): Promise<ScrabbleDexEntry[] | null> {
118118
const winCtx = game.winCtx as ScrabbleWinCtx | undefined;
119119
const winners = winCtx?.type === 'win' ? winCtx.winnerIds : [];
120120
const logs = game.log.map<ScrabbleLog>(log => JSON.parse(log));
121-
if (winCtx?.type === 'dq') {
121+
if (winCtx?.type === 'dq' || winCtx?.type === 'regular') {
122122
const leftUsers = logs.filter(log => log.action === 'dq' || log.action === 'forfeit').map(log => log.turn);
123-
winners.push(
124-
...Object.values(game.players)
125-
.map(player => player.turn)
126-
.filter(player => !leftUsers.includes(player))
127-
);
123+
if (winCtx.type === 'dq')
124+
winners.push(
125+
...Object.values(game.players)
126+
.map(player => player.turn)
127+
.filter(player => !leftUsers.includes(player))
128+
);
129+
else if (winCtx.type === 'regular' && logs.filter(log => log.action === 'play').length > 20) {
130+
const points: Record<string, number> = {};
131+
logs.forEach(log => {
132+
if (log.action === 'play') {
133+
points[log.turn] ??= 0;
134+
points[log.turn] += log.ctx.points.total;
135+
}
136+
});
137+
const players = Object.entries(points).filter(([player]) => !leftUsers.includes(player));
138+
const maxPoints = Math.max(...players.map(([_player, score]) => score));
139+
winners.push(...players.filter(([_player, score]) => score === maxPoints).map(([player]) => player));
140+
}
128141
}
129142
return logs
130143
.filterMap<ScrabbleDexEntry[]>(log => {

src/ps/games/scrabble/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export type RenderCtx = {
4848
turn: string;
4949
selected?: Point | null;
5050
};
51-
export type WinCtx = { type: 'win'; winnerIds: string[]; score: State['score'] } | { type: 'draw' } | { type: 'dq' };
51+
export type WinCtx =
52+
| { type: 'win'; winnerIds: string[]; score: State['score'] }
53+
| { type: 'draw' }
54+
| { type: 'dq' }
55+
| { type: 'regular' };
5256

5357
export type Word = { word: string; baseScore: number; bonuses: BonusReducer[] };
5458
export type WordScore = [times: number, plus: number];

0 commit comments

Comments
 (0)