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

Resource not Incremented though Chat says it is. #103

Open KonteiKeisei opened 1 month ago

KonteiKeisei commented 1 month ago

I have a worldscript that checks for nat 1s and increments a party resource on that occurrence. The resource is called group inspiration, and the ID is set up properly.

Chat even registers that API call on the rolled 1

However the group resource is not updated:

image

KonteiKeisei commented 1 month ago

Here is the part that uses the API provided:


      if (groupInspiration < maxGroupInspiration) {
        await window.pr.api.increment('group-inspiration', 1);
        ChatMessage.create({
          content: `${actor.name} rolled a natural 1! Group Inspiration increased by 1. Try again, you'll get it eventually!`
        });

Here is the full world script

// Listen to different types of rolls using the appropriate hooks
Hooks.on('ready', () => {
  Hooks.on('dnd5e.rollAbilitySave', (actor, roll, ability) => {
    provideInspiration('rollAbilitySave', actor, roll);
  });

  Hooks.on('dnd5e.rollAbilityTest', (actor, roll, ability) => {
    provideInspiration('rollAbilityTest', actor, roll);
  });

  Hooks.on('dnd5e.rollAttack', (item, roll) => {
    provideInspiration('rollAttack', item, roll);
  });

  Hooks.on('dnd5e.rollSkill', (actor, roll, skill) => {
    provideInspiration('rollSkill', actor, roll);
  });
});

// Function to provide inspiration or update Group Inspiration
async function provideInspiration(rollType, entity, roll) {
  const actor = (rollType === 'rollAttack') ? entity.parent : entity;

  // Ignore NPCs and GMs
  if (!actor || actor.type === 'npc' || game.users.get(actor.id)?.role >= CONST.USER_ROLES.ASSISTANT) return;

  const d20Value = roll.terms.find(term => term.faces === 20)?.total;

  if (d20Value === 1) {  // Natural 1 rolled
    try {
      const groupInspiration = await window.pr.api.get('group-inspiration'); // Using the correct resource ID
      const maxGroupInspiration = 3;

      if (groupInspiration < maxGroupInspiration) {
        await window.pr.api.increment('group-inspiration', 1);
        ChatMessage.create({
          content: `${actor.name} rolled a natural 1! Group Inspiration increased by 1. Try again, you'll get it eventually!`
        });
      } else if (!actor.system.attributes.inspiration) {
        await actor.update({ 'system.attributes.inspiration': true });
        ChatMessage.create({
          content: `${actor.name} rolled a natural 1 As a consolation, they gain personal inspiration!`
        });
      } else {
        ChatMessage.create({
          content: `${actor.name} rolled a natural 1, but both Group and Personal inspiration are full. Even the gods are laughing at this point!`
        });
      }
    } catch (error) {
      console.error("Error handling Group Inspiration:", error);
    }
  }
}
KonteiKeisei commented 1 month ago

Further investigation, this is a permissions issue. World script is not adhering to the "Players can modify" setting in the resource.

davelens commented 1 month ago

@KonteiKeisei Thank you for reporting this. I will try to reproduce this over the weekend, though as you can read in the README.md my free time is currently very thinly spread. If I'm successful I'll plan in a moment to hopefully get a fix out next week.

In the mean time, can you confirm that changes made by the controls in the GUI still work in your world?

KonteiKeisei commented 1 month ago

The gui works perfectly, its just the API via the world script. Not even sure it's a bug, it might just be a world script limitation, either way, I appreciate any time you give!

davelens commented 3 weeks ago

@KonteiKeisei I know it's a bunch later than what I said, but life always gets in the way.

Anyway I had a moment to check this out. Reading up on world scripts I came across this excerpt on this page:

... world scripts are executed on every client. Developers must be careful to ensure that updates are only called from one client to avoid doing duplicate work, and avoid permissions issues from trying to update entities that the client does not have access to update.

That tells me world scripts should adhere to permissions issued on players. Party Resources still requires the Modify Configuration Settings to be enabled on the Player level in the Permissions Configurations setting. If that is set correctly, the world script should respect those settings.

In the instance that no permissions were set, you should get a clear cut message like so: dl-pr-1

I tried a very basic world script to just check if the API call executes with set permissions:

Hooks.once('ready', () => {
  alert('post-ready event')

  Hooks.on('dnd5e.rollAbilityTest', (actor, roll, ability) => {
    alert('rolling ability')
    window.pr.api.increment('foobar', 1)
  });
})

And it did work with my localized test. It incremented a resource, and sent a corresponding message. So in short, I'm not sure why the world script doesn't do what it should be able to do for you. 🫤