From 4aca42d9595047ef66d39bedbe28c5e081574e93 Mon Sep 17 00:00:00 2001 From: b Date: Wed, 6 Jan 2021 18:08:19 -0600 Subject: [PATCH] Refactor to move db load stuff out --- build.js | 42 ++++++++++++++++ index.html | 7 ++- load.js | 101 +++++++++++++++++++++++++++++++++++++ test.js | 142 +---------------------------------------------------- 4 files changed, 147 insertions(+), 145 deletions(-) create mode 100644 load.js diff --git a/build.js b/build.js index 79fc017..3c62161 100644 --- a/build.js +++ b/build.js @@ -1,3 +1,45 @@ +/*Turns the input amount of skill points into a float precision percentage. +* @param skp - the integer skillpoint count to be converted +*/ +function skillPointsToPercentage(skp){ + if (skp<=0){ + return 0.0; + }else if(skp>=150){ + return 0.808; + }else{ + return(-0.0000000066695* Math.pow(Math.E, -0.00924033 * skp + 18.9) + 1.0771); + //return(-0.0000000066695* Math.pow(Math.E, -0.00924033 * skp + 18.9) + 1.0771).toFixed(3); + } +} + +/*Turns the input amount of levels into skillpoints available. +* +* @param level - the integer level count te be converted +*/ +function levelToSkillPoints(level){ + if(level < 1){ + return 0; + }else if(level >= 101){ + return 200; + }else{ + return (level - 1) * 2; + } +} + +/*Turns the input amount of levels in to base HP. +* @param level - the integer level count to be converted +*/ +function levelToHPBase(level){ + if(level < 1){ //bad level + return this.levelToHPBase(1); + }else if (level > 106){ //also bad level + return this.levelToHPBase(106); + }else{ //good level + return 5*level + 5; + } +} + + /*Class that represents a wynn player's build. */ class Build{ diff --git a/index.html b/index.html index 418b11f..916d7b2 100644 --- a/index.html +++ b/index.html @@ -144,9 +144,8 @@ - - + + + diff --git a/load.js b/load.js new file mode 100644 index 0000000..868c01b --- /dev/null +++ b/load.js @@ -0,0 +1,101 @@ +const DB_VERSION = 2; +// @See https://github.com/mdn/learning-area/blob/master/javascript/apis/client-side-storage/indexeddb/video-store/index.js + +let db; +let reload = false; +let items; + +/* + * Load item set from local DB. Calls init() on success. + */ +async function load_local(init_func) { + let get_tx = db.transaction('item_db', 'readonly'); + let get_store = get_tx.objectStore('item_db'); + let request = get_store.getAll(); + request.onerror = function(event) { + console.log("Could not read local db..."); + } + request.onsuccess = function(event) { + console.log("Successfully read local db."); + items = request.result; + init_func(); + } + await get_tx.complete; + db.close(); +} + +/* + * Clean bad item data. For now just assigns display name if it isn't already assigned. + */ +function clean_item(item) { + if (item.displayName === undefined) { + item.displayName = item.name; + } +} + +/* + * Load item set from remote DB (aka a big json file). Calls init() on success. + */ +async function load(init_func) { + let url = "https://hppeng-wynn.github.io/compress.json"; + let result = await (await fetch(url)).json(); + items = result.items; + + // https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/clear + let clear_tx = db.transaction('item_db', 'readwrite'); + let clear_store = clear_tx.objectStore('item_db'); + + await clear_store.clear(); + await clear_tx.complete; + + let add_tx = db.transaction('item_db', 'readwrite'); + let add_store = add_tx.objectStore('item_db'); + let add_promises = []; + for (const item of items) { + clean_item(item); + add_promises.push(add_store.add(item, item.name)); + } + add_promises.push(add_tx.complete); + Promise.all(add_promises).then((values) => { + db.close(); + init_func(); + }); +} + +function load_init(init_func) { + let request = window.indexedDB.open('item_db', DB_VERSION); + + request.onerror = function() { + console.log("DB failed to open..."); + }; + + request.onsuccess = function() { + db = request.result; + if (!reload) { + console.log("Using stored data...") + load_local(init_func); + } + else { + console.log("Using new data...") + load(init_func); + } + } + + request.onupgradeneeded = function(e) { + reload = true; + + let db = e.target.result; + + try { + db.deleteObjectStore('item_db'); + } + catch (error) { + console.log("Could not delete DB. This is probably fine"); + } + let objectStore = db.createObjectStore('item_db'); + + objectStore.createIndex('item', 'item', {unique: false}); + + console.log("DB setup complete..."); + } +} diff --git a/test.js b/test.js index 076c0bf..22b7ee3 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,3 @@ -const DB_VERSION = 2; -// @See https://github.com/mdn/learning-area/blob/master/javascript/apis/client-side-storage/indexeddb/video-store/index.js - -let db; -let items; -let reload = false; // Set up item lists for quick access later. let armorTypes = [ "helmet", "chestplate", "leggings", "boots" ]; let accessoryTypes = [ "ring", "bracelet", "necklace" ]; @@ -104,99 +98,6 @@ function init() { }); } -/* - * Load item set from local DB. Calls init() on success. - */ -async function load_local() { - let get_tx = db.transaction('item_db', 'readonly'); - let get_store = get_tx.objectStore('item_db'); - let request = get_store.getAll(); - request.onerror = function(event) { - console.log("Could not read local db..."); - } - request.onsuccess = function(event) { - console.log("Successfully read local db."); - items = request.result; - init(); - } - await get_tx.complete; - db.close(); -} - -/* - * Clean bad item data. For now just assigns display name if it isn't already assigned. - */ -function clean_item(item) { - if (item.displayName === undefined) { - item.displayName = item.name; - } -} - -/* - * Load item set from remote DB (aka a big json file). Calls init() on success. - */ -async function load() { - let url = "https://hppeng-wynn.github.io/compress.json"; - let result = await (await fetch(url)).json(); - items = result.items; - - // https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/clear - let clear_tx = db.transaction('item_db', 'readwrite'); - let clear_store = clear_tx.objectStore('item_db'); - - await clear_store.clear(); - await clear_tx.complete; - - let add_tx = db.transaction('item_db', 'readwrite'); - let add_store = add_tx.objectStore('item_db'); - let add_promises = []; - for (const item of items) { - clean_item(item); - add_promises.push(add_store.add(item, item.name)); - } - add_promises.push(add_tx.complete); - Promise.all(add_promises).then((values) => { - db.close(); - init(); - }); -} - -let request = window.indexedDB.open('item_db', DB_VERSION); - -request.onerror = function() { - console.log("DB failed to open..."); -}; - -request.onsuccess = function() { - db = request.result; - if (!reload) { - console.log("Using stored data...") - load_local(); - } - else { - console.log("Using new data...") - load(); - } -} - -request.onupgradeneeded = function(e) { - reload = true; - - let db = e.target.result; - - try { - db.deleteObjectStore('item_db'); - } - catch (error) { - console.log("Could not delete DB. This is probably fine"); - } - let objectStore = db.createObjectStore('item_db'); - - objectStore.createIndex('item', 'item', {unique: false}); - - console.log("DB setup complete..."); -} - function calculateBuild(){ /* TODO: implement level changing Make this entire function prettier @@ -274,45 +175,4 @@ function resetFields(){ document.getElementById("agi-skp").value = ""; } - -/*Turns the input amount of skill points into a float precision percentage. -* @param skp - the integer skillpoint count to be converted -*/ -function skillPointsToPercentage(skp){ - if (skp<=0){ - return 0.0; - }else if(skp>=150){ - return 0.808; - }else{ - return(-0.0000000066695* Math.pow(Math.E, -0.00924033 * skp + 18.9) + 1.0771); - //return(-0.0000000066695* Math.pow(Math.E, -0.00924033 * skp + 18.9) + 1.0771).toFixed(3); - } -} - -/*Turns the input amount of levels into skillpoints available. -* -* @param level - the integer level count te be converted -*/ -function levelToSkillPoints(level){ - if(level < 1){ - return 0; - }else if(level >= 101){ - return 200; - }else{ - return (level - 1) * 2; - } -} - -/*Turns the input amount of levels in to base HP. -* @param level - the integer level count to be converted -*/ -function levelToHPBase(level){ - if(level < 1){ //bad level - return this.levelToHPBase(1); - }else if (level > 106){ //also bad level - return this.levelToHPBase(106); - }else{ //good level - return 5*level + 5; - } -} - +load_init(init);