Esempio n. 1
0
        /// <summary>
        /// Join a game
        /// </summary>
        /// <param name="gameID">The id of the game to join</param>
        /// <param name="user">The current user</param>
        /// <param name="passphrase">The passphrase for the game</param>
        /// <param name="playerType">Type of player joining</param>
        /// <returns>The response to a join request</returns>
        public Entities.JoinResponse Execute(Int32 gameID, Entities.User user, String passphrase,
                                             Entities.Enums.GamePlayerType playerType)
        {
            Entities.Filters.Game.Select filter = new Entities.Filters.Game.Select();
            filter.GameID        = gameID;
            filter.DataToSelect |= Entities.Enums.Game.Select.Rounds;
            filter.DataToSelect |= Entities.Enums.Game.Select.GamePlayerCards;

            Entities.Game game = _selectGame.Execute(filter);

            Entities.JoinResponse response = _joinGame.Execute(game, user, passphrase, playerType);

            if (response.Game != null)
            {
                if (response.Game.IsWaiting() &&
                    response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.SuccessfulAlreadyPlayer) == false)
                {
                    _sendMessage.UpdateWaiting(response.Game, true);
                }
                else if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.NewRoundStart) == true)
                {
                    _sendMessage.UpdateGame(response.Game, true);
                }
                else
                {
                    _sendMessage.UpdateLobby(response.Game, true);
                }
            }

            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Starts a game if certain requirements are met
        /// </summary>
        /// <param name="gameID">The ID for the game to start</param>
        /// <param name="commander">The commander for the first round</param>
        /// <returns>The started game</returns>
        public Entities.Game Execute(Int32 gameID, Entities.User commander)
        {
            Entities.Filters.Game.Select filter = new Entities.Filters.Game.Select();
            filter.GameID       = gameID;
            filter.DataToSelect = Entities.Enums.Game.Select.GamePlayerCards;

            Entities.Game game = _selectGame.Execute(filter);

            Entities.GamePlayer host = game.Players.First();

            if (game.IsCurrentCommander(commander.UserId))
            {
                Boolean successful = _startRound.Execute(game, commander);

                if (successful)
                {
                    _sendMessage.UpdateGame(game, true);
                }
            }

            return(game);
        }
Esempio n. 3
0
        /// <summary>
        /// Removes a player from the game
        /// </summary>
        /// <param name="gameID">The ID of the game to leave</param>
        /// <param name="user">The user leaving the game</param>
        /// <param name="playerType">Type of player leaving</param>
        /// <param name="forcedToLeave">The player was forced to leave</param>
        public void Execute(Int32 gameID, Entities.User user, Entities.Enums.GamePlayerType playerType, Boolean forcedToLeave = false)
        {
            Entities.Filters.Game.Select filter = new Entities.Filters.Game.Select();
            filter.GameID       = gameID;
            filter.DataToSelect = Entities.Enums.Game.Select.Rounds | Entities.Enums.Game.Select.GamePlayerCards;

            Entities.Game game = _selectGame.Execute(filter);

            Boolean wasWaiting = game.IsWaiting();

            Boolean wasCurrentCommander = game.IsCurrentCommander(user.UserId) && playerType == Entities.Enums.GamePlayerType.Player;

            Entities.GamePlayer player = null;

            if (playerType == Entities.Enums.GamePlayerType.Player)
            {
                player = game.Players.Find(x => x.User.UserId == user.UserId);
                game.Players.Remove(player);
                game.PlayerCount--;
            }
            else
            {
                player = game.Spectators.Find(x => x.User.UserId == user.UserId);

                game.Spectators.Remove(player);
                game.SpectatorCount--;
            }

            if (wasCurrentCommander)
            {
                if (game.HasRounds())
                {
                    Entities.GameRound current = game.CurrentRound();

                    if (!current.HasWinner())
                    {
                        game.Rounds.Remove(current);
                        game.RoundCount--;

                        Entities.Filters.GameRound.Delete deleteRoundFilter = new Entities.Filters.GameRound.Delete();
                        deleteRoundFilter.GameRoundID = current.GameRoundID;

                        _deleteRound.Execute(deleteRoundFilter);
                    }
                }

                Boolean started = false;

                if (game.PlayerCount > 0)
                {
                    started = _startRound.Execute(game, game.NextCommander(null));
                }

                if (game.HasRounds() && started)
                {
                    _sendMessage.CommanderLeft(game, user.DisplayName);
                }
                else
                {
                    _sendMessage.UpdateGame(game, true);
                }
            }
            else if (game.IsWaiting() && wasWaiting)
            {
                _sendMessage.UpdateWaiting(game, true);
            }
            else
            {
                if (game.HasRounds())
                {
                    Entities.GameRound current = game.CurrentRound();
                    current.CurrentPlayerCount--;

                    if (!game.Players.Any(x => x.Hand.Count > 0 && !current.IsCommander(x.User.UserId)))
                    {
                        if (!current.HasWinner() && current.Answers.Count == 0)
                        {
                            _dealCards.Execute(game, false);
                        }
                    }
                }

                _sendMessage.UpdateGame(game, true, (forcedToLeave ? (int?)user.UserId : null));
            }

            _leaveGame.Execute(gameID, user, playerType);
        }