2025-08-13 16:44:25 +08:00
|
|
|
<script setup>
|
2025-08-25 13:47:32 +08:00
|
|
|
import { ref, onMounted, onBeforeUnmount, nextTick } from "vue";
|
2025-08-13 16:44:25 +08:00
|
|
|
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
|
|
|
|
import Container from "@/components/Container.vue";
|
|
|
|
|
import BattleHeader from "@/components/BattleHeader.vue";
|
|
|
|
|
import BowTarget from "@/components/BowTarget.vue";
|
|
|
|
|
import PlayersRow from "@/components/PlayersRow.vue";
|
|
|
|
|
import BattleFooter from "@/components/BattleFooter.vue";
|
|
|
|
|
import ScreenHint from "@/components/ScreenHint.vue";
|
|
|
|
|
import SButton from "@/components/SButton.vue";
|
|
|
|
|
import RoundEndTip from "@/components/RoundEndTip.vue";
|
|
|
|
|
import TestDistance from "@/components/TestDistance.vue";
|
|
|
|
|
import TeamAvatars from "@/components/TeamAvatars.vue";
|
|
|
|
|
import ShootProgress2 from "@/components/ShootProgress2.vue";
|
2025-10-30 09:09:03 +08:00
|
|
|
import { getCurrentGameAPI, laserCloseAPI } from "@/apis";
|
2025-08-13 16:44:25 +08:00
|
|
|
import { isGameEnded } from "@/util";
|
|
|
|
|
import { MESSAGETYPES, roundsName } from "@/constants";
|
2025-09-18 09:28:14 +08:00
|
|
|
import audioManager from "@/audioManager";
|
2025-08-13 16:44:25 +08:00
|
|
|
import useStore from "@/store";
|
|
|
|
|
import { storeToRefs } from "pinia";
|
|
|
|
|
const store = useStore();
|
|
|
|
|
const { user } = storeToRefs(store);
|
2025-08-14 10:50:44 +08:00
|
|
|
const start = ref(false);
|
|
|
|
|
const tips = ref("");
|
2025-08-13 16:44:25 +08:00
|
|
|
const battleId = ref("");
|
|
|
|
|
const currentRound = ref(1);
|
2025-08-18 16:09:11 +08:00
|
|
|
const goldenRound = ref(0);
|
2025-08-13 16:44:25 +08:00
|
|
|
const currentRedPoint = ref(0);
|
|
|
|
|
const currentBluePoint = ref(0);
|
|
|
|
|
const scores = ref([]);
|
|
|
|
|
const blueScores = ref([]);
|
|
|
|
|
const redTeam = ref([]);
|
2025-08-14 10:50:44 +08:00
|
|
|
const blueTeam = ref([]);
|
2025-08-13 16:44:25 +08:00
|
|
|
const currentShooterId = ref(0);
|
|
|
|
|
const roundResults = ref([]);
|
|
|
|
|
const redPoints = ref(0);
|
|
|
|
|
const bluePoints = ref(0);
|
|
|
|
|
const showRoundTip = ref(false);
|
|
|
|
|
const isFinalShoot = ref(false);
|
|
|
|
|
const isEnded = ref(false);
|
|
|
|
|
|
2025-08-14 10:50:44 +08:00
|
|
|
function recoverData(battleInfo) {
|
|
|
|
|
uni.removeStorageSync("last-awake-time");
|
|
|
|
|
battleId.value = battleInfo.id;
|
|
|
|
|
redTeam.value = battleInfo.redTeam;
|
|
|
|
|
blueTeam.value = battleInfo.blueTeam;
|
|
|
|
|
if (battleInfo.status === 0) {
|
|
|
|
|
const readyRemain = Date.now() / 1000 - battleInfo.startTime;
|
|
|
|
|
console.log(`当前局已进行${readyRemain}秒`);
|
|
|
|
|
if (readyRemain > 0) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.$emit("update-timer", 15 - readyRemain);
|
|
|
|
|
}, 200);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
start.value = true;
|
|
|
|
|
bluePoints.value = 0;
|
|
|
|
|
redPoints.value = 0;
|
|
|
|
|
currentRound.value = battleInfo.currentRound;
|
2025-08-20 18:30:02 +08:00
|
|
|
roundResults.value = [...battleInfo.roundResults];
|
2025-11-07 16:28:52 +08:00
|
|
|
// 算得分
|
2025-08-14 10:50:44 +08:00
|
|
|
battleInfo.roundResults.forEach((round) => {
|
|
|
|
|
const blueTotal = round.blueArrows.reduce(
|
|
|
|
|
(last, next) => last + next.ring,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
const redTotal = round.redArrows.reduce(
|
|
|
|
|
(last, next) => last + next.ring,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
if (blueTotal === redTotal) {
|
|
|
|
|
bluePoints.value += 1;
|
|
|
|
|
redPoints.value += 1;
|
|
|
|
|
} else if (blueTotal > redTotal) {
|
|
|
|
|
bluePoints.value += 2;
|
|
|
|
|
} else {
|
|
|
|
|
redPoints.value += 2;
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-11-07 16:28:52 +08:00
|
|
|
if (battleInfo.goldenRoundNumber) {
|
|
|
|
|
currentRound.value += battleInfo.goldenRoundNumber;
|
|
|
|
|
goldenRound.value = battleInfo.goldenRoundNumber;
|
2025-08-14 10:50:44 +08:00
|
|
|
isFinalShoot.value = true;
|
2025-11-07 16:28:52 +08:00
|
|
|
for (let i = 1; i <= battleInfo.goldenRoundNumber; i++) {
|
|
|
|
|
const redArrows = [];
|
|
|
|
|
battleInfo.redTeam.forEach((item) => {
|
|
|
|
|
if (item.shotHistory[roundResults.value.length + 1]) {
|
|
|
|
|
item.shotHistory[roundResults.value.length + 1]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.forEach((item) => redArrows.push(item));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const blueArrows = [];
|
|
|
|
|
battleInfo.blueTeam.forEach((item) => {
|
|
|
|
|
if (item.shotHistory[roundResults.value.length + 1]) {
|
|
|
|
|
item.shotHistory[roundResults.value.length + 1]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.forEach((item) => blueArrows.push(item));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
roundResults.value.push({
|
|
|
|
|
number: roundResults.value.length + 1,
|
|
|
|
|
blueArrows,
|
|
|
|
|
redArrows,
|
|
|
|
|
blueTotal: blueArrows.reduce((last, next) => last + next.ring, 0),
|
|
|
|
|
redTotal: redArrows.reduce((last, next) => last + next.ring, 0),
|
|
|
|
|
});
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
2025-09-03 16:34:54 +08:00
|
|
|
} else {
|
2025-11-07 16:28:52 +08:00
|
|
|
const hasCurrentRoundData =
|
|
|
|
|
battleInfo.redTeam.some(
|
|
|
|
|
(item) => !!item.shotHistory[battleInfo.currentRound]
|
|
|
|
|
) ||
|
|
|
|
|
battleInfo.blueTeam.some(
|
|
|
|
|
(item) => !!item.shotHistory[battleInfo.currentRound]
|
|
|
|
|
);
|
|
|
|
|
if (
|
|
|
|
|
battleInfo.currentRound > battleInfo.roundResults.length &&
|
|
|
|
|
hasCurrentRoundData
|
|
|
|
|
) {
|
|
|
|
|
const blueArrows = [];
|
|
|
|
|
const redArrows = [];
|
|
|
|
|
battleInfo.redTeam.forEach((item) =>
|
|
|
|
|
item.shotHistory[battleInfo.currentRound]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.forEach((item) => redArrows.push(item))
|
|
|
|
|
);
|
|
|
|
|
battleInfo.blueTeam.forEach((item) =>
|
|
|
|
|
item.shotHistory[battleInfo.currentRound]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.forEach((item) => blueArrows.push(item))
|
|
|
|
|
);
|
|
|
|
|
roundResults.value.push({
|
|
|
|
|
redArrows,
|
|
|
|
|
blueArrows,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-09-03 16:34:54 +08:00
|
|
|
[...battleInfo.redTeam, ...battleInfo.blueTeam].some((p) => {
|
|
|
|
|
if (p.id === user.value.id) {
|
|
|
|
|
const roundArrows = Object.values(p.shotHistory);
|
|
|
|
|
if (roundArrows.length) {
|
|
|
|
|
uni.$emit("update-shot", {
|
|
|
|
|
currentShot: roundArrows[roundArrows.length - 1].length,
|
|
|
|
|
totalShot: battleInfo.config.teamSize === 2 ? 3 : 2,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
const lastIndex = roundResults.value.length - 1;
|
|
|
|
|
if (roundResults.value[lastIndex]) {
|
|
|
|
|
const redArrows = roundResults.value[lastIndex].redArrows;
|
2025-11-07 16:28:52 +08:00
|
|
|
scores.value = [...redArrows]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.sort((a, b) => a.shotTimeUnix - b.shotTimeUnix);
|
2025-08-14 10:50:44 +08:00
|
|
|
const blueArrows = roundResults.value[lastIndex].blueArrows;
|
2025-11-07 16:28:52 +08:00
|
|
|
blueScores.value = [...blueArrows]
|
|
|
|
|
.filter((item) => !!item.playerId)
|
|
|
|
|
.sort((a, b) => a.shotTimeUnix - b.shotTimeUnix);
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
if (battleInfo.firePlayerIndex) {
|
|
|
|
|
currentShooterId.value = battleInfo.firePlayerIndex;
|
2025-08-15 11:23:23 +08:00
|
|
|
const redPlayer = redTeam.value.find(
|
|
|
|
|
(item) => item.id === currentShooterId.value
|
|
|
|
|
);
|
2025-11-08 11:33:03 +08:00
|
|
|
let nextTips = redPlayer ? "请红队射箭" : "请蓝队射箭";
|
2025-11-10 09:26:51 +08:00
|
|
|
nextTips += "重回";
|
|
|
|
|
// if (battleInfo.firePlayerIndex === user.value.id) nextTips += "你";
|
2025-11-08 11:33:03 +08:00
|
|
|
tips.value = nextTips;
|
|
|
|
|
uni.$emit("update-tips", nextTips);
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
if (battleInfo.fireTime > 0) {
|
|
|
|
|
const remain = Date.now() / 1000 - battleInfo.fireTime;
|
|
|
|
|
console.log(`当前箭已过${remain}秒`);
|
|
|
|
|
if (remain > 0 && remain <= 15) {
|
|
|
|
|
// 等渲染好再通知
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.$emit("update-ramain", 15 - remain);
|
|
|
|
|
}, 300);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function onReceiveMessage(messages = []) {
|
|
|
|
|
messages.forEach((msg) => {
|
2025-11-10 13:43:25 +08:00
|
|
|
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
|
|
|
|
|
redTeam.value = msg.groupUserStatus.redTeam;
|
|
|
|
|
blueTeam.value = msg.groupUserStatus.blueTeam;
|
|
|
|
|
}
|
2025-08-14 10:50:44 +08:00
|
|
|
if (msg.constructor === MESSAGETYPES.AllReady) {
|
|
|
|
|
start.value = true;
|
|
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.ToSomeoneShoot) {
|
|
|
|
|
if (currentShooterId.value !== msg.userId) {
|
|
|
|
|
currentShooterId.value = msg.userId;
|
2025-08-15 11:23:23 +08:00
|
|
|
const redPlayer = redTeam.value.find(
|
|
|
|
|
(item) => item.id === currentShooterId.value
|
|
|
|
|
);
|
2025-11-10 09:26:51 +08:00
|
|
|
|
2025-11-08 11:33:03 +08:00
|
|
|
let nextTips = redPlayer ? "请红队射箭" : "请蓝队射箭";
|
2025-11-10 09:26:51 +08:00
|
|
|
if (msg.userId === user.value.id && redTeam.value.length > 1) {
|
|
|
|
|
nextTips += "你";
|
|
|
|
|
}
|
2025-08-15 15:25:41 +08:00
|
|
|
if (nextTips !== tips.value) {
|
|
|
|
|
tips.value = nextTips;
|
|
|
|
|
uni.$emit("update-tips", tips.value);
|
|
|
|
|
} else {
|
|
|
|
|
uni.$emit("update-ramain", 15);
|
|
|
|
|
}
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.ShootResult) {
|
2025-11-08 11:33:03 +08:00
|
|
|
if (msg.battleInfo) recoverData(msg.battleInfo);
|
2025-08-14 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
|
|
|
|
|
const result = msg.preRoundResult;
|
|
|
|
|
scores.value = [];
|
|
|
|
|
blueScores.value = [];
|
|
|
|
|
currentShooterId.value = 0;
|
|
|
|
|
currentBluePoint.value = result.blueScore;
|
|
|
|
|
currentRedPoint.value = result.redScore;
|
|
|
|
|
bluePoints.value += result.blueScore;
|
|
|
|
|
redPoints.value += result.redScore;
|
2025-08-20 18:30:02 +08:00
|
|
|
if (!result.goldenRound) {
|
2025-08-14 10:50:44 +08:00
|
|
|
showRoundTip.value = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.FinalShoot) {
|
|
|
|
|
currentShooterId.value = 0;
|
2025-08-18 16:09:11 +08:00
|
|
|
currentBluePoint.value = bluePoints.value;
|
|
|
|
|
currentRedPoint.value = redPoints.value;
|
2025-08-14 10:50:44 +08:00
|
|
|
if (!isFinalShoot.value) {
|
|
|
|
|
isFinalShoot.value = true;
|
|
|
|
|
showRoundTip.value = true;
|
|
|
|
|
tips.value = "准备开始决金箭";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.MatchOver) {
|
|
|
|
|
if (msg.endStatus.noSaved) {
|
|
|
|
|
currentBluePoint.value = 0;
|
|
|
|
|
currentRedPoint.value = 0;
|
|
|
|
|
showRoundTip.value = true;
|
2025-08-20 18:30:02 +08:00
|
|
|
isFinalShoot.value = false;
|
2025-08-14 10:50:44 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.navigateBack();
|
|
|
|
|
}, 3000);
|
|
|
|
|
} else {
|
|
|
|
|
isEnded.value = true;
|
|
|
|
|
uni.setStorageSync("last-battle", msg.endStatus);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
url: "/pages/battle-result",
|
|
|
|
|
});
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
|
|
|
|
uni.$emit("update-header-loading", false);
|
|
|
|
|
if (msg.battleInfo) recoverData(msg.battleInfo);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-13 16:44:25 +08:00
|
|
|
|
|
|
|
|
onLoad(async (options) => {
|
|
|
|
|
if (options.battleId) {
|
|
|
|
|
battleId.value = options.battleId;
|
|
|
|
|
redTeam.value = uni.getStorageSync("red-team");
|
|
|
|
|
blueTeam.value = uni.getStorageSync("blue-team");
|
|
|
|
|
const battleInfo = uni.getStorageSync("current-battle");
|
|
|
|
|
if (battleInfo) {
|
|
|
|
|
await nextTick(() => {
|
|
|
|
|
recoverData(battleInfo);
|
|
|
|
|
});
|
|
|
|
|
setTimeout(getCurrentGameAPI, 2000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-11 16:31:47 +08:00
|
|
|
uni.enableAlertBeforeUnload({
|
|
|
|
|
message: "离开比赛可能导致比赛失败,是否继续?",
|
|
|
|
|
success: (res) => {
|
|
|
|
|
console.log("已启用离开提示");
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-08-13 16:44:25 +08:00
|
|
|
});
|
2025-10-30 09:09:03 +08:00
|
|
|
onMounted(async () => {
|
2025-08-13 16:44:25 +08:00
|
|
|
uni.setKeepScreenOn({
|
|
|
|
|
keepScreenOn: true,
|
|
|
|
|
});
|
|
|
|
|
uni.$on("socket-inbox", onReceiveMessage);
|
2025-10-30 09:09:03 +08:00
|
|
|
await laserCloseAPI();
|
2025-08-13 16:44:25 +08:00
|
|
|
});
|
2025-08-25 13:47:32 +08:00
|
|
|
onBeforeUnmount(() => {
|
2025-08-13 16:44:25 +08:00
|
|
|
uni.setKeepScreenOn({
|
|
|
|
|
keepScreenOn: false,
|
|
|
|
|
});
|
|
|
|
|
uni.$off("socket-inbox", onReceiveMessage);
|
2025-11-13 11:58:16 +08:00
|
|
|
audioManager.stopAll();
|
2025-08-13 16:44:25 +08:00
|
|
|
});
|
2025-08-14 10:50:44 +08:00
|
|
|
const refreshTimer = ref(null);
|
|
|
|
|
onShow(async () => {
|
|
|
|
|
if (battleId.value) {
|
|
|
|
|
if (!isEnded.value && (await isGameEnded(battleId.value))) return;
|
|
|
|
|
getCurrentGameAPI();
|
|
|
|
|
const refreshData = () => {
|
|
|
|
|
const lastAwakeTime = uni.getStorageSync("last-awake-time");
|
|
|
|
|
if (lastAwakeTime) {
|
|
|
|
|
getCurrentGameAPI();
|
|
|
|
|
} else {
|
|
|
|
|
clearInterval(refreshTimer.value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
refreshTimer.value = setInterval(refreshData, 2000);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
onHide(() => {
|
|
|
|
|
if (refreshTimer.value) clearInterval(refreshTimer.value);
|
|
|
|
|
uni.setStorageSync("last-awake-time", Date.now());
|
|
|
|
|
});
|
2025-08-13 16:44:25 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-11-11 16:31:47 +08:00
|
|
|
<Container :bgType="start ? 3 : 1">
|
2025-08-13 16:44:25 +08:00
|
|
|
<view class="container">
|
|
|
|
|
<BattleHeader v-if="!start" :redTeam="redTeam" :blueTeam="blueTeam" />
|
2025-08-22 11:51:52 +08:00
|
|
|
<TestDistance v-if="!start" :guide="false" :isBattle="true" />
|
2025-08-14 10:50:44 +08:00
|
|
|
<view v-if="start" class="players-row">
|
|
|
|
|
<TeamAvatars
|
|
|
|
|
:team="blueTeam"
|
|
|
|
|
:isRed="false"
|
|
|
|
|
:currentShooterId="currentShooterId"
|
|
|
|
|
/>
|
2025-08-18 16:09:11 +08:00
|
|
|
<ShootProgress2
|
|
|
|
|
:tips="tips"
|
|
|
|
|
:currentRound="
|
|
|
|
|
goldenRound > 0 ? 'gold' + goldenRound : 'round' + currentRound
|
|
|
|
|
"
|
|
|
|
|
/>
|
2025-08-14 15:24:12 +08:00
|
|
|
<TeamAvatars :team="redTeam" :currentShooterId="currentShooterId" />
|
2025-08-13 16:44:25 +08:00
|
|
|
</view>
|
|
|
|
|
<BowTarget
|
|
|
|
|
v-if="start"
|
|
|
|
|
mode="team"
|
|
|
|
|
:scores="scores"
|
|
|
|
|
:blueScores="blueScores"
|
|
|
|
|
/>
|
|
|
|
|
<BattleFooter
|
|
|
|
|
v-if="start"
|
|
|
|
|
:roundResults="roundResults"
|
|
|
|
|
:redPoints="redPoints"
|
|
|
|
|
:bluePoints="bluePoints"
|
2025-08-18 16:09:11 +08:00
|
|
|
:goldenRound="goldenRound"
|
2025-08-13 16:44:25 +08:00
|
|
|
/>
|
|
|
|
|
<ScreenHint
|
|
|
|
|
:show="showRoundTip"
|
|
|
|
|
:onClose="() => (showRoundTip = false)"
|
|
|
|
|
:mode="isFinalShoot ? 'tall' : 'normal'"
|
|
|
|
|
>
|
|
|
|
|
<RoundEndTip
|
|
|
|
|
v-if="showRoundTip"
|
|
|
|
|
:isFinal="isFinalShoot"
|
2025-11-07 16:28:52 +08:00
|
|
|
:round="currentRound"
|
2025-08-13 16:44:25 +08:00
|
|
|
:bluePoint="currentBluePoint"
|
|
|
|
|
:redPoint="currentRedPoint"
|
|
|
|
|
:roundData="
|
2025-11-07 16:42:07 +08:00
|
|
|
roundResults[currentRound - 1] ? roundResults[currentRound - 1] : []
|
2025-08-13 16:44:25 +08:00
|
|
|
"
|
|
|
|
|
:onAutoClose="() => (showRoundTip = false)"
|
|
|
|
|
/>
|
|
|
|
|
</ScreenHint>
|
|
|
|
|
</view>
|
|
|
|
|
</Container>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.container {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
.players-row {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
2025-09-03 16:34:54 +08:00
|
|
|
justify-content: center;
|
2025-11-10 17:47:32 +08:00
|
|
|
margin-top: -2%;
|
2025-11-08 10:57:37 +08:00
|
|
|
margin-bottom: 6%;
|
2025-08-13 16:44:25 +08:00
|
|
|
}
|
|
|
|
|
</style>
|