Sync cloud saves every minute

This commit is contained in:
thepaperpilot 2024-02-18 10:51:50 -06:00
parent 919f55331d
commit f28aaa0c80
2 changed files with 6 additions and 6 deletions

View file

@ -54,8 +54,8 @@ function onLoggedInChanged(g: GalaxyApi) {
.then(list => {
const saves = syncSaves(list);
// If our current save has under a minute of playtime, load the cloud save with the most recent time.
if (player.timePlayed < 60 && saves.length > 0) {
// If our current save has under 2 minutes of playtime, load the cloud save with the most recent time.
if (player.timePlayed < 120 && saves.length > 0) {
const longestSave = saves.reduce((acc, curr) =>
acc.content.time < curr.content.time ? curr : acc
);
@ -63,6 +63,8 @@ function onLoggedInChanged(g: GalaxyApi) {
}
})
.catch(console.error);
setInterval(sync, 60000);
}
function syncSaves(
@ -123,9 +125,9 @@ function syncSaves(
localSave.timePlayed - cloudSave.content.timePlayed
);
const timeDiff = Math.abs(localSave.time - cloudSave.content.time);
// If their last played time and total time played are both within a minute, just use the newer save (very unlikely to be coincidence)
// If their last played time and total time played are both within 2 minutes, just use the newer save (very unlikely to be coincidence)
// Otherwise, ask the player
if (timePlayedDiff < 60 && timeDiff < 60) {
if (timePlayedDiff < 120 && timeDiff < 120) {
if (localSave.time < cloudSave.content.time) {
save(setupInitialStore(cloudSave.content));
if (settings.active === localSaveId) {

View file

@ -6,7 +6,6 @@ import player, { stringifySave } from "game/player";
import settings, { loadSettings } from "game/settings";
import LZString from "lz-string";
import { ref, shallowReactive } from "vue";
import { sync } from "./galaxy";
export function setupInitialStore(player: Partial<Player> = {}): Player {
return Object.assign(
@ -31,7 +30,6 @@ export function setupInitialStore(player: Partial<Player> = {}): Player {
export function save(playerData?: Player): string {
const stringifiedSave = LZString.compressToUTF16(stringifySave(playerData ?? player));
localStorage.setItem((playerData ?? player).id, stringifiedSave);
sync();
return stringifiedSave;
}