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

Players cannot use macro w/ API calls to update resources unless elevated roles #105

Open Lyinggod opened 2 weeks ago

Lyinggod commented 2 weeks ago

I have created a macro that is intended to allow players to modify a resource via dialog such as entering "+10000" to increment the current resource or "-512" to decrement it. Unless the player role allows for updating System Configurations, the macro fails. Giving this blanket authorization to all players seems.... bad.

Please allow for API calls via macro to update the resources by players with base role. I have tried using the Advanced Macro's module to allow macro use by everyone, but the problem persists.

davelens commented 2 weeks ago

Please allow for API calls via macro to update the resources by players with base role. I have tried using the Advanced Macro's module to allow macro use by everyone, but the problem persists.

It's not a question of allowing or not allowing, it's a restriction of Foundry's world settings (where the data is currently stored). The FAQ explains why.

Lyinggod commented 2 weeks ago

Sorry. I saw that and took it to mean that it referred to the "Allow players to manage resource" checkbox

Lyinggod commented 2 weeks ago

On a whim, I asked CHATGPT a generalized question about this issue. I know that this can result in non-functional solutions. I dont know about the following but I thought I would put it here just in case it was useful. It seems to imply that the GM may need to directly authorize the resource update but it could be that being logged in is enough:

Rather than directly allowing players to update settings, you can create a custom socket in your module that listens for player requests to update data. This approach allows players to send data to the GM or another trusted user, who then executes the setting update on their behalf. Here’s a basic outline of how to implement this:

Server Side (GM executes the command): Set up a socket listener that only the GM can act on.
Player Side (Client sends the request): Players can emit an event with the necessary data to the socket.

// Initialize Socket (GM checks)
if (game.user.isGM) {
  game.socket.on('module.yourModuleName', (data) => {
    if (data.action === "updateSetting") {
      game.settings.set("yourModuleName", data.key, data.value);
    }
  });
}

// Player function to emit data
function updateModuleSetting(key, value) {
  if (game.user.isGM) {
    // If the player happens to be the GM, perform the update directly
    game.settings.set("yourModuleName", key, value);
  } else {
    // Emit data to GM
    game.socket.emit('module.yourModuleName', {
      action: "updateSetting",
      key: key,
      value: value,
    });
  }
}