public void CreateGameTest()
        {
            //Arrange
            GameController        gameController = new GameController();
            IGameDBIF             gameDB         = new GameDB();
            IGameTableDBIF        gameTableDB    = new GameTableDB();
            List <GameTableModel> gameTables     = (List <GameTableModel>)gameTableDB.GetAll();
            GameTableModel        gameTable      = null;
            bool found = false;
            Game game  = new Game();

            //Act
            if (gameTables.Count > 0)
            {
                for (int i = 0; i < gameTables.Count && !found; i++)
                {
                    if (gameController.GetByTableId(gameTables[i].Id) == null)
                    {
                        gameTable = gameTables[i];
                        found     = true;
                    }
                }
                game.gameTable = GameTableConverter.ConvertFromGameTableModelToGameTable(gameTable);
                gameController.CreateGame(game);
            }
            GameModel gameModel = gameController.GetByTableId(gameTable.Id);

            //Assert
            Assert.IsNotNull(gameModel);
            //Cleanup
            gameDB.Delete(gameModel);
        }
 public GameTable GetGameTableByTableName(string name)
 {
     if (name == null)
     {
         throw new ArgumentNullException();
     }
     else
     {
         return(GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetGameTableByTableName(name)));
     }
 }
 public GameTable GetGameTableById(int id)
 {
     if (id == 0)
     {
         throw new ArgumentException();
     }
     else
     {
         return(GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetById(id)));
     }
 }
        /* This method is called when a user want to join a GameTable.
         * It checks wether or notthe table is already full and if it is the user cannot join the table.
         * if a user is successfull in joining a table, a seat is subtracted from the seat count,
         * such that the it can keep track of how many users have joined the table
         */
        public GameTable JoinGameTable(CGUser user, GameTable chosenTable)
        {
            if (user == null || chosenTable == null)
            {
                throw new ArgumentNullException();
            }
            else
            {
                GameTable      databaseTable = null;
                GameTableModel modelTable    = null;
                CGUserModel    userModel     = CGUserConverter.ConvertFromCGUserToCGUserModel(user);
                //Checking if the user is already sitting at the table and returning it without modifying if so.
                for (int i = 0; i < chosenTable.Users.Count; i++)
                {
                    if (userModel.UserName == chosenTable.Users[i].UserName)
                    {
                        return(chosenTable);
                    }
                }
                TransactionOptions transOptions = new TransactionOptions();
                transOptions.IsolationLevel = IsolationLevel.ReadUncommitted;
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transOptions)) {
                    //Checking if the user is sitting at another table.
                    if (userModel.TableID != 0 && userModel.TableID != chosenTable.Id)
                    {
                        modelTable = gameTableDB.GetById(userModel.TableID);
                    }
                    //Getting the table from the database for later comparison.
                    databaseTable = GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetById(chosenTable.Id));
                    //Optimistically handling concurrency by checking if the seats available at the chosen table
                    //are the same as the seats available in the database, if not we throw an exception.
                    if (chosenTable.seats == databaseTable.seats && databaseTable.seats > 0)
                    {
                        userManagement.UpdateUserTableId(user, databaseTable.Id);
                        databaseTable.Users.Add(user);
                        UpdateGameTableSeats(databaseTable, 1);
                        //If the user was sitting at another table we free up the seat.
                        if (modelTable != null)
                        {
                            gameTableDB.UpdateGameTableSeats(modelTable, -1);
                        }
                    }
                    else
                    {
                        throw new Exception("Table busy");
                    }
                    Thread.Sleep(2000);
                    scope.Complete();
                }

                return(databaseTable);
            }
        }
 public bool DeleteGameTable(int id)
 {
     if (id == 0)
     {
         throw new ArgumentException();
     }
     else
     {
         bool      res   = false;
         GameTable table = GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetById(id));
         if (table != null)
         {
             gameTableDB.Delete(GameTableConverter.ConvertFromGameTableToGameTableModel(table));
             res = true;
         }
         return(res);
     }
 }
 public GameTable CreateGameTable(CGUser user, string tableName)
 {
     if (user == null || tableName == null)
     {
         throw new ArgumentNullException();
     }
     else
     {
         GameTableModel tableModel = new GameTableModel()
         {
             TableName = tableName,
             DeckId    = 2,
             seats     = 4
         };
         gameTableDB.Insert(tableModel);
         GameTable table = GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetGameTableByTableName(tableName));
         return(JoinGameTable(user, table));
     }
 }
        public void GetUserByTableIdTest()
        {
            //Arrange
            cgUserDB = new CGUserDB();
            GameTableDB gameTableDB = new GameTableDB();
            var         table       = new GameTableModel("TestTable");

            table.DeckId = 2;
            table.seats  = 4;
            //Act
            gameTableDB.Insert(table);
            table = gameTableDB.GetGameTableByTableName("TestTable");
            GameTable gameTable = GameTableConverter.ConvertFromGameTableModelToGameTable(table);

            gameTable.Users.Add(CGUserConverter.convertFromCGUserModelToCGUser(cgUserDB.GetById("Test")));
            CGUserModel testUser = cgUserDB.GetById("Test");

            cgUserDB.UpdateUserTableId(testUser, table.Id);
            List <CGUserModel> foundUser = cgUserDB.GetUserByTableId(table.Id);

            Assert.AreEqual(testUser.UserName, foundUser[0].UserName);
            gameTableDB.Delete(table);
        }
        public void StartGameTest()
        {
            //Arrange
            GameController   gameController = new GameController();
            IGameDBIF        gameDB         = new GameDB();
            IDeckDBIF        deckDb         = new DeckDB();
            ICGUserDBIF      userDB         = new CGUserDB();
            IGameTableDBIF   gameTableDB    = new GameTableDB();
            List <GameModel> games          = (List <GameModel>)gameDB.GetAll();
            List <CGUser>    users          = CGUserConverter.ConvertFromListOfCGUserModelToListOfCGUser((List <CGUserModel>)userDB.GetAll());
            GameTable        gameTable      = new GameTable {
                Deck      = DeckConverter.ConvertFromDeckModelToDeck(deckDb.GetById(2)),
                seats     = 4,
                TableName = "TestTable",
            };

            if (users.Count > 4)
            {
                for (int i = 0; i < 4; i++)
                {
                    gameTable.Users.Add(users[i]);
                }
            }
            gameTableDB.Insert(GameTableConverter.ConvertFromGameTableToGameTableModel(gameTable));
            gameTable = GameTableConverter.ConvertFromGameTableModelToGameTable(gameTableDB.GetGameTableByTableName("TestTable"));

            //Act
            gameController.StartGame(gameTable);
            List <GameModel> games2 = (List <GameModel>)gameDB.GetAll();

            //Assert
            Assert.AreNotEqual(games.Count, games2.Count);
            //Cleanup
            gameDB.Delete(gameDB.GetByTableId(gameTable.Id));
            gameTableDB.Delete(GameTableConverter.ConvertFromGameTableToGameTableModel(gameTable));
        }
 public IEnumerable <GameTable> GetAll()
 {
     return(GameTableConverter.ConvertFromListOfGameTableModelToListOfGameTable((List <GameTableModel>)gameTableDB.GetAll()));
 }
 public void UpdateGameTableSeats(GameTable gameTable, int amount)
 {
     gameTableDB.UpdateGameTableSeats(GameTableConverter.ConvertFromGameTableToGameTableModel(gameTable), amount);
 }