2023-12-30 02:55:31 +00:00
|
|
|
const ING_DB_VERSION = 31;
|
2021-01-18 12:10:28 +00:00
|
|
|
|
|
|
|
// @See https://github.com/mdn/learning-area/blob/master/javascript/apis/client-side-storage/indexeddb/video-store/index.js
|
|
|
|
|
2021-01-30 08:50:25 +00:00
|
|
|
let idb;
|
|
|
|
let ireload = false;
|
2022-06-12 14:45:48 +00:00
|
|
|
let iload_in_progress = false;
|
2021-04-02 22:19:22 +00:00
|
|
|
let iload_complete = false;
|
2021-01-18 12:10:28 +00:00
|
|
|
let ings;
|
|
|
|
let recipes;
|
|
|
|
|
2022-07-17 21:34:54 +00:00
|
|
|
let ingMap = new Map();
|
2021-03-14 07:55:08 +00:00
|
|
|
let ingList = [];
|
|
|
|
|
2021-04-02 17:44:54 +00:00
|
|
|
let recipeMap;
|
2021-03-14 07:55:08 +00:00
|
|
|
let recipeList = [];
|
|
|
|
|
2022-07-17 21:34:54 +00:00
|
|
|
let ingIDMap = new Map();
|
2021-04-02 17:44:54 +00:00
|
|
|
let recipeIDMap;
|
2021-03-14 07:55:08 +00:00
|
|
|
|
2021-01-18 12:10:28 +00:00
|
|
|
/*
|
|
|
|
* Load item set from local DB. Calls init() on success.
|
|
|
|
*/
|
2022-06-12 14:45:48 +00:00
|
|
|
async function ing_load_local() {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
let get_tx = idb.transaction(['ing_db', 'recipe_db'], 'readonly');
|
|
|
|
let ings_store = get_tx.objectStore('ing_db');
|
|
|
|
let recipes_store = get_tx.objectStore('recipe_db');
|
|
|
|
let request3 = ings_store.getAll();
|
|
|
|
request3.onerror = function(event) {
|
|
|
|
reject("Could not read local ingredient db...");
|
|
|
|
}
|
|
|
|
request3.onsuccess = function(event) {
|
|
|
|
console.log("Successfully read local ingredient db.");
|
|
|
|
}
|
2021-04-02 21:53:18 +00:00
|
|
|
let request4 = recipes_store.getAll();
|
|
|
|
request4.onerror = function(event) {
|
2022-06-12 14:45:48 +00:00
|
|
|
reject("Could not read local recipe db...");
|
2021-04-02 17:44:54 +00:00
|
|
|
}
|
2021-04-02 21:53:18 +00:00
|
|
|
request4.onsuccess = function(event) {
|
|
|
|
console.log("Successfully read local recipe db.");
|
2022-06-12 14:45:48 +00:00
|
|
|
}
|
|
|
|
get_tx.oncomplete = function(event) {
|
|
|
|
ings = request3.result;
|
2021-04-02 21:53:18 +00:00
|
|
|
recipes = request4.result;
|
|
|
|
init_ing_maps();
|
2021-04-02 22:19:22 +00:00
|
|
|
iload_complete = true;
|
2022-06-12 14:45:48 +00:00
|
|
|
idb.close();
|
|
|
|
resolve()
|
2021-04-02 17:44:54 +00:00
|
|
|
}
|
2022-06-12 14:45:48 +00:00
|
|
|
});
|
2021-01-18 12:10:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 14:47:08 +00:00
|
|
|
function clean_ing(ing) {
|
|
|
|
if (ing.remapID === undefined) {
|
|
|
|
if (ing.displayName === undefined) {
|
|
|
|
ing.displayName = ing.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 10:29:01 +00:00
|
|
|
async function load_ings_old_version(version_str) {
|
|
|
|
iload_in_progress = true;
|
|
|
|
let getUrl = window.location;
|
|
|
|
let baseUrl = `${getUrl.protocol}//${getUrl.host}/`;
|
|
|
|
// No random string -- we want to use caching
|
|
|
|
let url = `${baseUrl}/data/${version_str}/ingreds.json`;
|
|
|
|
let result = await (await fetch(url)).json();
|
|
|
|
ings = result;
|
|
|
|
for (const id in ings) {
|
|
|
|
clean_ing(ings[id]);
|
|
|
|
}
|
|
|
|
|
2023-07-15 01:34:30 +00:00
|
|
|
url = `${baseUrl}/data/${version_str}/recipes.json`;
|
2022-12-16 10:29:01 +00:00
|
|
|
result = await (await fetch(url)).json();
|
|
|
|
recipes = result.recipes;
|
|
|
|
|
|
|
|
init_maps();
|
|
|
|
iload_complete = true;
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:10:28 +00:00
|
|
|
/*
|
|
|
|
* Load item set from remote DB (aka a big json file). Calls init() on success.
|
|
|
|
*/
|
2022-06-12 14:45:48 +00:00
|
|
|
async function load_ings() {
|
2021-01-18 12:10:28 +00:00
|
|
|
|
|
|
|
let getUrl = window.location;
|
2022-12-16 10:29:01 +00:00
|
|
|
let baseUrl = `${getUrl.protocol}//${getUrl.host}/`;
|
2022-06-12 14:45:48 +00:00
|
|
|
// "Random" string to prevent caching!
|
|
|
|
let url = baseUrl + "/ingreds_compress.json?"+new Date();
|
2021-01-18 12:10:28 +00:00
|
|
|
let result = await (await fetch(url)).json();
|
|
|
|
|
|
|
|
result = await (await fetch(url)).json();
|
2021-05-20 03:35:35 +00:00
|
|
|
ings = result;
|
2021-01-18 12:10:28 +00:00
|
|
|
|
2022-12-16 10:29:01 +00:00
|
|
|
url = baseUrl + "/recipes_compress.json?"+new Date();
|
2021-01-18 12:10:28 +00:00
|
|
|
result = await (await fetch(url)).json();
|
|
|
|
recipes = result.recipes;
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/clear
|
2021-01-19 17:32:27 +00:00
|
|
|
/*let clear_tx2 = db.transaction(['ing_db'], 'readwrite');
|
2021-01-18 12:10:28 +00:00
|
|
|
let clear_ings = clear_tx2.objectStore('ing_db');
|
|
|
|
let clear_tx3 = db.transaction(['recipe_db'], 'readwrite');
|
|
|
|
let clear_recipes = clear_tx3.objectStore('recipe_db');
|
|
|
|
await clear_ings.clear();
|
|
|
|
await clear_recipes.clear();
|
|
|
|
await clear_tx2.complete;
|
2021-01-19 17:32:27 +00:00
|
|
|
await clear_tx3.complete;*/
|
|
|
|
let add_promises = [];
|
2021-01-30 08:50:25 +00:00
|
|
|
let add_tx2 = idb.transaction(['ing_db'], 'readwrite');
|
2021-01-18 12:10:28 +00:00
|
|
|
let ings_store = add_tx2.objectStore('ing_db');
|
2021-03-04 14:47:08 +00:00
|
|
|
for (const id in ings) {
|
|
|
|
clean_ing(ings[id]);
|
|
|
|
add_promises.push(ings_store.add(ings[id], id));
|
2021-01-18 12:10:28 +00:00
|
|
|
}
|
2021-01-30 08:50:25 +00:00
|
|
|
let add_tx3 = idb.transaction(['recipe_db'], 'readwrite');
|
2021-01-18 12:10:28 +00:00
|
|
|
let recipes_store = add_tx3.objectStore('recipe_db');
|
|
|
|
for (const recipe in recipes) {
|
|
|
|
add_promises.push(recipes_store.add(recipes[recipe], recipe));
|
|
|
|
}
|
|
|
|
add_promises.push(add_tx2.complete);
|
|
|
|
add_promises.push(add_tx3.complete);
|
|
|
|
|
2022-06-12 14:45:48 +00:00
|
|
|
await Promise.all(add_promises);
|
|
|
|
init_ing_maps();
|
|
|
|
iload_complete = true;
|
|
|
|
idb.close();
|
|
|
|
}
|
2021-01-18 12:10:28 +00:00
|
|
|
|
2022-06-12 14:45:48 +00:00
|
|
|
async function load_ing_init() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let request = window.indexedDB.open("ing_db", ING_DB_VERSION)
|
|
|
|
request.onerror = function() {
|
|
|
|
reject("DB failed to open...");
|
2021-01-18 12:10:28 +00:00
|
|
|
}
|
2022-06-12 14:45:48 +00:00
|
|
|
|
|
|
|
request.onsuccess = async function() {
|
|
|
|
idb = request.result;
|
|
|
|
if (iload_in_progress) {
|
|
|
|
while (!iload_complete) {
|
|
|
|
await sleep(100);
|
|
|
|
}
|
|
|
|
console.log("Skipping load...")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
iload_in_progress = true
|
|
|
|
if (ireload) {
|
|
|
|
console.log("Using new data...")
|
|
|
|
await load_ings();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("Using stored data...")
|
|
|
|
await ing_load_local();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve();
|
2021-01-18 12:10:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 14:45:48 +00:00
|
|
|
request.onupgradeneeded = function(e) {
|
|
|
|
ireload = true;
|
2021-01-18 12:10:28 +00:00
|
|
|
|
2022-06-12 14:45:48 +00:00
|
|
|
let idb = e.target.result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
idb.deleteObjectStore('ing_db');
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("Could not delete ingredient DB. This is probably fine");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
idb.deleteObjectStore('recipe_db');
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("Could not delete recipe DB. This is probably fine");
|
|
|
|
}
|
|
|
|
idb.createObjectStore('ing_db');
|
|
|
|
idb.createObjectStore('recipe_db');
|
2021-01-18 12:10:28 +00:00
|
|
|
|
2022-06-12 14:45:48 +00:00
|
|
|
console.log("DB setup complete...");
|
|
|
|
}
|
|
|
|
});
|
2021-01-18 12:10:28 +00:00
|
|
|
}
|
2021-03-14 07:55:08 +00:00
|
|
|
|
|
|
|
function init_ing_maps() {
|
2021-04-02 17:44:54 +00:00
|
|
|
recipeMap = new Map();
|
|
|
|
recipeIDMap = new Map();
|
|
|
|
|
2022-07-17 20:10:50 +00:00
|
|
|
let ing = {
|
|
|
|
name: "No Ingredient",
|
|
|
|
displayName: "No Ingredient",
|
|
|
|
tier: 0,
|
|
|
|
lvl: 0,
|
|
|
|
skills: ["ARMOURING", "TAILORING", "WEAPONSMITHING", "WOODWORKING", "JEWELING", "COOKING", "ALCHEMISM", "SCRIBING"],
|
|
|
|
ids: {},
|
|
|
|
itemIDs: {"dura": 0, "strReq": 0, "dexReq": 0,"intReq": 0,"defReq": 0,"agiReq": 0,},
|
|
|
|
consumableIDs: {"dura": 0, "charges": 0},
|
|
|
|
posMods: {"left": 0, "right": 0, "above": 0, "under": 0, "touching": 0, "notTouching": 0},
|
|
|
|
id: 4000
|
|
|
|
};
|
2022-07-17 21:34:54 +00:00
|
|
|
ingMap.set(ing.displayName, ing);
|
2022-07-17 20:10:50 +00:00
|
|
|
ingList.push(ing.displayName);
|
2022-07-17 21:34:54 +00:00
|
|
|
ingIDMap.set(ing.id, ing.displayName);
|
2021-03-14 07:55:08 +00:00
|
|
|
let numerals = new Map([[1, "I"], [2, "II"], [3, "III"], [4, "IV"], [5, "V"], [6, "VI"]]);
|
2023-04-15 00:18:52 +00:00
|
|
|
|
|
|
|
// pairs of (dura, req)
|
|
|
|
let powder_ing_info = [
|
|
|
|
[-35,0],[-52.5,0],[-70,10],[-91,20],[-112,28],[-133,36]
|
|
|
|
];
|
2021-03-14 07:55:08 +00:00
|
|
|
for (let i = 0; i < 5; i ++) {
|
2023-04-15 00:18:52 +00:00
|
|
|
for (let powder_tier = 0; powder_tier < 6; ++powder_tier) {
|
|
|
|
powder_info = powder_ing_info[powder_tier];
|
2022-07-17 20:10:50 +00:00
|
|
|
let ing = {
|
2023-04-15 00:18:52 +00:00
|
|
|
name: "" + damageClasses[i+1] + " Powder " + numerals.get(powder_tier + 1),
|
2022-07-17 20:10:50 +00:00
|
|
|
tier: 0,
|
|
|
|
lvl: 0,
|
|
|
|
skills: ["ARMOURING", "TAILORING", "WEAPONSMITHING", "WOODWORKING", "JEWELING"],
|
|
|
|
ids: {},
|
|
|
|
isPowder: true,
|
2023-04-15 00:18:52 +00:00
|
|
|
pid: 6*i + powder_tier,
|
|
|
|
itemIDs: {"dura": powder_info[0], "strReq": 0, "dexReq": 0,"intReq": 0,"defReq": 0,"agiReq": 0},
|
2022-07-17 20:10:50 +00:00
|
|
|
consumableIDs: {"dura": 0, "charges": 0},
|
|
|
|
posMods: {"left": 0, "right": 0, "above": 0, "under": 0, "touching": 0, "notTouching": 0}
|
|
|
|
};
|
2021-03-14 07:55:08 +00:00
|
|
|
ing.id = 4001 + ing.pid;
|
2022-08-03 06:42:59 +00:00
|
|
|
ing.displayName = ing.name;
|
2023-04-15 00:18:52 +00:00
|
|
|
ing.itemIDs[skp_order[i] + "Req"] = powder_info[1];
|
2022-07-17 21:34:54 +00:00
|
|
|
ingMap.set(ing.displayName, ing);
|
2022-07-17 20:10:50 +00:00
|
|
|
ingList.push(ing.displayName);
|
2022-07-17 21:34:54 +00:00
|
|
|
ingIDMap.set(ing.id, ing.displayName);
|
2021-03-14 07:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const ing of ings) {
|
2022-07-17 21:34:54 +00:00
|
|
|
ingMap.set(ing.displayName, ing);
|
2022-07-17 20:10:50 +00:00
|
|
|
ingList.push(ing.displayName);
|
2022-07-17 21:34:54 +00:00
|
|
|
ingIDMap.set(ing.id, ing.displayName);
|
2021-03-14 07:55:08 +00:00
|
|
|
}
|
|
|
|
for (const recipe of recipes) {
|
2022-07-17 20:10:50 +00:00
|
|
|
recipeMap.set(recipe.name, recipe);
|
|
|
|
recipeList.push(recipe.name);
|
|
|
|
recipeIDMap.set(recipe.id, recipe.name);
|
2021-03-14 07:55:08 +00:00
|
|
|
}
|
2021-04-01 06:52:43 +00:00
|
|
|
}
|