forked from profectus/Profectus
Fix NaN checks
This commit is contained in:
parent
2affe53321
commit
d30cfe857f
12 changed files with 44 additions and 23 deletions
|
@ -288,7 +288,7 @@ export function createCollapsibleModifierSections(
|
|||
return sections;
|
||||
}
|
||||
|
||||
const collapsed = persistent<Record<number, boolean>>({});
|
||||
const collapsed = persistent<Record<number, boolean>>({}, false);
|
||||
const jsxFunc = jsx(() => {
|
||||
const sections = calculateSections();
|
||||
|
||||
|
@ -389,7 +389,7 @@ export function colorText(textToColor: string, color = "var(--accent2)"): JSX.El
|
|||
export function createCollapsibleMilestones(milestones: Record<string, GenericMilestone>) {
|
||||
// Milestones are typically defined from easiest to hardest, and we want to show hardest first
|
||||
const orderedMilestones = Object.values(milestones).reverse();
|
||||
const collapseMilestones = persistent<boolean>(true);
|
||||
const collapseMilestones = persistent<boolean>(true, false);
|
||||
const lockedMilestones = computed(() =>
|
||||
orderedMilestones.filter(m => m.earned.value === false)
|
||||
);
|
||||
|
|
|
@ -74,7 +74,7 @@ export type GenericAchievement = Replace<
|
|||
export function createAchievement<T extends AchievementOptions>(
|
||||
optionsFunc?: OptionsFunc<T, BaseAchievement, GenericAchievement>
|
||||
): Achievement<T> {
|
||||
const earned = persistent<boolean>(false);
|
||||
const earned = persistent<boolean>(false, false);
|
||||
return createLazyProxy(() => {
|
||||
const achievement = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);
|
||||
achievement.id = getUniqueID("achievement-");
|
||||
|
|
|
@ -208,11 +208,14 @@ export type GenericBoard = Replace<
|
|||
export function createBoard<T extends BoardOptions>(
|
||||
optionsFunc: OptionsFunc<T, BaseBoard, GenericBoard>
|
||||
): Board<T> {
|
||||
const state = persistent<BoardData>({
|
||||
nodes: [],
|
||||
selectedNode: null,
|
||||
selectedAction: null
|
||||
});
|
||||
const state = persistent<BoardData>(
|
||||
{
|
||||
nodes: [],
|
||||
selectedNode: null,
|
||||
selectedAction: null
|
||||
},
|
||||
false
|
||||
);
|
||||
|
||||
return createLazyProxy(() => {
|
||||
const board = optionsFunc();
|
||||
|
|
|
@ -101,7 +101,7 @@ export function createChallenge<T extends ChallengeOptions>(
|
|||
optionsFunc: OptionsFunc<T, BaseChallenge, GenericChallenge>
|
||||
): Challenge<T> {
|
||||
const completions = persistent(0);
|
||||
const active = persistent(false);
|
||||
const active = persistent(false, false);
|
||||
return createLazyProxy(() => {
|
||||
const challenge = optionsFunc();
|
||||
|
||||
|
|
|
@ -238,7 +238,7 @@ export type GenericGrid = Replace<
|
|||
export function createGrid<T extends GridOptions>(
|
||||
optionsFunc: OptionsFunc<T, BaseGrid, GenericGrid>
|
||||
): Grid<T> {
|
||||
const cellState = persistent<Record<string | number, State>>({});
|
||||
const cellState = persistent<Record<string | number, State>>({}, false);
|
||||
return createLazyProxy(() => {
|
||||
const grid = optionsFunc();
|
||||
grid.id = getUniqueID("grid-");
|
||||
|
|
|
@ -58,7 +58,7 @@ export type GenericInfobox = Replace<
|
|||
export function createInfobox<T extends InfoboxOptions>(
|
||||
optionsFunc: OptionsFunc<T, BaseInfobox, GenericInfobox>
|
||||
): Infobox<T> {
|
||||
const collapsed = persistent<boolean>(false);
|
||||
const collapsed = persistent<boolean>(false, false);
|
||||
return createLazyProxy(() => {
|
||||
const infobox = optionsFunc();
|
||||
infobox.id = getUniqueID("infobox-");
|
||||
|
|
|
@ -94,7 +94,7 @@ export type GenericMilestone = Replace<
|
|||
export function createMilestone<T extends MilestoneOptions>(
|
||||
optionsFunc?: OptionsFunc<T, BaseMilestone, GenericMilestone>
|
||||
): Milestone<T> {
|
||||
const earned = persistent<boolean>(false);
|
||||
const earned = persistent<boolean>(false, false);
|
||||
return createLazyProxy(() => {
|
||||
const milestone = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);
|
||||
milestone.id = getUniqueID("milestone-");
|
||||
|
|
|
@ -101,7 +101,7 @@ export function createTabFamily<T extends TabFamilyOptions>(
|
|||
throw "Cannot create tab family with 0 tabs";
|
||||
}
|
||||
|
||||
const selected = persistent(Object.keys(tabs)[0]);
|
||||
const selected = persistent(Object.keys(tabs)[0], false);
|
||||
return createLazyProxy(() => {
|
||||
const tabFamily = optionsFunc?.() ?? ({} as ReturnType<NonNullable<typeof optionsFunc>>);
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ export function addTooltip<T extends TooltipOptions>(
|
|||
options.pinnable = false;
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(element as any).pinned = options.pinned = persistent<boolean>(false);
|
||||
(element as any).pinned = options.pinned = persistent<boolean>(false, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ export type GenericUpgrade = Replace<
|
|||
export function createUpgrade<T extends UpgradeOptions>(
|
||||
optionsFunc: OptionsFunc<T, BaseUpgrade, GenericUpgrade>
|
||||
): Upgrade<T> {
|
||||
const bought = persistent<boolean>(false);
|
||||
const bought = persistent<boolean>(false, false);
|
||||
return createLazyProxy(() => {
|
||||
const upgrade = optionsFunc();
|
||||
upgrade.id = getUniqueID("upgrade-");
|
||||
|
|
|
@ -219,7 +219,7 @@ export function createLayer<T extends LayerOptions>(
|
|||
|
||||
addingLayers.push(id);
|
||||
persistentRefs[id] = new Set();
|
||||
layer.minimized = persistent(false);
|
||||
layer.minimized = persistent(false, false);
|
||||
Object.assign(layer, optionsFunc.call(layer as BaseLayer));
|
||||
if (
|
||||
addingLayers[addingLayers.length - 1] == null ||
|
||||
|
|
|
@ -40,6 +40,11 @@ export const NonPersistent = Symbol("NonPersistent");
|
|||
* @see {@link Persistent[SaveDataPath]}
|
||||
*/
|
||||
export const SaveDataPath = Symbol("SaveDataPath");
|
||||
/**
|
||||
* A symbol used in {@link Persistent} objects.
|
||||
* @see {@link Persistent[CheckNaN]}
|
||||
*/
|
||||
export const CheckNaN = Symbol("CheckNaN");
|
||||
|
||||
/**
|
||||
* This is a union of things that should be safely stringifiable without needing special processes or knowing what to load them in as.
|
||||
|
@ -78,6 +83,10 @@ export type Persistent<T extends State = State> = Ref<T> & {
|
|||
* The path this persistent appears in within the save data object. Predominantly used to ensure it's only placed in there one time.
|
||||
*/
|
||||
[SaveDataPath]: string[] | undefined;
|
||||
/**
|
||||
* Whether or not to NaN-check this ref. Should only be true on values expected to always be DecimalSources.
|
||||
*/
|
||||
[CheckNaN]: boolean;
|
||||
};
|
||||
|
||||
export type NonPersistent<T extends State = State> = WritableComputedRef<T> & { [DefaultValue]: T };
|
||||
|
@ -94,10 +103,7 @@ function getStackTrace() {
|
|||
|
||||
function checkNaNAndWrite<T extends State>(persistent: Persistent<T>, value: T) {
|
||||
// Decimal is smart enough to return false on things that aren't supposed to be numbers
|
||||
if (
|
||||
Decimal.isNaN(value as DecimalSource) &&
|
||||
!Decimal.isNaN(persistent.value as DecimalSource)
|
||||
) {
|
||||
if (Decimal.isNaN(value as DecimalSource)) {
|
||||
if (!state.hasNaN) {
|
||||
player.autosave = false;
|
||||
state.hasNaN = true;
|
||||
|
@ -118,8 +124,12 @@ function checkNaNAndWrite<T extends State>(persistent: Persistent<T>, value: T)
|
|||
* Create a persistent ref, which can be saved and loaded.
|
||||
* All (non-deleted) persistent refs must be included somewhere within the layer object returned by that layer's options func.
|
||||
* @param defaultValue The value the persistent ref should start at on fresh saves or when reset.
|
||||
* @param checkNaN Whether or not to check this ref for being NaN on set. Only use on refs that should always be DecimalSources.
|
||||
*/
|
||||
export function persistent<T extends State>(defaultValue: T | Ref<T>): Persistent<T> {
|
||||
export function persistent<T extends State>(
|
||||
defaultValue: T | Ref<T>,
|
||||
checkNaN = true
|
||||
): Persistent<T> {
|
||||
const persistentState: Ref<T> = isRef(defaultValue)
|
||||
? defaultValue
|
||||
: (ref<T>(defaultValue) as Ref<T>);
|
||||
|
@ -133,7 +143,11 @@ export function persistent<T extends State>(defaultValue: T | Ref<T>): Persisten
|
|||
return persistentState.value;
|
||||
},
|
||||
set(value) {
|
||||
checkNaNAndWrite(persistent, value);
|
||||
if (checkNaN) {
|
||||
checkNaNAndWrite(persistent, value);
|
||||
} else {
|
||||
persistent[PersistentState].value = value;
|
||||
}
|
||||
}
|
||||
}) as NonPersistent<T>;
|
||||
nonPersistent[DefaultValue] = defaultValue;
|
||||
|
@ -144,7 +158,11 @@ export function persistent<T extends State>(defaultValue: T | Ref<T>): Persisten
|
|||
return persistentState.value as T;
|
||||
},
|
||||
set value(value: T) {
|
||||
checkNaNAndWrite(persistent, value);
|
||||
if (checkNaN) {
|
||||
checkNaNAndWrite(persistent, value);
|
||||
} else {
|
||||
persistent[PersistentState].value = value;
|
||||
}
|
||||
},
|
||||
__v_isRef: true,
|
||||
[PersistentState]: persistentState,
|
||||
|
|
Loading…
Reference in a new issue