feat(match): Enhance challenge score confirmation and match detail refresh logic

- Implemented logic to notify users when challenge scores are confirmed, prompting a refresh of match details.
- Updated the match controller to use sendToUser for sending challenge response notifications, improving notification handling.
- Added checks to ensure match details are only refreshed for the relevant match, enhancing data accuracy and user experience.
This commit is contained in:
ethanfly 2026-02-10 01:34:09 +08:00
parent e871eed0e8
commit 4719e931b0
2 changed files with 70 additions and 8 deletions

View File

@ -430,8 +430,12 @@ App({
currentPage2 &&
currentPage2.route === "pages/match/challenge-detail/index"
) {
if (typeof currentPage2.loadMatchDetail === "function") {
currentPage2.loadMatchDetail();
// 检查是否是同一个比赛
const matchId = data.data?.matchId;
if (!matchId || currentPage2.data?.matchId == matchId) {
if (typeof currentPage2.loadMatchDetail === "function") {
currentPage2.loadMatchDetail();
}
}
}
break;
@ -439,6 +443,25 @@ App({
// 挑战被拒绝
wx.showToast({ title: "对方已拒绝挑战", icon: "none" });
break;
case "challenge_score_confirmed":
// 挑战赛比分已确认
wx.showToast({ title: "比分已确认", icon: "success" });
// 如果当前在挑战赛详情页面,刷新数据
const pages3 = getCurrentPages();
const currentPage3 = pages3[pages3.length - 1];
if (
currentPage3 &&
currentPage3.route === "pages/match/challenge-detail/index"
) {
// 检查是否是同一个比赛
const matchId = data.data?.matchId;
if (!matchId || currentPage3.data?.matchId == matchId) {
if (typeof currentPage3.loadMatchDetail === "function") {
currentPage3.loadMatchDetail();
}
}
}
break;
case "score_confirm_request":
// 收到比分确认请求
wx.showModal({

View File

@ -2,7 +2,7 @@ const { Match, MatchGame, MatchPlayer, MatchRound, LadderUser, User, Store, sequ
const { MATCH_TYPES, MATCH_STATUS, CONFIRM_STATUS, RANKING_STAGE, MATCH_WEIGHTS, POWER_CALC } = require('../config/constants');
const { generateMatchCode, success, error, getPagination, pageResult, normalizeAvatarForClient } = require('../utils/helper');
const PowerCalculator = require('../services/powerCalculator');
const { sendChallengeNotification, sendScoreConfirmNotification, sendMatchNotification, broadcastToUsers } = require('../websocket');
const { sendChallengeNotification, sendScoreConfirmNotification, sendMatchNotification, broadcastToUsers, sendToUser } = require('../websocket');
const matchAdminController = require('./matchAdminController');
const { Op } = require('sequelize');
@ -518,9 +518,13 @@ class MatchController {
const challengerLadderUser = await LadderUser.findByPk(match.challenger_id);
const challengerUser = await User.findByPk(challengerLadderUser.user_id);
sendChallengeNotification(challengerUser.id, {
// 使用 sendToUser 直接发送挑战响应通知
sendToUser(challengerUser.id, {
type: accept ? 'challenge_accepted' : 'challenge_rejected',
matchId: match.id
data: {
matchId: match.id,
matchCode: match.match_code
}
});
res.json(success(null, accept ? '已接受挑战' : '已拒绝挑战'));
@ -712,6 +716,25 @@ class MatchController {
}, { transaction: t });
await t.commit();
// 通知双方刷新比赛详情(有争议时也需要刷新)
const player1Ladder = await LadderUser.findByPk(game.player1_id);
const player2Ladder = await LadderUser.findByPk(game.player2_id);
const player1User = player1Ladder ? await User.findByPk(player1Ladder.user_id) : null;
const player2User = player2Ladder ? await User.findByPk(player2Ladder.user_id) : null;
const userIds = [player1User?.id, player2User?.id].filter(Boolean);
if (game.match.type === MATCH_TYPES.CHALLENGE && userIds.length > 0) {
broadcastToUsers(userIds, {
type: 'challenge_score_confirmed',
data: {
matchId: game.match.id,
matchCode: game.match.match_code,
gameId: game.id
}
});
}
return res.json(success(null, '已标记为有争议,请重新比赛'));
}
@ -808,11 +831,15 @@ class MatchController {
await t.commit();
// 获取双方用户ID
const winnerUser = await User.findByPk(winner.user_id);
const loserUser = await User.findByPk(loser.user_id);
const userIds = [winnerUser?.id, loserUser?.id].filter(Boolean);
// 排位赛:通知双方刷新当前对局(含新匹配)
if (game.match.type === MATCH_TYPES.RANKING) {
const rankingUserIds = [winner.user_id, loser.user_id].filter(Boolean);
if (rankingUserIds.length > 0) {
broadcastToUsers(rankingUserIds, {
if (userIds.length > 0) {
broadcastToUsers(userIds, {
type: 'ranking_game_updated',
data: {
matchId: game.match.id,
@ -821,6 +848,18 @@ class MatchController {
}
});
}
} else if (game.match.type === MATCH_TYPES.CHALLENGE) {
// 挑战赛:通知双方刷新比赛详情
if (userIds.length > 0) {
broadcastToUsers(userIds, {
type: 'challenge_score_confirmed',
data: {
matchId: game.match.id,
matchCode: game.match.match_code,
gameId: game.id
}
});
}
}
res.json(success({