witchertrpg-foundryvtt / TheWitcherTRPG

GNU General Public License v3.0
14 stars 5 forks source link

[BUG] When buying less then the whole stack, the whole stack is added to the buyer #135

Open SquidOfDoomed opened 3 weeks ago

SquidOfDoomed commented 3 weeks ago

Description:

I'm encountering an issue where players cannot successfully purchase items from a shop in Foundry VTT. When a player clicks "purchase," the chat logs the transaction, but the item is not added to their inventory. This issue occurs with both custom shops and the default shop functionality. Foundry VTT Version:

12.33.1 The Witcher TRPG Version:

12.2.1 Modules Installed:

Monk's Enhanced Journal 12.01
lib-wrapper 1.12.14.0

Steps to Reproduce:

Create a shop and configure it so that players can buy items.
Add items to the shop.
Show the shop to players and give them observer rights.
A player clicks "purchase" for an item.
The chat shows that the item was bought, but it is not added to the player's inventory.

Console Error:

The following error is shown in the console when an item is purchased:

javascript

Uncaught (in promise) TypeError: event.dataTransfer is undefined [Detected 3 packages: system:TheWitcherTRPG(v12.2.1), monks-enhanced-journal(12.01), lib-wrapper(1.12.14.0)] _onDropItem itemMixin.js:23 grantItem LootSheet.js:511 jQuery 9 activateListeners LootSheet.js:127 renderSubSheet enhanced-journal.js:439 _render enhanced-journal.js:149 render foundry.js:5793

monks-enhanced-journal.js:4270 #call foundry.js:632 callAll foundry.js:589 handleUpdateDocuments foundry-esm.js:56584 documents foundry-esm.js:56590 #handleUpdateDocuments foundry-esm.js:56590 _updateDocuments foundry-esm.js:56473 update foundry-esm.js:12329 updateDocuments foundry-esm.js:11473 update foundry-esm.js:11584 call_wrapped libWrapper-wrapper.js:507 updateJournalEntryPage monks-enhanced-journal.js:819 call_wrapper libWrapper-wrapper.js:616 update#0 libWrapper-wrapper.js:189 setFlag foundry-esm.js:11824 requestLoot monks-enhanced-journal.js:3265 onMessage monks-enhanced-journal.js:2669 emit index.mjs:136 value socket.js:538 value socket.js:525 value socket.js:495 emit index.mjs:136 value manager.js:204 promise callback*ce< websocket-constructor.browser.js:5 value manager.js:203 emit index.mjs:136 value index.js:146 value manager.js:190 emit index.mjs:136 value socket.js:341 emit index.mjs:136 value transport.js:98 value transport.js:90 onmessage websocket.js:68 Analysis: The error suggests that event.dataTransfer is undefined when attempting to drop an item, indicating a possible issue with how item transactions are handled. This problem occurs even with the default shop, leading me to believe the issue might be with the core handling of item transfers in the Witcher TRPG system, possibly in combination with or exacerbated by other modules like Monk's Enhanced Journal. Expected Behavior: When a player purchases an item, it should be automatically added to their inventory without errors. Temporary Workaround: I have tried disabling Monk's Enhanced Journal, but the issue persists, indicating the problem might be within the Witcher TRPG system itself. Additional Context: This issue significantly impacts gameplay, as it prevents players from acquiring items through shops. Any guidance or fixes would be greatly appreciated.
Stexinator commented 3 weeks ago

Buying works from the loot sheet. If you want to increase the compatibility with Monks Module I need more info what they need. I wont research it

SquidOfDoomed commented 3 weeks ago

Good point. Cant supply that since I am still starting off with foundry. Tried the same thing with the default loot and no modules and could not purchase items with a test character. I created a user, an item, a character (555 crowns) and a loot sheet containing the item (for 50 crowns). I updated the test user permissions (owner of character, observer of shop) and logged in as that account. Trying to purchase it led to the following error on console:

Uncaught (in promise) TypeError: buyerActor is undefined
    _onItemBuy https://<ip>:30000/systems/TheWitcherTRPG/module/actor/sheets/WitcherLootSheet.js:250
    jQuery 2
        dispatch
        handle

Result: No items were transferred but money removed. It is very possible that one of my steps was wrong or I am missing some setting. If so, it would be great if you could point me towards the solution. This was the main reason I tried to use something like Monks Enhanced Journal.

If this should be another bug, I can also report it. My hope is that both could be the same issue since the behavior is identical. Thanks for your quick response by the way!

Stexinator commented 3 weeks ago

I see, the buyer needs to be owner of the loot actor due to the content of the loot actor being changed. Can you try with updated permissions?

Can you open a bug for the deduction of money. It should only be spent if the target actor can get the item

SquidOfDoomed commented 3 weeks ago

Did create that bug report: https://github.com/witchertrpg-foundryvtt/TheWitcherTRPG/issues/136 I also tried with owner. Now the buy works. Sadly not as expected. If you have x50 of an item and you click buy it removes the crowns of one item but adds 50 items to the character sheet. The shop then results having x49 items left. Screenshot: image

SquidOfDoomed commented 2 weeks ago

I looked a bit further into this and it seems to me that the "createEmbeddedDocuments" function is using the readonly _stats (item base) values instead of the system.quantity that is set. I managed to get it to work via updating the item after creation (module/actor/sheets/WitcherLootSheet.js:112): await createdItem.update({ "system.quantity": newItem.system.quantity });

I am aware that this is a potentially ugly solution but it works better than before. It is possible that I understood the documentation wrongly. I'm open for feedback on this.

What do you think?