UNPKG

@gamepark/rules-api

Version:

API to implement the rules of a board game

48 lines 1.5 kB
/** * Utility class to manipulate a player's memory in a {@link MaterialGame}. */ export class PlayerMemory { game; player; /** * @constructor * @param {MaterialGame} game Current state of the game * @param player The player to work with */ constructor(game, player) { this.game = game; this.player = player; } /** * Save a new value inside the memory * @param key The key to index the memorized value * @param value Any JSON serializable value to store, or a function that takes previous stored value and returns the new value to store */ memorize(key, value) { if (!this.game.memory[key]) this.game.memory[key] = {}; if (typeof value === 'function') { this.game.memory[key][this.player] = value(this.game.memory[key][this.player]); } else { this.game.memory[key][this.player] = value; } return this.game.memory[key][this.player]; } /** * Get a value stored in the memory * @param key The key to index the memorized value * @returns the value stored (or undefined is nothing was stored under this key yet) */ remind(key) { return this.game.memory[key]?.[this.player]; } /** * Delete a value from the memory * @param key Key of the value to delete */ forget(key) { delete this.game.memory[key]?.[this.player]; } } //# sourceMappingURL=PlayerMemory.js.map