Zackery23 / DM-ToolTip

0 stars 0 forks source link

APi to useable data #1

Open Zackery23 opened 2 years ago

Zackery23 commented 2 years ago

find a way to get the data from the api and be able to use it in a reasonable fashion to roll for the monster or just have it as a health and turn order

dovezp commented 1 year ago

I got a few ideas for you. First, here is a way to get the data from the API with JavaScript:

fetch('http://example.com/monster-db.json')
  .then((response) => response.json())
  .then((data) => {
  // debug print data
  console.log(data))
  // store the data in the browser cache
  // https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
  window.localStorage.setItem("monster_db", JSON.stringify(data));
  })
  .catch(error => console.log(error));

Now, lets say the game starts. We first should fetch and store all the database json files into the cache. Then let's say the DM needs a "goblin" to enter the map. We would then lookup some monster in monster_db

// load monster db from storage on a new game
let monster_db = window.localStorage.getItem("monster_db");
monster_db = JSON.parse(monster_db); 

// while in the new game obtain goblin information
if (monster_db .hasOwnProperty("goblin")) {
   // pretty much have some sort of storage for current monsters on field for example 'monster_pool'; where we would need another function to write and read all the monsters and there information in this array to/from strings, and then remove them from the array on death.
   // where monster_pool would store / read from sessionStorage
   // https://www.w3schools.com/jsref/prop_win_sessionstorage.asp
   monster_pool.push(new Monster(monster_db.goblin));
}