@pompidup/kingdomino-engine 2.0.1 → 2.0.5

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- This is under development. The goal is to create a simple and lightweight TypeScript engine designed to facilitate the gameplay of Kingdomino. It provides the core logic for managing game states, rules, and player interactions, making it easy to integrate into any application that requires a Kingdomino game engine.
5
+ This is a simple and lightweight TypeScript engine designed to facilitate the gameplay of Kingdomino. It provides the core logic for managing game states, rules, and player interactions, making it easy to integrate into any application that requires a Kingdomino game engine.
6
6
 
7
7
  ## Requirements
8
8
 
@@ -13,94 +13,227 @@ This is under development. The goal is to create a simple and lightweight TypeSc
13
13
  - **Game State Management**: Efficiently manage game states, including player turns, game rules, and game progression.
14
14
  - **Rule Enforcement**: Automatically enforce game rules, ensuring a fair and consistent gameplay experience.
15
15
  - **Player Interaction**: Simplify player interactions, including drawing tiles, placing dominoes, and scoring.
16
+ - **Extra Rules Support**: Implement and manage additional game rules for enhanced gameplay.
17
+ - **Multiple Game Modes**: Support for different game modes, including Classic and potentially others.
16
18
 
17
19
  ## Installation
18
20
 
19
- To install the Kingdomino Engine, you can use npm, yarn or pnpm. Run the following command in your terminal:
21
+ To install the Kingdomino Engine, you can use npm, yarn or pnpm. Run one of the following commands in your terminal:
20
22
 
21
23
  ```bash
22
24
  npm install @pompidup/kingdomino-engine
23
- ```
24
-
25
- or
26
-
27
- ```bash
28
25
  yarn add @pompidup/kingdomino-engine
29
- ```
30
-
31
- or
32
-
33
- ```bash
34
26
  pnpm add @pompidup/kingdomino-engine
35
27
  ```
36
28
 
37
29
  ## Usage
38
30
 
39
- The Kingdomino Engine is designed to be easy to use and integrate into any application. Below are some examples of how you can use the engine to create a new game, add players, and start playing.
31
+ The Kingdomino Engine is designed to be easy to use and integrate into any application. Below are examples of how you can use the engine to create a new game, add players, set rules, and manage gameplay.
40
32
 
41
- Each method returns an updated game state object, which contains the current state of the game, including the board, players, and dominoes. In return you will receive too the nextLord (the next player to play) and the nextAction (the next action to be performed by the next player).
33
+ Each method returns an updated game state object, which contains the current state of the game, including the board, players, and dominoes. You will also receive the nextLord (the next player to play) and the nextAction (the next action to be performed by the next player).
42
34
 
43
35
  **It's your responsibility to save and pass the updated game state to the next method call.**
44
36
 
45
37
  ### Basic Setup
46
38
 
47
- To start using the Kingdomino Engine, you need to import it and initialize a new game instance.
39
+ To start using the Kingdomino Engine, import it and initialize a new game instance:
48
40
 
49
41
  ```typescript
50
42
  import gameEngine from "@pompidup/kingdomino-engine";
51
43
 
52
44
  const engine = gameEngine();
53
- let gameState = engine.init();
45
+ let gameState = engine.createGame({ mode: "Classic" });
46
+
54
47
  ```
55
48
 
56
- ### Start game and setting up players
49
+ ### Adding Players
57
50
 
58
- You can add players to the game by passing an array of player.
51
+ Add players to the game by passing an array of player names:
59
52
 
60
53
  ```typescript
61
- const players = [{ name: "Player 1" }, { name: "Player 2" }];
62
- gameState = engine.start(gameState, players);
54
+ gameState = engine.addPlayers(gameState, ["Player 1", "Player 2", "Player 3"]);
63
55
  ```
64
56
 
65
- ### Choosing game mode (Classic, Queendomino...)
57
+ ### Setting Up Extra Rules
66
58
 
67
- Incoming...
59
+ You can get available extra rules and add them to the game:
68
60
 
