select.js
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* @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();
// });
},
},
};