Пример #1
0
        public void DeletePlayerTest()
        {
            bool testDelete = false;

            // Test Parameters
            string deleteName = "Tinkle",
                   testName = "Frank",
                   testName2 = "June";
            List<int> tinkleScores = new List<int>() { 12, 56, 65, 90 };
            List<int> testScores = new List<int>() { 1, 2, 3 };
            List<int> testScores2 = new List<int>() { 5, 6, 7, 8 };

            Player deletePlayer = new Player(deleteName, false, 4, tinkleScores);
            Player testPlayer1 = new Player(testName, false, 3, testScores);
            Player testPlayer2 = new Player(testName2, false, 4, testScores2);

            Factory testFactory = Factory.GetInstance();

            IPlayerSvc testSVC = (IPlayerSvc)
                testFactory.GetService(typeof(IPlayerSvc).Name);

            // Add players to DB for
            testSVC.SavePlayerToDb(testPlayer1);
            testSVC.SavePlayerToDb(deletePlayer);
            testSVC.SavePlayerToDb(testPlayer2);

            // Delete testPlayer and test return value
            testDelete = testSVC.DeletePlayer(deleteName);

            Assert.IsTrue(testDelete);
        }
Пример #2
0
        public int GetNumberOfPlayerScores(Player player)
        {
            IPlayerSvc playSvc =
                (IPlayerSvc)GetService(typeof(IPlayerSvc).Name);

            return playSvc.GetNumberOfScores(player);
        }
Пример #3
0
        public int GetAPlayerScore(Player player, int scoreIdx)
        {
            IPlayerSvc playSvc =
                (IPlayerSvc)GetService(typeof(IPlayerSvc).Name);

            return playSvc.GetIndexScore(player, scoreIdx);
        }
Пример #4
0
        public void GMCheckWinTest()
        {
            int testNumDigits = 4,
                testNumCorrect1 = 4,
                testNumCorrect2 = 3;
            long testCurrNumJum = 1234;
            long testCurrGuess = 1111;
            bool testStatus = false;

            JumbledNumber testNumJum = new JumbledNumber(testNumDigits, testCurrNumJum, testCurrGuess, 0, 0, true);
            Player testPlayer = new Player("Link");

            GameManager testGameManager = new GameManager();

            // Set win condition and test
            testNumJum.NumCorrect = testNumCorrect1;

            testStatus = testGameManager.CheckWin(testNumJum, testPlayer);
            Assert.IsTrue(testStatus);

            // Set win condition and test
            testNumJum.NumCorrect = testNumCorrect2;

            testStatus = testGameManager.CheckWin(testNumJum, testPlayer);
            Assert.IsFalse(testStatus);
        }
Пример #5
0
        public void DecScoreTest()
        {
            List<int> scores = new List<int>() { 14, 3, 6, 0, 0 };            // 1st Test score ArrayList

            Player target = new Player("Link", false, 3, scores);
            target.DecScore();

            Assert.AreEqual(target.NumberOfScores, 2);
        }
Пример #6
0
        public void IncScoreTest()
        {
            List<int> scores = new List<int>() { 14, 3, 6, 0, 0 };  // 1st Test score ArrayList

            Player target = new Player("Link", false, 3, scores);
            target.IncScore();

            // Test that the number of scores was incremented
            Assert.AreEqual(target.NumberOfScores, 4);
        }
Пример #7
0
        public void DelScoreTest()
        {
            List<int> scores = new List<int>() { 14, 3, 6, 0, 0 };  // 1st Test score ArrayList
            Player target = new Player("Link", false, 3, scores);   // 1st Test Player
            target.DelScore();

            //Test that score decremented
            Assert.AreEqual(target.NumberOfScores, 2);

            //Test that 3rd score was set to 0
            Assert.AreEqual(target.ScoreList[2], 0);
        }
