Bladeren bron

exam:getAnswers转换为前端需要的格式

yourname 8 maanden geleden
bovenliggende
commit
f576ce3ed9
1 gewijzigde bestanden met toevoegingen van 33 en 19 verwijderingen
  1. 33 19
      server/routes_io_exam.ts

+ 33 - 19
server/routes_io_exam.ts

@@ -150,8 +150,14 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
       //     timestamp: new Date().toISOString()
       //   }
       // });
+      // 存储价格到Redis
+      const priceKey = `exam:${roomId}:prices`;
+      await apiClient.redis.hset(priceKey, question.date, String(question.price));
+
+      // console.log(`用户 ${user.username} 存储房间 ${roomId} 的价格历史: ${date} - ${price}`);
       const quizState:QuizState = {
-        id: `question-${Date.now()}`,
+        // id: `question-${Date.now()}`,
+        id: question.date, 
         date: question.date, 
         price: question.price
       }
@@ -198,14 +204,14 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
         username: user.username
       });
 
-      // 通知管理员有新答案提交
-      socket.to(`admin-${roomId}`).emit('exam:newAnswer', {
-        roomId,
-        questionId,
-        userId: user.id,
-        username: user.username,
-        timestamp: new Date().toISOString()
-      });
+      // // 通知管理员有新答案提交
+      // socket.to(`admin-${roomId}`).emit('exam:newAnswer', {
+      //   roomId,
+      //   questionId,
+      //   userId: user.id,
+      //   username: user.username,
+      //   timestamp: new Date().toISOString()
+      // });
 
       console.log(`用户 ${user.username} 在房间 ${roomId} 存储答案`);
       callback(true);
@@ -232,15 +238,20 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
 
       // 从Redis获取答案
       const answerKey = `exam:${roomId}:${questionId}:answers`;
-      const answers = await apiClient.redis.hgetall(answerKey);
+      const answersMap = await apiClient.redis.hgetall(answerKey);
+      const formattedAnswers:Answer[] = [];
 
       // 转换为前端需要的格式
-      const formattedAnswers: Answer[] = Object.entries(answers).map(([userId, answerStr]) => {
-        const answer = JSON.parse(answerStr);
-        return {
-          ...answer,
-          userId
-        };
+      Object.entries(answersMap).forEach(([userId, answerStr]) => {
+        try {
+          const answer = JSON.parse(answerStr);
+          formattedAnswers.push({
+            ...answer,
+            userId
+          });
+        } catch (error) {
+          console.log('answerStr转换失败:', error);
+        }
       });
 
       callback(formattedAnswers);
@@ -344,7 +355,7 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
   // 获取所有价格历史
   socket.on('exam:getPrices', async (data: {
     roomId: string;
-  }, callback: (prices: Record<string, string>) => void) => {
+  }, callback: (prices: Record<string, number>) => void) => {
     try {
       const { roomId } = data;
       const user = socket.user;
@@ -357,8 +368,11 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
       // 从Redis获取所有价格
       const priceKey = `exam:${roomId}:prices`;
       const priceMap = await apiClient.redis.hgetall(priceKey);
-
-      callback(priceMap);
+      const entries = Object.entries(priceMap);
+      const convertedEntries = entries.map(([date, price]) => [date, Number(price)]);
+      const convertedMap = Object.fromEntries(convertedEntries);
+      
+      callback(convertedMap);
     } catch (error) {
       console.error('获取所有价格历史失败:', error);
       socket.emit('error', '获取所有价格历史失败');