src_store_game_game.js.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Source: src/store/game/game.js | 12 Heroes</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css">
  12. <link type="text/css" rel="stylesheet" href="styles/prettify-jsdoc.css">
  13. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  14. <link type="text/css" rel="stylesheet" href="styles/tui-doc.css">
  15. </head>
  16. <body>
  17. <nav class="lnb" id="lnb">
  18. <div class="logo" style="">
  19. <img src="img/toast-ui.png" width="100%" height="100%">
  20. </div>
  21. <div class="title">
  22. <h1><a href="index.html" class="link">12 Heroes</a></h1>
  23. </div>
  24. <div class="search-container" id="search-container">
  25. <input type="text" placeholder="Search">
  26. <ul></ul>
  27. </div>
  28. <div class="lnb-api hidden"><h3>Namespaces</h3><ul><li><a href="Constants.html">Constants</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Constants_sub"></div></li><li><a href="Constants.HERO.html">Constants.HERO</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Constants.HERO_sub"><div class="member-type">Members</div><ul class="inner"><li><a href="Constants.HERO.html#.HERO_ABILITY">HERO_ABILITY</a></li><li><a href="Constants.HERO.html#.HERO_DEPLOY">HERO_DEPLOY</a></li><li><a href="Constants.HERO.html#.HERO_DISCARD">HERO_DISCARD</a></li><li><a href="Constants.HERO.html#.HERO_DISMISS">HERO_DISMISS</a></li><li><a href="Constants.HERO.html#.HERO_MOVE">HERO_MOVE</a></li><li><a href="Constants.HERO.html#.HERO_RECRUIT">HERO_RECRUIT</a></li><li><a href="Constants.HERO.html#.HERO_REPLACE">HERO_REPLACE</a></li></ul></div></li><li><a href="Constants.PLAYER.html">Constants.PLAYER</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Constants.PLAYER_sub"><div class="member-type">Members</div><ul class="inner"><li><a href="Constants.PLAYER.html#.PLAYER_PASS">PLAYER_PASS</a></li><li><a href="Constants.PLAYER.html#.PLAYER_SUPPLY">PLAYER_SUPPLY</a></li></ul></div></li><li><a href="Constants.POS.html">Constants.POS</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Constants.POS_sub"><div class="member-type">Members</div><ul class="inner"><li><a href="Constants.POS.html#.POS_BATTLE_CENTER">POS_BATTLE_CENTER</a></li><li><a href="Constants.POS.html#.POS_BATTLE_LEFT">POS_BATTLE_LEFT</a></li><li><a href="Constants.POS.html#.POS_BATTLE_RIGHT">POS_BATTLE_RIGHT</a></li><li><a href="Constants.POS.html#.POS_CAMP">POS_CAMP</a></li><li><a href="Constants.POS.html#.POS_DISCARD">POS_DISCARD</a></li><li><a href="Constants.POS.html#.POS_HAND">POS_HAND</a></li><li><a href="Constants.POS.html#.POS_PILE">POS_PILE</a></li></ul></div></li><li><a href="Constants.REQ.html">Constants.REQ</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Constants.REQ_sub"><div class="member-type">Members</div><ul class="inner"><li><a href="Constants.REQ.html#.REQ_ERROR">REQ_ERROR</a></li><li><a href="Constants.REQ.html#.REQ_IDLE">REQ_IDLE</a></li><li><a href="Constants.REQ.html#.REQ_REQUESTED">REQ_REQUESTED</a></li><li><a href="Constants.REQ.html#.REQ_SUCCESS">REQ_SUCCESS</a></li></ul></div></li></ul></div><div class="lnb-api hidden"><h3>Global</h3><ul><li><a href="global.html#getDraftSets">getDraftSets</a></li><li><a href="global.html#getHeroesIdsByFaction">getHeroesIdsByFaction</a></li><li><a href="global.html#shuffleHeroes">shuffleHeroes</a></li><li><a href="global.html#TH_Ability">TH_Ability</a></li><li><a href="global.html#TH_AbilityHook">TH_AbilityHook</a></li><li><a href="global.html#TH_AdvRule">TH_AdvRule</a></li><li><a href="global.html#TH_BattleTile">TH_BattleTile</a></li><li><a href="global.html#TH_Color">TH_Color</a></li><li><a href="global.html#TH_CommStatus">TH_CommStatus</a></li><li><a href="global.html#TH_DeckMode">TH_DeckMode</a></li><li><a href="global.html#TH_Faction">TH_Faction</a></li><li><a href="global.html#TH_GameDataStore">TH_GameDataStore</a></li><li><a href="global.html#TH_GameGlobalState">TH_GameGlobalState</a></li><li><a href="global.html#TH_GameStateEnum">TH_GameStateEnum</a></li><li><a href="global.html#TH_HeroAction">TH_HeroAction</a></li><li><a href="global.html#TH_HeroCard">TH_HeroCard</a></li><li><a href="global.html#TH_HeroInGame">TH_HeroInGame</a></li><li><a href="global.html#TH_HeroPosition">TH_HeroPosition</a></li><li><a href="global.html#TH_MessageChatFrom">TH_MessageChatFrom</a></li><li><a href="global.html#TH_MessageDraftStep">TH_MessageDraftStep</a></li><li><a href="global.html#TH_MessageFaction">TH_MessageFaction</a></li><li><a href="global.html#TH_MessageTournamentDeckStep">TH_MessageTournamentDeckStep</a></li><li><a href="global.html#TH_PlayerAction">TH_PlayerAction</a></li><li><a href="global.html#TH_PlayerGameState">TH_PlayerGameState</a></li><li><a href="global.html#TH_Popularity">TH_Popularity</a></li><li><a href="global.html#TH_ReqStatus">TH_ReqStatus</a></li></ul></div>
  29. </nav>
  30. <div id="resizer"></div>
  31. <div class="main" id="main">
  32. <section>
  33. <article>
  34. <pre class="prettyprint source linenums"><code>'use strict';
  35. import AnyPlayer from './player/any-player';
  36. import allHeroesJson from '../../../server/src/client-server-shared/all-heroes.json';
  37. const initGameState = {
  38. gameState: '',
  39. allHeroes: [],
  40. deckMode: '',
  41. advRules: [],
  42. 'waitingFor/blue': false,
  43. 'waitingFor/red': false,
  44. currentPlayer: '',
  45. 'battleTiles/left': [],
  46. 'battleTiles/center': [],
  47. 'battleTiles/right': [],
  48. totalFood: 0,
  49. allHeroesJson
  50. };
  51. const state = Object.assign({}, initGameState);
  52. const getters = {
  53. state(state) {
  54. return state.gameState;
  55. },
  56. allHeroes(state) {
  57. return state.allHeroes;
  58. },
  59. deckMode(state) {
  60. return state.deckMode;
  61. },
  62. myColor(state, getters, rootState) {
  63. let color = 'blue';
  64. if (rootState.username === state.redPlayer.name) {
  65. color = 'red';
  66. }
  67. return color;
  68. },
  69. my: (state, getters) => target => {
  70. return getters.myColor + 'Player/' + target;
  71. },
  72. ennemyColor(state, getters) {
  73. let color = 'red';
  74. if (getters.myColor === 'red') {
  75. color = 'blue';
  76. }
  77. return color;
  78. },
  79. isItMyTurn(state, getters) {
  80. return state['waitingFor/' + getters.myColor];
  81. },
  82. isPopularityRule(state) {
  83. return state.advRules.includes('popularity');
  84. },
  85. isDiscardRule(state) {
  86. return state.advRules.includes('discard');
  87. },
  88. heroById: state => id => {
  89. return state.allHeroes.find(hero => hero.id === id);
  90. },
  91. myTwelveHeroes(state, getters) {
  92. let myHeroesIds = getters[getters.my('twelveHeroes')].map(hero => hero.id);
  93. return state.allHeroes.filter(hero => {
  94. return myHeroesIds.includes(hero.id);
  95. });
  96. },
  97. myDraftIds(state, getters) {
  98. return getters[getters.my('draftIds')];
  99. },
  100. filterHeroes: state =>
  101. /**
  102. * Get a filtered array of heroes
  103. *
  104. * vuex : 'game/filterHeroes'
  105. *
  106. * @param {object} filter - the filter to apply on all heroes array
  107. * @param {string} filter.faction - Filter on faction attribute, can be : orcs humans elves meca none all
  108. * @param {string} filter.popularity - Filter on popularity attribute
  109. * can be :
  110. * - none : no filter on popularity
  111. * - with : do not return heroes that are usable only when no popularity rule
  112. * - without : do not return heroes that are usable only when popularity rule
  113. * @param {string} filter.draft - Filter on draft attribute, can be : yes no all
  114. * @param {number} filter.minPower - Filter on power attribute >= minPower
  115. * @param {number} filter.maxPower - Filter on power attribute &lt;= maxPower
  116. * @param {number} filter.minCost - Filter on cost attribute >= minCost
  117. * @param {number} filter.maxCost - Filter on cost attribute &lt;= maxCost
  118. * @param {boolean} filter.byName - Activate filter on Hero's name
  119. * @param {string} filter.name - Filter on Hero's name
  120. * @param {number[]} filter.byId - Filter on Heroes Id(s). Do not filter in Ids if array is empty
  121. * @param state
  122. * @returns {object[]} Filtered array of heroes
  123. */
  124. filter => {
  125. if (!filter.faction) filter.faction = 'all';
  126. if (!filter.popularity) filter.popularity = 'without';
  127. if (!filter.minPower) filter.minPower = 0;
  128. if (!filter.maxPower) filter.maxPower = 8;
  129. if (!filter.minCost) filter.minCost = 0;
  130. if (!filter.maxCost) filter.minCost = 8;
  131. let draftFilter = 'all';
  132. switch (filter.draft) {
  133. case 'yes':
  134. draftFilter = true;
  135. break;
  136. case 'no':
  137. draftFilter = false;
  138. break;
  139. default:
  140. draftFilter = 'all';
  141. break;
  142. }
  143. return state.allHeroes.filter(hero => {
  144. if (filter.faction !== 'all' &amp;&amp; hero.faction !== filter.faction)
  145. return false;
  146. if (
  147. filter.popularity !== 'none' &amp;&amp;
  148. hero.popularity !== 'any' &amp;&amp;
  149. hero.popularity !== filter.popularity
  150. )
  151. return false;
  152. if (draftFilter !== 'all' &amp;&amp; hero.isDraftable !== draftFilter)
  153. return false;
  154. if (hero.power &lt; filter.minPower || hero.power > filter.maxPower)
  155. return false;
  156. if (hero.cost &lt; filter.minCost || hero.cost > filter.maxCost)
  157. return false;
  158. if (
  159. filter.byName === true &amp;&amp;
  160. !hero.name.toLowerCase().includes(filter.name.toLowerCase())
  161. )
  162. return false;
  163. if (
  164. filter.byId &amp;&amp;
  165. filter.byId.length > 0 &amp;&amp;
  166. !filter.byId.includes(hero.id)
  167. ) {
  168. return false;
  169. }
  170. return true;
  171. });
  172. }
  173. };
  174. const mutations = {
  175. SET_HEROES: (state, payload) => {
  176. state.allHeroes = payload;
  177. },
  178. SET_FULL_GAME_STATE: (state, payload) => {
  179. Object.assign(state, payload);
  180. },
  181. INIT_GAME_STATE: state => {
  182. Object.assign(state, initGameState);
  183. },
  184. SET_WAITING_FOR: (state, payload) => {
  185. state['waitingFor/' + payload.color] = payload.value;
  186. }
  187. };
  188. const actions = {
  189. /**
  190. * Sets the array containing all heroes
  191. *
  192. * vuex : 'game/setHeroes'
  193. *
  194. * @param {object[]} payload - the array with all heroes
  195. */
  196. setHeroes: ({ commit }, payload) => {
  197. commit('SET_HEROES', payload);
  198. },
  199. /**
  200. * Update the full game state (main + players)
  201. *
  202. * Will be called by the game master to update the game state
  203. *
  204. * vuex : 'game/update'
  205. *
  206. * @param {object} Context - vuex context
  207. * @param {Function} Context.commit - vuex commit func
  208. * @param {object} payload - the full game state
  209. * @param {object} payload.game - match store state in game.js
  210. * @param {object} payload.bluePlayer - match store state in any-player.js for blue
  211. * @param {object} payload.redPlayer - match store state in any-player.js for red
  212. */
  213. update: ({ commit }, payload) => {
  214. commit('SET_FULL_GAME_STATE', payload.game);
  215. commit('bluePlayer/SET_FULL_PLAYER_STATE', payload.bluePlayer);
  216. commit('redPlayer/SET_FULL_PLAYER_STATE', payload.redPlayer);
  217. },
  218. /**
  219. * Reset the whole game state
  220. *
  221. * vuex : 'game/resetGameState'
  222. *
  223. * Typically to call when game is stopped or user disconnects
  224. *
  225. * @param {object} Context - vuex context
  226. * @param {Function} Context.commit - vuex commit func
  227. */
  228. resetGameState: ({ commit }) => {
  229. commit('INIT_GAME_STATE');
  230. commit('bluePlayer/INIT_PLAYER_STATE');
  231. commit('redPlayer/INIT_PLAYER_STATE');
  232. },
  233. /**
  234. * Submit chosen faction to the game master
  235. *
  236. * vuex : 'game/submitMyFaction'
  237. *
  238. * @param {string} payload - the faction name (lowercase)
  239. */
  240. submitMyFaction: ({ dispatch, getters }, payload) => {
  241. dispatch(getters.my('submitFaction'), payload);
  242. }
  243. };
  244. const bluePlayer = new AnyPlayer();
  245. const redPlayer = new AnyPlayer();
  246. export default {
  247. namespaced: true,
  248. state,
  249. getters,
  250. mutations,
  251. actions,
  252. modules: {
  253. bluePlayer: {
  254. namespaced: true,
  255. ...bluePlayer
  256. },
  257. redPlayer: {
  258. namespaced: true,
  259. ...redPlayer
  260. }
  261. }
  262. };
  263. </code></pre>
  264. </article>
  265. </section>
  266. </div>
  267. <footer>
  268. <img class="logo" src="img/toast-ui.png" style="">
  269. <div class="footer-text">NHN Entertainment. Frontend Development Lab</div>
  270. </footer>
  271. <script>prettyPrint();</script>
  272. <script src="scripts/jquery.min.js"></script>
  273. <script src="scripts/tui-doc.js"></script>
  274. <script src="scripts/linenumber.js"></script>
  275. <script>
  276. var id = '_sub'.replace(/"/g, '_');
  277. var selectedApi = document.getElementById(id); // do not use jquery selector
  278. var $selectedApi = $(selectedApi);
  279. $selectedApi.removeClass('hidden');
  280. $selectedApi.parent().find('.glyphicon').removeClass('glyphicon-plus').addClass('glyphicon-minus');
  281. showLnbApi();
  282. </script>
  283. </body>
  284. </html>