game.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import AnyPlayer from './player/any-player';
  2. import allHeroesJson from '../../../server/src/client-server-shared/all-heroes.json';
  3. const initGameState = {
  4. gameState: '',
  5. allHeroes: [],
  6. deckMode: '',
  7. advRules: [],
  8. 'waitingFor/blue': false,
  9. 'waitingFor/red': false,
  10. currentPlayer: '',
  11. 'battleTiles/left': [],
  12. 'battleTiles/center': [],
  13. 'battleTiles/right': [],
  14. totalFood: 0,
  15. allHeroesJson
  16. };
  17. const state = Object.assign({}, initGameState);
  18. const getters = {
  19. state(state) {
  20. return state.gameState;
  21. },
  22. allHeroes(state) {
  23. return state.allHeroes;
  24. },
  25. deckMode(state) {
  26. return state.deckMode;
  27. },
  28. myColor(state, getters, rootState) {
  29. let color = 'blue';
  30. if (rootState.username === state.redPlayer.name) {
  31. color = 'red';
  32. }
  33. return color;
  34. },
  35. ennemyColor(state, getters) {
  36. let color = 'red';
  37. if (getters.myColor === 'red') {
  38. color = 'blue';
  39. }
  40. return color;
  41. },
  42. isItMyTurn(state, getters) {
  43. return state['waitingFor/' + getters.myColor];
  44. },
  45. isPopularityRule(state) {
  46. return state.advRules.includes('popularity');
  47. },
  48. isDiscardRule(state) {
  49. return state.advRules.includes('discard');
  50. },
  51. heroById: state => id => {
  52. return state.allHeroes.find(hero => hero.id === id);
  53. },
  54. myTwelveHeroes(state, getters) {
  55. let myColor = getters.myColor;
  56. let myHeroesIds = getters[myColor + 'Player/twelveHeroes'].map(
  57. hero => hero.id
  58. );
  59. return state.allHeroes.filter(hero => {
  60. return myHeroesIds.includes(hero.id);
  61. });
  62. },
  63. myDraftIds(state, getters) {
  64. let myColor = getters.myColor;
  65. return getters[myColor + 'Player/draftIds'];
  66. },
  67. filterHeroes: state => filter => {
  68. // filter has
  69. // - faction (orcs humans elves meca none all)
  70. // - popularity (with without)
  71. // - draft (yes no all)
  72. // - minPower <= power <= maxPower
  73. // - minCost <= cost <= maxCost
  74. // - byName : if true filter name with filter.name
  75. // - byId : [] of ids, deactivated if null or empty.
  76. if (!filter.faction) filter.faction = 'all';
  77. if (!filter.popularity) filter.popularity = 'without';
  78. if (!filter.minPower) filter.minPower = 0;
  79. if (!filter.maxPower) filter.maxPower = 0;
  80. if (!filter.minCost) filter.minCost = 0;
  81. if (!filter.maxCost) filter.minCost = 0;
  82. let draftFilter = 'all';
  83. switch (filter.draft) {
  84. case 'yes':
  85. draftFilter = true;
  86. break;
  87. case 'no':
  88. draftFilter = false;
  89. break;
  90. default:
  91. draftFilter = 'all';
  92. break;
  93. }
  94. return state.allHeroes.filter(hero => {
  95. if (filter.faction !== 'all' && hero.faction !== filter.faction)
  96. return false;
  97. if (
  98. filter.popularity !== 'none' &&
  99. hero.popularity !== 'any' &&
  100. hero.popularity !== filter.popularity
  101. )
  102. return false;
  103. if (draftFilter !== 'all' && hero.isDraftable !== draftFilter)
  104. return false;
  105. if (hero.power < filter.minPower || hero.power > filter.maxPower)
  106. return false;
  107. if (hero.cost < filter.minCost || hero.cost > filter.maxCost)
  108. return false;
  109. if (
  110. filter.byName === true &&
  111. !hero.name.toLowerCase().includes(filter.name.toLowerCase())
  112. )
  113. return false;
  114. if (
  115. filter.byId &&
  116. filter.byId.length > 0 &&
  117. !filter.byId.includes(hero.id)
  118. ) {
  119. return false;
  120. }
  121. return true;
  122. });
  123. }
  124. };
  125. const mutations = {
  126. SET_HEROES: (state, payload) => {
  127. state.allHeroes = payload;
  128. },
  129. SET_FULL_GAME_STATE: (state, payload) => {
  130. Object.assign(state, payload);
  131. },
  132. INIT_GAME_STATE: state => {
  133. Object.assign(state, initGameState);
  134. },
  135. SET_WAITING_FOR: (state, payload) => {
  136. state['waitingFor/' + payload.color] = payload.value;
  137. }
  138. };
  139. const actions = {
  140. setHeroes: ({ commit }, payload) => {
  141. commit('SET_HEROES', payload);
  142. },
  143. update: ({ commit }, payload) => {
  144. commit('SET_FULL_GAME_STATE', payload.game);
  145. commit('bluePlayer/SET_FULL_PLAYER_STATE', payload.bluePlayer);
  146. commit('redPlayer/SET_FULL_PLAYER_STATE', payload.redPlayer);
  147. },
  148. resetGameState: ({ commit }) => {
  149. commit('INIT_GAME_STATE');
  150. commit('bluePlayer/INIT_PLAYER_STATE');
  151. commit('redPlayer/INIT_PLAYER_STATE');
  152. }
  153. };
  154. const bluePlayer = new AnyPlayer();
  155. const redPlayer = new AnyPlayer();
  156. export default {
  157. namespaced: true,
  158. state,
  159. getters,
  160. mutations,
  161. actions,
  162. modules: {
  163. bluePlayer: {
  164. namespaced: true,
  165. ...bluePlayer
  166. },
  167. redPlayer: {
  168. namespaced: true,
  169. ...redPlayer
  170. }
  171. }
  172. };