@gamepark/rules-api 7.6.2 → 7.6.3

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.
@@ -1,308 +1,21 @@
1
- import { Location } from '../location';
2
- import { CreateItem, CreateItemsAtOnce, DeleteItem, DeleteItemsAtOnce, ItemMove, MoveItem, MoveItemsAtOnce, RollItem, SelectItem, Shuffle } from '../moves';
3
- import { MaterialDeck, MaterialMoney } from './index';
1
+ import { MaterialBase } from './MaterialBase';
2
+ import { MaterialDeck } from './MaterialDeck';
4
3
  import { MaterialItem } from './MaterialItem';
5
- export type ItemEntry<P extends number = number, L extends number = number> = [number, MaterialItem<P, L>];
4
+ import { MaterialMoney } from './MaterialMoney';
6
5
  /**
7
6
  * The Material class is the core helper class that help manipulate the game items in a simple way.
8
7
  * It includes two kind of functions: functions to filter items, and functions to build {@link ItemMove} objects.
9
8
  * Filter function will all return a new instance of Material with only the filtered items. This class is designed to be immutable.
10
9
  *
10
+ * All the filtering and move creation logic lives in {@link MaterialBase}; this class only adds the factory helpers
11
+ * ({@link deck} and {@link money}) that build specialised instances — keeping the dependency arrow one-way and
12
+ * avoiding a circular import with {@link MaterialDeck} / {@link MaterialMoney}.
13
+ *
11
14
  * @typeparam P - identifier of a player. Either a number or a numeric enum (eg: PlayerColor)
12
15
  * @typeparam M - Numeric enum of the types of material manipulated in the game
13
16
  * @typeparam L - Numeric enum of the types of location in the game where the material can be located
14
17
  */
