Skip to content

Commit

Permalink
Implement SaveEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
HLXII committed Jul 17, 2021
1 parent c0cb51f commit f72405b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/ig-template/IgtGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {DeveloperPanelTab} from "@/ig-template/developer-panel/DeveloperPanelTab
import {FunctionField} from "@/ig-template/developer-panel/fields/FunctionField";
import {DisplayField} from "@/ig-template/developer-panel/fields/DisplayField";
import {ChoiceField} from "@/ig-template/developer-panel/fields/ChoiceField";
import { IgtSaveEncoder } from "./tools/saving/IgtSaveEncoder";
import { DefaultSaveEncoder } from "./tools/saving/DefaultSaveEncoder";

export abstract class IgtGame {
protected _tickInterval: NodeJS.Timeout | null = null;
Expand All @@ -30,6 +32,7 @@ export abstract class IgtGame {
*/
protected readonly SAVE_INTERVAL = 30;
protected _nextSave = this.SAVE_INTERVAL;
protected saveEncoder: IgtSaveEncoder = new DefaultSaveEncoder();

protected gameSpeed = 1;
protected _lastUpdate: number = 0;
Expand Down Expand Up @@ -213,7 +216,7 @@ export abstract class IgtGame {
for (const feature of this.featureList) {
res[feature.saveKey] = feature.save()
}
LocalStorage.store(this.SAVE_KEY, res)
LocalStorage.store(this.SAVE_KEY, res, this.saveEncoder)
}

/**
Expand All @@ -227,7 +230,7 @@ export abstract class IgtGame {
* Recursively load all registered features
*/
public load(): void {
const saveData = LocalStorage.get(this.SAVE_KEY)
const saveData = LocalStorage.get(this.SAVE_KEY, this.saveEncoder);
if (saveData == null) {
return;
}
Expand Down
12 changes: 12 additions & 0 deletions src/ig-template/tools/saving/DefaultSaveEncoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IgtSaveEncoder } from "./IgtSaveEncoder";

export class DefaultSaveEncoder extends IgtSaveEncoder {

encode(data: string): string {
return data;
}
decode(data: string): string {
return data;
}

}
15 changes: 15 additions & 0 deletions src/ig-template/tools/saving/IgtSaveEncoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export abstract class IgtSaveEncoder {
/**
* Encodes the stringified save data
* @param data The save data
* @returns An encoded save string to be stored in localStorage
*/
abstract encode(data: string): string;

/**
* Decodes the save data into a JSON string
* @param data The save data
* @returns The save data in a JSON string format
*/
abstract decode(data: string): string;
}
12 changes: 8 additions & 4 deletions src/ig-template/tools/saving/LocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { IgtSaveEncoder } from "./IgtSaveEncoder";

export class LocalStorage {
public static store(key: string, data: Record<string, unknown>): void {
localStorage.setItem(key, JSON.stringify(data));
public static store(key: string, data: Record<string, unknown>, saveEncoder: IgtSaveEncoder): void {
const saveString = saveEncoder.encode(JSON.stringify(data));
localStorage.setItem(key, saveString);
}

// TODO(@Isha) add error handling here
public static get(key: string): Record<string, unknown> {
return JSON.parse(localStorage.getItem(key) as string) as Record<string, unknown>;
public static get(key: string, saveEncoder: IgtSaveEncoder): Record<string, unknown> {
const saveString = saveEncoder.decode(localStorage.getItem(key) as string);
return JSON.parse(saveString) as Record<string, unknown>;
}

public static delete(key: string): void {
Expand Down
1 change: 1 addition & 0 deletions src/ig-template/tools/saving/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {LocalStorage} from "@/ig-template/tools/saving/LocalStorage";
export {Saveable} from "@/ig-template/tools/saving/Saveable";
export {SaveData} from "@/ig-template/tools/saving/SaveData";
export {UpgradesFeatureSaveData} from "@/ig-template/tools/saving/UpgradesFeatureSaveData";
export {IgtSaveEncoder} from "@/ig-template/tools/saving/IgtSaveEncoder";

0 comments on commit f72405b

Please sign in to comment.