优化返回游戏的数据更新
This commit is contained in:
@@ -46,6 +46,7 @@ const isFinalShoot = ref(false);
|
||||
const isEnded = ref(false);
|
||||
|
||||
function recoverData(battleInfo) {
|
||||
uni.removeStorageSync("last-awake-time");
|
||||
battleId.value = battleInfo.id;
|
||||
redTeam.value = battleInfo.redTeam;
|
||||
blueTeam.value = battleInfo.blueTeam;
|
||||
@@ -103,7 +104,7 @@ 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) {
|
||||
@@ -128,7 +129,7 @@ onLoad(async (options) => {
|
||||
if (options.battleId) {
|
||||
const battleInfo = uni.getStorageSync("current-battle");
|
||||
if (battleInfo) recoverData(battleInfo);
|
||||
uni.removeStorageSync("last-awake-time");
|
||||
setTimeout(getCurrentGameAPI, 2000);
|
||||
} else {
|
||||
gameType.value = options.gameType;
|
||||
teamSize.value = options.teamSize;
|
||||
@@ -185,6 +186,7 @@ async function onReceiveMessage(messages = []) {
|
||||
}
|
||||
}
|
||||
if (msg.constructor === MESSAGETYPES.ShootResult) {
|
||||
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);
|
||||
@@ -246,9 +248,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);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -274,14 +275,24 @@ onUnmounted(() => {
|
||||
matchGameAPI(false, gameType.value, teamSize.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>
|
||||
|
||||
Reference in New Issue
Block a user