@runox-game/game-engine
Version:
RunoX game engine
194 lines (193 loc) • 8.49 kB
JavaScript
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameState = void 0;
var deck_model_1 = require("./deck.model");
var players_group_model_1 = require("./players-group.model");
var turn_model_1 = require("./turn.model");
var stack_model_1 = require("./stack.model");
var game_direction_model_1 = require("./game-direction.model");
var card_model_1 = require("./card.model");
var player_model_1 = require("./player.model");
var game_events_1 = require("../events/game-events");
var values_model_1 = require("./values.model");
var rxjs_1 = require("rxjs");
var log_factory_1 = require("../log/log.factory");
var log_levels_enum_1 = require("../log/log-levels.enum");
var operators_1 = require("rxjs/operators");
/** Clase que representa el estado del juego */
var GameState = /** @class */ (function () {
function GameState() {
this.id = new Date().getTime();
this.deck = new deck_model_1.Deck();
this.stack = new stack_model_1.Stack();
this.playersGroup = new players_group_model_1.PlayersGroup();
this.turn = new turn_model_1.Turn();
this.events = game_events_1.GameEvents.getInstance();
this.gameDirection = game_direction_model_1.GameDirection.CLOCKWISE;
this.cardsToGive = 0;
this.unoYellers = {};
this.gameModes = {
randomTakeDeckCard: false,
};
this.winner = undefined;
this.winnerScore = 0;
this.log$ = new rxjs_1.BehaviorSubject(log_factory_1.LogFactory.default());
}
Object.defineProperty(GameState.prototype, "nextPlayerToPlay", {
get: function () {
var _this = this;
// es el primer turno, entonces elegimos el primer jugador
if (!this.turn.player) {
return this.playersGroup.players[0];
}
var currentPlayerIndex = this.playersGroup.players.findIndex(function (player) { var _a; return player.id === ((_a = _this.turn.player) === null || _a === void 0 ? void 0 : _a.id); });
var nextPlayerIndex = currentPlayerIndex + 1;
// no ha terminado la vuelta, entonces elegimos el proximo en la lista
if (nextPlayerIndex < this.playersGroup.players.length) {
return this.playersGroup.players[nextPlayerIndex];
}
// ya ha jugado el ultimo, entonces comienza nuevamente desde el primero
return this.playersGroup.players[0];
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameState.prototype, "valid", {
get: function () {
return (this.deck !== undefined &&
this.gameDirection !== undefined &&
this.gameModes !== undefined &&
this.nextPlayerToPlay !== undefined &&
this.playersGroup !== undefined && this.playersGroup.valid &&
this.turn !== undefined && this.turn.valid &&
this.unoYellers !== undefined);
},
enumerable: false,
configurable: true
});
GameState.prototype.changeDirection = function () {
var newDirection = this.gameDirection === game_direction_model_1.GameDirection.CLOCKWISE
? game_direction_model_1.GameDirection.COUNTER_CLOCKWISE
: game_direction_model_1.GameDirection.CLOCKWISE;
this.gameDirection = newDirection;
this.playersGroup.players.reverse();
};
GameState.prototype.giveCards = function (quantity, toPlayer) {
if (quantity > this.deck.cards.length) {
this.addStackCardsToDeck();
}
var newCards = [];
for (var index = 0; index < quantity; index++) {
newCards = __spreadArrays(newCards, [this.deck.takeCard()]);
}
toPlayer.hand.addCards(newCards);
return newCards;
};
GameState.prototype.addStackCardsToDeck = function () {
var _this = this;
var newDeckCards = this.stack.cards.filter(function (card) { var _a; return card.id !== ((_a = _this.stack.cardOnTop) === null || _a === void 0 ? void 0 : _a.id); });
this.deck.addCards(newDeckCards);
var cardOnTopTheStack = this.stack.cardOnTop;
if (!cardOnTopTheStack) {
throw new Error('No se pudo obtener la carta de la cima del stack');
}
this.stack.empty();
this.stack.addCard(cardOnTopTheStack);
this.deck.shuffle();
};
GameState.prototype.overrideInternalState = function (state) {
this.id = state.id;
this.deck.cards = state.deck.cards.map(function (card) {
return new card_model_1.Card(card.value, card.color, card.id);
});
this.stack.cards = state.stack.cards.map(function (card) {
if (card.value === values_model_1.Value.PLUS_FOUR || card.value === values_model_1.Value.WILDCARD) {
var specialCard = new card_model_1.Card(card.value, undefined, card.id);
specialCard.setColor(card.color);
return specialCard;
}
return new card_model_1.Card(card.value, card.color, card.id);
});
this.playersGroup.players = state.playersGroup.players.map(function (player) {
var pl = new player_model_1.Player(player.id, player.name, player.pic);
pl.hand.cards = player.hand.cards.map(function (card) {
return new card_model_1.Card(card.value, card.color, card.id);
});
return pl;
});
this.turn.player = state.turn.player
? this.playersGroup.players.find(function (player) { var _a; return player.id === ((_a = state.turn.player) === null || _a === void 0 ? void 0 : _a.id); })
: undefined;
this.unoYellers = state.unoYellers;
this.gameDirection = state.gameDirection;
this.cardsToGive = state.cardsToGive;
this.gameModes = state.gameModes;
this.winner = state.winner;
};
GameState.prototype.setWinner = function (player, score) {
if (this.winner) {
throw new Error('Ya hay un ganador');
}
this.winner = player;
this.winnerScore = score;
};
/**
* Log message in the state with a level asigned.
*
* @remarks
* If levels is undefined will be assigned LogLevel.DEFAULT
*
* @param state
* @param message
* @param level
*/
GameState.prototype.log = function (message, level) {
this.log$.next({ level: level !== null && level !== void 0 ? level : log_levels_enum_1.LogLevel.DEFAULT, mesagge: message });
};
/**
* Return an observable with a flow of logs filtered by level
* @param level
*/
GameState.prototype.logs = function (level) {
return this.log$.asObservable().pipe(operators_1.filter(function (x) { return x.level === level; }));
};
/**
* Return an observable with a special card has played
*/
GameState.prototype.onSpecialCardPlayed = function () {
return this.events.specialCard$;
};
/**
* Return an observable with a common card has played
*/
GameState.prototype.onCardPlayed = function () {
return this.events.cardplayed$;
};
/**
* Return an observable with game state
*/
GameState.prototype.onStateChanged = function () {
return this.events.stateChanged$;
};
/**
* Find and return users who should yell uno
*/
GameState.prototype.getPlayersWhoShouldHaveYelled = function () {
var _this = this;
return this.playersGroup.players.filter(function (player) {
var _a;
return player.id !== ((_a = _this.turn.player) === null || _a === void 0 ? void 0 : _a.id) &&
player.hand.cards.length === 1 &&
!_this.unoYellers[player.id];
});
};
return GameState;
}());
exports.GameState = GameState;