select.js 3.32 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-01-31 15:41:18
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2023-02-16 14:58:43
 * @FilePath: \h5Demo\src\mixins\select.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import analysis from "./analysis";
export default {
  mixins: [analysis],
  data() {
    return {
      selectResult: "",
      selectIndex: -1,
      isFinish: false, //是否完成
    };
  },
  watch: {
    // 监听服务器返回的数据变化
    dataObject(data) {
      // 答题动作
      GPJSSDK.Game.knowAction({
        data,
        // 是游戏题目消息,并且题库数大于0
        getQuestion: () => {
          // 清除上次的选项
          this.selectResult = "";
          this.selectIndex = -1;
          // 开始题目倒计时
          clearInterval(this.timer);
          this.duration = 0;
          // ios端音频不能自动播放,ios端时默认不播放
          this.playState = GPJSSDK.Env.os == "ios" ? false : true;
          if (this.isFinish || this.isEnd) {
            return;
          }
          Object.assign(this.answerData, data);
          // 答题时间倒计时
          this.timer = setInterval(() => {
            this.answerData.body.timeLimit--;
            if (this.answerData.body.timeLimit <= 0)
              return clearInterval(this.timer);
          }, 1000);
        },
        // 游戏答案消息
        getAnswer: (res) => {
          if (this.selectIndex == -1) {
            this.selectResult = res.answer || data.body.answer;
            this.selectIndex = this.answerData.body.choice.indexOf(
              this.selectResult
            );
          }
        },
        // 答题消息,并更新答对的人数
        answerInfo: (res, isMe) => {
          this.answerData.body.rightNum = res.rightNum;
          if (!isMe) return;
          this.selectResult = res.answer || data.body.answer;
          this.selectIndex = this.answerData.body.choice.indexOf(
            this.selectResult
          );
        },
      });
    },
  },
  methods: {
    reStart() {
      this.isEnd = false;
      this.isFinish = false;
      this.$emit("hanldStart");
    },
    // 选择答案
    optionsHandle(item, index, time) {
      if (this.selectResult) {
        return;
      }
      this.selectResult = item;
      this.selectIndex = index;
      let correctNum = this.$store.state.correctNum;
      if (this.selectResult == this.answerData.body.answer) {
        correctNum++;
        this.$store.commit("setCorrectNum", correctNum);
      }
      GPJSSDK.Game.answerGame(
        this.selectResult,
        this.answerData.body.currentSize
      );
      setTimeout(() => {
        GPJSSDK.Game.nextQuestion();
      }, (time - 1) * 1000);
      if (this.finishNum == correctNum) {
        this.isFinish = true;
        GPJSSDK.Game.endGame();
        clearInterval(this.timer);
      }
      // GPJSSDK.Game.nextQuestion();

      // this.$nextTick(() => {
      //   const cloneEl = document.querySelector(".popup").cloneNode(true);
      //   console.log(cloneEl);
      //   const pageEl = document.querySelector(".mask");
      //   pageEl.appendChild(cloneEl);
      //   GPJSSDK.Game.endGame();
      // });
    },
  },
};