-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgames.ts
More file actions
140 lines (133 loc) · 3.58 KB
/
games.ts
File metadata and controls
140 lines (133 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 { toId } from '@/tools';
import { instantInRange } from '@/utils/timeInRange';
import type { Log as ScrabbleLog } from '@/ps/games/scrabble/logs';
import type { WinCtx as ScrabbleWinCtx } from '@/ps/games/scrabble/types';
import type { Player } from '@/ps/games/types';
const schema = new mongoose.Schema({
id: {
type: String,
required: true,
unique: true,
},
game: {
type: String,
required: true,
},
mod: String,
room: {
type: String,
required: true,
},
players: {
type: Map,
of: {
name: {
type: String,
required: true,
},
id: {
type: String,
required: true,
},
turn: {
type: String,
required: true,
},
},
required: true,
},
created: {
type: Date,
required: true,
},
started: {
type: Date,
required: true,
},
ended: {
type: Date,
required: true,
default: Date.now,
},
log: [String],
winCtx: mongoose.Schema.Types.Mixed,
});
export interface GameModel {
id: string;
game: string;
mod?: string | null | undefined;
room: string;
players: Map<string, Player>;
created: Date;
started: Date | null;
ended: Date;
log: string[];
winCtx?: { type: 'win'; winner: Player } | unknown;
}
const model = mongoose.model('game', schema, 'games', { overwriteModels: true });
export async function uploadGame(game: GameModel): Promise<GameModel | null> {
if (!IS_ENABLED.DB) return null;
return model.create(game);
}
export async function getGameById(gameType: string, gameId: string): Promise<HydratedDocument<GameModel> | null> {
if (!IS_ENABLED.DB) return null;
const id = gameId.toUpperCase().replace(/^#?/, '#');
const game = await model.findOne({ game: gameType, id });
if (!game) throw new Error(`Unable to find a game of ${gameType} with ID ${id}.`);
return game;
}
// UGO-CODE
export type ScrabbleDexEntry = {
pokemon: string;
pokemonName: string;
num: number;
by: string;
byName: string | null;
at: Date;
gameId: string;
mod: string;
won: boolean;
};
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));
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;
const mon = pokedex[word];
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();
});
}