/// <summary> /// method for creating user /// </summary> /// <param name="user">the user provided by client. </param> /// <returns>return a userToken for a user. </returns> public RegReturn Register(UserInfo user) { lock (sync) { if (user == null || user.Nickname == null || user.Nickname.Trim().Length == 0) { SetStatus(Forbidden); return(null); } if (user.Nickname.StartsWith("@")) { Thread.Sleep(10000); } string userID = Guid.NewGuid().ToString(); while (users.ContainsKey(userID)) { userID = Guid.NewGuid().ToString(); } Player player = new Player(); player.UserToken = userID; player.Nickname = user.Nickname; users.Add(userID, player); RegReturn info = new RegReturn(); info.UserToken = userID; SetStatus(Created); return(info); } }
/// <summary> /// cancel join request method /// </summary> /// <param name="user">parameter</param> public void CancelJoinRequest(RegReturn user) { lock (sync) { if (users.ContainsKey(user.UserToken) == false) { SetStatus(Forbidden); } else { Player player = users[user.UserToken]; String GameID = player.GameID; if (GameID == null || games.ContainsKey(GameID) == false) { SetStatus(Forbidden); } else { Game game = games[GameID]; if (game.GameState != "pending") { SetStatus(Forbidden); } else { game.player1 = null; UserInfo info = new UserInfo(); info.Nickname = ""; } } } } }
/// <summary> /// method for creating user /// </summary> /// <param name="user">the user provided by client. </param> /// <returns>return a userToken for a user. </returns> public RegReturn Register(UserInfo user) { //check user, user name if (user == null || user.Nickname == null || user.Nickname.Trim().Length == 0) { SetStatus(Forbidden); return(null); } // check username with "@" start if (user.Nickname.StartsWith("@")) { Thread.Sleep(5000); } using (SqlConnection conn = new SqlConnection(BoggleDB)) { // Connections must be opened conn.Open(); // Database commands should be executed within a transaction. When commands // are executed within a transaction, either all of the commands will succeed // or all will be canceled. You don't have to worry about some of the commands // changing the DB and others failing. using (SqlTransaction trans = conn.BeginTransaction()) { // An SqlCommand executes a SQL statement on the database. In this case it is an // insert statement. The first parameter is the statement, the second is the // connection, and the third is the transaction. using (SqlCommand command = new SqlCommand(getPath("registerInsert"), conn, trans)) { string userID = Guid.NewGuid().ToString(); // This is where the placeholders are replaced. command.Parameters.AddWithValue("@UserID", userID); command.Parameters.AddWithValue("@Nickname", user.Nickname); // This executes the command within the transaction over the connection. The number of rows // that were modified is returned. Perhaps I should check and make sure that 1 is returned // as expected. command.ExecuteNonQuery(); // Immediately before each return that appears within the scope of a transaction, it is // important to commit the transaction. Otherwise, the transaction will be aborted and // rolled back as soon as control leaves the scope of the transaction. trans.Commit(); // return info RegReturn info = new RegReturn(); info.UserToken = userID; SetStatus(Created); return(info); } } } }