2026-02-21 18:31:25 -05:00
|
|
|
import { type ItemType, type WorldItem } from '../state/gameState';
|
2026-02-24 01:58:53 -05:00
|
|
|
import { CLOCK_TIME_ZONE_OPTIONS } from './types/clock';
|
|
|
|
|
import { DEFAULT_ITEM_TYPE_DEFINITIONS, DEFAULT_ITEM_TYPE_SEQUENCE } from './types';
|
2026-02-21 18:31:25 -05:00
|
|
|
|
2026-02-21 20:47:02 -05:00
|
|
|
export type ItemPropertyValueType = 'boolean' | 'text' | 'number' | 'list' | 'sound';
|
|
|
|
|
|
|
|
|
|
export type ItemPropertyMetadata = {
|
|
|
|
|
valueType?: ItemPropertyValueType;
|
|
|
|
|
tooltip?: string;
|
2026-02-22 03:50:52 -05:00
|
|
|
maxLength?: number;
|
2026-02-21 20:47:02 -05:00
|
|
|
range?: {
|
|
|
|
|
min: number;
|
|
|
|
|
max: number;
|
|
|
|
|
step?: number;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-21 19:12:58 -05:00
|
|
|
type UiDefinitionsPayload = {
|
|
|
|
|
itemTypeOrder?: ItemType[];
|
|
|
|
|
itemTypes?: Array<{
|
|
|
|
|
type: ItemType;
|
|
|
|
|
label?: string;
|
2026-02-21 20:47:02 -05:00
|
|
|
tooltip?: string;
|
2026-02-21 19:12:58 -05:00
|
|
|
editableProperties?: string[];
|
|
|
|
|
propertyOptions?: Record<string, string[]>;
|
2026-02-21 20:47:02 -05:00
|
|
|
propertyMetadata?: Record<string, unknown>;
|
2026-02-21 19:12:58 -05:00
|
|
|
globalProperties?: Record<string, unknown>;
|
|
|
|
|
}>;
|
|
|
|
|
};
|
2026-02-21 18:31:25 -05:00
|
|
|
|
2026-02-21 19:12:58 -05:00
|
|
|
let itemTypeSequence: ItemType[] = [...DEFAULT_ITEM_TYPE_SEQUENCE];
|
2026-02-24 01:58:53 -05:00
|
|
|
let itemTypeLabels: Record<ItemType, string> = {} as Record<ItemType, string>;
|
2026-02-21 20:58:16 -05:00
|
|
|
let itemTypeTooltips: Partial<Record<ItemType, string>> = {};
|
2026-02-24 01:58:53 -05:00
|
|
|
let itemTypeEditableProperties: Record<ItemType, string[]> = {} as Record<ItemType, string[]>;
|
|
|
|
|
let itemTypeGlobalProperties: Record<ItemType, Record<string, string | number | boolean>> = {} as Record<
|
|
|
|
|
ItemType,
|
|
|
|
|
Record<string, string | number | boolean>
|
|
|
|
|
>;
|
|
|
|
|
let optionItemPropertyValues: Partial<Record<string, string[]>> = {};
|
2026-02-21 20:58:16 -05:00
|
|
|
let itemTypePropertyMetadata: Partial<Record<ItemType, Record<string, ItemPropertyMetadata>>> = {};
|
2026-02-21 18:31:25 -05:00
|
|
|
|
2026-02-24 01:58:53 -05:00
|
|
|
for (const definition of DEFAULT_ITEM_TYPE_DEFINITIONS) {
|
|
|
|
|
itemTypeLabels[definition.type] = definition.label;
|
|
|
|
|
if (definition.tooltip) {
|
|
|
|
|
itemTypeTooltips[definition.type] = definition.tooltip;
|
|
|
|
|
}
|
|
|
|
|
itemTypeEditableProperties[definition.type] = [...definition.editableProperties];
|
|
|
|
|
itemTypeGlobalProperties[definition.type] = { ...definition.globalProperties };
|
|
|
|
|
if (definition.propertyMetadata) {
|
|
|
|
|
itemTypePropertyMetadata[definition.type] = { ...definition.propertyMetadata };
|
|
|
|
|
}
|
|
|
|
|
if (definition.propertyOptions) {
|
|
|
|
|
for (const [key, values] of Object.entries(definition.propertyOptions)) {
|
|
|
|
|
optionItemPropertyValues[key] = [...values];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 19:12:58 -05:00
|
|
|
export let EDITABLE_ITEM_PROPERTY_KEYS = new Set<string>(
|
|
|
|
|
Object.values(itemTypeEditableProperties).flatMap((keys) => keys),
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Rebuilds the flattened editable-key lookup after item-type definitions are replaced. */
|
2026-02-21 19:12:58 -05:00
|
|
|
function rebuildEditablePropertyKeySet(): void {
|
|
|
|
|
EDITABLE_ITEM_PROPERTY_KEYS = new Set<string>(Object.values(itemTypeEditableProperties).flatMap((keys) => keys));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Normalizes server-provided property metadata into strict client metadata shape. */
|
2026-02-21 20:47:02 -05:00
|
|
|
function normalizePropertyMetadataRecord(raw: Record<string, unknown> | undefined): Record<string, ItemPropertyMetadata> {
|
|
|
|
|
if (!raw) return {};
|
|
|
|
|
const normalized: Record<string, ItemPropertyMetadata> = {};
|
|
|
|
|
for (const [key, value] of Object.entries(raw)) {
|
|
|
|
|
if (!value || typeof value !== 'object') continue;
|
|
|
|
|
const valueObj = value as Record<string, unknown>;
|
|
|
|
|
const metadata: ItemPropertyMetadata = {};
|
|
|
|
|
if (valueObj.valueType === 'boolean' || valueObj.valueType === 'text' || valueObj.valueType === 'number' || valueObj.valueType === 'list' || valueObj.valueType === 'sound') {
|
|
|
|
|
metadata.valueType = valueObj.valueType;
|
|
|
|
|
}
|
|
|
|
|
if (typeof valueObj.tooltip === 'string' && valueObj.tooltip.trim().length > 0) {
|
|
|
|
|
metadata.tooltip = valueObj.tooltip.trim();
|
|
|
|
|
}
|
2026-02-22 03:50:52 -05:00
|
|
|
if (valueObj.maxLength !== undefined) {
|
|
|
|
|
const maxLength = Number(valueObj.maxLength);
|
|
|
|
|
if (Number.isFinite(maxLength) && maxLength > 0) {
|
|
|
|
|
metadata.maxLength = Math.floor(maxLength);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-21 20:47:02 -05:00
|
|
|
const range = valueObj.range;
|
|
|
|
|
if (range && typeof range === 'object') {
|
|
|
|
|
const rangeObj = range as Record<string, unknown>;
|
|
|
|
|
const min = Number(rangeObj.min);
|
|
|
|
|
const max = Number(rangeObj.max);
|
|
|
|
|
const step = rangeObj.step === undefined ? undefined : Number(rangeObj.step);
|
|
|
|
|
if (Number.isFinite(min) && Number.isFinite(max)) {
|
|
|
|
|
metadata.range = {
|
|
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
...(Number.isFinite(step) ? { step } : {}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
normalized[key] = metadata;
|
|
|
|
|
}
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns current timezone option list used by clock item properties. */
|
2026-02-21 19:12:58 -05:00
|
|
|
export function getClockTimeZoneOptions(): string[] {
|
2026-02-24 01:58:53 -05:00
|
|
|
return [...(optionItemPropertyValues.timeZone ?? CLOCK_TIME_ZONE_OPTIONS)];
|
2026-02-21 19:12:58 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns default timezone used by clock items when no override is set. */
|
2026-02-21 19:12:58 -05:00
|
|
|
export function getDefaultClockTimeZone(): string {
|
|
|
|
|
return getClockTimeZoneOptions()[0] ?? 'America/Detroit';
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns item-type display order for add-item menus. */
|
2026-02-21 19:12:58 -05:00
|
|
|
export function getItemTypeSequence(): ItemType[] {
|
|
|
|
|
return [...itemTypeSequence];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns global per-type property defaults provided by server/item catalog. */
|
2026-02-21 19:12:58 -05:00
|
|
|
export function getItemTypeGlobalProperties(itemType: ItemType): Record<string, string | number | boolean> {
|
|
|
|
|
return itemTypeGlobalProperties[itemType] ?? {};
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns item-type tooltip text, if defined. */
|
2026-02-21 20:47:02 -05:00
|
|
|
export function getItemTypeTooltip(itemType: ItemType): string | undefined {
|
|
|
|
|
return itemTypeTooltips[itemType];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns metadata for a given item property on a specific type. */
|
2026-02-21 20:47:02 -05:00
|
|
|
export function getItemPropertyMetadata(itemType: ItemType, key: string): ItemPropertyMetadata | undefined {
|
|
|
|
|
return itemTypePropertyMetadata[itemType]?.[key];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns option-list values for list-based properties, if defined. */
|
2026-02-21 18:31:25 -05:00
|
|
|
export function getItemPropertyOptionValues(key: string): string[] | undefined {
|
2026-02-21 19:12:58 -05:00
|
|
|
return optionItemPropertyValues[key];
|
2026-02-21 18:31:25 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns human-facing label for an item type. */
|
2026-02-21 18:31:25 -05:00
|
|
|
export function itemTypeLabel(type: ItemType): string {
|
2026-02-21 19:12:58 -05:00
|
|
|
return itemTypeLabels[type] ?? type;
|
2026-02-21 18:31:25 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns human-facing label for a property key. */
|
2026-02-21 18:31:25 -05:00
|
|
|
export function itemPropertyLabel(key: string): string {
|
|
|
|
|
if (key === 'use24Hour') return 'use 24 hour format';
|
2026-02-21 20:31:34 -05:00
|
|
|
if (key === 'emitRange') return 'emit range';
|
2026-02-21 22:38:48 -05:00
|
|
|
if (key === 'mediaVolume') return 'media volume';
|
|
|
|
|
if (key === 'emitVolume') return 'emit volume';
|
2026-02-21 23:10:17 -05:00
|
|
|
if (key === 'emitSoundSpeed') return 'emit sound speed';
|
2026-02-21 23:17:18 -05:00
|
|
|
if (key === 'emitSoundTempo') return 'emit sound tempo';
|
2026-02-21 22:55:20 -05:00
|
|
|
if (key === 'mediaChannel') return 'media channel';
|
|
|
|
|
if (key === 'mediaEffect') return 'media effect';
|
|
|
|
|
if (key === 'mediaEffectValue') return 'media effect value';
|
|
|
|
|
if (key === 'emitEffect') return 'emit effect';
|
|
|
|
|
if (key === 'emitEffectValue') return 'emit effect value';
|
2026-02-22 23:42:17 -05:00
|
|
|
if (key === 'instrument') return 'instrument';
|
2026-02-23 00:22:36 -05:00
|
|
|
if (key === 'voiceMode') return 'voice mode';
|
|
|
|
|
if (key === 'octave') return 'octave';
|
2026-02-22 23:42:17 -05:00
|
|
|
if (key === 'attack') return 'attack';
|
|
|
|
|
if (key === 'decay') return 'decay';
|
2026-02-23 00:05:01 -05:00
|
|
|
if (key === 'release') return 'release';
|
|
|
|
|
if (key === 'brightness') return 'brightness';
|
2026-02-21 22:20:15 -05:00
|
|
|
if (key === 'useSound') return 'use sound';
|
|
|
|
|
if (key === 'emitSound') return 'emit sound';
|
2026-02-21 18:31:25 -05:00
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns editable properties for one item instance/type. */
|
2026-02-21 18:31:25 -05:00
|
|
|
export function getEditableItemPropertyKeys(item: WorldItem): string[] {
|
2026-02-21 19:12:58 -05:00
|
|
|
return [...(itemTypeEditableProperties[item.type] ?? ['title'])];
|
2026-02-21 18:31:25 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Returns inspect-mode property key list (editable first, then system/global extras). */
|
2026-02-21 18:31:25 -05:00
|
|
|
export function getInspectItemPropertyKeys(item: WorldItem): string[] {
|
|
|
|
|
const editableKeys = getEditableItemPropertyKeys(item);
|
|
|
|
|
const seen = new Set(editableKeys);
|
|
|
|
|
const allKeys: string[] = [...editableKeys];
|
|
|
|
|
|
|
|
|
|
const baseKeys = [
|
|
|
|
|
'type',
|
|
|
|
|
'x',
|
|
|
|
|
'y',
|
|
|
|
|
'carrierId',
|
|
|
|
|
'version',
|
|
|
|
|
'createdBy',
|
|
|
|
|
'createdAt',
|
|
|
|
|
'updatedAt',
|
|
|
|
|
'capabilities',
|
|
|
|
|
'useSound',
|
|
|
|
|
'emitSound',
|
|
|
|
|
];
|
|
|
|
|
for (const key of baseKeys) {
|
|
|
|
|
if (seen.has(key)) continue;
|
|
|
|
|
seen.add(key);
|
|
|
|
|
allKeys.push(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const paramKeys = Object.keys(item.params).sort((a, b) => a.localeCompare(b));
|
|
|
|
|
for (const key of paramKeys) {
|
|
|
|
|
if (seen.has(key)) continue;
|
|
|
|
|
seen.add(key);
|
|
|
|
|
allKeys.push(key);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 19:12:58 -05:00
|
|
|
const globalKeys = Object.keys(itemTypeGlobalProperties[item.type] ?? {}).sort((a, b) => a.localeCompare(b));
|
2026-02-21 18:31:25 -05:00
|
|
|
for (const key of globalKeys) {
|
|
|
|
|
if (seen.has(key)) continue;
|
|
|
|
|
seen.add(key);
|
|
|
|
|
allKeys.push(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allKeys;
|
|
|
|
|
}
|
2026-02-21 19:12:58 -05:00
|
|
|
|
2026-02-22 17:23:33 -05:00
|
|
|
/** Applies server-supplied UI/catalog definitions for item types, properties, and options. */
|
2026-02-21 19:12:58 -05:00
|
|
|
export function applyServerItemUiDefinitions(uiDefinitions: UiDefinitionsPayload | undefined): void {
|
|
|
|
|
if (!uiDefinitions) return;
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(uiDefinitions.itemTypeOrder) && uiDefinitions.itemTypeOrder.length > 0) {
|
|
|
|
|
itemTypeSequence = uiDefinitions.itemTypeOrder.filter((entry) => typeof entry === 'string') as ItemType[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(uiDefinitions.itemTypes) || uiDefinitions.itemTypes.length === 0) {
|
|
|
|
|
rebuildEditablePropertyKeySet();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextLabels = { ...itemTypeLabels };
|
2026-02-21 20:47:02 -05:00
|
|
|
const nextTooltips = { ...itemTypeTooltips };
|
2026-02-21 19:12:58 -05:00
|
|
|
const nextEditable = { ...itemTypeEditableProperties };
|
|
|
|
|
const nextGlobals = { ...itemTypeGlobalProperties };
|
|
|
|
|
const nextOptions: Partial<Record<string, string[]>> = { ...optionItemPropertyValues };
|
2026-02-21 20:47:02 -05:00
|
|
|
const nextPropertyMetadata = { ...itemTypePropertyMetadata };
|
2026-02-21 19:12:58 -05:00
|
|
|
|
|
|
|
|
for (const definition of uiDefinitions.itemTypes) {
|
|
|
|
|
if (!definition || typeof definition.type !== 'string') continue;
|
|
|
|
|
const itemType = definition.type as ItemType;
|
|
|
|
|
if (typeof definition.label === 'string' && definition.label.trim()) {
|
|
|
|
|
nextLabels[itemType] = definition.label.trim();
|
|
|
|
|
}
|
2026-02-21 20:47:02 -05:00
|
|
|
if (typeof definition.tooltip === 'string' && definition.tooltip.trim()) {
|
|
|
|
|
nextTooltips[itemType] = definition.tooltip.trim();
|
|
|
|
|
}
|
2026-02-21 19:12:58 -05:00
|
|
|
if (Array.isArray(definition.editableProperties) && definition.editableProperties.length > 0) {
|
|
|
|
|
nextEditable[itemType] = definition.editableProperties.filter((entry) => typeof entry === 'string');
|
|
|
|
|
}
|
2026-02-21 20:47:02 -05:00
|
|
|
if (definition.propertyMetadata && typeof definition.propertyMetadata === 'object') {
|
|
|
|
|
nextPropertyMetadata[itemType] = normalizePropertyMetadataRecord(definition.propertyMetadata);
|
|
|
|
|
}
|
2026-02-21 19:12:58 -05:00
|
|
|
if (definition.globalProperties && typeof definition.globalProperties === 'object') {
|
|
|
|
|
const normalized: Record<string, string | number | boolean> = {};
|
|
|
|
|
for (const [key, raw] of Object.entries(definition.globalProperties)) {
|
|
|
|
|
if (typeof raw === 'string' || typeof raw === 'number' || typeof raw === 'boolean') {
|
|
|
|
|
normalized[key] = raw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
nextGlobals[itemType] = normalized;
|
|
|
|
|
}
|
|
|
|
|
if (definition.propertyOptions && typeof definition.propertyOptions === 'object') {
|
|
|
|
|
for (const [propertyKey, values] of Object.entries(definition.propertyOptions)) {
|
|
|
|
|
if (!Array.isArray(values) || values.length === 0) continue;
|
|
|
|
|
const normalizedValues = values.filter((entry) => typeof entry === 'string');
|
|
|
|
|
if (normalizedValues.length > 0) {
|
|
|
|
|
nextOptions[propertyKey] = normalizedValues;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
itemTypeLabels = nextLabels;
|
2026-02-21 20:47:02 -05:00
|
|
|
itemTypeTooltips = nextTooltips;
|
2026-02-21 19:12:58 -05:00
|
|
|
itemTypeEditableProperties = nextEditable;
|
|
|
|
|
itemTypeGlobalProperties = nextGlobals;
|
|
|
|
|
optionItemPropertyValues = nextOptions;
|
2026-02-21 20:47:02 -05:00
|
|
|
itemTypePropertyMetadata = nextPropertyMetadata;
|
2026-02-21 19:12:58 -05:00
|
|
|
rebuildEditablePropertyKeySet();
|
|
|
|
|
}
|