Skip to content

Commit

Permalink
Added saving on changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andev0 committed Sep 1, 2024
1 parent 3a4216c commit 1855bd0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 39 deletions.
66 changes: 65 additions & 1 deletion src/AppData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { SankeyNode } from "./Sankey/SankeyNode";

export class AppData
export class AppData extends EventTarget
{
public static onDataLoad: () => void;

public static serialize(): string
{
let data: AppData.SerializableData = { nodes: [] };
Expand Down Expand Up @@ -39,6 +41,64 @@ export class AppData
{
this._nodes[nodeIndex].connectDeserializedSlots(data.nodes[nodeIndex], nodeIds);
}

this.onDataLoad();
}

public static loadFromUrl()
{
this.isSavingEnabled = false;

let dataEncoded = location.hash.slice(1);
if (dataEncoded == ``) return;

let dataCompressedString = atob(decodeURI(dataEncoded));
let dataCompressed: number[] = [];
for (let i = 0; i < dataCompressedString.length; ++i)
{
dataCompressed.push(dataCompressedString.charCodeAt(i));
}
let dataStream = new Blob([new Uint8Array(dataCompressed)]).stream().pipeThrough(new DecompressionStream(`deflate`));

new Response(dataStream).text().then(savedData =>
{
AppData.deserialize(savedData);
});

this.isSavingEnabled = true;
}

public static saveToUrl()
{
if (this.isSavingEnabled)
{
if (AppData.nodes.length === 0)
{
location.hash = "";
return;
}

let savedData = AppData.serialize();

let dataCompressedStream = new Blob([savedData]).stream().pipeThrough(new CompressionStream(`deflate`));

new Response(dataCompressedStream).arrayBuffer().then((dataCompressed) =>
{
let dataEncoded = encodeURI(btoa(String.fromCharCode(...new Uint8Array(dataCompressed))));

location.hash = dataEncoded;
});
}
}

public static get isSavingEnabled(): boolean
{
return this._isSavingEnabled;
}

public static set isSavingEnabled(value: boolean)
{
this._isSavingEnabled = value;
}

private static dataFromArray(array: any[]): AppData.SerializableData
Expand Down Expand Up @@ -100,6 +160,8 @@ export class AppData

this._nodes.splice(index, 1);
});

this.saveToUrl();
}

public static get nodes(): SankeyNode[]
Expand Down Expand Up @@ -138,6 +200,8 @@ export class AppData
};

private static _nodes: SankeyNode[] = [];

private static _isSavingEnabled = true;
}

// Don't change positions and types of existing properties!
Expand Down
3 changes: 3 additions & 0 deletions src/MouseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Curve } from "./Geometry/Curve";
import { SvgPathBuilder } from "./SVG/SvgPathBuilder";
import { PanZoomConfiguration } from "./PanZoomConfiguration";
import { Settings } from "./Settings";
import { AppData } from "./AppData";

export class MouseHandler
{
Expand Down Expand Up @@ -81,6 +82,8 @@ export class MouseHandler

this.lastMousePos.x = 0;
this.lastMousePos.y = 0;

AppData.saveToUrl();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Sankey/SankeyLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SankeySlot } from "./Slots/SankeySlot";
import { Point } from '../Geometry/Point';
import { loadSatisfactoryResource, satisfactoryIconPath } from '../GameData/GameData';
import { LinkContextMenu } from '../ContextMenu/LinkContextMenu';
import { AppData } from "../AppData";

export class SankeyLink
{
Expand All @@ -18,6 +19,8 @@ export class SankeyLink

linksGroup.appendChild(link._svgPath);
linksGroup.appendChild(link._resourceDisplay);

AppData.saveToUrl();
}

public constructor(firstSlot: SankeySlot, secondSlot: SankeySlot, panContext: PanZoom)
Expand Down
10 changes: 10 additions & 0 deletions src/Sankey/SankeyNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,25 @@ export class SankeyNode extends EventTarget
{
for (const slotsGroup of this._inputSlotGroups)
{
AppData.isSavingEnabled = false;

slotsGroup.delete();
}

for (const slotsGroup of this._outputSlotGroups)
{
AppData.isSavingEnabled = false;

slotsGroup.delete();
}

this.nodeSvgGroup.remove();

this.dispatchEvent(new Event(SankeyNode.deletionEvent));

AppData.isSavingEnabled = true;

AppData.saveToUrl();
}

public toSerializable(): AppData.SerializableNode
Expand Down Expand Up @@ -321,6 +329,8 @@ export class SankeyNode extends EventTarget
{
this.machinesAmount = configurator.machinesAmount;
this.overclockRatio = configurator.overclockRatio;

AppData.saveToUrl();
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/Sankey/Slots/SankeySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ export abstract class SankeySlot extends EventTarget

public delete(): void
{
AppData.isSavingEnabled = false;

this.dispatchEvent(new Event(SankeySlot.deletionEvent));

this.slotSvgRect.remove();

AppData.saveToUrl();

AppData.isSavingEnabled = true;
}

public get resourcesAmount()
Expand Down
4 changes: 4 additions & 0 deletions src/Sankey/SlotsGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ export class SlotsGroup extends EventTarget
// Don't use for/for-of because of iterator invalidation after array is spliced by event.
while (this._slots.length !== 0)
{
AppData.isSavingEnabled = false;

this._slots[0].delete();
}

this._groupSvg.remove();

AppData.isSavingEnabled = true;
}

public toSerializable(): AppData.SerializableSlotsGroup
Expand Down
45 changes: 7 additions & 38 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,13 @@ async function main()
}
});

function loadFromUrl()
AppData.onDataLoad = () =>
{
let dataEncoded = location.hash.slice(1);
if (dataEncoded == ``) return;

let dataCompressedString = atob(decodeURI(dataEncoded));
let dataCompressed: number[] = [];
for (let i = 0; i < dataCompressedString.length; ++i)
for (const node of AppData.nodes)
{
dataCompressed.push(dataCompressedString.charCodeAt(i));
registerNode(node);
}
let dataStream = new Blob([new Uint8Array(dataCompressed)]).stream().pipeThrough(new DecompressionStream(`deflate`));

new Response(dataStream).text().then(savedData =>
{
AppData.deserialize(savedData);

for (const node of AppData.nodes)
{
registerNode(node);
}
});
}
};

window.addEventListener("keydown", (event) =>
{
Expand All @@ -195,27 +179,12 @@ async function main()

if (event.code === "KeyS")
{
if (AppData.nodes.length === 0)
{
location.hash = "";
return;
}

let savedData = AppData.serialize();

let dataCompressedStream = new Blob([savedData]).stream().pipeThrough(new CompressionStream(`deflate`));

new Response(dataCompressedStream).arrayBuffer().then((dataCompressed) =>
{
let dataEncoded = encodeURI(btoa(String.fromCharCode(...new Uint8Array(dataCompressed))));

location.hash = dataEncoded;
});
AppData.saveToUrl();
}

if (event.code === "KeyR")
{
loadFromUrl();
AppData.loadFromUrl();
}
});

Expand Down Expand Up @@ -320,7 +289,7 @@ async function main()
MouseHandler.getInstance().handleTouchMove(event);
});

loadFromUrl();
AppData.loadFromUrl();
}

main().catch((reason) =>
Expand Down

0 comments on commit 1855bd0

Please sign in to comment.