|
|
@@ -74,6 +74,7 @@ export default function ExamAdmin() {
|
|
|
const {
|
|
|
socketRoom,
|
|
|
answerManagement,
|
|
|
+ currentQuestion,
|
|
|
// calculateCumulativeResults
|
|
|
} = useSocketClient(classroom as string);
|
|
|
|
|
|
@@ -85,57 +86,92 @@ export default function ExamAdmin() {
|
|
|
const [activeTab, setActiveTab] = useState('current');
|
|
|
|
|
|
// 获取当前题目
|
|
|
- const [currentQuestion, setCurrentQuestion] = useState<QuizState | null>(null);
|
|
|
+ // const [currentQuestion, setCurrentQuestion] = useState<QuizState | null>(null);
|
|
|
const [lastMessage, setLastMessage] = useState<ExamSocketRoomMessage | null>(null);
|
|
|
|
|
|
// 初始化socket连接和监听
|
|
|
- useEffect(() => {
|
|
|
- if (!classroom) return;
|
|
|
+ // useEffect(() => {
|
|
|
+ // if (!classroom) return;
|
|
|
|
|
|
- socketRoom.joinRoom(classroom);
|
|
|
+ // socketRoom.joinRoom(classroom);
|
|
|
|
|
|
- const handleMessage = (data: ExamSocketRoomMessage) => {
|
|
|
- setLastMessage(data);
|
|
|
- };
|
|
|
+ // const handleMessage = (data: ExamSocketRoomMessage) => {
|
|
|
+ // setLastMessage(data);
|
|
|
+ // };
|
|
|
+
|
|
|
+ // socketRoom.onRoomMessage(handleMessage);
|
|
|
+
|
|
|
+ // return () => {
|
|
|
+ // socketRoom.leaveRoom(classroom);
|
|
|
+ // socketRoom.onRoomMessage(handleMessage);
|
|
|
+ // };
|
|
|
+ // }, [classroom]);
|
|
|
+ // 加入/离开房间
|
|
|
+ useEffect(() => {
|
|
|
+ if (!classroom) return;
|
|
|
+
|
|
|
+ socketRoom.joinRoom(classroom);
|
|
|
+
|
|
|
+ return () => {
|
|
|
+ socketRoom.leaveRoom(classroom);
|
|
|
+ };
|
|
|
+ }, [classroom, socketRoom.joinRoom, socketRoom.leaveRoom]);
|
|
|
+
|
|
|
+ // // 获取当前题目状态
|
|
|
+ // useEffect(() => {
|
|
|
+ // if (!classroom) return;
|
|
|
|
|
|
- socketRoom.onRoomMessage(handleMessage);
|
|
|
+ // const fetchCurrentQuestion = async () => {
|
|
|
+ // const question = await answerManagement.getCurrentQuestion(classroom);
|
|
|
+ // setCurrentQuestion(question);
|
|
|
+ // };
|
|
|
|
|
|
- return () => {
|
|
|
- socketRoom.leaveRoom(classroom);
|
|
|
- socketRoom.onRoomMessage(handleMessage);
|
|
|
- };
|
|
|
- }, [classroom]);
|
|
|
+ // fetchCurrentQuestion();
|
|
|
+ // }, [classroom, lastMessage]);
|
|
|
|
|
|
- // 获取当前题目状态
|
|
|
+ // 监听答题消息并更新答案
|
|
|
useEffect(() => {
|
|
|
- if (!classroom) return;
|
|
|
+ if (!classroom || !currentDate) return;
|
|
|
|
|
|
- const fetchCurrentQuestion = async () => {
|
|
|
- const question = await answerManagement.getCurrentQuestion(classroom);
|
|
|
- setCurrentQuestion(question);
|
|
|
+ const handleAnswerMessage = async () => {
|
|
|
+ try {
|
|
|
+ const answers = await answerManagement.getAnswers(
|
|
|
+ classroom as string,
|
|
|
+ currentDate
|
|
|
+ );
|
|
|
+
|
|
|
+ const processedAnswers = answers.map(answer => ({
|
|
|
+ ...answer,
|
|
|
+ profitAmount: answer.profitAmount || 0,
|
|
|
+ profitPercent: answer.profitPercent || 0,
|
|
|
+ holdingStock: answer.holdingStock || '0',
|
|
|
+ holdingCash: answer.holdingCash || '0'
|
|
|
+ }));
|
|
|
+
|
|
|
+ setAnswers(processedAnswers);
|
|
|
+ setDailyAnswers(prev => ({
|
|
|
+ ...prev,
|
|
|
+ [currentDate]: processedAnswers
|
|
|
+ }));
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取答案失败:', error);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- fetchCurrentQuestion();
|
|
|
- }, [classroom, lastMessage]);
|
|
|
+ // 初始获取答案
|
|
|
+ handleAnswerMessage();
|
|
|
|
|
|
- // 获取答案
|
|
|
- useEffect(() => {
|
|
|
- if (!classroom || !currentDate) return;
|
|
|
+ // 订阅答题消息
|
|
|
+ if (socketRoom.client) {
|
|
|
+ socketRoom.client.on('exam:answer', handleAnswerMessage);
|
|
|
+ }
|
|
|
|
|
|
- const fetchAnswers = async () => {
|
|
|
- const answers = await answerManagement.getAnswers(
|
|
|
- classroom as string,
|
|
|
- currentDate
|
|
|
- );
|
|
|
- setAnswers(answers);
|
|
|
- setDailyAnswers(prev => ({
|
|
|
- ...prev,
|
|
|
- [currentDate]: answers
|
|
|
- }));
|
|
|
+ return () => {
|
|
|
+ if (socketRoom.client) {
|
|
|
+ socketRoom.client.off('exam:answer', handleAnswerMessage);
|
|
|
+ }
|
|
|
};
|
|
|
-
|
|
|
- fetchAnswers();
|
|
|
- }, [classroom, currentDate, lastMessage]);
|
|
|
+ }, [classroom, currentDate, answerManagement, socketRoom]);
|
|
|
|
|
|
// // 更新答案状态
|
|
|
// useEffect(() => {
|