69
- ### Setting Up Rules
61
+ ```typescript
62
+ const extraRules = engine.getExtraRules({ mode: "Classic", players: 2 });
63
+ gameState = engine.addExtraRules({
64
+ game: gameState,
65
+ extraRules: ["The middle Kingdom"],
66
+ });
67
+ ```
68
+
69
+ ### Starting the Game
70
+
71
+ After setting up players and rules, start the game:
72
+
73
+ ```typescript
74
+ gameState = engine.startGame({ game: gameState });
75
+ ```
70
76
 
71
- Incoming...
72
77
 
73
78
  ### Player Actions
74
79
 
75
- Players can perform actions like pick domino, placing domino, and passing turns.
80
+ Players can perform actions like picking a domino, placing a domino, and discarding a domino.
76
81
 
77
82
  - Pick Domino:
78
- You can pick a domino by passing the lordId and the number of the domino you want to pick.
79
83
 
80
84
  ```typescript
81
- gameState = engine.pickDomino(gameState, "lordId", 12);
85
+ gameState = engine.chooseDomino({
86
+ game: gameState,
87
+ lordId: "lordId",
88
+ dominoPick: 12
89
+ });
82
90
  ```
83
91
 
84
92
  - Place Domino:
85
- You can place a domino by passing the lordId, the position where you want to place the domino, the orientation (horizontal or vertical), and the rotation (if you want to switch left and right tile you can pass 180 else 0).
86
93
 
87
94
  ```typescript
88
- gameState = engine.placeDomino(
89
- gameState,
90
- "lordId",
91
- { x: 0, y: 0 },
92
- "horizontal",
93
- 0
94
- );
95
+ gameState = engine.placeDomino({
96
+ game: gameState,
97
+ lordId: "lordId",
98
+ position: { x: 0, y: 0 },
99
+ orientation: "horizontal",
100
+ rotation: 0
101
+ });
95
102
  ```
96
103
 
97
- - Pass Turn:
98
- If you can not place a domino you can pass your turn.
104
+ - Discard Domino:
99
105
 
100
106
  ```typescript
101
- gameState = engine.passTurn(gameState, "lordId");
107
+ gameState = engine.discardDomino({
108
+ game: gameState,
109
+ lordId: "lordId",
110
+ });
102
111
  ```
103
112
 
113
+
114
+ ### Game Flow
115
+
116
+ The game progresses through various steps and actions. The `nextAction` property in the game state indicates what action should be taken next. This could be a step (like "addPlayers", "options", "start", or "result") or an action (like "pickDomino" or "placeDomino").
117
+
104
118
  ### Scoring
105
119
 
