Beispiel #1
0
        public void TestRemovePlayerByID()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int         playerID          = repository.GetPlayerID("player1");
            GamesResult gamesResult;

            if (playerID == 0)
            {
                //Add player
                repository.AddPlayer("player1", "password");
                playerID = repository.GetPlayerID("player1");
            }

            gamesResult = repository.GetGamesResultForPlayer(playerID);
            if (gamesResult.NumberOfGames == 0)
            {
                int wordID = repository.GetWords(1)[0];
                repository.RecordGame(playerID, wordID, 3, 3, true);
                gamesResult = repository.GetGamesResultForPlayer(playerID);
            }
            Assert.IsTrue(gamesResult.NumberOfGames > 0);
            Assert.IsTrue(gamesResult.NumberOfSuccess > 0);

            Assert.IsTrue(playerID > 0);

            repository.RemovePlayerByID(playerID);

            int id2 = repository.GetPlayerID("player1");

            gamesResult = repository.GetGamesResultForPlayer(playerID);
            Assert.IsTrue(id2 == 0);
            Assert.IsTrue(gamesResult.NumberOfGames == 0);
        }
Beispiel #2
0
        public void TestUpdatePlayerByID()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int id1 = repository.GetPlayerID("player1");

            if (id1 == 0)
            {
                //Add player
                repository.AddPlayer("player1", "password");
                id1 = repository.GetPlayerID("player1");
            }
            Assert.IsTrue(id1 > 0);

            repository.UpdatePlayerByID(id1, "player12", "password12");

            int id2 = repository.GetPlayerID("player1");

            Assert.IsTrue(id2 == 0);

            int id3 = repository.GetPlayerID("player12");

            Assert.IsTrue(id3 == id1);

            repository.UpdatePlayerByID(id1, "player1", "password");

            int id4 = repository.GetPlayerID("player1");

            Assert.IsTrue(id4 == id1);
        }
Beispiel #3
0
        public static GamesResult GetGamesResult(string username)
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int         playerID          = repository.GetPlayerID(username);
            GamesResult result            = repository.GetGamesResultForPlayer(playerID);

            return(result);
        }
Beispiel #4
0
        public void TestWordByID()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            string             word       = repository.GetWordByID(13)[0];

            Assert.IsNotNull(word);
            Assert.AreEqual("Seattle", word);
        }
Beispiel #5
0
        public void TestCategoryWords()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();

            int[] words = repository.GetWords(2);

            Assert.IsNotNull(words);
            Assert.AreEqual(11, words.Length);
            Assert.AreEqual(13, words[0]);
            Assert.AreEqual(14, words[1]);
        }
Beispiel #6
0
        public void TestCategoriesRead()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();

            int[] categories = repository.GetCategories();

            Assert.IsNotNull(categories);
            Assert.AreEqual(2, categories.Length);
            Assert.AreEqual("Cities", repository.GetCategoryByID(categories[0]));
            Assert.AreEqual("States", repository.GetCategoryByID(categories[1]));
        }
Beispiel #7
0
        private void AddPlayer(string username, string password)
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int playerID = repository.GetPlayerID(username);

            if (playerID == 0)
            {
                repository.AddPlayer(username, password);
            }
            else
            {
                repository.UpdatePlayerByID(playerID, username, password);
            }
        }
Beispiel #8
0
        public static List <KeyValuePair <int, string> > GetCategories()
        {
            if (categories == null)
            {
                categories = new List <KeyValuePair <int, string> >();
                IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
                int[] categoryIDs             = repository.GetCategories();
                foreach (int id in categoryIDs)
                {
                    categories.Add(new KeyValuePair <int, string>(id, repository.GetCategoryByID(id)));
                }
            }

            return(categories);
        }
Beispiel #9
0
        public void TestAddPlayer()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int id = repository.GetPlayerID("player1");

            if (id != 0)
            {
                repository.RemovePlayerByID(id);
            }
            bool firstTimeAdded  = repository.AddPlayer("player1", "password");
            bool secondTimeAdded = repository.AddPlayer("player1", "password");

            Assert.IsTrue(firstTimeAdded = id > 0 ? true : false);
            Assert.IsFalse(secondTimeAdded);
        }
Beispiel #10
0
        public Game(int categoryID, string username)
        {
            this.categoryID = categoryID;
            repository      = HangmanRepositoryFactory.CreateRepository();
            int[] wordIDs = repository.GetWords(categoryID);
            this.playerID = repository.GetPlayerID(username);

            Random rnd = new Random();
            int    n   = rnd.Next(wordIDs.Length - 1);

            wordID = wordIDs[n];

            string[] wordInfo = repository.GetWordByID(wordID);

            Word        = wordInfo[0];
            Description = wordInfo[1];

            StringBuilder sb = new StringBuilder();

            sb.Append(Word[0]);
            for (int i = 1; i < Word.Length - 1; i++)
            {
                sb.Append("_");
            }
            sb.Append(Word[Word.Length - 1]);

            GuessedWord = sb.ToString();

            sb.Clear(); sb.Append(GuessedWord[0]);

            for (int i = 1; i < GuessedWord.Length; i++)
            {
                sb.Append(" " + GuessedWord[i]);
            }

            DisplayWord = sb.ToString();

            WrongChars = "";

            WrongCharsCount = 0;
            Status          = "";
        }
Beispiel #11
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
                int playerID = repository.GetPlayerID(model.UserName);
                if (playerID != 0)
                {
                    if (repository.VerifyUser(playerID, model.UserName, model.Password))
                    {
                        var user = await UserManager.FindAsync(model.UserName, model.Password);

                        if (user != null)
                        {
                            await SignInAsync(user, model.RememberMe);

                            return(RedirectToLocal(returnUrl));
                        }
                        else
                        {
                            ModelState.AddModelError("", "Invalid username or password.");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid username or password.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "No user exists with provided username.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #12
0
        public void TestVerifyUser()
        {
            IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
            int id1 = repository.GetPlayerID("player1");

            if (id1 == 0)
            {
                //Add player
                repository.AddPlayer("player1", "password");
                id1 = repository.GetPlayerID("player1");
            }
            Assert.IsTrue(id1 > 0);
            bool userVerified = repository.VerifyUser(id1, "player1", "password");

            Assert.IsTrue(userVerified);

            bool userVerificationFailedForName = !repository.VerifyUser(id1, "player", "password");

            Assert.IsTrue(userVerificationFailedForName);

            bool userVerificationFailedForPassword = !repository.VerifyUser(id1, "player1", "password2");

            Assert.IsTrue(userVerificationFailedForPassword);
        }
Beispiel #13
0
 public void TestGetPlayerID()
 {
     IHangmanRepository repository = HangmanRepositoryFactory.CreateRepository();
     int id = repository.GetPlayerID("player1");
 }