Пример #8
0
        public void PMCreatePlayerTest()
        {
            // Create file and data for testing
            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4;
            Player testPlayer = new Player();
            PlayerManager testManager = new PlayerManager();
            List<int> testScores = new List<int>() { 60, 32, 15, 4};    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);

            Factory testFactory = Factory.GetInstance();
            IPlayerSvc testSVC = (IPlayerSvc)
                testFactory.GetService(typeof(IPlayerSvc).Name);

            // Create file for test
            try
            {
                testSVC.SavePlayerToDb(newPlayer);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Unable to create test file");
            }

            // Create new player using business manager
            try
            {
                testPlayer = testManager.LoadPlayer(testName);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Unable to create player!");
            }

            //Test if player data has been loaded
            Assert.AreEqual(testPlayer.PlayerName, testName);
            Assert.AreEqual(testPlayer.GameStatus, testStatus);
            Assert.AreEqual(testPlayer.NumberOfScores, testNumScores);

            //Test arbitrary list value
            // Sort test array first
            testScores.Sort();
            Assert.AreEqual(testPlayer.ScoreList[1], testScores[1]);
        }
Пример #9
0
 public bool CheckWin(JumbledNumber NumJum, Player player)
 {
     if (NumJum.NumCorrect == NumJum.NumDigits)
     {
         // Set player win value to true
         player.GameStatus = true;
         return true;
     }
     else
     {
         // Set player win to false
         player.GameStatus = false;
         return false;
     }
 }
Пример #10
0
        public void GMStartGameTest()
        {
            string testName = "Link";
            int testDiff = 1,
                testNumdigts = 0;

            Player testPlayer = new Player(testName);

            JumbledNumber testNumJum = new JumbledNumber();

            GameManager testGameManager = new GameManager();

            testNumdigts = testGameManager.StartGame(testNumJum, testDiff);

            Console.WriteLine("Generated Number is: " + testNumJum.JNumberValue.ToString());
            Console.WriteLine("Player Name is: " + testPlayer.PlayerName);
            Console.WriteLine("Game status is: " + testNumJum.GameInProgress.ToString());

            Assert.IsTrue((testNumdigts > 0));
            Assert.IsTrue(testNumJum.GameInProgress);
        }
Пример #11
0
        public void AddPlayerScoreTest()
        {
            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4;
            List<int> testScores = new List<int>() { 60, 32, 15, 4 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);
            Factory testFactory = Factory.GetInstance();

            IPlayerSvc testSVC = (IPlayerSvc)
               testFactory.GetService(typeof(IPlayerSvc).Name);

            // Add new score
            // Should increment count to 5
            testSVC.AddPlayerScore(newPlayer, 2);
            Assert.IsTrue((newPlayer.NumberOfScores == 5));

            // Test if first score is now 2
            Assert.AreEqual(newPlayer.ScoreList[0], 2);
        }
Пример #12
0
        public void PMCreateNewPlayerTest()
        {
            string fileName = "Tinker";                                     // File name for test that does not exist
            Player testPlayer = new Player();
            PlayerManager testManager = new PlayerManager();

            // Create player object from file
            try
            {
                testPlayer = testManager.LoadPlayer(fileName);
            }
            catch (PlayerNotFoundException)
            {
                Console.WriteLine("Caught Exception in Test");
            }
            finally
            {
                //Test if player is created based on input filename
                //If the exception is caught and handled,  it should
                //Create a new player with the specified name
                Assert.AreEqual(testPlayer.PlayerName, fileName);
            }
        }
Пример #13
0
        public void AddScoreTest()
        {
            Player target = new Player("Link");
            int score1 = 10,
                score2 = 25,
                score3 = 6,
                score4 = 14;
            target.AddScore(score1);
            target.AddScore(score2);
            target.AddScore(score3);
            target.AddScore(score4);

            //Test if expected score was added
            // Method adds, and then sorts value so should be at top
            for (int i = 0; i < target.NumberOfScores; i++)
            {
                Console.Write(" " + target.ScoreList[i] + " ");
            }
            Console.WriteLine("");
            Assert.AreEqual(target.ScoreList[0], score3);
            //Test if number of scores has been incremented
            Assert.AreEqual(target.NumberOfScores, 4);
        }
