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/README.md
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
# Unocab
|
|
2
2
|
|
|
3
|
-
Unocab is an UNO game engine.
|
|
3
|
+
Unocab is an UNO game engine.
|
|
4
|
+
|
|
5
|
+
# Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add unocab
|
|
9
|
+
# or
|
|
10
|
+
npm install unocab
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# Usage
|
|
4
14
|
|
|
5
15
|
```typescript
|
|
6
16
|
// On creation of a Game, an UNO deck of 108 cards is
|
|
@@ -43,7 +53,7 @@ game.draw(p2);
|
|
|
43
53
|
game.pass(p2);
|
|
44
54
|
|
|
45
55
|
game.play(p1, { type: "plus-four" });
|
|
46
|
-
game.
|
|
56
|
+
game.chooseColor(p1, "blue");
|
|
47
57
|
|
|
48
58
|
game.callBluff(p2);
|
|
49
59
|
|
|
@@ -102,7 +112,17 @@ directly be used like `game.processNewEvent(event)`, which acts as an alternativ
|
|
|
102
112
|
|
|
103
113
|
# Demo implementations
|
|
104
114
|
|
|
105
|
-
Please refer to `impls/cli-uno.ts` for a cli-based game with an AI.
|
|
106
|
-
There's also `impls/autoplay.ts`, which is an AI vs AI game.
|
|
115
|
+
Please refer to `impls/cli-uno.ts` for a cli-based game with an AI (`bun run play`).
|
|
116
|
+
There's also `impls/autoplay.ts`, which is an AI vs AI game (`bun run autoplay`).
|
|
107
117
|
|
|
108
118
|
(AI here naturally means if conditions, ofcourse!)
|
|
119
|
+
|
|
120
|
+
# Development
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
bun install
|
|
124
|
+
bun test # run the tests
|
|
125
|
+
bun check # type check
|
|
126
|
+
bun fmt # format
|
|
127
|
+
bun run build # build the npm package into dist/
|
|
128
|
+
```
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Card, GameEvent, PlayerId } from "./types.
|
|
2
|
-
export declare
|
|
1
|
+
import type { Card, GameEvent, PlayerId } from "./types.ts";
|
|
2
|
+
export declare enum UECode {
|
|
3
3
|
GameEnded = 0,
|
|
4
4
|
MustPickColor = 1,
|
|
5
5
|
MustNotPlayLastCardColorChanging = 2,
|
|
@@ -16,19 +16,26 @@ export declare const enum UECode {
|
|
|
16
16
|
NotPlayersTurn = 13,
|
|
17
17
|
PlayerNotInGame = 14,
|
|
18
18
|
PlayerDoesntHaveCard = 15,
|
|
19
|
-
DeckPileExhausted = 16
|
|
19
|
+
DeckPileExhausted = 16,
|
|
20
|
+
PlayerAlreadyInGame = 17,
|
|
21
|
+
CannotJumpInShorthandMode = 18,
|
|
22
|
+
InvalidEventIndex = 19
|
|
20
23
|
}
|
|
21
24
|
export interface UEData extends Record<UECode, Record<string, unknown> | undefined> {
|
|
22
25
|
[UECode.CannotCallBluff]: undefined;
|
|
23
26
|
[UECode.MustNotDrawTwice]: undefined;
|
|
24
27
|
[UECode.DeckPileExhausted]: undefined;
|
|
25
28
|
[UECode.CannotSwitchColors]: undefined;
|
|
29
|
+
[UECode.CannotJumpInShorthandMode]: undefined;
|
|
26
30
|
[UECode.TooFewPlayers]: {
|
|
27
31
|
playerCount: number;
|
|
28
32
|
};
|
|
29
33
|
[UECode.TooManyPlayers]: {
|
|
30
34
|
playerCount: number;
|
|
31
35
|
};
|
|
36
|
+
[UECode.InvalidEventIndex]: {
|
|
37
|
+
index: number;
|
|
38
|
+
};
|
|
32
39
|
[UECode.MustPickColor]: {
|
|
33
40
|
performedEvent: GameEvent;
|
|
34
41
|
};
|
|
@@ -53,6 +60,9 @@ export interface UEData extends Record<UECode, Record<string, unknown> | undefin
|
|
|
53
60
|
[UECode.PlayerNotInGame]: {
|
|
54
61
|
playerId: PlayerId;
|
|
55
62
|
};
|
|
63
|
+
[UECode.PlayerAlreadyInGame]: {
|
|
64
|
+
playerId: PlayerId;
|
|
65
|
+
};
|
|
56
66
|
[UECode.PlayerDoesntHaveCard]: {
|
|
57
67
|
playerId: PlayerId;
|
|
58
68
|
card: Card;
|
package/dist/game.d.ts
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { UECode, UnocabError } from "./errors.ts";
|
|
2
|
+
import { type Card, type CardColor, type FullGameState, type GameEvent, type GameState, type InfoEvent, type PlayerId, type ProcessEventResult, type ShorthandGameState } from "./types.ts";
|
|
3
|
+
import { type XOR } from "./utils.ts";
|
|
4
|
+
export declare class Game<Shorthand extends boolean = false> {
|
|
5
|
+
protected _state: boolean extends Shorthand ? GameState : true extends Shorthand ? ShorthandGameState : FullGameState;
|
|
6
|
+
get state(): typeof this._state;
|
|
7
|
+
constructor(props?: XOR<{
|
|
8
|
+
serialisedState?: string;
|
|
9
|
+
}, {
|
|
10
|
+
shorthandMode?: Shorthand;
|
|
11
|
+
stackPlusTwos?: boolean;
|
|
12
|
+
initialSeed?: string | number;
|
|
13
|
+
}>);
|
|
14
|
+
protected random(): number;
|
|
15
|
+
protected shuffleDeck(): void;
|
|
16
|
+
/**
|
|
17
|
+
* The live hand of a player, for internal mutation. Use
|
|
18
|
+
* {@link Game.handOf} to hand a player's cards to outside code.
|
|
19
|
+
*/
|
|
20
|
+
protected handRef(playerId: PlayerId): Card[];
|
|
21
|
+
/** Seated players who still have cards, in seating order. */
|
|
22
|
+
protected playersInPlay(): string[];
|
|
23
|
+
/**
|
|
24
|
+
* Moves the turn by `steps` players in the current direction.
|
|
25
|
+
* Players who have emptied their hand are not counted.
|
|
26
|
+
*/
|
|
27
|
+
protected advanceTurn(steps?: number): void;
|
|
28
|
+
protected init(customDeckTopCard?: Card): void;
|
|
29
|
+
protected recordEvent(event: GameEvent | InfoEvent): void;
|
|
30
|
+
/** Use {@link Game.takeCards}, which makes sure enough cards are left. */
|
|
31
|
+
protected deckTopCard(): {
|
|
32
|
+
type: "eight" | "five" | "four" | "nine" | "one" | "plus-two" | "reverse" | "seven" | "six" | "skip" | "three" | "two" | "zero";
|
|
33
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
34
|
+
} | {
|
|
35
|
+
type: "color-chooser" | "plus-four";
|
|
36
|
+
color?: undefined;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Takes `count` cards from the deck. Throws before taking any
|
|
40
|
+
* card if the deck and pile do not have enough cards together.
|
|
41
|
+
*/
|
|
42
|
+
protected takeCards(count: number): Card[];
|
|
43
|
+
/**
|
|
44
|
+
* Whether the active player has to draw 4 or call bluff,
|
|
45
|
+
* which is the case after a +4 and its color choice.
|
|
46
|
+
*/
|
|
47
|
+
protected facingPlusFour(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Ends the game if a player has gone out and only one seated player
|
|
50
|
+
* still has cards, who then loses. Returns the loser if it ended.
|
|
51
|
+
*/
|
|
52
|
+
protected endIfDecided(): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the last two events that don't have the type "game_info",
|
|
55
|
+
* most recent first.
|
|
56
|
+
*/
|
|
57
|
+
lastTwoNonInfoEvents(): [GameEvent] | [GameEvent, GameEvent];
|
|
58
|
+
/**
|
|
59
|
+
* Returns the playing pile. This is where the first card is
|
|
60
|
+
* played, and where all the subsequent cards played by players
|
|
61
|
+
* are put. It is guaranteed to have one card at the start.
|
|
62
|
+
*
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const game = new Game();
|
|
65
|
+
* game.play(p1, { type: "one", color: "red" });
|
|
66
|
+
* game.play(p2, { type: "one", color: "green" });
|
|
67
|
+
*
|
|
68
|
+
* assertEquals(game.getPile(), [
|
|
69
|
+
* { type: "six", color: "red" }, // randomly chosen by engine
|
|
70
|
+
* { type: "one", color: "red" },
|
|
71
|
+
* { type: "one", color: "green" },
|
|
72
|
+
* ]);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
getPile(): Card[];
|
|
76
|
+
/**
|
|
77
|
+
* @param bluffSiteCheck - if set to true, checks whether the card could
|
|
78
|
+
* have been played instead of the last +4.
|
|
79
|
+
*
|
|
80
|
+
* Check whether the given event is valid at the current
|
|
81
|
+
* state of the game. Returns `undefined` if it is valid, and
|
|
82
|
+
* an {@link UnocabError} describing the problem if it is not.
|
|
83
|
+
*
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const game = new Game();
|
|
86
|
+
* game.draw(p1);
|
|
87
|
+
*
|
|
88
|
+
* // UnocabError, since the last card is not a +4
|
|
89
|
+
* game.checkEvent({ type: "bluff_called", by: p1 });
|
|
90
|
+
*
|
|
91
|
+
* // undefined, since you can pass after drawing
|
|
92
|
+
* game.checkEvent({ type: "pass", by: p1 });
|
|
93
|
+
*
|
|
94
|
+
* game.pass(p1);
|
|
95
|
+
*
|
|
96
|
+
* // UnocabError, since you cannot pass without drawing
|
|
97
|
+
* game.checkEvent({ type: "pass", by: p2 });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
checkEvent(event: GameEvent, bluffSiteCheck?: boolean): UnocabError<UECode.GameEnded> | UnocabError<UECode.MustPickColor> | UnocabError<UECode.MustNotPlayLastCardColorChanging> | UnocabError<UECode.MustDrawOrPlusTwo> | UnocabError<UECode.MustDrawOrCallBluff> | UnocabError<UECode.MustPlayExpectedCard> | UnocabError<UECode.MustNotDrawTwice> | UnocabError<UECode.MustNotPassWithoutDraw> | UnocabError<UECode.CannotCallBluff> | UnocabError<UECode.CannotSwitchColors> | UnocabError<UECode.TooFewPlayers> | UnocabError<UECode.NotPlayersTurn> | UnocabError<UECode.PlayerNotInGame> | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* This function can be used as an alternative to the user-friendly
|
|
103
|
+
* methods like {@link Game.draw} or {@link Game.pass}, where you are
|
|
104
|
+
* allowed to directly pass in a {@link GameEvent}.
|
|
105
|
+
*
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const game = new Game();
|
|
108
|
+
*
|
|
109
|
+
* // regular way
|
|
110
|
+
* game.draw(p1);
|
|
111
|
+
*
|
|
112
|
+
* // processNewEvent way
|
|
113
|
+
* game.processNewEvent({ type: "draw", by: p1 });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
processNewEvent(event: GameEvent): ProcessEventResult;
|
|
117
|
+
/**
|
|
118
|
+
* Have player(s) denoted by an ID join the game. New players are
|
|
119
|
+
* dealt 7 cards, while a player who left and joins again gets
|
|
120
|
+
* their old hand back.
|
|
121
|
+
*
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const game = new Game();
|
|
124
|
+
* const p1 = uuidv4();
|
|
125
|
+
* const p2 = uuidv4();
|
|
126
|
+
* game.join(p1, p2);
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
join(...playerIds: PlayerId[]): void;
|
|
130
|
+
/**
|
|
131
|
+
* Have player(s) denoted by an ID leave the game. Their hand is
|
|
132
|
+
* kept aside and given back if they join again. If the active
|
|
133
|
+
* player leaves, the turn moves on to the next player. If a player
|
|
134
|
+
* has already gone out and only one seated player is left with
|
|
135
|
+
* cards, the game ends with that player as the loser.
|
|
136
|
+
*
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const game = new Game();
|
|
139
|
+
* const p1 = uuidv4();
|
|
140
|
+
* const p2 = uuidv4();
|
|
141
|
+
* game.join(p1, p2);
|
|
142
|
+
*
|
|
143
|
+
* game.leave(p1, p2);
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
leave(...playerIds: PlayerId[]): void;
|
|
147
|
+
/**
|
|
148
|
+
* Have a player denoted by an ID play a card. Throws
|
|
149
|
+
* an {@link UnocabError} in case the player does not
|
|
150
|
+
* have the given card, or if playing it is an invalid move.
|
|
151
|
+
*
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const game = new Game();
|
|
154
|
+
* game.play(p1, { type: "one", color: "red" });
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
play(by: PlayerId, card: Card): ProcessEventResult;
|
|
158
|
+
/**
|
|
159
|
+
* Have a player denoted by an ID draw a card. Throws
|
|
160
|
+
* an {@link UnocabError} in case it's invalid to draw.
|
|
161
|
+
*
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const game = new Game();
|
|
164
|
+
* game.draw(p1);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
draw(by: PlayerId): ProcessEventResult;
|
|
168
|
+
/**
|
|
169
|
+
* Have a player denoted by an ID pass their turn. Throws
|
|
170
|
+
* an {@link UnocabError} in case it's invalid to pass.
|
|
171
|
+
*
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const game = new Game();
|
|
174
|
+
* game.draw(p1);
|
|
175
|
+
* game.pass(p1);
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
pass(by: PlayerId): ProcessEventResult;
|
|
179
|
+
/**
|
|
180
|
+
* Have a player denoted by an ID call bluff. Throws
|
|
181
|
+
* an {@link UnocabError} in case a +4 was not played
|
|
182
|
+
* as the previous event, or if it is an invalid move.
|
|
183
|
+
*
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const game = new Game();
|
|
186
|
+
* game.play(p1, { type: "plus-four" });
|
|
187
|
+
* game.chooseColor(p1, "red");
|
|
188
|
+
* game.callBluff(p2);
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
callBluff(by: PlayerId): ProcessEventResult;
|
|
192
|
+
/**
|
|
193
|
+
* Have a player denoted by an ID switch color. Throws
|
|
194
|
+
* an {@link UnocabError} in case a color switching card
|
|
195
|
+
* was not played as the previous event, or if it is an invalid move.
|
|
196
|
+
*
|
|
197
|
+
* ```typescript
|
|
198
|
+
* const game = new Game();
|
|
199
|
+
* game.play(p1, { type: "plus-four" });
|
|
200
|
+
* game.chooseColor(p1, "red");
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
chooseColor(by: PlayerId, color: CardColor): ProcessEventResult;
|
|
204
|
+
/**
|
|
205
|
+
* Fetches a copy of the hand of a player denoted by an ID.
|
|
206
|
+
* Throws an {@link UnocabError} if the player has never
|
|
207
|
+
* joined the game.
|
|
208
|
+
*/
|
|
209
|
+
handOf(playerId: PlayerId): Card[];
|
|
210
|
+
/**
|
|
211
|
+
* Serialises the game state into a JSON. This can then
|
|
212
|
+
* be passed to the constructor when creating a new game
|
|
213
|
+
* in order to recreate the game.
|
|
214
|
+
*
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const game = new Game({ shorthandMode: true });
|
|
217
|
+
*
|
|
218
|
+
* const json = game.serialise();
|
|
219
|
+
*
|
|
220
|
+
* const recreatedGame = new Game<true>({ serialisedState: json });
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
serialise(): string;
|
|
224
|
+
/**
|
|
225
|
+
* Clones this game to create another game with the exact
|
|
226
|
+
* same state and set of players.
|
|
227
|
+
*/
|
|
228
|
+
clone(): Game<Shorthand>;
|
|
229
|
+
/**
|
|
230
|
+
* Fetches events of the given type from the game.
|
|
231
|
+
*
|
|
232
|
+
* ```typescript
|
|
233
|
+
* const game = new Game();
|
|
234
|
+
* game.draw(p1);
|
|
235
|
+
* game.pass(p1);
|
|
236
|
+
* game.draw(p2);
|
|
237
|
+
*
|
|
238
|
+
* assertEquals(game.eventsOfType("draw"), [
|
|
239
|
+
* { type: "draw", by: p1 },
|
|
240
|
+
* { type: "draw", by: p2 }
|
|
241
|
+
* ]);
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
eventsOfType<T extends GameState["events"][number]["type"]>(type: T): (Extract<{
|
|
245
|
+
type: "card_played";
|
|
246
|
+
by: string;
|
|
247
|
+
card: {
|
|
248
|
+
type: "eight" | "five" | "four" | "nine" | "one" | "plus-two" | "reverse" | "seven" | "six" | "skip" | "three" | "two" | "zero";
|
|
249
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
250
|
+
} | {
|
|
251
|
+
type: "color-chooser" | "plus-four";
|
|
252
|
+
color?: undefined;
|
|
253
|
+
};
|
|
254
|
+
}, {
|
|
255
|
+
type: T;
|
|
256
|
+
}> | Extract<{
|
|
257
|
+
type: "bluff_called";
|
|
258
|
+
by: string;
|
|
259
|
+
}, {
|
|
260
|
+
type: T;
|
|
261
|
+
}> | Extract<{
|
|
262
|
+
type: "choose-color";
|
|
263
|
+
by: string;
|
|
264
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
265
|
+
}, {
|
|
266
|
+
type: T;
|
|
267
|
+
}> | Extract<{
|
|
268
|
+
type: "draw";
|
|
269
|
+
by: string;
|
|
270
|
+
}, {
|
|
271
|
+
type: T;
|
|
272
|
+
}> | Extract<{
|
|
273
|
+
type: "pass";
|
|
274
|
+
by: string;
|
|
275
|
+
}, {
|
|
276
|
+
type: T;
|
|
277
|
+
}> | Extract<{
|
|
278
|
+
type: "game_info";
|
|
279
|
+
info: {
|
|
280
|
+
type: "bluff_called";
|
|
281
|
+
by: string;
|
|
282
|
+
} | {
|
|
283
|
+
type: "bluff_call_succeeded";
|
|
284
|
+
by: string;
|
|
285
|
+
of: string;
|
|
286
|
+
} | {
|
|
287
|
+
type: "bluff_call_failed";
|
|
288
|
+
by: string;
|
|
289
|
+
of: string;
|
|
290
|
+
} | {
|
|
291
|
+
type: "seed_changed";
|
|
292
|
+
newSeed: number;
|
|
293
|
+
} | {
|
|
294
|
+
type: "player_won";
|
|
295
|
+
id: string;
|
|
296
|
+
} | {
|
|
297
|
+
type: "game_ended";
|
|
298
|
+
loser: string;
|
|
299
|
+
} | {
|
|
300
|
+
type: "player_joined";
|
|
301
|
+
id: string;
|
|
302
|
+
} | {
|
|
303
|
+
type: "player_left";
|
|
304
|
+
id: string;
|
|
305
|
+
} | {
|
|
306
|
+
type: "pile_to_deck";
|
|
307
|
+
};
|
|
308
|
+
}, {
|
|
309
|
+
type: T;
|
|
310
|
+
}>)[];
|
|
311
|
+
/**
|
|
312
|
+
* Get the player ID of the active player who is expected to play.
|
|
313
|
+
*
|
|
314
|
+
* ```typescript
|
|
315
|
+
* const game = new Game();
|
|
316
|
+
*
|
|
317
|
+
* assertEquals(game.activePlayer(), p1);
|
|
318
|
+
*
|
|
319
|
+
* game.draw(p1);
|
|
320
|
+
* game.pass(p1);
|
|
321
|
+
*
|
|
322
|
+
* assertEquals(game.activePlayer(), p2);
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
activePlayer(): PlayerId;
|
|
326
|
+
/**
|
|
327
|
+
* Checks whether this game has ended. If it has, returns an
|
|
328
|
+
* object containing the player ID of the losing player.
|
|
329
|
+
*/
|
|
330
|
+
hasEnded(): false | {
|
|
331
|
+
loser: string;
|
|
332
|
+
};
|
|
333
|
+
/**
|
|
334
|
+
* @param bluffSiteCheck - if set to true, the last 3 events will be ignored.
|
|
335
|
+
*
|
|
336
|
+
* Gets the expected card type and color that, if playing a card is
|
|
337
|
+
* possible in the current state, would be valid. Please note that this
|
|
338
|
+
* does not mean actually playing that card is okay. It just denotes the
|
|
339
|
+
* current "pile status", so to speak. To check if playing a card is valid
|
|
340
|
+
* in the current situation, use {@link Game.checkEvent}. Also, in case any
|
|
341
|
+
* of `expectedType` or `expectedColor` is undefined, it becomes a non
|
|
342
|
+
* condition. This means any type/color is allowed.
|
|
343
|
+
*/
|
|
344
|
+
expectedCard(bluffSiteCheck?: boolean): {
|
|
345
|
+
expectedType?: Card["type"];
|
|
346
|
+
expectedColor: Card["color"];
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Resets the game to a previous index of the events array.
|
|
350
|
+
* This can be used to replay a game from the start, or to
|
|
351
|
+
* undo a previous move. Cannot be used in shorthand mode.
|
|
352
|
+
* Returns the events that will be "removed".
|
|
353
|
+
*
|
|
354
|
+
* ```typescript
|
|
355
|
+
* const game = new Game();
|
|
356
|
+
*
|
|
357
|
+
* game.play(p1, { type: "seven", color: "blue" });
|
|
358
|
+
*
|
|
359
|
+
* // jump to the second last index, where the above
|
|
360
|
+
* // move does not exist, effectively undo-ing it.
|
|
361
|
+
* game.jumpToEventIndex(-2);
|
|
362
|
+
*
|
|
363
|
+
* game.play(p1, { type: "six", color: "blue" });
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
jumpToEventIndex(index: number): ({
|
|
367
|
+
type: "card_played";
|
|
368
|
+
by: string;
|
|
369
|
+
card: {
|
|
370
|
+
type: "eight" | "five" | "four" | "nine" | "one" | "plus-two" | "reverse" | "seven" | "six" | "skip" | "three" | "two" | "zero";
|
|
371
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
372
|
+
} | {
|
|
373
|
+
type: "color-chooser" | "plus-four";
|
|
374
|
+
color?: undefined;
|
|
375
|
+
};
|
|
376
|
+
} | {
|
|
377
|
+
type: "bluff_called";
|
|
378
|
+
by: string;
|
|
379
|
+
} | {
|
|
380
|
+
type: "choose-color";
|
|
381
|
+
by: string;
|
|
382
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
383
|
+
} | {
|
|
384
|
+
type: "draw";
|
|
385
|
+
by: string;
|
|
386
|
+
} | {
|
|
387
|
+
type: "pass";
|
|
388
|
+
by: string;
|
|
389
|
+
} | {
|
|
390
|
+
type: "game_info";
|
|
391
|
+
info: {
|
|
392
|
+
type: "bluff_called";
|
|
393
|
+
by: string;
|
|
394
|
+
} | {
|
|
395
|
+
type: "bluff_call_succeeded";
|
|
396
|
+
by: string;
|
|
397
|
+
of: string;
|
|
398
|
+
} | {
|
|
399
|
+
type: "bluff_call_failed";
|
|
400
|
+
by: string;
|
|
401
|
+
of: string;
|
|
402
|
+
} | {
|
|
403
|
+
type: "seed_changed";
|
|
404
|
+
newSeed: number;
|
|
405
|
+
} | {
|
|
406
|
+
type: "player_won";
|
|
407
|
+
id: string;
|
|
408
|
+
} | {
|
|
409
|
+
type: "game_ended";
|
|
410
|
+
loser: string;
|
|
411
|
+
} | {
|
|
412
|
+
type: "player_joined";
|
|
413
|
+
id: string;
|
|
414
|
+
} | {
|
|
415
|
+
type: "player_left";
|
|
416
|
+
id: string;
|
|
417
|
+
} | {
|
|
418
|
+
type: "pile_to_deck";
|
|
419
|
+
};
|
|
420
|
+
})[];
|
|
421
|
+
/**
|
|
422
|
+
* Get a set of {@link GameEvent} that are valid in the current state of
|
|
423
|
+
* the game. For example, after playing a +4, choosing a color is the only
|
|
424
|
+
* valid move, so this function will return just that. The returned events
|
|
425
|
+
* can also be directly passed to {@link Game.processNewEvent}.
|
|
426
|
+
*
|
|
427
|
+
* ```typescript
|
|
428
|
+
* const game = new Game();
|
|
429
|
+
* game.play(p1, { type: "plus-four" });
|
|
430
|
+
* assertEquals(game.validEvents(p1), [
|
|
431
|
+
* { type: "choose-color", by: p1, color: "red" }
|
|
432
|
+
* ]);
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
validEvents(playerId: PlayerId): ({
|
|
436
|
+
type: "card_played";
|
|
437
|
+
by: string;
|
|
438
|
+
card: {
|
|
439
|
+
type: "eight" | "five" | "four" | "nine" | "one" | "plus-two" | "reverse" | "seven" | "six" | "skip" | "three" | "two" | "zero";
|
|
440
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
441
|
+
} | {
|
|
442
|
+
type: "color-chooser" | "plus-four";
|
|
443
|
+
color?: undefined;
|
|
444
|
+
};
|
|
445
|
+
} | {
|
|
446
|
+
type: "bluff_called";
|
|
447
|
+
by: string;
|
|
448
|
+
} | {
|
|
449
|
+
type: "choose-color";
|
|
450
|
+
by: string;
|
|
451
|
+
color: "blue" | "green" | "red" | "yellow";
|
|
452
|
+
} | {
|
|
453
|
+
type: "draw";
|
|
454
|
+
by: string;
|
|
455
|
+
} | {
|
|
456
|
+
type: "pass";
|
|
457
|
+
by: string;
|
|
458
|
+
})[];
|
|
459
|
+
}
|
package/dist/index.d.ts
ADDED