Search code examples
orleans

How to implement turn-based game in Orleans?


I am trying to implement a simple turn based game when N players can join the game before the games begins. Once the game begins, in each round each player takes a turn to roll a pair of dice in order they joined the game. There are three rounds in the game. At the end of the game, each player is awarded points.

For example, assume players P1, P2, P3, P4 joined the game in that order, then in the same order they will go about rolling the pair of dice. After three rounds, assume the results are

P1: (3,5), (4,1), (2,3)
P2: (5,5), (4,3), (1,3)
P3: (3,3), (4,4), (2,1)

For game, I am thinking something like:

public interface IGameBase : IGrainWithGuidKey
{
    Task<bool> IsActive();
    Task Join(IPlayerBase player);
    Task Leave(IPlayerBase player);
}
public interface IGame<TState, in TRequest> : IGameBase
{
    Task<TState> State();
    Task<TState> Play(IPlayerBase player, TRequest request);
}

public interface IPlayerBase : IGrainWithGuidCompoundKey
{
    Task<IPlayerInfo> Info();

    Task<IEnumerable<Guid>> Games();
    
    Task Subscribe(IGameBase game);

    Task Unsubscribe(IGameBase game);
}

How should I implement a way to let players know that it is their turn to play?


Solution

  • I was able to resolve this by implementing it using streams. Basically sent a play message to the stream with game id and player id, and the players listen to these messages. The one whose turn it is will send a play move to game grain.

    The code is here https://github.com/sheeri185adidam/orleanedgames