index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <script setup lang="ts">
  2. import { showSuccessToast } from 'vant'
  3. import GameAPI, { type GameResultVO, type GameVO, type Result, type ResultLevel } from '@/api/game'
  4. const router = useRouter()
  5. const subjectInfo = ref<GameVO>({})
  6. // 控制倒计时的显隐
  7. const showCountDown = ref(true)
  8. // 显示题目文本
  9. const showSpanText = ref('')
  10. // 测试总次数
  11. const totalCount = ref(0)
  12. // 当前题答案
  13. const currentAnswer = ref(0)
  14. // 正确总次数
  15. const rightCount = ref(0)
  16. // 当前轮正确次数
  17. const currentRoundRightCount = ref(0)
  18. // 连续对3次
  19. const rightThree = ref(0)
  20. // 连续错两次
  21. const wrongTwo = ref(0)
  22. // 当前轮需要计算的数字个数
  23. const additionNumCount = ref(2)
  24. // 统计每个层级答对个数
  25. const rightCountList: Result[] = reactive([])
  26. // 测试时长 100 秒
  27. const gameDuration = ref(100 * 1000)
  28. // 游戏结束时间戳
  29. const gameEndTime = ref(0)
  30. // 游戏开始时间戳
  31. const gameStartTime = ref(0)
  32. // 游戏收集的数据
  33. const gameData: GameResultVO = {
  34. finish: '1',
  35. gameId: subjectInfo.value.id,
  36. gameName: subjectInfo.value.name,
  37. userId: sessionStorage.getItem('userId'),
  38. paramList: [],
  39. levelList: [],
  40. }
  41. function nNumRightCount() {
  42. const isIndex = rightCountList.findIndex(item => Number.parseInt(item.name) === additionNumCount.value)
  43. if (isIndex !== -1) {
  44. if (typeof rightCountList[isIndex].value === 'number') {
  45. rightCountList[isIndex].value++
  46. }
  47. }
  48. else {
  49. rightCountList.push({
  50. name: additionNumCount.value.toString(),
  51. value: 1,
  52. })
  53. }
  54. }
  55. function createComputeSpanText() {
  56. // 生成下一道题和答案
  57. const tempSpanText: number[] = []
  58. let additionResult = 0
  59. for (let i = additionNumCount.value; i > 0; i--) {
  60. const tempNum = Math.floor(Math.random() * 10)
  61. additionResult += tempNum
  62. tempSpanText.push(tempNum)
  63. }
  64. showSpanText.value = tempSpanText.join('+')
  65. currentAnswer.value = additionResult % 10
  66. }
  67. function userClick(answer: number) {
  68. // 如果游戏时长大于或等于 100 秒,则游戏结束
  69. gameEndTime.value = performance.now()
  70. const duration = gameEndTime.value - gameStartTime.value
  71. if (duration >= gameDuration.value) {
  72. // 游戏结束,发送数据
  73. sendData()
  74. return
  75. }
  76. // 用户点击动作
  77. totalCount.value++
  78. if (answer === currentAnswer.value) {
  79. // 正确次数
  80. rightCount.value++
  81. // 当前数字个数计算正确数加1
  82. nNumRightCount()
  83. // 本轮正确次数
  84. currentRoundRightCount.value++
  85. rightThree.value++
  86. wrongTwo.value = 0
  87. if (rightThree.value >= 3 && wrongTwo.value === 0) {
  88. additionNumCount.value++
  89. rightThree.value = 0
  90. wrongTwo.value = 0
  91. }
  92. }
  93. else {
  94. // 正式测试时要做降级处理
  95. currentRoundRightCount.value = 0
  96. rightThree.value = 0
  97. wrongTwo.value++
  98. if (additionNumCount.value > 2 && rightThree.value === 0 && wrongTwo.value >= 2) {
  99. additionNumCount.value--
  100. rightThree.value = 0
  101. wrongTwo.value = 0
  102. }
  103. }
  104. // 生成下一道题
  105. createComputeSpanText()
  106. }
  107. /**
  108. * 倒计时结束时的回调
  109. */
  110. function endCountDown() {
  111. // 隐藏倒计时组件
  112. showCountDown.value = false
  113. // 开始生成游戏的第一道题
  114. createComputeSpanText()
  115. // 记录游戏开始时间戳
  116. gameStartTime.value = performance.now()
  117. // 重置游戏结束时间戳
  118. gameEndTime.value = 0
  119. // 重置收集的游戏数据
  120. }
  121. function sendData() {
  122. const totalScore = rightCountList.reduce((acc, curr) => {
  123. acc += curr.value as number * 2 ** (Number.parseInt(curr.name) - 2)
  124. return acc
  125. }, 0)
  126. gameData.levelList = rightCountList.map((item) => {
  127. return {
  128. level: item.name,
  129. levelParamList: [{ ...item }],
  130. } as ResultLevel
  131. })
  132. gameData.paramList = [
  133. {
  134. code: 'totalScore',
  135. name: '总分',
  136. value: totalScore === 0 ? 0 : Number.parseFloat(Math.log2(totalScore).toString()).toFixed(2),
  137. },
  138. {
  139. code: 'totalCount',
  140. name: '题目总数',
  141. value: totalCount.value,
  142. },
  143. {
  144. code: 'rightCount',
  145. name: '总正确数',
  146. value: rightCount.value,
  147. },
  148. ]
  149. GameAPI.add(gameData).then(() => {
  150. showSuccessToast('本次训练已结束')
  151. setTimeout(() => {
  152. router.go(-1)
  153. }, 1300)
  154. })
  155. }
  156. onMounted(() => {
  157. const temp = sessionStorage.getItem('subjectInfo')
  158. if (temp) {
  159. subjectInfo.value = JSON.parse(temp)
  160. }
  161. })
  162. </script>
  163. <template>
  164. <section class="app-container">
  165. <van-nav-bar class="self-nav-bar" :title="subjectInfo.name" left-arrow @click-left="router.go(-1)" />
  166. <count-down v-if="showCountDown" :time="5" color="white" @end-count-down="endCountDown" />
  167. <div v-else style="" class="w-[90%] mx-auto mt-[15px] p-[15px] border-6 border-white border-solid rounded-[8px] bg-[#425363]">
  168. <div class="h-[80px] flex flex-row justify-center items-center bg-[#D2E2F1] rounded-[8px]">
  169. <span class="text-[40px] text-[#222222]">{{ showSpanText }}</span>
  170. </div>
  171. <div class="flex flex-row justify-between mt-[24px]">
  172. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(1)">
  173. <span class="text-[32px] text-[#222222]">1</span>
  174. </div>
  175. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(2)">
  176. <span class="text-[32px] text-[#222222]">2</span>
  177. </div>
  178. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(3)">
  179. <span class="text-[32px] text-[#222222]">3</span>
  180. </div>
  181. </div>
  182. <div class="flex flex-row justify-between mt-[24px]">
  183. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(4)">
  184. <span class="text-[32px] text-[#222222]">4</span>
  185. </div>
  186. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(5)">
  187. <span class="text-[32px] text-[#222222]">5</span>
  188. </div>
  189. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(6)">
  190. <span class="text-[32px] text-[#222222]">6</span>
  191. </div>
  192. </div>
  193. <div class="flex flex-row justify-between mt-[24px]">
  194. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(7)">
  195. <span class="text-[32px] text-[#222222]">7</span>
  196. </div>
  197. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(8)">
  198. <span class="text-[32px] text-[#222222]">8</span>
  199. </div>
  200. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(9)">
  201. <span class="text-[32px] text-[#222222]">9</span>
  202. </div>
  203. </div>
  204. <div class="flex flex-row justify-center mt-[24px]">
  205. <div class="w-[72px] h-[72px] flex flex-row justify-center items-center bg-white rounded-[8px]" @click="userClick(0)">
  206. <span class="text-[32px] text-[#222222]">0</span>
  207. </div>
  208. </div>
  209. </div>
  210. </section>
  211. </template>
  212. <style scoped lang="less">
  213. .app-container {
  214. background-image: url('/static/image/game/bg-continue-addition.png');
  215. background-size: 100% 100%;
  216. background-position: center center;
  217. background-repeat: no-repeat;
  218. :deep(.van-nav-bar) {
  219. &.self-nav-bar {
  220. .van-nav-bar__title {
  221. color: #ffffff;
  222. }
  223. .van-icon {
  224. color: #ffffff;
  225. }
  226. }
  227. }
  228. }
  229. </style>