Browse Source

feat(广度训练): 对接数据接口

chenlianwei 6 months ago
parent
commit
79f546c7f4
1 changed files with 66 additions and 7 deletions
  1. 66 7
      src/pages/cognitiveTasks/BreadthTraining/index.vue

+ 66 - 7
src/pages/cognitiveTasks/BreadthTraining/index.vue

@@ -9,7 +9,9 @@
 
 import { shuffle } from 'lodash-es'
 import CountUp from 'vue-countup-v3'
+import { showSuccessToast } from 'vant'
 import BTRandomPentagram from './BTRandomPentagram.vue'
+import GameAPI, { type GameResultVO, type GameVO } from '@/api/game'
 
 const router = useRouter()
 interface IMainData {
@@ -17,9 +19,7 @@ interface IMainData {
   dataList: any[]
 }
 
-const subjectInfo = ref({
-  name: '广度训练',
-})
+const subjectInfo = ref<GameVO>({})
 
 let selectArray: any[] = reactive([])
 const BTRandomPentagramRef = ref()
@@ -38,13 +38,21 @@ const mainData: IMainData = reactive({
 //   return Math.round((currentIndex.value / mainData.dataList.length) * 100)
 // })
 const selectiveIndex = ref(-1) // 选择的索引
-
+// 游戏收集的数据
+const gameData: GameResultVO = {
+  finish: '1',
+  gameId: subjectInfo.value.id,
+  gameName: subjectInfo.value.name,
+  userId: sessionStorage.getItem('userId'),
+  paramList: [],
+  levelList: [],
+}
 /**
  * 生成基础数据
  */
 function generateBaseData() {
   // 生成选项组
-  const selectDataList: number[][] = [
+  const selectDataList = [
     // [3, 4, 5, 6, 7], [3, 4, 5, 6, 7], ...,
     // [4, 5, 6, 7, 8], [4, 5, 6, 7, 8], ...,
     ...Array.from({ length: 10 }).fill([3, 4, 5, 6, 7]),
@@ -57,7 +65,7 @@ function generateBaseData() {
     ...Array.from({ length: 10 }).fill([10, 11, 12, 13, 14]),
   ]
   selectArray = selectDataList.map((item) => {
-    return shuffle(item)
+    return shuffle(item as number[])
   })
 
   // 生成主体数据
@@ -77,7 +85,7 @@ function generateBaseData() {
   mainData.dataList = baseDataList.map((item, index) => {
     return {
       index,
-      ...item,
+      ...item as object,
       ...{ reactionTime: 0, beginTime: 0, endTime: 0, choice: '' },
     }
   })
@@ -102,6 +110,7 @@ function nextClick() {
   }
   else {
     // console.log('游戏结束')
+    sendData()
   }
 }
 
@@ -162,7 +171,57 @@ function exec() {
   changeShow.value = true
 }
 
+function sendData() {
+  const { totalReactionTime, gameTime } = mainData.dataList.reduce(
+    (obj, it) => {
+      obj.gameTime = obj.gameTime + (it.endTime - it.beginTime)
+      obj.totalReactionTime += it.reactionTime
+      return obj
+    },
+    { gameTime: 0, totalReactionTime: 0 },
+  )
+
+  gameData.paramList = [
+    {
+      code: 'score',
+      name: '得分',
+      value: mainData.totalScore,
+    },
+    {
+      code: 'gameTime',
+      name: '游戏用时',
+      value: `${(gameTime / 1000).toFixed(2)}s`,
+    },
+    {
+      code: 'avrTime',
+      name: '平均反应时长',
+      value: `${Math.ceil(totalReactionTime / mainData.dataList.length)}ms`,
+    },
+    {
+      code: 'attentionBreadthPath',
+      name: '注意广度曲线图',
+      value: JSON.stringify(mainData.dataList),
+    },
+    {
+      code: 'attentionBreadth',
+      name: '注意广度',
+      value: JSON.stringify(mainData.dataList),
+    },
+  ]
+
+  GameAPI.add(gameData).then(() => {
+    showSuccessToast('本次训练已结束')
+    setTimeout(() => {
+      router.go(-1)
+    }, 1300)
+  })
+}
+
 onMounted(() => {
+  const temp = sessionStorage.getItem('subjectInfo')
+  if (temp) {
+    subjectInfo.value = JSON.parse(temp)
+  }
   showCountDown.value = true
   generateBaseData()
   mainData.dataList[currentIndex.value].beginTime = Date.now()