-
Hi there, I have read this, however I have a slightly different variation on this question. I am rewriting a game engine for an online board game. At the moment it uses a very crude self-built state machine, however I thought it would be great to formalise the logic and make it more maintainable using xstate. I have the following "state diagram" of the game. In normal mode, there is a simple Pick, Vote and End phase. However the game is customisable with different roles, different intermediate states, and different logic flows. (Green arrows represent some of the main events that cause transitions) This is what a standard game looks like. It has a simple Pick, Vote and End phase. Now this is what a standard game with two extra roles looks like. If there is an Assassin role in play (a player entity), then the state machine should go to the assassin state before ending the game. Similarly, if there is a Lady card in play (a non-player entity), then the state machine should go to the Lady state before returning back to the Pick state. Essentially, I want to somehow be able to make the Game machine include an optional state without hard coding it into the base Game machine, so that it is extendable when new roles are added. I thought about two possible ways to do this via xstate, however I've been running into some issues. I'm not sure if my approach is correct, or whether xstate is suitable for this, so please let me know what you think.
This approach also makes it difficult for a child machine to "override" the Game machine's state. For example the Assassin machine needs to be able to override the Game machine's state and make it the Assassin state, until the child machine is satisfied (i.e. when the assassin makes their selection).
I have a few concerns about this approach however:
How should I go about approaching this? Is there something I have missed? Or perhaps I am using xstate in a way that it initially wasn't designed to be used? Thank you for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Out of curiosity - what board game? I'm a #bgg 😅 Looks like some variation of Mafia/Werewolf/Avalon? As to the topic itself - I think your first intuition might be right: make players separate actors and communicate with the game through events.
This is not true - actors (so including parent and its children) are able to communicate through events at any time! So it's perfectly fine to have long-lived player actors and exchange events between them and the game actor during the game. As to the customizability - a question would be: how many roles do you plan to have there? do roles have to be pluggable? or is it a single game that has its strict rules and it just happens that not all roles are involved in a particular play? I think the latter might be the case and if it is then I wouldn't complicate this too much - I would actually hardcode all of this within the game machine. It would get more lengthy but it would be actually IMHO more readable (as it would be less abstract). Unless... you really want to create an engine for this type of games - then I would craft carefully "placeholders" in the core game machine and go with a factory approach. So basically let caller define extra "rules"/states/etc and inject them into those placeholders with the factory function. This is a more complex solution and requires more thought on your side as only you know how much customizability you would like to have there and what exact requirements do you have. |
Beta Was this translation helpful? Give feedback.
Out of curiosity - what board game? I'm a #bgg 😅 Looks like some variation of Mafia/Werewolf/Avalon?
As to the topic itself - I think your first intuition might be right: make players separate actors and communicate with the game through events.
This is not true - actors (so including parent and its children) are able to communicate through events at any time! So it's perfectly fine to have long-lived player actors and exchange events between them and the game actor during the game.
As to the customizability …