/// <inheritdoc/>
        public CardDeckDto GetCardDeckAndCards(int cardDeckId)
        {
            UnitOfWork unitOfWork = new UnitOfWork(new MemoryGameContext());

            try
            {
                CardDeck cardDeck = unitOfWork.CardDecks.GetCardDeckAndCards(cardDeckId);
                _cardDeckDTO = new CardDeckDto()
                {
                    CardDeckId    = cardDeck.CardDeckId,
                    Name          = cardDeck.Name,
                    BackImage     = cardDeck.BackImage,
                    NumberOfPairs = cardDeck.NumberOfPairs,
                    Cards         = new List <CardDto>()
                };
                _cards = cardDeck.Cards;
                //This code is adding the same set of cards Twice.
                //This is because this way I don't need to store each pair of cards
                //in the database (it would be a waste of storage)

                PopulateCardDeckDtoWithCards();
                PopulateCardDeckDtoWithCards();

                ShuffleCards();
                return(_cardDeckDTO);
            }
            catch (SqlException sqlException)
            {
                _logger.Fatal("CardDeckRetrieverService.cs: An exception was thrown while trying to get a Card entity with " +
                              "the specified primary key (cardDeckId) " +
                              "from the database. Method GetCardDeckAndCards, line 53", sqlException);
                throw;
            }
            catch (EntityException entityException)
            {
                _logger.Fatal("CardDeckRetrieverService.cs: An exception was thrown while trying to access the database. " +
                              "It is possible that the database is corrupted or that it does not exist. " +
                              "Method GetCardDeckAndCards, line 53", entityException);
                throw;
            }
            finally
            {
                unitOfWork.Dispose();
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public IList <MatchDto> GetActiveMatches()
        {
            IList <MatchDto> matchesWaitingToStart = new List <MatchDto>();

            foreach (var match in _matches)
            {
                if (!match.HasStarted)
                {
                    IList <CardDto> cards = new List <CardDto>();

                    foreach (var card in match.ServiceCardDeck.Cards)
                    {
                        CardDto cardDto = new CardDto()
                        {
                            CardId     = card.CardId,
                            FrontImage = card.FrontImage
                        };
                        cards.Add(cardDto);
                    }

                    CardDeckDto cardDeckDto = new CardDeckDto()
                    {
                        CardDeckId = match.ServiceCardDeck.CardDeckId,
                        Name       = match.ServiceCardDeck.Name,
                        BackImage  = match.ServiceCardDeck.BackImage,
                        Cards      = cards
                    };

                    MatchDto matchDto = new MatchDto()
                    {
                        Host               = match.Host,
                        HasStarted         = false,
                        MaxNumberOfPlayers = match.MaxNumberOfPlayers,
                        CardDeckDto        = cardDeckDto
                    };
                    matchesWaitingToStart.Add(matchDto);
                }
            }
            return(matchesWaitingToStart);
        }