<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Source: src/store/menu/menu.js | 12 Heroes</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-jsdoc.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/tui-doc.css">

    
</head>
<body>
<nav class="lnb" id="lnb">
    <div class="logo" style="">
        
            <img src="img/toast-ui.png" width="100%" height="100%">
        
    </div>
    <div class="title">
        <h1><a href="index.html" class="link">12 Heroes</a></h1>
        
    </div>
    <div class="search-container" id="search-container">
        <input type="text" placeholder="Search">
        <ul></ul>
    </div>
    
    <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>
</nav>
<div id="resizer"></div>

<div class="main" id="main">
    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>import { socketService } from '../../main';
import { Constants } from 'const/constants';

const state = {
  joinableGames: [],
  /** @type {import('type/comm').CommStatus} */
  loadGamesStatus: { status: Constants.REQ_IDLE, text: 'Not connected' },
  connectionStatus: { status: Constants.REQ_IDLE, text: 'Not connected' },
  createGameStatus: { status: Constants.REQ_IDLE, text: 'Not connected' },
  joinGameStatus: { status: Constants.REQ_IDLE, text: 'Not connected' }
};

const getters = {
  joinableGames(state) {
    return state.joinableGames;
  },
  connectionStatus(state) {
    return state.connectionStatus;
  },
  loadGamesStatus(state) {
    return state.loadGamesStatus;
  },
  createGameStatus(state) {
    return state.createGameStatus;
  },
  joinGameStatus(state) {
    return state.joinGameStatus;
  }
};

const mutations = {
  SET_JOINABLE_GAMES: (state, payload) => {
    state.joinableGames = payload;
  },
  SET_CONNECTION_STATUS: (state, payload) => {
    state.connectionStatus = payload;
  },
  SET_LOAD_GAMES_STATUS: (state, payload) => {
    state.loadGamesStatus = payload;
  },
  SET_CREATE_GAME_STATUS: (state, payload) => {
    state.createGameStatus = payload;
  },
  SET_JOIN_GAME_STATUS: (state, payload) => {
    state.joinGameStatus = payload;
  },
  RESET_ALL_STATUS: state => {
    let status = { status: Constants.REQ_IDLE, text: 'Not connected' };
    state.connectionStatus = status;
    state.loadGamesStatus = status;
    state.createGameStatus = status;
    state.joinGameStatus = status;
  }
};

const actions = {
  fetchJoinableGames: ({ commit, rootState }) => {
    let loadGamesStatus = {
      status: Constants.REQ_REQUESTED,
      text: 'Loading Games'
    };
    commit('SET_LOAD_GAMES_STATUS', loadGamesStatus);
    socketService
      .getGamesList(rootState.username)
      .then(games => {
        loadGamesStatus.status = Constants.REQ_SUCCESS;
        if (games.length === 0) {
          loadGamesStatus.text =
            'No games found, wait for a new or create one !';
        } else {
          loadGamesStatus.text = 'Online games loaded';
        }
        commit('SET_JOINABLE_GAMES', games);
        commit('SET_LOAD_GAMES_STATUS', loadGamesStatus);
      })
      .catch(() => {
        loadGamesStatus = {
          status: Constants.REQ_ERROR,
          text: 'Error reaching server'
        };
        commit('SET_LOAD_GAMES_STATUS', loadGamesStatus);
      });
  },
  connect: ({ commit }, payload) => {
    return new Promise((resolve, reject) => {
      let connectionStatus = {};
      if (payload === '') {
        connectionStatus = {
          status: Constants.REQ_ERROR,
          text: 'Enter a valid username'
        };

        commit('SET_CONNECTION_STATUS', connectionStatus);
        reject();
      } else {
        commit('SET_USERNAME', payload, { root: true });
        connectionStatus = {
          status: Constants.REQ_REQUESTED,
          text: 'Connecting...'
        };
        commit('SET_CONNECTION_STATUS', connectionStatus);
        socketService
          .connect(payload)
          .then(() => {
            connectionStatus = {
              status: Constants.REQ_SUCCESS,
              text: 'Connected as ' + payload
            };
            commit('SET_CONNECTION_STATUS', connectionStatus);
            commit('SET_IS_CONNECTED', true, { root: true });
            localStorage.setItem('username', payload);
            resolve();
          })
          .catch(err => {
            connectionStatus = {
              status: Constants.REQ_ERROR,
              text: 'Could not connect as ' + payload + ' (' + err + ')'
            };
            commit('SET_CONNECTION_STATUS', connectionStatus);
            commit('SET_USERNAME', '', { root: true });
            reject();
          });
      }
    });
  },
  createOnlineGame: ({ commit, rootState }, payload) => {
    let createGameStatus = {
      status: Constants.REQ_REQUESTED,
      text: 'Creating Game'
    };
    let game = {
      player1: rootState.username,
      player2: '',
      deck: payload.deckMode,
      advRules: payload.advRules,
      status: 'CREATED',
      data: '{}'
    };
    commit('SET_CREATE_GAME_STATUS', createGameStatus);

    socketService
      .createGame(game)
      .then(id => {
        createGameStatus.status = Constants.REQ_SUCCESS;
        createGameStatus.text = 'Success creating online game ' + id;
        commit('SET_CREATE_GAME_STATUS', createGameStatus);
        commit('SET_GAME_ID', id, { root: true });
        commit('SET_IS_GAME_RUNNING', true, { root: true });
      })
      .catch(() => {
        createGameStatus = {
          status: Constants.REQ_ERROR,
          text: 'Error reaching server'
        };
        commit('SET_CREATE_GAME_STATUS', createGameStatus);
      });
  },
  joinOnlineGame: ({ commit, rootState }, payload) => {
    let joinGameStatus = {
      status: Constants.REQ_REQUESTED,
      text: 'Joining Game'
    };
    commit('SET_JOIN_GAME_STATUS', joinGameStatus);

    socketService
      .joinGame(rootState.username, payload.id, payload.isNew)
      .then(() => {
        joinGameStatus.status = Constants.REQ_SUCCESS;
        joinGameStatus.text = 'Success joining online game ' + payload.id;
        commit('SET_JOIN_GAME_STATUS', joinGameStatus);
        commit('SET_GAME_ID', payload.id, { root: true });
        commit('SET_IS_GAME_RUNNING', true, { root: true });
        if (!payload.isNew) {
          localStorage.setItem('gameId', payload.id);
        }
      })
      .catch(() => {
        joinGameStatus.status = Constants.REQ_ERROR;
        joinGameStatus.text = 'Error +-reaching server';
        commit('SET_JOIN_GAME_STATUS', joinGameStatus);
      });
  }
};
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
</code></pre>
        </article>
    </section>




</div>

<footer>
    <img class="logo" src="img/toast-ui.png" style="">
    <div class="footer-text">NHN Entertainment. Frontend Development Lab</div>
</footer>
<script>prettyPrint();</script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/tui-doc.js"></script>
<script src="scripts/linenumber.js"></script>

    <script>
        var id = '_sub'.replace(/"/g, '_');
        var selectedApi = document.getElementById(id); // do not use jquery selector
        var $selectedApi = $(selectedApi);

        $selectedApi.removeClass('hidden');
        $selectedApi.parent().find('.glyphicon').removeClass('glyphicon-plus').addClass('glyphicon-minus');
        showLnbApi();
    </script>

</body>
</html>