Exemplo n.º 1
0
        /// <summary>
        /// Complete the current round
        /// </summary>
        /// <param name="gameID">The ID of the game that contains the round</param>
        /// <param name="cardIDs">The IDs of the winning cards</param>
        /// <param name="userId">The user Id trying to complete the round</param>
        /// <returns></returns>
        public Entities.ActionResponses.RoundComplete Execute(Int32 gameID, List <Int32> cardIDs, Int32 userId)
        {
            Entities.ActionResponses.RoundComplete response = new Entities.ActionResponses.RoundComplete();

            Entities.GameRound currentRound = _selectGameRound.Execute(gameID, true);

            //Validate that the user trying to complete the round is in fact the commander
            if (currentRound.IsCommander(userId))
            {
                //Validate that select cards were actually played during the round
                List <Int32> invalidWinners = currentRound.ValidateWinnerSelection(cardIDs);

                if (invalidWinners.Count == 0)
                {
                    Entities.User newCommander = currentRound.Winner();

                    //Update cards as winners
                    Entities.Filters.GameRoundCard.UpdateWinner cardfilter = new Entities.Filters.GameRoundCard.UpdateWinner();
                    cardfilter.CardIDs = cardIDs;
                    cardfilter.GameID  = gameID;

                    Boolean autoPlayed = _updateGameRoundCard.Execute(cardfilter);

                    if (!autoPlayed)
                    {
                        //Update player points
                        Entities.Filters.GamePlayer.UpdatePoints playerFilter = new Entities.Filters.GamePlayer.UpdatePoints();
                        playerFilter.GameID = gameID;
                        playerFilter.UserId = newCommander.UserId;

                        _updateGamePlayer.Execute(playerFilter);
                    }

                    //Start round
                    Entities.Filters.Game.Select gameFilter = new Entities.Filters.Game.Select();
                    gameFilter.GameID       = gameID;
                    gameFilter.DataToSelect = Entities.Enums.Game.Select.GamePlayerCards;

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

                    response.NewRoundCreated = _startGameRoud.Execute(game, game.NextCommander(newCommander));

                    response.CompletedRound = currentRound;
                    response.Game           = game;

                    if (!response.NewRoundCreated)
                    {
                        response.Game.Rounds.Add(currentRound);
                    }
                }
            }

            return(response);
        }
Exemplo n.º 2
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);
        }