Пример #14
0
        public bool SavePlayer(Player player, string storeName)
        {
            bool SaveGood = false;

            IPlayerSvc playSvc =
                (IPlayerSvc)GetService(typeof(IPlayerSvc).Name);

            // Check for file exceptions
            try
            {
                //playSvc.SavePlayerState(player, storeName);
                SaveGood = playSvc.SavePlayerToDb(player);

            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                // Catch file error and throw up to presentation layer
                SaveGood = false;
                throw e;
            }

            return SaveGood;
        }
Пример #15
0
 public int GetIndexScore(Player player, int scoreIdx)
 {
     return (int)player.ScoreList[scoreIdx];
 }
Пример #16
0
 public void SetGameState(Player player, bool gamestate)
 {
     player.GameStatus = gamestate;
 }
Пример #17
0
 public void AddPlayerScore(Player player, int score)
 {
     // Update player object with score
     player.AddScore(score);
 }
Пример #18
0
        public bool SavePlayerToDb(Player player)
        {
            bool SaveGood = false,
                NewPlayer = true;

            PlayerDb AddPlayer = null;
            int HoldPlayerDbID = 0;

            try
            {
                // If a player exisits in db, find and attach to a context to update

                // Get a context to the DB and save the passed object
                using (var db = new PlayerDbContext())
                {
                    foreach (PlayerDb findPlayer in db.PlayersDb)
                    {
                        if (findPlayer.PlayerName == player.PlayerName)
                        {
                            NewPlayer = false;
                            // Copy values to object
                            HoldPlayerDbID = findPlayer.PlayerDbID;
                            findPlayer.PlayerName = player.PlayerName;
                            findPlayer.GameStatus = player.GameStatus;
                            findPlayer.NumScores = player.NumberOfScores;
                        }
                    }

                    // If new player, add to DB
                    if (NewPlayer)
                    {
                        AddPlayer = new PlayerDb();
                        AddPlayer.PlayerName = player.PlayerName;
                        AddPlayer.GameStatus = player.GameStatus;
                        AddPlayer.NumScores = player.NumberOfScores;
                        db.PlayersDb.Add(AddPlayer);
                        db.SaveChanges();
                        HoldPlayerDbID = AddPlayer.PlayerDbID;
                    }
                    // Close DB
                    db.Dispose();
                }

                // If there are scores, update PlayerScores object
                // Get a context to the DB and save the passed object
                using (var db = new PlayerDbContext())
                {
                    if (player.NumberOfScores > 0)
                    {

                        // Remove Scores first to compensate for score
                        // auto-sorting in domain object
                        // otherwise database would have to be sorted and a
                        // comparison done to insert new scores
                        // this is simpiler :-)
                        foreach (PlayerScore checkScore in db.PlayerScores)
                        {
                            if (checkScore.PlayerDbID == HoldPlayerDbID)
                                db.PlayerScores.Remove(checkScore);
                        }

                        PlayerScore SaveScores = new PlayerScore();
                        for (int i = 0; i < player.NumberOfScores; i++)
                        {
                            SaveScores.PlayerDbID = HoldPlayerDbID;
                            SaveScores.Score = player.ScoreList[i];
                            db.PlayerScores.Add(SaveScores);

                            // Start next entry
                            SaveScores = new PlayerScore();
                        }
                    }

                    db.SaveChanges();
                    db.Dispose();
                    // No errors, set return value
                    SaveGood = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("DB store exception" + e.Message);
                SaveGood = false;
            }
            return SaveGood;
        }
Пример #19
0
        public void PMGetNumberOfPlayerScoresTest()
        {
            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4,
                testNumScoreRet = 0;
            List<int> testScores = new List<int>() { 45, 10, 8, 4, 0 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);

            PlayerManager testPlayMnger = new PlayerManager();

            testNumScoreRet = testPlayMnger.GetNumberOfPlayerScores(newPlayer);

            Assert.AreEqual(testNumScores, testNumScoreRet);
        }
Пример #20
0
        public void UpdatePlayerScore(Player player, int score)
        {
            IPlayerSvc playSvc =
                (IPlayerSvc)GetService(typeof(IPlayerSvc).Name);

            playSvc.AddPlayerScore(player, score);
        }
Пример #21
0
        public void UpdateGameState(Player player, bool gamestate)
        {
            IPlayerSvc playSvc =
                (IPlayerSvc)GetService(typeof(IPlayerSvc).Name);

            playSvc.SetGameState(player, gamestate);
        }
Пример #22
0
        public void GetNumberOfScoresTest()
        {
            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4,
                testNumScoreRet = 0;
            List<int> testScores = new List<int>() { 60, 32, 15, 4, 0 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);
            Factory testFactory = Factory.GetInstance();

            IPlayerSvc testPlaySvc = (IPlayerSvc)
                testFactory.GetService(typeof(IPlayerSvc).Name);

            testNumScoreRet = testPlaySvc.GetNumberOfScores(newPlayer);

            Assert.AreEqual(testNumScores, testNumScoreRet);
        }
Пример #23
0
        public void PMSavePlayerTest()
        {
            // Create player for testing
            // Test parameters
            string testName = "Link";
            bool testStatus = false,
                SaveGood = false;
            int testNumScores = 2;
            PlayerManager testManager;
            List<int> testScores = new List<int>() { 99, 23 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);

            // Try to save Player using Manager
            testManager = new PlayerManager();
            try
            {
                SaveGood = testManager.SavePlayer(newPlayer, testName);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Unable to create player object");
            }

            //Test if player was saved to database
            //Create compare player using database
            Player comparePlayer = null;
            comparePlayer = testManager.LoadPlayer(testName);

            Assert.AreEqual(testName, comparePlayer.PlayerName);
            Assert.IsTrue(SaveGood);
        }
Пример #24
0
        public void GetIndexScoreTest()
        {
            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4,
                testScoreRet = 0;
            List<int> testScores = new List<int>() { 60, 32, 15, 4, 0 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);
            Factory testFactory = Factory.GetInstance();

            IPlayerSvc testPlaySvc = (IPlayerSvc)
                testFactory.GetService(typeof(IPlayerSvc).Name);

            // Use a For loop to check each value
            for (int i = 0; i < testNumScores; i++)
            {
                testScoreRet = testPlaySvc.GetIndexScore(newPlayer, i);
                Assert.AreEqual(testScores[i], testScoreRet);
            }
        }
Пример #25
0
        public void LoadPlayerExceptionTest()
        {
            string fileName = "Tinker";                                     // File name for test that does not exist
            Player testPlayer = new Player();
            Factory testFactory = Factory.GetInstance();                    // Factory instance using Singleton pattern
            IPlayerSvc testSVC = (IPlayerSvc)                               // Test service using a marker interface
                testFactory.GetService(typeof(IPlayerSvc).Name);

            // Create player object from file
            try
            {
                testPlayer = testSVC.GetPlayerFromDb(fileName);
            }
            catch (PlayerNotFoundException)
            {
                testPlayer = new Player("Caught");
            }
            finally
            {
                //Test if player is created based on input filename
                //If the exception is caught and handled,  it should
                //have the name 'Caught'
                Assert.AreEqual(testPlayer.PlayerName, "Caught");
            }
        }
Пример #26
0
        public void LoadPlayerTest()
        {
            // Create file and data for testing
            // Test parameters
            string testName = "Ash";
            bool testStatus = true;
            int testNumScores = 4;
            Player testPlayer = null;
            List<int> testScores = new List<int>() { 60, 32, 15, 4};    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);

            Factory testFactory = Factory.GetInstance();                    // Factory instance using Singleton pattern
            IPlayerSvc testSVC = (IPlayerSvc)                               // Test Service using in interface marker
                testFactory.GetService(typeof(IPlayerSvc).Name);

            // Create player in DB for test
            try
            {
                testSVC.SavePlayerToDb(newPlayer);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Save Player Failed");
            }

            // Try to Create player object from DB
            try
            {
                testPlayer = testSVC.GetPlayerFromDb(testName);
            }
            catch (PlayerNotFoundException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Unable to load player data from database");
            }

            //Test if player data has been loaded
            Assert.AreEqual(testPlayer.PlayerName, testName);
            Assert.AreEqual(testPlayer.GameStatus, testStatus);
            Assert.AreEqual(testPlayer.NumberOfScores, testNumScores);

            //Test arbitrary list value
            testScores.Sort();
            Assert.AreEqual(testPlayer.ScoreList[1], testScores[1]);
        }
