First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
import { fixOldSave, getInitialLayers, getStartingData } from "@/data/mod";
|
|
|
|
import modInfo from "@/data/modInfo.json";
|
|
|
|
import player from "@/game/player";
|
2021-09-05 23:53:04 +00:00
|
|
|
import settings, { loadSettings } from "@/game/settings";
|
|
|
|
import state from "@/game/state";
|
|
|
|
import { PlayerData } from "@/typings/player";
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
import Decimal from "./bignum";
|
|
|
|
|
|
|
|
export function getInitialStore(playerData: Partial<PlayerData> = {}): PlayerData {
|
|
|
|
return applyPlayerData(
|
|
|
|
{
|
|
|
|
id: `${modInfo.id}-0`,
|
|
|
|
points: new Decimal(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),
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
timePlayed: new Decimal(0),
|
|
|
|
keepGoing: false,
|
|
|
|
subtabs: {},
|
|
|
|
minimized: {},
|
|
|
|
modID: modInfo.id,
|
|
|
|
modVersion: modInfo.versionNumber,
|
|
|
|
layers: {},
|
2021-08-28 16:35:25 +00:00
|
|
|
justLoaded: false,
|
2021-09-05 23:53:04 +00:00
|
|
|
...getStartingData()
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
},
|
|
|
|
playerData
|
|
|
|
) as PlayerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function save(): void {
|
2021-09-05 23:53:04 +00:00
|
|
|
state.saveToExport = btoa(unescape(encodeURIComponent(JSON.stringify(player.__state))));
|
|
|
|
localStorage.setItem(player.id, state.saveToExport);
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function load(): Promise<void> {
|
2021-09-05 23:53:04 +00:00
|
|
|
// Load global settings
|
|
|
|
loadSettings();
|
|
|
|
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
try {
|
2021-09-05 23:53:04 +00:00
|
|
|
const save = localStorage.getItem(settings.active);
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
if (save == null) {
|
|
|
|
await loadSave(newSave());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const playerData = JSON.parse(decodeURIComponent(escape(atob(save))));
|
|
|
|
if (playerData.modID !== modInfo.id) {
|
|
|
|
await loadSave(newSave());
|
|
|
|
return;
|
|
|
|
}
|
2021-09-05 23:53:04 +00:00
|
|
|
playerData.id = settings.active;
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
await loadSave(playerData);
|
|
|
|
} catch (e) {
|
|
|
|
await loadSave(newSave());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function newSave(): PlayerData {
|
|
|
|
const id = getUniqueID();
|
|
|
|
const playerData = getInitialStore({ id });
|
|
|
|
localStorage.setItem(id, btoa(unescape(encodeURIComponent(JSON.stringify(playerData)))));
|
|
|
|
|
2021-09-05 23:53:04 +00:00
|
|
|
settings.saves.push(id);
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
|
|
|
|
return playerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUniqueID(): string {
|
|
|
|
let id,
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
id = `${modInfo.id}-${i++}`;
|
|
|
|
} while (localStorage.getItem(id));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadSave(playerData: Partial<PlayerData>): Promise<void> {
|
|
|
|
const { layers, removeLayer, addLayer } = await import("../game/layers");
|
|
|
|
|
|
|
|
for (const layer in layers) {
|
|
|
|
removeLayer(layer);
|
|
|
|
}
|
|
|
|
getInitialLayers(playerData).forEach(layer => addLayer(layer, playerData));
|
|
|
|
|
|
|
|
playerData = getInitialStore(playerData);
|
|
|
|
if (playerData.offlineProd && playerData.time) {
|
|
|
|
if (playerData.offlineTime == undefined) playerData.offlineTime = new Decimal(0);
|
|
|
|
playerData.offlineTime = playerData.offlineTime.add((Date.now() - playerData.time) / 1000);
|
|
|
|
}
|
|
|
|
playerData.time = Date.now();
|
|
|
|
if (playerData.modVersion !== modInfo.versionNumber) {
|
|
|
|
fixOldSave(playerData.modVersion, playerData);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(player, playerData);
|
|
|
|
for (const prop in player) {
|
|
|
|
if (!(prop in playerData) && !(prop in layers) && prop !== "__state" && prop !== "__path") {
|
|
|
|
delete player.layers[prop];
|
|
|
|
}
|
|
|
|
}
|
2021-08-28 16:35:25 +00:00
|
|
|
player.justLoaded = true;
|
2021-09-05 23:53:04 +00:00
|
|
|
settings.active = player.id;
|
First pass at typescript support
Oh man did this end up requiring a *ton* of other work as well.
There's still a few typing issues I still can't quite work out,
and others I'd like to improve when I have time. In fact, this version
doesn't even really work, it has a stack overflow error caused by
a tooltip for some reason have a tree inside it, which in turn has
another tooltip, etc. There's also 17 errors that I *really* feel like
shouldn't be there, but they are, and 113 warnings - mostly using !
to assert that things are non-null. Lots of work left to do, to sum up.
The reason I'm committing this now is because I really need to get to
work on my game jam, and since it won't use a tree or really many of
TMT-X's features, I can get away with using a broken engine :)
2021-08-17 04:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function applyPlayerData<T extends Record<string, any>>(
|
|
|
|
target: T,
|
|
|
|
source: T,
|
|
|
|
destructive = false
|
|
|
|
): T {
|
|
|
|
for (const prop in source) {
|
|
|
|
if (target[prop] == null) {
|
|
|
|
target[prop] = source[prop];
|
|
|
|
} else if (target[prop as string] instanceof Decimal) {
|
|
|
|
target[prop as keyof T] = new Decimal(source[prop]) as any;
|
|
|
|
} else if (Array.isArray(target[prop]) || typeof target[prop] === "object") {
|
|
|
|
target[prop] = applyPlayerData(target[prop], source[prop], destructive);
|
|
|
|
} else {
|
|
|
|
target[prop] = source[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (destructive) {
|
|
|
|
for (const prop in target) {
|
|
|
|
if (!(prop in source)) {
|
|
|
|
delete target[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 () => {
|
2021-08-22 07:00:38 +00:00
|
|
|
await loadSave(newSave());
|
2021-08-28 16:35:25 +00:00
|
|
|
});
|