コード例 #1
0
        public bool addGameWithDrawToDB(string player1, string player2, DateTime gameTime)
        {
            using (var db = new ConnectFourContext())
            {
                User player1InDB = db.Users.SingleOrDefault(user => user.userName == player1);
                User player2InDB = db.Users.SingleOrDefault(user => user.userName == player2);

                if (player1InDB == null || player2InDB == null)
                {
                    return(false);
                }

                Game g = new Game();
                g.user1    = player1InDB;
                g.user2    = player2InDB;
                g.gameTime = gameTime;
                // set winner to 0 that means its draw
                g.winner = 0;

                player1InDB.numberOfGames += 1;
                player2InDB.numberOfGames += 1;

                db.Entry(player1InDB).State = System.Data.Entity.EntityState.Modified;
                db.Entry(player2InDB).State = System.Data.Entity.EntityState.Modified;

                db.Games.Add(g);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #2
0
        public bool addGameWithWinToDB(string player1, string player2, string winnerPlayer, DateTime gameTime)
        {
            using (var db = new ConnectFourContext())
            {
                User player1InDB = db.Users.SingleOrDefault(user => user.userName == player1);
                User player2InDB = db.Users.SingleOrDefault(user => user.userName == player2);
                if (player1InDB == null || player2InDB == null)
                {
                    return(false);
                }
                Game g = new Game();
                g.user1    = player1InDB;
                g.user2    = player2InDB;
                g.gameTime = gameTime;
                if (winnerPlayer == player1)
                {
                    g.winner = player1InDB.userId;
                    player1InDB.numberOfPoints += 1;
                    player1InDB.numberOfWins   += 1;
                    player2InDB.numberOfLoses  += 1;
                }
                else
                {
                    g.winner = player2InDB.userId;
                    player1InDB.numberOfLoses  += 1;
                    player2InDB.numberOfWins   += 1;
                    player2InDB.numberOfPoints += 1;
                }

                player1InDB.numberOfGames += 1;
                player2InDB.numberOfGames += 1;

                db.Entry(player1InDB).State = System.Data.Entity.EntityState.Modified;
                db.Entry(player2InDB).State = System.Data.Entity.EntityState.Modified;

                db.Games.Add(g);
                db.SaveChanges();
                return(true);
            }
        }
コード例 #3
0
 public void updateUserStatus(string username, USER_STATUS new_status)
 {
     using (var db = new ConnectFourContext())
     {
         User playerInDB = db.Users.SingleOrDefault(user => user.userName == username);
         if (playerInDB == null)
         {
             return;
         }
         playerInDB.status          = new_status;
         db.Entry(playerInDB).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }