From 93cd6db0bc4489a1d7e3d10a4db2e38c0447113b Mon Sep 17 00:00:00 2001 From: Dawid Izydor <30767613+DawidIzydor@users.noreply.github.com> Date: Sat, 4 Feb 2023 13:44:15 +0100 Subject: [PATCH] #71 Adds api wrapper for easier macro usage --- src/init.mjs | 2 ++ src/resources_api_wrapper.mjs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/resources_api_wrapper.mjs diff --git a/src/init.mjs b/src/init.mjs index 8da6d6b..2acf6eb 100755 --- a/src/init.mjs +++ b/src/init.mjs @@ -1,5 +1,6 @@ import ModuleSettings from "./settings.mjs"; import ResourcesApi from "./resources_api.mjs"; +import PartyResources from "./resources_api_wrapper.mjs"; import DashboardDirections from "./dashboard_directions.mjs"; import ResourceNotifications from "./resource_notifications.mjs" import ResourcesDashboard from "./apps/resources_dashboard.mjs" @@ -16,6 +17,7 @@ Hooks.once('init', () => { loadTemplates(templates()) ModuleSettings.register() + window.PartyResources = PartyResources }) Hooks.once('ready', () => { diff --git a/src/resources_api_wrapper.mjs b/src/resources_api_wrapper.mjs new file mode 100644 index 0000000..a3ece41 --- /dev/null +++ b/src/resources_api_wrapper.mjs @@ -0,0 +1,33 @@ +export default class PartyResources{ + static getById(id){ + return window.pr.api.get(id); + } + + static _getResourceId(name){ + return window.pr.api.resources().resources.find(r=>r.name == name)?.id; + } + + static getByName(name){ + return PartyResources.getById(this._getResourceId(name)); + } + + static incrementById(id, amount){ + window.pr.api.increment(id, amount); + } + + static decrementById(id, amount){ + window.pr.api.decrement(id, amount); + } + + static incrementByName(name, amount){ + PartyResources.incrementById(_getResourceId(name), amount); + } + + static decrementByName(name, amount){ + PartyResources.decrementById(_getResourceId(name), amount); + } + + static getResources(){ + return window.pr.api.resources().resources; + } +} \ No newline at end of file