From 9f66eeaae8ca2441f8b9d8977f998ccc51d4495b Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 11 May 2023 06:25:09 -0500 Subject: [PATCH] Add caching feature and retainer individual tracking --- .gitignore | 3 +- inventory_monitor.js | 94 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 3224a90..f88fa7b 100644 --- a/.gitignore +++ b/.gitignore @@ -141,4 +141,5 @@ dist # SvelteKit build / generate output .svelte-kit -# End of https://www.toptal.com/developers/gitignore/api/node \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/node +cache \ No newline at end of file diff --git a/inventory_monitor.js b/inventory_monitor.js index 3c1e291..bad22d2 100644 --- a/inventory_monitor.js +++ b/inventory_monitor.js @@ -6,34 +6,118 @@ console.log("Hello!") finalData=[] firstLoad=true +itemCache={} +retainerIDs=[1572880,16,2359328,131092,9,0,13,1048586,2162700] +retainerInventories=[[],[],[],[],[],[],[],[],[]] + +function getRetainerIndex(id){ + switch(id){ + case 1572880:{ + return 0; + } + case 16:{ + return 1; + } + case 2359328:{ + return 2; + } + case 131092:{ + return 3; + } + case 9:{ + return 4; + } + case 0:{ + return 5; + } + case 13:{ + return 6; + } + case 1048586:{ + return 7; + } + case 2162700:{ + return 8; + } + default:{ + console.log("No retainer found with ID "+id+"!") + return -1 + } + } +} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function getItemData(item){ + if (Number.isInteger(item.id)){ + if ((item.id in itemCache)){ + item.name=itemCache[item.id].Name + item.icon=itemCache[item.id].Icon + } else { + if (fs.existsSync("cache/"+item.id)){ + data=JSON.parse(fs.readFileSync("cache/"+item.id)) + console.log("Loaded Item "+item.id+" ("+data.Name+") from Cache") + itemCache[item.id]=data + getItemData(item) + } else { + await axios.get("https://xivapi.com/Item/"+item.id) + .then((data)=>{ + itemCache[item.id]=data.data + fs.writeFileSync("cache/"+item.id,JSON.stringify(data.data)) + console.log("Added Item "+item.id+" ("+data.data.Name+") to Cache") + }) + .catch((err)=>{ + itemCache[item.id]={} + console.log(item.id+" is not a valid Item ID!")}) + await sleep(250) + getItemData(item) + } + } + } +} + function update(){ changed=false fs.readFile("C:\\Users\\sigon\\Documents\\memory", - (err,data)=>{ + async(err,data)=>{ var fileContents = data.toString().split("\n"); if (fileContents.length>=387){ for (i=0;i