Esempio n. 1
0
        /// <summary>
        /// Play a list of cards from a user's hand
        /// </summary>
        /// <param name="cardIDs">The card IDs the user has selected </param>
        /// <param name="gameID">The game ID in which the user wants to play the card</param>
        /// <param name="userId">The user Id</param>
        /// <param name="autoPlayed">Were these cards auto played</param>
        /// <returns>PlayCard action result containing any errors and the round the card was played.</returns>
        public Entities.ActionResponses.PlayCard Execute(List <Int32> cardIDs, Int32 gameID, Int32 userId, Boolean autoPlayed)
        {
            Entities.ActionResponses.PlayCard playResponse = new Entities.ActionResponses.PlayCard();

            List <Entities.GamePlayerCard> cards = _selectGamePlayerCard.Execute(gameID, userId);

            //Check that the user did not play cards they do not have
            List <Int32> missedIds = CheckHand(cards, cardIDs);

            if (missedIds.Count == 0)
            {
                Entities.GameRound round = _selectGameRound.Execute(gameID, true);

                //Validate the user isn't trying to answer multiple times
                if (round.HasAnswer(userId) == false)
                {
                    //Validate the correct number of cards were played
                    if (round.ValidateCardPlayedCount(cardIDs.Count))
                    {
                        //Create GameRoundCards for played cards
                        List <Entities.GameRoundCard> playedCards = CreateRoundCards(cardIDs, userId, round.GameRoundID, gameID, autoPlayed);

                        //Insert playedCards
                        _insertGameRoundCard.Execute(playedCards);

                        if (autoPlayed)
                        {
                            playResponse.AutoPlayedSuccess = true;
                        }

                        //Select round with game cards
                        round = _selectGameRound.Execute(gameID, true);

                        //Remove cards from player's hand
                        _deleteGamePlayerCard.Execute(cardIDs, gameID, userId);

                        playResponse.CurrentRound = round;
                        playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.Success;
                    }
                    else
                    {
                        playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.InvalidNumberOfCardsPlayed;
                    }
                }
                else
                {
                    playResponse.CurrentRound = round;
                    playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.Success;
                }
            }
            else
            {
                playResponse.ResponseCode = Entities.ActionResponses.Enums.PlayCardResponseCode.InvalidCardPlayed;
            }

            return(playResponse);
        }
