unocab 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -4
- package/{script → dist}/errors.d.ts +13 -3
- package/dist/game.d.ts +459 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +786 -0
- package/dist/types.d.ts +1106 -0
- package/{esm → dist}/utils.d.ts +4 -3
- package/package.json +17 -11
- package/esm/_dnt.shims.d.ts +0 -4
- package/esm/_dnt.shims.js +0 -62
- package/esm/_dnt.test_shims.d.ts +0 -7
- package/esm/deps/deno.land/std@0.190.0/fmt/colors.d.ts +0 -270
- package/esm/deps/deno.land/std@0.190.0/testing/_diff.d.ts +0 -26
- package/esm/deps/deno.land/std@0.190.0/testing/_format.d.ts +0 -1
- package/esm/deps/deno.land/std@0.190.0/testing/asserts.d.ts +0 -284
- package/esm/deps/deno.land/x/zod@v3.21.4/ZodError.d.ts +0 -163
- package/esm/deps/deno.land/x/zod@v3.21.4/ZodError.js +0 -148
- package/esm/deps/deno.land/x/zod@v3.21.4/errors.d.ts +0 -5
- package/esm/deps/deno.land/x/zod@v3.21.4/errors.js +0 -9
- package/esm/deps/deno.land/x/zod@v3.21.4/external.d.ts +0 -6
- package/esm/deps/deno.land/x/zod@v3.21.4/external.js +0 -6
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/enumUtil.d.ts +0 -8
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/enumUtil.js +0 -1
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/errorUtil.d.ts +0 -9
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/errorUtil.js +0 -5
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/parseUtil.d.ts +0 -78
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/parseUtil.js +0 -104
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/partialUtil.d.ts +0 -8
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/partialUtil.js +0 -1
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/typeAliases.d.ts +0 -2
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/typeAliases.js +0 -1
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/util.d.ts +0 -68
- package/esm/deps/deno.land/x/zod@v3.21.4/helpers/util.js +0 -138
- package/esm/deps/deno.land/x/zod@v3.21.4/index.d.ts +0 -4
- package/esm/deps/deno.land/x/zod@v3.21.4/index.js +0 -4
- package/esm/deps/deno.land/x/zod@v3.21.4/locales/en.d.ts +0 -3
- package/esm/deps/deno.land/x/zod@v3.21.4/locales/en.js +0 -127
- package/esm/deps/deno.land/x/zod@v3.21.4/mod.d.ts +0 -1
- package/esm/deps/deno.land/x/zod@v3.21.4/mod.js +0 -1
- package/esm/deps/deno.land/x/zod@v3.21.4/types.d.ts +0 -965
- package/esm/deps/deno.land/x/zod@v3.21.4/types.js +0 -3689
- package/esm/errors.d.ts +0 -72
- package/esm/errors.js +0 -39
- package/esm/game.d.ts +0 -868
- package/esm/game.js +0 -868
- package/esm/mod.d.ts +0 -3
- package/esm/mod.js +0 -3
- package/esm/package.json +0 -3
- package/esm/tests/basic-tests.d.ts +0 -8
- package/esm/tests/draw-tests.d.ts +0 -8
- package/esm/tests/non-specific-tests.test.d.ts +0 -1
- package/esm/tests/rule-violation-tests.d.ts +0 -8
- package/esm/tests/specific-tests.test.d.ts +0 -1
- package/esm/tests/test-utils.d.ts +0 -91
- package/esm/tests/turn-tests.d.ts +0 -8
- package/esm/types.d.ts +0 -2280
- package/esm/types.js +0 -152
- package/esm/utils.js +0 -44
- package/script/_dnt.shims.d.ts +0 -4
- package/script/_dnt.shims.js +0 -66
- package/script/_dnt.test_shims.d.ts +0 -7
- package/script/deps/deno.land/std@0.190.0/fmt/colors.d.ts +0 -270
- package/script/deps/deno.land/std@0.190.0/testing/_diff.d.ts +0 -26
- package/script/deps/deno.land/std@0.190.0/testing/_format.d.ts +0 -1
- package/script/deps/deno.land/std@0.190.0/testing/asserts.d.ts +0 -284
- package/script/deps/deno.land/x/zod@v3.21.4/ZodError.d.ts +0 -163
- package/script/deps/deno.land/x/zod@v3.21.4/ZodError.js +0 -152
- package/script/deps/deno.land/x/zod@v3.21.4/errors.d.ts +0 -5
- package/script/deps/deno.land/x/zod@v3.21.4/errors.js +0 -17
- package/script/deps/deno.land/x/zod@v3.21.4/external.d.ts +0 -6
- package/script/deps/deno.land/x/zod@v3.21.4/external.js +0 -22
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/enumUtil.d.ts +0 -8
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/enumUtil.js +0 -2
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/errorUtil.d.ts +0 -9
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/errorUtil.js +0 -8
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/parseUtil.d.ts +0 -78
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/parseUtil.js +0 -119
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/partialUtil.d.ts +0 -8
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/partialUtil.js +0 -2
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/typeAliases.d.ts +0 -2
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/typeAliases.js +0 -2
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/util.d.ts +0 -68
- package/script/deps/deno.land/x/zod@v3.21.4/helpers/util.js +0 -142
- package/script/deps/deno.land/x/zod@v3.21.4/index.d.ts +0 -4
- package/script/deps/deno.land/x/zod@v3.21.4/index.js +0 -33
- package/script/deps/deno.land/x/zod@v3.21.4/locales/en.d.ts +0 -3
- package/script/deps/deno.land/x/zod@v3.21.4/locales/en.js +0 -129
- package/script/deps/deno.land/x/zod@v3.21.4/mod.d.ts +0 -1
- package/script/deps/deno.land/x/zod@v3.21.4/mod.js +0 -17
- package/script/deps/deno.land/x/zod@v3.21.4/types.d.ts +0 -965
- package/script/deps/deno.land/x/zod@v3.21.4/types.js +0 -3739
- package/script/errors.js +0 -43
- package/script/game.d.ts +0 -868
- package/script/game.js +0 -895
- package/script/mod.d.ts +0 -3
- package/script/mod.js +0 -19
- package/script/package.json +0 -3
- package/script/tests/basic-tests.d.ts +0 -8
- package/script/tests/draw-tests.d.ts +0 -8
- package/script/tests/non-specific-tests.test.d.ts +0 -1
- package/script/tests/rule-violation-tests.d.ts +0 -8
- package/script/tests/specific-tests.test.d.ts +0 -1
- package/script/tests/test-utils.d.ts +0 -91
- package/script/tests/turn-tests.d.ts +0 -8
- package/script/types.d.ts +0 -2280
- package/script/types.js +0 -155
- package/script/utils.d.ts +0 -19
- package/script/utils.js +0 -50
package/dist/index.js
ADDED
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
// errors.ts
|
|
2
|
+
var UECode;
|
|
3
|
+
((UECode) => {
|
|
4
|
+
UECode[UECode["GameEnded"] = 0] = "GameEnded";
|
|
5
|
+
UECode[UECode["MustPickColor"] = 1] = "MustPickColor";
|
|
6
|
+
UECode[UECode["MustNotPlayLastCardColorChanging"] = 2] = "MustNotPlayLastCardColorChanging";
|
|
7
|
+
UECode[UECode["MustDrawOrPlusTwo"] = 3] = "MustDrawOrPlusTwo";
|
|
8
|
+
UECode[UECode["MustDrawOrCallBluff"] = 4] = "MustDrawOrCallBluff";
|
|
9
|
+
UECode[UECode["MustPlayExpectedCard"] = 5] = "MustPlayExpectedCard";
|
|
10
|
+
UECode[UECode["MustNotDrawTwice"] = 6] = "MustNotDrawTwice";
|
|
11
|
+
UECode[UECode["MustNotPassWithoutDraw"] = 7] = "MustNotPassWithoutDraw";
|
|
12
|
+
UECode[UECode["CannotCallBluff"] = 8] = "CannotCallBluff";
|
|
13
|
+
UECode[UECode["CannotSwitchColors"] = 9] = "CannotSwitchColors";
|
|
14
|
+
UECode[UECode["TooFewPlayers"] = 10] = "TooFewPlayers";
|
|
15
|
+
UECode[UECode["TooManyPlayers"] = 11] = "TooManyPlayers";
|
|
16
|
+
UECode[UECode["InvalidId"] = 12] = "InvalidId";
|
|
17
|
+
UECode[UECode["NotPlayersTurn"] = 13] = "NotPlayersTurn";
|
|
18
|
+
UECode[UECode["PlayerNotInGame"] = 14] = "PlayerNotInGame";
|
|
19
|
+
UECode[UECode["PlayerDoesntHaveCard"] = 15] = "PlayerDoesntHaveCard";
|
|
20
|
+
UECode[UECode["DeckPileExhausted"] = 16] = "DeckPileExhausted";
|
|
21
|
+
UECode[UECode["PlayerAlreadyInGame"] = 17] = "PlayerAlreadyInGame";
|
|
22
|
+
UECode[UECode["CannotJumpInShorthandMode"] = 18] = "CannotJumpInShorthandMode";
|
|
23
|
+
UECode[UECode["InvalidEventIndex"] = 19] = "InvalidEventIndex";
|
|
24
|
+
})(UECode ||= {});
|
|
25
|
+
|
|
26
|
+
class UnocabError extends Error {
|
|
27
|
+
code;
|
|
28
|
+
data;
|
|
29
|
+
constructor(code, data, message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.data = data;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// types.ts
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
var regularCards = z.enum([
|
|
39
|
+
"zero",
|
|
40
|
+
"one",
|
|
41
|
+
"two",
|
|
42
|
+
"three",
|
|
43
|
+
"four",
|
|
44
|
+
"five",
|
|
45
|
+
"six",
|
|
46
|
+
"seven",
|
|
47
|
+
"eight",
|
|
48
|
+
"nine"
|
|
49
|
+
]);
|
|
50
|
+
var colorSwitchingScs = z.enum(["plus-four", "color-chooser"]);
|
|
51
|
+
var nonColorSwitchingScs = z.enum(["plus-two", "reverse", "skip"]);
|
|
52
|
+
var specialCards = z.enum([
|
|
53
|
+
...nonColorSwitchingScs.options,
|
|
54
|
+
...colorSwitchingScs.options
|
|
55
|
+
]);
|
|
56
|
+
var allCards = z.enum([
|
|
57
|
+
...regularCards.options,
|
|
58
|
+
...specialCards.options
|
|
59
|
+
]);
|
|
60
|
+
var cardColor = z.enum(["red", "blue", "green", "yellow"]);
|
|
61
|
+
var card = z.union([
|
|
62
|
+
z.object({
|
|
63
|
+
type: z.union([regularCards, nonColorSwitchingScs]),
|
|
64
|
+
color: cardColor
|
|
65
|
+
}),
|
|
66
|
+
z.object({
|
|
67
|
+
type: colorSwitchingScs,
|
|
68
|
+
color: z.undefined().optional()
|
|
69
|
+
})
|
|
70
|
+
]);
|
|
71
|
+
var playerId = z.string();
|
|
72
|
+
var cardPlayedEvent = z.object({
|
|
73
|
+
type: z.literal("card_played"),
|
|
74
|
+
by: playerId,
|
|
75
|
+
card
|
|
76
|
+
});
|
|
77
|
+
var bluffCalledEvent = z.object({
|
|
78
|
+
type: z.literal("bluff_called"),
|
|
79
|
+
by: playerId
|
|
80
|
+
});
|
|
81
|
+
var drawEvent = z.object({
|
|
82
|
+
type: z.literal("draw"),
|
|
83
|
+
by: playerId
|
|
84
|
+
});
|
|
85
|
+
var passEvent = z.object({
|
|
86
|
+
type: z.literal("pass"),
|
|
87
|
+
by: playerId
|
|
88
|
+
});
|
|
89
|
+
var chooseColorEvent = z.object({
|
|
90
|
+
type: z.literal("choose-color"),
|
|
91
|
+
by: playerId,
|
|
92
|
+
color: cardColor
|
|
93
|
+
});
|
|
94
|
+
var gameEvent = z.union([
|
|
95
|
+
cardPlayedEvent,
|
|
96
|
+
bluffCalledEvent,
|
|
97
|
+
chooseColorEvent,
|
|
98
|
+
drawEvent,
|
|
99
|
+
passEvent
|
|
100
|
+
]);
|
|
101
|
+
var infoEvent = z.object({
|
|
102
|
+
type: z.literal("game_info"),
|
|
103
|
+
info: z.union([
|
|
104
|
+
z.object({
|
|
105
|
+
type: z.literal("bluff_called"),
|
|
106
|
+
by: playerId
|
|
107
|
+
}),
|
|
108
|
+
z.object({
|
|
109
|
+
type: z.literal("bluff_call_succeeded"),
|
|
110
|
+
by: playerId,
|
|
111
|
+
of: playerId
|
|
112
|
+
}),
|
|
113
|
+
z.object({
|
|
114
|
+
type: z.literal("bluff_call_failed"),
|
|
115
|
+
by: playerId,
|
|
116
|
+
of: playerId
|
|
117
|
+
}),
|
|
118
|
+
z.object({
|
|
119
|
+
type: z.literal("seed_changed"),
|
|
120
|
+
newSeed: z.number()
|
|
121
|
+
}),
|
|
122
|
+
z.object({
|
|
123
|
+
type: z.literal("player_won"),
|
|
124
|
+
id: playerId
|
|
125
|
+
}),
|
|
126
|
+
z.object({
|
|
127
|
+
type: z.literal("game_ended"),
|
|
128
|
+
loser: playerId
|
|
129
|
+
}),
|
|
130
|
+
z.object({
|
|
131
|
+
type: z.literal("player_joined"),
|
|
132
|
+
id: playerId
|
|
133
|
+
}),
|
|
134
|
+
z.object({
|
|
135
|
+
type: z.literal("player_left"),
|
|
136
|
+
id: playerId
|
|
137
|
+
}),
|
|
138
|
+
z.object({
|
|
139
|
+
type: z.literal("pile_to_deck")
|
|
140
|
+
})
|
|
141
|
+
])
|
|
142
|
+
});
|
|
143
|
+
var commonGameState = {
|
|
144
|
+
index: z.number(),
|
|
145
|
+
initialSeed: z.number(),
|
|
146
|
+
players: playerId.array(),
|
|
147
|
+
stackPlusTwos: z.boolean(),
|
|
148
|
+
cache: z.object({
|
|
149
|
+
hands: z.record(playerId, card.array()),
|
|
150
|
+
deck: card.array(),
|
|
151
|
+
seed: z.number(),
|
|
152
|
+
stackedPlusTwos: z.number(),
|
|
153
|
+
turnStep: z.union([z.literal(1), z.literal(-1)]),
|
|
154
|
+
ended: z.union([z.literal(false), z.object({ loser: playerId })]),
|
|
155
|
+
pile: card.array(),
|
|
156
|
+
last3CardPlayOrColorChanges: z.union([card, cardColor]).array()
|
|
157
|
+
})
|
|
158
|
+
};
|
|
159
|
+
var fullGameState = z.object({
|
|
160
|
+
shorthandMode: z.literal(false),
|
|
161
|
+
...commonGameState,
|
|
162
|
+
events: z.union([gameEvent, infoEvent]).array()
|
|
163
|
+
});
|
|
164
|
+
var shorthandGameState = z.object({
|
|
165
|
+
shorthandMode: z.literal(true),
|
|
166
|
+
...commonGameState,
|
|
167
|
+
events: gameEvent.array()
|
|
168
|
+
});
|
|
169
|
+
var gameState = z.union([fullGameState, shorthandGameState]);
|
|
170
|
+
|
|
171
|
+
// utils.ts
|
|
172
|
+
var ENGINE_ID = "ENGINE";
|
|
173
|
+
var MAX_SHORTHAND_EVENTS = 5;
|
|
174
|
+
var MIN_PLAYERS = 2;
|
|
175
|
+
var MAX_PLAYERS = 10;
|
|
176
|
+
var nonZeroRegularTypes = regularCards.options.filter((t) => t != "zero");
|
|
177
|
+
var twoCardTypes = [...nonZeroRegularTypes, ...nonColorSwitchingScs.options];
|
|
178
|
+
var DECK = [
|
|
179
|
+
...cardColor.options.map((color) => ({ type: "zero", color })),
|
|
180
|
+
...cardColor.options.flatMap((color) => twoCardTypes.flatMap((type) => Array.from({ length: 2 }, () => ({ type, color })))),
|
|
181
|
+
...colorSwitchingScs.options.flatMap((type) => Array.from({ length: 4 }, () => ({ type })))
|
|
182
|
+
];
|
|
183
|
+
function cyrb128(str) {
|
|
184
|
+
let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762;
|
|
185
|
+
for (let i = 0, k;i < str.length; i++) {
|
|
186
|
+
k = str.charCodeAt(i);
|
|
187
|
+
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
188
|
+
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
189
|
+
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
|
190
|
+
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
|
191
|
+
}
|
|
192
|
+
h1 = Math.imul(h3 ^ h1 >>> 18, 597399067);
|
|
193
|
+
h2 = Math.imul(h4 ^ h2 >>> 22, 2869860233);
|
|
194
|
+
h3 = Math.imul(h1 ^ h3 >>> 17, 951274213);
|
|
195
|
+
h4 = Math.imul(h2 ^ h4 >>> 19, 2716044179);
|
|
196
|
+
return (h1 ^ h2 ^ h3 ^ h4) >>> 0;
|
|
197
|
+
}
|
|
198
|
+
function includes(arr, v) {
|
|
199
|
+
return arr.includes(v);
|
|
200
|
+
}
|
|
201
|
+
function exhaustive(_) {}
|
|
202
|
+
function mod(n, m) {
|
|
203
|
+
return (n % m + m) % m;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// game.ts
|
|
207
|
+
class Game {
|
|
208
|
+
_state;
|
|
209
|
+
get state() {
|
|
210
|
+
return structuredClone(this._state);
|
|
211
|
+
}
|
|
212
|
+
constructor(props = {}) {
|
|
213
|
+
if ("serialisedState" in props && props.serialisedState) {
|
|
214
|
+
const state = JSON.parse(props.serialisedState);
|
|
215
|
+
gameState.parse(state);
|
|
216
|
+
this._state = state;
|
|
217
|
+
} else {
|
|
218
|
+
const initialSeed = (() => {
|
|
219
|
+
if (props.initialSeed !== undefined) {
|
|
220
|
+
return typeof props.initialSeed == "string" ? cyrb128(props.initialSeed) : props.initialSeed;
|
|
221
|
+
}
|
|
222
|
+
return crypto.getRandomValues(new Uint32Array(1))[0];
|
|
223
|
+
})();
|
|
224
|
+
const commonState = {
|
|
225
|
+
index: 0,
|
|
226
|
+
players: [],
|
|
227
|
+
initialSeed,
|
|
228
|
+
stackPlusTwos: props.stackPlusTwos ?? true,
|
|
229
|
+
cache: {
|
|
230
|
+
hands: {},
|
|
231
|
+
deck: [],
|
|
232
|
+
seed: initialSeed,
|
|
233
|
+
ended: false,
|
|
234
|
+
stackedPlusTwos: 0,
|
|
235
|
+
turnStep: 1,
|
|
236
|
+
pile: [],
|
|
237
|
+
last3CardPlayOrColorChanges: []
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
if (props.shorthandMode) {
|
|
241
|
+
const shorthandGameState = {
|
|
242
|
+
shorthandMode: true,
|
|
243
|
+
events: [],
|
|
244
|
+
...commonState
|
|
245
|
+
};
|
|
246
|
+
this._state = shorthandGameState;
|
|
247
|
+
} else {
|
|
248
|
+
const fullGameState = {
|
|
249
|
+
shorthandMode: false,
|
|
250
|
+
events: [],
|
|
251
|
+
...commonState
|
|
252
|
+
};
|
|
253
|
+
this._state = fullGameState;
|
|
254
|
+
}
|
|
255
|
+
this.init();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
random() {
|
|
259
|
+
let t = this._state.cache.seed += 1831565813;
|
|
260
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
261
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
262
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
263
|
+
}
|
|
264
|
+
shuffleDeck() {
|
|
265
|
+
const toShuffle = this._state.cache.deck;
|
|
266
|
+
for (let i = toShuffle.length - 1;i > 0; i--) {
|
|
267
|
+
const j = Math.floor(this.random() * (i + 1));
|
|
268
|
+
[toShuffle[i], toShuffle[j]] = [toShuffle[j], toShuffle[i]];
|
|
269
|
+
}
|
|
270
|
+
this.recordEvent({
|
|
271
|
+
type: "game_info",
|
|
272
|
+
info: {
|
|
273
|
+
type: "seed_changed",
|
|
274
|
+
newSeed: this._state.cache.seed
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
handRef(playerId2) {
|
|
279
|
+
const hand = this._state.cache.hands[playerId2];
|
|
280
|
+
if (!hand)
|
|
281
|
+
throw new UnocabError(14 /* PlayerNotInGame */, { playerId: playerId2 }, "This player is not in the game");
|
|
282
|
+
return hand;
|
|
283
|
+
}
|
|
284
|
+
playersInPlay() {
|
|
285
|
+
return this._state.players.filter((id) => this.handRef(id).length);
|
|
286
|
+
}
|
|
287
|
+
advanceTurn(steps = 1) {
|
|
288
|
+
const players = this._state.players;
|
|
289
|
+
for (let step = 0;step < steps; step++) {
|
|
290
|
+
for (let tries = 0;tries < players.length; tries++) {
|
|
291
|
+
this._state.index = mod(this._state.index + this._state.cache.turnStep, players.length);
|
|
292
|
+
if (this.handRef(players[this._state.index]).length)
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
init(customDeckTopCard) {
|
|
298
|
+
this._state.cache.seed = this._state.initialSeed;
|
|
299
|
+
this._state.cache.deck = structuredClone(DECK);
|
|
300
|
+
this.shuffleDeck();
|
|
301
|
+
const pileTopCard = (() => {
|
|
302
|
+
if (customDeckTopCard)
|
|
303
|
+
return customDeckTopCard;
|
|
304
|
+
const index = this._state.cache.deck.findIndex((card) => !includes(colorSwitchingScs.options, card.type));
|
|
305
|
+
return this._state.cache.deck.splice(index, 1)[0];
|
|
306
|
+
})();
|
|
307
|
+
const firstCardPlayEvent = {
|
|
308
|
+
type: "card_played",
|
|
309
|
+
by: ENGINE_ID,
|
|
310
|
+
card: pileTopCard
|
|
311
|
+
};
|
|
312
|
+
this._state.events = [firstCardPlayEvent];
|
|
313
|
+
this._state.cache.pile = [pileTopCard];
|
|
314
|
+
this._state.cache.last3CardPlayOrColorChanges = [pileTopCard];
|
|
315
|
+
this._state.cache.stackedPlusTwos = pileTopCard.type == "plus-two" ? 1 : 0;
|
|
316
|
+
}
|
|
317
|
+
recordEvent(event) {
|
|
318
|
+
if (this._state.shorthandMode) {
|
|
319
|
+
if (event.type == "game_info")
|
|
320
|
+
return;
|
|
321
|
+
if (this._state.events.length == MAX_SHORTHAND_EVENTS) {
|
|
322
|
+
this._state.events.shift();
|
|
323
|
+
}
|
|
324
|
+
this._state.events.push(event);
|
|
325
|
+
} else
|
|
326
|
+
this._state.events.push(event);
|
|
327
|
+
}
|
|
328
|
+
deckTopCard() {
|
|
329
|
+
if (!this._state.cache.deck.length) {
|
|
330
|
+
const pile = this.getPile();
|
|
331
|
+
const topCard = pile.pop();
|
|
332
|
+
this._state.cache.deck = pile;
|
|
333
|
+
this.shuffleDeck();
|
|
334
|
+
this._state.cache.pile = [topCard];
|
|
335
|
+
this.recordEvent({
|
|
336
|
+
type: "game_info",
|
|
337
|
+
info: { type: "pile_to_deck" }
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return this._state.cache.deck.shift();
|
|
341
|
+
}
|
|
342
|
+
takeCards(count) {
|
|
343
|
+
const { deck, pile } = this._state.cache;
|
|
344
|
+
const available = deck.length + Math.max(0, pile.length - 1);
|
|
345
|
+
if (count > available) {
|
|
346
|
+
throw new UnocabError(16 /* DeckPileExhausted */, undefined, "Both the deck and pile have been exhausted, cannot draw");
|
|
347
|
+
}
|
|
348
|
+
return Array.from({ length: count }, () => this.deckTopCard());
|
|
349
|
+
}
|
|
350
|
+
facingPlusFour() {
|
|
351
|
+
const [lastEvent, secondLastEvent] = this.lastTwoNonInfoEvents();
|
|
352
|
+
return lastEvent.type == "choose-color" && secondLastEvent?.type == "card_played" && secondLastEvent.card.type == "plus-four";
|
|
353
|
+
}
|
|
354
|
+
endIfDecided() {
|
|
355
|
+
if (this._state.cache.ended)
|
|
356
|
+
return;
|
|
357
|
+
const someoneWon = Object.values(this._state.cache.hands).some((hand) => !hand.length);
|
|
358
|
+
const [loser, ...othersWithCards] = this.playersInPlay();
|
|
359
|
+
if (!someoneWon || loser === undefined || othersWithCards.length)
|
|
360
|
+
return;
|
|
361
|
+
this.recordEvent({
|
|
362
|
+
type: "game_info",
|
|
363
|
+
info: {
|
|
364
|
+
type: "game_ended",
|
|
365
|
+
loser
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
this._state.cache.ended = { loser };
|
|
369
|
+
return loser;
|
|
370
|
+
}
|
|
371
|
+
lastTwoNonInfoEvents() {
|
|
372
|
+
const found = [];
|
|
373
|
+
for (let i = this._state.events.length - 1;i >= 0; i--) {
|
|
374
|
+
const event = this._state.events[i];
|
|
375
|
+
if (event.type == "game_info")
|
|
376
|
+
continue;
|
|
377
|
+
found.push(event);
|
|
378
|
+
if (found.length == 2)
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
return structuredClone(found);
|
|
382
|
+
}
|
|
383
|
+
getPile() {
|
|
384
|
+
return structuredClone(this._state.cache.pile);
|
|
385
|
+
}
|
|
386
|
+
checkEvent(event, bluffSiteCheck = false) {
|
|
387
|
+
const gameEnded = this.hasEnded();
|
|
388
|
+
if (gameEnded) {
|
|
389
|
+
return new UnocabError(0 /* GameEnded */, { loser: gameEnded.loser }, "This game has ended");
|
|
390
|
+
}
|
|
391
|
+
const [lastEvent] = this.lastTwoNonInfoEvents();
|
|
392
|
+
if (!bluffSiteCheck) {
|
|
393
|
+
const playerCount = this.playersInPlay().length;
|
|
394
|
+
if (playerCount < MIN_PLAYERS) {
|
|
395
|
+
return new UnocabError(10 /* TooFewPlayers */, { playerCount }, "An active game must have at least two players");
|
|
396
|
+
}
|
|
397
|
+
if (!this._state.players.includes(event.by)) {
|
|
398
|
+
return new UnocabError(14 /* PlayerNotInGame */, { playerId: event.by }, "This player is not in the game");
|
|
399
|
+
}
|
|
400
|
+
if (event.by != this.activePlayer()) {
|
|
401
|
+
return new UnocabError(13 /* NotPlayersTurn */, { playerId: event.by }, "It is not your turn");
|
|
402
|
+
}
|
|
403
|
+
if (lastEvent.type == "card_played" && includes(colorSwitchingScs.options, lastEvent.card.type) && lastEvent.by == this.activePlayer() && event.type != "choose-color") {
|
|
404
|
+
return new UnocabError(1 /* MustPickColor */, { performedEvent: event }, "Must pick color after playing a color switching card");
|
|
405
|
+
}
|
|
406
|
+
if (lastEvent.type == "card_played" && lastEvent.card.type == "plus-two" && event.type != "draw") {
|
|
407
|
+
const plusTwoPlayed = event.type == "card_played" && event.card.type == "plus-two";
|
|
408
|
+
if (!plusTwoPlayed) {
|
|
409
|
+
return new UnocabError(3 /* MustDrawOrPlusTwo */, { performedEvent: event }, "Must draw or play another +2 on a +2");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (this.facingPlusFour() && event.type != "draw" && event.type != "bluff_called") {
|
|
413
|
+
return new UnocabError(4 /* MustDrawOrCallBluff */, { performedEvent: event }, "Must either draw or call bluff after +4");
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (event.type == "card_played") {
|
|
417
|
+
const card = event.card;
|
|
418
|
+
if (includes(colorSwitchingScs.options, card.type) && this.handRef(event.by).length == 1) {
|
|
419
|
+
return new UnocabError(2 /* MustNotPlayLastCardColorChanging */, { performedEvent: event }, `Cannot play one of ${colorSwitchingScs.options} as the last card`);
|
|
420
|
+
}
|
|
421
|
+
const { expectedColor, expectedType } = this.expectedCard(bluffSiteCheck);
|
|
422
|
+
const colorMismatch = expectedColor !== undefined && (card.color ?? expectedColor) != expectedColor;
|
|
423
|
+
if (expectedType != card.type && colorMismatch) {
|
|
424
|
+
let msg = "Must play ";
|
|
425
|
+
if (expectedType) {
|
|
426
|
+
msg += `card of type ${expectedType}`;
|
|
427
|
+
}
|
|
428
|
+
if (expectedColor) {
|
|
429
|
+
const ecErr = `color ${expectedColor}`;
|
|
430
|
+
msg += msg == "Must play " ? `card of ${ecErr}` : ` or ${ecErr}`;
|
|
431
|
+
}
|
|
432
|
+
const cardColor = card.color;
|
|
433
|
+
msg += `, found ${card.type}${cardColor ? ` of color ${cardColor}` : ""}`;
|
|
434
|
+
return new UnocabError(5 /* MustPlayExpectedCard */, {
|
|
435
|
+
expected: { expectedType, expectedColor },
|
|
436
|
+
found: card
|
|
437
|
+
}, msg);
|
|
438
|
+
}
|
|
439
|
+
} else if (event.type == "draw") {
|
|
440
|
+
if (lastEvent.type == "draw" && lastEvent.by == event.by) {
|
|
441
|
+
return new UnocabError(6 /* MustNotDrawTwice */, undefined, "Cannot draw twice in a row");
|
|
442
|
+
}
|
|
443
|
+
} else if (event.type == "pass") {
|
|
444
|
+
const hasDrawn = lastEvent.type == "draw" && lastEvent.by == event.by;
|
|
445
|
+
if (!hasDrawn)
|
|
446
|
+
return new UnocabError(7 /* MustNotPassWithoutDraw */, undefined, "Cannot pass without drawing");
|
|
447
|
+
} else if (event.type == "bluff_called") {
|
|
448
|
+
if (!this.facingPlusFour()) {
|
|
449
|
+
return new UnocabError(8 /* CannotCallBluff */, undefined, "You cannot call bluff when the last card is not a +4 or a color has not yet been chosen");
|
|
450
|
+
}
|
|
451
|
+
} else if (event.type == "choose-color") {
|
|
452
|
+
if (lastEvent.type != "card_played" || !includes(colorSwitchingScs.options, lastEvent.card.type) || event.by != lastEvent.by) {
|
|
453
|
+
return new UnocabError(9 /* CannotSwitchColors */, undefined, `You can only switch colors if you played one of [${colorSwitchingScs.options}] in the last turn`);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
processNewEvent(event) {
|
|
458
|
+
event = structuredClone(event);
|
|
459
|
+
if (!playerId.safeParse(event.by).success) {
|
|
460
|
+
throw new UnocabError(12 /* InvalidId */, { id: event.by }, "Invalid player ID");
|
|
461
|
+
}
|
|
462
|
+
const hand = this.handRef(event.by);
|
|
463
|
+
const err = this.checkEvent(event);
|
|
464
|
+
if (err)
|
|
465
|
+
throw err;
|
|
466
|
+
if (event.type == "card_played") {
|
|
467
|
+
const currentCard = event.card;
|
|
468
|
+
const cardIndex = hand.findIndex((c) => c.type == event.card.type && c.color == event.card.color);
|
|
469
|
+
if (cardIndex < 0) {
|
|
470
|
+
throw new UnocabError(15 /* PlayerDoesntHaveCard */, { playerId: event.by, card: event.card }, "You do not have this card");
|
|
471
|
+
}
|
|
472
|
+
const playersInPlay = this.playersInPlay().length;
|
|
473
|
+
hand.splice(cardIndex, 1);
|
|
474
|
+
this.recordEvent(event);
|
|
475
|
+
if (this._state.cache.last3CardPlayOrColorChanges.length == 3)
|
|
476
|
+
this._state.cache.last3CardPlayOrColorChanges.shift();
|
|
477
|
+
this._state.cache.last3CardPlayOrColorChanges.push(currentCard);
|
|
478
|
+
this._state.cache.pile.push(currentCard);
|
|
479
|
+
if (currentCard.type == "plus-two") {
|
|
480
|
+
this._state.cache.stackedPlusTwos++;
|
|
481
|
+
}
|
|
482
|
+
if (currentCard.type == "reverse") {
|
|
483
|
+
this._state.cache.turnStep *= -1;
|
|
484
|
+
}
|
|
485
|
+
if (!includes(colorSwitchingScs.options, currentCard.type)) {
|
|
486
|
+
const steps = (() => {
|
|
487
|
+
if (currentCard.type == "skip") {
|
|
488
|
+
return 2;
|
|
489
|
+
} else if (currentCard.type == "reverse") {
|
|
490
|
+
return playersInPlay == 2 ? 2 : 1;
|
|
491
|
+
}
|
|
492
|
+
return 1;
|
|
493
|
+
})();
|
|
494
|
+
this.advanceTurn(steps);
|
|
495
|
+
}
|
|
496
|
+
if (!hand.length) {
|
|
497
|
+
this.recordEvent({
|
|
498
|
+
type: "game_info",
|
|
499
|
+
info: {
|
|
500
|
+
type: "player_won",
|
|
501
|
+
id: event.by
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
const loser = this.endIfDecided();
|
|
505
|
+
if (loser !== undefined) {
|
|
506
|
+
return {
|
|
507
|
+
type: "player_win_game_end",
|
|
508
|
+
won: event.by,
|
|
509
|
+
lost: loser,
|
|
510
|
+
formatted: `Player ${event.by} won! Game ended.`
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
return {
|
|
514
|
+
type: "player_win",
|
|
515
|
+
won: event.by,
|
|
516
|
+
formatted: `Player ${event.by} won!`
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
type: "card_played",
|
|
521
|
+
card: structuredClone(event.card),
|
|
522
|
+
formatted: `Played a ${event.card.color ? `${event.card.color} ` : ""}${event.card.type}`
|
|
523
|
+
};
|
|
524
|
+
} else if (event.type == "draw") {
|
|
525
|
+
const [lastEvent] = this.lastTwoNonInfoEvents();
|
|
526
|
+
const toDraw = (() => {
|
|
527
|
+
if (lastEvent.type == "card_played" && lastEvent.card.type == "plus-two") {
|
|
528
|
+
const stackedPlusTwos = this._state.stackPlusTwos ? this._state.cache.stackedPlusTwos : 1;
|
|
529
|
+
return stackedPlusTwos * 2;
|
|
530
|
+
}
|
|
531
|
+
if (this.facingPlusFour())
|
|
532
|
+
return 4;
|
|
533
|
+
return 1;
|
|
534
|
+
})();
|
|
535
|
+
const drawCards = this.takeCards(toDraw);
|
|
536
|
+
hand.push(...drawCards);
|
|
537
|
+
if (toDraw != 1) {
|
|
538
|
+
this.advanceTurn();
|
|
539
|
+
}
|
|
540
|
+
this.recordEvent(event);
|
|
541
|
+
this._state.cache.stackedPlusTwos = 0;
|
|
542
|
+
return {
|
|
543
|
+
type: "drawing_cards",
|
|
544
|
+
cards: structuredClone(drawCards),
|
|
545
|
+
count: drawCards.length,
|
|
546
|
+
formatted: `Drawing ${drawCards.length} card(s)`
|
|
547
|
+
};
|
|
548
|
+
} else if (event.type == "pass") {
|
|
549
|
+
this.recordEvent(event);
|
|
550
|
+
this.advanceTurn();
|
|
551
|
+
return {
|
|
552
|
+
type: "turn_passed",
|
|
553
|
+
formatted: "Turn passed"
|
|
554
|
+
};
|
|
555
|
+
} else if (event.type == "bluff_called") {
|
|
556
|
+
const [, plusFourEvent] = this.lastTwoNonInfoEvents();
|
|
557
|
+
const lastPlayerId = plusFourEvent.by;
|
|
558
|
+
const lastPlayerHand = this.handRef(lastPlayerId);
|
|
559
|
+
const bluffed = lastPlayerHand.filter((c) => !includes(colorSwitchingScs.options, c.type)).some((c) => !this.checkEvent({
|
|
560
|
+
type: "card_played",
|
|
561
|
+
card: c,
|
|
562
|
+
by: ENGINE_ID
|
|
563
|
+
}, true));
|
|
564
|
+
const penalisedHand = bluffed ? lastPlayerHand : hand;
|
|
565
|
+
penalisedHand.push(...this.takeCards(bluffed ? 4 : 6));
|
|
566
|
+
this.recordEvent(event);
|
|
567
|
+
this.advanceTurn();
|
|
568
|
+
const info = {
|
|
569
|
+
type: bluffed ? "bluff_call_succeeded" : "bluff_call_failed",
|
|
570
|
+
by: event.by,
|
|
571
|
+
of: lastPlayerId
|
|
572
|
+
};
|
|
573
|
+
this.recordEvent({
|
|
574
|
+
type: "game_info",
|
|
575
|
+
info
|
|
576
|
+
});
|
|
577
|
+
return {
|
|
578
|
+
...info,
|
|
579
|
+
formatted: bluffed ? "Bluff called! giving 4 cards to the previous player." : "Bluff call failed! Giving 6 cards to current player"
|
|
580
|
+
};
|
|
581
|
+
} else if (event.type == "choose-color") {
|
|
582
|
+
if (this._state.cache.last3CardPlayOrColorChanges.length == 3)
|
|
583
|
+
this._state.cache.last3CardPlayOrColorChanges.shift();
|
|
584
|
+
this._state.cache.last3CardPlayOrColorChanges.push(event.color);
|
|
585
|
+
this.recordEvent(event);
|
|
586
|
+
this.advanceTurn();
|
|
587
|
+
return {
|
|
588
|
+
type: "color_changed",
|
|
589
|
+
to: event.color,
|
|
590
|
+
formatted: `Color changed to ${event.color}`
|
|
591
|
+
};
|
|
592
|
+
} else
|
|
593
|
+
exhaustive(event);
|
|
594
|
+
throw "unreachable";
|
|
595
|
+
}
|
|
596
|
+
join(...playerIds) {
|
|
597
|
+
const playerCount = this._state.players.length;
|
|
598
|
+
if (playerCount + playerIds.length > MAX_PLAYERS) {
|
|
599
|
+
throw new UnocabError(11 /* TooManyPlayers */, { playerCount }, "At most 10 players can join a game");
|
|
600
|
+
}
|
|
601
|
+
const seated = new Set(this._state.players);
|
|
602
|
+
playerIds.forEach((id) => {
|
|
603
|
+
if (id == ENGINE_ID || !playerId.safeParse(id).success) {
|
|
604
|
+
throw new UnocabError(12 /* InvalidId */, { id }, `${id} is an invalid id`);
|
|
605
|
+
}
|
|
606
|
+
if (seated.has(id)) {
|
|
607
|
+
throw new UnocabError(17 /* PlayerAlreadyInGame */, { playerId: id }, "This player is already in the game");
|
|
608
|
+
}
|
|
609
|
+
seated.add(id);
|
|
610
|
+
});
|
|
611
|
+
const newPlayers = playerIds.filter((id) => !this._state.cache.hands[id]);
|
|
612
|
+
const dealtCards = this.takeCards(7 * newPlayers.length);
|
|
613
|
+
playerIds.forEach((playerId2) => {
|
|
614
|
+
this._state.players.push(playerId2);
|
|
615
|
+
this._state.cache.hands[playerId2] ??= dealtCards.splice(0, 7);
|
|
616
|
+
this.recordEvent({
|
|
617
|
+
type: "game_info",
|
|
618
|
+
info: {
|
|
619
|
+
type: "player_joined",
|
|
620
|
+
id: playerId2
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
leave(...playerIds) {
|
|
626
|
+
playerIds.forEach((playerId2) => {
|
|
627
|
+
const seat = this._state.players.indexOf(playerId2);
|
|
628
|
+
if (seat < 0) {
|
|
629
|
+
throw new UnocabError(14 /* PlayerNotInGame */, { playerId: playerId2 }, "This player is not in the game");
|
|
630
|
+
}
|
|
631
|
+
if (this.activePlayer() == playerId2)
|
|
632
|
+
this.advanceTurn();
|
|
633
|
+
const nextActive = this.activePlayer();
|
|
634
|
+
this._state.players.splice(seat, 1);
|
|
635
|
+
this._state.index = Math.max(0, this._state.players.indexOf(nextActive));
|
|
636
|
+
this.recordEvent({
|
|
637
|
+
type: "game_info",
|
|
638
|
+
info: {
|
|
639
|
+
type: "player_left",
|
|
640
|
+
id: playerId2
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
this.endIfDecided();
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
play(by, card) {
|
|
647
|
+
return this.processNewEvent({
|
|
648
|
+
type: "card_played",
|
|
649
|
+
by,
|
|
650
|
+
card
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
draw(by) {
|
|
654
|
+
return this.processNewEvent({
|
|
655
|
+
type: "draw",
|
|
656
|
+
by
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
pass(by) {
|
|
660
|
+
return this.processNewEvent({
|
|
661
|
+
type: "pass",
|
|
662
|
+
by
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
callBluff(by) {
|
|
666
|
+
return this.processNewEvent({
|
|
667
|
+
type: "bluff_called",
|
|
668
|
+
by
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
chooseColor(by, color) {
|
|
672
|
+
return this.processNewEvent({
|
|
673
|
+
type: "choose-color",
|
|
674
|
+
by,
|
|
675
|
+
color
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
handOf(playerId2) {
|
|
679
|
+
return structuredClone(this.handRef(playerId2));
|
|
680
|
+
}
|
|
681
|
+
serialise() {
|
|
682
|
+
return JSON.stringify(this._state);
|
|
683
|
+
}
|
|
684
|
+
clone() {
|
|
685
|
+
return new Game({ serialisedState: this.serialise() });
|
|
686
|
+
}
|
|
687
|
+
eventsOfType(type) {
|
|
688
|
+
return structuredClone(this._state.events.filter((event) => event.type == type));
|
|
689
|
+
}
|
|
690
|
+
activePlayer() {
|
|
691
|
+
const players = this._state.players;
|
|
692
|
+
return players[mod(this._state.index, players.length)];
|
|
693
|
+
}
|
|
694
|
+
hasEnded() {
|
|
695
|
+
const ended = this._state.cache.ended;
|
|
696
|
+
return ended ? { loser: ended.loser } : false;
|
|
697
|
+
}
|
|
698
|
+
expectedCard(bluffSiteCheck = false) {
|
|
699
|
+
const expectedType = (() => {
|
|
700
|
+
const lastPileCard = this._state.cache.pile.at(-1);
|
|
701
|
+
if (lastPileCard && !includes(colorSwitchingScs.options, lastPileCard.type)) {
|
|
702
|
+
return lastPileCard.type;
|
|
703
|
+
}
|
|
704
|
+
})();
|
|
705
|
+
const expectedColor = (() => {
|
|
706
|
+
const lcc = this._state.cache.last3CardPlayOrColorChanges.at(bluffSiteCheck ? -3 : -1);
|
|
707
|
+
if (lcc) {
|
|
708
|
+
return typeof lcc == "string" ? lcc : lcc.color;
|
|
709
|
+
}
|
|
710
|
+
return;
|
|
711
|
+
})();
|
|
712
|
+
return {
|
|
713
|
+
expectedType,
|
|
714
|
+
expectedColor
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
jumpToEventIndex(index) {
|
|
718
|
+
if (this._state.shorthandMode) {
|
|
719
|
+
throw new UnocabError(18 /* CannotJumpInShorthandMode */, undefined, "Cannot jump to index in shorthand mode");
|
|
720
|
+
}
|
|
721
|
+
const length = this._state.events.length;
|
|
722
|
+
if (index < -length || index >= length) {
|
|
723
|
+
throw new UnocabError(19 /* InvalidEventIndex */, { index }, "Invalid event index");
|
|
724
|
+
}
|
|
725
|
+
if (index < 0) {
|
|
726
|
+
index = this._state.events.length + index;
|
|
727
|
+
}
|
|
728
|
+
const remainingEvents = this._state.events.splice(index + 1);
|
|
729
|
+
const cg = new Game({
|
|
730
|
+
initialSeed: this._state.initialSeed,
|
|
731
|
+
stackPlusTwos: this._state.stackPlusTwos
|
|
732
|
+
});
|
|
733
|
+
for (const event of this.state.events) {
|
|
734
|
+
if (event.type == "game_info") {
|
|
735
|
+
const info = event.info;
|
|
736
|
+
if (info.type == "player_joined")
|
|
737
|
+
cg.join(info.id);
|
|
738
|
+
if (info.type == "player_left")
|
|
739
|
+
cg.leave(info.id);
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
if (event.by == ENGINE_ID)
|
|
743
|
+
continue;
|
|
744
|
+
cg.processNewEvent(event);
|
|
745
|
+
}
|
|
746
|
+
this._state = cg.state;
|
|
747
|
+
return remainingEvents;
|
|
748
|
+
}
|
|
749
|
+
validEvents(playerId2) {
|
|
750
|
+
const allEvents = [
|
|
751
|
+
{ type: "draw", by: playerId2 },
|
|
752
|
+
{ type: "bluff_called", by: playerId2 },
|
|
753
|
+
{ type: "pass", by: playerId2 },
|
|
754
|
+
{ type: "choose-color", by: playerId2, color: "red" },
|
|
755
|
+
...this.handOf(playerId2).map((card) => ({
|
|
756
|
+
type: "card_played",
|
|
757
|
+
by: playerId2,
|
|
758
|
+
card
|
|
759
|
+
}))
|
|
760
|
+
];
|
|
761
|
+
return allEvents.filter((e) => !this.checkEvent(e));
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
export {
|
|
765
|
+
Game,
|
|
766
|
+
UECode,
|
|
767
|
+
UnocabError,
|
|
768
|
+
allCards,
|
|
769
|
+
bluffCalledEvent,
|
|
770
|
+
card,
|
|
771
|
+
cardColor,
|
|
772
|
+
cardPlayedEvent,
|
|
773
|
+
chooseColorEvent,
|
|
774
|
+
colorSwitchingScs,
|
|
775
|
+
drawEvent,
|
|
776
|
+
fullGameState,
|
|
777
|
+
gameEvent,
|
|
778
|
+
gameState,
|
|
779
|
+
infoEvent,
|
|
780
|
+
nonColorSwitchingScs,
|
|
781
|
+
passEvent,
|
|
782
|
+
playerId,
|
|
783
|
+
regularCards,
|
|
784
|
+
shorthandGameState,
|
|
785
|
+
specialCards
|
|
786
|
+
};
|