Files
mini-zmc/pages/result/result.js
2026-07-29 10:50:33 +08:00

99 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const service = require("../../services/game-service");
const { formatCountdown } = require("../../utils/format");
Page({
data: {
code: "",
room: null,
winnerName: "",
winnerCopy: "",
ranking: [],
elapsedText: "00:00",
eventCount: 0,
errorMessage: "",
replaying: false
},
onLoad(options) {
const code = options.code || "";
const room = service.getRoom(code);
if (!room) {
this.setData({ errorMessage: "战报不存在或本地数据已被清理" });
return;
}
const hidersWin = room.winner === "hiders";
const snapshot = room.resultSnapshot;
const sourcePlayers = snapshot
? snapshot.players
: room.players.map((player) => ({
...player,
initialRole: player.initialRole || player.role,
finalRole: player.role,
survivedSeconds: 0
}));
const ranking = [...sourcePlayers]
.sort((a, b) => {
if (b.captures !== a.captures) return b.captures - a.captures;
if (b.survivedSeconds !== a.survivedSeconds) {
return b.survivedSeconds - a.survivedSeconds;
}
if (a.joinedAt !== b.joinedAt) return a.joinedAt - b.joinedAt;
return a.id.localeCompare(b.id);
})
.map((player, index) => ({
...player,
rank: index + 1,
roleText: player.initialRole === "hider" && player.finalRole === "seeker"
? "躲藏者→寻找者"
: player.initialRole === "seeker" ? "寻找者" : "躲藏者",
resultText:
player.captures > 0
? `完成 ${player.captures} 次抓捕`
: player.caughtAt
? "勇敢参与追逃"
: player.initialRole === "hider" ? "坚持到游戏结束" : "持续追踪目标"
}));
this.setData({
code,
room,
errorMessage: "",
winnerName: hidersWin ? "躲藏者胜利" : "寻找者胜利",
winnerCopy: hidersWin
? "有人成功坚持到了倒计时结束。"
: "所有躲藏者都已被找到。",
ranking,
elapsedText: formatCountdown(snapshot ? snapshot.elapsedSeconds : 0),
eventCount: snapshot ? snapshot.eventCount : room.events.length
});
},
replay() {
if (this.data.replaying) return;
this.setData({ replaying: true });
try {
service.resetRoom(this.data.code);
wx.redirectTo({
url: `/pages/lobby/lobby?code=${this.data.code}`
});
} catch (error) {
this.setData({ replaying: false });
wx.showToast({ title: error.message, icon: "none" });
}
},
backHome() {
service.clearCurrentRoom();
wx.reLaunch({ url: "/pages/home/home" });
},
onShareAppMessage() {
if (!this.data.room) {
return { title: "藏猫猫计划", path: "/pages/home/home" };
}
return {
title: `${this.data.room.name}${this.data.winnerName}`,
path: "/pages/home/home"
};
}
});