import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    messages: [],
    playerName: "",
    inputText: ""
  },

  getters: {
    playerName: (state) => state.playerName,
    messages: (state) => state.messages,
    inputText: (state) => state.inputText
  },

  actions: {
    setPlayerName({ commit }, name) {
      commit("SET_PLAYER_NAME", name);
    },
    addMessage({ commit }, data) {
      commit("ADD_MESSAGE", data);
      // Have to wait for DOM to be updated before scrolling
      Vue.nextTick(() => {
        document.getElementById("output").lastChild.scrollIntoView();
      });
    },
    setInputText({ commit }, text) {
      commit("SET_INPUT_TEXT", text);
    }
  },

  mutations: {
    SET_PLAYER_NAME(state, name) {
      state.playerName = name;
    },
    ADD_MESSAGE(state, data) {
      state.messages.push({
        entity: data.entity,
        message: data.message
      });
    },
    SET_INPUT_TEXT(state, text) {
      state.inputText = text;
    }
  }
});

export default store;

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    messages: [],
    playerName: "",
    inputText: ""
  },

  getters: {
    playerName: (state) => state.playerName,
    messages: (state) => state.messages,
    inputText: (state) => state.inputText
  },

  actions: {
    setPlayerName({ commit }, name) {
      commit("SET_PLAYER_NAME", name);
    },
    addMessage({ commit }, data) {
      commit("ADD_MESSAGE", data);
    },
    enterCommand({ commit, state }, text) {
      commit("ADD_MESSAGE", {
        entity: state.playerName,
        message: text
      });
      commit("SET_INPUT_TEXT", "");
      console.log(`Parsing ${text}`);
    },
    setInputText({ commit }, text) {
      commit("SET_INPUT_TEXT", text);
    }
  },

  mutations: {
    SET_PLAYER_NAME(state, name) {
      state.playerName = name;
    },
    ADD_MESSAGE(state, data) {
      state.messages.push({
        entity: data.entity,
        message: data.message
      });
    },
    SET_INPUT_TEXT(state, text) {
      state.inputText = text;
    }
  }
});

export default store;

Add command action for player's commands and refactor