kyleady / inkpot-powerroll

GNU General Public License v3.0
2 stars 2 forks source link

[BugFix] Add Effects to Unowned Tokens #1

Closed kyleady closed 1 year ago

kyleady commented 1 year ago

Problem

Players that tried to use the PowerRoll Effects feature to add an effect like "Prone" to a token they did not own would encounter an error from FoundryVTT. Fundamentally, FoundryVTT prevents users from editing tokens they do not own.

Solution

Whenever a user tries to use PowerRoll Effects feature to add an effect to a token they do not own, the request will be piped to a GM user that is online. The GM user's client will fulfill the request. If no GM is currently online, the user will be told to wait until a GM is online.

The user should be entirely unaware that this is happening.

Implementation

When a user attempts to add an effect to a token they own, the effect will be added immediately by the user's client.

When a user attempts to add an effect to a token they do not own, their client will check if a GM is online and warn the user if no GM is currently online. If an online GM is found, the user's client will record a request on the user's game.user.character in its flags.

{
    ...
    flags: {
        ...
        inkpot-powerroll: {
            <UNIX_TIMESTAMP>: {
                gmId: <ID OF THE GM USER THAT WILL FULFILL THE REQUEST>,
                sceneId: <ID OF THE SCENE THAT THE TARGET TOKEN IS IN>,
                tokenId: <ID OF THE TARGET TOKENN>,
                effectDefinition: <EFFECT DOCUMENT THAT WILL BE ADDED TO THE TARGET TOKEN>
            }
        ...
    }
    ...
}

A hook on the GM user's client will see the flag being created on the original user's token, fulfill the request, and delete the successfully processed flag.

Edge Case

There is an edge case of the user making a request as one of multiple GMs signs off. The user's client could see the signing off GM still logged on and happen to select that one GM to fulfill the request. By the time the GM user would have received the request, they are already signed off. The user will get no warning and a flag will linger on their default character. Further, the user would be confused as they attempted to add an effect on a token but nothing happened, even though there are still GMs logged on.

The user can try again, with the remaining GMs still logged on. A new request is made for a GM that is logged on, and the user will experience no further issues. That is to say, if it doesn't work, try again.

To prevent flags from building up due to unfulfilled requests, default tokens are cleaned of any lingering requests on their default character each time the user logs on.