@gamepark/rules-api
Version:
API to implement the rules of a board game
53 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlayerMemory = void 0;
/**
* Utility class to manipulate a player's memory in a {@link MaterialGame}.
*/
var PlayerMemory = /** @class */ (function () {
/**
* @constructor
* @param {MaterialGame} game Current state of the game
* @param player The player to work with
*/
function PlayerMemory(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
*/
PlayerMemory.prototype.memorize = function (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)
*/
PlayerMemory.prototype.remind = function (key) {
var _a;
return (_a = this.game.memory[key]) === null || _a === void 0 ? void 0 : _a[this.player];
};
/**
* Delete a value from the memory
* @param key Key of the value to delete
*/
PlayerMemory.prototype.forget = function (key) {
var _a;
(_a = this.game.memory[key]) === null || _a === void 0 ? true : delete _a[this.player];
};
return PlayerMemory;
}());
exports.PlayerMemory = PlayerMemory;
//# sourceMappingURL=PlayerMemory.js.map