|
@@ -56,21 +56,6 @@ function getAnswers(client: Socket | null, roomId: string, questionId: string):
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function getCurrentQuestion(client: Socket | null, roomId: string, getAnswersFn: typeof getAnswers): Promise<QuizState | null> {
|
|
|
|
|
- if (!client) return Promise.resolve(null);
|
|
|
|
|
-
|
|
|
|
|
- return getAnswersFn(client, roomId, 'current_state').then(answers => {
|
|
|
|
|
- const currentState = answers[0];
|
|
|
|
|
- if (currentState) {
|
|
|
|
|
- return {
|
|
|
|
|
- date: currentState.date || '',
|
|
|
|
|
- price: currentState.price || '0'
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
- return null;
|
|
|
|
|
- });
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
export function useSocketClient(roomId: string | null) {
|
|
export function useSocketClient(roomId: string | null) {
|
|
|
const { token } = useAuth();
|
|
const { token } = useAuth();
|
|
|
const queryClient = useQueryClient();
|
|
const queryClient = useQueryClient();
|
|
@@ -154,7 +139,6 @@ export function useSocketClient(roomId: string | null) {
|
|
|
if (!client || !roomId) return;
|
|
if (!client || !roomId) return;
|
|
|
|
|
|
|
|
const handleQuestionUpdate = (question:QuizState ) => {
|
|
const handleQuestionUpdate = (question:QuizState ) => {
|
|
|
- // const question = await getCurrentQuestion(client, roomId, getAnswers);
|
|
|
|
|
setCurrentQuestion(question);
|
|
setCurrentQuestion(question);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -216,14 +200,9 @@ export function useSocketClient(roomId: string | null) {
|
|
|
|
|
|
|
|
// 获取该用户的所有历史答案
|
|
// 获取该用户的所有历史答案
|
|
|
const dates = Object.keys(pricesData).sort();
|
|
const dates = Object.keys(pricesData).sort();
|
|
|
- const allAnswers = await Promise.all(
|
|
|
|
|
- dates.map(date => getAnswers(client, roomId, date))
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 计算收益
|
|
|
|
|
- const userAnswers = allAnswers
|
|
|
|
|
- .flat()
|
|
|
|
|
- .filter((a: Answer) => a.userId === userId)
|
|
|
|
|
|
|
+ const allUserAnswers = await getUserAnswers(roomId, userId);
|
|
|
|
|
+ const userAnswers = allUserAnswers
|
|
|
|
|
+ .filter((a: Answer) => dates.includes(a.date || ''))
|
|
|
.map((a: Answer) => ({
|
|
.map((a: Answer) => ({
|
|
|
...a,
|
|
...a,
|
|
|
price: pricesData[a.date || '']?.price || '0'
|
|
price: pricesData[a.date || '']?.price || '0'
|
|
@@ -263,10 +242,14 @@ export function useSocketClient(roomId: string | null) {
|
|
|
}, (success: boolean) => {
|
|
}, (success: boolean) => {
|
|
|
callback?.(success);
|
|
callback?.(success);
|
|
|
|
|
|
|
|
- setUserAnswers([...userAnswers, answerWithProfit])
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 提交成功后获取最新回答记录
|
|
|
|
|
+ if (success && roomId && answerWithProfit.userId) {
|
|
|
|
|
+ setUserAnswers([...userAnswers, answerWithProfit])
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
}, '存储答案失败');
|
|
}, '存储答案失败');
|
|
|
- }, [client, getAnswers]);
|
|
|
|
|
|
|
+ }, [client]);
|
|
|
|
|
|
|
|
// 清理房间数据
|
|
// 清理房间数据
|
|
|
const cleanupRoom = useCallback(async (roomId: string, questionId?: string) => {
|
|
const cleanupRoom = useCallback(async (roomId: string, questionId?: string) => {
|
|
@@ -342,14 +325,6 @@ export function useSocketClient(roomId: string | null) {
|
|
|
}, '获取答案失败');
|
|
}, '获取答案失败');
|
|
|
}, [client]);
|
|
}, [client]);
|
|
|
|
|
|
|
|
- // 获取当前题目 (封装为useCallback)
|
|
|
|
|
- const getCurrentQuestionCallback = useCallback((roomId: string): Promise<QuizState | null> => {
|
|
|
|
|
- if (!client) return Promise.resolve(null);
|
|
|
|
|
- return handleAsyncOperation(async () => {
|
|
|
|
|
- return getCurrentQuestion(client, roomId, getAnswers);
|
|
|
|
|
- }, '获取当前题目状态失败');
|
|
|
|
|
- }, [client]);
|
|
|
|
|
-
|
|
|
|
|
// 清理socket连接
|
|
// 清理socket连接
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
return () => {
|
|
return () => {
|
|
@@ -369,26 +344,42 @@ export function useSocketClient(roomId: string | null) {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 获取用户答案
|
|
// 获取用户答案
|
|
|
- const getUserAnswers = useCallback(async (examId: string, userId: string): Promise<Answer[]> => {
|
|
|
|
|
- if (!client || !examId || !userId) return Promise.resolve([]);
|
|
|
|
|
|
|
+ const getUserAnswers = useCallback(async (roomId: string, userId: string): Promise<Answer[]> => {
|
|
|
|
|
+ if (!client || !roomId || !userId) return Promise.resolve([]);
|
|
|
|
|
|
|
|
return handleAsyncOperation(async () => {
|
|
return handleAsyncOperation(async () => {
|
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
|
- client.emit('exam:getUserAnswers', { examId, userId }, (answers: Answer[]) => {
|
|
|
|
|
|
|
+ client.emit('exam:getUserAnswers', { roomId, userId }, (answers: Answer[]) => {
|
|
|
resolve(answers || []);
|
|
resolve(answers || []);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
}, '获取用户答案失败');
|
|
}, '获取用户答案失败');
|
|
|
}, [client]);
|
|
}, [client]);
|
|
|
|
|
|
|
|
|
|
+ const getCurrentQuestion = useCallback(async (roomId: string): Promise<QuizState> => {
|
|
|
|
|
+ if (!client) return Promise.reject(new Error('Socket not connected'));
|
|
|
|
|
+
|
|
|
|
|
+ return handleAsyncOperation(async () => {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ client.emit('exam:currentQuestion', { roomId }, (question: QuizState) => {
|
|
|
|
|
+ if (!question) {
|
|
|
|
|
+ reject(new Error('No current question available'));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ resolve(question);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ }, '获取当前问题失败');
|
|
|
|
|
+ }, [client])
|
|
|
|
|
+
|
|
|
const answerManagement = {
|
|
const answerManagement = {
|
|
|
storeAnswer,
|
|
storeAnswer,
|
|
|
getAnswers: getAnswersCallback,
|
|
getAnswers: getAnswersCallback,
|
|
|
cleanupRoom,
|
|
cleanupRoom,
|
|
|
sendNextQuestion,
|
|
sendNextQuestion,
|
|
|
- getCurrentQuestion: getCurrentQuestionCallback,
|
|
|
|
|
getPriceHistory,
|
|
getPriceHistory,
|
|
|
- getUserAnswers
|
|
|
|
|
|
|
+ getUserAnswers,
|
|
|
|
|
+ getCurrentQuestion
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 计算累计结果
|
|
// 计算累计结果
|
|
@@ -414,12 +405,11 @@ export function useSocketClient(roomId: string | null) {
|
|
|
answerManagement,
|
|
answerManagement,
|
|
|
// calculateCumulativeResults,
|
|
// calculateCumulativeResults,
|
|
|
currentQuestion,
|
|
currentQuestion,
|
|
|
|
|
+ setCurrentQuestion,
|
|
|
lastMessage,
|
|
lastMessage,
|
|
|
userAnswers,
|
|
userAnswers,
|
|
|
// 兼容旧版导入
|
|
// 兼容旧版导入
|
|
|
...socketRoom,
|
|
...socketRoom,
|
|
|
...answerManagement
|
|
...answerManagement
|
|
|
};
|
|
};
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// 保留原有其他hook实现...
|
|
|
|
|
|
|
+}
|