106
- When last turn is played, you will received the final score of each player and their position.
120
+ When the last turn is played, the game will automatically calculate and provide the final score for each player and their position.
121
+
122
+ ## Extra Rules
123
+
124
+ The engine supports extra rules that can modify gameplay. Some examples include:
125
+
126
+ - **The Middle Kingdom**: Gain 10 additional points if your castle is in the middle of the kingdom.
127
+ - **Harmony**: Gain 5 additional points if your kingdom is complete (no discarded dominoes).
128
+
129
+ ## API Documentation
130
+
131
+ ### Key Types and Interfaces
132
+
133
+ #### GameEngine
134
+
135
+ The main interface for interacting with the Kingdomino engine.
136
+
137
+ ```typescript
138
+ type GameEngine = {
139
+ getModes: (command: GetModesCommand) => GameMode[];
140
+ getExtraRules: (command: GetExtraRulesCommand) => ExtraRule[];
141
+ createGame: (command: CreateGameCommand) => GameWithNextStep;
142
+ addPlayers: (command: AddPlayersCommand) => GameWithNextStep;
143
+ addExtraRules: (command: AddExtraRulesCommand) => GameWithNextStep;
144
+ startGame: (command: StartGameCommand) => GameWithNextAction;
145
+ chooseDomino: (command: ChooseDominoCommand) => GameWithNextAction;
146
+ placeDomino: (command: PlaceDominoCommand) => GameWithNextAction | GameWithNextStep;
147
+ discardDomino: (command: DiscardDominoCommand) => GameWithNextAction | GameWithNextStep;
148
+ getResults: (command: GetResultCommand) => GameWithResults;
149
+ };
150
+ ```
151
+
152
+
153
+ #### Game
154
+
155
+ Represents the current state of a Kingdomino game.
156
+
157
+ ```typescript
158
+ type Game = {
159
+ id: string;
160
+ dominoes: Domino[];
161
+ currentDominoes: RevealedDomino[];
162
+ players: Player[];
163
+ lords: Lord[];
164
+ turn: number;
165
+ rules: SelectedRules;
166
+ mode: GameMode;
167
+ };
168
+ ```
169
+
170
+
171
+ #### NextStep and NextAction
172
+
173
+ Represent the next step or action in the game flow.
174
+
175
+ ```typescript
176
+ type NextStep = {
177
+ type: "step";
178
+ step: "addPlayers" | "options" | "start" | "result";
179
+ };
180
+ type NextAction = {
181
+ type: "action";
182
+ nextLord: string;
183
+ nextAction: "pickDomino" | "placeDomino";
184
+ };
185
+ ```
186
+
187
+
188
+ #### ExtraRule
189
+
190
+ Represents an additional rule that can be applied to the game.
191
+
192
+ ```typescript
193
+ type ExtraRule = {
194
+ name: string;
195
+ description: string;
196
+ mode: GameMode[];
197
+ playersLimit?: number;
198
+ };
199
+ ```
200
+
201
+
202
+ ### Methods
203
+
204
+ #### createGame(command: CreateGameCommand): GameWithNextStep
205
+
206
+ Creates a new game with the specified game mode.
207
+
208
+ #### addPlayers(command: AddPlayersCommand): GameWithNextStep
209
+
210
+ Adds players to the game.
211
+
212
+ #### addExtraRules(command: AddExtraRulesCommand): GameWithNextStep
213
+
214
+ Adds extra rules to the game.
215
+
216
+ #### startGame(command: StartGameCommand): GameWithNextAction
217
+
218
+ Starts the game, setting up the initial game state.
219
+
220
+ #### chooseDomino(command: ChooseDominoCommand): GameWithNextAction
221
+
222
+ Allows a player to choose a domino.
223
+
224
+ #### placeDomino(command: PlaceDominoCommand): GameWithNextAction | GameWithNextStep
225
+
226
+ Allows a player to place a domino on their kingdom.
227
+
228
+ #### discardDomino(command: DiscardDominoCommand): GameWithNextAction | GameWithNextStep
229
+
230
+ Allows a player to discard a domino if they cannot place it.
231
+
232
+ #### getResults(command: GetResultCommand): GameWithResults
233
+
234
+ Calculates and returns the final game results.
235
+
236
+ ## Advanced Usage
237
+
238
+ For more advanced usage and detailed API documentation, please refer to the source code and inline comments.
239
+
package/dist/index.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- type GameMode = {
2
- name: string;
3
- description: string;
4
- };
5
-
6
1
  declare const GROUND: readonly ["castle", "wheat", "forest", "sea", "plain", "swamp", "mine"];
7
2
  type Ground = (typeof GROUND)[number];
8
3
  type EmptyGround = "empty";
@@ -27,6 +22,14 @@ type RevealsDomino = {
27
22
  position: number;
28
23
  };
29
24
 