Пример #27
0
 public int GetNumberOfScores(Player player)
 {
     return player.NumberOfScores;
 }
Пример #28
0
        public void SavePlayerTest()
        {
            string testName = "Link";                                       // Name for test
            List<int> scores = new List<int>() { 14, 3, 6};                 // Test score ArrayList
            Player target = new Player(testName, false, 3, scores);
            bool SaveGood = false;

            Factory testFactory = Factory.GetInstance();                    // Use Singleton pattern for Factory
            IPlayerSvc testSVC = (IPlayerSvc)
                testFactory.GetService(typeof(IPlayerSvc).Name);            // Test SVC via an interface

            // Try to save Player using service
            try
            {
                SaveGood = testSVC.SavePlayerToDb(target);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                Assert.Fail("Unable to create player object");
            }

            //Test if player was saved to database
            //Create compare player using database
            Player comparePlayer = testSVC.GetPlayerFromDb(testName);

            Assert.AreEqual(testName, comparePlayer.PlayerName);
            Assert.IsTrue(SaveGood);
        }
Пример #29
0
        public Player GetPlayerFromDb(string PlayerName)
        {
            Player getPlayer = null;

            // Holding Entity Object
            PlayerDb holdPlayer = new PlayerDb();

            // Get data from database
            // Get a context to the db
            using (var db = new PlayerDbContext())
            {

                foreach (PlayerDb findPlayer in db.PlayersDb)
                {
                    if (findPlayer.PlayerName == PlayerName)
                    {
                        getPlayer = new Player();
                        getPlayer.PlayerName = findPlayer.PlayerName;
                        getPlayer.GameStatus = findPlayer.GameStatus;
                        // Set number of scores to 0, and let value be built when loading
                        getPlayer.NumberOfScores = 0;

                        // If there are scores, need to load up player
                        // Get a context to the DB
                        using (var db1 = new PlayerDbContext())
                        {
                            if (findPlayer.NumScores > 0)
                            {
                                foreach (PlayerScore findScore in db.PlayerScores)
                                {
                                    if (findScore.PlayerDbID == findPlayer.PlayerDbID)
                                    {
                                        getPlayer.AddScore(findScore.Score);
                                    }
                                }
                            }
                            // Close DB
                            db1.Dispose();
                        }
                    }
                }
                // Close DB
                db.Dispose();
            }

            // Check to see if player was loaded, and if not throw exception
            if (getPlayer == null)
            {
                throw new PlayerNotFoundException("Player not found in DB");
            }
            return getPlayer;
        }
Пример #30
0
        public void SetGameStateTest()
        {
            bool testState1 = true,
                testState2 = false;

            // Test parameters
            string testName = "Zelda";
            bool testStatus = true;
            int testNumScores = 4;
            List<int> testScores = new List<int>() { 60, 32, 15, 4, 0 };    // Test score ArrayList
            Player newPlayer = new Player(testName, testStatus,
                                        testNumScores, testScores);
            Factory testFactory = Factory.GetInstance();

            IPlayerSvc testSVC = (IPlayerSvc)
               testFactory.GetService(typeof(IPlayerSvc).Name);

            // Set value  usign service and test
            testSVC.SetGameState(newPlayer, testState1);
            Assert.IsTrue(newPlayer.GameStatus);

            // Set value usign serivce and test
            testSVC.SetGameState(newPlayer, testState2);
            Assert.IsFalse(newPlayer.GameStatus);
        }