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

220 lines
7.0 KiB
Vue
Raw Normal View History

2025-05-16 15:56:54 +08:00
<script setup>
2025-06-05 21:32:51 +08:00
import { ref, onMounted, onUnmounted } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
2025-06-08 20:59:41 +08:00
import BattleHeader from "@/components/BattleHeader.vue";
import Guide from "@/components/Guide.vue";
2025-05-16 15:56:54 +08:00
import BowTarget from "@/components/BowTarget.vue";
import ShootProgress from "@/components/ShootProgress.vue";
import PlayersRow from "@/components/PlayersRow.vue";
2025-06-05 21:32:51 +08:00
import Timer from "@/components/Timer.vue";
2025-06-06 00:34:54 +08:00
import BattleFooter from "@/components/BattleFooter.vue";
2025-06-19 21:03:33 +08:00
import ScreenHint from "@/components/ScreenHint.vue";
2025-06-05 21:32:51 +08:00
import SButton from "@/components/SButton.vue";
import { matchGameAPI, readyGameAPI } from "@/apis";
2025-06-17 16:02:29 +08:00
import { MESSAGETYPES, roundsName, getMessageTypeName } from "@/constants";
2025-06-08 20:59:41 +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 matching = ref(false);
const start = ref(false);
const battleId = ref("");
const currentRound = ref(1);
2025-06-19 21:03:33 +08:00
const currentRedPoint = ref(0);
const currentBluePoint = ref(0);
2025-06-05 21:32:51 +08:00
const totalRounds = ref(0);
const power = ref(0);
const scores = ref([]);
const redTeam = ref([]);
const blueTeam = ref([]);
const currentShooterId = ref(0);
const tips = ref("即将开始...");
const seq = ref(0);
const timerSeq = ref(0);
2025-06-06 00:34:54 +08:00
const roundResults = ref([
// {
// blueArrows: [{ ring: 2 }, { ring: 2 }],
// redArrows: [{ ring: 2 }, { ring: 3 }],
// },
]);
2025-06-08 20:59:41 +08:00
const redPoints = ref(0);
const bluePoints = ref(0);
2025-06-19 21:03:33 +08:00
const showRoundTip = ref(false);
2025-06-05 21:32:51 +08:00
onLoad((options) => {
gameType.value = options.gameType;
teamSize.value = options.teamSize;
});
async function startMatch() {
if (gameType.value && teamSize.value) {
2025-06-08 20:59:41 +08:00
scores.value = [];
2025-06-05 21:32:51 +08:00
await matchGameAPI(true, gameType.value, teamSize.value);
2025-06-19 21:03:33 +08:00
matching.value = true;
2025-06-05 21:32:51 +08:00
}
}
async function stopMatch() {
if (gameType.value && teamSize.value) {
await matchGameAPI(false, gameType.value, teamSize.value);
2025-06-19 21:03:33 +08:00
matching.value = false;
2025-06-05 21:32:51 +08:00
}
}
async function readyToGo() {
if (battleId.value) {
await readyGameAPI(battleId.value);
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
}
2025-06-08 12:52:49 +08:00
if (!start.value && msg.constructor === MESSAGETYPES.ShootSyncMeArrowID) {
scores.value.push(msg.target);
power.value = msg.target.battery;
}
2025-06-05 21:32:51 +08:00
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
// 这里会掉多次;
timerSeq.value += 1;
battleId.value = msg.id;
redTeam.value = msg.groupUserStatus.redTeam;
blueTeam.value = msg.groupUserStatus.blueTeam;
}
if (msg.id !== battleId.value) return;
if (msg.constructor === MESSAGETYPES.AllReady) {
start.value = true;
timerSeq.value = 0;
2025-06-08 20:59:41 +08:00
scores.value = [];
2025-06-13 14:05:30 +08:00
totalRounds.value = msg.groupUserStatus.config.maxRounds;
2025-06-05 21:32:51 +08:00
}
if (msg.constructor === MESSAGETYPES.ToSomeoneShoot) {
scores.value = [];
seq.value += 1;
currentShooterId.value = msg.userId;
if (redTeam.value[0].id === currentShooterId.value) {
tips.value = `请红队射箭-第${roundsName[currentRound.value]}`;
} else {
tips.value = `请蓝队射箭-第${roundsName[currentRound.value]}`;
}
}
if (msg.constructor === MESSAGETYPES.ShootResult) {
scores.value = [msg.target];
}
2025-06-08 20:59:41 +08:00
if (msg.constructor === MESSAGETYPES.RoundPoint) {
bluePoints.value += msg.blueScore;
redPoints.value += msg.redScore;
}
2025-06-05 21:32:51 +08:00
if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
2025-06-06 00:34:54 +08:00
const result = msg.preRoundResult;
2025-06-05 21:32:51 +08:00
scores.value = [];
currentShooterId.value = 0;
2025-06-19 21:03:33 +08:00
currentBluePoint.value = result.blueScore;
currentRedPoint.value = result.redScore;
showRoundTip.value = true;
// 开始下一轮;
roundResults.value = result.roundResults;
currentRound.value = result.currentRound + 1;
2025-06-05 21:32:51 +08:00
}
if (msg.constructor === MESSAGETYPES.MatchOver) {
uni.redirectTo({
2025-06-08 20:59:41 +08:00
url: `/pages/battle-result?battleId=${msg.id}&winner=${msg.endStatus.winner}`,
2025-06-05 21:32:51 +08:00
});
}
});
}
onMounted(() => {
uni.$on("socket-inbox", onReceiveMessage);
});
onUnmounted(() => {
uni.$off("socket-inbox", onReceiveMessage);
2025-06-19 21:03:33 +08:00
if (matching.value) {
2025-06-05 21:32:51 +08:00
matchGameAPI(false, gameType.value, teamSize.value);
}
});
2025-05-16 15:56:54 +08:00
</script>
<template>
2025-06-09 01:17:18 +08:00
<Container title="1V1排位赛" :bgType="1">
2025-06-05 21:32:51 +08:00
<view class="container">
2025-06-15 15:53:57 +08:00
<BattleHeader
v-if="!start && redTeam.length && blueTeam.length"
:redTeam="redTeam"
:blueTeam="blueTeam"
/>
2025-06-24 13:18:03 +08:00
<Guide noBg v-if="!start && battleId">
2025-06-08 20:59:41 +08:00
<view :style="{ display: 'flex', justifyContent: 'space-between' }">
<view :style="{ display: 'flex', flexDirection: 'column' }">
<text :style="{ color: '#fed847' }">请预先射几箭测试</text>
<text>请确保射击距离只有5米</text>
</view>
<BowPower :power="45" />
</view>
</Guide>
2025-06-24 13:18:03 +08:00
<ShootProgress v-if="start" :tips="tips" :seq="seq" :total="15" />
2025-06-05 21:32:51 +08:00
<PlayersRow
v-if="start"
:currentShooterId="currentShooterId"
:blueTeam="blueTeam"
:redTeam="redTeam"
2025-05-16 15:56:54 +08:00
/>
2025-06-05 21:32:51 +08:00
<BowTarget
2025-06-24 13:18:03 +08:00
v-if="battleId"
2025-06-08 20:59:41 +08:00
:showE="start && user.id === currentShooterId"
:power="start ? power : 0"
2025-06-05 21:32:51 +08:00
:currentRound="currentRound"
:totalRound="totalRounds"
:scores="scores"
2025-06-08 12:52:49 +08:00
:tips="
!start && scores.length > 0
? `本次射程${scores[scores.length - 1].dst / 100}米,${
scores[scores.length - 1].dst / 100 >= 5 ? '已' : '未'
}达到距离要求`
: ''
"
2025-06-05 21:32:51 +08:00
/>
2025-06-06 00:34:54 +08:00
<BattleFooter
2025-06-08 20:59:41 +08:00
v-if="start"
2025-06-06 00:34:54 +08:00
:roundResults="roundResults"
2025-06-08 20:59:41 +08:00
:redPoints="redPoints"
:bluePoints="bluePoints"
2025-06-06 00:34:54 +08:00
/>
2025-06-05 21:32:51 +08:00
<Timer :seq="timerSeq" :callBack="readyToGo" />
2025-06-19 21:03:33 +08:00
<ScreenHint :show="showRoundTip" :onClose="() => (showRoundTip = false)">
<view class="round-end-tip">
<text>{{ currentRound - 1 }}轮射击结束</text>
<view>
<text>蓝队</text>
<text>{{ currentBluePoint }}</text>
<text>红队</text>
<text>{{ currentRedPoint }}</text>
<text></text>
</view>
<!-- <text>同分僵局最后一箭定江山</text>
<text>10 秒后蓝红双方 决金箭 一箭决胜负</text> -->
</view>
</ScreenHint>
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" :onClick="matching ? stopMatch : startMatch">{{
matching ? "取消匹配" : "开始匹配"
}}</SButton>
<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%;
}
</style>