25
+ type Position = {
26
+ x: number;
27
+ y: number;
28
+ };
29
+ type Orientation = "vertical" | "horizontal";
30
+ type Rotation = 0 | 180;
31
+ type Kingdom = (Tile | EmptyTile)[][];
32
+
30
33
  type Lord = {
31
34
  id: string;
32
35
  playerId: string;
@@ -36,13 +39,10 @@ type Lord = {
36
39
  dominoPicked?: Domino;
37
40
  };
38
41
 
39
- type Position = {
40
- x: number;
41
- y: number;
42
+ type GameMode = {
43
+ name: string;
44
+ description: string;
42
45
  };
43
- type Orientation = "vertical" | "horizontal";
44
- type Rotation = 0 | 180;
45
- type Kingdom = (Tile | EmptyTile)[][];
46
46
 
47
47
  type ObjectValues<T> = T[keyof T];
48
48
 
@@ -70,10 +70,6 @@ type ExtraRule = {
70
70
  mode: GameMode[];
71
71
  playersLimit?: number;
72
72
  };
73
- type Rules = {
74
- basic: Record<number, BasicRules>;
75
- extraRules: ExtraRule[];
76
- };
77
73
  type SelectedRules = {
78
74
  basic: BasicRules;
79
75
  extra: ExtraRule[];
@@ -112,6 +108,7 @@ type GameWithNextAction = Game & {
112
108
  type GameWithNextStep = Game & {
113
109
  nextAction: NextStep;
114
110
  };
111
+ type GameState = GameWithNextAction | GameWithNextStep;
115
112
  type Score = {
116
113
  points: number;
117
114
  maxPropertiesSize: number;
@@ -141,33 +138,27 @@ type CreateGameCommand = {
141
138
  };
142
139
 
143
140
  type AddPlayersCommand = {
144
- game: Game & {
145
- nextAction: NextStep;
146
- };
141
+ game: GameState;
147
142
  players: string[];
148
143
  };
149
144
 
150
145
  type AddExtraRulesCommand = {
151
- game: Game & {
152
- nextAction: NextStep;
153
- };
146
+ game: GameState;
154
147
  extraRules: string[];
155
148
  };
156
149
 
157
150
  type StartGameCommand = {
158
- game: Game & {
159
- nextAction: NextStep;
160
- };
151
+ game: GameState;
161
152
  };
162
153
 
163
154
  type ChooseDominoCommand = {
164
- game: GameWithNextAction;
155
+ game: GameState;
165
156
  dominoPick: number;
166
157
  lordId: string;
167
158
  };
168
159
 
169
160
  type PlaceDominoCommand = {
170
- game: GameWithNextAction;
161
+ game: GameState;
171
162
  lordId: string;
172
163
  position: Position;
173
164
  orientation: Orientation;
@@ -175,12 +166,12 @@ type PlaceDominoCommand = {
175
166
  };
176
167
 
177
168
  type DiscardDominoCommand = {
178
- game: GameWithNextAction;
169
+ game: GameState;
179
170
  lordId: string;
180
171
  };
181
172
 
182
173
  type GetResultCommand = {
183
- game: GameWithNextStep;
174
+ game: GameState;
184
175
  };
185
176
 
186
177
  type GameEngine = {
@@ -191,32 +182,16 @@ type GameEngine = {
191
182
  addExtraRules: (command: AddExtraRulesCommand) => GameWithNextStep;
192
183
  startGame: (command: StartGameCommand) => GameWithNextAction;
193
184
  chooseDomino: (command: ChooseDominoCommand) => GameWithNextAction;
194
- placeDomino: (command: PlaceDominoCommand) => GameWithNextAction | GameWithNextStep;
195
- discardDomino: (command: DiscardDominoCommand) => GameWithNextAction | GameWithNextStep;
185
+ placeDomino: (command: PlaceDominoCommand) => GameState;
186
+ discardDomino: (command: DiscardDominoCommand) => GameState;
196
187
  getResults: (command: GetResultCommand) => GameWithResults;
197
188
  };
198
189
 
199
- type DominoesRepository = {
200
- getForMode: (mode: GameMode) => Domino[] | [];
201
- };
202
-
203
- type ModeRepository = {
204
- getAvailables: () => GameMode[];
205
- };
206
-
207
- type RuleRepository = {
208
- getAll: () => Rules;
209
- getAllExtra: () => ExtraRule[];
210
- };
211
-
212
190
  type ShuffleMethod = <T>(array: T[]) => T[];
213
191
 
214
192
  type UuidMethod = () => string;
215
193
 
216
194
  type EngineConfig = {
217
- modeRepository?: ModeRepository;
218
- dominoesRepository?: DominoesRepository;
219
- ruleRepository?: RuleRepository;
220
195
  uuidMethod?: UuidMethod;
221
196
  shuffleMethod?: ShuffleMethod;
222
197
  };