index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // 测试时长 300 秒
  27. const gameDuration = ref(300)
  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. // 如果游戏时长大于或等于 300 秒,则游戏结束
  69. gameEndTime.value = performance.now()
  70. const duration = gameEndTime.value - gameStartTime.value
  71. if (duration >= gameDuration.value * 1000) {
  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.gameId = subjectInfo.value.id
  127. gameData.gameName = subjectInfo.value.name
  128. gameData.levelList = rightCountList.map((item) => {
  129. return {
  130. level: item.name,
  131. levelParamList: [{ ...item }],
  132. } as ResultLevel
  133. })
  134. gameData.paramList = [
  135. {
  136. code: 'totalScore',
  137. name: '总分',
  138. value: totalScore === 0 ? 0 : Number.parseFloat(Math.log2(totalScore).toString()).toFixed(2),
  139. },
  140. {
  141. code: 'totalCount',
  142. name: '题目总数',
  143. value: totalCount.value,
  144. },
  145. {
  146. code: 'rightCount',
  147. name: '总正确数',
  148. value: rightCount.value,
  149. },
  150. ]
  151. GameAPI.add(gameData).then(() => {
  152. showSuccessToast('本次训练已结束')
  153. setTimeout(() => {
  154. const userAgent = navigator.userAgent.toLowerCase()
  155. const isWechatEnvironment = userAgent.includes('micromessenger')
  156. if (isWechatEnvironment) {
  157. // 执行微信环境下的逻辑,如调用微信JS-SDK
  158. if (wx && wx.miniProgram) {
  159. wx.miniProgram.navigateTo({
  160. url: '/pages/media/pages/feedbackTask/finish/finish',
  161. })
  162. }
  163. }
  164. else {
  165. router.go(-1)
  166. // 执行浏览器环境下的逻辑
  167. }
  168. }, 1300)
  169. })
  170. }
  171. onMounted(() => {
  172. const temp = sessionStorage.getItem('subjectInfo')
  173. if (temp) {
  174. subjectInfo.value = JSON.parse(temp)
  175. }
  176. })
  177. </script>
  178. <template>
  179. <section class="app-container">
  180. <van-nav-bar class="self-nav-bar" :title="subjectInfo.name" left-arrow @click-left="router.go(-1)" />
  181. <count-down v-if="showCountDown" :time="5" color="white" @end-count-down="endCountDown" />
  182. <div v-else style="" class="mx-auto mt-[15px] w-[90%] border-6 border-white rounded-[8px] border-solid bg-[#425363] p-[15px]">
  183. <div class="h-[80px] flex flex-row items-center justify-center rounded-[8px] bg-[#D2E2F1]">
  184. <span
  185. class="text-[#222222]"
  186. :class="{
  187. 'text-[40px]': showSpanText.length <= 11,
  188. 'text-[36px]': showSpanText.length === 12,
  189. 'text-[34px]': showSpanText.length === 13,
  190. 'text-[32px]': [14, 15].includes(showSpanText.length),
  191. 'text-[30px]': [16, 17].includes(showSpanText.length),
  192. 'text-[27px]': [18, 19].includes(showSpanText.length),
  193. 'text-[24px]': [20, 21].includes(showSpanText.length),
  194. 'text-[21px]': [22, 23].includes(showSpanText.length),
  195. 'text-[19px]': [24, 25].includes(showSpanText.length),
  196. }"
  197. >
  198. {{ showSpanText }}
  199. </span>
  200. </div>
  201. <div class="mt-[24px] flex flex-row justify-between">
  202. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(1)">
  203. <span class="text-[32px] text-[#222222]">1</span>
  204. </div>
  205. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(2)">
  206. <span class="text-[32px] text-[#222222]">2</span>
  207. </div>
  208. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(3)">
  209. <span class="text-[32px] text-[#222222]">3</span>
  210. </div>
  211. </div>
  212. <div class="mt-[24px] flex flex-row justify-between">
  213. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(4)">
  214. <span class="text-[32px] text-[#222222]">4</span>
  215. </div>
  216. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(5)">
  217. <span class="text-[32px] text-[#222222]">5</span>
  218. </div>
  219. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(6)">
  220. <span class="text-[32px] text-[#222222]">6</span>
  221. </div>
  222. </div>
  223. <div class="mt-[24px] flex flex-row justify-between">
  224. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(7)">
  225. <span class="text-[32px] text-[#222222]">7</span>
  226. </div>
  227. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(8)">
  228. <span class="text-[32px] text-[#222222]">8</span>
  229. </div>
  230. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(9)">
  231. <span class="text-[32px] text-[#222222]">9</span>
  232. </div>
  233. </div>
  234. <div class="mt-[24px] flex flex-row justify-center">
  235. <div class="h-[72px] w-[72px] flex flex-row items-center justify-center rounded-[8px] bg-white" @click="userClick(0)">
  236. <span class="text-[32px] text-[#222222]">0</span>
  237. </div>
  238. </div>
  239. </div>
  240. <div class="absolute bottom-[16px] left-[50%] h-[45px] w-[200px] translate-x-[-50%]">
  241. <count-down v-if="!showCountDown" text="" is-format :time="gameDuration" :size="32" color="white" @end-count-down="userClick(0)" />
  242. </div>
  243. </section>
  244. </template>
  245. <style scoped lang="scss">
  246. .app-container {
  247. background-image: url('/static/image/game/bg-continue-addition.png');
  248. background-size: 100% 100%;
  249. background-position: center center;
  250. background-repeat: no-repeat;
  251. :deep(.van-nav-bar) {
  252. &.self-nav-bar {
  253. .van-nav-bar__title {
  254. color: #ffffff;
  255. }
  256. .van-icon {
  257. color: #ffffff;
  258. }
  259. }
  260. }
  261. }
  262. </style>