优化返回游戏的数据更新

This commit is contained in:
kron
2025-07-23 11:18:47 +08:00
parent b87ccbb7b0
commit be4619ee24
8 changed files with 94 additions and 31 deletions

View File

@@ -80,6 +80,7 @@ watch(
);
function recoverData(battleInfo) {
uni.removeStorageSync("last-awake-time");
battleId.value = battleInfo.id;
battleType.value = battleInfo.config.mode;
step.value = 2;
@@ -144,7 +145,8 @@ function recoverData(battleInfo) {
});
}
}
if (battleInfo.status !== 11) return;
// 这个状态不准
// if (battleInfo.status !== 11) return;
if (battleInfo.firePlayerIndex) {
currentShooterId.value = battleInfo.firePlayerIndex;
if (redTeam.value[0].id === currentShooterId.value) {
@@ -193,10 +195,8 @@ function recoverData(battleInfo) {
onLoad(async (options) => {
if (options.battleId) {
const battleInfo = uni.getStorageSync("current-battle");
if (battleInfo) {
recoverData(battleInfo);
uni.removeStorageSync("last-awake-time");
}
if (battleInfo) recoverData(battleInfo);
setTimeout(getCurrentGameAPI, 2000);
}
if (options.roomNumber) {
roomNumber.value = options.roomNumber;
@@ -383,6 +383,8 @@ async function onReceiveMessage(messages = []) {
}
if (msg.constructor === MESSAGETYPES.ShootResult) {
if (battleType.value === 1) {
// 会有蓝队射箭时间,红队射箭也有结果返回的情况
if (currentShooterId.value !== msg.userId) return;
const isRed = redTeam.value.find((item) => item.id === msg.userId);
if (isRed) scores.value.push(msg.target);
else blueScores.value.push(msg.target);
@@ -472,9 +474,8 @@ async function onReceiveMessage(messages = []) {
}
}
if (msg.constructor === MESSAGETYPES.BackToGame) {
if (msg.battleInfo && uni.getStorageSync("last-awake-time")) {
recoverData(msg.battleInfo);
}
uni.$emit("update-header-loading", false);
if (msg.battleInfo) recoverData(msg.battleInfo);
}
}
});
@@ -521,14 +522,24 @@ onUnmounted(() => {
exitRoomAPI(roomNumber.value);
}
});
const refreshTimer = ref(null);
onShow(async () => {
if (battleId.value) {
if (!isEnded.value && (await isGameEnded())) return;
const lastAwakeTime = uni.getStorageSync("last-awake-time");
if (lastAwakeTime) await getCurrentGameAPI();
getCurrentGameAPI();
const refreshData = () => {
const lastAwakeTime = uni.getStorageSync("last-awake-time");
if (lastAwakeTime) {
getCurrentGameAPI();
} else {
clearInterval(refreshTimer.value);
}
};
refreshTimer.value = setInterval(refreshData, 2000);
}
});
onHide(() => {
if (refreshTimer.value) clearInterval(refreshTimer.value);
uni.setStorageSync("last-awake-time", Date.now());
});
</script>
@@ -608,7 +619,7 @@ onHide(() => {
<ShootProgress
:tips="tips"
:seq="seq"
:start="start && startCount"
:start="players.length > 0 && start && startCount"
:total="total"
:currentRound="currentRound"
:battleId="battleId"