Create common.js for more common stuff (Item class, expandItem func)

This commit is contained in:
hppeng 2022-05-21 22:38:43 -07:00
parent 1c4042325d
commit 1e863fd015
4 changed files with 165 additions and 155 deletions

123
js/common.js Normal file
View file

@ -0,0 +1,123 @@
let nonRolledIDs = [
"name",
"lore",
"displayName",
"tier",
"set",
"slots",
"type",
"material",
"drop",
"quest",
"restrict",
"nDam", "fDam", "wDam", "aDam", "tDam", "eDam",
"atkSpd",
"hp",
"fDef", "wDef", "aDef", "tDef", "eDef",
"lvl",
"classReq",
"strReq", "dexReq", "intReq", "defReq", "agiReq",
"str", "dex", "int", "agi", "def",
"fixID",
"category",
"id",
"skillpoints",
"reqs",
"nDam_", "fDam_", "wDam_", "aDam_", "tDam_", "eDam_",
"majorIds"];
let rolledIDs = [
"hprPct",
"mr",
"sdPct",
"mdPct",
"ls",
"ms",
"xpb",
"lb",
"ref",
"thorns",
"expd",
"spd",
"atkTier",
"poison",
"hpBonus",
"spRegen",
"eSteal",
"hprRaw",
"sdRaw",
"mdRaw",
"fDamPct", "wDamPct", "aDamPct", "tDamPct", "eDamPct",
"fDefPct", "wDefPct", "aDefPct", "tDefPct", "eDefPct",
"spPct1", "spRaw1",
"spPct2", "spRaw2",
"spPct3", "spRaw3",
"spPct4", "spRaw4",
"rainbowRaw",
"sprint",
"sprintReg",
"jh",
"lq",
"gXp",
"gSpd"
];
/**
* Take an item with id list and turn it into a set of minrolls and maxrolls.
*/
function expandItem(item) {
let minRolls = new Map();
let maxRolls = new Map();
let expandedItem = new Map();
if (item.fixID) { //The item has fixed IDs.
expandedItem.set("fixID",true);
for (const id of rolledIDs) { //all rolled IDs are numerical
let val = (item[id] || 0);
minRolls.set(id,val);
maxRolls.set(id,val);
}
} else { //The item does not have fixed IDs.
for (const id of rolledIDs) {
let val = (item[id] || 0);
if (val > 0) { // positive rolled IDs
if (reversedIDs.includes(id)) {
maxRolls.set(id,idRound(val*0.3));
minRolls.set(id,idRound(val*1.3));
} else {
maxRolls.set(id,idRound(val*1.3));
minRolls.set(id,idRound(val*0.3));
}
} else if (val < 0) { //negative rolled IDs
if (reversedIDs.includes(id)) {
maxRolls.set(id,idRound(val*1.3));
minRolls.set(id,idRound(val*0.7));
}
else {
maxRolls.set(id,idRound(val*0.7));
minRolls.set(id,idRound(val*1.3));
}
}
else { // if val == 0
// NOTE: DO NOT remove this case! idRound behavior does not round to 0!
maxRolls.set(id,0);
minRolls.set(id,0);
}
}
}
for (const id of nonRolledIDs) {
expandedItem.set(id,item[id]);
}
expandedItem.set("minRolls",minRolls);
expandedItem.set("maxRolls",maxRolls);
expandedItem.set("powders", powders);
return expandedItem;
}
class Item {
constructor(item_obj) {
this.statMap = ; //can use the statMap as an expanded Item
this.atkSpd = attackSpeed;
this.hash = "CR-" + hash;
this.initCraftStats();
this.statMap.set("hash", this.hash);
}
}

View file

