Refactor to move db load stuff out
This commit is contained in:
parent
87b6212eab
commit
4aca42d959
4 changed files with 147 additions and 145 deletions
42
build.js
42
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{
|
||||
|
|
|
@ -144,9 +144,8 @@
|
|||
</div>
|
||||
|
||||
</div>
|
||||
<script type="text/javascript" src="build.js">
|
||||
</script>
|
||||
<script type="text/javascript" src="test.js">
|
||||
</script>
|
||||
<script type="text/javascript" src="build.js"></script>
|
||||
<script type="text/javascript" src="load.js"></script>
|
||||
<script type="text/javascript" src="test.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
101
load.js
Normal file
101
load.js
Normal file
|
@ -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...");
|
||||
}
|
||||
}
|
142
test.js
142
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);
|
||||
|
|
Loading…
Reference in a new issue