/// <summary> /// method for joining game /// </summary> /// <param name="user">the user which is joining the game, contains a UserToken and a TimeLimit. </param> /// <returns>return a GameID. </returns> public joinReturn JoinGame(JoinType user) { if (user.TimeLimit < 5 || user.TimeLimit > 120) { SetStatus(Forbidden); return(null); } // need UserToken, GameID, TimeLimit, Player1. else { using (SqlConnection conn = new SqlConnection(BoggleDB)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { // Here, the SqlCommand is a select query. We are interested in whether item.UserID exists in // the Users table. using (SqlCommand command = new SqlCommand(getPath("check"), conn, trans)) { command.Parameters.AddWithValue("@UserID", user.UserToken); // This executes a query (i.e. a select statement). The result is an // SqlDataReader that you can use to iterate through the rows in the response. using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.HasRows) { SetStatus(Forbidden); reader.Close(); return(null); } } } // select all data when the game only has player1 using (SqlCommand command = new SqlCommand(getPath("checkStatus"), conn, trans)) { using (SqlDataReader reader = command.ExecuteReader()) { // if the table does not have player1 if (reader.HasRows == false) { reader.Close(); // insert player1 and timelimit into Games table using (SqlCommand cmd = new SqlCommand(getPath("joinAdd1"), conn, trans)) { cmd.Parameters.AddWithValue("@Player1", user.UserToken); cmd.Parameters.AddWithValue("@TimeLimit", user.TimeLimit); // set the return info joinReturn info = new joinReturn(); info.GameID = cmd.ExecuteScalar().ToString(); SetStatus(Accepted); trans.Commit(); return(info); } } // if the table has player1 else { int TimeLimit = 0; int GameID = 0; // read to get player1's information reader.Read(); Int32.TryParse(reader["GameID"].ToString(), out GameID); Int32.TryParse(reader["TimeLimit"].ToString(), out TimeLimit); String checkToken = reader["Player1"].ToString(); // check user token valid or not if (checkToken == user.UserToken) { SetStatus(Conflict); return(null); } reader.Close(); // when the game need to add player2, insert player2, timelimit, board, startTime using (SqlCommand cmd = new SqlCommand(getPath("joinAdd2"), conn, trans)) { int timeLimit = (TimeLimit + user.TimeLimit) / 2; cmd.Parameters.AddWithValue("@GameID", GameID); cmd.Parameters.AddWithValue("@Player2", user.UserToken); cmd.Parameters.AddWithValue("@TimeLimit", timeLimit); cmd.Parameters.AddWithValue("@Board", new BoggleBoard().ToString()); cmd.Parameters.AddWithValue("@StartTime", DateTime.Now); // We execute the command with the ExecuteScalar method, which will return to // us the requested auto-generated ItemID. // return info joinReturn info = new joinReturn(); info.GameID = cmd.ExecuteScalar().ToString(); SetStatus(Created); trans.Commit(); return(info); } } } } } } } }
/// <summary> /// method for joining game /// </summary> /// <param name="user">the user which is joining the game, contains a UserToken and a TimeLimit. </param> /// <returns>return a GameID. </returns> public joinReturn JoinGame(JoinType user) { lock (sync) { if (!users.ContainsKey(user.UserToken) || user.TimeLimit < 5 || user.TimeLimit > 120) { SetStatus(Forbidden); return(null); } else { if (games.Count == 0) { Game firstGame = new Game(); firstGame.GameID = (games.Count + 1) + ""; firstGame.GameState = "pending"; games[(games.Count + 1) + ""] = firstGame; } Game game = games[(games.Count) + ""]; if (game.player1 != null) { if (game.player1.UserToken == user.UserToken) { SetStatus(Conflict); return(null); } game.GameState = "active"; Player player2 = new Player(); player2.UserToken = user.UserToken; player2.TimeLimit = user.TimeLimit; player2.GameID = game.GameID; player2.Nickname = users[user.UserToken].Nickname; game.player2 = player2; game.player1.words = new List <WordsPlayed>(); game.player2.words = new List <WordsPlayed>(); int avgTimeLimit = (game.player1.TimeLimit + game.player2.TimeLimit) / 2; game.TimeLimit = avgTimeLimit; string gameID = games[games.Count + ""].GameID; Game newPendingGame = new Game(); newPendingGame.GameState = "pending"; newPendingGame.GameID = (games.Count + 1) + ""; games[(games.Count + 1) + ""] = newPendingGame; game.boggleboard = new BoggleBoard(); game.startTime = DateTime.Now; users[game.player2.UserToken] = player2; joinReturn info = new joinReturn(); info.GameID = gameID; SetStatus(Created); return(info); } else { Player player1 = new Player(); player1.UserToken = user.UserToken; player1.TimeLimit = user.TimeLimit; player1.Nickname = users[user.UserToken].Nickname; game.TimeLimit = player1.TimeLimit; player1.GameID = game.GameID; game.player1 = player1; users[game.player1.UserToken] = player1; joinReturn info = new joinReturn(); info.GameID = game.GameID; SetStatus(Accepted); return(info); } } } }