123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import AnyPlayer from './player/any-player';
- import allHeroesJson from '../../../server/src/client-server-shared/all-heroes.json';
- const initGameState = {
- gameState: '',
- allHeroes: [],
- deckMode: '',
- advRules: [],
- 'waitingFor/blue': false,
- 'waitingFor/red': false,
- currentPlayer: '',
- 'battleTiles/left': [],
- 'battleTiles/center': [],
- 'battleTiles/right': [],
- totalFood: 0,
- allHeroesJson
- };
- const state = Object.assign({}, initGameState);
- const getters = {
- state(state) {
- return state.gameState;
- },
- allHeroes(state) {
- return state.allHeroes;
- },
- deckMode(state) {
- return state.deckMode;
- },
- myColor(state, getters, rootState) {
- let color = 'blue';
- if (rootState.username === state.redPlayer.name) {
- color = 'red';
- }
- return color;
- },
- ennemyColor(state, getters) {
- let color = 'red';
- if (getters.myColor === 'red') {
- color = 'blue';
- }
- return color;
- },
- isItMyTurn(state, getters) {
- return state['waitingFor/' + getters.myColor];
- },
- isPopularityRule(state) {
- return state.advRules.includes('popularity');
- },
- isDiscardRule(state) {
- return state.advRules.includes('discard');
- },
- heroById: state => id => {
- return state.allHeroes.find(hero => hero.id === id);
- },
- myTwelveHeroes(state, getters) {
- let myColor = getters.myColor;
- let myHeroesIds = getters[myColor + 'Player/twelveHeroes'].map(
- hero => hero.id
- );
- return state.allHeroes.filter(hero => {
- return myHeroesIds.includes(hero.id);
- });
- },
- myDraftIds(state, getters) {
- let myColor = getters.myColor;
- return getters[myColor + 'Player/draftIds'];
- },
- filterHeroes: state => filter => {
- // filter has
- // - faction (orcs humans elves meca none all)
- // - popularity (with without)
- // - draft (yes no all)
- // - minPower <= power <= maxPower
- // - minCost <= cost <= maxCost
- // - byName : if true filter name with filter.name
- // - byId : [] of ids, deactivated if null or empty.
- if (!filter.faction) filter.faction = 'all';
- if (!filter.popularity) filter.popularity = 'without';
- if (!filter.minPower) filter.minPower = 0;
- if (!filter.maxPower) filter.maxPower = 0;
- if (!filter.minCost) filter.minCost = 0;
- if (!filter.maxCost) filter.minCost = 0;
- let draftFilter = 'all';
- switch (filter.draft) {
- case 'yes':
- draftFilter = true;
- break;
- case 'no':
- draftFilter = false;
- break;
- default:
- draftFilter = 'all';
- break;
- }
- return state.allHeroes.filter(hero => {
- if (filter.faction !== 'all' && hero.faction !== filter.faction)
- return false;
- if (
- filter.popularity !== 'none' &&
- hero.popularity !== 'any' &&
- hero.popularity !== filter.popularity
- )
- return false;
- if (draftFilter !== 'all' && hero.isDraftable !== draftFilter)
- return false;
- if (hero.power < filter.minPower || hero.power > filter.maxPower)
- return false;
- if (hero.cost < filter.minCost || hero.cost > filter.maxCost)
- return false;
- if (
- filter.byName === true &&
- !hero.name.toLowerCase().includes(filter.name.toLowerCase())
- )
- return false;
- if (
- filter.byId &&
- filter.byId.length > 0 &&
- !filter.byId.includes(hero.id)
- ) {
- return false;
- }
- return true;
- });
- }
- };
- const mutations = {
- SET_HEROES: (state, payload) => {
- state.allHeroes = payload;
- },
- SET_FULL_GAME_STATE: (state, payload) => {
- Object.assign(state, payload);
- },
- INIT_GAME_STATE: state => {
- Object.assign(state, initGameState);
- },
- SET_WAITING_FOR: (state, payload) => {
- state['waitingFor/' + payload.color] = payload.value;
- }
- };
- const actions = {
- setHeroes: ({ commit }, payload) => {
- commit('SET_HEROES', payload);
- },
- update: ({ commit }, payload) => {
- commit('SET_FULL_GAME_STATE', payload.game);
- commit('bluePlayer/SET_FULL_PLAYER_STATE', payload.bluePlayer);
- commit('redPlayer/SET_FULL_PLAYER_STATE', payload.redPlayer);
- },
- resetGameState: ({ commit }) => {
- commit('INIT_GAME_STATE');
- commit('bluePlayer/INIT_PLAYER_STATE');
- commit('redPlayer/INIT_PLAYER_STATE');
- }
- };
- const bluePlayer = new AnyPlayer();
- const redPlayer = new AnyPlayer();
- export default {
- namespaced: true,
- state,
- getters,
- mutations,
- actions,
- modules: {
- bluePlayer: {
- namespaced: true,
- ...bluePlayer
- },
- redPlayer: {
- namespaced: true,
- ...redPlayer
- }
- }
- };
|