@runox-game/game-engine
Version:
RunoX game engine
154 lines (153 loc) • 6.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameEngine = void 0;
var game_state_model_1 = require("./models/game-state.model");
var game_events_1 = require("./events/game-events");
var game_event_enum_1 = require("./events/game-event.enum");
var command_service_1 = require("./commands/command.service");
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var log_levels_enum_1 = require("./log/log-levels.enum");
var GameEngine = /** @class */ (function () {
function GameEngine() {
this.state = new game_state_model_1.GameState();
this.commandService = new command_service_1.CommandService();
this.gameEvents = game_events_1.GameEvents.getInstance();
}
Object.defineProperty(GameEngine.prototype, "events", {
get: function () {
var _a;
return _a = {},
_a[game_event_enum_1.GameEvent.AFTER_GAME_START] = this.gameEvents.afterGameStart$,
_a[game_event_enum_1.GameEvent.AFTER_PLAY_CARD] = this.gameEvents.afterPlayCard$,
_a[game_event_enum_1.GameEvent.AFTER_TAKE_CARDS] = this.gameEvents.afterTakeCards$,
_a[game_event_enum_1.GameEvent.AFTER_YELL_UNO] = this.gameEvents.afterYellUno$,
_a[game_event_enum_1.GameEvent.BEFORE_TURN] = this.gameEvents.beforeTurn$,
_a[game_event_enum_1.GameEvent.GAME_END] = this.gameEvents.gameEnd$,
_a[game_event_enum_1.GameEvent.CHANGE_COLOR] = this.gameEvents.changeColor$,
_a[game_event_enum_1.GameEvent.SKIP] = this.gameEvents.skip$,
_a[game_event_enum_1.GameEvent.REVERSE] = this.gameEvents.reverse$,
_a[game_event_enum_1.GameEvent.ERROR] = this.gameEvents.error$,
_a[game_event_enum_1.GameEvent.SPECIAL_CARD] = this.gameEvents.specialCard$,
_a[game_event_enum_1.GameEvent.STATE_CHANGED] = this.gameEvents.stateChanged$,
_a;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameEngine.prototype, "players", {
get: function () {
return this.state.playersGroup.players;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameEngine.prototype, "playerTurn", {
get: function () {
return this.state.turn.player;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameEngine.prototype, "stackCard", {
get: function () {
return this.state.stack.cardOnTop;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameEngine.prototype, "modes", {
get: function () {
return this.state.gameModes;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameEngine.prototype, "gameStateAsJSON", {
get: function () {
return {
id: this.state.id,
deck: this.state.deck,
stack: this.state.stack,
playersGroup: this.state.playersGroup,
turn: this.state.turn,
unoYellers: this.state.unoYellers,
gameDirection: this.state.gameDirection,
cardsToGive: this.state.cardsToGive,
gameModes: this.state.gameModes,
};
},
enumerable: false,
configurable: true
});
GameEngine.prototype.start = function (gameModes) {
return this.commandService
.startGame(this.state, gameModes)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.reset = function (gameModes) {
return this.commandService
.resetGame(this.state, gameModes)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.join = function (players) {
return this.commandService
.addPlayers(this.state, players)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.remove = function (player) {
return this.commandService
.removePlayer(this.state, player)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.playCard = function (playerId, card, toPlayerId) {
return this.commandService
.playCard(this.state, playerId, card, toPlayerId)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.takeCard = function () {
return this.commandService
.takeCard(this.state)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.takeCardToRandomPlayer = function () {
return this.commandService
.takeCardToRandomPlayer(this.state)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.uno = function (yellerId) {
return this.commandService
.yellUno(this.state, yellerId)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.switchHands = function (player1, player2) {
return this.commandService
.switchHands(this.state, player1, player2)
.pipe(operators_1.catchError(this.handleError()));
};
GameEngine.prototype.overrideInternalState = function (externalState) {
this.state.overrideInternalState(externalState);
};
GameEngine.prototype.logs = function (level) {
if (level === void 0) { level = log_levels_enum_1.LogLevel.USER; }
return this.state.logs(level);
};
GameEngine.prototype.onSpecialCardPlayed = function () {
return this.state.onSpecialCardPlayed();
};
GameEngine.prototype.onCardPlayed = function () {
return this.state.onCardPlayed();
};
GameEngine.prototype.onStateChanged = function () {
return this.state.onStateChanged();
};
GameEngine.prototype.handleError = function () {
var _this = this;
return function (e) {
_this.gameEvents.dispatchError(e);
return rxjs_1.of(e);
};
};
return GameEngine;
}());
exports.GameEngine = GameEngine;