比赛逻辑代码统一
This commit is contained in:
@@ -4,24 +4,21 @@ import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
|
||||
import Container from "@/components/Container.vue";
|
||||
import BowTarget from "@/components/BowTarget.vue";
|
||||
import ShootProgress from "@/components/ShootProgress.vue";
|
||||
import Guide from "@/components/Guide.vue";
|
||||
import BattleHeader from "@/components/BattleHeader.vue";
|
||||
import Timer from "@/components/Timer.vue";
|
||||
import PlayerScore from "@/components/PlayerScore.vue";
|
||||
import SButton from "@/components/SButton.vue";
|
||||
import Avatar from "@/components/Avatar.vue";
|
||||
import ScreenHint from "@/components/ScreenHint.vue";
|
||||
import Matching from "@/components/Matching.vue";
|
||||
import TestDistance from "@/components/TestDistance.vue";
|
||||
import { getCurrentGameAPI, matchGameAPI } from "@/apis";
|
||||
import { getCurrentGameAPI } from "@/apis";
|
||||
import { isGameEnded } from "@/util";
|
||||
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
||||
import { MESSAGETYPES } 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("大乱斗");
|
||||
const start = ref(false);
|
||||
const startCount = ref(true);
|
||||
const battleId = ref("");
|
||||
@@ -29,12 +26,10 @@ const currentRound = ref(1);
|
||||
const power = ref(0);
|
||||
const scores = ref([]);
|
||||
const tips = ref("即将开始...");
|
||||
const seq = ref(0);
|
||||
const players = ref([]);
|
||||
const playersSorted = ref([]);
|
||||
const playersScores = ref({});
|
||||
const halfTimeTip = ref(false);
|
||||
const onComplete = ref(null);
|
||||
const isEnded = ref(false);
|
||||
|
||||
watch(
|
||||
@@ -92,44 +87,31 @@ function recoverData(battleInfo) {
|
||||
}
|
||||
|
||||
onLoad(async (options) => {
|
||||
if (options.gameMode == 1) title.value = "好友约战 - 大乱斗";
|
||||
if (options.gameMode == 2) title.value = "排位赛 - 大乱斗";
|
||||
if (options.battleId) {
|
||||
battleId.value = options.battleId;
|
||||
const players = uni.getStorageSync("melee-players");
|
||||
if (players) {
|
||||
players.value = players;
|
||||
players.value.forEach((p) => {
|
||||
playersScores.value[p.id] = [];
|
||||
});
|
||||
}
|
||||
const battleInfo = uni.getStorageSync("current-battle");
|
||||
if (battleInfo) recoverData(battleInfo);
|
||||
setTimeout(getCurrentGameAPI, 2000);
|
||||
} else {
|
||||
gameType.value = options.gameType;
|
||||
teamSize.value = options.teamSize;
|
||||
if (gameType.value && teamSize.value) {
|
||||
await matchGameAPI(true, gameType.value, teamSize.value);
|
||||
if (battleInfo) {
|
||||
recoverData(battleInfo);
|
||||
setTimeout(getCurrentGameAPI, 2000);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async function stopMatch() {
|
||||
uni.$showHint(3);
|
||||
}
|
||||
|
||||
async function onReceiveMessage(messages = []) {
|
||||
messages.forEach((msg) => {
|
||||
if (msg.constructor === MESSAGETYPES.WaitForAllReady) {
|
||||
onComplete.value = () => {
|
||||
// 这里会掉多次;
|
||||
battleId.value = msg.id;
|
||||
players.value = [
|
||||
...msg.groupUserStatus.redTeam,
|
||||
...msg.groupUserStatus.blueTeam,
|
||||
];
|
||||
players.value.forEach((p) => {
|
||||
playersScores.value[p.id] = [];
|
||||
});
|
||||
uni.$hideHint();
|
||||
};
|
||||
}
|
||||
if (msg.id !== battleId.value) return;
|
||||
if (msg.constructor === MESSAGETYPES.MeleeAllReady) {
|
||||
start.value = true;
|
||||
startCount.value = true;
|
||||
seq.value += 1;
|
||||
tips.value = scores.value.length
|
||||
? "下半场:请再射6箭"
|
||||
: "上半场:请先射6箭";
|
||||
@@ -171,11 +153,7 @@ async function onReceiveMessage(messages = []) {
|
||||
});
|
||||
}
|
||||
const onBack = () => {
|
||||
if (battleId.value) {
|
||||
uni.$showHint(2);
|
||||
} else {
|
||||
uni.$showHint(3);
|
||||
}
|
||||
uni.$showHint(2);
|
||||
};
|
||||
onMounted(() => {
|
||||
uni.setKeepScreenOn({
|
||||
@@ -188,9 +166,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 () => {
|
||||
@@ -215,64 +190,50 @@ onHide(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Container
|
||||
:title="battleId ? '大乱斗排位赛' : '搜索对手...'"
|
||||
:bgType="1"
|
||||
:onBack="onBack"
|
||||
>
|
||||
<Container :title="title" :bgType="1" :onBack="onBack">
|
||||
<view class="container">
|
||||
<block v-if="battleId">
|
||||
<BattleHeader v-if="!start" :players="players" />
|
||||
<TestDistance v-if="!start" :guide="false" />
|
||||
<ShootProgress
|
||||
:show="start"
|
||||
:seq="seq"
|
||||
:start="start && startCount"
|
||||
:tips="tips"
|
||||
:total="90"
|
||||
:melee="true"
|
||||
:battleId="battleId"
|
||||
/>
|
||||
<view v-if="start" class="user-row">
|
||||
<Avatar :src="user.avatar" :size="35" />
|
||||
<BowPower :power="power" />
|
||||
</view>
|
||||
<BowTarget
|
||||
<BattleHeader v-if="!start" :players="players" />
|
||||
<TestDistance v-if="!start" :guide="false" />
|
||||
<ShootProgress
|
||||
:show="start"
|
||||
:start="start && startCount"
|
||||
:tips="tips"
|
||||
:total="90"
|
||||
:melee="true"
|
||||
:battleId="battleId"
|
||||
/>
|
||||
<view v-if="start" class="user-row">
|
||||
<Avatar :src="user.avatar" :size="35" />
|
||||
<BowPower :power="power" />
|
||||
</view>
|
||||
<BowTarget
|
||||
v-if="start"
|
||||
:currentRound="scores.length"
|
||||
:totalRound="12"
|
||||
:scores="scores"
|
||||
:stop="!startCount"
|
||||
/>
|
||||
<view :style="{ paddingBottom: '20px' }">
|
||||
<PlayerScore
|
||||
v-if="start"
|
||||
:currentRound="scores.length"
|
||||
:totalRound="12"
|
||||
:scores="scores"
|
||||
:stop="!startCount"
|
||||
v-for="(player, index) in playersSorted"
|
||||
:key="index"
|
||||
:name="player.name"
|
||||
:avatar="player.avatar"
|
||||
:scores="playersScores[player.id] || []"
|
||||
/>
|
||||
<view :style="{ paddingBottom: '20px' }">
|
||||
<PlayerScore
|
||||
v-if="start"
|
||||
v-for="(player, index) in playersSorted"
|
||||
:key="index"
|
||||
:name="player.name"
|
||||
:avatar="player.avatar"
|
||||
:scores="playersScores[player.id] || []"
|
||||
/>
|
||||
</view>
|
||||
<Timer v-if="!start" />
|
||||
<ScreenHint
|
||||
:show="halfTimeTip"
|
||||
mode="small"
|
||||
:onClose="() => (halfTimeTip = false)"
|
||||
>
|
||||
<view class="half-time-tip">
|
||||
<text>上半场结束,休息一下吧:)</text>
|
||||
<text>20秒后开始下半场</text>
|
||||
</view>
|
||||
<Timer v-if="!start" />
|
||||
<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>
|
||||
</ScreenHint>
|
||||
</view>
|
||||
</Container>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user