-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
1140 lines (1011 loc) Β· 31.4 KB
/
game.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { UECode, UnocabError } from "./errors.ts";
import {
Card,
CardColor,
CardPlayedEvent,
FullGameState,
GameEvent,
GameState,
InfoEvent,
PlayerId,
ProcessEventResult,
ShorthandGameState,
colorSwitchingScs,
gameState,
playerId,
} from "./types.ts";
import {
DECK,
ENGINE_ID,
MAX_PLAYERS,
MAX_SHORTHAND_EVENTS,
MIN_PLAYERS,
XOR,
cyrb128,
exhaustive,
includes,
} from "./utils.ts";
export class Game<Shorthand extends boolean = false> {
// FullGameState if Shorthand is false.
// ShorthandGameState if Shorthand is true.
// GameState (FullGameState | ShorthandGameState) if Shorthand is boolean.
protected _state: boolean extends Shorthand
? GameState
: true extends Shorthand
? ShorthandGameState
: FullGameState;
get state() {
return structuredClone(this._state) as typeof this._state;
}
constructor(
// if a serialised state is provided, none of the
// customisation props are used, so don't allow
// passing them together with the serialised state.
props: XOR<
{ serialisedState?: string },
{
shorthandMode?: Shorthand;
stackPlusTwos?: boolean;
initialSeed?: string | number;
noInit?: boolean;
}
> = {}
) {
if ("serialisedState" in props && props.serialisedState) {
const state = JSON.parse(props.serialisedState);
gameState.parse(state);
this._state = state;
} else {
const initialSeed = (() => {
if (props.initialSeed) {
return typeof props.initialSeed == "string"
? cyrb128(props.initialSeed)
: props.initialSeed;
}
const randomValues = new Uint32Array(1);
crypto.getRandomValues(randomValues);
return randomValues[0];
})();
const commonState = {
index: 0,
players: [],
initialSeed,
stackPlusTwos: props.stackPlusTwos ?? true,
cache: {
hands: {},
deck: [],
seed: initialSeed,
ended: false,
stackedPlusTwos: 0,
turnStep: 1,
pile: [],
last3CardPlayOrColorChanges: [],
},
} satisfies Omit<GameState, "shorthandMode" | "events">;
if (props.shorthandMode) {
const shorthandGameState = {
shorthandMode: true,
events: [],
...commonState,
} satisfies ShorthandGameState;
// @ts-expect-error todo: figure out why we cannot narrow here
this._state = shorthandGameState;
} else {
const fullGameState = {
shorthandMode: false,
events: [],
...commonState,
} satisfies FullGameState;
// @ts-expect-error todo: figure out why we cannot narrow here
this._state = fullGameState;
}
if (!props.noInit) {
// assign everything that needs random(), since it
// cannot be used before _state is assigned.
this.init();
}
}
}
// mulberry32
protected random(): number {
let t = (this._state.cache.seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
protected shuffleDeck() {
const toShuffle = this._state.cache.deck;
for (let i = toShuffle.length - 1; i > 0; i--) {
const j = Math.floor(this.random() * (i + 1));
[toShuffle[i], toShuffle[j]] = [toShuffle[j], toShuffle[i]];
}
this.recordEvent({
type: "game_info",
info: {
type: "seed_changed",
newSeed: this._state.cache.seed,
},
});
}
protected nonInfoEvents() {
return this.state.events.filter((e) => e.type !== "game_info");
}
protected init(customDeckTopCard?: Card) {
this._state.cache.deck = structuredClone(DECK);
this.shuffleDeck();
// todo: return the deck top card here, and figure out what
// happens when it happens to be a color switching card.
const pileTopCard = (() => {
if (customDeckTopCard) return customDeckTopCard;
const index = this._state.cache.deck.findIndex(
(card) => !includes(colorSwitchingScs.options, card.type)
);
const [ptc] = this._state.cache.deck.splice(index, 1);
return ptc;
})();
const firstCardPlayEvent = {
type: "card_played",
by: ENGINE_ID,
card: pileTopCard,
} satisfies CardPlayedEvent;
this._state.events = [firstCardPlayEvent];
this._state.cache.pile = [pileTopCard];
this._state.cache.last3CardPlayOrColorChanges = [pileTopCard];
}
protected recordEvent(event: GameEvent | InfoEvent) {
if (this._state.shorthandMode) {
if (event.type == "game_info") return;
// in shorthand mode, we keep at most MAX_SHORTHAND_EVENTS
if (this._state.events.length == MAX_SHORTHAND_EVENTS) {
this._state.events.shift();
}
this._state.events.push(event);
} else this._state.events.push(event);
}
protected deckTopCard() {
// if the deck is empty, shuffle the playing pile and make it the new deck
if (!this._state.cache.deck.length) {
const pile = this.getPile();
if (!pile.length) {
throw new UnocabError(
UECode.DeckPileExhausted,
undefined,
"Both the deck and pile have been exhausted, cannot draw"
);
}
this._state.cache.deck = pile;
this.shuffleDeck();
this._state.cache.pile = [];
this.recordEvent({
type: "game_info",
info: { type: "pile_to_deck" },
});
}
return this._state.cache.deck.shift()!;
}
/**
* Returns the last two events that don't have the type "game_info".
* @param ignoredEvents - the number of non-info events to ignore from
* the end. This param is useful, say, when checking during a bluff call,
* where you might need to ignore the last 3 events.
*/
lastTwoNonInfoEvents(ignoredEvents?: number) {
// one game event is always guaranteed to be present, since at the
// start of the game, the engine always plays a card.
return this.nonInfoEvents()
.slice(-2 + (ignoredEvents ?? 0), ignoredEvents)
.reverse() as [GameEvent] | [GameEvent, GameEvent];
}
/**
* Returns the playing pile. This is where the first card is
* played, and where all the subsequent cards played by players
* are put. It is guaranteed to have one card at the start.
*
* ```typescript
* const game = new Game();
* game.play(p1, { type: "one", color: "red" });
* game.play(p2, { type: "one", color: "green" });
*
* assertEquals(game.getPile(), [
* { type: "six", color: "red" }, // randomly chosen by engine
* { type: "one", color: "red" },
* { type: "one", color: "green" },
* ]);
* ```
*/
getPile(): Card[] {
return structuredClone(this._state.cache.pile);
}
/**
* @param bluffSiteCheck - if set to true, the last 3 events will be ignored.
*
* Check whether the given event is valid at the current
* state of the game.
*
* ```typescript
* const game = new Game();
* game.draw(p1);
*
* // false, since you cannot call bluff after drawing
* game.checkEvent({ type: "bluff_called", by: p1 });
*
* // true, since you can pass after drawing
* game.checkEvent({ type: "pass", by: p1 }); // true
*
* game.pass(p1);
*
* // false, since you cannot pass without drawing
* game.checkEvent({ type: "draw", by: p2 }); // false
* ```
*/
checkEvent(event: GameEvent, bluffSiteCheck = false) {
if (!bluffSiteCheck) {
// if a player leaves before another player calls bluff, this should still work
const playerCount = this._state.players.length;
if (playerCount < MIN_PLAYERS) {
return new UnocabError(
UECode.TooFewPlayers,
{ playerCount },
"An active game must have at least two players"
);
}
// during a bluff site check, an actual move is not being checked for
if (event.by != this.activePlayer()) {
return new UnocabError(
UECode.NotPlayersTurn,
{ playerId: event.by },
"It is not your turn"
);
}
}
const gameEnded = this.hasEnded();
if (gameEnded) {
return new UnocabError(
UECode.GameEnded,
{ loser: gameEnded.loser },
"This game has ended"
);
}
// during a bluff site check, we need to not consider
// the last 3 events, since those would be the playing
// of +4, a color change, and a bluff call. By going
// back 3 steps, we reach a point where we check the
// event as if the player has not yet played a +4. This
// allows us to figure out if the player was bluffing.
const [lastEvent, secondLastEvent] = this.lastTwoNonInfoEvents(
bluffSiteCheck ? -3 : undefined
);
// if the last card was a color switching card, enforce color choose
if (
lastEvent.type == "card_played" &&
includes(colorSwitchingScs.options, lastEvent.card.type) &&
event.type != "choose-color"
) {
return new UnocabError(
UECode.MustPickColor,
{ performedEvent: event },
"Must pick color after playing a color switching card"
);
}
// if the last card was a +2, enforce draw or play of another +2
if (
lastEvent.type == "card_played" &&
lastEvent.card.type == "plus-two" &&
event.type != "draw"
) {
const plusTwoPlayed =
event.type == "card_played" && event.card.type == "plus-two";
if (!plusTwoPlayed) {
return new UnocabError(
UECode.MustDrawOrPlusTwo,
{ performedEvent: event },
"Must draw or play another +2 on a +2"
);
}
}
if (secondLastEvent) {
// if the last card was a +4, enforce draw or call bluff
if (
secondLastEvent.type == "card_played" &&
secondLastEvent.card.type == "plus-four" &&
event.type != "draw" &&
event.type != "bluff_called"
) {
return new UnocabError(
UECode.MustDrawOrCallBluff,
{ performedEvent: event },
"Must either draw or call bluff after +4"
);
}
}
if (event.type == "card_played") {
const card = event.card;
if (
includes(colorSwitchingScs.options, card.type) &&
this.handOf(event.by).length == 1
) {
return new UnocabError(
UECode.MustNotPlayLastCardColorChanging,
{ performedEvent: event },
`Cannot play one of ${colorSwitchingScs.options} as the last card`
);
}
// if any of these are/have undefined, they become a non-condition.
const { expectedColor, expectedType } = this.expectedCard(bluffSiteCheck);
if (
expectedType != card.type &&
(card.color ?? expectedColor) != expectedColor
) {
let msg = "Must play ";
if (expectedType) {
msg += `card of type ${expectedType}`;
}
if (expectedColor) {
const ecErr = `color ${expectedColor}`;
msg += msg == "Must play " ? `card of ${ecErr}` : ` or ${ecErr}`;
}
const cardColor = card.color;
msg += `, found ${card.type}${
cardColor ? ` of color ${cardColor}` : ""
}`;
return new UnocabError(
UECode.MustPlayExpectedCard,
{
expected: { expectedType, expectedColor },
found: card,
},
msg
);
}
} else if (event.type == "draw") {
if (lastEvent.type == "draw" && lastEvent.by == event.by) {
return new UnocabError(
UECode.MustNotDrawTwice,
undefined,
"Cannot draw twice in a row"
);
}
} else if (event.type == "pass") {
const hasDrawn = lastEvent.type == "draw" && lastEvent.by == event.by;
if (!hasDrawn)
return new UnocabError(
UECode.MustNotPassWithoutDraw,
undefined,
"Cannot pass without drawing"
);
} else if (event.type == "bluff_called") {
const [, secondLastEvent] = this.lastTwoNonInfoEvents();
if (
!secondLastEvent ||
secondLastEvent.type != "card_played" ||
secondLastEvent.card.type != "plus-four"
) {
return new UnocabError(
UECode.CannotCallBluff,
undefined,
"You cannot call bluff when the last card is not a +4 or a color has not yet been chosen"
);
}
} else if (event.type == "choose-color") {
if (
lastEvent.type != "card_played" ||
!includes(colorSwitchingScs.options, lastEvent.card.type) ||
event.by != lastEvent.by
) {
return new UnocabError(
UECode.CannotSwitchColors,
undefined,
`You can only switch colors if you played one of [${colorSwitchingScs.options}] in the last turn`
);
}
}
}
// βββ Event Processing Method βββββββββββββββββββββββββββββββββββββββββ
/**
* This function can be used as an alternative to the user-friendly
* methods like {@link Game.draw} or {@link Game.pass}, where you are
* allowed to directly pass in a {@link GameEvent}.
*
* ```typescript
* const game = new Game();
*
* // regular way
* game.draw(p1);
*
* // processNewEvent way
* game.processNewEvent({ type: "draw", by: p1 });
* ```
*/
processNewEvent(event: GameEvent): ProcessEventResult {
if (!playerId.safeParse(event.by).success) {
throw new UnocabError(
UECode.InvalidId,
{ id: event.by },
"Invalid player ID"
);
}
const hand = this.handOf(event.by);
const err = this.checkEvent(event);
if (err) throw err;
// βββ Card Played Event βββββββββββββββββββββββββββββββ
if (event.type == "card_played") {
const currentCard = event.card;
const cardIndex = hand.findIndex(
(c) => c.type == event.card.type && c.color == event.card.color
);
if (cardIndex < 0) {
throw new UnocabError(
UECode.PlayerDoesntHaveCard,
{ playerId: event.by, card: event.card },
"You do not have this card"
);
}
// remove the card from the players deck
hand.splice(cardIndex, 1);
// add this event to the game events
this.recordEvent(event);
if (this._state.cache.last3CardPlayOrColorChanges.length == 3)
this._state.cache.last3CardPlayOrColorChanges.shift();
this._state.cache.last3CardPlayOrColorChanges.push(currentCard);
this._state.cache.pile.push(currentCard);
if (currentCard.type == "plus-two") {
this._state.cache.stackedPlusTwos++;
}
if (currentCard.type == "reverse") {
this._state.cache.turnStep *= -1;
}
if (!includes(colorSwitchingScs.options, currentCard.type)) {
const turnStepMultiplier = (() => {
if (currentCard.type == "skip") {
return 2;
} else if (currentCard.type == "reverse") {
return this._state.players.length == 2 ? 2 : 1;
}
return 1;
})();
this._state.index += this._state.cache.turnStep * turnStepMultiplier;
}
if (!this.handOf(event.by).length) {
// add 'player_won' event if the player's deck is now empty
this.recordEvent({
type: "game_info",
info: {
type: "player_won",
id: event.by,
},
});
// if there's only one person left with cards, end the game
const playersWithCards = Object.entries(this._state.cache.hands)
.filter(([, v]) => v.length)
.map(([k]) => k);
if (playersWithCards.length == 1) {
this.recordEvent({
type: "game_info",
info: {
type: "game_ended",
loser: playersWithCards[0],
},
});
this._state.cache.ended = true;
return {
type: "player_win_game_end",
won: event.by,
lost: playersWithCards[0],
formatted: `Player ${event.by} won! Game ended.`,
};
}
return {
type: "player_win",
won: event.by,
formatted: `Player ${event.by} won!`,
};
}
return {
type: "card_played",
card: event.card,
formatted: `Played a ${event.card.color ? `${event.card.color} ` : ""}${
event.card.type
}`,
};
}
// βββ Draw Event ββββββββββββββββββββββββββββββββββββββββββββββ
else if (event.type == "draw") {
const [lastEvent, secondLastEvent] = this.lastTwoNonInfoEvents();
const toDraw = (() => {
if (
lastEvent.type == "card_played" &&
lastEvent.card.type == "plus-two"
) {
const stackedPlusTwos = this._state.stackPlusTwos
? this._state.cache.stackedPlusTwos
: 1;
return stackedPlusTwos * 2;
}
if (
secondLastEvent &&
secondLastEvent.type == "card_played" &&
secondLastEvent.card.type == "plus-four"
) {
return 4;
}
return 1;
})();
const drawCards = Array(toDraw)
.fill(null)
.map(() => this.deckTopCard());
this.handOf(event.by).push(...drawCards);
// forfeit the turn after drawing 2/4 cards
if (toDraw != 1) {
this._state.index += this._state.cache.turnStep;
}
// add this event to the game events
this.recordEvent(event);
this._state.cache.stackedPlusTwos = 0;
return {
type: "drawing_cards",
cards: drawCards,
count: drawCards.length,
formatted: `Drawing ${drawCards.length} card(s)`,
};
}
// βββ Pass Event ββββββββββββββββββββββββββββββββββββββββββββββ
else if (event.type == "pass") {
// add this event to the game events and increase the turn
this.recordEvent(event);
this._state.index += this._state.cache.turnStep;
return {
type: "turn_passed",
formatted: "Turn passed",
};
}
// βββ Bluff Called Event ββββββββββββββββββββββββββββββββββββββ
else if (event.type == "bluff_called") {
// this cast is safe, because it has already been checked for
// in the checkEvent() function above.
const [, secondLastEvent] = this.lastTwoNonInfoEvents() as [
GameEvent,
CardPlayedEvent
];
const lastPlayerId = secondLastEvent!.by;
const lastPlayerHand = this.handOf(lastPlayerId)!;
// add this event to the game events and increase the step
this.recordEvent(event);
this._state.index += this._state.cache.turnStep;
if (
lastPlayerHand
.filter((c) => !includes(colorSwitchingScs.options, c.type))
.some(
(c) =>
// if the return value is not an error, it's a valid play
!this.checkEvent(
{
type: "card_played",
card: c,
by: ENGINE_ID,
},
true
)
)
) {
// bluff call succeeded, give 4 cards to last player
lastPlayerHand.push(
...Array(4)
.fill(null)
.map(() => this.deckTopCard())
);
const info = {
type: "bluff_call_succeeded" as const,
by: event.by,
of: lastPlayerId,
};
this.recordEvent({
type: "game_info",
info,
});
return {
...info,
formatted: "Bluff called! giving 4 cards to the previous player.",
} as const;
} else {
// bluff call failed, give 6 cards to current player
this.handOf(event.by).push(
...Array(6)
.fill(null)
.map(() => this.deckTopCard())
);
const info = {
type: "bluff_call_failed" as const,
by: event.by,
of: lastPlayerId,
};
this.recordEvent({
type: "game_info",
info,
});
return {
...info,
formatted: "Bluff call failed! Giving 6 cards to current player",
} as const;
}
}
// βββ Choose Color Event ββββββββββββββββββββββββββββββ
else if (event.type == "choose-color") {
if (this._state.cache.last3CardPlayOrColorChanges.length == 3)
this._state.cache.last3CardPlayOrColorChanges.shift();
this._state.cache.last3CardPlayOrColorChanges.push(event.color);
// add this event to the game events and increase the turn
this.recordEvent(event);
this._state.index += this._state.cache.turnStep;
return {
type: "color_changed",
to: event.color,
formatted: `Color changed to ${event.color}`,
};
} else exhaustive(event);
throw "unreachable";
}
// βββ Gameplay Functions ββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Have player(s) denoted by an ID join the game.
*
* ```typescript
* const game = new Game();
* const p1 = uuidv4();
* const p2 = uuidv4();
* game.join(p1, p2);
* ```
*/
join(...playerIds: PlayerId[]) {
const playerCount = this._state.players.length;
if (playerCount == MAX_PLAYERS) {
throw new UnocabError(
UECode.TooManyPlayers,
{ playerCount },
"At most 10 players can join a game"
);
}
playerIds.forEach((id) => {
if (id == ENGINE_ID || !playerId.safeParse(id).success) {
throw new UnocabError(
UECode.InvalidId,
{ id },
`${id} is an invalid id`
);
}
});
playerIds.forEach((playerId) => {
const hand = Array(7)
.fill(null)
.map(() => this.deckTopCard());
this._state.players.push(playerId);
this._state.cache.hands[playerId] = hand;
this.recordEvent({
type: "game_info",
info: {
type: "player_joined",
id: playerId,
},
});
});
}
/**
* Have player(s) denoted by an ID join the game.
*
* ```typescript
* const game = new Game();
* const p1 = uuidv4();
* const p2 = uuidv4();
* game.join(p1, p2);
*
* game.leave(p1, p2);
* ```
*/
leave(...playerIds: PlayerId[]) {
playerIds.forEach((playerId) => {
if (!this._state.players.find((id) => id == playerId)) {
throw new UnocabError(
UECode.PlayerNotInGame,
{ playerId },
"This player is not in the game"
);
}
this._state.players = this._state.players.filter((id) => id != playerId);
this.recordEvent({
type: "game_info",
info: {
type: "player_left",
id: playerId,
},
});
});
}
/**
* Have a player denoted by an ID play a card. Throws
* an {@link UnocabError} in case the player does not
* have the given card, or if playing it is an invalid move.
*
* ```typescript
* const game = new Game();
* game.play(p1, { type: "one", color: "red" });
* ```
*/
play(by: PlayerId, card: Card) {
return this.processNewEvent({
type: "card_played",
by,
card,
});
}
/**
* Have a player denoted by an ID draw a card. Throws
* an {@link UnocabError} in case it's invalid to draw.
*
* ```typescript
* const game = new Game();
* game.draw(p1);
* ```
*/
draw(by: PlayerId) {
return this.processNewEvent({
type: "draw",
by,
});
}
/**
* Have a player denoted by an ID draw a card. Throws
* an {@link UnocabError} in case it's invalid to pass.
*
* ```typescript
* const game = new Game();
* game.draw(p1);
* game.pass(p1);
* ```
*/
pass(by: PlayerId) {
return this.processNewEvent({
type: "pass",
by,
});
}
/**
* Have a player denoted by an ID call bluff. Throws
* an {@link UnocabError} in case a +4 was not played
* as the previous event, or if it is an invalid move.
*
* ```typescript
* const game = new Game();
* game.play(p1, { type: "plus-four" });
* game.switchColor(p1, "red");
* game.callBluff(p2);
* ```
*/
callBluff(by: PlayerId) {
return this.processNewEvent({
type: "bluff_called",
by,
});
}
/**
* Have a player denoted by an ID switch color. Throws
* an {@link UnocabError} in case a color switching card
* was not played as the previous event, or if it is an invalid move.
*
* ```typescript
* const game = new Game();
* game.play(p1, { type: "plus-four" });
* game.switchColor(p1, "red");
* ```
*/
chooseColor(by: PlayerId, color: CardColor) {
return this.processNewEvent({
type: "choose-color",
by,
color,
});
}
/**
* Fetches the hand of a player denoted by an ID.
* Throws an {@link UnocabError} if the player is
* not present in the game.
*/
handOf(playerId: PlayerId): Card[] {
const hand = this._state.cache.hands[playerId];
if (!hand)
throw new UnocabError(
UECode.PlayerNotInGame,
{ playerId },
"This player is not in the game"
);
return hand;
}
// βββ Util Functions ββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Serialises the game state into a JSON. This can then
* be passed to the constructor when creating a new game
* in order to recreate the game.
*
* ```typescript
* const game = new Game({ shorthandMode: true });
*
* const json = game.serialise();
*
* const recreatedGame = new Game<true>({ serialisedState: json });
* ```
*/
serialise() {
return JSON.stringify(this._state);
}
/**
* Clones this game to create another game with the exact
* same state and set of players.
*/
clone() {
return new Game<Shorthand>({ serialisedState: this.serialise() });
}
/**
* Fetches events of the given type from the game.
*
* ```typescript
* const game = new Game();
* game.draw(p1);
* game.pass(p1);
* game.draw(p2);
*
* assertEquals(game.eventsOfType("draw"), [
* { type: "draw", by: p1 },
* { type: "draw", by: p2 }
* ]);
* ```
*/
eventsOfType<T extends GameState["events"][number]["type"]>(type: T) {
type FilteredType = Extract<GameState["events"][number], { type: T }>;
return this._state.events.filter(
(event) => event.type == type
) as FilteredType[];
}
/**
* Get the player ID of the active player who is expected to play.
*
* ```typescript
* const game = new Game();
*
* assertEquals(game.activePlayer(), p1);
*
* game.draw(p1);
* game.pass(p1);
*
* assertEquals(game.activePlayer(), p2);
* ```
*/
activePlayer(): PlayerId {
const validPlayers = Object.entries(this._state.cache.hands)
.filter(([, v]) => v)
.map(([k]) => k);
const index = Math.abs(this._state.index % validPlayers.length);
return validPlayers[index];
}
/**
* Checks whether this game has ended. If it has, returns an
* object containing the player ID of the losing player.