Exemplo 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);
        }