<template>
  <div id="app">
    <game-nav></game-nav>
    <div class="game-wrap">
      <game-main></game-main>
    </div>
    <game-table></game-table>
    <mod-footer></mod-footer>
  </div>
</template>

<script>
import gameNav from "@/components/nav/gameNav";
import gameMain from "@/components/game/gameMain";
import GameTable from '@/components/game/table'
import modFooter from "@/components/footer/modFooter";
import Eos from "eosjs";
import ScatterJS from 'scatterjs-core'
import ScatterEOS from 'scatterjs-plugin-eosjs'
import { showError } from "./util/util.js";
import { mapActions } from "vuex";
ScatterJS.plugins(new ScatterEOS())

export default {
  name: "App",
  data() {
    return {
      connectTimes: 0
    };
  },
  components: {
    gameNav,
    gameMain,
    GameTable,
    modFooter
  },
  methods: {
    ...mapActions(["setScatter", "setAccount", "doScatterLogin"]),
    connect() {
      // 连接scatter
      const connectionOptions = { initTimeout: 20000 };
      ScatterJS.scatter
        .connect("EOSGET_BACCRAT", connectionOptions)
        .then(connected => {
          if (connected) {
            // 设置scatter
            this.setScatter(ScatterJS.scatter);

            window.__MeeChat && window.__MeeChat.setScatterJS(ScatterJS);
            window.__MeeChat && window.__MeeChat.setEos(Eos);

            // 清空全局scatter引用
            window.ScatterJS = null;

            // 调起scatter授权登录
            this.doScatterLogin();
          } else {
            this.connectTimes++;
            if (this.connectTimes <= 3) {
              setTimeout(() => {
                this.connect();
              }, 1000);
            } else {
              // 用户scatter未解锁
              showError(this.$t("installScatter"), "Scatter");

              this.setAccount("NO_LOGINING");
            }
          }
        });
    }
  },
  async created() {
    this.connect();
  },

  mounted() {
    document.title = this.$t("pageTitle");
  }
};
</script>

<style lang="scss">
@import "./style/global.scss";
</style>
