-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.ts
372 lines (337 loc) · 8.21 KB
/
types.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
export type UserId = string;
export type Network =
| "google"
| "github"
| "password"
| "telegram"
| "reddit"
| "steam";
export type Emoji = string;
export type Timestamp = number;
export enum Color {
Neutral = 0,
Red = 1,
Blue = 2,
Green = 3,
Yellow = 4,
Magenta = 5,
Cyan = 6,
Orange = 7,
Black = 8,
Beige = 9,
}
export type TableProps = {
readonly playerStartCount: number;
readonly status: TableStatus;
readonly gameStart: Timestamp;
readonly turnIndex: number;
readonly turnStart: Timestamp;
readonly turnActivity: boolean;
readonly turnCount: number;
readonly roundCount: number;
readonly attack: Attack | null;
readonly currentGame: number | null;
readonly mapName: string;
readonly adjacency: Adjacency;
};
export type Table = TableProps & {
readonly name: string;
readonly tag: string;
readonly stackSize: number;
readonly playerSlots: number;
readonly startSlots: number;
readonly points: number;
readonly params: TableParams;
readonly players: readonly Player[];
readonly lands: readonly Land[];
readonly watching: readonly Watcher[];
readonly retired: readonly Player[];
};
export type TableParams = {
noFlagRounds: number;
botLess: boolean;
startingCapitals: boolean;
readySlots: number | null;
turnSeconds: number | null;
tournament: undefined | TournamentParam;
};
export type TournamentParam = {
frequency: TournamentFrequency;
prize: number;
fee: number;
};
export type TournamentFrequency =
| "minutely"
| "5minutely"
| "hourly"
| "daily"
| "weekly"
| "monthly";
export type Land = {
readonly emoji: Emoji;
readonly color: Color;
readonly points: number;
readonly capital: boolean;
};
export type Attack = {
start: Timestamp;
from: Emoji;
to: Emoji;
};
export type TableStatus = "PAUSED" | "PLAYING" | "FINISHED";
export type Adjacency = {
readonly matrix: ReadonlyArray<ReadonlyArray<boolean>>;
indexes: Readonly<{ [index: string]: number }>;
};
export type TableInfo = Omit<
Table,
"lands" | "players" | "watchers" | "adjacency"
> & {
landCount: number;
playerCount: number;
watchCount: number;
botCount: number;
};
export type UserLike = {
readonly id: UserId;
readonly name: string;
readonly picture: string;
readonly level: number;
readonly points: number;
readonly rank: number;
readonly skin: number;
};
export type User = UserLike & {
readonly email: string;
readonly networks: readonly string[];
readonly claimed: boolean;
readonly levelPoints: number;
readonly voted: string[];
readonly awards: readonly Award[];
readonly ip: string | undefined;
};
export type Player = UserLike & {
readonly clientId: any;
readonly color: Color;
readonly reserveDice: number;
readonly out: boolean;
readonly outTurns: number;
readonly points: number;
readonly awards: readonly Award[];
readonly position: number;
readonly score: number;
readonly flag: number | null;
readonly lastBeat: Timestamp;
readonly joined: Timestamp;
readonly ready: boolean;
readonly bot: Persona | null;
readonly ip: string | null;
};
export type Chatter = {
name: string;
color?: number;
} | null;
export type Preferences = {};
export type PushNotificationEvents = "game-start" | "turn";
export type Award = {
type: "monthly_rank" | "weekly_rank" | "early_adopter";
position: number;
timestamp: Date;
table?: string;
};
export type Watcher = {
clientId: any;
id: UserId | null;
name: string | null;
lastBeat: number;
death: number;
};
export type Elimination = {
player: Player;
position: number;
reason: EliminationReason;
source: EliminationSource;
};
export type EliminationSource =
| { turns: number }
| { player: Player; points: number }
| { flag: number; under: null | { player: Player; points: number } };
export type EliminationReason = "☠" | "💤" | "🏆" | "🏳";
export type ScoredElimination = Elimination & {
score: number;
};
export enum IllegalMoveCode {
UserIsNull = 1,
NotPlaying,
JoinWhilePlaying,
AlreadyJoined,
NotEnoughPoints,
TableFull,
IsRetired,
NoTakeoverTargets,
LeaveWhilePlaying,
NotJoined,
AttackWhileStopped,
AttackOutOfTurn,
AttackWhileAttack,
AttackLandsNotFound,
AttackFromNeutral,
AttackFromOnePoint,
AttackSameColor,
AttackNotBorder,
EndTurnWhileStopped,
EndTurnOutOfTurn,
EndTurnDuringAttack,
EndTurnNoPlayer,
SitOutNotPlaying,
SitOutNoPlayer,
SitInNoPlayerfalse,
FlagWhileNotPlaying,
FlagFirst,
FlagMismatch,
FlagUp,
DuplicateIP,
LeaveTournament,
IllegalReady,
ReadyWhilePlaying,
InsufficientFee,
}
export class IllegalMoveError extends Error {
code: IllegalMoveCode;
bot: boolean;
constructor(
message: string,
code: IllegalMoveCode,
player?: Player | boolean
) {
super(message);
Object.setPrototypeOf(this, IllegalMoveError.prototype);
this.code = code;
this.bot =
player === undefined
? false
: typeof player === "boolean"
? player
: !!player.bot;
}
}
export type CommandType =
| "Enter"
| "Exit"
| "Join"
| "Takeover"
| "Leave"
| "Attack"
| "EndTurn"
| "SitOut"
| "SitIn"
| "Chat"
| "ToggleReady"
| "Flag"
| "Heartbeat"
| "Roll"
| "TickStart"
| "CleanWatchers"
| "CleanPlayers"
| "BotState"
| "EndGame";
type CommandSkeleton<T, P = {}> = {
readonly type: T;
} & P;
export type Command =
| CommandSkeleton<"Start", { players: readonly Player[] }>
| CommandSkeleton<"Enter", { user: User | null; clientId: string }>
| CommandSkeleton<"Exit", { user: User | null; clientId: string }>
| CommandSkeleton<
"Chat",
{ user: { id: string; name: string } | null; message: string }
>
| CommandSkeleton<
"Join",
{ user: User; clientId: string | null; bot: Persona | null }
>
| CommandSkeleton<"Leave", { player: Player }>
| CommandSkeleton<"Attack", { player: Player; from: string; to: string }>
| CommandSkeleton<
"Roll",
{
attacker: Player;
defender: Player | null;
from: string;
to: string;
fromRoll: number[];
toRoll: number[];
round: number;
}
>
| CommandSkeleton<
"EndTurn",
{
player: Player;
dice: {
lands: readonly [Emoji, number][];
reserve: number;
capitals: readonly Emoji[];
};
sitPlayerOut: boolean;
}
>
| CommandSkeleton<"SitOut", { player: Player }>
| CommandSkeleton<"SitIn", { player: Player }>
| CommandSkeleton<"ToggleReady", { player: Player; ready: boolean }>
| CommandSkeleton<"Flag", { player: Player; position: number }>
| CommandSkeleton<"EndGame", { winner: Player | null; turnCount: number }>
| CommandSkeleton<"Clear">
| CommandSkeleton<"Heartbeat", { user: User | null; clientId: string }>
| CommandSkeleton<"BotState", { player: Player; botCommand: BotCommand }>
| CommandSkeleton<
"SetGameStart",
{ gameStart: number; map: string | null; returnFee: number | null }
>;
export type CommandResult = {
readonly table?: Partial<TableProps>;
readonly lands?: ReadonlyArray<Land>;
readonly players?: ReadonlyArray<Player>;
readonly watchers?: ReadonlyArray<Watcher>;
readonly eliminations?: ReadonlyArray<Elimination>;
readonly retired?: ReadonlyArray<Player>;
readonly payScores?: readonly [UserId, string | null, number][];
};
export type BotPlayer = Player & {
bot: Persona;
};
export type Persona = {
name: string;
picture: string;
strategy: BotStrategy;
state: BotState;
};
export type BotState = {
deadlockCount: number;
lastAgressor: UserId | null;
surrender: boolean;
};
export type BotStrategy =
| "RandomCareful"
| "RandomCareless"
| "Revengeful"
| "ExtraCareful"
| "TargetCareful";
export type BotCommand = "Surrender";
export type PlayerStats = {
readonly rolls?: [number, number, number, number, number, number]; // key/index: die, value: count
readonly attacks?: [number, number]; // [failed, succeeded]
readonly eliminations?: [
number,
number,
number,
number,
number,
number,
number,
number,
number
]; // key/index: positions 1-9, value: count
readonly kills?: number; // count
readonly luck?: [number, number]; // [lucky, unlucky] attacks/defenses count
};