qor / transition

Transition is a Golang state machine implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use one event name to transition from many states

csterritt opened this issue · comments

Hello,

First, thanks for doing this package, I appreciate it. And my apologies if I'm doing this wrong -- I'd appreciate any input you'd care to give.

What I'm doing is modeling a multi-player game. I start with an emptyTable state, and trigger addPlayer events to it.

My first thought was to have a "ladder" of states, so:

emptyTable -> addPlayer -> onePlayer
onePlayer -> addPlayer -> twoPlayers
onePlayer -> removePlayer -> emptyTable

and so on. So the 'counting' of players would be encoded by what state the system is in.

However, I found that there can be a single event with a given name, so the above is impossible.

It seems reasonable to encode events by fromState/eventName pairs, rather than just event name?

Now, I would have to give these events different names, and then look at the current state to decide which event name to use, given where the machine is at the time. But I kind of feel like I'm doing the state machine's job for it at that point.

I suppose that I could count the users in the struct, and fail on the addPlayer event if there are already enough players at the table. But then I have to decide how to signal the system that there are enough players, so that "Start Game" buttons can be shown... although I suppose I could trigger an event from inside the handler for the addPlayer event.

Hi @csterritt,
I just read your comment because I got curious about the issue, hehe.
I think you may be implementing your state machine in a wrong way.

Try these states:
(Waiting player) ---t1---> (playing) ---t2---> (other state)

AddPlayer may be a method, that checks the number of players and then execute the transition t1, that could be the event "teams completed".
But if you, for any reason, really have different states for each number of players, then forget my comments.

Hi @rubens21,

Thanks! I'm doing something like that, with another library.