mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-11 02:03:58 +01:00
manMax, oneOf, subsetOf validators
This commit is contained in:
parent
d9064b608f
commit
3da3a61e20
@ -18,34 +18,16 @@ import { HacknetNodeConstants } from "./data/Constants";
|
|||||||
|
|
||||||
import { dialogBoxCreate } from "../ui/React/DialogBox";
|
import { dialogBoxCreate } from "../ui/React/DialogBox";
|
||||||
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
|
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
|
||||||
import { ObjectValidator } from "src/utils/Validator";
|
import { ObjectValidator, minMax } from "../utils/Validator";
|
||||||
|
|
||||||
export class HacknetNode implements IHacknetNode {
|
export class HacknetNode implements IHacknetNode {
|
||||||
|
|
||||||
static validationData: ObjectValidator<HacknetNode> = {
|
static validationData: ObjectValidator<HacknetNode> = {
|
||||||
cores: {
|
cores: minMax(1, 1, HacknetNodeConstants.MaxCores),
|
||||||
default: 1,
|
level: minMax(1, 1, HacknetNodeConstants.MaxLevel),
|
||||||
min: 1,
|
ram: minMax(1, 1, HacknetNodeConstants.MaxRam),
|
||||||
max: HacknetNodeConstants.MaxCores
|
onlineTimeSeconds: minMax(0, 0, Infinity),
|
||||||
},
|
totalMoneyGenerated: minMax(0, 0, Infinity)
|
||||||
level: {
|
|
||||||
default: 1,
|
|
||||||
min: 1,
|
|
||||||
max: HacknetNodeConstants.MaxLevel
|
|
||||||
},
|
|
||||||
ram: {
|
|
||||||
default: 1,
|
|
||||||
min: 1,
|
|
||||||
max: HacknetNodeConstants.MaxRam
|
|
||||||
},
|
|
||||||
onlineTimeSeconds: {
|
|
||||||
default: 0,
|
|
||||||
min: 0
|
|
||||||
},
|
|
||||||
totalMoneyGenerated: {
|
|
||||||
default: 0,
|
|
||||||
min: 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node's number of cores
|
// Node's number of cores
|
||||||
|
@ -2,30 +2,77 @@ export type ObjectValidator<T> = {
|
|||||||
[key in keyof T]?: ParameterValidator<T, keyof T>;
|
[key in keyof T]?: ParameterValidator<T, keyof T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ParameterValidator<T, U extends keyof T> {
|
interface ParameterValidatorObject<Type, Key extends keyof Type> {
|
||||||
default?: any;
|
default?: any;
|
||||||
min?: number;
|
min?: number;
|
||||||
max?: number;
|
max?: number;
|
||||||
func?: (obj: T, validator: ObjectValidator<T>, key: U) => void;
|
func?: (obj: Type, validator: ObjectValidator<Type>, key: Key) => void;
|
||||||
}
|
}
|
||||||
|
type ParameterValidatorFunction<Type, Key extends keyof Type> = (obj: Type, key: Key) => void;
|
||||||
|
type ParameterValidator<Type, Key extends keyof Type> = ParameterValidatorObject<Type, Key> | ParameterValidatorFunction<Type, Key>
|
||||||
|
|
||||||
export function validateObject<T extends Record<string, unknown>, U extends keyof T>(obj: T, validator: ObjectValidator<T>): void {
|
export function validateObject<Type extends Record<string, unknown>, Key extends keyof Type>(obj: Type, validator: ObjectValidator<Type>): void {
|
||||||
for (const key of Object.keys(validator) as U[]) {
|
for (const key of Object.keys(validator) as Key[]) {
|
||||||
const paramValidator = validator[key];
|
const paramValidator = validator[key];
|
||||||
if (paramValidator !== undefined) {
|
if (paramValidator !== undefined) {
|
||||||
if (paramValidator.func !== undefined) {
|
if (typeof paramValidator === 'function') {
|
||||||
paramValidator.func(obj, validator, key);
|
paramValidator(obj, key);
|
||||||
} else {
|
} else {
|
||||||
if ((typeof obj[key]) !== (typeof paramValidator.default)) {
|
if (paramValidator.func !== undefined) {
|
||||||
obj[key] = paramValidator.default
|
paramValidator.func(obj, validator, key);
|
||||||
}
|
} else {
|
||||||
if (typeof obj[key] === 'number' && paramValidator.min !== undefined) {
|
if ((typeof obj[key]) !== (typeof paramValidator.default)) {
|
||||||
if (obj[key] < paramValidator.min) obj[key] = paramValidator.min as T[U];
|
obj[key] = paramValidator.default
|
||||||
}
|
}
|
||||||
if (typeof obj[key] === 'number' && paramValidator.max !== undefined) {
|
if (typeof obj[key] === 'number' && paramValidator.min !== undefined) {
|
||||||
if (obj[key] > paramValidator.max) obj[key] = paramValidator.max as T[U];
|
if (obj[key] < paramValidator.min) obj[key] = paramValidator.min as Type[Key];
|
||||||
|
}
|
||||||
|
if (typeof obj[key] === 'number' && paramValidator.max !== undefined) {
|
||||||
|
if (obj[key] > paramValidator.max) obj[key] = paramValidator.max as Type[Key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function minMax<Type, Key extends keyof Type>(def: number, min: number, max: number): (obj: Type, key: Key & keyof Type) => void {
|
||||||
|
return (obj, key) => {
|
||||||
|
if (typeof obj[key] !== 'number') {
|
||||||
|
obj[key] = def as unknown as Type[Key];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((obj[key] as unknown as number) < min) {
|
||||||
|
obj[key] = min as unknown as Type[Key];
|
||||||
|
}
|
||||||
|
if ((obj[key] as unknown as number) > max) {
|
||||||
|
obj[key] = max as unknown as Type[Key];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function oneOf<Type, Key extends keyof Type, Value>(def: Value, options: Value[]): (obj: Type, key: Key & keyof Type) => void {
|
||||||
|
return (obj, key) => {
|
||||||
|
if (typeof obj[key] !== typeof def) {
|
||||||
|
obj[key] = def as unknown as Type[Key];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!options.includes(obj[key] as unknown as Value)) {
|
||||||
|
obj[key] = def as unknown as Type[Key];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function subsetOf<Type, Key extends keyof Type, Value>(options: Value[]): (obj: Type, key: Key & keyof Type) => void {
|
||||||
|
return (obj, key) => {
|
||||||
|
if (typeof obj[key] !== 'object' || !Array.isArray(obj[key])) {
|
||||||
|
obj[key] = [] as unknown as Type[Key];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const validValues: Value[] = [];
|
||||||
|
for (const value of obj[key] as unknown as Value[]) {
|
||||||
|
if (options.includes(value)) validValues.push(value);
|
||||||
|
}
|
||||||
|
obj[key] = validValues as unknown as Type[Key];
|
||||||
|
};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user