2025-05-16 15:56:54 +08:00
|
|
|
|
<script setup>
|
2025-07-11 12:03:55 +08:00
|
|
|
|
import { ref, watch, onMounted, onUnmounted } from "vue";
|
2025-07-22 09:36:38 +08:00
|
|
|
|
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
2025-06-05 21:32:51 +08:00
|
|
|
|
import Container from "@/components/Container.vue";
|
2025-05-16 15:56:54 +08:00
|
|
|
|
import BowTarget from "@/components/BowTarget.vue";
|
|
|
|
|
|
import ShootProgress from "@/components/ShootProgress.vue";
|
2025-06-09 12:24:01 +08:00
|
|
|
|
import Guide from "@/components/Guide.vue";
|
|
|
|
|
|
import BattleHeader from "@/components/BattleHeader.vue";
|
2025-06-05 21:32:51 +08:00
|
|
|
|
import Timer from "@/components/Timer.vue";
|
2025-06-09 01:17:18 +08:00
|
|
|
|
import PlayerScore from "@/components/PlayerScore.vue";
|
2025-06-05 21:32:51 +08:00
|
|
|
|
import SButton from "@/components/SButton.vue";
|
2025-06-17 16:02:29 +08:00
|
|
|
|
import Avatar from "@/components/Avatar.vue";
|
2025-06-20 11:22:41 +08:00
|
|
|
|
import ScreenHint from "@/components/ScreenHint.vue";
|
2025-06-25 21:54:18 +08:00
|
|
|
|
import Matching from "@/components/Matching.vue";
|
2025-07-05 14:52:41 +08:00
|
|
|
|
import TestDistance from "@/components/TestDistance.vue";
|
2025-07-22 15:47:07 +08:00
|
|
|
|
import { getCurrentGameAPI, matchGameAPI } from "@/apis";
|
|
|
|
|
|
import { isGameEnded } from "@/util";
|
2025-06-17 16:02:29 +08:00
|
|
|
|
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
2025-06-09 01:17:18 +08:00
|
|
|
|
import useStore from "@/store";
|
|
|
|
|
|
import { storeToRefs } from "pinia";
|
|
|
|
|
|
const store = useStore();
|
|
|
|
|
|
const { user } = storeToRefs(store);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
const gameType = ref(0);
|
|
|
|
|
|
const teamSize = ref(0);
|
|
|
|
|
|
const start = ref(false);
|
2025-07-17 17:02:05 +08:00
|
|
|
|
const startCount = ref(true);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
const battleId = ref("");
|
|
|
|
|
|
const currentRound = ref(1);
|
|
|
|
|
|
const power = ref(0);
|
|
|
|
|
|
const scores = ref([]);
|
|
|
|
|
|
const tips = ref("即将开始...");
|
2025-06-15 20:55:34 +08:00
|
|
|
|
const seq = ref(0);
|
2025-06-09 01:17:18 +08:00
|
|
|
|
const players = ref([]);
|
2025-07-11 12:03:55 +08:00
|
|
|
|
const playersSorted = ref([]);
|
2025-06-09 01:17:18 +08:00
|
|
|
|
const playersScores = ref({});
|
2025-06-20 11:22:41 +08:00
|
|
|
|
const halfTimeTip = ref(false);
|
2025-06-25 21:54:18 +08:00
|
|
|
|
const onComplete = ref(null);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
const isEnded = ref(false);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
|
2025-07-11 12:03:55 +08:00
|
|
|
|
watch(
|
|
|
|
|
|
() => [players.value, playersScores.value],
|
|
|
|
|
|
([n_players, n_scores]) => {
|
|
|
|
|
|
if (n_players.length) {
|
|
|
|
|
|
playersSorted.value = Object.keys(n_scores)
|
|
|
|
|
|
.sort((a, b) => n_scores[b].length - n_scores[a].length)
|
|
|
|
|
|
.map((pid) => n_players.find((p) => p.id == pid));
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
deep: true, // 添加深度监听
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-07-22 15:47:07 +08:00
|
|
|
|
function recoverData(battleInfo) {
|
2025-07-23 11:18:47 +08:00
|
|
|
|
uni.removeStorageSync("last-awake-time");
|
2025-07-22 15:47:07 +08:00
|
|
|
|
battleId.value = battleInfo.id;
|
|
|
|
|
|
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
|
|
|
|
|
|
players.value.forEach((p) => {
|
|
|
|
|
|
playersScores.value[p.id] = [...p.arrows];
|
2025-07-22 17:58:22 +08:00
|
|
|
|
if (p.id === user.value.id) scores.value = [...p.arrows];
|
2025-07-22 15:47:07 +08:00
|
|
|
|
});
|
|
|
|
|
|
const remain = Date.now() / 1000 - battleInfo.startTime;
|
|
|
|
|
|
console.log(`当前局已进行${remain}秒`);
|
|
|
|
|
|
if (battleInfo.status === 0) {
|
|
|
|
|
|
if (remain > 0) {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.$emit("update-timer", 15 - remain);
|
|
|
|
|
|
}, 200);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
start.value = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (battleInfo.status === 2) {
|
2025-07-23 14:31:21 +08:00
|
|
|
|
const elapsedTime = (Date.now() - Date.parse(battleInfo.createdAt)) / 1000;
|
|
|
|
|
|
console.log("elapsedTime:", elapsedTime);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
startCount.value = true;
|
|
|
|
|
|
// 这里的开始时间不是游戏开始时间,而是上半场或者下半场或者中场的开始时间,还要根据状态来判断
|
|
|
|
|
|
tips.value = battleInfo.halfGame
|
|
|
|
|
|
? "下半场:请再射6箭"
|
|
|
|
|
|
: "上半场:请先射6箭";
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.$emit("update-ramain", 90 - remain);
|
|
|
|
|
|
}, 200);
|
|
|
|
|
|
} else if (battleInfo.status === 9) {
|
|
|
|
|
|
startCount.value = false;
|
|
|
|
|
|
tips.value = "准备下半场";
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.$emit("update-ramain", 0);
|
|
|
|
|
|
}, 200);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-25 21:54:18 +08:00
|
|
|
|
onLoad(async (options) => {
|
2025-07-05 17:24:52 +08:00
|
|
|
|
if (options.battleId) {
|
2025-07-15 18:14:59 +08:00
|
|
|
|
const battleInfo = uni.getStorageSync("current-battle");
|
2025-07-23 11:18:47 +08:00
|
|
|
|
if (battleInfo) recoverData(battleInfo);
|
2025-07-05 17:24:52 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
gameType.value = options.gameType;
|
|
|
|
|
|
teamSize.value = options.teamSize;
|
|
|
|
|
|
if (gameType.value && teamSize.value) {
|
|
|
|
|
|
await matchGameAPI(true, gameType.value, teamSize.value);
|
|
|
|
|
|
}
|
2025-06-25 21:54:18 +08:00
|
|
|
|
}
|
2025-06-05 21:32:51 +08:00
|
|
|
|
});
|
2025-07-21 10:40:43 +08:00
|
|
|
|
|
2025-06-05 21:32:51 +08:00
|
|
|
|
async function stopMatch() {
|
2025-07-13 14:57:16 +08:00
|
|
|
|
uni.$showHint(3);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 21:03:33 +08:00
|
|
|
|
async function onReceiveMessage(messages = []) {
|
2025-06-05 21:32:51 +08:00
|
|
|
|
messages.forEach((msg) => {
|
|
|
|
|
|
if (
|
|
|
|
|
|
!msg.id ||
|
|
|
|
|
|
(battleId.value && msg.id === battleId.value) ||
|
|
|
|
|
|
msg.constructor === MESSAGETYPES.WaitForAllReady
|
|
|
|
|
|
) {
|
2025-06-17 16:02:29 +08:00
|
|
|
|
console.log("收到消息:", getMessageTypeName(msg.constructor), msg);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
|
2025-06-25 21:54:18 +08:00
|
|
|
|
onComplete.value = () => {
|
|
|
|
|
|
// 这里会掉多次;
|
|
|
|
|
|
battleId.value = msg.id;
|
|
|
|
|
|
players.value = [
|
|
|
|
|
|
...msg.groupUserStatus.redTeam,
|
|
|
|
|
|
...msg.groupUserStatus.blueTeam,
|
|
|
|
|
|
];
|
|
|
|
|
|
players.value.forEach((p) => {
|
|
|
|
|
|
playersScores.value[p.id] = [];
|
|
|
|
|
|
});
|
2025-07-16 17:55:11 +08:00
|
|
|
|
uni.$hideHint();
|
2025-06-25 21:54:18 +08:00
|
|
|
|
};
|
2025-06-09 01:17:18 +08:00
|
|
|
|
}
|
2025-06-05 21:32:51 +08:00
|
|
|
|
if (msg.id !== battleId.value) return;
|
2025-06-09 01:17:18 +08:00
|
|
|
|
if (msg.constructor === MESSAGETYPES.MeleeAllReady) {
|
2025-06-05 21:32:51 +08:00
|
|
|
|
start.value = true;
|
2025-07-17 17:02:05 +08:00
|
|
|
|
startCount.value = true;
|
2025-06-15 20:55:34 +08:00
|
|
|
|
seq.value += 1;
|
2025-07-21 10:40:43 +08:00
|
|
|
|
tips.value = scores.value.length
|
|
|
|
|
|
? "下半场:请再射6箭"
|
|
|
|
|
|
: "上半场:请先射6箭";
|
2025-07-16 17:55:11 +08:00
|
|
|
|
halfTimeTip.value = false;
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (msg.constructor === MESSAGETYPES.ShootResult) {
|
2025-07-22 17:58:22 +08:00
|
|
|
|
if (!start.value) getCurrentGameAPI();
|
2025-06-09 01:17:18 +08:00
|
|
|
|
if (msg.userId === user.value.id) {
|
2025-07-19 16:16:53 +08:00
|
|
|
|
scores.value.push({ ...msg.target });
|
2025-06-09 01:17:18 +08:00
|
|
|
|
power.value = msg.target.battery;
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
2025-07-19 16:16:53 +08:00
|
|
|
|
playersScores.value[msg.userId].push({ ...msg.target });
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
2025-06-17 21:34:41 +08:00
|
|
|
|
if (msg.constructor === MESSAGETYPES.HalfTimeOver) {
|
2025-07-17 16:14:30 +08:00
|
|
|
|
uni.$emit("update-ramain", 0);
|
2025-07-16 18:18:02 +08:00
|
|
|
|
[...msg.groupUserStatus.redTeam, ...msg.groupUserStatus.blueTeam].forEach(
|
|
|
|
|
|
(player) => {
|
2025-07-22 17:58:22 +08:00
|
|
|
|
playersScores.value[player.id] = [...player.arrows];
|
|
|
|
|
|
if (player.id === user.value.id) scores.value = [...player.arrows];
|
2025-07-16 18:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
);
|
2025-07-15 18:14:59 +08:00
|
|
|
|
startCount.value = false;
|
2025-06-20 11:22:41 +08:00
|
|
|
|
halfTimeTip.value = true;
|
|
|
|
|
|
tips.value = "准备下半场";
|
2025-06-17 21:34:41 +08:00
|
|
|
|
}
|
2025-06-05 21:32:51 +08:00
|
|
|
|
if (msg.constructor === MESSAGETYPES.MatchOver) {
|
2025-07-25 09:59:54 +08:00
|
|
|
|
isEnded.value = true;
|
2025-07-22 09:36:38 +08:00
|
|
|
|
uni.setStorageSync("last-battle", msg.endStatus);
|
2025-07-25 09:59:54 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
|
url: "/pages/battle-result",
|
2025-07-16 16:09:10 +08:00
|
|
|
|
});
|
2025-07-25 15:18:46 +08:00
|
|
|
|
}, 3000);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
2025-07-22 15:47:07 +08:00
|
|
|
|
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
2025-07-23 11:18:47 +08:00
|
|
|
|
uni.$emit("update-header-loading", false);
|
|
|
|
|
|
if (msg.battleInfo) recoverData(msg.battleInfo);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
}
|
2025-06-05 21:32:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-01 00:25:17 +08:00
|
|
|
|
const onBack = () => {
|
|
|
|
|
|
if (battleId.value) {
|
2025-07-11 00:47:34 +08:00
|
|
|
|
uni.$showHint(2);
|
2025-07-01 00:25:17 +08:00
|
|
|
|
} else {
|
2025-07-11 01:00:54 +08:00
|
|
|
|
uni.$showHint(3);
|
2025-07-01 00:25:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-06-05 21:32:51 +08:00
|
|
|
|
onMounted(() => {
|
2025-07-13 11:21:19 +08:00
|
|
|
|
uni.setKeepScreenOn({
|
|
|
|
|
|
keepScreenOn: true,
|
|
|
|
|
|
});
|
2025-06-05 21:32:51 +08:00
|
|
|
|
uni.$on("socket-inbox", onReceiveMessage);
|
|
|
|
|
|
});
|
|
|
|
|
|
onUnmounted(() => {
|
2025-07-13 11:21:19 +08:00
|
|
|
|
uni.setKeepScreenOn({
|
|
|
|
|
|
keepScreenOn: false,
|
|
|
|
|
|
});
|
2025-06-05 21:32:51 +08:00
|
|
|
|
uni.$off("socket-inbox", onReceiveMessage);
|
2025-07-19 16:16:53 +08:00
|
|
|
|
if (gameType.value && teamSize.value && !battleId.value) {
|
2025-07-15 12:03:14 +08:00
|
|
|
|
matchGameAPI(false, gameType.value, teamSize.value);
|
2025-06-05 21:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-07-23 11:18:47 +08:00
|
|
|
|
const refreshTimer = ref(null);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
onShow(async () => {
|
|
|
|
|
|
if (battleId.value) {
|
2025-07-23 20:47:31 +08:00
|
|
|
|
if (!isEnded.value && (await isGameEnded(battleId.value))) return;
|
2025-07-23 11:18:47 +08:00
|
|
|
|
getCurrentGameAPI();
|
|
|
|
|
|
const refreshData = () => {
|
|
|
|
|
|
const lastAwakeTime = uni.getStorageSync("last-awake-time");
|
|
|
|
|
|
if (lastAwakeTime) {
|
|
|
|
|
|
getCurrentGameAPI();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
clearInterval(refreshTimer.value);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
refreshTimer.value = setInterval(refreshData, 2000);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
onHide(() => {
|
2025-07-23 11:18:47 +08:00
|
|
|
|
if (refreshTimer.value) clearInterval(refreshTimer.value);
|
2025-07-22 15:47:07 +08:00
|
|
|
|
uni.setStorageSync("last-awake-time", Date.now());
|
|
|
|
|
|
});
|
2025-05-16 15:56:54 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-07-01 00:25:17 +08:00
|
|
|
|
<Container
|
|
|
|
|
|
:title="battleId ? '大乱斗排位赛' : '搜索对手...'"
|
|
|
|
|
|
:bgType="1"
|
|
|
|
|
|
:onBack="onBack"
|
|
|
|
|
|
>
|
2025-06-05 21:32:51 +08:00
|
|
|
|
<view class="container">
|
2025-06-25 21:54:18 +08:00
|
|
|
|
<block v-if="battleId">
|
2025-07-18 22:17:17 +08:00
|
|
|
|
<BattleHeader v-if="!start" :players="players" />
|
2025-07-05 14:52:41 +08:00
|
|
|
|
<TestDistance v-if="!start" :guide="false" />
|
2025-06-25 21:54:18 +08:00
|
|
|
|
<ShootProgress
|
2025-07-19 16:16:53 +08:00
|
|
|
|
:show="start"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
:seq="seq"
|
2025-07-15 18:14:59 +08:00
|
|
|
|
:start="start && startCount"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
:tips="tips"
|
2025-07-16 18:18:02 +08:00
|
|
|
|
:total="90"
|
2025-07-15 17:10:41 +08:00
|
|
|
|
:melee="true"
|
2025-07-16 18:18:02 +08:00
|
|
|
|
:battleId="battleId"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
/>
|
2025-07-11 00:47:34 +08:00
|
|
|
|
<view v-if="start" class="user-row">
|
2025-06-25 21:54:18 +08:00
|
|
|
|
<Avatar :src="user.avatar" :size="35" />
|
|
|
|
|
|
<BowPower :power="power" />
|
2025-06-09 12:24:01 +08:00
|
|
|
|
</view>
|
2025-06-25 21:54:18 +08:00
|
|
|
|
<BowTarget
|
2025-07-05 14:52:41 +08:00
|
|
|
|
v-if="start"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
:currentRound="scores.length"
|
2025-07-21 10:40:43 +08:00
|
|
|
|
:totalRound="12"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
:scores="scores"
|
2025-07-16 15:32:49 +08:00
|
|
|
|
:stop="!startCount"
|
2025-06-25 21:54:18 +08:00
|
|
|
|
/>
|
2025-06-26 22:54:17 +08:00
|
|
|
|
<view :style="{ paddingBottom: '20px' }">
|
|
|
|
|
|
<PlayerScore
|
|
|
|
|
|
v-if="start"
|
2025-07-11 12:03:55 +08:00
|
|
|
|
v-for="(player, index) in playersSorted"
|
2025-06-26 22:54:17 +08:00
|
|
|
|
:key="index"
|
|
|
|
|
|
:name="player.name"
|
|
|
|
|
|
:avatar="player.avatar"
|
2025-06-28 12:03:33 +08:00
|
|
|
|
:scores="playersScores[player.id] || []"
|
2025-06-26 22:54:17 +08:00
|
|
|
|
/>
|
|
|
|
|
|
</view>
|
2025-07-21 10:40:43 +08:00
|
|
|
|
<Timer v-if="!start" />
|
2025-06-25 21:54:18 +08:00
|
|
|
|
<ScreenHint
|
|
|
|
|
|
:show="halfTimeTip"
|
|
|
|
|
|
mode="small"
|
|
|
|
|
|
:onClose="() => (halfTimeTip = false)"
|
|
|
|
|
|
>
|
|
|
|
|
|
<view class="half-time-tip">
|
|
|
|
|
|
<text>上半场结束,休息一下吧:)</text>
|
|
|
|
|
|
<text>20秒后开始下半场</text>
|
|
|
|
|
|
</view>
|
|
|
|
|
|
</ScreenHint>
|
|
|
|
|
|
</block>
|
|
|
|
|
|
<block v-else>
|
|
|
|
|
|
<Matching
|
|
|
|
|
|
v-if="!battleId"
|
|
|
|
|
|
:stopMatch="stopMatch"
|
|
|
|
|
|
:onComplete="onComplete"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</block>
|
2025-06-05 21:32:51 +08:00
|
|
|
|
</view>
|
|
|
|
|
|
</Container>
|
2025-05-16 15:56:54 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.container {
|
|
|
|
|
|
width: 100%;
|
2025-06-25 21:54:18 +08:00
|
|
|
|
height: 100%;
|
2025-05-16 15:56:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|