比赛逻辑代码统一

This commit is contained in:
kron
2025-07-28 13:54:37 +08:00
parent 0d7a421546
commit e116c8d22f
8 changed files with 290 additions and 764 deletions

View File

@@ -3,7 +3,6 @@ import { ref, onMounted, onUnmounted, nextTick } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import BattleHeader from "@/components/BattleHeader.vue";
import Guide from "@/components/Guide.vue";
import BowTarget from "@/components/BowTarget.vue";
import ShootProgress from "@/components/ShootProgress.vue";
import PlayersRow from "@/components/PlayersRow.vue";
@@ -11,18 +10,16 @@ import Timer from "@/components/Timer.vue";
import BattleFooter from "@/components/BattleFooter.vue";
import ScreenHint from "@/components/ScreenHint.vue";
import SButton from "@/components/SButton.vue";
import Matching from "@/components/Matching.vue";
import RoundEndTip from "@/components/RoundEndTip.vue";
import TestDistance from "@/components/TestDistance.vue";
import { getCurrentGameAPI, matchGameAPI } from "@/apis";
import { getCurrentGameAPI } from "@/apis";
import { isGameEnded } from "@/util";
import { MESSAGETYPES, roundsName } from "@/constants";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const gameType = ref(0);
const teamSize = ref(0);
const title = ref("1V1");
const start = ref(false);
const battleId = ref("");
const currentRound = ref(1);
@@ -36,12 +33,10 @@ const redTeam = ref([]);
const blueTeam = ref([]);
const currentShooterId = ref(0);
const tips = ref("即将开始...");
const seq = ref(0);
const roundResults = ref([]);
const redPoints = ref(0);
const bluePoints = ref(0);
const showRoundTip = ref(false);
const onComplete = ref(null);
const isFinalShoot = ref(false);
const isEnded = ref(false);
@@ -127,47 +122,33 @@ function recoverData(battleInfo) {
const blueArrows = roundResults.value[lastIndex].blueArrows;
blueScores.value = [...blueArrows].filter((item) => !!item.playerId);
}
}
// if (battleInfo.status !== 11) return;
if (battleInfo.firePlayerIndex) {
currentShooterId.value = battleInfo.firePlayerIndex;
const teamPrefix =
redTeam.value[0].id === currentShooterId.value
? "请队射箭 - "
: "请蓝队射箭 - ";
const roundSuffix = isFinalShoot.value
? "决金箭"
: `${roundsName[currentRound.value]}`;
tips.value = teamPrefix + roundSuffix;
}
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);
// if (battleInfo.status !== 11) return;
if (battleInfo.firePlayerIndex) {
currentShooterId.value = battleInfo.firePlayerIndex;
const teamPrefix =
redTeam.value[0].id === currentShooterId.value
? "请红队射箭 - "
: "请队射箭 - ";
const roundSuffix = isFinalShoot.value
? "决金箭"
: `${roundsName[currentRound.value]}`;
tips.value = teamPrefix + roundSuffix;
}
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 stopMatch() {
uni.$showHint(3);
}
async function onReceiveMessage(messages = []) {
messages.forEach((msg) => {
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
// 这里会掉多次;
if (!onComplete.value) {
onComplete.value = () => {
battleId.value = msg.id;
redTeam.value = msg.groupUserStatus.redTeam;
blueTeam.value = msg.groupUserStatus.blueTeam;
uni.$hideHint();
};
}
}
if (msg.id !== battleId.value) return;
if (msg.constructor === MESSAGETYPES.AllReady) {
start.value = true;
@@ -181,7 +162,6 @@ async function onReceiveMessage(messages = []) {
}
if (msg.constructor === MESSAGETYPES.ToSomeoneShoot) {
if (currentShooterId.value !== msg.userId) {
seq.value += 1;
currentShooterId.value = msg.userId;
const teamPrefix =
redTeam.value[0].id === currentShooterId.value
@@ -265,15 +245,16 @@ async function onReceiveMessage(messages = []) {
});
}
const onBack = () => {
if (battleId.value) {
uni.$showHint(2);
} else {
uni.$showHint(3);
}
uni.$showHint(2);
};
onLoad(async (options) => {
if (options.gameMode == 1) title.value = "好友约战 - 1V1";
if (options.gameMode == 2) title.value = "排位赛 - 1V1";
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(() => {
@@ -281,12 +262,6 @@ onLoad(async (options) => {
});
setTimeout(getCurrentGameAPI, 2000);
}
} else {
gameType.value = options.gameType;
teamSize.value = options.teamSize;
if (gameType.value && teamSize.value) {
await matchGameAPI(true, gameType.value, teamSize.value);
}
}
});
onMounted(() => {
@@ -300,9 +275,6 @@ onUnmounted(() => {
keepScreenOn: false,
});
uni.$off("socket-inbox", onReceiveMessage);
if (gameType.value && teamSize.value && !battleId.value) {
matchGameAPI(false, gameType.value, teamSize.value);
}
});
const refreshTimer = ref(null);
onShow(async () => {
@@ -327,72 +299,58 @@ onHide(() => {
</script>
<template>
<Container
:title="battleId ? '1V1排位赛' : '搜索对手...'"
:bgType="1"
:onBack="onBack"
>
<Container :title="title" :bgType="1" :onBack="onBack">
<view class="container">
<block v-if="battleId">
<BattleHeader v-if="!start" :redTeam="redTeam" :blueTeam="blueTeam" />
<TestDistance v-if="!start" :guide="false" />
<ShootProgress
:show="start"
:tips="tips"
:seq="seq"
:total="15"
:currentRound="currentRound"
:battleId="battleId"
<BattleHeader v-if="!start" :redTeam="redTeam" :blueTeam="blueTeam" />
<TestDistance v-if="!start" :guide="false" />
<ShootProgress
:show="start"
:tips="tips"
:total="15"
:currentRound="currentRound"
:battleId="battleId"
/>
<PlayersRow
v-if="start"
:currentShooterId="currentShooterId"
:blueTeam="blueTeam"
:redTeam="redTeam"
/>
<BowTarget
v-if="start"
mode="team"
:power="start ? power : 0"
:currentRound="scores.length"
:totalRound="3"
:scores="scores"
:blueScores="blueScores"
/>
<BattleFooter
v-if="start"
:roundResults="roundResults"
:redPoints="redPoints"
:bluePoints="bluePoints"
/>
<Timer v-if="!start" />
<ScreenHint
:show="showRoundTip"
:onClose="() => (showRoundTip = false)"
:mode="isFinalShoot ? 'tall' : 'normal'"
>
<RoundEndTip
v-if="showRoundTip"
:isFinal="isFinalShoot"
:round="currentRound - 1"
:bluePoint="currentBluePoint"
:redPoint="currentRedPoint"
:roundData="
roundResults[roundResults.length - 2]
? roundResults[roundResults.length - 2]
: []
"
:onAutoClose="() => (showRoundTip = false)"
/>
<PlayersRow
v-if="start"
:currentShooterId="currentShooterId"
:blueTeam="blueTeam"
:redTeam="redTeam"
/>
<BowTarget
v-if="start"
mode="team"
:power="start ? power : 0"
:currentRound="scores.length"
:totalRound="3"
:scores="scores"
:blueScores="blueScores"
/>
<BattleFooter
v-if="start"
:roundResults="roundResults"
:redPoints="redPoints"
:bluePoints="bluePoints"
/>
<Timer v-if="!start" />
<ScreenHint
:show="showRoundTip"
:onClose="() => (showRoundTip = false)"
:mode="isFinalShoot ? 'tall' : 'normal'"
>
<RoundEndTip
v-if="showRoundTip"
:isFinal="isFinalShoot"
:round="currentRound - 1"
:bluePoint="currentBluePoint"
:redPoint="currentRedPoint"
:roundData="
roundResults[roundResults.length - 2]
? roundResults[roundResults.length - 2]
: []
"
:onAutoClose="() => (showRoundTip = false)"
/>
</ScreenHint>
</block>
<block v-else>
<Matching
v-if="!battleId"
:stopMatch="stopMatch"
:onComplete="onComplete"
/>
</block>
</ScreenHint>
</view>
</Container>
</template>