|
@@ -6,9 +6,8 @@ import { message } from 'antd';
|
|
|
import { useSocketClient } from './hooks/useSocketClient.ts';
|
|
import { useSocketClient } from './hooks/useSocketClient.ts';
|
|
|
import { ClassroomDataAPI } from '../../api/classroom_data.ts';
|
|
import { ClassroomDataAPI } from '../../api/classroom_data.ts';
|
|
|
import { ClassroomStatus } from '../../../share/types_stock.ts';
|
|
import { ClassroomStatus } from '../../../share/types_stock.ts';
|
|
|
-import type { ExamSocketRoomMessage } from './types.ts';
|
|
|
|
|
|
|
+import type { QuizState } from './types.ts';
|
|
|
import type { AnswerRecord, Answer } from './types.ts';
|
|
import type { AnswerRecord, Answer } from './types.ts';
|
|
|
-import type { ClassroomData } from '../../../share/types_stock.ts';
|
|
|
|
|
import { useAuth } from "../../hooks.tsx";
|
|
import { useAuth } from "../../hooks.tsx";
|
|
|
|
|
|
|
|
|
|
|
|
@@ -21,12 +20,9 @@ export default function ExamCard() {
|
|
|
const [searchParams] = useSearchParams();
|
|
const [searchParams] = useSearchParams();
|
|
|
const classroom = searchParams.get('classroom');
|
|
const classroom = searchParams.get('classroom');
|
|
|
const {
|
|
const {
|
|
|
- socketRoom: { joinRoom, leaveRoom },
|
|
|
|
|
|
|
+ socketRoom: { joinRoom, leaveRoom, client },
|
|
|
answerManagement,
|
|
answerManagement,
|
|
|
- currentQuestion,
|
|
|
|
|
- setCurrentQuestion,
|
|
|
|
|
lastMessage,
|
|
lastMessage,
|
|
|
- userAnswers
|
|
|
|
|
} = useSocketClient(classroom as string);
|
|
} = useSocketClient(classroom as string);
|
|
|
const [currentDate, setCurrentDate] = useState('');
|
|
const [currentDate, setCurrentDate] = useState('');
|
|
|
const [currentPrice, setCurrentPrice] = useState('0');
|
|
const [currentPrice, setCurrentPrice] = useState('0');
|
|
@@ -65,12 +61,10 @@ export default function ExamCard() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取当前问题并更新状态
|
|
// 获取当前问题并更新状态
|
|
|
- try {
|
|
|
|
|
- const question = await answerManagement.getCurrentQuestion(classroom);
|
|
|
|
|
- setCurrentQuestion(question);
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error('获取当前问题失败:', error);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const question = await answerManagement.getCurrentQuestion(classroom);
|
|
|
|
|
+ setCurrentDate(question.date);
|
|
|
|
|
+ setCurrentPrice(String(question.price));
|
|
|
|
|
+ setIsStarted(true);
|
|
|
|
|
|
|
|
// 获取用户回答记录
|
|
// 获取用户回答记录
|
|
|
if (user?.id) {
|
|
if (user?.id) {
|
|
@@ -129,26 +123,12 @@ export default function ExamCard() {
|
|
|
}
|
|
}
|
|
|
}, [lastMessage]);
|
|
}, [lastMessage]);
|
|
|
|
|
|
|
|
- useEffect(() => {
|
|
|
|
|
- if (currentQuestion) {
|
|
|
|
|
- console.log('currentQuestion', currentQuestion);
|
|
|
|
|
- setCurrentDate(currentQuestion.date);
|
|
|
|
|
- setCurrentPrice(String(currentQuestion.price));
|
|
|
|
|
- setIsStarted(true);
|
|
|
|
|
- } else {
|
|
|
|
|
- // 如果没有当前问题,重置状态
|
|
|
|
|
- setCurrentDate('');
|
|
|
|
|
- setCurrentPrice('0');
|
|
|
|
|
- setIsStarted(false);
|
|
|
|
|
- }
|
|
|
|
|
- }, [currentQuestion]);
|
|
|
|
|
-
|
|
|
|
|
// 处理选择A(持股)
|
|
// 处理选择A(持股)
|
|
|
const handleChooseA = useCallback(async () => {
|
|
const handleChooseA = useCallback(async () => {
|
|
|
setHoldingStock('1');
|
|
setHoldingStock('1');
|
|
|
setHoldingCash('0');
|
|
setHoldingCash('0');
|
|
|
|
|
|
|
|
- if (classroom && user?.username) {
|
|
|
|
|
|
|
+ if (classroom && user?.username && currentDate) {
|
|
|
const answer = {
|
|
const answer = {
|
|
|
date: currentDate,
|
|
date: currentDate,
|
|
|
holdingStock: '1',
|
|
holdingStock: '1',
|
|
@@ -160,28 +140,33 @@ export default function ExamCard() {
|
|
|
try {
|
|
try {
|
|
|
await answerManagement.storeAnswer(
|
|
await answerManagement.storeAnswer(
|
|
|
classroom as string,
|
|
classroom as string,
|
|
|
- currentQuestion?.id || '',
|
|
|
|
|
- user.username,
|
|
|
|
|
|
|
+ currentDate,
|
|
|
|
|
+ String(user.id),
|
|
|
answer,
|
|
answer,
|
|
|
- (success) => {
|
|
|
|
|
- if (success) {
|
|
|
|
|
- message.success('答案提交成功');
|
|
|
|
|
- } else {
|
|
|
|
|
- message.error('提交答案失败');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ (answers) => {
|
|
|
|
|
+ const records = answers.map((answer: Answer, index: number): AnswerRecord => ({
|
|
|
|
|
+ date: answer.date,
|
|
|
|
|
+ price: String(answer.price || '0'),
|
|
|
|
|
+ holdingStock: answer.holdingStock,
|
|
|
|
|
+ holdingCash: answer.holdingCash,
|
|
|
|
|
+ profitAmount: answer.profitAmount || 0,
|
|
|
|
|
+ profitPercent: answer.profitPercent || 0,
|
|
|
|
|
+ index: index + 1
|
|
|
|
|
+ }));
|
|
|
|
|
+ setAnswerRecords(records);
|
|
|
}
|
|
}
|
|
|
);
|
|
);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
message.error('提交答案失败');
|
|
message.error('提交答案失败');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }, [classroom, user, currentDate, currentPrice, answerManagement, currentQuestion]);
|
|
|
|
|
|
|
+ }, [classroom, user, currentDate, currentPrice, answerManagement]);
|
|
|
|
|
|
|
|
const handleChooseB = useCallback(async () => {
|
|
const handleChooseB = useCallback(async () => {
|
|
|
setHoldingStock('0');
|
|
setHoldingStock('0');
|
|
|
setHoldingCash('1');
|
|
setHoldingCash('1');
|
|
|
|
|
|
|
|
- if (classroom && user?.username) {
|
|
|
|
|
|
|
+ if (classroom && user?.username && currentDate) {
|
|
|
const answer = {
|
|
const answer = {
|
|
|
date: currentDate,
|
|
date: currentDate,
|
|
|
holdingStock: '0',
|
|
holdingStock: '0',
|
|
@@ -193,53 +178,43 @@ export default function ExamCard() {
|
|
|
try {
|
|
try {
|
|
|
await answerManagement.storeAnswer(
|
|
await answerManagement.storeAnswer(
|
|
|
classroom as string,
|
|
classroom as string,
|
|
|
- currentQuestion?.id || '',
|
|
|
|
|
|
|
+ currentDate,
|
|
|
String(user.id),
|
|
String(user.id),
|
|
|
answer,
|
|
answer,
|
|
|
- (success) => {
|
|
|
|
|
- if (success) {
|
|
|
|
|
- message.success('答案提交成功');
|
|
|
|
|
- } else {
|
|
|
|
|
- message.error('提交答案失败');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ (answers) => {
|
|
|
|
|
+ const records = answers.map((answer: Answer, index: number): AnswerRecord => ({
|
|
|
|
|
+ date: answer.date,
|
|
|
|
|
+ price: String(answer.price || '0'),
|
|
|
|
|
+ holdingStock: answer.holdingStock,
|
|
|
|
|
+ holdingCash: answer.holdingCash,
|
|
|
|
|
+ profitAmount: answer.profitAmount || 0,
|
|
|
|
|
+ profitPercent: answer.profitPercent || 0,
|
|
|
|
|
+ index: index + 1
|
|
|
|
|
+ }));
|
|
|
|
|
+ setAnswerRecords(records);
|
|
|
}
|
|
}
|
|
|
);
|
|
);
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
message.error('提交答案失败');
|
|
message.error('提交答案失败');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }, [classroom, user, currentDate, currentPrice, answerManagement, currentQuestion]);
|
|
|
|
|
|
|
+ }, [classroom, user, currentDate, currentPrice, answerManagement]);
|
|
|
|
|
|
|
|
- // // 监听socket消息更新答题记录
|
|
|
|
|
- // useEffect(() => {
|
|
|
|
|
- // if (!lastMessage?.message) return;
|
|
|
|
|
|
|
+ // 监听当前问题变化
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (!client ) return;
|
|
|
|
|
|
|
|
- // const { type } = lastMessage.message;
|
|
|
|
|
- // if (type === 'answer' || type === 'question' || type === 'restart') {
|
|
|
|
|
- // if (classroom && user?.id) {
|
|
|
|
|
- // answerManagement.getUserAnswers(classroom, String(user.id))
|
|
|
|
|
- // .then(answers => {
|
|
|
|
|
- // if (answers && answers.length > 0) {
|
|
|
|
|
- // const lastAnswer = answers[answers.length - 1];
|
|
|
|
|
- // setHoldingStock(lastAnswer.holdingStock);
|
|
|
|
|
- // setHoldingCash(lastAnswer.holdingCash);
|
|
|
|
|
-
|
|
|
|
|
- // const records = answers.map((answer: Answer, index: number): AnswerRecord => ({
|
|
|
|
|
- // date: answer.date,
|
|
|
|
|
- // price: String(answer.price || '0'),
|
|
|
|
|
- // holdingStock: answer.holdingStock,
|
|
|
|
|
- // holdingCash: answer.holdingCash,
|
|
|
|
|
- // profitAmount: answer.profitAmount || 0,
|
|
|
|
|
- // profitPercent: answer.profitPercent || 0,
|
|
|
|
|
- // index: index + 1
|
|
|
|
|
- // }));
|
|
|
|
|
-
|
|
|
|
|
- // setAnswerRecords(records);
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
- // }
|
|
|
|
|
- // }
|
|
|
|
|
- // }, [lastMessage, classroom, user, answerManagement]);
|
|
|
|
|
|
|
+ const handleQuestionUpdate = (question:QuizState ) => {
|
|
|
|
|
+ setCurrentDate(question.date);
|
|
|
|
|
+ setCurrentPrice(String(question.price));
|
|
|
|
|
+ setIsStarted(true);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ client.on('exam:question', handleQuestionUpdate);
|
|
|
|
|
+ return () => {
|
|
|
|
|
+ client.off('exam:question', handleQuestionUpdate);
|
|
|
|
|
+ };
|
|
|
|
|
+ }, [client]);
|
|
|
|
|
|
|
|
if (isLoading || !classroomData) {
|
|
if (isLoading || !classroomData) {
|
|
|
return <div className="flex items-center justify-center min-h-screen">加载中...</div>;
|
|
return <div className="flex items-center justify-center min-h-screen">加载中...</div>;
|