Juice sell price
This commit is contained in:
+17
-8
@@ -1,9 +1,9 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// fluids with their data (see FluidVariant in mod_api/shared.ts) and the rules shared scripts give them, which name and
|
||||
// color a fluid by its data. each side has its own FluidRules from the same scripts, so the server's tooltips and a
|
||||
// client's drawing agree
|
||||
// fluids with their data (see FluidVariant in mod_api/shared.ts) and the rules shared scripts give them, which name,
|
||||
// color and value a fluid by its data. each side has its own FluidRules from the same scripts, so the server's tooltips
|
||||
// and a client's drawing agree
|
||||
import type { FluidApi, FluidRules as Rules, FluidVariant } from "./mod_api/shared.ts";
|
||||
import { EverythingRegistry } from "./everything_registry.ts";
|
||||
import type { FluidJson } from "./mod_data.ts";
|
||||
@@ -25,16 +25,25 @@ export class FluidRules {
|
||||
|
||||
name(fluid: FluidVariant): string {
|
||||
const json = known_fluid(fluid.id);
|
||||
const name = this.#ask(fluid, "name", "a name", (value) => typeof value === "string" && value.length > 0);
|
||||
const valid = (value: unknown): value is string => typeof value === "string" && value.length > 0;
|
||||
const name = this.#ask(fluid, "name", "a name", valid);
|
||||
return name ?? json.name ?? name_from_id(fluid.id);
|
||||
}
|
||||
|
||||
color(fluid: FluidVariant): string {
|
||||
const json = known_fluid(fluid.id);
|
||||
const valid = (value: unknown) => color_schema.safeParse(value).success;
|
||||
const valid = (value: unknown): value is string => color_schema.safeParse(value).success;
|
||||
return this.#ask(fluid, "color", 'a color like "#c42d39"', valid) ?? json.color;
|
||||
}
|
||||
|
||||
// what 1000 mB sells for, undefined when it can't be sold
|
||||
sell_value(fluid: FluidVariant): number | undefined {
|
||||
const json = known_fluid(fluid.id);
|
||||
const valid = (value: unknown): value is number =>
|
||||
typeof value === "number" && Number.isFinite(value) && value >= 0;
|
||||
return this.#ask(fluid, "sell_value", "a number of 0 or more", valid) ?? json.base_sell_value;
|
||||
}
|
||||
|
||||
// ctx.fluids for server and client scripts
|
||||
api(): FluidApi {
|
||||
return {
|
||||
@@ -46,14 +55,14 @@ export class FluidRules {
|
||||
}
|
||||
|
||||
// what the fluid's rule says, or undefined when it has none, says nothing, or says something that isn't one
|
||||
#ask(fluid: FluidVariant, rule: "name" | "color", wanted: string, valid: (value: unknown) => boolean) {
|
||||
#ask<T>(fluid: FluidVariant, rule: keyof Rules, wanted: string, valid: (value: unknown) => value is T) {
|
||||
const entry = this.#rules.get(fluid.id);
|
||||
const fn = entry?.rules[rule];
|
||||
const fn: ((data: unknown) => unknown) | undefined = entry?.rules[rule];
|
||||
if (!entry || !fn) return undefined;
|
||||
return this.run(entry.mod, `the ${rule} rule of ${fluid.id}`, () => {
|
||||
const value = fn.call(entry.rules, fluid.data);
|
||||
if (value !== undefined && !valid(value)) throw new Error(`gave ${JSON.stringify(value)}, not ${wanted}`);
|
||||
return value;
|
||||
return value as T | undefined;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user