|
|
@@ -172,23 +172,19 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 存储答案
|
|
|
- await apiClient.database.table('exam_answers').insert({
|
|
|
- room_id: roomId,
|
|
|
- question_id: questionId,
|
|
|
- user_id: user.id,
|
|
|
+ // 存储答案到Redis
|
|
|
+ const answerKey = `exam:${roomId}:${questionId}:answers`;
|
|
|
+ await apiClient.redis.hset(answerKey, String(user.id), JSON.stringify({
|
|
|
username: user.username,
|
|
|
date: answer.date,
|
|
|
price: answer.price,
|
|
|
- holding_stock: answer.holdingStock,
|
|
|
- holding_cash: answer.holdingCash,
|
|
|
- profit_amount: answer.profitAmount,
|
|
|
- profit_percent: answer.profitPercent,
|
|
|
- total_profit_amount: answer.totalProfitAmount,
|
|
|
- total_profit_percent: answer.totalProfitPercent,
|
|
|
- created_at: apiClient.database.fn.now(),
|
|
|
- updated_at: apiClient.database.fn.now()
|
|
|
- });
|
|
|
+ holdingStock: answer.holdingStock,
|
|
|
+ holdingCash: answer.holdingCash,
|
|
|
+ profitAmount: answer.profitAmount,
|
|
|
+ profitPercent: answer.profitPercent,
|
|
|
+ totalProfitAmount: answer.totalProfitAmount,
|
|
|
+ totalProfitPercent: answer.totalProfitPercent
|
|
|
+ }));
|
|
|
|
|
|
// 广播答案更新到房间
|
|
|
socket.to(roomId).emit('exam:answerUpdated', {
|
|
|
@@ -219,24 +215,18 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 查询答案
|
|
|
- const answers = await apiClient.database.table('exam_answers')
|
|
|
- .where('room_id', roomId)
|
|
|
- .where('question_id', questionId)
|
|
|
- .select('*');
|
|
|
+ // 从Redis获取答案
|
|
|
+ const answerKey = `exam:${roomId}:${questionId}:answers`;
|
|
|
+ const answers = await apiClient.redis.hgetall(answerKey);
|
|
|
|
|
|
// 转换为前端需要的格式
|
|
|
- const formattedAnswers: Answer[] = answers.map((a: any) => ({
|
|
|
- date: a.date,
|
|
|
- price: a.price,
|
|
|
- holdingStock: a.holding_stock,
|
|
|
- holdingCash: a.holding_cash,
|
|
|
- profitAmount: a.profit_amount,
|
|
|
- profitPercent: a.profit_percent,
|
|
|
- totalProfitAmount: a.total_profit_amount,
|
|
|
- totalProfitPercent: a.total_profit_percent,
|
|
|
- userId: a.user_id
|
|
|
- }));
|
|
|
+ const formattedAnswers: Answer[] = Object.entries(answers).map(([userId, answerStr]) => {
|
|
|
+ const answer = JSON.parse(answerStr);
|
|
|
+ return {
|
|
|
+ ...answer,
|
|
|
+ userId
|
|
|
+ };
|
|
|
+ });
|
|
|
|
|
|
callback(formattedAnswers);
|
|
|
} catch (error) {
|
|
|
@@ -262,23 +252,17 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
|
|
|
if (questionId) {
|
|
|
// 清理特定问题的数据
|
|
|
- await apiClient.database.table('exam_answers')
|
|
|
- .where('room_id', roomId)
|
|
|
- .where('question_id', questionId)
|
|
|
- .delete();
|
|
|
+ const answerKey = `exam:${roomId}:${questionId}:answers`;
|
|
|
+ const keys = await apiClient.redis.keys(answerKey);
|
|
|
+ if (keys.length > 0) {
|
|
|
+ await Promise.all(keys.map((key) => apiClient.redis.del(key)))
|
|
|
+ }
|
|
|
} else {
|
|
|
// 清理整个房间的数据
|
|
|
- await apiClient.database.table('exam_answers')
|
|
|
- .where('room_id', roomId)
|
|
|
- .delete();
|
|
|
-
|
|
|
- await apiClient.database.table('exam_questions')
|
|
|
- .where('room_id', roomId)
|
|
|
- .delete();
|
|
|
-
|
|
|
- await apiClient.database.table('exam_prices')
|
|
|
- .where('room_id', roomId)
|
|
|
- .delete();
|
|
|
+ const keys = await apiClient.redis.keys(`exam:${roomId}:*`);
|
|
|
+ if (keys.length > 0) {
|
|
|
+ await Promise.all(keys.map((key) => apiClient.redis.del(key)))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
socket.emit('exam:cleaned', {
|
|
|
@@ -306,31 +290,9 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 检查是否已存在该日期的价格
|
|
|
- const existing = await apiClient.database.table('exam_prices')
|
|
|
- .where('room_id', roomId)
|
|
|
- .where('date', date)
|
|
|
- .first();
|
|
|
-
|
|
|
- if (existing) {
|
|
|
- // 更新现有价格
|
|
|
- await apiClient.database.table('exam_prices')
|
|
|
- .where('room_id', roomId)
|
|
|
- .where('date', date)
|
|
|
- .update({
|
|
|
- price,
|
|
|
- updated_at: apiClient.database.fn.now()
|
|
|
- });
|
|
|
- } else {
|
|
|
- // 插入新价格
|
|
|
- await apiClient.database.table('exam_prices').insert({
|
|
|
- room_id: roomId,
|
|
|
- date,
|
|
|
- price,
|
|
|
- created_at: apiClient.database.fn.now(),
|
|
|
- updated_at: apiClient.database.fn.now()
|
|
|
- });
|
|
|
- }
|
|
|
+ // 存储价格到Redis
|
|
|
+ const priceKey = `exam:${roomId}:prices`;
|
|
|
+ await apiClient.redis.hset(priceKey, date, price);
|
|
|
|
|
|
console.log(`用户 ${user.username} 存储房间 ${roomId} 的价格历史: ${date} - ${price}`);
|
|
|
} catch (error) {
|
|
|
@@ -353,14 +315,10 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 查询价格
|
|
|
- const priceData = await apiClient.database.table('exam_prices')
|
|
|
- .where('room_id', roomId)
|
|
|
- .where('date', date)
|
|
|
- .select('price')
|
|
|
- .first();
|
|
|
-
|
|
|
- callback(priceData?.price || '0');
|
|
|
+ // 从Redis获取价格
|
|
|
+ const priceKey = `exam:${roomId}:prices`;
|
|
|
+ const price = await apiClient.redis.hget(priceKey, date);
|
|
|
+ callback(price || '0');
|
|
|
} catch (error) {
|
|
|
console.error('获取历史价格失败:', error);
|
|
|
socket.emit('error', '获取历史价格失败');
|
|
|
@@ -381,16 +339,9 @@ export function setupExamEvents({ socket, apiClient }: Variables) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 查询所有价格
|
|
|
- const prices = await apiClient.database.table('exam_prices')
|
|
|
- .where('room_id', roomId)
|
|
|
- .select('date', 'price');
|
|
|
-
|
|
|
- // 转换为日期-价格的映射
|
|
|
- const priceMap: Record<string, string> = {};
|
|
|
- prices.forEach((p: any) => {
|
|
|
- priceMap[p.date] = p.price;
|
|
|
- });
|
|
|
+ // 从Redis获取所有价格
|
|
|
+ const priceKey = `exam:${roomId}:prices`;
|
|
|
+ const priceMap = await apiClient.redis.hgetall(priceKey);
|
|
|
|
|
|
callback(priceMap);
|
|
|
} catch (error) {
|