Files
shoot-miniprograms/src/components/HeaderProgress.vue

187 lines
5.5 KiB
Vue
Raw Normal View History

2025-08-13 16:44:25 +08:00
<script setup>
2025-08-25 13:47:32 +08:00
import { ref, watch, onMounted, onBeforeUnmount } from "vue";
2025-08-13 16:44:25 +08:00
import audioManager from "@/audioManager";
import { MESSAGETYPES } from "@/constants";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
2025-08-14 10:50:44 +08:00
const tips = ref("");
const melee = ref(false);
2025-08-13 16:44:25 +08:00
const timer = ref(null);
const sound = ref(true);
const currentSound = ref("");
2025-09-15 11:23:22 +08:00
const currentRound = ref(0);
2025-08-13 16:44:25 +08:00
const currentRoundEnded = ref(false);
const ended = ref(false);
const halfTime = ref(false);
2025-09-03 16:34:54 +08:00
const currentShot = ref(0);
const totalShot = ref(0);
2025-08-13 16:44:25 +08:00
2025-08-14 10:50:44 +08:00
watch(
() => tips.value,
(newVal) => {
let key = "";
2025-11-10 09:26:51 +08:00
if (newVal.includes("重回")) return;
2025-08-20 13:53:14 +08:00
if (newVal.includes("红队")) key = "请红方射箭";
if (newVal.includes("蓝队")) key = "请蓝方射箭";
2025-09-15 11:23:22 +08:00
if (!sound.value) return;
if (currentRoundEnded.value) {
currentRound.value += 1;
// 播放当前轮次语音
audioManager.play(
`${["一", "二", "三", "四", "五"][currentRound.value - 1]}`
);
2025-08-14 10:50:44 +08:00
}
2025-09-15 11:23:22 +08:00
// 延迟播放队伍提示音
setTimeout(
() => {
2025-11-08 11:33:03 +08:00
if (key) audioManager.play(newVal.includes("你") ? "轮到你了" : key);
2025-09-15 11:23:22 +08:00
currentRoundEnded.value = false;
},
currentRoundEnded.value ? 1000 : 0
);
2025-08-14 10:50:44 +08:00
}
);
2025-08-13 16:44:25 +08:00
const updateSound = () => {
sound.value = !sound.value;
if (!sound.value) audioManager.stop(currentSound.value);
};
async function onReceiveMessage(messages = []) {
if (!sound.value || ended.value) return;
messages.forEach((msg) => {
2025-08-18 16:09:11 +08:00
if (msg.constructor === MESSAGETYPES.ShootResult) {
2025-08-13 16:44:25 +08:00
if (melee.value && msg.userId !== user.value.id) return;
2025-09-03 16:34:54 +08:00
if (msg.userId === user.value.id) currentShot.value++;
2025-11-06 16:08:02 +08:00
if (msg.battleInfo && msg.userId === user.value.id) {
const players = [
...(msg.battleInfo.blueTeam || []),
...(msg.battleInfo.redTeam || []),
];
const currentPlayer = players.find((p) => p.id === msg.userId);
currentShot.value = 0;
try {
if (
currentPlayer &&
currentPlayer.shotHistory &&
currentPlayer.shotHistory[msg.battleInfo.currentRound]
) {
currentShot.value =
currentPlayer.shotHistory[msg.battleInfo.currentRound].length;
}
} catch (_) {}
}
2025-08-13 16:44:25 +08:00
if (!halfTime.value && msg.target) {
currentSound.value = msg.target.ring
? `${msg.target.ring}`
: "未上靶";
audioManager.play(currentSound.value);
}
2025-08-21 16:15:29 +08:00
} else if (msg.constructor === MESSAGETYPES.InvalidShot) {
2025-08-27 18:23:59 +08:00
if (msg.userId === user.value.id) {
uni.showToast({
title: "距离不足,无效",
icon: "none",
});
audioManager.play("射击无效");
}
2025-08-13 16:44:25 +08:00
} else if (msg.constructor === MESSAGETYPES.AllReady) {
2025-09-03 16:34:54 +08:00
const { config } = msg.groupUserStatus;
if (config && config.mode === 1) {
totalShot.value = config.teamSize === 2 ? 3 : 2;
}
2025-08-15 11:23:23 +08:00
currentRoundEnded.value = true;
2025-08-13 16:44:25 +08:00
audioManager.play("比赛开始");
} else if (msg.constructor === MESSAGETYPES.MeleeAllReady) {
2025-08-14 10:50:44 +08:00
melee.value = true;
2025-08-13 16:44:25 +08:00
halfTime.value = false;
audioManager.play("比赛开始");
} else if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
2025-09-03 16:34:54 +08:00
currentShot.value = 0;
2025-08-15 11:23:23 +08:00
if (msg.preRoundResult && msg.preRoundResult.currentRound) {
2025-09-15 11:23:22 +08:00
currentRound.value = msg.preRoundResult.currentRound;
2025-08-15 11:23:23 +08:00
currentRoundEnded.value = true;
}
2025-08-13 16:44:25 +08:00
} else if (msg.constructor === MESSAGETYPES.HalfTimeOver) {
halfTime.value = true;
audioManager.play("中场休息");
} else if (msg.constructor === MESSAGETYPES.MatchOver) {
audioManager.play("比赛结束");
} else if (msg.constructor === MESSAGETYPES.FinalShoot) {
2025-09-03 16:34:54 +08:00
totalShot.value = 0;
2025-08-13 16:44:25 +08:00
audioManager.play("决金箭轮");
2025-08-21 16:15:29 +08:00
tips.value = "即将开始...";
2025-09-18 09:28:14 +08:00
currentRoundEnded.value = false;
2025-08-13 16:44:25 +08:00
} else if (msg.constructor === MESSAGETYPES.MatchOver) {
ended.value = true;
2025-08-18 16:09:11 +08:00
} else if (msg.constructor === MESSAGETYPES.BackToGame) {
if (msg.battleInfo) {
2025-08-19 18:42:22 +08:00
melee.value = msg.battleInfo.config.mode === 2;
2025-08-18 16:09:11 +08:00
}
2025-08-13 16:44:25 +08:00
}
});
}
const playSound = (key) => {
currentSound.value = key;
audioManager.play(key);
};
2025-08-14 10:50:44 +08:00
const onUpdateTips = (newVal) => {
tips.value = newVal;
};
2025-09-03 16:34:54 +08:00
const onUpdateTotalShot = (newVal) => {
currentShot.value = newVal.currentShot;
totalShot.value = newVal.totalShot;
};
2025-08-13 16:44:25 +08:00
onMounted(() => {
2025-09-03 16:34:54 +08:00
uni.$on("update-shot", onUpdateTotalShot);
2025-08-14 10:50:44 +08:00
uni.$on("update-tips", onUpdateTips);
2025-08-13 16:44:25 +08:00
uni.$on("socket-inbox", onReceiveMessage);
uni.$on("play-sound", playSound);
});
2025-08-25 13:47:32 +08:00
onBeforeUnmount(() => {
2025-09-03 16:34:54 +08:00
uni.$off("update-shot", onUpdateTotalShot);
2025-08-13 16:44:25 +08:00
uni.$off("socket-inbox", onReceiveMessage);
uni.$off("play-sound", playSound);
if (timer.value) clearInterval(timer.value);
});
</script>
<template>
<view class="container">
2025-11-10 09:26:51 +08:00
<text>{{ (tips || "").replace(/你/g, "").replace(/重回/g, "") }}</text>
2025-09-03 16:34:54 +08:00
<text v-if="totalShot > 0"> ({{ currentShot }}/{{ totalShot }}) </text>
2025-08-15 11:23:23 +08:00
<button v-if="!!tips" hover-class="none" @click="updateSound">
2025-08-13 16:44:25 +08:00
<image
:src="`../static/sound${sound ? '' : '-off'}-yellow.png`"
mode="widthFix"
/>
</button>
</view>
</template>
<style scoped>
.container {
width: 50vw;
color: #fed847;
display: flex;
align-items: center;
justify-content: center;
}
.container > button:last-child {
width: 36px;
height: 36px;
}
.container > button:last-child > image {
width: 36px;
min-height: 36px;
}
</style>