2021-03-22 21:33:10 +00:00
|
|
|
const MAP_DB_VERSION = 3;
|
2021-03-16 05:40:05 +00:00
|
|
|
|
|
|
|
// @See https://github.com/mdn/learning-area/blob/master/javascript/apis/client-side-storage/indexeddb/video-store/index.js
|
|
|
|
|
|
|
|
let mdb;
|
|
|
|
let mreload = false;
|
|
|
|
var terrs = new Map(); //terr name : location: rectangle def {startX, startY, endX, endY
|
|
|
|
var claims = new Map(); //terr name: guild name
|
|
|
|
var neighbors = new Map(); //terr name: [neighbor names]
|
|
|
|
var resources = new Map(); //terr name: Map({emeralds: bool, doubleemeralds: bool, doubleresource: bool, resources: [], storage: []})
|
2021-03-22 21:33:10 +00:00
|
|
|
var maplocs = []; //literally the object array from maploc_compress
|
2021-03-16 05:40:05 +00:00
|
|
|
var terrdata;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load item set from local DB. Calls init() on success.
|
|
|
|
*/
|
|
|
|
async function map_load_local(init_func) {
|
2021-03-22 21:33:10 +00:00
|
|
|
let get_tx = mdb.transaction(['map_db','maploc_db'], 'readonly');
|
2021-03-16 05:40:05 +00:00
|
|
|
let map_store = get_tx.objectStore('map_db');
|
2021-03-22 21:33:10 +00:00
|
|
|
let maploc_store = get_tx.objectStore('maploc_db');
|
2021-03-16 05:40:05 +00:00
|
|
|
let request5 = map_store.getAll();
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
request5.onerror = function(event) {
|
|
|
|
console.log("Could not read local map db...");
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
request5.onsuccess = function(event) {
|
|
|
|
console.log("Successfully read local map db.");
|
|
|
|
terrdata = request5.result;
|
2021-03-22 21:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_tx = mdb.transaction(['maploc_db'], 'readonly');
|
|
|
|
map_store = get_tx.objectStore('maploc_db');
|
|
|
|
let request6 = maploc_store.getAll();
|
|
|
|
|
|
|
|
request6.onerror = function(event) {
|
|
|
|
console.log("Could not read local map locations db...");
|
|
|
|
}
|
|
|
|
request6.onsuccess = function(event) {
|
|
|
|
console.log("Successfully read local locations map db.");
|
|
|
|
maplocs = request6.result;
|
2021-03-16 05:40:05 +00:00
|
|
|
init_map_maps();
|
|
|
|
init_func();
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
await get_tx.complete;
|
|
|
|
mdb.close();
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load item set from remote DB (aka a big json file). Calls init() on success.
|
|
|
|
*/
|
|
|
|
async function load_map(init_func) {
|
|
|
|
|
|
|
|
let getUrl = window.location;
|
|
|
|
let baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
|
|
|
|
let url = baseUrl + "/terrs_compress.json";
|
|
|
|
url = url.replace(/\w+.html/, "") ;
|
|
|
|
let result = await (await fetch(url)).json();
|
2021-03-22 21:33:10 +00:00
|
|
|
terrdata = result;
|
2021-03-16 05:40:05 +00:00
|
|
|
|
2021-03-22 21:33:10 +00:00
|
|
|
url = baseUrl + "/maploc_compress.json";
|
|
|
|
url = url.replace(/\w+.html/, "");
|
2021-03-16 05:40:05 +00:00
|
|
|
result = await (await fetch(url)).json();
|
2021-03-22 21:33:10 +00:00
|
|
|
maplocs = result.locations;
|
|
|
|
|
2021-04-12 21:30:16 +00:00
|
|
|
refreshData();
|
2021-03-16 05:40:05 +00:00
|
|
|
console.log(terrdata);
|
2021-03-22 21:33:10 +00:00
|
|
|
console.log(maplocs);
|
2021-03-16 05:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/clear
|
|
|
|
/*let clear_tx2 = db.transaction(['ing_db'], 'readwrite');
|
|
|
|
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;
|
|
|
|
await clear_tx3.complete;*/
|
|
|
|
let add_promises = [];
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
let add_tx2 = mdb.transaction(['map_db'], 'readwrite');
|
|
|
|
let map_store = add_tx2.objectStore('map_db');
|
|
|
|
for (const terr of Object.entries(terrdata)) {
|
|
|
|
add_promises.push(map_store.add(terr[1],terr[0])); //WHY? WHY WOULD YOU STORE AS VALUE, KEY? WHY NOT KEEP THE NORMAL KEY, VALUE CONVENTION?
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
|
|
|
let add_tx3 = mdb.transaction(['maploc_db'], 'readwrite');
|
|
|
|
let maploc_store = add_tx3.objectStore('maploc_db');
|
|
|
|
for (const i in maplocs) {
|
|
|
|
add_promises.push(maploc_store.add(maplocs[i],i));
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
add_promises.push(add_tx2.complete);
|
2021-03-22 21:33:10 +00:00
|
|
|
add_promises.push(add_tx3.complete);
|
2021-03-16 05:40:05 +00:00
|
|
|
|
|
|
|
Promise.all(add_promises).then((values) => {
|
|
|
|
mdb.close();
|
|
|
|
init_map_maps();
|
|
|
|
init_func();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function load_map_init(init_func) {
|
|
|
|
//uncomment below line to force reload
|
2021-03-22 21:33:10 +00:00
|
|
|
|
|
|
|
// window.indexedDB.deleteDatabase("map_db", MAP_DB_VERSION);
|
|
|
|
// window.indexedDB.deleteDatabase("maploc_db", MAP_DB_VERSION);
|
2021-04-02 22:09:29 +00:00
|
|
|
if (mdb) {
|
|
|
|
console.log("Map db already loaded, skipping load sequence");
|
|
|
|
init_func();
|
|
|
|
return;
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
let request = window.indexedDB.open("map_db", MAP_DB_VERSION)
|
|
|
|
request.onerror = function() {
|
|
|
|
console.log("DB failed to open...");
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onsuccess = function() {
|
|
|
|
mdb = request.result;
|
|
|
|
if (!mreload) {
|
|
|
|
console.log("Using stored data...")
|
|
|
|
map_load_local(init_func);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("Using new data...")
|
|
|
|
load_map(init_func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onupgradeneeded = function(e) {
|
|
|
|
mreload = true;
|
|
|
|
|
|
|
|
let mdb = e.target.result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
mdb.deleteObjectStore('map_db');
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("Could not delete map DB. This is probably fine");
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
try {
|
|
|
|
mdb.deleteObjectStore('maploc_db');
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("Could not delete map location DB. This is probably fine");
|
|
|
|
}
|
2021-03-16 05:40:05 +00:00
|
|
|
|
|
|
|
mdb.createObjectStore('map_db');
|
2021-03-22 21:33:10 +00:00
|
|
|
mdb.createObjectStore('maploc_db');
|
2021-03-16 05:40:05 +00:00
|
|
|
|
|
|
|
console.log("DB setup complete...");
|
|
|
|
}
|
2021-04-12 21:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Saves map data. Meant to be called after territories and guilds are refreshed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function save_map_data() {
|
|
|
|
let add_promises = [];
|
|
|
|
|
|
|
|
let add_tx2 = mdb.transaction(['map_db'], 'readwrite');
|
|
|
|
let map_store = add_tx2.objectStore('map_db');
|
|
|
|
for (const terr of Object.entries(terrdata)) {
|
|
|
|
add_promises.push(map_store.add(terr[1],terr[0])); //WHY? WHY WOULD YOU STORE AS VALUE, KEY? WHY NOT KEEP THE NORMAL KEY, VALUE CONVENTION?
|
|
|
|
}
|
|
|
|
|
|
|
|
let add_tx3 = mdb.transaction(['maploc_db'], 'readwrite');
|
|
|
|
let maploc_store = add_tx3.objectStore('maploc_db');
|
|
|
|
for (const i in maplocs) {
|
|
|
|
add_promises.push(maploc_store.add(maplocs[i],i));
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-04-12 21:30:16 +00:00
|
|
|
add_promises.push(add_tx2.complete);
|
|
|
|
add_promises.push(add_tx3.complete);
|
|
|
|
|
|
|
|
Promise.all(add_promises).then((values) => {
|
|
|
|
mdb.close();
|
|
|
|
init_map_maps();
|
|
|
|
init_func();
|
|
|
|
});
|
2021-03-16 05:40:05 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:30:16 +00:00
|
|
|
|
|
|
|
|
2021-03-16 05:40:05 +00:00
|
|
|
function init_map_maps() {
|
|
|
|
for (const [terr,data] of Object.entries(terrdata)) {
|
|
|
|
terrs.set(data.territory,data.location);
|
2021-04-12 22:32:50 +00:00
|
|
|
// claims.set(data.territory,data.guild);
|
|
|
|
// if (!guilds.includes(data.guild)) {
|
|
|
|
// guilds.push(data.guild);
|
|
|
|
// }
|
2021-03-16 05:40:05 +00:00
|
|
|
neighbors.set(data.territory,data.neighbors);
|
|
|
|
resources.set(data.territory,{"resources":data.resources,"storage":data.storage,"emeralds":data.emeralds,"doubleemeralds":data.doubleemeralds,"doubleresource":data.doubleresource});
|
|
|
|
}
|
2021-03-22 21:33:10 +00:00
|
|
|
|
2021-04-02 22:09:29 +00:00
|
|
|
}
|