添加游戏中途挂起,返回同步数据
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script setup>
|
||||
import { ref, onMounted, onUnmounted } from "vue";
|
||||
import { onLoad } from "@dcloudio/uni-app";
|
||||
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";
|
||||
@@ -14,7 +14,8 @@ 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 { matchGameAPI } from "@/apis";
|
||||
import { getCurrentGameAPI, matchGameAPI } from "@/apis";
|
||||
import { isGameEnded } from "@/util";
|
||||
import { MESSAGETYPES, roundsName, getMessageTypeName } from "@/constants";
|
||||
import useStore from "@/store";
|
||||
import { storeToRefs } from "pinia";
|
||||
@@ -42,89 +43,92 @@ const bluePoints = ref(0);
|
||||
const showRoundTip = ref(false);
|
||||
const onComplete = ref(null);
|
||||
const isFinalShoot = ref(false);
|
||||
const isEnded = ref(false);
|
||||
|
||||
function recoverData(battleInfo) {
|
||||
battleId.value = battleInfo.id;
|
||||
redTeam.value = battleInfo.redTeam;
|
||||
blueTeam.value = battleInfo.blueTeam;
|
||||
if (battleInfo.status === 0) {
|
||||
const readyRemain = Date.now() / 1000 - battleInfo.startTime;
|
||||
console.log(`当前局已进行${readyRemain}秒`);
|
||||
if (readyRemain > 0) {
|
||||
setTimeout(() => {
|
||||
uni.$emit("update-timer", 15 - readyRemain);
|
||||
}, 200);
|
||||
}
|
||||
} else {
|
||||
start.value = true;
|
||||
currentRound.value = battleInfo.currentRound;
|
||||
totalRounds.value = battleInfo.maxRound;
|
||||
roundResults.value = battleInfo.roundResults;
|
||||
battleInfo.roundResults.forEach((round) => {
|
||||
if (round.blueTotal && round.redTotal) {
|
||||
if (round.blueTotal === round.redTotal) {
|
||||
bluePoints.value += 1;
|
||||
redPoints.value += 1;
|
||||
} else if (round.blueTotal > round.redTotal) {
|
||||
bluePoints.value += 2;
|
||||
} else {
|
||||
redPoints.value += 2;
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
if (battleInfo.roundResults[battleInfo.roundResults.length - 1]) {
|
||||
scores.value = battleInfo.roundResults[
|
||||
battleInfo.roundResults.length - 1
|
||||
].redArrows.filter((item) => !!item.playerId);
|
||||
blueScores.value = battleInfo.roundResults[
|
||||
battleInfo.roundResults.length - 1
|
||||
].blueArrows.filter((item) => !!item.playerId);
|
||||
}
|
||||
}, 300);
|
||||
if (
|
||||
battleInfo.redTeam[0].shotHistory[battleInfo.currentRound] ||
|
||||
battleInfo.blueTeam[0].shotHistory[battleInfo.currentRound]
|
||||
) {
|
||||
roundResults.value.push({
|
||||
redArrows: battleInfo.redTeam[0].shotHistory[
|
||||
battleInfo.currentRound
|
||||
].filter((item) => !!item.playerId),
|
||||
blueArrows: battleInfo.blueTeam[0].shotHistory[
|
||||
battleInfo.currentRound
|
||||
].filter((item) => !!item.playerId),
|
||||
});
|
||||
} else if (battleInfo.currentRound < 5) {
|
||||
roundResults.value.push({
|
||||
redArrows: [],
|
||||
blueArrows: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
if (battleInfo.status !== 11) return;
|
||||
if (battleInfo.firePlayerIndex) {
|
||||
currentShooterId.value = battleInfo.firePlayerIndex;
|
||||
if (redTeam.value[0].id === currentShooterId.value) {
|
||||
tips.value = `请红队射箭-第${roundsName[currentRound.value]}轮`;
|
||||
} else {
|
||||
tips.value = `请蓝队射箭-第${roundsName[currentRound.value]}轮`;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onLoad(async (options) => {
|
||||
if (options.battleId) {
|
||||
const battleInfo = uni.getStorageSync("current-battle");
|
||||
console.log("----battleInfo", battleInfo);
|
||||
if (battleInfo) {
|
||||
battleId.value = battleInfo.id;
|
||||
redTeam.value = battleInfo.redTeam;
|
||||
blueTeam.value = battleInfo.blueTeam;
|
||||
if (battleInfo.status === 0) {
|
||||
const readyRemain = Date.now() / 1000 - battleInfo.startTime;
|
||||
console.log(`当前局已进行${readyRemain}秒`);
|
||||
if (readyRemain > 0) {
|
||||
setTimeout(() => {
|
||||
uni.$emit("update-timer", 15 - readyRemain);
|
||||
}, 200);
|
||||
}
|
||||
} else {
|
||||
start.value = true;
|
||||
currentRound.value = battleInfo.currentRound;
|
||||
totalRounds.value = battleInfo.maxRound;
|
||||
roundResults.value = battleInfo.roundResults;
|
||||
battleInfo.roundResults.forEach((round) => {
|
||||
if (round.blueTotal && round.redTotal) {
|
||||
if (round.blueTotal === round.redTotal) {
|
||||
bluePoints.value += 1;
|
||||
redPoints.value += 1;
|
||||
} else if (round.blueTotal > round.redTotal) {
|
||||
bluePoints.value += 2;
|
||||
} else {
|
||||
redPoints.value += 2;
|
||||
}
|
||||
}
|
||||
});
|
||||
setTimeout(() => {
|
||||
if (battleInfo.roundResults[battleInfo.roundResults.length - 1]) {
|
||||
scores.value = battleInfo.roundResults[
|
||||
battleInfo.roundResults.length - 1
|
||||
].redArrows.filter((item) => !!item.playerId);
|
||||
blueScores.value = battleInfo.roundResults[
|
||||
battleInfo.roundResults.length - 1
|
||||
].blueArrows.filter((item) => !!item.playerId);
|
||||
}
|
||||
}, 300);
|
||||
if (
|
||||
battleInfo.redTeam[0].shotHistory[battleInfo.currentRound] ||
|
||||
battleInfo.blueTeam[0].shotHistory[battleInfo.currentRound]
|
||||
) {
|
||||
roundResults.value.push({
|
||||
redArrows: battleInfo.redTeam[0].shotHistory[
|
||||
battleInfo.currentRound
|
||||
].filter((item) => !!item.playerId),
|
||||
blueArrows: battleInfo.blueTeam[0].shotHistory[
|
||||
battleInfo.currentRound
|
||||
].filter((item) => !!item.playerId),
|
||||
});
|
||||
} else if (battleInfo.currentRound < 5) {
|
||||
roundResults.value.push({
|
||||
redArrows: [],
|
||||
blueArrows: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
if (battleInfo.status !== 11) return;
|
||||
if (battleInfo.firePlayerIndex) {
|
||||
currentShooterId.value = battleInfo.firePlayerIndex;
|
||||
if (redTeam.value[0].id === currentShooterId.value) {
|
||||
tips.value = `请红队射箭-第${roundsName[currentRound.value]}轮`;
|
||||
} else {
|
||||
tips.value = `请蓝队射箭-第${roundsName[currentRound.value]}轮`;
|
||||
}
|
||||
}
|
||||
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) recoverData(battleInfo);
|
||||
uni.removeStorageSync("last-awake-time");
|
||||
} else {
|
||||
gameType.value = options.gameType;
|
||||
teamSize.value = options.teamSize;
|
||||
@@ -235,11 +239,17 @@ async function onReceiveMessage(messages = []) {
|
||||
});
|
||||
uni.navigateBack();
|
||||
} else {
|
||||
isEnded.value = true;
|
||||
uni.redirectTo({
|
||||
url: `/pages/battle-result?battleId=${msg.id}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
||||
if (msg.battleInfo && uni.getStorageSync("last-awake-time")) {
|
||||
recoverData(msg.battleInfo);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
const onBack = () => {
|
||||
@@ -264,6 +274,16 @@ onUnmounted(() => {
|
||||
matchGameAPI(false, gameType.value, teamSize.value);
|
||||
}
|
||||
});
|
||||
onShow(async () => {
|
||||
if (battleId.value) {
|
||||
if (!isEnded.value && (await isGameEnded())) return;
|
||||
const lastAwakeTime = uni.getStorageSync("last-awake-time");
|
||||
if (lastAwakeTime) await getCurrentGameAPI();
|
||||
}
|
||||
});
|
||||
onHide(() => {
|
||||
uni.setStorageSync("last-awake-time", Date.now());
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user