Exemple #1
0
        public async Task PostMessage(CatanMessage message)
        {
            if (message == null)
            {
                return;
            }
            if (message.GameInfo == null)
            {
                return;
            }
            if (message.GameInfo.Id == null)
            {
                return;
            }

            Game game = Games.GetGame(message.GameInfo.Id);

            if (game != null)
            {
                game.PostLog(message, false);
            }

            string gameId = message.GameInfo.Id.ToString();

            message.MessageDirection = MessageDirection.ServerToClient;

            switch (message.MessageType)
            {
            case MessageType.BroadcastMessage:
                message.MessageType = MessageType.BroadcastMessage;
                await Clients.Group(gameId).ToAllClients(message);

                break;

            case MessageType.PrivateMessage:
                await Clients.Group(gameId).ToAllClients(message);      // no private message for now

                break;

            case MessageType.CreateGame:

                //
                //  do nothing if game is already created
                if (game == default)
                {
                    game = new Game()
                    {
                        GameInfo = message.GameInfo
                    };
                    Games.AddGame(message.GameInfo.Id, game);
                    await Groups.AddToGroupAsync(Context.ConnectionId, gameId);

                    game.PostLog(message);
                }
                else
                {
                    //
                    //  this will set the creator correctly
                    message.GameInfo = game.GameInfo;
                }

                //
                //  tell *all* the clients that a game was created
                await Clients.All.CreateGame(message);

                break;

            case MessageType.DeleteGame:
                Games.DeleteGame(message.GameInfo.Id, out Game _);
                //
                //  tell *all* the clients that a game was deleted
                await Clients.All.DeleteGame(message);

                break;

            case MessageType.RejoinGame:
                //
                //  this will only fail if the player is already there, but we don't care about that
                game.NameToPlayerDictionary.TryAdd(message.From, new Player(game.GameLog));

                await Groups.AddToGroupAsync(Context.ConnectionId, gameId);

                //
                //  the difference between Join and Rejoin is that on Rejoin only the caller is told
                //  the client will (presumably) make the rejoin seamless to the user
                await Clients.Caller.RejoinGame(message);

                break;

            case MessageType.JoinGame:

                //
                //  this will only fail if the player is already there, but we don't care about that
                game.NameToPlayerDictionary.TryAdd(message.From, new Player(game.GameLog));

                await Groups.AddToGroupAsync(Context.ConnectionId, gameId);

                //
                //  notify everybody else in the group that somebody has joined the game
                await Clients.Group(gameId).JoinGame(message);

                break;

            case MessageType.LeaveGame:
                game.NameToPlayerDictionary.TryRemove(message.From, out Player _);
                _ = Groups.RemoveFromGroupAsync(Context.ConnectionId, gameId);
                await Clients.Group(gameId).LeaveGame(message);

                break;

            case MessageType.Ack:
                await Clients.Group(gameId).OnAck(message);

                break;

            default:
                break;
            }
        }