Exemple #1
0
        public static List <CardPack> GetCardPacks()
        {
            log.Info("ShopAdministration - GetCardPacks()");
            List <CardPack> allCardPacks = null;

            try
            {
                using (var context = new clonestoneEntities())
                {
                    allCardPacks = context.AllCardPacks.OrderBy(x => x.NumberOfCards).ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("ShopAdministration - GetCardPacks() - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("ShopAdministration - GetCardPacks() - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(allCardPacks);
        }
        public static Card GetCard(int id)
        {
            log.Info("CardAdministration - GetCard(id)");
            Card card = null;

            if (id < 1)
            {
                throw new ArgumentException("invalid card id", nameof(id));
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    card = context.AllCards.FirstOrDefault(x => x.ID == id);
                }
            }
            catch (Exception ex)
            {
                log.Error("CardAdministration - GetCard(id) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("CardAdministration - GetCard(id) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(card);
        }
Exemple #3
0
        /// <summary>
        /// Returns login status for given credentials
        /// </summary>
        /// <param name="username">username to login</param>
        /// <param name="password">password to login</param>
        /// <exception cref="ArgumentException">in case any parameter is null or empty</exception>
        /// <exception cref="Exception">in case of a database error</exception>
        /// <returns>login status for given credentials</returns>
        public static LoginResult Login(string username, string password)
        {
            log.Info("UserAdministration - Login");

            LoginResult result = LoginResult.NotSet;

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException($"{nameof(username)} is null or empty");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException($"{nameof(password)} is null or empty");
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    User user = context.AllUsers.FirstOrDefault(x => x.Username.Equals(username));

                    if (user != null)
                    {
                        byte[] hashedPassword = Tools.GetHash(password + user.UserSalt);
                        if (user.Password.SequenceEqual(hashedPassword))
                        {
                            result = LoginResult.Successful;
                            log.Debug($"User {username} logged in successfully");
                        }
                        else
                        {
                            result = LoginResult.InvalidPassword;
                            log.Warn($"User {username} entered invalid password");
                        }
                    }
                    else
                    {
                        result = LoginResult.InvalidUsername;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("UserAdministration - Login - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("UserAdministration - Login - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// returns a user object for a given username
        /// </summary>
        /// <param name="username">a non-empty, existing username</param>
        /// <returns>user object for given username</returns>
        /// <exception cref="Exception">in case of a common db error</exception>
        /// <exception cref="ArgumentNullException">username is null or empty</exception>
        /// <exception cref="ArgumentException">invalid username</exception>
        public static User GetUser(string username)
        {
            log.Info("UserAdministration - GetUser(username)");
            User currentUser = null;

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            try
            {
                /// open connection and keep it open for as long as needed
                using (var context = new clonestoneEntities())
                {
                    currentUser = context.AllUsers

                                  /// therefore we should/could/may (as userstory indicates!)
                                  /// eager load any navigation properties
                                  .Include(x => x.UserRole)


                                  .FirstOrDefault(x => x.Username.Equals(username));
                    if (currentUser == null)
                    {
                        throw new ArgumentException($"Invalid username {username}");
                    }
                } /// connection will be close

                /// so at this point LAZY LOADING is NOT available anymore!!
            }
            catch (Exception ex)
            {
                log.Error("UserAdministration - GetUser(username) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("UserAdministration - GetUser(username) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(currentUser);
        }
        /// <summary>
        /// Returns a list of deckCards assigned to a given idDeck
        /// </summary>
        /// <param name="idDeck"></param>
        /// <returns></returns>
        public static List <DeckCard> GetCardsInDeck(int idDeck)
        {
            log.Info("DeckAdministration - GetCardsInDeck(idDeck)");
            List <DeckCard> deckCards = null;

            if (idDeck < 1)
            {
                throw new ArgumentException("Invalid idDeck");
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    Deck deck = context.AllDecks
                                .Include(x => x.AllDeckCards)                     /// eager loading of deckCards
                                .Include(x => x.AllDeckCards.Select(y => y.Card)) /// eager loading of cards
                                .FirstOrDefault(x => x.ID == idDeck);

                    if (deck == null)
                    {
                        throw new ArgumentException("Invalid idDeck");
                    }

                    deckCards = deck.AllDeckCards.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeckAdministration -GetDeckCardsForDeck(idDeck) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("DeckAdministration - GetDeckCardsForDeck(idDeck) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(deckCards);
        }
        public static List <Deck> GetUserDecks(string username)
        {
            log.Info("DeckAdministration - GetUserDecks(username)");
            List <Deck> userDecks = null;

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    User user = context.AllUsers
                                .Include(x => x.AllDecks)
                                .Include(x => x.AllDecks.Select(y => y.AllDeckCards))
                                .FirstOrDefault(x => x.Username.Equals(username));

                    if (user == null)
                    {
                        throw new ArgumentException("Invalid username", nameof(username));
                    }

                    userDecks = user.AllDecks.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeckAdministration - GetUserDecks(username) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("DeckAdministration - GetUserDecks(username) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(userDecks);
        }
Exemple #7
0
        /// <summary>
        /// Returns a user role for a given username
        /// </summary>
        /// <param name="username">a non-empty username</param>
        /// <returns>user role of given username</returns>
        /// <exception cref="Exception">in case of a database error</exception>
        /// <exception cref="ArgumentNullException">if username is null or empty</exception>
        /// <exception cref="ArgumentException">if username is unknown</exception>
        public static UserRole GetUserRole(string username)
        {
            log.Info("RolesAdministration - GetUserRoles(username)");

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            UserRole userRole = null;

            try
            {
                using (var context = new clonestoneEntities())
                {
                    User currentUser = context.AllUsers.FirstOrDefault(x => x.Username.Equals(username));
                    if (currentUser != null)
                    {
                        userRole = currentUser.UserRole;
                    }
                    else
                    {
                        throw new ArgumentException($"Invalid username {username}");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("RolesAdministration - GetUserRoles(username) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("RolesAdministration - GetUserRoles(username) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }
            return(userRole);
        }
        public static bool AddCardToDeck(int idDeck, int idCard)
        {
            log.Info("DeckAdministration - AddCardToDeck(idDeck, idCard)");
            bool result = false;

            if (idDeck < 1)
            {
                throw new ArgumentException("Invalid idDeck");
            }
            if (idCard < 1)
            {
                throw new ArgumentException("Invalid idCard");
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    Deck deck = context.AllDecks.FirstOrDefault(x => x.ID == idDeck);

                    if (deck == null)
                    {
                        throw new ArgumentException("Invalid idDeck");
                    }

                    Card card = context.AllCards.FirstOrDefault(x => x.ID == idCard);

                    if (card == null)
                    {
                        throw new ArgumentException("Invalid idCard");
                    }

                    /// check if card is already present in deck
                    DeckCard deckCard = deck.AllDeckCards.FirstOrDefault(x => x.ID_Card == idCard);

                    /// if so
                    if (deckCard != null)
                    {
                        /// increase number
                        deckCard.NumberOfCards++;
                    }
                    else
                    {
                        /// else, add new entry
                        deckCard = new DeckCard()
                        {
                            Card = card,
                            Deck = deck
                        };
                        deck.AllDeckCards.Add(deckCard);
                    }

                    context.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                log.Error("DeckAdministration - AddCardToDeck(idDeck, idCard) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("DeckAdministration - AddCardToDeck(idDeck, idCard) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }


            return(result);
        }
        /// <summary>
        /// Returns a list of UserCards available for a given idDeck
        /// </summary>
        /// <param name="idDeck"></param>
        /// <returns></returns>
        /// <exception cref="Exception">in case db access fails</exception>
        public static List <UserCard> GetCardsForDeck(string username, int idDeck)
        {
            log.Info("DeckAdministration - GetDeckCards(username, idDeck)");
            List <UserCard> userCards = null;

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }
            if (idDeck < 1)
            {
                throw new ArgumentException("Invalid idDeck");
            }


            try
            {
                using (var context = new clonestoneEntities())
                {
                    User user = context.AllUsers
                                .Include(x => x.AllUserCards)
                                .Include(x => x.AllUserCards.Select(y => y.Card))
                                .FirstOrDefault(x => x.Username.Equals(username));

                    if (user == null)
                    {
                        throw new ArgumentException("Invalid username");
                    }

                    /// get ALL cards assigned to this user
                    userCards = user.AllUserCards.ToList();

                    /// get deck for given idDeck
                    Deck deck = context.AllDecks.FirstOrDefault(x => x.ID == idDeck);

                    if (deck == null)
                    {
                        throw new ArgumentException("Invalid idDeck");
                    }

                    /// iterate over all userCards
                    foreach (var userCard in userCards)
                    {
                        /// check if current userCard is already present in current deck
                        DeckCard deckCard = deck.AllDeckCards.FirstOrDefault(x => x.ID_Card == userCard.ID_Card);

                        /// if card is already in deck
                        if (deckCard != null)
                        {
                            /// decrease number of cards available
                            userCard.NumberOfCards -= deckCard.NumberOfCards;
                        }
                    }

                    /// return only those cards, whose number is greater than 0
                    userCards = userCards.Where(x => x.NumberOfCards > 0).ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeckAdministration - GetDeckCards(username, idDeck) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("DeckAdministration - GetDeckCards(username, idDeck) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(userCards);
        }
Exemple #10
0
        public static bool RemoveCardFromDeck(int idDeck, int idCard)
        {
            log.Info("DeckAdministration - RemoveCardFromDeck(idDeck, idCard)");
            bool result = false;

            if (idDeck < 1)
            {
                throw new ArgumentException("Invalid idDeck");
            }
            if (idCard < 1)
            {
                throw new ArgumentException("Invalid idCard");
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    Deck deck = context.AllDecks.FirstOrDefault(x => x.ID == idDeck);

                    if (deck == null)
                    {
                        throw new ArgumentException("Invalid idDeck");
                    }

                    Card card = context.AllCards.FirstOrDefault(x => x.ID == idCard);

                    if (card == null)
                    {
                        throw new ArgumentException("Invalid idCard");
                    }

                    /// get existing deckCard
                    DeckCard deckCard = deck.AllDeckCards.FirstOrDefault(x => x.ID_Card == idCard);

                    /// if so
                    if (deckCard != null)
                    {
                        /// decrease number
                        deckCard.NumberOfCards--;

                        /// remove entry if necessary
                        if (deckCard.NumberOfCards == 0)
                        {
                            deck.AllDeckCards.Remove(deckCard);
                        }
                    }

                    context.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                log.Error("DeckAdministration - RemoveCardFromDeck(idDeck, idCard) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("DeckAdministration - RemoveCardFromDeck(idDeck, idCard) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(result);
        }
Exemple #11
0
        /// <summary>
        /// Registers a new user in data storage
        /// adds three static user decks with randomized deck names
        /// returns true if successful, otherelse false
        /// throws exception on invalid data
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="gamerTag"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="avatar"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">invalid data within parameters</exception>
        /// <exception cref="Exception">in case of a database error</exception>
        public static RegisterResult Register(string firstName, string lastName, string gamerTag, string username, string password)
        {
            log.Info("UserAdministration - Register");

            RegisterResult result = RegisterResult.NotSet;

            if (string.IsNullOrEmpty(firstName))
            {
                throw new ArgumentException($"{nameof(firstName)} is null or empty");
            }
            if (string.IsNullOrEmpty(lastName))
            {
                throw new ArgumentException($"{nameof(lastName)} is null or empty");
            }
            if (string.IsNullOrEmpty(gamerTag))
            {
                throw new ArgumentException($"{nameof(gamerTag)} is null or empty");
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException($"{nameof(username)} is null or empty");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException($"{nameof(password)} is null or empty");
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    User user = context.AllUsers.FirstOrDefault(x => x.Username.Equals(username));

                    if (user != null)
                    {
                        result = RegisterResult.UsernameExists;
                        log.Debug($"Username {username} exists");
                    }
                    else
                    {
                        string userSalt = Tools.GenerateSalt(128);
                        user = new User()
                        {
                            FirstName   = firstName,
                            LastName    = lastName,
                            GamerTag    = gamerTag,
                            Username    = username, // = email
                            Password    = Tools.GetHash(password + userSalt),
                            UserSalt    = userSalt,
                            ID_UserRole = UserRole.PLAYER
                        };

                        for (int i = 0; i < User.MAX_DECK; i++)
                        {
                            Deck userDeck = new Deck()
                            {
                                Name = $"{gamerTag}'s {Deck.GetRandomizedDeckName}",
                                User = user
                            };
                            context.AllDecks.Add(userDeck);
                            //user.AllDecks.Add(userDeck);
                        }

                        context.AllUsers.Add(user);
                        context.SaveChanges();
                        result = RegisterResult.Successful;
                        log.Debug($"Username {username} registered!");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("UserAdministration - Register - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("UserAdministration - Register - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(result);
        }
Exemple #12
0
        /// <summary>
        /// creates a pack with a number of cards
        /// cards will be chosen at random
        /// random cards may be duplicates
        /// assign cards to user
        /// decrease money of user by price of pack
        /// add new virtual purchase entry
        /// </summary>
        /// <param name="idCardPack"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public static BuyResult BuyPack(int idCardPack, string username)
        {
            log.Info("ShopAdministration - BuyPack(idCardPack, username)");
            BuyResult result = BuyResult.Success;

            if (idCardPack <= 0)
            {
                throw new ArgumentException("Invalid Value", nameof(idCardPack));
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException(nameof(username));
            }

            try
            {
                using (var context = new clonestoneEntities())
                {
                    User user = context.AllUsers.FirstOrDefault(x => x.Username == username);
                    if (user == null)
                    {
                        throw new ArgumentException("Invalid value", nameof(username));
                    }

                    CardPack cardPack = context.AllCardPacks.FirstOrDefault(x => x.ID == idCardPack);
                    if (cardPack == null)
                    {
                        throw new ArgumentException("Invalid value", nameof(idCardPack));
                    }

                    if (user.AmountMoney < cardPack.Price)
                    {
                        log.Debug($"User {username} has not enough money for Pack {idCardPack}");
                        result = BuyResult.NotEnoughMoney;
                    }
                    else
                    {
                        log.Debug($"Reduce money of user by {cardPack.Price}");
                        user.AmountMoney -= cardPack.Price;

                        VirtualPurchase purchase = new VirtualPurchase()
                        {
                            ID_CardPack   = idCardPack,
                            ID_User       = user.ID,
                            NumberOfPacks = 1,
                            PurchaseDate  = DateTime.Now
                        };
                        context.AllVirtualPurchases.Add(purchase);
                        log.Debug($"Added new VirtualPurchas for user {username}");

                        int count = context.AllCards.Count();

                        log.Debug($"Start creating random cards for pack {idCardPack}");
                        /// create cards at random
                        for (int numberOfCard = 0; numberOfCard < cardPack.NumberOfCards; numberOfCard++)
                        {
                            /// get a valid idCard (generated by random)
                            Card randomCard = context.AllCards.OrderBy(x => x.ID).Skip(RandomNumberGenerator.Next(0, count)).Take(1).Single();
                            log.Debug($"\tRandomCard {randomCard.Name} (ID: {randomCard.ID})");

                            /// save new card to userCards

                            /// if card is already an userCard
                            /// increase number
                            UserCard userCard = user.AllUserCards.Where(x => x.ID_Card == randomCard.ID).FirstOrDefault();
                            if (userCard != null)
                            {
                                log.Debug($"\t\tUserCard already exists - increase numberOfCards");
                                userCard.NumberOfCards++;
                            }
                            else       /// else - add new userCard
                            {
                                log.Debug($"\t\tAdd NEW UserCard");
                                userCard = new UserCard()
                                {
                                    ID_User       = user.ID,
                                    ID_Card       = randomCard.ID,
                                    NumberOfCards = 1
                                };
                                context.AllUserCards.Add(userCard);
                            }
                        }
                        context.SaveChanges();
                        log.Debug($"Finished adding random cards!");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("ShopAdministration - BuyPack(idCardPack, username) - Exception", ex);
                if (ex.InnerException != null)
                {
                    log.Error("ShopAdministration - BuyPack(idCardPack, username) - Exception (inner)", ex.InnerException);
                }

                Debugger.Break();
                throw ex;
            }

            return(result);
        }