Skip to content

Commit ca3bb98

Browse files
committed
games: Update ScrabbleDex to detect DQ-win games as wins
1 parent a1e3642 commit ca3bb98

5 files changed

Lines changed: 41 additions & 27 deletions

File tree

src/database/games.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { Log as ScrabbleLog } from '@/ps/games/scrabble/logs';
1313
import type { WinCtx as ScrabbleWinCtx } from '@/ps/games/scrabble/types';
1414
import type { Player } from '@/ps/games/types';
1515

16-
const schema = new mongoose.Schema({
16+
const schema = new mongoose.Schema<GameModel>({
1717
id: {
1818
type: String,
1919
required: true,
@@ -44,6 +44,7 @@ const schema = new mongoose.Schema({
4444
type: String,
4545
required: true,
4646
},
47+
out: Boolean,
4748
},
4849
required: true,
4950
},
@@ -117,6 +118,14 @@ export async function getScrabbleDex(): Promise<ScrabbleDexEntry[] | null> {
117118
const winCtx = game.winCtx as ScrabbleWinCtx | undefined;
118119
const winners = winCtx?.type === 'win' ? winCtx.winnerIds : [];
119120
const logs = game.log.map<ScrabbleLog>(log => JSON.parse(log));
121+
if (winCtx?.type === 'dq') {
122+
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+
);
128+
}
120129
return logs
121130
.filterMap<ScrabbleDexEntry[]>(log => {
122131
if (log.action !== 'play') return;

src/ps/games/scrabble/logs.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import type { DIRECTION } from '@/ps/games/scrabble/constants';
22
import type { BoardTile, Points } from '@/ps/games/scrabble/types';
3-
import type { BaseLog } from '@/ps/games/types';
3+
import type { BaseLog, BaseLogAction } from '@/ps/games/types';
44
import type { Satisfies, SerializedInstance } from '@/types/common';
55
import type { Point } from '@/utils/grid';
66

7-
export type Log = Satisfies<
8-
BaseLog,
9-
{
10-
time: Date;
11-
turn: string;
12-
} & (
13-
| {
14-
action: 'play';
15-
ctx: {
16-
points: Points;
17-
tiles: BoardTile[];
18-
dir: DIRECTION;
19-
point: Point;
20-
newTiles: string[];
21-
rack: string[];
22-
words: Record<string, number>;
23-
};
24-
}
25-
| { action: 'exchange'; ctx: { tiles: string[]; newTiles: string[]; rack: string[] } }
26-
| { action: 'pass'; ctx: { rack: string[] } }
27-
)
28-
>;
7+
export type Log =
8+
| Satisfies<
9+
BaseLog,
10+
{
11+
time: Date;
12+
turn: string;
13+
} & (
14+
| {
15+
action: 'play';
16+
ctx: {
17+
points: Points;
18+
tiles: BoardTile[];
19+
dir: DIRECTION;
20+
point: Point;
21+
newTiles: string[];
22+
rack: string[];
23+
words: Record<string, number>;
24+
};
25+
}
26+
| { action: 'exchange'; ctx: { tiles: string[]; newTiles: string[]; rack: string[] } }
27+
| { action: 'pass'; ctx: { rack: string[] } }
28+
)
29+
>
30+
| BaseLogAction;
2931

3032
export type APILog = SerializedInstance<Log>;

src/ps/games/scrabble/types.ts

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

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

src/ps/games/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export type ActionResponse<T = null> = { success: true; data: T } | { success: f
6868
export type EndType = 'regular' | 'force' | 'dq' | 'loss';
6969

7070
export type BaseLog = { action: string; time: Date; turn: string | null; ctx: unknown };
71+
export type BaseLogAction = { action: 'dq' | 'forfeit' | 'skip'; turn: string; ctx: null } & BaseLog;
7172

7273
export type CommonLog<Turn extends string = string> = Satisfies<
7374
BaseLog,

src/ps/loaders/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export default async function init() {
99
await connection;
1010
await loadCommands();
1111
if (IS_ENABLED.DB) {
12-
await loadAlts();
13-
await loadSeens();
12+
if (process.env.NODE_ENV !== 'development') {
13+
await loadAlts();
14+
await loadSeens();
15+
}
1416
await loadRoomConfigs();
1517
}
1618
}

0 commit comments

Comments
 (0)