UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

86 lines (85 loc) 4.54 kB
import { HiddenInformation } from '../HiddenInformation'; import { PlayMoveContext } from '../Rules'; import { MaterialItem } from './items'; import { MaterialGame } from './MaterialGame'; import { MaterialRules } from './MaterialRules'; import { MaterialMove, MaterialMoveRandomized, MaterialMoveView } from './moves'; /** * Implement HiddenMaterialRules when you want to use the {@link MaterialRules} approach with {@link HiddenInformation}. * Using some {@link HidingStrategy} allows to enforce the security of a game with hidden information easily. * If the game has secret information (some players have information not available to others, link cards in their hand), then you * must implement {@link SecretMaterialRules} instead. */ export declare abstract class HiddenMaterialRules<P extends number = number, M extends number = number, L extends number = number, R extends number = number> extends MaterialRules<P, M, L, R> implements HiddenInformation<MaterialGame<P, M, L, R>, MaterialMove<P, M, L, R>, MaterialMove<P, M, L, R>> { private readonly client?; constructor(game: MaterialGame<P, M, L, R>, client?: { player?: P; } | undefined); /** * A hiding strategy allows to hide automatically some information about an item when it is at a specific location type. * Usually, we hide the item id (or a part of it). * Example: {[MaterialType.Card]: {[LocationType.Deck]: hideItemId}} will hide the id of the cards in the deck to everybody. * See {@link HidingStrategy} */ abstract readonly hidingStrategies: Partial<Record<M, Partial<Record<L, HidingStrategy<P, L>>>>>; randomize(move: MaterialMove<P, M, L, R>, player?: P): MaterialMove<P, M, L> & MaterialMoveRandomized<P, M, L, R>; private isRevealingItemMove; /** * Items that can be hidden cannot merge by default, to prevent hidden items to merge only because they have no id. */ itemsCanMerge(type: M): boolean; /** * Moves that reveal some information (like drawing a card) cannot be predicted by the player. */ isUnpredictableMove(move: MaterialMove<P, M, L, R>, player: P): boolean; /** * Moves than reveals an information to someone cannot be undone by default */ protected moveBlocksUndo(move: MaterialMove<P, M, L, R>, player?: P): boolean; /** * @param move A move to test * @returns true if the move revealed something to some player */ protected moveRevealedSomething(move: MaterialMove<P, M, L, R>): boolean; /** * With the material approach, we can offer a default working implementation for {@link HiddenInformation.getView} */ getView(player?: P): MaterialGame<P, M, L, R>; private hideItem; private getItemHiddenPaths; private itemHasHiddenInformation; /** * To be able to know if a MoveItem cannot be undone, the server flags the moves with a "reveal" property. * This difference must be integrated without error during the callback. */ canIgnoreServerDifference(clientMove: MaterialMove<P, M, L, R>, serverMove: MaterialMove<P, M, L, R>): boolean; /** * With the material approach, we can offer a default working implementation for {@link HiddenInformation.getMoveView} */ getMoveView(move: MaterialMoveRandomized<P, M, L, R>, player?: P): MaterialMove<P, M, L, R>; private getMoveItemView; private getMoveAtOnceView; private getMoveItemRevealedPath; private getMoveAtOnceRevealedPath; private moveItemWillRevealSomething; private moveAtOnceWillRevealSomething; private getShuffleItemsView; private canSeeShuffleResult; /** * Override of {@link MaterialRules.play} that also removes the hidden information from items, for example when a card is flipped face down */ play(move: MaterialMoveRandomized<P, M, L, R> | MaterialMoveView<P, M, L, R>, context?: PlayMoveContext): MaterialMove<P, M, L, R>[]; } /** * A Hiding Strategy is a function that takes an item and returns a list of path to hide in the item object. * See {@link hideItemId} and {@link hideFront} for 2 hiding strategy frequently used. */ export type HidingStrategy<P extends number = number, L extends number = number> = (item: MaterialItem<P, L>) => string[]; /** * Hiding strategy that removes the item id */ export declare const hideItemId: HidingStrategy; /** * Hiding strategy that removes "id.front" from the item (when we have cards with composite ids, back & front) */ export declare const hideFront: HidingStrategy;