@ -18,6 +18,7 @@ class ComputeNode {
this.name = name;
this.update_task = null;
this.update_time = Date.now();
this.fail_cb = false; // Set to true to force updates even if parent failed.
}
/***
@ -33,9 +34,11 @@ class ComputeNode {
for (const input of this.inputs) {
value_map.set(input.name, input.get_value());
}
this.value = this.compute_func();
this.value = this.compute_func(value_map);
for (const child of this.children) {
child.update();
if (this.value || child.fail_cb) {
child.update();
}
}
}
@ -49,7 +52,7 @@ class ComputeNode {
/***
* Abstract method for computing something. Return value is set into this.value
*/
compute_func() {
compute_func(input_map) {
throw "no compute func specified";
}
@ -94,10 +97,14 @@ class ItemStats extends ComputeNode {
this.none_item = none_item;
}
compute_func() {
compute_func(input_map) {
// built on the assumption of no one will type in CI/CR letter by letter
let item_text = this.input_field.value;
if (!item_text) {
return this.none_item;
}
let item;
if (item_text.slice(0, 3) == "CI-") {
@ -113,10 +120,39 @@ class ItemStats extends ComputeNode {
item = tomeMap.get(item_text);
}
if (!item) {
return this.none_item;
if (!item || item.) {
return null;
}
return item;
}
}
/***
* Node for updating item input fields from parsed items.
*/
class ItemInputDisplay extends ComputeNode {
constructor(name, item_input_field, item_image) {
super(name);
this.input_field = item_input_field;
this.image = item_image;
this.fail_cb = true;
}
compute_func(input_map) {
if (input_map.size !== 1) {
throw "ItemInputDisplay accepts exactly one input (item)";
}
const [item] = input_map.values(); // Extract values, pattern match it into size one list and bind to first element
this.input_field.classList.remove("text-light", "is-invalid", 'Normal', 'Unique', 'Rare', 'Legendary', 'Fabled', 'Mythic', 'Set', 'Crafted', 'Custom');
this.input_field.classList.add("text-light");
this.image.classList.remove('Normal-shadow', 'Unique-shadow', 'Rare-shadow', 'Legendary-shadow', 'Fabled-shadow', 'Mythic-shadow', 'Set-shadow', 'Crafted-shadow', 'Custom-shadow');
if (!item) {
this.input_field.classList.add("is-invalid");
return null;
}
}
}

View file

@ -27,60 +27,6 @@ function applyArmorPowdersOnce(expandedItem, powders) {
}
}
/**
* Take an item with id list and turn it into a set of minrolls and maxrolls.
* Also applies powders to armor.
*/
function expandItem(item, powders) {
let minRolls = new Map();
let maxRolls = new Map();
let expandedItem = new Map();
if (item.fixID) { //The item has fixed IDs.
expandedItem.set("fixID",true);
for (const id of rolledIDs) { //all rolled IDs are numerical
let val = (item[id] || 0);
minRolls.set(id,val);
maxRolls.set(id,val);
}
} else { //The item does not have fixed IDs.
for (const id of rolledIDs) {
let val = (item[id] || 0);
if (val > 0) { // positive rolled IDs
if (reversedIDs.includes(id)) {
maxRolls.set(id,idRound(val*0.3));
minRolls.set(id,idRound(val*1.3));
} else {
maxRolls.set(id,idRound(val*1.3));
minRolls.set(id,idRound(val*0.3));
}
} else if (val < 0) { //negative rolled IDs
if (reversedIDs.includes(id)) {
maxRolls.set(id,idRound(val*1.3));
minRolls.set(id,idRound(val*0.7));
}
else {
maxRolls.set(id,idRound(val*0.7));
minRolls.set(id,idRound(val*1.3));
}
}
else { // if val == 0
// NOTE: DO NOT remove this case! idRound behavior does not round to 0!
maxRolls.set(id,0);
minRolls.set(id,0);
}
}
}
for (const id of nonRolledIDs) {
expandedItem.set(id,item[id]);
}
expandedItem.set("minRolls",minRolls);
expandedItem.set("maxRolls",maxRolls);
expandedItem.set("powders", powders);
if (item.category === "armor") {
applyArmorPowders(expandedItem, powders);
}
return expandedItem;
}
/* Takes in an ingredient object and returns an equivalent Map().
*/

View file

@ -1,98 +1,3 @@
let nonRolledIDs = [
"name",
"lore",
"displayName",
"tier",
"set",
"slots",
"type",
"material",
"drop",
"quest",
"restrict",
"nDam",
"fDam",
"wDam",
"aDam",
"tDam",
"eDam",
"atkSpd",
"hp",
"fDef",
"wDef",
"aDef",
"tDef",
"eDef",
"lvl",
"classReq",
"strReq",
"dexReq",
"intReq",
"defReq",
"agiReq","str",
"dex",
"int",
"agi",
"def",
"fixID",
"category",
"id",
"skillpoints",
"reqs",
"nDam_",
"fDam_",
"wDam_",
"aDam_",
"tDam_",
"eDam_",
"majorIds"];
let rolledIDs = [
"hprPct",
"mr",
"sdPct",
"mdPct",
"ls",
"ms",
"xpb",
"lb",
"ref",
"thorns",
"expd",
"spd",
"atkTier",
"poison",
"hpBonus",
"spRegen",
"eSteal",
"hprRaw",
"sdRaw",
"mdRaw",
"fDamPct",
"wDamPct",
"aDamPct",
"tDamPct",
"eDamPct",
"fDefPct",
"wDefPct",
"aDefPct",
"tDefPct",
"eDefPct",
"spPct1",
"spRaw1",
"spPct2",
"spRaw2",
"spPct3",
"spRaw3",
"spPct4",
"spRaw4",
"rainbowRaw",
"sprint",
"sprintReg",
"jh",
"lq",
"gXp",
"gSpd"
];
let reversedIDs = [ "spPct1", "spRaw1", "spPct2", "spRaw2", "spPct3", "spRaw3", "spPct4", "spRaw4" ];
let colorMap = new Map(
[