duel-controller.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _heroesHelper = require("./heroesHelper");
  7. var DuelController = function DuelController(dataStore) {
  8. var currentGameState = null;
  9. var gameData = {};
  10. var allHeroesSet = {};
  11. var states = {
  12. '0_INIT': new initState(this),
  13. '1_SELECT_FACTION': new selectFactionState(this)
  14. };
  15. var setCurrentState = function setCurrentState(stateName) {
  16. var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  17. currentGameState = states[stateName];
  18. gameData.gameState = stateName;
  19. currentGameState.start(payload);
  20. };
  21. var updateCurrentState = function updateCurrentState() {
  22. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  23. currentGameState.update(payload);
  24. };
  25. var storeData = function storeData() {
  26. return dataStore.save(gameData);
  27. };
  28. var loadData = function loadData() {
  29. gameData = dataStore.load();
  30. };
  31. var startNewGame = function startNewGame(player1Name, player2Name, allHeroesJson, gameOptions) {
  32. allHeroesSet = (0, _heroesHelper.initHeroesFromJson)(allHeroesJson);
  33. setCurrentState('0_INIT', {
  34. player1Name: player1Name,
  35. player2Name: player2Name,
  36. gameOptions: gameOptions
  37. });
  38. updateCurrentState();
  39. };
  40. var resumeGame = function resumeGame() {};
  41. var DuelControllerProxy = function DuelControllerProxy() {
  42. return {
  43. endTurn: function endTurn(data) {
  44. currentGameState.update(data);
  45. }
  46. };
  47. };
  48. return {
  49. startNewGame: startNewGame,
  50. resumeGame: resumeGame,
  51. duelControllerProxy: new DuelControllerProxy(),
  52. storeData: storeData,
  53. loadData: loadData,
  54. setCurrentState: setCurrentState,
  55. updateCurrentState: updateCurrentState,
  56. gameData: gameData,
  57. allHeroesSet: allHeroesSet
  58. };
  59. };
  60. var initState = function initState(duelCtrl) {
  61. var start = function start() {
  62. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  63. duelCtrl.gameData = {
  64. playerBlue: {
  65. name: payload.player1Name
  66. },
  67. playerRed: {
  68. name: payload.player2Name
  69. },
  70. gameOptions: payload.gameOptions,
  71. waitingForBlue: false,
  72. waitingForRed: false,
  73. currentPlayer: ''
  74. };
  75. };
  76. var update = function update() {
  77. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  78. duelCtrl.storeData().then(function () {
  79. switch (duelCtrl.gameData.gameOptions.deck) {
  80. case 'faction':
  81. duelCtrl.setCurrentState('1_SELECT_FACTION');
  82. break;
  83. case 'tournament':
  84. break;
  85. case 'draft':
  86. break;
  87. default:
  88. break;
  89. }
  90. }).catch(function (err) {
  91. console.log('Error storing data ', err); // Retry in 5 sec
  92. setTimeout(function () {
  93. duelCtrl.updateCurrentState();
  94. }, 5000);
  95. });
  96. };
  97. return {
  98. start: start,
  99. update: update
  100. };
  101. };
  102. var selectFactionState = function selectFactionState(duelCtrl) {
  103. var start = function start() {
  104. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  105. duelCtrl.gameData.heroesToSelect = duelCtrl.allHeroesSet;
  106. duelCtrl.gameData.waitingForBlue = true;
  107. duelCtrl.gameData.waitingForRed = true;
  108. duelCtrl.gameData.currentPlayer = 'both';
  109. duelCtrl.storeData.then(function () {
  110. console.log('Wating for both players');
  111. }).catch(function (err) {
  112. console.log('Error storing data ', err); // Retry in 5 sec
  113. setTimeout(function () {
  114. duelCtrl.currentGameState.start();
  115. }, 5000);
  116. });
  117. };
  118. var update = function update() {
  119. var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  120. if (payload) {
  121. var player = 'player' + payload.player;
  122. duelCtrl.gameData[player].faction = payload.faction;
  123. duelCtrl.gameData[player].twelveHeroes = (0, _heroesHelper.getRandomHeroesByFaction)(payload.faction);
  124. duelCtrl.gameData['waitingFor' + payload.player] = false;
  125. }
  126. if (duelCtrl.gameData.waitingForBlue === false && duelCtrl.gameData.waitingForRed === false) {
  127. duelCtrl.storeData.then(function () {
  128. console.log('Going to next STATE');
  129. }).catch(function (err) {
  130. console.log('Error storing data ', err); // Retry in 5 sec
  131. setTimeout(function () {
  132. duelCtrl.currentGameState.update();
  133. }, 5000);
  134. });
  135. }
  136. };
  137. return {
  138. start: start,
  139. update: update
  140. };
  141. };
  142. var _default = DuelController;
  143. exports.default = _default;
  144. //# sourceMappingURL=duel-controller.js.map