Esempio n. 2
0
        /// <summary>
        /// Play cards for all players that have yet to play
        /// </summary>
        /// <param name="gameID">The game ID</param>
        public void Execute(int gameID)
        {
            Entities.Filters.Game.Select gameFilter = new Entities.Filters.Game.Select();
            gameFilter.DataToSelect = Entities.Enums.Game.Select.GamePlayerCards | Entities.Enums.Game.Select.Rounds;
            gameFilter.GameID       = gameID;

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

            Entities.GameRound round = game.CurrentRound();

            List <Entities.GamePlayer> players = game.Players.Where(x => !round.HasAnswer(x.User.UserId) &&
                                                                    game.IsCurrentPlayer(x.User.UserId) &&
                                                                    !game.IsCurrentCommander(x.User.UserId))
                                                 .Select(x => x).ToList();

            Int32 selectCount = 1 + (Int32)round.Question.Instructions;

            Random rdm = new Random();

            List <Int32> cardIDs = null;

            Entities.ActionResponses.PlayCard response = null;

            foreach (Entities.GamePlayer player in players)
            {
                cardIDs  = player.Hand.OrderBy(x => rdm.Next()).Take(selectCount).Select(x => x.CardID).ToList();
                response = _playCard.Execute(cardIDs, gameID, player.User.UserId, true);

                if (response.AutoPlayedSuccess)
                {
                    if (player.IdlePlayCount + 1 == 3)
                    {
                        //Player is forced to leave game now
                        UnityConfig.Container.Resolve <AppServices.Game.Base.ILeave>().Execute(gameID, player.User, Entities.Enums.GamePlayerType.Player, true);
                    }
                    else
                    {
                        Entities.Filters.GamePlayer.UpdateIdlePlayCount idlePlayCount =
                            new Entities.Filters.GamePlayer.UpdateIdlePlayCount
                        {
                            GameID = gameID,
                            UserId = player.User.UserId
                        };

                        _updateGamePlayer.Execute(idlePlayCount);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="game"></param>
        /// <param name="userId"></param>
        /// <param name="hand"></param>
        /// <param name="playerType"></param>
        /// <param name="voteToKickList"></param>
        /// <param name="completedRounds"></param>
        public GameBoard(Entities.Game game, Int32 userId, Entities.Enums.GamePlayerType playerType,
                         List <Models.Game.Board.VoteToKick> voteToKickList = null,
                         List <Entities.GameRound> completedRounds          = null)
        {
            Game   = game;
            UserId = userId;

            Hand = new List <GamePlayerCard>();

            if (playerType == Enums.GamePlayerType.Player)
            {
                Entities.GamePlayer player = Game.Players.FirstOrDefault(x => x.User.UserId == userId);

                if (player != null && player.Hand != null)
                {
                    Hand = player.Hand;
                }
            }


            Hand         = playerType == Enums.GamePlayerType.Player ? Game.Players.First(x => x.User.UserId == userId).Hand : new List <GamePlayerCard>();
            ActivePlayer = Hand.Count > 0;
            PlayerType   = playerType;

            Entities.GameRound round = Game.CurrentRound();

            if (Game.HasRounds() && round != null)
            {
                Answered       = round.HasAnswer(UserId);
                ShowAnswers    = round.PlayedCount >= round.CurrentPlayerCount && round.Answers.Count > 0;
                RoundHasWinner = round.Winner() != null;
                GroupedAnswers = round.GroupedAnswers();
                IsCommander    = Game.IsCurrentCommander(UserId) && PlayerType == Entities.Enums.GamePlayerType.Player;
            }
            else
            {
                Answered       = false;
                ShowAnswers    = false;
                RoundHasWinner = false;
                IsCommander    = false;
            }

            ShowHand = ActivePlayer && !Answered && !IsCommander && PlayerType == Entities.Enums.GamePlayerType.Player;

            ShowWaiting = (round == null || RoundHasWinner) && Game.IsWaiting();

            WaitingOnAllAnswersOrWinner = !RoundHasWinner && !ShowAnswers;

            ShowBoard = !ShowWaiting && !Game.HasWinner();

            if (ShowBoard && round != null)
            {
                Question = round.Question;
            }
            else
            {
                Question = null;
            }

            AnswersViewModel       = new Answers(RoundHasWinner, IsCommander, WaitingOnAllAnswersOrWinner, ShowAnswers, ShowHand, ShowBoard, GroupedAnswers);
            GameOverViewModel      = new GameOver(Game.HasWinner(), Game.GameID, Game.Players);
            HandViewModel          = new Board.Hand(ShowHand, Hand, ShowBoard);
            LobbyViewModel         = new Lobby(PlayerType, Game.Players, Game.MaxNumberOfSpectators > 0, Game.Spectators);
            RoundQuestionViewModel = new RoundQuestion(round, ShowBoard);
            WaitingViewModel       = new Waiting(ShowWaiting);
            VotesToKickViewModel   = new VotesToKick(voteToKickList ?? new List <Models.Game.Board.VoteToKick>());
            AllRoundsViewModel     = new AllRounds(completedRounds ?? new List <GameRound>());

            HeaderViewModel = new Shared.Header();

            if (Game.HasWinner())
            {
                HeaderViewModel.SubHeaderText = "Game Over, man!";
            }
            else if (ShowWaiting)
            {
                HeaderViewModel.SubHeaderText = ArmedCards.Entities.Models.Helpers.WaitingHeader.Build(Game, UserId, PlayerType);
            }
            else
            {
                HeaderViewModel.SubHeaderText = String.Format("{0} is the Card Commander", Game.DetermineCommander().DisplayName);
            }
        }