-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
239 lines (216 loc) Β· 6.35 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
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts";
/*
* This file exports both Zod types and TS types for everything
* Zod types start with a lowercase character, while TS types
* start with an uppercase character.
*/
// βββ Card Utils ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export const regularCards = z.enum([
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
]);
export type RegularCard = z.infer<typeof regularCards>;
// color-switching special cards
export const colorSwitchingScs = z.enum(["plus-four", "color-chooser"]);
export type ColorSwitchingScs = z.infer<typeof colorSwitchingScs>;
// non color-switching special cards
export const nonColorSwitchingScs = z.enum(["plus-two", "reverse", "skip"]);
export type NonColorSwitchingScs = z.infer<typeof nonColorSwitchingScs>;
export const specialCards = z.enum([
...nonColorSwitchingScs.options,
...colorSwitchingScs.options,
]);
export type SpecialCard = z.infer<typeof specialCards>;
// all cards = regular cards + special cards
export const allCards = z.enum([
...regularCards.options,
...specialCards.options,
]);
export type AllCards = z.infer<typeof allCards>;
export const cardColor = z.enum(["red", "blue", "green", "yellow"]);
export type CardColor = z.infer<typeof cardColor>;
export const card = z.union([
z.object({
type: z.union([regularCards, nonColorSwitchingScs]),
color: cardColor,
}),
z.object({
type: colorSwitchingScs,
color: z.undefined(),
}),
]);
export type Card = z.infer<typeof card>;
export const playerId = z.string();
export type PlayerId = z.infer<typeof playerId>;
// βββ Game Events βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export const cardPlayedEvent = z.object({
type: z.literal("card_played"),
by: playerId,
card,
});
export type CardPlayedEvent = z.infer<typeof cardPlayedEvent>;
export const bluffCalledEvent = z.object({
type: z.literal("bluff_called"),
by: playerId,
});
export type BluffCalledEvent = z.infer<typeof bluffCalledEvent>;
export const drawEvent = z.object({
type: z.literal("draw"),
by: playerId,
});
export type DrawEvent = z.infer<typeof drawEvent>;
export const passEvent = z.object({
type: z.literal("pass"),
by: playerId,
});
export type PassEvent = z.infer<typeof passEvent>;
export const chooseColorEvent = z.object({
type: z.literal("choose-color"),
by: playerId,
color: cardColor,
});
export type ChooseColorEvent = z.infer<typeof chooseColorEvent>;
// the collective game event object
export const gameEvent = z.union([
cardPlayedEvent,
bluffCalledEvent,
chooseColorEvent,
drawEvent,
passEvent,
]);
export type GameEvent = z.infer<typeof gameEvent>;
// the game info event is added the engine
export const infoEvent = z.object({
type: z.literal("game_info"),
info: z.union([
z.object({
type: z.literal("bluff_called"),
by: playerId,
}),
z.object({
type: z.literal("bluff_call_succeeded"),
by: playerId,
of: playerId,
}),
z.object({
type: z.literal("bluff_call_failed"),
by: playerId,
of: playerId,
}),
z.object({
type: z.literal("seed_changed"),
newSeed: z.number(),
}),
z.object({
type: z.literal("player_won"),
id: playerId,
}),
z.object({
type: z.literal("game_ended"),
loser: playerId,
}),
z.object({
type: z.literal("player_joined"),
id: playerId,
}),
z.object({
type: z.literal("player_left"),
id: playerId,
}),
z.object({
type: z.literal("pile_to_deck"),
}),
]),
});
export type InfoEvent = z.infer<typeof infoEvent>;
// βββ Game Utils ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const commonGameState = {
// the index gets modulo'd with the player count to get the active player index
index: z.number(),
initialSeed: z.number(),
players: playerId.array(),
// game options
stackPlusTwos: z.boolean(),
// this is a cache, since all of the info here can be obtained from the
// events array; but since it's expensive to do that all the time, we
// can keep it here. note that re-obtaining this info in shorthand mode
// is not possible, since past events are not kept, so it acts as state.
cache: z.object({
hands: z.record(playerId, card.array()),
deck: card.array(),
seed: z.number(),
stackedPlusTwos: z.number(),
turnStep: z.union([z.literal(1), z.literal(-1)]),
ended: z.boolean(),
pile: card.array(),
last3CardPlayOrColorChanges: z.union([card, cardColor]).array(),
}),
};
export const fullGameState = z.object({
shorthandMode: z.literal(false),
...commonGameState,
events: z.union([gameEvent, infoEvent]).array(),
});
export type FullGameState = z.infer<typeof fullGameState>;
export const shorthandGameState = z.object({
shorthandMode: z.literal(true),
...commonGameState,
events: gameEvent.array(),
});
export type ShorthandGameState = z.infer<typeof shorthandGameState>;
export const gameState = z.union([fullGameState, shorthandGameState]);
export type GameState = z.infer<typeof gameState>;
// returned from `processNewEvent` or any of the user-friendly
// functions like `draw`, `pass`, `play`, `callBluff`, or `switchColor`.
export type ProcessEventResult =
| {
type: "card_played";
card: Card;
formatted: string;
}
| {
type: "player_win_game_end";
won: string;
lost: string;
formatted: string;
}
| {
type: "player_win";
won: string;
formatted: string;
}
| {
type: "drawing_cards";
cards: Card[];
count: number;
formatted: string;
}
| {
type: "turn_passed";
formatted: string;
}
| {
type: "bluff_call_succeeded";
by: string;
of: string;
formatted: string;
}
| {
type: "bluff_call_failed";
by: string;
of: string;
formatted: string;
}
| {
type: "color_changed";
to: string;
formatted: string;
};