"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _heroesHelper = require("./heroesHelper"); var DuelController = function DuelController(dataStore) { var currentGameState = null; var gameData = {}; var allHeroesSet = {}; var states = { '0_INIT': new initState(this), '1_SELECT_FACTION': new selectFactionState(this) }; var setCurrentState = function setCurrentState(stateName) { var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; currentGameState = states[stateName]; gameData.gameState = stateName; currentGameState.start(payload); }; var updateCurrentState = function updateCurrentState() { var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; currentGameState.update(payload); }; var storeData = function storeData() { return dataStore.save(gameData); }; var loadData = function loadData() { gameData = dataStore.load(); }; var startNewGame = function startNewGame(player1Name, player2Name, allHeroesJson, gameOptions) { allHeroesSet = (0, _heroesHelper.initHeroesFromJson)(allHeroesJson); setCurrentState('0_INIT', { player1Name: player1Name, player2Name: player2Name, gameOptions: gameOptions }); updateCurrentState(); }; var resumeGame = function resumeGame() {}; var DuelControllerProxy = function DuelControllerProxy() { return { endTurn: function endTurn(data) { currentGameState.update(data); } }; }; return { startNewGame: startNewGame, resumeGame: resumeGame, duelControllerProxy: new DuelControllerProxy(), storeData: storeData, loadData: loadData, setCurrentState: setCurrentState, updateCurrentState: updateCurrentState, gameData: gameData, allHeroesSet: allHeroesSet }; }; var initState = function initState(duelCtrl) { var start = function start() { var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; duelCtrl.gameData = { playerBlue: { name: payload.player1Name }, playerRed: { name: payload.player2Name }, gameOptions: payload.gameOptions, waitingForBlue: false, waitingForRed: false, currentPlayer: '' }; }; var update = function update() { var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; duelCtrl.storeData().then(function () { switch (duelCtrl.gameData.gameOptions.deck) { case 'faction': duelCtrl.setCurrentState('1_SELECT_FACTION'); break; case 'tournament': break; case 'draft': break; default: break; } }).catch(function (err) { console.log('Error storing data ', err); // Retry in 5 sec setTimeout(function () { duelCtrl.updateCurrentState(); }, 5000); }); }; return { start: start, update: update }; }; var selectFactionState = function selectFactionState(duelCtrl) { var start = function start() { var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; duelCtrl.gameData.heroesToSelect = duelCtrl.allHeroesSet; duelCtrl.gameData.waitingForBlue = true; duelCtrl.gameData.waitingForRed = true; duelCtrl.gameData.currentPlayer = 'both'; duelCtrl.storeData.then(function () { console.log('Wating for both players'); }).catch(function (err) { console.log('Error storing data ', err); // Retry in 5 sec setTimeout(function () { duelCtrl.currentGameState.start(); }, 5000); }); }; var update = function update() { var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; if (payload) { var player = 'player' + payload.player; duelCtrl.gameData[player].faction = payload.faction; duelCtrl.gameData[player].twelveHeroes = (0, _heroesHelper.getRandomHeroesByFaction)(payload.faction); duelCtrl.gameData['waitingFor' + payload.player] = false; } if (duelCtrl.gameData.waitingForBlue === false && duelCtrl.gameData.waitingForRed === false) { duelCtrl.storeData.then(function () { console.log('Going to next STATE'); }).catch(function (err) { console.log('Error storing data ', err); // Retry in 5 sec setTimeout(function () { duelCtrl.currentGameState.update(); }, 5000); }); } }; return { start: start, update: update }; }; var _default = DuelController; exports.default = _default; //# sourceMappingURL=duel-controller.js.map