Profectus-Demo/src/util/save.ts

111 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-01-14 04:25:47 +00:00
import { fixOldSave, getInitialLayers } from "@/data/mod";
import modInfo from "@/data/modInfo.json";
2022-01-14 04:25:47 +00:00
import player, { Player, PlayerData, stringifySave } from "@/game/player";
import settings, { loadSettings } from "@/game/settings";
import Decimal from "./bignum";
2022-01-14 04:25:47 +00:00
export function setupInitialStore(player: Partial<PlayerData> = {}): asserts player is Player {
Object.assign(
{
id: `${modInfo.id}-0`,
name: "Default Save",
tabs: modInfo.initialTabs.slice(),
time: Date.now(),
autosave: true,
offlineProd: true,
2021-08-21 02:14:08 +00:00
offlineTime: new Decimal(0),
timePlayed: new Decimal(0),
keepGoing: false,
modID: modInfo.id,
modVersion: modInfo.versionNumber,
2022-01-14 04:25:47 +00:00
layers: {}
},
2022-01-14 04:25:47 +00:00
player
);
}
2022-01-14 04:25:47 +00:00
export function save(): string {
const stringifiedSave = btoa(unescape(encodeURIComponent(stringifySave(player))));
localStorage.setItem(player.id, stringifiedSave);
return stringifiedSave;
}
export async function load(): Promise<void> {
// Load global settings
loadSettings();
try {
const save = localStorage.getItem(settings.active);
if (save == null) {
await loadSave(newSave());
return;
}
2022-01-14 04:25:47 +00:00
const player = JSON.parse(decodeURIComponent(escape(atob(save))));
if (player.modID !== modInfo.id) {
await loadSave(newSave());
return;
}
2022-01-14 04:25:47 +00:00
player.id = settings.active;
await loadSave(player);
} catch (e) {
await loadSave(newSave());
}
}
export function newSave(): PlayerData {
const id = getUniqueID();
2022-01-14 04:25:47 +00:00
const player = { id };
setupInitialStore(player);
localStorage.setItem(id, btoa(unescape(encodeURIComponent(stringifySave(player)))));
settings.saves.push(id);
2022-01-14 04:25:47 +00:00
return player;
}
export function getUniqueID(): string {
let id,
i = 0;
do {
id = `${modInfo.id}-${i++}`;
} while (localStorage.getItem(id));
return id;
}
2022-01-14 04:25:47 +00:00
export async function loadSave(playerObj: Partial<PlayerData>): Promise<void> {
const { layers, removeLayer, addLayer } = await import("../game/layers");
for (const layer in layers) {
2022-01-14 04:25:47 +00:00
removeLayer(layers[layer]);
}
2022-01-14 04:25:47 +00:00
getInitialLayers(playerObj).forEach(layer => addLayer(layer, playerObj));
2022-01-14 04:25:47 +00:00
setupInitialStore(playerObj);
if (playerObj.offlineProd && playerObj.time) {
if (playerObj.offlineTime == undefined) playerObj.offlineTime = new Decimal(0);
playerObj.offlineTime = playerObj.offlineTime.add((Date.now() - playerObj.time) / 1000);
}
2022-01-14 04:25:47 +00:00
playerObj.time = Date.now();
if (playerObj.modVersion !== modInfo.versionNumber) {
fixOldSave(playerObj.modVersion, playerObj);
}
2022-01-14 04:25:47 +00:00
Object.assign(player, playerObj);
settings.active = playerObj.id;
}
setInterval(() => {
if (player.autosave) {
save();
}
}, 1000);
window.onbeforeunload = () => {
if (player.autosave) {
save();
}
};
window.save = save;
2021-08-28 16:35:25 +00:00
export const hardReset = (window.hardReset = async () => {
await loadSave(newSave());
2021-08-28 16:35:25 +00:00
});