duel-controller.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. /* This file will be used by server for online game, and by app for local game
  3. Therfore it should not have any dependency on any of those environments.
  4. It is a state machine that follows the game steps
  5. It receives a dataStore to get and share data but doesn't care what is
  6. behind (socket.io comm for online, direct call to vuex store for local)
  7. Is gives an interface in order to receive notifications
  8. Receiving interface :
  9. - dataStore : provides methods :
  10. - .load() : to get saved game data
  11. - .save(data) : to store game data and propagate (returns true if it succeeded, false otherwise)
  12. - .getHeroesJson() : to get JSON file with all heroes cards described
  13. Provides interface :
  14. - duelControllerProxy : provides methods :
  15. - endTurn(data) : notifies the controller that players ended current turn with attached data */
  16. import { initHeroesFromJson } from './heroesHelper';
  17. import gameStates from './gameStates/gameStates';
  18. let DuelController = function(dataStore) {
  19. let gameData = {
  20. game: {},
  21. bluePlayer: {},
  22. redPlayer: {}
  23. };
  24. let allHeroes = null;
  25. /** ***** STATE MACHINE internal methods ***** */
  26. // Holds current running state
  27. let currentGameState = null;
  28. // Set new current State
  29. let setCurrentState = stateName => {
  30. currentGameState = states[stateName];
  31. gameData.game.gameState = stateName;
  32. console.log('Set new state : ', stateName);
  33. };
  34. // get new State
  35. let getCurrentState = () => {
  36. return currentGameState;
  37. };
  38. // Start the current state
  39. let startCurrentState = (payload = null) => {
  40. currentGameState.start(payload);
  41. };
  42. // Update the current state
  43. let updateCurrentState = (payload = null) => {
  44. currentGameState.update(payload);
  45. };
  46. // End the current state
  47. let endCurrentState = (payload = null) => {
  48. currentGameState.end(payload);
  49. };
  50. let getGameData = () => gameData;
  51. // Interface to set data and store it
  52. let storeData = (data = null) => {
  53. if (data) {
  54. setData(data);
  55. }
  56. return dataStore.save(gameData);
  57. };
  58. // Interface to set data only
  59. let setData = data => {
  60. gameData = data;
  61. };
  62. // Interface to load data
  63. let loadData = () => {
  64. gameData = dataStore.load();
  65. };
  66. // Interface to get set of all heroes
  67. let getAllHeroes = () => {
  68. if (!allHeroes) {
  69. allHeroes = initHeroesFromJson(dataStore.getHeroesJson());
  70. }
  71. return allHeroes;
  72. };
  73. // Pass those state machine methods to each state
  74. let stateCtrl = {
  75. setCurrentState,
  76. getCurrentState,
  77. startCurrentState,
  78. updateCurrentState,
  79. endCurrentState,
  80. storeData,
  81. setData,
  82. getGameData,
  83. loadData,
  84. getAllHeroes
  85. };
  86. // declare all states
  87. // First state to be set after public APIs (start or resume game) are called
  88. let states = {
  89. '0_INIT': new gameStates.initState(stateCtrl),
  90. '1_SELECT_FACTION': new gameStates.selectFactionState(stateCtrl),
  91. '1_SELECT_DRAFT': new gameStates.selectDraftState(stateCtrl),
  92. '1_SELECT_TOURNAMENT': new gameStates.selectTournamentState(stateCtrl),
  93. '2_CHANGE_UP_TO_3_CARDS': new gameStates.changeUpTo3Cards(stateCtrl)
  94. };
  95. /** ***** Following are public methods ***** */
  96. // Method to create and start a new game (both players are there)
  97. let startNewGame = function(player1Name, player2Name, gameOptions) {
  98. // 1st state is init
  99. setCurrentState('0_INIT');
  100. //Start it
  101. startCurrentState({
  102. player1Name,
  103. player2Name,
  104. gameOptions
  105. });
  106. };
  107. // Method to resume existing game, gameData is passed already
  108. let resumeGame = function(data) {
  109. gameData = data;
  110. // Just set state per data in DB
  111. // This state will wait for update
  112. setCurrentState(gameData.game.gameState);
  113. };
  114. // Provide an interface so that players can notify the DuelController
  115. let DuelControllerProxy = () => {
  116. return {
  117. endTurn: data => {
  118. console.log('data from player :>> ', data);
  119. currentGameState.update(data);
  120. }
  121. };
  122. };
  123. // Return public methods
  124. return {
  125. startNewGame,
  126. resumeGame,
  127. duelControllerProxy: new DuelControllerProxy()
  128. };
  129. };
  130. export default DuelController;