@gamepark/rules-api
Version:
API to implement the rules of a board game
98 lines (97 loc) • 4.59 kB
TypeScript
import { GameSetup } from '../GameSetup';
import { Material } from './items';
import { MaterialGame } from './MaterialGame';
import { MaterialRules, MaterialRulesCreator } from './MaterialRules';
import { GameMemory, PlayerMemory } from './memory';
import { MaterialMove } from './moves';
import { TutorialState } from './tutorial';
/**
* Helper class to implement {@link GameSetup} when using the {@link MaterialRules} approach.
*/
export declare abstract class MaterialGameSetup<P extends number = number, M extends number = number, L extends number = number, Options = any, R extends number = number> implements GameSetup<MaterialGame<P, M, L, R>, Options> {
/**
* The rules of the game
*/
abstract Rules: MaterialRulesCreator<P, M, L, R>;
/**
* The game setup state we are working on
* @protected
*/
protected game: MaterialGame<P, M, L, R>;
/**
* Get an instance of the rules of the game
*/
get rules(): MaterialRules<P, M, L, R>;
/**
* Entry point for {@link GameSetup}
* @param options Options of the game
* @param tutorial Initial tutorial state if any
* @returns the initial state of the game
*/
setup(options: Options, tutorial?: TutorialState<P, M, L, R>): MaterialGame<P, M, L, R>;
/**
* @returns array of the player ids (shortcut for this.game.players)
*/
get players(): P[];
/**
* Override this function if you need to set up some material before the game starts. Called by {@link setup}.
* @param _options Options of the game
*/
setupMaterial(_options: Options): void;
/**
* Help function to execute a move immediately on the game state.
* When the game is on, the moves should never be played directly: the framework takes care of playing the moves when necessary
* (on te server, on the clients, during replays...).
* However, during the setup, the moves must be played immediately in the game state to provide the initial game state to the framework.
* This help function allows to easily play a {@link MaterialMove}, using the rules provided in {@link Rules}, including the consequences.
*
* @param move The MaterialMove to play
* @protected
*/
protected playMove(move: MaterialMove<P, M, L, R>): void;
/**
* Helper function to manipulate the items of the game. See {@link Material}.
* @param type The type of Material we want to work on
* @returns a Material instance to manipulate all the material of that type in current game state.
*/
material(type: M): Material<P, M, L>;
/**
* Utility function to access the memory tool for the game or on player.
* this.game.memory can be used to store any data that is not available through the state of the material, or current rule.
*
* @param player Optional, identifier of the player if we want to manipulate a specific player's memory
* @returns {@link GameMemory} or {@link PlayerMemory} utility
* @protected
*/
protected getMemory(player?: P): GameMemory<P> | PlayerMemory<P>;
/**
* Helper function to memorize some information that does not fit in an item or the rules state, in the game state
* @param key Key under which the memory is store. Usually a value of a numeric enum named "Memory".
* @param value Value to memorize
* @param player optional, if we need to memorize a different value for each player.
*/
memorize<T = any>(key: keyof any, value: T | ((lastValue: T) => T), player?: P): T;
/**
* Implement this function to provide the first rules step of the game (see {@link MaterialRules.rules}). Called by {@link setup}.
* You can use {@link startPlayerTurn} or {@link startSimultaneousRule} for instance.
* @param options Options of the game
*/
abstract start(options: Options): void;
/**
* Helper function to play a {@link StartPlayerTurn} move on the game setup state.
* @param id Rule id to start
* @param player Player that starts the game (default value: this.game.players[0])
*/
startPlayerTurn(id: R, player?: P): void;
/**
* Helper function to play a {@link StartSimultaneousRule} move on the game setup state.
* @param id Rule id to start
* @param players Players that are active (all the players by default)
*/
startSimultaneousRule(id: R, players?: P[]): void;
/**
* Helper function to play a {@link StartRule} move on the game setup state.
* @param id Rule id to start
*/
startRule(id: R): void;
}