UNPKG

@runox-game/game-engine

Version:
56 lines (55 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandsInvoker = void 0; var rxjs_1 = require("rxjs"); var log_levels_enum_1 = require("../log/log-levels.enum"); /** * Class in charge of grouping the execution of commands that alter the game state */ var CommandsInvoker = /** @class */ (function () { /** * Class in charge of grouping the execution of commands that alter the game state * * @param commands - commands to execute */ function CommandsInvoker(commands) { this.commands = commands; } /** * Loops through the list of commands validating that they can be executed * and executing if there are no errors during validation * * @param currentState - current game state * @returns observable with the intention of being able to track the success or failure * of the command group invocation */ CommandsInvoker.prototype.invoke = function (currentState) { var _this = this; var observable = new rxjs_1.Observable(function (subscriber) { try { for (var i = 0; i < _this.commands.length; i++) { var command = _this.commands[i]; currentState.log(command.toString(), log_levels_enum_1.LogLevel.BEFORE_VALIDATION); var commandValidation = command.validate(currentState); if (!commandValidation.isValid) { currentState.log(command.toString() + " invalid", log_levels_enum_1.LogLevel.AFTER_VALIDATION); subscriber.error(commandValidation.error); return; } currentState.log(command.toString() + " valid", log_levels_enum_1.LogLevel.AFTER_VALIDATION); currentState.log(command.toString(), log_levels_enum_1.LogLevel.BEFORE_COMMAND); command.execute(currentState); currentState.log(command.toString(), log_levels_enum_1.LogLevel.BEFORE_COMMAND); } subscriber.next(); subscriber.complete(); } catch (error) { subscriber.error(error); } }); return observable; }; return CommandsInvoker; }()); exports.CommandsInvoker = CommandsInvoker;