Add Galaxy cloud saves support #75
1 changed files with 23 additions and 13 deletions
|
@ -53,9 +53,10 @@ function onLoggedInChanged(g: GalaxyApi) {
|
||||||
g.getSaveList()
|
g.getSaveList()
|
||||||
.then(list => {
|
.then(list => {
|
||||||
const saves = syncSaves(list);
|
const saves = syncSaves(list);
|
||||||
|
syncedSaves.value = saves.map(s => s.content.id);
|
||||||
|
|
||||||
// If our current save has under 2 minutes 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 < 120 && saves.length > 0) {
|
if (player.timePlayed < 120 * 1000 && 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
|
||||||
);
|
);
|
||||||
|
@ -64,7 +65,7 @@ function onLoggedInChanged(g: GalaxyApi) {
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|
||||||
setInterval(sync, 60000);
|
setInterval(sync, 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncSaves(
|
function syncSaves(
|
||||||
|
@ -83,7 +84,11 @@ function syncSaves(
|
||||||
.map(slot => {
|
.map(slot => {
|
||||||
const { label, content } = list[slot as unknown as number];
|
const { label, content } = list[slot as unknown as number];
|
||||||
try {
|
try {
|
||||||
return { slot: parseInt(slot), label, content: JSON.parse(content) };
|
return {
|
||||||
|
slot: parseInt(slot),
|
||||||
|
label,
|
||||||
|
content: JSON.parse(decodeSave(content) ?? "")
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -127,7 +132,7 @@ function syncSaves(
|
||||||
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 2 minutes, 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 < 120 && timeDiff < 120) {
|
if (timePlayedDiff < 120 * 1000 && timeDiff < 120 * 1000) {
|
||||||
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) {
|
||||||
|
@ -138,9 +143,9 @@ function syncSaves(
|
||||||
?.save(
|
?.save(
|
||||||
cloudSave.slot,
|
cloudSave.slot,
|
||||||
LZString.compressToUTF16(
|
LZString.compressToUTF16(
|
||||||
stringifySave(setupInitialStore(cloudSave.content))
|
stringifySave(setupInitialStore(localSave))
|
||||||
),
|
),
|
||||||
cloudSave.label
|
localSave.name ?? cloudSave.label
|
||||||
)
|
)
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
// Update cloud save content for the return value
|
// Update cloud save content for the return value
|
||||||
|
@ -162,13 +167,18 @@ function syncSaves(
|
||||||
});
|
});
|
||||||
|
|
||||||
savesToUpload.forEach(id => {
|
savesToUpload.forEach(id => {
|
||||||
const localSave = decodeSave(localStorage.getItem(id) ?? "");
|
try {
|
||||||
if (localSave != null && availableSlots.size > 0) {
|
if (availableSlots.size > 0) {
|
||||||
const parsedLocalSave = JSON.parse(localSave);
|
const localSave = localStorage.getItem(id) ?? "";
|
||||||
const slot = parseInt(Object.keys(availableSlots)[0]);
|
const parsedLocalSave = JSON.parse(decodeSave(localSave) ?? "");
|
||||||
galaxy.value?.save(slot, localSave, parsedLocalSave.name).catch(console.error);
|
const slot = availableSlots.values().next().value;
|
||||||
availableSlots.delete(slot);
|
galaxy.value
|
||||||
}
|
?.save(slot, localSave, parsedLocalSave.name)
|
||||||
|
.then(() => syncedSaves.value.push(parsedLocalSave.id))
|
||||||
|
.catch(console.error);
|
||||||
|
availableSlots.delete(slot);
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
return saves;
|
return saves;
|
||||||
|
|
Loading…
Reference in a new issue