15
- export declare class Material<P extends number = number, M extends number = number, L extends number = number> {
16
- readonly type: M;
17
- protected items: MaterialItem<P, L>[];
18
- protected readonly processMove?: ((move: ItemMove<P, M, L>) => void) | undefined;
19
- entries: ItemEntry<P, L>[];
20
- /**
21
- * Construct a new Material helper instance
22
- * @param {number} type Type of items this instance will work on
23
- * @param {MaterialItem[]} items The complete list of items of this type in current game state.
24
- * @param {function} processMove if provided, this function will be executed on every move created with this instance
25
- * @param {ItemEntry[]} entries The list of items to work on. Each entry consists of an array with the index of the item, and the item
26
- */
27
- constructor(type: M, items?: MaterialItem<P, L>[], processMove?: ((move: ItemMove<P, M, L>) => void) | undefined, entries?: ItemEntry<P, L>[]);
28
- /**
29
- * Helper function to return a new instance of the same class (works also for children class)
30
- * @param {ItemEntry[]} entries Filtered entries for the new class
31
- * @returns {this} the new Material instance
32
- * @protected
33
- */
34
- protected new(entries: ItemEntry<P, L>[]): this;
35
- /**
36
- * Use this function to collect all the items in the current Material instance.
37
- * For example, you can filter then collect the matching items: this.material(type).location(locationType).getItems()
38
- *
39
- * @typeparam Id - Identifier of the items (item.id)
40
- * @param {function} predicate If provided, only the items matching the predicate will be returned
41
- * @returns {MaterialItem[]} the items in this Material instance
42
- */
43
- getItems<Id = any>(predicate?: (item: MaterialItem<P, L, Id>) => boolean): MaterialItem<P, L, Id>[];
44
- /**
45
- * @overload
46
- * Get the item at a specific index.
47
- * @throws Error if there is no item at this index
48
- *
49
- * @param {number} index Index of the item
50
- * @returns {MaterialItem} the item
51
- */
52
- getItem<Id = any>(index: number): MaterialItem<P, L, Id>;
53
- /**
54
- * @overload
55
- * @param predicate If provided, returns the first item matching the predicate
56
- * @returns {MaterialItem | undefined} the item, or undefined if none match
57
- */
58
- getItem<Id = any>(predicate?: (item: MaterialItem<P, L, Id>) => boolean): MaterialItem<P, L, Id> | undefined;
59
- /**
60
- * @returns {number} index of the first item
61
- */
62
- getIndex(): number;
63
- /**
64
- * @returns {number[]} indexes of the items
65
- */
66
- getIndexes(): number[];
67
- /**
68
- * Filter and return a new instance with only the items that match a specific index, or a specific index predicate
69
- * @param {number | function} arg The index to keep, or the predicate matching the indexes to keep
70
- */
71
- index(arg?: number | number[] | ((index: number) => boolean)): this;
72
- /**
73
- * @deprecated Use {@link index} instead
74
- */
75
- indexes(indexes: number[]): this;
76
- /**
77
- * @returns {number} number of items
78
- */
79
- get length(): number;
80
- /**
81
- * @returns {boolean} true if there is at least one item
82
- */
83
- get exists(): boolean;
84
- /**
85
- * @returns {number} Sum of the quantity of all items
86
- */
87
- getQuantity(): number;
88
- /**
89
- * This function filter the items and returns a new instance with only the filtered items.
90
- * This function is the top level filtering function, but other function can be used to simplify the code:
91
- * {@link player}, {@link location}, {@link rotation}, {@link id}, {@link locationId}...
92
- *
93
- * @param {function} predicate The predicate function. Takes every item and index, and keep the item if it returns true
94
- * @returns {this} New instance with only the items that match the predicate
95
- */
96
- filter<Id extends string | number | Record<string, any> | undefined>(predicate: (item: MaterialItem<P, L, Id>, index: number) => boolean): this;
97
- /**
98
- * Filters the items based on their ids.
99
- *
100
- * @param {function | string | number | Record} arg Id to keep, or predicate function to match the ids of the items to keep
101
- * @returns {this} New instance with only the items which ids match the argument
102
- */
103
- id<Id extends string | number | Record<string, any> | undefined>(arg?: Id | ((id: Id) => boolean)): this;
104
- /**
105
- * Filters the items based on their location type, or their location.
106
- *
107
- * @param {function | number} arg Location type to keep, or predicate function to match the location of the items to keep
108
- * @returns {this} New instance with only the items which locations match the argument
109
- */
110
- location<Id = any, Rotation = any>(arg: L | ((location: Location<P, L, Id, Rotation>) => boolean)): this;
111
- /**
112
- * Filters the items based on their rotation (item.location.rotation)
113
- *
114
- * @param {function | string | number | boolean | Record} arg rotation to keep, or predicate function to match the rotations of the items to keep
115
- * @returns {this} New instance with only the items which rotation match the argument
116
- */
117
- rotation<R extends string | number | boolean | Record<string, any> | undefined>(arg?: R | ((rotation: R) => boolean)): this;
118
- /**
119
- * Filters the items based on their owner (item.location.player)
120
- *
121
- * @param {function | number} arg player id to keep, or predicate function to match the owner player of the items to keep
122
- * @returns {this} New instance with only the items which owner match the argument
123
- */
124
- player(arg?: P | ((player?: P) => boolean)): this;
125
- /**
126
- * Filters the items based on their location's id (item.location.id)
127
- *
128
- * @param {function | number} arg location id to keep, or predicate function to match the location id of the items to keep
129
- * @returns {this} New instance with only the items which location id match the argument
130
- */
131
- locationId<Id extends string | number | boolean | Record<string, any> | undefined>(arg: Id | ((id: Id) => boolean)): this;
132
- /**
133
- * Filters the items based on their location's parent (item.location.parent).
134
- *
135
- * @param {function | number} arg location parent to keep, or predicate function to match the location parent of the items to keep
136
- * @returns {this} New instance with only the items which location parent match the argument
137
- */
138
- parent(arg?: number | ((parent?: number) => boolean)): this;
139
- /**
140
- * Filters the items that are selected (item.selected).
141
- *
142
- * @param {number | boolean} selected The selected value to compare (default is true)
143
- * @returns {this} New instance with only the items which are selected
144
- */
145
- selected(selected?: number | boolean): this;
146
- /**
147
- * Keep only the item that has the minimum value returned by the selector argument.
148
- * See {@link minBy} from Lodash
149
- *
150
- * @param {function} selector The function that evaluate the item's value
151
- * @returns {this} New instance with only the item which has the minimum value
152
- */
153
- minBy(selector: (item: MaterialItem<P, L>) => number): this;
154
- /**
155
- * Keep only the item that has the maximum value returned by the selector argument.
156
- * See {@link maxBy} from Lodash
157
- *
158
- * @param {function} selector The function that evaluate the item's value
159
- * @returns {this} New instance with only the item which has the maximum value
160
- */
161
- maxBy(selector: (item: MaterialItem<P, L>) => number): this;
162
- /**
163
- * Return a new material instance which items are ordered based on provided selector functions.
164
- * See {@link orderBy} from Lodash
165
- *
166
- * @param {...function} selectors The function or functions that evaluate each item's value
167
- * @returns {this} New instance with items ordered by the selector functions
168
- */
169
- sort(...selectors: ((item: MaterialItem<P, L>) => number)[]): this;
170
- /**
171
- * Return a new material instance with only the first N items.
172
- * You have to use {@link sort} first as the items are ordered initially by index, which is not relevant.
173
- * Example: material.sort(item => !item.location.x!).limit(10)
174
- *
175
- * @param count Number of items to keep
176
- * @returns {this} New instance with only the first "count" items
177
- */
178
- limit(count: number): this;
179
- private process;
180
- /**
181
- * Prepare a move that will create a new item
182
- * @param {MaterialItem} item The item to create
183
- * @returns {CreateItem} the move that creates an item when executed
184
- */
185
- createItem(item: MaterialItem<P, L>): CreateItem<P, M, L>;
186
- /**
187
- * Prepare a list of moves to create new items
188
- * @param {MaterialItem[]} items The items to create
189
- * @returns {CreateItem[]} the moves that creates the new items when executed
190
- */
191
- createItems(items: MaterialItem<P, L>[]): CreateItem<P, M, L>[];
192
- /**
193
- * Prepare one move to create new items
194
- * @param {MaterialItem[]} items The items to create
195
- * @returns {CreateItemsAtOnce} the move that creates the new items when executed
196
- */
197
- createItemsAtOnce(items: MaterialItem<P, L>[]): CreateItemsAtOnce<P, M, L>;
198
- /**
199
- * Prepare a move that will delete current first item in this material instance
200
- *
201
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to remove. If undefined, the item is completely removed
202
- * @returns {DeleteItem} the move that delete the item, or a part of its quantity, when executed
203
- */
204
- deleteItem(quantity?: number): DeleteItem<M>;
205
- /**
206
- * Prepare moves that will delete all the items in this material instance
207
- *
208
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to remove. If undefined, the items are completely removed
209
- * @returns {DeleteItem[]} the moves that delete the items, or a part of their quantity, when executed
210
- */
211
- deleteItems(quantity?: number): DeleteItem<M>[];
212
- /**
213
- * Prepare one move that will delete all the items in this material instance
214
- *
215
- * @returns {DeleteItemsAtOnce} the move that delete the items when executed
216
- */
217
- deleteItemsAtOnce(): DeleteItemsAtOnce<M>;
218
- /**
219
- * Prepare a move that will change the location of the current first item in this material instance
220
- *
221
- * @param {Location | function} location The new location of the item. It can be a function to process the location based on the item current state.
222
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to move. If undefined, the item is completely moved.
223
- * @returns {MoveItem} the move that will change the location of the item (or a part of its quantity) when executed
224
- */
225
- moveItem<ItemId = any, LocId = ItemId, Rotation = any>(location: Location<P, L, LocId, Rotation> | ((item: MaterialItem<P, L, ItemId>) => Location<P, L, LocId, Rotation>), quantity?: number): MoveItem<P, M, L>;
226
- /**
227
- * Prepare moves that will change the location of all the items in this material instance
228
- *
229
- * @param {Location | function} location The new location of the items. It can be a function to process the location based on each item current state.
230
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to move. If undefined, the items are completely moved.
231
- * @returns {MoveItem[]} the moves that will change the location of the items (or a part of their quantity) when executed
232
- */
233
- moveItems<ItemId = any, LocId = ItemId, Rotation = any>(location: Partial<Location<P, L, LocId, Rotation>> | ((item: MaterialItem<P, L, ItemId>, index: number) => Partial<Location<P, L, LocId, Rotation>>), quantity?: number): MoveItem<P, M, L>[];
234
- /**
235
- * Prepare one move that will change the location of all the items in this material instance
236
- *
237
- * @param {Location} location The new location of the items. It can only be the same location for every item.
238
- * @returns {MoveItemsAtOnce} the move that will change the location of the items when executed
239
- */
240
- moveItemsAtOnce<Id = any, Rotation = any>(location: Partial<Location<P, L, Id, Rotation>>): MoveItemsAtOnce<P, M, L>;
241
- /**
242
- * Prepare a move that will select current first item in this material instance
243
- *
244
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to select. If undefined, the item is completely selected.
245
- * @returns {SelectItem} the move that will select the item (or a part of its quantity) when executed
246
- */
247
- selectItem(quantity?: number): SelectItem<M>;
248
- /**
249
- * Prepare a move that will select all the items in this material instance
250
- *
251
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to select. If undefined, the items are completely selected.
252
- * @returns {SelectItem[]} the moves that will select the items (or a part of their quantity) when executed
253
- */
254
- selectItems(quantity?: number): SelectItem<M>[];
255
- /**
256
- * Prepare a move that will unselect current first item in this material instance
257
- *
258
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to unselect. If undefined, the item is completely unselected.
259
- * @returns {SelectItem} the move that will unselect the item (or a part of its quantity) when executed
260
- */
261
- unselectItem(quantity?: number): SelectItem<M>;
262
- /**
263
- * Prepare a move that will unselect all the items in this material instance
264
- *
265
- * @param {number | undefined} quantity Optional: for items with a quantity, the number of items to unselect. If undefined, the items are completely unselected.
266
- * @returns {SelectItem[]} the moves that will unselect the items (or a part of their quantity) when executed
267
- */
268
- unselectItems(quantity?: number): SelectItem<M>[];
269
- /**
270
- * Prepare a move that will rotate current first item in this material instance.
271
- * This function creates a {@link MoveItem} but copies the existing location values, only replacing the rotation property.
272
- *
273
- * @param {string | number | boolean | Record | function | undefined} arg Value of the rotation to give to the item.
274
- * In case of a function, process the value based on the item state.
275
- * @returns {MoveItem} the move that will rotate the item when executed
276
- */
277
- rotateItem<R extends string | number | boolean | Record<string, any> | undefined>(arg?: R | ((item: MaterialItem<P, L>) => R)): MoveItem<P, M, L>;
278
- /**
279
- * Prepare a move that will rotate all the items in this material instance.
280
- * This function creates an array of {@link MoveItem} but copies the existing locations values, only replacing the rotation property.
281
- *
282
- * @param {string | number | boolean | Record | function | undefined} arg Value of the rotation to give to the item.
283
- * In case of a function, process the value based on the item state.
284
- * @returns {MoveItem[]} the moves that will rotate the item when executed
285
- */
286
- rotateItems<R extends string | number | boolean | Record<string, any> | undefined>(arg?: R | ((item: MaterialItem<P, L>) => R | undefined)): MoveItem<P, M, L>[];
287
- /**
288
- * Prepare a move that will shuffle all the items in the current material instance
289
- * @returns {Shuffle} the move that shuffle the items when executed
290
- */
291
- shuffle(): Shuffle<M>;
292
- /**
293
- * Prepare a move that will roll the current first item in this material instance. See {@link RollItem}.
294
- *
295
- * @param {Location | function} location The new location of the item. It can be a function to process the location based on the item current state.
296
- * @returns {RollItem} the move that rolls the item when executed
297
- */
298
- rollItem(location?: Location<P, L> | ((item: MaterialItem<P, L>) => Location<P, L>)): RollItem<P, M, L>;
299
- /**
300
- * Prepare a move that will roll all the items in this material instance. See {@link RollItem}.
301
- *
302
- * @param {Location | function} location The new location of the items. It can be a function to process the location based on each item current state.
303
- * @returns {RollItem[]} the moves that rolls the items when executed
304
- */
305
- rollItems(location?: Location<P, L> | ((item: MaterialItem<P, L>) => Location<P, L>)): RollItem<P, M, L>[];
18
+ export declare class Material<P extends number = number, M extends number = number, L extends number = number> extends MaterialBase<P, M, L> {
306
19
  /**
307
20
  * Return a new {@link MaterialDeck} helper class, to deal cards easily.
308
21
  *