From 3198afcea1da7559ccadc290eabdca1458b01024 Mon Sep 17 00:00:00 2001 From: b Date: Mon, 19 Jul 2021 14:04:56 -0700 Subject: [PATCH] Default values for items in local storage --- build_utils.js | 3 +++ builder.js | 1 - items_2.js | 27 --------------------------- load.js | 15 ++++++++++++++- utils.js | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/build_utils.js b/build_utils.js index abb6515..5f60bb1 100644 --- a/build_utils.js +++ b/build_utils.js @@ -63,6 +63,9 @@ let itemTypes = armorTypes.concat(accessoryTypes).concat(weaponTypes); let elementIcons = ["\u2724","\u2726", "\u2749", "\u2739", "\u274b" ]; let skpReqs = skp_order.map(x => x + "Req"); +let item_fields = [ "name", "displayName", "lore", "color", "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", "hprPct", "mr", "sdPct", "mdPct", "ls", "ms", "xpb", "lb", "ref", "str", "dex", "int", "agi", "def", "thorns", "expd", "spd", "atkTier", "poison", "hpBonus", "spRegen", "eSteal", "hprRaw", "sdRaw", "mdRaw", "fDamPct", "wDamPct", "aDamPct", "tDamPct", "eDamPct", "fDefPct", "wDefPct", "aDefPct", "tDefPct", "eDefPct", "fixID", "category", "spPct1", "spRaw1", "spPct2", "spRaw2", "spPct3", "spRaw3", "spPct4", "spRaw4", "rainbowRaw", "sprint", "sprintReg", "jh", "lq", "gXp", "gSpd", "id", "majorIds"]; +let str_item_fields = [ "name", "displayName", "lore", "color", "tier", "set", "type", "material", "drop", "quest", "restrict", "category", "atkSpd" ] + //File reading for ID translations for JSON purposes let reversetranslations = new Map(); let translations = new Map([["name", "name"], ["displayName", "displayName"], ["tier", "tier"], ["set", "set"], ["sockets", "slots"], ["type", "type"], ["dropType", "drop"], ["quest", "quest"], ["restrictions", "restrict"], ["damage", "nDam"], ["fireDamage", "fDam"], ["waterDamage", "wDam"], ["airDamage", "aDam"], ["thunderDamage", "tDam"], ["earthDamage", "eDam"], ["attackSpeed", "atkSpd"], ["health", "hp"], ["fireDefense", "fDef"], ["waterDefense", "wDef"], ["airDefense", "aDef"], ["thunderDefense", "tDef"], ["earthDefense", "eDef"], ["level", "lvl"], ["classRequirement", "classReq"], ["strength", "strReq"], ["dexterity", "dexReq"], ["intelligence", "intReq"], ["agility", "agiReq"], ["defense", "defReq"], ["healthRegen", "hprPct"], ["manaRegen", "mr"], ["spellDamage", "sdPct"], ["damageBonus", "mdPct"], ["lifeSteal", "ls"], ["manaSteal", "ms"], ["xpBonus", "xpb"], ["lootBonus", "lb"], ["reflection", "ref"], ["strengthPoints", "str"], ["dexterityPoints", "dex"], ["intelligencePoints", "int"], ["agilityPoints", "agi"], ["defensePoints", "def"], ["thorns", "thorns"], ["exploding", "expd"], ["speed", "spd"], ["attackSpeedBonus", "atkTier"], ["poison", "poison"], ["healthBonus", "hpBonus"], ["soulPoints", "spRegen"], ["emeraldStealing", "eSteal"], ["healthRegenRaw", "hprRaw"], ["spellDamageRaw", "sdRaw"], ["damageBonusRaw", "mdRaw"], ["bonusFireDamage", "fDamPct"], ["bonusWaterDamage", "wDamPct"], ["bonusAirDamage", "aDamPct"], ["bonusThunderDamage", "tDamPct"], ["bonusEarthDamage", "eDamPct"], ["bonusFireDefense", "fDefPct"], ["bonusWaterDefense", "wDefPct"], ["bonusAirDefense", "aDefPct"], ["bonusThunderDefense", "tDefPct"], ["bonusEarthDefense", "eDefPct"], ["type", "type"], ["identified", "fixID"], ["skin", "skin"], ["category", "category"], ["spellCostPct1", "spPct1"], ["spellCostRaw1", "spRaw1"], ["spellCostPct2", "spPct2"], ["spellCostRaw2", "spRaw2"], ["spellCostPct3", "spPct3"], ["spellCostRaw3", "spRaw3"], ["spellCostPct4", "spPct4"], ["spellCostRaw4", "spRaw4"], ["rainbowSpellDamageRaw", "rainbowRaw"], ["sprint", "sprint"], ["sprintRegen", "sprintReg"], ["jumpHeight", "jh"], ["lootQuality", "lq"], ["gatherXpBonus", "gXp"], ["gatherSpeed", "gSpd"]]); diff --git a/builder.js b/builder.js index 5c14cca..962210f 100644 --- a/builder.js +++ b/builder.js @@ -22,7 +22,6 @@ setTitle(); let player_build; // THIS IS SUPER DANGEROUS, WE SHOULD NOT BE KEEPING THIS IN SO MANY PLACES -let item_fields = [ "name", "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", "hprPct", "mr", "sdPct", "mdPct", "ls", "ms", "xpb", "lb", "ref", "str", "dex", "int", "agi", "def", "thorns", "expd", "spd", "atkTier", "poison", "hpBonus", "spRegen", "eSteal", "hprRaw", "sdRaw", "mdRaw", "fDamPct", "wDamPct", "aDamPct", "tDamPct", "eDamPct", "fDefPct", "wDefPct", "aDefPct", "tDefPct", "eDefPct", "fixID", "category", "spPct1", "spRaw1", "spPct2", "spRaw2", "spPct3", "spRaw3", "spPct4", "spRaw4", "rainbowRaw", "sprint", "sprintReg", "jh", "lq", "gXp", "gSpd", "id" ]; let editable_item_fields = [ "sdPct", "sdRaw", "mdPct", "mdRaw", "poison", "fDamPct", "wDamPct", "aDamPct", "tDamPct", "eDamPct", "fDefPct", "wDefPct", "aDefPct", "tDefPct", "eDefPct", "hprRaw", "hprPct", "hpBonus", "atkTier", "spPct1", "spRaw1", "spPct2", "spRaw2", "spPct3", "spRaw3", "spPct4", "spRaw4" ]; let editable_elems = []; diff --git a/items_2.js b/items_2.js index 3e4dc31..1f846ed 100644 --- a/items_2.js +++ b/items_2.js @@ -233,33 +233,6 @@ class ExprField { function compareLexico(ia, keysA, ib, keysB) { for (let i = 0; i < keysA.length; i++) { // assuming keysA and keysB are the same length let aKey = keysA[i], bKey = keysB[i]; - if (typeof aKey === 'undefined') { - switch (typeof bKey) { - case 'string': - aKey = ""; - break; - case 'number': - aKey = 0; - break; - case 'undefined': - return 0; - default: - throw new Error(`Incomparable type ${typeof bKey}`); - } - } - else if (typeof(bKey) === 'undefined') { - switch (typeof aKey) { - case 'string': - bKey = ""; - break; - case 'number': - bKey = 0; - break; - default: - throw new Error(`Incomparable type ${typeof aKey}`); - } - - } if (typeof aKey !== typeof bKey) throw new Error(`Incomparable types ${typeof aKey} and ${typeof bKey}`); // can this even happen? switch (typeof aKey) { case 'string': diff --git a/load.js b/load.js index e2a245f..d691140 100644 --- a/load.js +++ b/load.js @@ -1,4 +1,4 @@ -const DB_VERSION = 74; +const DB_VERSION = 75; // @See https://github.com/mdn/learning-area/blob/master/javascript/apis/client-side-storage/indexeddb/video-store/index.jsA let db; @@ -71,6 +71,19 @@ function clean_item(item) { if (item.slots === undefined) { item.slots = 0 } + for (let key of item_fields) { + if (item[key] === undefined) { + if (key in str_item_fields) { + item[key] = ""; + } + else if (key == "majorIds") { + item[key] = []; + } + else { + item[key] = 0; + } + } + } } } diff --git a/utils.js b/utils.js index 8705150..064ef03 100644 --- a/utils.js +++ b/utils.js @@ -343,6 +343,26 @@ function toggleButton(button_id) { } } +/** + * If the input object is undefined, make it "match" the target type + * with default value (0 or empty str). + */ +function matchType(object, target) { + if (typeof object === 'undefined') { + switch (target) { + case 'string': + return ""; + case 'number': + return 0; + case 'undefined': + return undefined; + default: + throw new Error(`Incomparable type ${target}`); + } + } + return object; +} + /** A utility function that reloads the page forcefully. * */ @@ -353,3 +373,4 @@ async function hardReload() { location.reload(); } +