forked from profectus/Profectus
Sync cloud saves every minute
This commit is contained in:
parent
d237201339
commit
c85bca110b
2 changed files with 6 additions and 6 deletions
|
@ -54,8 +54,8 @@ function onLoggedInChanged(g: GalaxyApi) {
|
||||||
.then(list => {
|
.then(list => {
|
||||||
const saves = syncSaves(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 our current save has under 2 minutes of playtime, load the cloud save with the most recent time.
|
||||||
if (player.timePlayed < 60 && saves.length > 0) {
|
if (player.timePlayed < 120 && saves.length > 0) {
|
||||||
const longestSave = saves.reduce((acc, curr) =>
|
const longestSave = saves.reduce((acc, curr) =>
|
||||||
acc.content.time < curr.content.time ? curr : acc
|
acc.content.time < curr.content.time ? curr : acc
|
||||||
);
|
);
|
||||||
|
@ -63,6 +63,8 @@ function onLoggedInChanged(g: GalaxyApi) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|
||||||
|
setInterval(sync, 60000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncSaves(
|
function syncSaves(
|
||||||
|
@ -123,9 +125,9 @@ function syncSaves(
|
||||||
localSave.timePlayed - cloudSave.content.timePlayed
|
localSave.timePlayed - cloudSave.content.timePlayed
|
||||||
);
|
);
|
||||||
const timeDiff = Math.abs(localSave.time - cloudSave.content.time);
|
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
|
// Otherwise, ask the player
|
||||||
if (timePlayedDiff < 60 && timeDiff < 60) {
|
if (timePlayedDiff < 120 && timeDiff < 120) {
|
||||||
if (localSave.time < cloudSave.content.time) {
|
if (localSave.time < cloudSave.content.time) {
|
||||||
save(setupInitialStore(cloudSave.content));
|
save(setupInitialStore(cloudSave.content));
|
||||||
if (settings.active === localSaveId) {
|
if (settings.active === localSaveId) {
|
||||||
|
|
|
@ -6,7 +6,6 @@ import player, { stringifySave } from "game/player";
|
||||||
import settings, { loadSettings } from "game/settings";
|
import settings, { loadSettings } from "game/settings";
|
||||||
import LZString from "lz-string";
|
import LZString from "lz-string";
|
||||||
import { ref, shallowReactive } from "vue";
|
import { ref, shallowReactive } from "vue";
|
||||||
import { sync } from "./galaxy";
|
|
||||||
|
|
||||||
export function setupInitialStore(player: Partial<Player> = {}): Player {
|
export function setupInitialStore(player: Partial<Player> = {}): Player {
|
||||||
return Object.assign(
|
return Object.assign(
|
||||||
|
@ -31,7 +30,6 @@ export function setupInitialStore(player: Partial<Player> = {}): Player {
|
||||||
export function save(playerData?: Player): string {
|
export function save(playerData?: Player): string {
|
||||||
const stringifiedSave = LZString.compressToUTF16(stringifySave(playerData ?? player));
|
const stringifiedSave = LZString.compressToUTF16(stringifySave(playerData ?? player));
|
||||||
localStorage.setItem((playerData ?? player).id, stringifiedSave);
|
localStorage.setItem((playerData ?? player).id, stringifiedSave);
|
||||||
sync();
|
|
||||||
return stringifiedSave;
|
return stringifiedSave;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue