Exemple #1
0
        public void OpenFirstBoosterPackage(RequestContext requestContext)
        {
            Assert.NotNull(requestContext, nameof(requestContext));

            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
                    User        user    = userRepository.GetUser(session.UserID, transaction);
                    Assert.NotNull(user, nameof(user));

                    if (!boosterRepository.HasBoosterPackages(user.UserID, transaction))
                    {
                        throw new NoBoosterPackageAssignedException($"Cannot Open Booster- package. No Booster- Packages found for User: {user.UserName}.");
                    }

                    BoosterPack firstBoosterPack = boosterRepository.GetFirstBooserPackage(user.UserID, transaction);
                    Assert.NotNull(firstBoosterPack, nameof(firstBoosterPack));

                    foreach (BoosterPackCard assignedCard in firstBoosterPack.AssignedCards)
                    {
                        cardLibraryRepository.AddCardToLibrary(assignedCard.CardID, firstBoosterPack.UserID);
                    }

                    boosterRepository.DeleteBooster(firstBoosterPack, transaction);
                    transaction.Commit();
                }
        }
Exemple #2
0
 public Card GetFirstAvailableCardForTrade(RequestContext requestContext)
 {
     using (NpgsqlConnection connection = database.CreateAndOpenConnection())
         using (NpgsqlTransaction transaction = connection.BeginTransaction())
         {
             UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
             return(cardRepository.GetCardsWithoutDeck(session.UserID, transaction)
                    .FirstOrDefault());
         }
 }
Exemple #3
0
        public IEnumerable <CardLibraryItem> GetCardLibrary(RequestContext requestContext)
        {
            Assert.NotNull(requestContext, nameof(requestContext));

            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
                    return(cardLibraryRepository.GetCardLibraryItemsByUserID(session.UserID, transaction));
                }
        }
Exemple #4
0
        public void CreateTrade(Guid tradeID, Card card, RequestContext requestContext)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);

                    ValidateCardForTrade(card.CardID, session.UserID, transaction);

                    if (tradeRepository.TradeUIDExists(tradeID, transaction))
                    {
                        throw new UniqueConstraintViolationException($"Cannot Insert new Trade with ID:{tradeID}. This ID already exists.");
                    }

                    tradeRepository.InsertTrade(tradeID, session.UserID, card.CardID, transaction);
                    transaction.Commit();
                }
        }
Exemple #5
0
        public void AcceptTrade(Guid tradeID, Guid tradeOfferID, RequestContext requestContext)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);

                    Trade      trade = tradeRepository.GetTrade(tradeID, transaction);
                    TradeOffer offer = tradeRepository.GetTradeOffer(tradeOfferID, transaction);

                    if (trade == null)
                    {
                        throw new NotFoundException($"Trade with ID:{trade} could not be found.");
                    }

                    if (offer == null)
                    {
                        throw new NotFoundException($"TradeOffer with ID:{tradeOfferID} could not be found.");
                    }

                    if (trade.User_ID != session.UserID)
                    {
                        throw new InvalidAcceptorException($"Cannot Accept Trade with ID: {trade.Trade_ID}. Invalid User.");
                    }

                    ValidateCardForTrade(trade.Card_ID, trade.User_ID, transaction);
                    ValidateCardForTrade(offer.Card_ID, offer.User_ID, transaction);

                    cardLibraryRepository.AddCardToLibrary(trade.Card_ID, offer.User_ID, transaction);

                    CardLibraryItem tradeItem = cardLibraryRepository.GetCardLibraryItem(trade.Card_ID, trade.User_ID, transaction);
                    cardLibraryRepository.RemoveCardFromLibrary(tradeItem, transaction);

                    cardLibraryRepository.AddCardToLibrary(offer.Card_ID, trade.User_ID, transaction);

                    CardLibraryItem offerItem = cardLibraryRepository.GetCardLibraryItem(offer.Card_ID, offer.User_ID, transaction);
                    cardLibraryRepository.RemoveCardFromLibrary(offerItem, transaction);

                    tradeRepository.DeleteTradeOffer(tradeOfferID, transaction);
                    tradeRepository.DeleteTrade(tradeID, transaction);

                    transaction.Commit();
                }
        }
Exemple #6
0
        public void AutoGenerateDeck(RequestContext requestContext, string name, int cardCountPerDeck)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);

                    if (deckRepository.Exists(name, session.UserID, transaction))
                    {
                        throw new UniqueConstraintViolationException($"Cannot create Deck with Name: {name}. A Deck with that Name already exists.");
                    }

                    int createdDeckId = deckRepository.InsertDeck(session.UserID, name, transaction);

                    IEnumerable <CardLibraryDeckAssignment> cardLibrary = cardLibraryRepository
                                                                          .GetCardLibraryItemsByUserID(session.UserID, transaction)
                                                                          .Select(i => new CardLibraryDeckAssignment(i));

                    int cardsInLibrary = cardLibrary.Sum(l => l.CardLibraryEntry.Quantity);

                    if (cardsInLibrary < cardCountPerDeck)
                    {
                        throw new NotEnoughCardsInLibraryException(
                                  $"Cannot Build Deck. Library is to small. Number of Cards in Library: {cardsInLibrary}. " +
                                  $"Number Cards needed for Decks: {cardCountPerDeck}.");
                    }

                    IEnumerable <int> bestPicks = GetBestPicks(cardCountPerDeck, cardLibrary.ToList());

                    Assert.That(
                        bestPicks.Count() == cardCountPerDeck,
                        $"Cards in {nameof(bestPicks)}: {bestPicks.Count()}. " +
                        $"{cardCountPerDeck} are needed to Build a valid Deck."
                        );

                    foreach (int cardID in bestPicks)
                    {
                        deckRepository.AssignCardToDeck(createdDeckId, cardID, transaction);
                    }

                    transaction.Commit();
                }
        }
Exemple #7
0
        public Deck GetDeck(RequestContext requestContext, string name)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
                    User        user    = userRepository.GetUser(session.UserID, transaction);
                    Assert.NotNull(user, nameof(user));

                    Deck deck = deckRepository.GetDeck(user, name, transaction);

                    if (deck == null)
                    {
                        throw new DeckNotFoundException($"Deck: Name: {name} - User: {user.UserName} could not be found.");
                    }

                    return(deck);
                }
        }
Exemple #8
0
        public void CreateTradeOffer(Guid tradeOfferID, Guid tradeID, Card offer, RequestContext requestContext)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
                    Trade       target  = tradeRepository.GetTrade(tradeID, transaction);

                    ValidateCardForTrade(offer.CardID, session.UserID, transaction);

                    if (target.User_ID == session.UserID)
                    {
                        throw new InvalidTradeException($"Session UserID: {session.UserID}, you cannot Trade with yourself.");
                    }

                    if (tradeRepository.TradeOfferUIDExists(tradeOfferID, transaction))
                    {
                        throw new UniqueConstraintViolationException($"Cannot Insert new Offer with ID:{tradeOfferID}. This ID already exists.");
                    }

                    tradeRepository.InsertTradeOffer(tradeOfferID, tradeID, session.UserID, offer.CardID, transaction);
                    transaction.Commit();
                }
        }
        public MatchmakingEntry FindMatch(RequestContext requestContext, string requestedDeck)
        {
            using (NpgsqlConnection connection = database.CreateAndOpenConnection())
                using (NpgsqlTransaction transaction = connection.BeginTransaction())
                {
                    UserSession session = CookieAuthenticationModule.GetUserSessionFromRequest(requestContext, transaction);
                    User        user    = userRepository.GetUser(session.UserID, transaction);
                    Assert.NotNull(user, nameof(user));

                    Validate.Condition(deckRepository.Exists(requestedDeck, user.UserID, transaction), $"Requested Deck: {requestedDeck} could not be found for User: {user.UserName}");

                    MatchmakingEntry self = new MatchmakingEntry(user, requestedDeck);

                    lock (duelQueue)
                    {
                        if (duelQueue.Any(mme => mme.Self.UserID == user.UserID || mme.Adversary?.UserID == user.UserID))
                        {
                            throw new InvalidOperationException(
                                      $"Gegnersuche fehlgeschlagen. Der Benutzer: {user.UserName} " +
                                      "ist dem Matchmaking bereits beigetreten.");
                        }

                        duelQueue.Add(self);
                    }

                    const int maxAttemptCount = 60;

                    int currentAttempt         = 0;
                    MatchmakingEntry adversary = null;

                    do
                    {
                        ++currentAttempt;
                        Thread.Sleep(2000);

                        lock (duelQueue)
                        {
                            if (self.IsMatched)
                            {
                                return(self);
                            }

                            adversary = duelQueue
                                        .FirstOrDefault(mmEntry => mmEntry != self);

                            if (adversary != null)
                            {
                                Guid matchID = Guid.NewGuid();

                                adversary.Adversary = user;
                                adversary.AdversaryRequestedDeck = requestedDeck;
                                adversary.IsMatched = true;
                                adversary.MatchID   = matchID;

                                duelQueue.Remove(adversary);

                                self.Adversary = adversary.Self;
                                self.AdversaryRequestedDeck = adversary.SelfRequestedDeck;
                                self.IsMatched      = true;
                                self.ShouldInitiate = true;
                                self.MatchID        = matchID;
                                duelQueue.Remove(self);

                                return(self);
                            }
                        }
                    } while (currentAttempt <= maxAttemptCount);

                    // no Match could be found -> deregister.
                    lock (duelQueue)
                    {
                        duelQueue.Remove(self);
                    }

                    return(self);
                }
        }