Skip to content

Commit 4645cf9

Browse files
committed
Utilize type alias StringifiedIdentifier where applicable, rename Game properties, and rename certain handler methods
1 parent f91c657 commit 4645cf9

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
lines changed

src/ts/handler.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class Handler<T> implements Iterable<T> {
22
/** This stores the stringified Identifier as the key and then the values is obviously T. We store the identifier stringified because objects are never equal so we can't `get()` from the Map without using the same obj reference. */
3-
protected registered: Map<string, T>;
3+
protected registered: Map<StringifiedIdentifier, T>;
44
/** the number of registered items in the handler */
55
public get length(): number {
66
return this.registered.size;
@@ -11,7 +11,7 @@ export class Handler<T> implements Iterable<T> {
1111
}
1212

1313
register(identifier: Identifier, object: T) {
14-
const stringifiedIdentifier = identifier.toString();
14+
const stringifiedIdentifier: StringifiedIdentifier = identifier.toString();
1515
if (this.registered.has(stringifiedIdentifier) !== false) {
1616
console.error(`Tried to register object with Identifier "${identifier.toString()}" to a Handler, but it already exists!`);
1717
// todo ASAP: should this return?
@@ -28,7 +28,7 @@ export class Handler<T> implements Iterable<T> {
2828
console.warn(`Tried to get object from a Handler but identifier was undefined. Will return %cundefined%c.`, "font-style: italic;", "font-style: default;");
2929
return undefined;
3030
}
31-
const stringifiedIdentifier = identifier.toString();
31+
const stringifiedIdentifier: StringifiedIdentifier = identifier.toString();
3232
if (this.registered.has(stringifiedIdentifier) === false) {
3333
console.warn(`Tried to get object from a Handler with Identifier "${identifier}" that does not exist. Will return %cundefined%c.`, "font-style: italic;", "font-style: default;");
3434
}
@@ -46,11 +46,11 @@ export class Handler<T> implements Iterable<T> {
4646
}
4747

4848
/**
49-
* Gets a stringified key from a given registered value
49+
* Gets a stringified identifier from a given registered value
5050
* @param value The value to get the key of
5151
* @returns The value's key
5252
*/
53-
getKeyFromValue(value: T): string | undefined {
53+
getIdentifierFromValue(value: T): StringifiedIdentifier | undefined {
5454
for (const [key, val] of this.registered.entries()) {
5555
if (val === value) {
5656
return key;
@@ -90,7 +90,7 @@ export class UniqueKeyHandler<T> implements Iterable<T> {
9090
* If a {@link T} is registered with the given ID than return it, if it's not then return undefined
9191
* @param key The key to get from
9292
*/
93-
getFromIdentifier(key: string): T | undefined {
93+
getFromKey(key: string): T | undefined {
9494
if (key === undefined) {
9595
console.warn(`Tried to get object from a UniqueKeyHandler but key was undefined. Will return %cundefined%c.`, "font-style: italic;", "font-style: default;");
9696
return undefined;
@@ -107,7 +107,7 @@ export class UniqueKeyHandler<T> implements Iterable<T> {
107107
* @param value The value to get the key of
108108
* @returns The value's key
109109
*/
110-
getKeyFromValue(value: T): string | undefined {
110+
getKeyFromValue(value: T): StringifiedIdentifier | undefined {
111111
for (const [key, val] of this.registered.entries()) {
112112
if (val === value) {
113113
return key;
@@ -127,7 +127,7 @@ export class Identifier {
127127
* @param stringified The stringified Identifier
128128
* @returns A new {@link Identifier}
129129
*/
130-
static fromString(stringified: string): Identifier | undefined {
130+
static fromString(stringified: StringifiedIdentifier): Identifier | undefined {
131131
const regex = /\w+:\w+/;
132132
if (!regex.test(stringified)) {
133133
console.warn(`Cannot construct a new Identifier from invalid string "${stringified}". Returning %cundefined%c.`, "font-style: italic;", "font-style: default;");
@@ -149,7 +149,7 @@ export class Identifier {
149149
/**
150150
* @returns Format: `namespace:uid`
151151
*/
152-
toString(): string {
152+
toString(): StringifiedIdentifier {
153153
return `${this.namespace}:${this.uid}`;
154154
}
155155

@@ -163,4 +163,7 @@ export class Identifier {
163163
else
164164
return false;
165165
}
166-
}
166+
}
167+
168+
/** An alias for a `string` that exists to make typing more clear to the user whenever a stringified identifier is involved, as there are obviously requirements expected in that case that need to be clearly shown to the user to avoid errors. */
169+
export type StringifiedIdentifier = string;

src/ts/handlers.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//* Ideally I would put things like BackgroundHandler in personalization.ts but because of lexical declarations we can't do that without getting circular imports as far as I can tell. This is fine.
22

33
import { Building, BuildingSave } from "./buildings.js";
4-
import { Handler, Identifier, UniqueKeyHandler } from "./handler.js";
4+
import { Handler, Identifier, StringifiedIdentifier, UniqueKeyHandler } from "./handler.js";
55
import { url } from "./helper.js";
66
import { Game } from "./main.js";
77
import { Mod } from "./mods.js";
@@ -39,7 +39,7 @@ export class BackgroundHandler extends Handler<Background> {
3939
* This method kinda sucks but we need it to work this way because of how {@link HTMLSelectElement}s work
4040
* @param value The UID of whatever {@link Background} we want from {@link backgroundHandler}. Usually `backgroundSelect`'s `value`
4141
*/
42-
setBackground(stringifiedIdentifier: string) {
42+
setBackground(stringifiedIdentifier: StringifiedIdentifier) {
4343
const foundObject = Handlers.BACKGROUND.getFromIdentifier(Identifier.fromString(stringifiedIdentifier));
4444
if (foundObject) {
4545
this.currentBackground = foundObject;
@@ -89,7 +89,7 @@ export class CurrentlyClickedHandler extends Handler<CurrentlyClickedObject> {
8989
* This method kinda sucks but we need it to work this way because of how {@link HTMLSelectElement}s work
9090
* @param stringifiedIdentifier The stringified identifier of whatever {@link Background} we want from {@link currentlyClickedHandler}. Usually `currentlyClickedSelect`'s `value`
9191
*/
92-
setCurrentlyClicked(stringifiedIdentifier: string): void {
92+
setCurrentlyClicked(stringifiedIdentifier: StringifiedIdentifier): void {
9393
const foundObject = Handlers.CURRENTLY_CLICKED.getFromIdentifier(Identifier.fromString(stringifiedIdentifier));
9494
if (foundObject) {
9595
this.currentlyClicked = foundObject;
@@ -146,9 +146,9 @@ export class BuildingHandler extends Handler<Building> {
146146
* @returns savedata as obj in following format: `stringifiedIdentifier: BuildingSave`
147147
*/
148148
dumpSave(namespace: string=undefined) {
149-
const saveObj: Record<string, BuildingSave> = {};
149+
const saveObj: Record<StringifiedIdentifier, BuildingSave> = {};
150150
for (const value of (namespace === undefined) ? this : this.getValuesFromNamespace(namespace)) {
151-
saveObj[this.getKeyFromValue(value)] = value.getSaveData();
151+
saveObj[this.getIdentifierFromValue(value)] = value.getSaveData();
152152
}
153153
return saveObj;
154154
}
@@ -158,7 +158,7 @@ export class BuildingHandler extends Handler<Building> {
158158
* @param saveObj The save object to load the data of
159159
* @param namespace Optional: if present will only load data for buildings of a given namespace.
160160
*/
161-
loadSave(saveObj: Record<string, BuildingSave>, namespace: string=undefined) {
161+
loadSave(saveObj: Record<StringifiedIdentifier, BuildingSave>, namespace: string=undefined) {
162162
for (const stringifiedIdentifier in saveObj) {
163163
const id = Identifier.fromString(stringifiedIdentifier);
164164

@@ -233,9 +233,9 @@ export class UpgradeHandler extends Handler<Upgrade> {
233233
* @returns savedata as obj in following format: `stringifiedIdentifier: UpgradeSave`
234234
*/
235235
dumpSave(namespace: string=undefined) {
236-
const saveObj: Record<string, UpgradeSave> = {};
236+
const saveObj: Record<StringifiedIdentifier, UpgradeSave> = {};
237237
for (const value of (namespace === undefined) ? this : this.getValuesFromNamespace(namespace)) {
238-
saveObj[this.getKeyFromValue(value)] = value.getSaveData();
238+
saveObj[this.getIdentifierFromValue(value)] = value.getSaveData();
239239
}
240240
return saveObj;
241241
}
@@ -245,7 +245,7 @@ export class UpgradeHandler extends Handler<Upgrade> {
245245
* @param saveObj The save object to load the data of
246246
* @param namespace Optional: if present will only load data for upgrades of a given namespace.
247247
*/
248-
loadSave(saveObj: Record<string, UpgradeSave>, namespace: string=undefined) {
248+
loadSave(saveObj: Record<StringifiedIdentifier, UpgradeSave>, namespace: string=undefined) {
249249
for (const stringifiedIdentifier in saveObj) {
250250
const id = Identifier.fromString(stringifiedIdentifier);
251251

@@ -277,7 +277,7 @@ export class ModHandler extends UniqueKeyHandler<Mod<any>> {
277277
(document.getElementById("addModURLForm") as HTMLFormElement).reset();
278278
document.getElementById("importedMessage").style.display = "block";
279279

280-
Game.getInstance().isModded = true;
280+
Game.getInstance().modded = true;
281281

282282
console.log("Loaded mod from URL: "+url);
283283
}
@@ -301,7 +301,7 @@ export class ModHandler extends UniqueKeyHandler<Mod<any>> {
301301
(document.getElementById("addModURLForm") as HTMLFormElement).reset();
302302
document.getElementById("importedMessage")!.style.display = "block";
303303

304-
Game.getInstance().isModded = true;
304+
Game.getInstance().modded = true;
305305

306306
console.log("Successfully added mod from file: "+file.name);
307307
};
@@ -378,7 +378,7 @@ export class ModHandler extends UniqueKeyHandler<Mod<any>> {
378378
*/
379379
override register(key: string, mod: Mod<any>) {
380380
//! below will always warn, should that be changed?
381-
if (this.getFromIdentifier(key) !== undefined) { //* do this before registering so we can get a more user-friendly popup than the console.error that we usually get for this type of error
381+
if (this.getFromKey(key) !== undefined) { //* do this before registering so we can get a more user-friendly popup than the console.error that we usually get for this type of error
382382
new SimplePopup({x: 400, y: 200, title: "Error", text: `The mod namespace "${key}" is already present!`});
383383
return;
384384
}

src/ts/main.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Handlers, ModHandler } from "./handlers.js";
1717
import { BuildingSave } from "./buildings.js";
1818

1919
import { NewMod } from "./exmod.js"; //* note: this import is intentionally left unused so tsc can find this file and compile it
20+
import { StringifiedIdentifier } from "./handler.js";
2021

2122
// ------------------------------------
2223
// Version Constants
@@ -65,8 +66,8 @@ export enum VersionBranch {
6566
type MiddleState = "none" | "stats" | "info" | "options";
6667

6768
export interface GameSaveData {
68-
hasCheated: boolean;
69-
isModded: boolean;
69+
cheated: boolean;
70+
modded: boolean;
7071
autoSavingAllowed: boolean;
7172

7273
// personalization
@@ -88,8 +89,8 @@ export class Game implements SaveProvider<GameSaveData> {
8889
"potatman4": "Playtesting",
8990
"Wolfsarecool44": "Playtesting, Emotional Support"
9091
}
91-
public static readonly DEFAULT_BACKGROUND: string = "clickercookie:blue";
92-
public static readonly DEFAULT_CURRENTLY_CLICKED_OBJECT: string = "clickercookie:cookie";
92+
public static readonly DEFAULT_BACKGROUND: StringifiedIdentifier = "clickercookie:blue";
93+
public static readonly DEFAULT_CURRENTLY_CLICKED_OBJECT: StringifiedIdentifier = "clickercookie:cookie";
9394

9495
public static getMousePosition(): MousePosition {
9596
return mousePos;
@@ -122,17 +123,17 @@ export class Game implements SaveProvider<GameSaveData> {
122123
public clickercookie: ClickerCookie;
123124

124125
// self-explainatory-ish things
125-
private _hasCheated: boolean;
126-
public get hasCheated() { return this._hasCheated }
127-
public set hasCheated(bool: boolean) {
128-
this._hasCheated = bool;
129-
document.getElementById("ifCheatedStat").style.display = (this._hasCheated) ? "block" : "none";
126+
private _cheated: boolean;
127+
public get cheated() { return this._cheated }
128+
public set cheated(bool: boolean) {
129+
this._cheated = bool;
130+
document.getElementById("ifCheatedStat").style.display = (this._cheated) ? "block" : "none";
130131
}
131-
private _isModded: boolean;
132-
public get isModded() { return this._isModded }
133-
public set isModded(bool: boolean) {
134-
this._isModded = bool;
135-
document.getElementById("ifModdedStat").style.display = (this._isModded) ? "block" : "none";
132+
private _modded: boolean;
133+
public get modded() { return this._modded }
134+
public set modded(bool: boolean) {
135+
this._modded = bool;
136+
document.getElementById("ifModdedStat").style.display = (this._modded) ? "block" : "none";
136137
}
137138
private _autoSavingAllowed: boolean;
138139
public get autoSavingAllowed(): boolean { return this._autoSavingAllowed }
@@ -171,8 +172,8 @@ export class Game implements SaveProvider<GameSaveData> {
171172
y: 0
172173
};
173174

174-
this.hasCheated = false;
175-
this.isModded = false;
175+
this.cheated = false;
176+
this.modded = false;
176177
this.autoSavingAllowed = true;
177178

178179
this.clickercookie = new ClickerCookie();
@@ -369,13 +370,13 @@ export class Game implements SaveProvider<GameSaveData> {
369370
const originalBuildingSave = (this.savinator5000.getLocalStorageSave()) ? (this.savinator5000.getLocalStorageSave().getData("game") as GameSaveData).buildingsSave : {};
370371

371372
return {
372-
hasCheated: this.hasCheated,
373-
isModded: this.isModded,
373+
cheated: this.cheated,
374+
modded: this.modded,
374375
autoSavingAllowed: this.autoSavingAllowed,
375376

376377
// personalization
377-
currentlyClickedObjectKey: Handlers.CURRENTLY_CLICKED.getKeyFromValue(Handlers.CURRENTLY_CLICKED.getCurrentlyClicked()),
378-
currentBackgroundKey: Handlers.BACKGROUND.getKeyFromValue(Handlers.BACKGROUND.getCurrentBackground()), // todo: make better
378+
currentlyClickedObjectKey: Handlers.CURRENTLY_CLICKED.getIdentifierFromValue(Handlers.CURRENTLY_CLICKED.getCurrentlyClicked()),
379+
currentBackgroundKey: Handlers.BACKGROUND.getIdentifierFromValue(Handlers.BACKGROUND.getCurrentBackground()), // todo: make better
379380

380381
upgradesSave: { // merge to preserve unregistered upgrades
381382
...originalUpgradeSave,
@@ -389,8 +390,8 @@ export class Game implements SaveProvider<GameSaveData> {
389390
}
390391

391392
loadSaveData(saveData: GameSaveData): void {
392-
this.hasCheated = saveData.hasCheated;
393-
this.isModded = saveData.isModded;
393+
this.cheated = saveData.cheated;
394+
this.modded = saveData.modded;
394395
this.autoSavingAllowed = saveData.autoSavingAllowed;
395396

396397
Handlers.CURRENTLY_CLICKED.setCurrentlyClicked(saveData.currentlyClickedObjectKey);

src/ts/saving.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export class Savinator {
100100
console.log(`Loaded save data for "${namespace}" namespace.`);
101101
}
102102
}
103-
if (this.saveHandler.getFromIdentifier("game") !== undefined) { // game should always be there, but just in case it isn't we check
104-
this.saveHandler.getFromIdentifier("game").loadSaveData(localStorageSave.getData("game"));
103+
if (this.saveHandler.getFromKey("game") !== undefined) { // game should always be there, but just in case it isn't we check
104+
this.saveHandler.getFromKey("game").loadSaveData(localStorageSave.getData("game"));
105105
console.log(`Loaded save data for game namespace.`);
106106
}
107107

@@ -378,8 +378,8 @@ export function convert06Save(save: string) {
378378

379379
game.clickercookie.cookiesPerClick = parsedSave["core.cookiesPerClick"];
380380
game.clickercookie.cookieBeenClickedTimes = parsedSave["core.cookieBeenClickedTimes"];
381-
game.hasCheated = !!parsedSave["hasCheated"];
382-
game.isModded = !!parsedSave["isModded"];
381+
game.cheated = !!parsedSave["hasCheated"];
382+
game.modded = !!parsedSave["isModded"];
383383
// no won
384384

385385
game.savinator5000.preserveUnusedNamespacesInSaves = false;

0 commit comments

Comments
 (0)