2022-05-21 22:51:09 +00:00
|
|
|
let _ALL_NODES = new Map();
|
2022-05-14 06:01:03 +00:00
|
|
|
|
2022-05-21 22:51:09 +00:00
|
|
|
class ComputeNode {
|
|
|
|
/***
|
|
|
|
* Make a generic compute node.
|
|
|
|
* Adds the node to the global map of nodenames to nodes (for calling from html listeners).
|
|
|
|
*
|
|
|
|
* @param name : Name of the node (string). Must be unique. Must "fit in" a JS string (terminated by single quotes).
|
|
|
|
*/
|
2022-05-14 06:01:03 +00:00
|
|
|
constructor(name) {
|
2022-05-21 22:51:09 +00:00
|
|
|
if (_ALL_NODES.has(name)) {
|
|
|
|
throw 'Duplicate node name: ' + name;
|
|
|
|
}
|
|
|
|
_ALL_NODES.set(name, this)
|
2022-05-14 06:01:03 +00:00
|
|
|
this.inputs = [];
|
|
|
|
this.children = [];
|
|
|
|
this.value = 0;
|
|
|
|
this.name = name;
|
2022-05-21 22:51:09 +00:00
|
|
|
this.update_task = null;
|
|
|
|
this.update_time = Date.now();
|
2022-05-14 06:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 02:14:00 +00:00
|
|
|
/***
|
|
|
|
* Request update of this compute node. Pushes updates to children.
|
|
|
|
*/
|
2022-05-21 22:51:09 +00:00
|
|
|
update(timestamp) {
|
2022-05-22 04:19:43 +00:00
|
|
|
if (timestamp <= this.update_time) {
|
2022-05-21 22:51:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.update_time = timestamp;
|
|
|
|
|
2022-05-14 06:01:03 +00:00
|
|
|
let value_map = Map();
|
|
|
|
for (const input of this.inputs) {
|
|
|
|
value_map.set(input.name, input.get_value());
|
|
|
|
}
|
2022-05-22 02:14:00 +00:00
|
|
|
this.value = this.compute_func();
|
2022-05-14 06:01:03 +00:00
|
|
|
for (const child of this.children) {
|
|
|
|
child.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 02:14:00 +00:00
|
|
|
/***
|
|
|
|
* Get value of this compute node. Can't trigger update cascades (push based update, not pull based.)
|
|
|
|
*/
|
2022-05-14 06:01:03 +00:00
|
|
|
get_value() {
|
|
|
|
return this.value
|
|
|
|
}
|
2022-05-22 02:14:00 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Abstract method for computing something. Return value is set into this.value
|
|
|
|
*/
|
|
|
|
compute_func() {
|
|
|
|
throw "no compute func specified";
|
|
|
|
}
|
2022-05-22 04:19:43 +00:00
|
|
|
|
|
|
|
link_to(parent_node) {
|
|
|
|
this.inputs.push(parent_node)
|
|
|
|
parent_node.children.push(this);
|
|
|
|
}
|
2022-05-14 06:01:03 +00:00
|
|
|
}
|
2022-05-21 22:51:09 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Schedule a ComputeNode to be updated.
|
|
|
|
*
|
|
|
|
* @param node_name : ComputeNode name to schedule an update for.
|
|
|
|
*/
|
|
|
|
function calcSchedule(node_name) {
|
|
|
|
node = _ALL_NODES.get(node_name);
|
|
|
|
if (node.update_task !== null) {
|
|
|
|
clearTimeout(node.update_task);
|
|
|
|
}
|
|
|
|
node.update_task = setTimeout(function() {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
node.update(timestamp);
|
|
|
|
node.update_task = null;
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Node for getting an item's stats from an item input field.
|
|
|
|
*/
|
|
|
|
class ItemStats extends ComputeNode {
|
|
|
|
/***
|
|
|
|
* Make an item stat pulling compute node.
|
|
|
|
*
|
|
|
|
* @param name: Name of this node.
|
2022-05-22 02:14:00 +00:00
|
|
|
* @param item_input_field: Input field (html element) to listen for item names from.
|
|
|
|
* @param none_item: Item object to use as the "none" for this field.
|
2022-05-21 22:51:09 +00:00
|
|
|
*/
|
2022-05-22 02:14:00 +00:00
|
|
|
constructor(name, item_input_field, none_item) {
|
2022-05-21 22:51:09 +00:00
|
|
|
super(name);
|
|
|
|
this.input_field.setAttribute("onInput", "calcSchedule('"+name+"');");
|
|
|
|
this.input_field = item_input_field;
|
2022-05-22 02:14:00 +00:00
|
|
|
this.none_item = none_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
compute_func() {
|
|
|
|
// built on the assumption of no one will type in CI/CR letter by letter
|
|
|
|
|
|
|
|
let item_text = this.input_field.value;
|
|
|
|
let item;
|
|
|
|
|
|
|
|
if (item_text.slice(0, 3) == "CI-") {
|
|
|
|
item = getCustomFromHash(item_text);
|
|
|
|
}
|
|
|
|
else if (item_text.slice(0, 3) == "CR-") {
|
|
|
|
item = getCraftFromHash(item_text);
|
|
|
|
}
|
|
|
|
else if (itemMap.has(item_text)) {
|
|
|
|
item = itemMap.get(item_text);
|
|
|
|
}
|
|
|
|
else if (tomeMap.has(item_text)) {
|
|
|
|
item = tomeMap.get(item_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return this.none_item;
|
|
|
|
}
|
|
|
|
return item;
|
2022-05-21 22:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|