davelens / fvtt-party-resources

A simple module for FoundryVTT to manage (and track) custom party-wide numeric values.
MIT License
6 stars 16 forks source link

Feature - Api Wrapper #71

Open DawidIzydor opened 1 year ago

DawidIzydor commented 1 year ago

Feature: add api wrapper for easier interactions with this module through macros

I have this feature already implemented, will create a PR shortly

DawidIzydor commented 1 year ago

Created PR #72

DawidIzydor commented 1 year ago

Example usage - using PartyResources to manage cooking ingredients from the Pathfinder Kingmaker AP


new Dialog({
  title:'Add resource',
  content:`
    <form>
      <div class="form-group">
        <label>Basic ingredient</label>
        <input type='number' name='basicIngredient'></input>
      </div>
      <div class="form-group">
        <label>Special ingredient</label>
        <input type='number' name='specialIngredient'></input>
      </div>
    </form>`,
  buttons:{
    yes: {
      icon: "<i class='fas fa-check'></i>",
      label: `Apply Changes`
    }},
  default:'yes',
  close: html => {
    let basicIngredient = html.find('input[name=\'basicIngredient\']');
    if (basicIngredient.val()!== '') {
        PartyResources.incrementByName('Basic Ingredient', Number(basicIngredient.val()));
      }
    let specialIngredient = html.find('input[name=\'specialIngredient\']');
    if (specialIngredient.val()!== '') {
        PartyResources.incrementByName('Special Ingredient', Number(specialIngredient.val()));
    }
  }
}).render(true);```
davelens commented 1 year ago

@DawidIzydor There already is a public-facing API, it's outlined in the FAQ.

DawidIzydor commented 1 year ago

That's true but this wrapper makes it more intuitive to use

davelens commented 1 year ago

@DawidIzydor Sorry for taking this long to respond, got a lot on my plate at the moment.

I reviewed this in more detail and will redact my previous comment. incrementByName and decrementByName can be useful in itself and they're probably a good idea to incorporate in the module. Though what I don't agree with is having two API endpoints where one contains several methods the other one doesn't have. I'm fine with having window.PartyResources as an alias to window.pr.api, but I'd aggregate incrementByName, decrementByName, _getResourceId, and getByName into window.pr.api so all API methods reside in the same class. Does that make sense to you?