Files
shoot-miniprograms/src/pages/melee-match.vue

269 lines
7.7 KiB
Vue
Raw Normal View History

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-06-05 21:32:51 +08:00
import { onLoad } from "@dcloudio/uni-app";
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-06-05 21:32:51 +08:00
import { matchGameAPI, readyGameAPI } from "@/apis";
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-15 18:14:59 +08:00
const startCount = ref(false);
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-05 21:32:51 +08:00
const timerSeq = 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-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-06-25 21:54:18 +08:00
onLoad(async (options) => {
if (options.battleId) {
2025-07-15 18:14:59 +08:00
const battleInfo = uni.getStorageSync("current-battle");
2025-07-13 21:06:48 +08:00
console.log("----battleInfo", battleInfo);
if (battleInfo) {
battleId.value = battleInfo.id;
start.value = true;
2025-07-16 15:32:49 +08:00
startCount.value = true;
tips.value = "请连续射出6支箭";
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
players.value.forEach((p) => {
playersScores.value[p.id] = p.arrows;
2025-07-16 17:55:11 +08:00
if (p.id === user.value.id) {
scores.value = p.arrows;
}
});
if (battleInfo.startTime) {
const remain = Date.now() / 1000 - battleInfo.startTime;
if (remain <= 90) {
setTimeout(() => {
uni.$emit("update-ramain", remain);
}, 300);
} else if (remain > 90 && remain <= 110) {
2025-07-16 15:32:49 +08:00
startCount.value = false;
tips.value = "准备下半场";
} else if (remain > 110) {
setTimeout(() => {
uni.$emit("update-ramain", remain - 110);
}, 300);
}
}
}
} 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
});
async function stopMatch() {
2025-07-13 14:57:16 +08:00
uni.$showHint(3);
2025-06-05 21:32:51 +08:00
}
async function readyToGo() {
if (battleId.value) {
await readyGameAPI(battleId.value);
2025-06-17 19:35:21 +08:00
scores.value = [];
2025-06-05 21:32:51 +08:00
start.value = true;
timerSeq.value = 0;
}
}
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 = () => {
// 这里会掉多次;
timerSeq.value += 1;
battleId.value = msg.id;
2025-07-17 16:14:30 +08:00
startCount.value = true;
2025-06-25 21:54:18 +08:00
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-06-15 20:55:34 +08:00
seq.value += 1;
2025-06-05 21:32:51 +08:00
timerSeq.value = 0;
2025-06-17 16:02:29 +08:00
tips.value = "请连续射出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-06-09 01:17:18 +08:00
if (msg.userId === user.value.id) {
2025-06-15 20:55:34 +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-06-09 01:17:18 +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) => {
playersScores.value[player.id] = player.arrows;
}
);
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-17 16:14:30 +08:00
uni.setStorageSync("last-battle", { ...msg.endStatus, lvl: msg.lvl });
2025-07-17 15:50:49 +08:00
if (msg.endStatus.noSaved) {
2025-07-16 16:09:10 +08:00
uni.showToast({
title: "游戏结束",
icon: "none",
});
2025-07-17 15:50:49 +08:00
uni.navigateBack();
2025-07-16 16:09:10 +08:00
} else {
uni.redirectTo({
url: `/pages/battle-result?battleId=${msg.id}`,
});
}
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-06-25 21:54:18 +08:00
if (gameType.value && teamSize.value) {
2025-07-15 12:03:14 +08:00
matchGameAPI(false, gameType.value, teamSize.value);
2025-06-05 21:32:51 +08:00
}
});
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-05 14:52:41 +08:00
<BattleHeader :players="players" />
<TestDistance v-if="!start" :guide="false" />
2025-06-25 21:54:18 +08:00
<ShootProgress
v-if="start"
: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"
:totalRound="start ? 12 : 0"
: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-06-25 21:54:18 +08:00
<Timer :seq="timerSeq" :callBack="readyToGo" />
<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>
2025-06-18 21:30:54 +08:00
<view :style="{ marginBottom: '20px' }">
2025-06-15 15:53:57 +08:00
<SButton v-if="battleId && !start" :onClick="readyToGo">准备完毕</SButton>
</view>
2025-06-05 21:32:51 +08:00
</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>