Profectus-Demo/src/game/player.ts

158 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-06-23 16:30:15 +00:00
import { isPlainObject } from "is-plain-object";
2022-06-27 00:17:22 +00:00
import Decimal from "util/bignum";
import type { ProxiedWithState } from "util/proxies";
import { ProxyPath, ProxyState } from "util/proxies";
import { reactive, unref } from "vue";
2022-06-05 22:30:40 +00:00
import type { Ref } from "vue";
import transientState from "./state";
2022-07-15 05:55:36 +00:00
/** The player save data object. */
2022-01-14 04:25:47 +00:00
export interface PlayerData {
2022-07-15 05:55:36 +00:00
/** The ID of this save. */
2022-01-14 04:25:47 +00:00
id: string;
2022-07-15 05:55:36 +00:00
/** A multiplier for time passing. Set to 0 when the game is paused. */
devSpeed: number | null;
2022-07-15 05:55:36 +00:00
/** The display name of this save. */
2022-01-14 04:25:47 +00:00
name: string;
2022-07-15 05:55:36 +00:00
/** The open tabs. */
2022-01-14 04:25:47 +00:00
tabs: Array<string>;
2022-07-15 05:55:36 +00:00
/** The current time this save was last opened at, in ms since the unix epoch. */
2022-01-14 04:25:47 +00:00
time: number;
2022-07-15 05:55:36 +00:00
/** Whether or not to automatically save every couple of seconds and on tab close. */
2022-01-14 04:25:47 +00:00
autosave: boolean;
2022-07-15 05:55:36 +00:00
/** Whether or not to apply offline time when loading this save. */
2022-01-14 04:25:47 +00:00
offlineProd: boolean;
2022-07-15 05:55:36 +00:00
/** How much offline time has been accumulated and not yet processed. */
offlineTime: number | null;
2022-07-15 05:55:36 +00:00
/** How long, in ms, this game has been played. */
timePlayed: number;
2022-07-15 05:55:36 +00:00
/** Whether or not to continue playing after {@link data/projEntry.hasWon} is true. */
2022-01-14 04:25:47 +00:00
keepGoing: boolean;
2022-07-15 05:55:36 +00:00
/** The ID of this project, to make sure saves aren't imported into the wrong project. */
2022-01-14 04:25:47 +00:00
modID: string;
2022-07-15 05:55:36 +00:00
/** The version of the project this save was created by. Used for upgrading saves for new versions. */
2022-01-14 04:25:47 +00:00
modVersion: string;
2022-07-15 05:55:36 +00:00
/** A dictionary of layer save data. */
2022-06-05 22:30:40 +00:00
layers: Record<string, LayerData<unknown>>;
2022-01-14 04:25:47 +00:00
}
2022-07-15 05:55:36 +00:00
/** The proxied player that is used to track NaN values. */
2022-01-14 04:25:47 +00:00
export type Player = ProxiedWithState<PlayerData>;
2022-07-15 05:55:36 +00:00
/** A layer's save data. Automatically unwraps refs. */
2022-06-05 22:30:40 +00:00
export type LayerData<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? Record<string, LayerData<U>>
2022-06-05 22:30:40 +00:00
: T[P] extends Record<string, never>
? never
: T[P] extends Ref<infer S>
? S
: T[P] extends object
? LayerData<T[P]>
: T[P];
};
const state = reactive<PlayerData>({
id: "",
2022-01-14 04:25:47 +00:00
devSpeed: null,
name: "",
tabs: [],
time: -1,
autosave: true,
offlineProd: true,
offlineTime: null,
timePlayed: 0,
keepGoing: false,
modID: "",
modVersion: "",
layers: {}
});
2022-07-15 05:55:36 +00:00
/** Convert a player save data object into a JSON string. Unwraps refs. */
2022-01-14 04:25:47 +00:00
export function stringifySave(player: PlayerData): string {
return JSON.stringify(player, (key, value) => unref(value));
2022-01-14 04:25:47 +00:00
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const playerHandler: ProxyHandler<Record<PropertyKey, any>> = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(target: Record<PropertyKey, any>, key: PropertyKey): any {
if (key === ProxyState || key === ProxyPath) {
return target[key];
}
2022-01-25 04:25:34 +00:00
const value = target[ProxyState][key];
2022-06-23 16:30:15 +00:00
if (key !== "value" && (isPlainObject(value) || Array.isArray(value))) {
2022-01-25 04:25:34 +00:00
if (value !== target[key]?.[ProxyState]) {
2022-01-14 04:25:47 +00:00
const path = [...target[ProxyPath], key];
2022-01-25 04:25:34 +00:00
target[key] = new Proxy({ [ProxyState]: value, [ProxyPath]: path }, playerHandler);
}
return target[key];
}
2022-01-25 04:25:34 +00:00
return value;
},
set(
2022-01-14 04:25:47 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target: Record<PropertyKey, any>,
property: PropertyKey,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
receiver: ProxyConstructor
): boolean {
if (
!transientState.hasNaN &&
((typeof value === "number" && isNaN(value)) ||
(value instanceof Decimal &&
(isNaN(value.sign) || isNaN(value.layer) || isNaN(value.mag))))
) {
2022-01-14 04:25:47 +00:00
const currentValue = target[ProxyState][property];
if (
!(
(typeof currentValue === "number" && isNaN(currentValue)) ||
(currentValue instanceof Decimal &&
(isNaN(currentValue.sign) ||
isNaN(currentValue.layer) ||
isNaN(currentValue.mag)))
)
) {
state.autosave = false;
transientState.hasNaN = true;
2022-01-14 04:25:47 +00:00
transientState.NaNPath = [...target[ProxyPath], property];
2022-01-25 04:23:30 +00:00
transientState.NaNReceiver = receiver as unknown as Record<string, unknown>;
console.error(
`Attempted to set NaN value`,
2022-01-14 04:25:47 +00:00
[...target[ProxyPath], property],
target[ProxyState]
);
throw "Attempted to set NaN value. See above for details";
}
}
2022-01-14 04:25:47 +00:00
target[ProxyState][property] = value;
return true;
},
2022-01-14 04:25:47 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ownKeys(target: Record<PropertyKey, any>) {
return Reflect.ownKeys(target[ProxyState]);
},
2022-01-14 04:25:47 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
has(target: Record<PropertyKey, any>, key: string) {
return Reflect.has(target[ProxyState], key);
},
getOwnPropertyDescriptor(target, key) {
return Object.getOwnPropertyDescriptor(target[ProxyState], key);
}
};
2022-07-10 03:09:25 +00:00
declare global {
2022-07-10 05:43:52 +00:00
/** Augment the window object so the player can be accessed from the console. */
2022-07-10 03:09:25 +00:00
interface Window {
player: Player;
}
}
2022-07-15 05:55:36 +00:00
/** The player save data object. */
export default window.player = new Proxy(
2022-01-14 04:25:47 +00:00
{ [ProxyState]: state, [ProxyPath]: ["player"] },
playerHandler
) as Player;