2021-06-22 19:25:41 -04:00
|
|
|
import { Serializable } from 'frigid';
|
2021-06-26 03:11:18 -04:00
|
|
|
import { getTheme } from '@themes';
|
2021-06-22 19:25:41 -04:00
|
|
|
import { Renderable } from '../ui/UI.js';
|
|
|
|
|
|
|
|
|
|
export type ItemID = string;
|
|
|
|
|
|
2021-06-26 03:11:18 -04:00
|
|
|
const items = new Map<ItemID, Item<any>>();
|
2021-06-22 19:25:41 -04:00
|
|
|
|
2021-06-25 23:08:45 -04:00
|
|
|
export type PropertyValue = number | boolean;
|
|
|
|
|
|
2021-06-22 19:25:41 -04:00
|
|
|
// ITEMS SHALL BE SINGULAR
|
2021-06-26 03:11:18 -04:00
|
|
|
export class Item<Data> extends Serializable {
|
2021-06-22 19:25:41 -04:00
|
|
|
|
|
|
|
|
name = '';
|
|
|
|
|
id: ItemID = '';
|
2021-06-25 23:08:45 -04:00
|
|
|
props: Map<string, PropertyValue> = new Map();
|
2021-06-22 19:25:41 -04:00
|
|
|
|
2021-06-26 13:07:11 -04:00
|
|
|
setName(name: string) {
|
2021-06-22 19:25:41 -04:00
|
|
|
this.name = name;
|
|
|
|
|
this.register(false);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setId(id: ItemID) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.register(false);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
register(force = true) {
|
|
|
|
|
if((!this.id || !this.name) && !force) return;
|
|
|
|
|
console.log('Added item', (this.name ?? "[No Name]").padStart(20, ' '), `| (${this.id})`)
|
|
|
|
|
items.set(this.id, this);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2021-06-24 17:34:05 -04:00
|
|
|
|
|
|
|
|
setProperty(prop: ItemProperty, value: any) {
|
2021-06-26 13:07:11 -04:00
|
|
|
this.props.set(prop.name, value);
|
2021-06-25 20:28:29 -04:00
|
|
|
return this;
|
2021-06-24 17:34:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getProperty(prop: ItemProperty) {
|
2021-06-25 23:08:45 -04:00
|
|
|
if(this.props.has(prop.name)) return this.props.get(prop.name);
|
|
|
|
|
else return null;
|
2021-06-24 17:34:05 -04:00
|
|
|
}
|
2021-06-22 19:25:41 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-26 03:11:18 -04:00
|
|
|
export class ItemState<Data> extends Serializable implements Renderable {
|
2021-06-22 19:25:41 -04:00
|
|
|
qty: number;
|
|
|
|
|
itemId: ItemID;
|
2021-06-26 03:11:18 -04:00
|
|
|
data: Data;
|
2021-06-22 19:25:41 -04:00
|
|
|
|
|
|
|
|
get item() {
|
|
|
|
|
if(!items.has(this.itemId))
|
|
|
|
|
throw new Error('unknown item: ' + this.itemId);
|
|
|
|
|
return items.get(this.itemId);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 03:11:18 -04:00
|
|
|
constructor(item: Item<Data>, amount: number, data: Data) {
|
2021-06-22 19:25:41 -04:00
|
|
|
super();
|
|
|
|
|
this.qty = amount;
|
|
|
|
|
this.itemId = item.id;
|
|
|
|
|
this.data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return getTheme().normal(` ${this.item.name}{|}${this.qty} `);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 17:34:05 -04:00
|
|
|
export class ItemProperty {
|
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
|
|
constructor(name: string) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 03:11:18 -04:00
|
|
|
export type ItemFilter = (itemState: ItemState<any>) => boolean;
|