添加游戏中途挂起,返回同步数据
This commit is contained in:
@@ -30,7 +30,6 @@ const checkBowData = () => {
|
|||||||
};
|
};
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
const battleInfo = uni.getStorageSync("last-battle");
|
const battleInfo = uni.getStorageSync("last-battle");
|
||||||
console.log("----battleInfo", battleInfo);
|
|
||||||
data.value = battleInfo;
|
data.value = battleInfo;
|
||||||
const mine = battleInfo.playerStats.find((p) => p.id === user.value.id);
|
const mine = battleInfo.playerStats.find((p) => p.id === user.value.id);
|
||||||
rank.value =
|
rank.value =
|
||||||
@@ -144,7 +143,7 @@ onUnmounted(() => {
|
|||||||
</block>
|
</block>
|
||||||
<view class="battle-e">
|
<view class="battle-e">
|
||||||
<image src="../static/row-yellow-bg.png" mode="widthFix" />
|
<image src="../static/row-yellow-bg.png" mode="widthFix" />
|
||||||
<Avatar v-if="data.mode === 1" :src="user.avatar" :size="40" />
|
<Avatar :src="user.avatar" :size="40" />
|
||||||
<text v-if="data.battleMode === 1">
|
<text v-if="data.battleMode === 1">
|
||||||
经验 {{ totalPoints > 0 ? "+" + totalPoints : totalPoints }}
|
经验 {{ totalPoints > 0 ? "+" + totalPoints : totalPoints }}
|
||||||
</text>
|
</text>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch, onMounted, onUnmounted } from "vue";
|
import { ref, watch, 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 Container from "@/components/Container.vue";
|
||||||
import PlayerSeats from "@/components/PlayerSeats.vue";
|
import PlayerSeats from "@/components/PlayerSeats.vue";
|
||||||
import Guide from "@/components/Guide.vue";
|
import Guide from "@/components/Guide.vue";
|
||||||
@@ -17,7 +17,14 @@ import ScreenHint from "@/components/ScreenHint.vue";
|
|||||||
import RoundEndTip from "@/components/RoundEndTip.vue";
|
import RoundEndTip from "@/components/RoundEndTip.vue";
|
||||||
import TestDistance from "@/components/TestDistance.vue";
|
import TestDistance from "@/components/TestDistance.vue";
|
||||||
import PlayerScore from "@/components/PlayerScore.vue";
|
import PlayerScore from "@/components/PlayerScore.vue";
|
||||||
import { getRoomAPI, destroyRoomAPI, exitRoomAPI, startRoomAPI } from "@/apis";
|
import {
|
||||||
|
getRoomAPI,
|
||||||
|
destroyRoomAPI,
|
||||||
|
exitRoomAPI,
|
||||||
|
startRoomAPI,
|
||||||
|
getCurrentGameAPI,
|
||||||
|
} from "@/apis";
|
||||||
|
import { isGameEnded } from "@/util";
|
||||||
import { MESSAGETYPES, roundsName, getMessageTypeName } from "@/constants";
|
import { MESSAGETYPES, roundsName, getMessageTypeName } from "@/constants";
|
||||||
import useStore from "@/store";
|
import useStore from "@/store";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
@@ -55,6 +62,7 @@ const halfTimeTip = ref(false);
|
|||||||
const isFinalShoot = ref(false);
|
const isFinalShoot = ref(false);
|
||||||
const total = ref(15);
|
const total = ref(15);
|
||||||
const battleType = ref(0);
|
const battleType = ref(0);
|
||||||
|
const isEnded = ref(false);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => [players.value, playersScores.value],
|
() => [players.value, playersScores.value],
|
||||||
@@ -71,120 +79,125 @@ watch(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function recoverData(battleInfo) {
|
||||||
|
battleId.value = battleInfo.id;
|
||||||
|
battleType.value = battleInfo.config.mode;
|
||||||
|
step.value = 2;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
step.value = 3;
|
||||||
|
start.value = true;
|
||||||
|
}
|
||||||
|
if (battleInfo.config.mode === 1) {
|
||||||
|
redTeam.value = battleInfo.redTeam;
|
||||||
|
blueTeam.value = battleInfo.blueTeam;
|
||||||
|
if (battleInfo.status !== 0) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (battleInfo.config.mode === 2) {
|
||||||
|
total.value = 90;
|
||||||
|
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
|
||||||
|
players.value.forEach((p) => {
|
||||||
|
playersScores.value[p.id] = [...p.arrows];
|
||||||
|
if (p.id === user.value.id) {
|
||||||
|
scores.value = [...p.arrows];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (battleInfo.status === 2) {
|
||||||
|
startCount.value = true;
|
||||||
|
const remain = Date.now() / 1000 - battleInfo.startTime;
|
||||||
|
console.log(`当前局已进行${remain}秒`);
|
||||||
|
tips.value = battleInfo.halfGame
|
||||||
|
? "下半场:请再射6箭"
|
||||||
|
: "上半场:请先射6箭";
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.$emit("update-ramain", 90 - remain);
|
||||||
|
}, 300);
|
||||||
|
} else if (battleInfo.status === 9) {
|
||||||
|
startCount.value = false;
|
||||||
|
tips.value = "准备下半场";
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.$emit("update-ramain", 0);
|
||||||
|
}, 300);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onLoad(async (options) => {
|
onLoad(async (options) => {
|
||||||
if (options.battleId) {
|
if (options.battleId) {
|
||||||
const battleInfo = uni.getStorageSync("current-battle");
|
const battleInfo = uni.getStorageSync("current-battle");
|
||||||
console.log("----battleInfo", battleInfo);
|
|
||||||
if (battleInfo) {
|
if (battleInfo) {
|
||||||
battleId.value = battleInfo.id;
|
recoverData(battleInfo);
|
||||||
battleType.value = battleInfo.config.mode;
|
uni.removeStorageSync("last-awake-time");
|
||||||
step.value = 2;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
step.value = 3;
|
|
||||||
start.value = true;
|
|
||||||
}
|
|
||||||
if (battleInfo.config.mode === 1) {
|
|
||||||
redTeam.value = battleInfo.redTeam;
|
|
||||||
blueTeam.value = battleInfo.blueTeam;
|
|
||||||
if (battleInfo.status !== 0) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (battleInfo.config.mode === 2) {
|
|
||||||
total.value = 90;
|
|
||||||
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
|
|
||||||
players.value.forEach((p) => {
|
|
||||||
playersScores.value[p.id] = [...p.arrows];
|
|
||||||
if (p.id === user.value.id) {
|
|
||||||
scores.value = [...p.arrows];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (battleInfo.status === 2) {
|
|
||||||
const remain = Date.now() / 1000 - battleInfo.startTime;
|
|
||||||
console.log(`当前局已进行${remain}秒`);
|
|
||||||
tips.value = battleInfo.halfGame
|
|
||||||
? "下半场:请再射6箭"
|
|
||||||
: "上半场:请先射6箭";
|
|
||||||
setTimeout(() => {
|
|
||||||
uni.$emit("update-ramain", 90 - remain);
|
|
||||||
}, 300);
|
|
||||||
} else if (battleInfo.status === 9) {
|
|
||||||
startCount.value = false;
|
|
||||||
tips.value = "准备下半场";
|
|
||||||
setTimeout(() => {
|
|
||||||
uni.$emit("update-ramain", 0);
|
|
||||||
}, 300);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options.roomNumber) {
|
if (options.roomNumber) {
|
||||||
@@ -439,6 +452,7 @@ async function onReceiveMessage(messages = []) {
|
|||||||
...msg.groupUserStatus.blueTeam,
|
...msg.groupUserStatus.blueTeam,
|
||||||
].forEach((player) => {
|
].forEach((player) => {
|
||||||
playersScores.value[player.id] = player.arrows;
|
playersScores.value[player.id] = player.arrows;
|
||||||
|
if (p.id === user.value.id) scores.value = [...p.arrows];
|
||||||
});
|
});
|
||||||
startCount.value = false;
|
startCount.value = false;
|
||||||
halfTimeTip.value = true;
|
halfTimeTip.value = true;
|
||||||
@@ -453,11 +467,17 @@ async function onReceiveMessage(messages = []) {
|
|||||||
});
|
});
|
||||||
uni.navigateBack();
|
uni.navigateBack();
|
||||||
} else {
|
} else {
|
||||||
|
isEnded.value = true;
|
||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: `/pages/battle-result?battleId=${msg.id}`,
|
url: `/pages/battle-result?battleId=${msg.id}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
||||||
|
if (msg.battleInfo && uni.getStorageSync("last-awake-time")) {
|
||||||
|
recoverData(msg.battleInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -503,6 +523,16 @@ onUnmounted(() => {
|
|||||||
exitRoomAPI(roomNumber.value);
|
exitRoomAPI(roomNumber.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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import Avatar from "@/components/Avatar.vue";
|
|||||||
import ScreenHint from "@/components/ScreenHint.vue";
|
import ScreenHint from "@/components/ScreenHint.vue";
|
||||||
import Matching from "@/components/Matching.vue";
|
import Matching from "@/components/Matching.vue";
|
||||||
import TestDistance from "@/components/TestDistance.vue";
|
import TestDistance from "@/components/TestDistance.vue";
|
||||||
import { matchGameAPI } from "@/apis";
|
import { getCurrentGameAPI, matchGameAPI } from "@/apis";
|
||||||
|
import { isGameEnded } from "@/util";
|
||||||
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
import { MESSAGETYPES, getMessageTypeName } from "@/constants";
|
||||||
import useStore from "@/store";
|
import useStore from "@/store";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
@@ -34,6 +35,7 @@ const playersSorted = ref([]);
|
|||||||
const playersScores = ref({});
|
const playersScores = ref({});
|
||||||
const halfTimeTip = ref(false);
|
const halfTimeTip = ref(false);
|
||||||
const onComplete = ref(null);
|
const onComplete = ref(null);
|
||||||
|
const isEnded = ref(false);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => [players.value, playersScores.value],
|
() => [players.value, playersScores.value],
|
||||||
@@ -50,45 +52,51 @@ watch(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function recoverData(battleInfo) {
|
||||||
|
battleId.value = battleInfo.id;
|
||||||
|
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
|
||||||
|
players.value.forEach((p) => {
|
||||||
|
playersScores.value[p.id] = [...p.arrows];
|
||||||
|
if (p.id === user.value.id) {
|
||||||
|
scores.value = [...p.arrows];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const remain = Date.now() / 1000 - battleInfo.startTime;
|
||||||
|
console.log(`当前局已进行${remain}秒`);
|
||||||
|
if (battleInfo.status === 0) {
|
||||||
|
if (remain > 0) {
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.$emit("update-timer", 15 - remain);
|
||||||
|
}, 200);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
start.value = true;
|
||||||
|
}
|
||||||
|
if (battleInfo.status === 2) {
|
||||||
|
startCount.value = true;
|
||||||
|
// 这里的开始时间不是游戏开始时间,而是上半场或者下半场或者中场的开始时间,还要根据状态来判断
|
||||||
|
tips.value = battleInfo.halfGame
|
||||||
|
? "下半场:请再射6箭"
|
||||||
|
: "上半场:请先射6箭";
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.$emit("update-ramain", 90 - remain);
|
||||||
|
}, 200);
|
||||||
|
} else if (battleInfo.status === 9) {
|
||||||
|
startCount.value = false;
|
||||||
|
tips.value = "准备下半场";
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.$emit("update-ramain", 0);
|
||||||
|
}, 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onLoad(async (options) => {
|
onLoad(async (options) => {
|
||||||
if (options.battleId) {
|
if (options.battleId) {
|
||||||
const battleInfo = uni.getStorageSync("current-battle");
|
const battleInfo = uni.getStorageSync("current-battle");
|
||||||
console.log("----battleInfo", battleInfo);
|
|
||||||
if (battleInfo) {
|
if (battleInfo) {
|
||||||
battleId.value = battleInfo.id;
|
recoverData(battleInfo);
|
||||||
players.value = [...battleInfo.blueTeam, ...battleInfo.redTeam];
|
console.log("remove last time");
|
||||||
players.value.forEach((p) => {
|
uni.removeStorageSync("last-awake-time");
|
||||||
playersScores.value[p.id] = [...p.arrows];
|
|
||||||
if (p.id === user.value.id) {
|
|
||||||
scores.value = [...p.arrows];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const remain = Date.now() / 1000 - battleInfo.startTime;
|
|
||||||
console.log(`当前局已进行${remain}秒`);
|
|
||||||
if (battleInfo.status === 0) {
|
|
||||||
if (remain > 0) {
|
|
||||||
setTimeout(() => {
|
|
||||||
uni.$emit("update-timer", 15 - remain);
|
|
||||||
}, 200);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
start.value = true;
|
|
||||||
}
|
|
||||||
if (battleInfo.status === 2) {
|
|
||||||
// 这里的开始时间不是游戏开始时间,而是上半场或者下半场或者中场的开始时间,还要根据状态来判断
|
|
||||||
tips.value = battleInfo.halfGame
|
|
||||||
? "下半场:请再射6箭"
|
|
||||||
: "上半场:请先射6箭";
|
|
||||||
setTimeout(() => {
|
|
||||||
uni.$emit("update-ramain", 90 - remain);
|
|
||||||
}, 200);
|
|
||||||
} else if (battleInfo.status === 9) {
|
|
||||||
startCount.value = false;
|
|
||||||
tips.value = "准备下半场";
|
|
||||||
setTimeout(() => {
|
|
||||||
uni.$emit("update-ramain", 0);
|
|
||||||
}, 200);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
gameType.value = options.gameType;
|
gameType.value = options.gameType;
|
||||||
@@ -148,6 +156,7 @@ async function onReceiveMessage(messages = []) {
|
|||||||
[...msg.groupUserStatus.redTeam, ...msg.groupUserStatus.blueTeam].forEach(
|
[...msg.groupUserStatus.redTeam, ...msg.groupUserStatus.blueTeam].forEach(
|
||||||
(player) => {
|
(player) => {
|
||||||
playersScores.value[player.id] = player.arrows;
|
playersScores.value[player.id] = player.arrows;
|
||||||
|
if (p.id === user.value.id) scores.value = [...p.arrows];
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
startCount.value = false;
|
startCount.value = false;
|
||||||
@@ -163,11 +172,17 @@ async function onReceiveMessage(messages = []) {
|
|||||||
});
|
});
|
||||||
uni.navigateBack();
|
uni.navigateBack();
|
||||||
} else {
|
} else {
|
||||||
|
isEnded.value = true;
|
||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: `/pages/battle-result?battleId=${msg.id}`,
|
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 = () => {
|
const onBack = () => {
|
||||||
@@ -177,7 +192,6 @@ const onBack = () => {
|
|||||||
uni.$showHint(3);
|
uni.$showHint(3);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
onShow(() => {});
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
uni.setKeepScreenOn({
|
uni.setKeepScreenOn({
|
||||||
keepScreenOn: true,
|
keepScreenOn: true,
|
||||||
@@ -193,6 +207,16 @@ onUnmounted(() => {
|
|||||||
matchGameAPI(false, gameType.value, teamSize.value);
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted, onUnmounted } from "vue";
|
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 Container from "@/components/Container.vue";
|
||||||
import BattleHeader from "@/components/BattleHeader.vue";
|
import BattleHeader from "@/components/BattleHeader.vue";
|
||||||
import Guide from "@/components/Guide.vue";
|
import Guide from "@/components/Guide.vue";
|
||||||
@@ -14,7 +14,8 @@ import SButton from "@/components/SButton.vue";
|
|||||||
import Matching from "@/components/Matching.vue";
|
import Matching from "@/components/Matching.vue";
|
||||||
import RoundEndTip from "@/components/RoundEndTip.vue";
|
import RoundEndTip from "@/components/RoundEndTip.vue";
|
||||||
import TestDistance from "@/components/TestDistance.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 { MESSAGETYPES, roundsName, getMessageTypeName } from "@/constants";
|
||||||
import useStore from "@/store";
|
import useStore from "@/store";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
@@ -42,89 +43,92 @@ const bluePoints = ref(0);
|
|||||||
const showRoundTip = ref(false);
|
const showRoundTip = ref(false);
|
||||||
const onComplete = ref(null);
|
const onComplete = ref(null);
|
||||||
const isFinalShoot = ref(false);
|
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) => {
|
onLoad(async (options) => {
|
||||||
if (options.battleId) {
|
if (options.battleId) {
|
||||||
const battleInfo = uni.getStorageSync("current-battle");
|
const battleInfo = uni.getStorageSync("current-battle");
|
||||||
console.log("----battleInfo", battleInfo);
|
if (battleInfo) recoverData(battleInfo);
|
||||||
if (battleInfo) {
|
uni.removeStorageSync("last-awake-time");
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
gameType.value = options.gameType;
|
gameType.value = options.gameType;
|
||||||
teamSize.value = options.teamSize;
|
teamSize.value = options.teamSize;
|
||||||
@@ -235,11 +239,17 @@ async function onReceiveMessage(messages = []) {
|
|||||||
});
|
});
|
||||||
uni.navigateBack();
|
uni.navigateBack();
|
||||||
} else {
|
} else {
|
||||||
|
isEnded.value = true;
|
||||||
uni.redirectTo({
|
uni.redirectTo({
|
||||||
url: `/pages/battle-result?battleId=${msg.id}`,
|
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 = () => {
|
const onBack = () => {
|
||||||
@@ -264,6 +274,16 @@ onUnmounted(() => {
|
|||||||
matchGameAPI(false, gameType.value, teamSize.value);
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
15
src/util.js
15
src/util.js
@@ -1,4 +1,5 @@
|
|||||||
import websocket from "@/websocket";
|
import websocket from "@/websocket";
|
||||||
|
import { isGamingAPI } from "@/apis";
|
||||||
|
|
||||||
export const formatTimestamp = (timestamp) => {
|
export const formatTimestamp = (timestamp) => {
|
||||||
const date = new Date(timestamp * 1000);
|
const date = new Date(timestamp * 1000);
|
||||||
@@ -264,3 +265,17 @@ export const wxShare = async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const isGameEnded = async () => {
|
||||||
|
const isGaming = await isGamingAPI();
|
||||||
|
if (!isGaming) {
|
||||||
|
uni.showToast({
|
||||||
|
title: "比赛已结束",
|
||||||
|
icon: "none",
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
uni.navigateBack();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
return !isGaming;
|
||||||
|
};
|
||||||
|
|||||||
@@ -28,8 +28,18 @@ function createWebSocket(token, onMessage) {
|
|||||||
const msg = data.data.updates[0];
|
const msg = data.data.updates[0];
|
||||||
if (!msg) return;
|
if (!msg) return;
|
||||||
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
if (msg.constructor === MESSAGETYPES.BackToGame) {
|
||||||
|
const pages = getCurrentPages();
|
||||||
|
const currentPage = pages[pages.length - 1];
|
||||||
|
if (
|
||||||
|
currentPage.route === "pages/battle-room" ||
|
||||||
|
currentPage.route === "pages/team-match" ||
|
||||||
|
currentPage.route === "pages/melee-match"
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const { battleInfo } = msg;
|
const { battleInfo } = msg;
|
||||||
uni.setStorageSync("current-battle", battleInfo);
|
uni.setStorageSync("current-battle", battleInfo);
|
||||||
|
console.log("----battleInfo", battleInfo);
|
||||||
// 约战
|
// 约战
|
||||||
if (battleInfo.config.battleMode === 1) {
|
if (battleInfo.config.battleMode === 1) {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
|
|||||||
Reference in New Issue
Block a user