/// <summary> /// this is a helper method to return a short version of GameStatus. /// </summary> /// <param name="GameID">the GameID that the player in. </param> /// <returns>return information of client need.</returns> public statudReturn4 helpBrief(String GameID, int timeLeft, String GameState) { 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("helpBriefCheck"), conn, trans)) { command.Parameters.AddWithValue("@GameID", GameID); using (SqlDataReader reader = command.ExecuteReader()) { reader.Read(); statudReturn4 info = new statudReturn4(); info.GameState = GameState; info.TimeLeft = timeLeft; info.Player1 = new playerForstatus4(); info.Player1.Score = getPoint(reader["Player1"].ToString(), GameID); info.Player2 = new playerForstatus4(); info.Player2.Score = getPoint(reader["Player2"].ToString(), GameID); SetStatus(OK); return(info); } } } } }
///// <summary> ///// Demo. You can delete this. ///// </summary> //public string WordAtIndex(int n) //{ // if (n < 0) // { // SetStatus(Forbidden); // return null; // } // string line; // using (StreamReader file = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "dictionary.txt")) // { // while ((line = file.ReadLine()) != null) // { // if (n == 0) break; // n--; // } // } // if (n == 0) // { // SetStatus(OK); // return line; // } // else // { // SetStatus(Forbidden); // return null; // } //} /// <summary> /// this is a helper method to return a short version of GameStatus. /// </summary> /// <param name="GameID">the GameID that the player in. </param> /// <returns>return information of client need.</returns> public statudReturn4 helpBrief(String GameID) { Game game = games[GameID]; statudReturn4 info = new statudReturn4(); info.GameState = game.GameState; info.TimeLeft = game.TimeLeft; info.Player1 = new playerForstatus4(); info.Player1.Score = game.player1.Score; info.Player2 = new playerForstatus4(); info.Player2.Score = game.player2.Score; return(info); }
/// <summary> /// used to check gameStatus. /// </summary> /// <param name="GameID">the game ID that the client want to check. </param> /// <param name="Brief">whether the user want just get a brief information </param> /// <returns>return the game status information. </returns> public statudReturn4 gameStatus(String GameID, String Brief) { 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("gameStatus"), conn, trans)) { command.Parameters.AddWithValue("@GameID", GameID); using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.HasRows) { SetStatus(Forbidden); reader.Close(); return(null); } else { reader.Read(); if (reader["Player2"] is DBNull) { SetStatus(OK); statudReturn4 info = new statudReturn4() { GameState = "pending" }; return(info); } else { String board = reader["Board"].ToString(); int timeLimit = (Int32)reader["TimeLimit"]; DateTime startTime = (DateTime)reader["StartTime"]; double tran = Convert.ToDouble((DateTime.Now.Subtract(startTime)).TotalSeconds); int getTime = timeLimit - (Int32)tran; //maybe finish the part for Active. if (getTime > 0) { if (Brief == "yes") { return(helpBrief(GameID, getTime, "active")); } statudReturn4 info = new statudReturn4(); info.GameState = "active"; info.Board = board; info.TimeLimit = timeLimit; info.TimeLeft = getTime; info.Player1 = new playerForstatus4(); info.Player1.Nickname = getName(reader["Player1"].ToString()); info.Player1.Score = getPoint(reader["Player1"].ToString(), GameID); info.Player2 = new playerForstatus4(); info.Player2.Nickname = getName(reader["Player2"].ToString()); info.Player2.Score = getPoint(reader["Player2"].ToString(), GameID); SetStatus(OK); return(info); } else { if (Brief == "yes") { return(helpBrief(GameID, 0, "completed")); } statudReturn4 info = new statudReturn4(); info.GameState = "completed"; info.Board = board; info.TimeLimit = timeLimit; info.TimeLeft = 0; info.Player1 = new playerForstatus4(); info.Player1.Nickname = getName(reader["Player1"].ToString()); info.Player1.Score = getPoint(reader["Player1"].ToString(), GameID); info.Player1.WordsPlayed = getWords(GameID, reader["Player1"].ToString()); info.Player2 = new playerForstatus4(); info.Player2.Nickname = getName(reader["Player2"].ToString()); info.Player2.Score = getPoint(reader["Player2"].ToString(), GameID); info.Player2.WordsPlayed = getWords(GameID, reader["Player2"].ToString()); SetStatus(OK); return(info); } } } } } } } }
/// <summary> /// method for play word /// </summary> /// <param name="user">the UserToek and the word that this player typied. </param> /// <param name="GameID">the GameID of this player in . </param> /// <returnsplayWordsReturn>return the score of the word that the user type. </returns> public playWordsReturn PlayWords(playGameInfo user, String GameID) { //get gameID int number; bool result = Int32.TryParse(GameID, out number); //check user info if (user == null || user.Word == null || user.Word.Trim().Length == 0 || GameID == null || user.UserToken == null || !result) { SetStatus(Forbidden); return(null); } //check game status statudReturn4 check = gameStatus(GameID, ""); if (check.GameState != "active") { SetStatus(Conflict); return(null); } // if (checkmatching(GameID, user.UserToken) == false) { SetStatus(Forbidden); return(null); } using (SqlConnection conn = new SqlConnection(BoggleDB)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { BoggleBoard NewGame = new BoggleBoard(check.Board); int scoreResult = 0; if (user.Word.Length < 3) { scoreResult = 0; } else if (NewGame.CanBeFormed(user.Word) && File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory.ToString() + "dictionary.txt").Contains(user.Word)) { scoreResult = CalculateScore(user.Word); } else { scoreResult--; } if (checkWordPlay(GameID, user.UserToken, user.Word) == false) { using (SqlCommand cmd = new SqlCommand(getPath("updatePlayWords"), conn, trans)) { cmd.Parameters.AddWithValue("@Word", user.Word); cmd.Parameters.AddWithValue("@Score", scoreResult); cmd.Parameters.AddWithValue("@GameID", GameID); cmd.Parameters.AddWithValue("@Player", user.UserToken); cmd.ExecuteNonQuery(); trans.Commit(); } } else { scoreResult = 0; } playWordsReturn info = new playWordsReturn(); info.Score = scoreResult; SetStatus(OK); return(info); } } }
/// <summary> /// used to check gameStatus. /// </summary> /// <param name="GameID">the game ID that the client want to check. </param> /// <param name="Brief">whether the user want just get a brief information </param> /// <returns></returns> public statudReturn4 gameStatus(String GameID, String Brief) { lock (sync) { if (games.ContainsKey(GameID) == false) { SetStatus(Forbidden); return(null); } else { Game game = games[GameID]; if (game.GameState == "pending") { SetStatus(OK); statudReturn4 info = new statudReturn4() { GameState = "pending" }; return(info); } else if (game.GameState == "active") { double trans = Convert.ToDouble((DateTime.Now.Subtract(game.startTime)).TotalSeconds); int getTime = game.TimeLimit - (Int32)trans; if (getTime > 0) { game.TimeLeft = getTime; } else { game.TimeLeft = 0; game.GameState = "completed"; return(gameStatus(GameID, Brief)); } if (Brief == "yes") { statudReturn4 info = helpBrief(GameID); SetStatus(OK); return(info); } else { statudReturn4 info = new statudReturn4(); info.GameState = game.GameState; info.Board = game.boggleboard.ToString(); info.TimeLimit = game.TimeLimit; info.TimeLeft = game.TimeLeft; info.Player1 = new playerForstatus4(); info.Player1.Score = game.player1.Score; info.Player1.Nickname = game.player1.Nickname; info.Player2 = new playerForstatus4(); info.Player2.Nickname = game.player2.Nickname; info.Player2.Score = game.player2.Score; SetStatus(OK); return(info); } } else { if (Brief == "yes") { statudReturn4 info2 = helpBrief(GameID); SetStatus(OK); return(info2); } else { statudReturn4 info = new statudReturn4(); info.GameState = game.GameState; info.Board = game.boggleboard.ToString(); info.TimeLimit = game.TimeLimit; info.TimeLeft = 0; info.Player1 = new playerForstatus4(); info.Player1.Score = game.player1.Score; info.Player1.Nickname = game.player1.Nickname; info.Player1.WordsPlayed = new List <WordsPlayed>(game.player1.words); info.Player2 = new playerForstatus4(); info.Player2.Nickname = game.player2.Nickname; info.Player2.Score = game.player2.Score; info.Player2.WordsPlayed = new List <WordsPlayed>(game.player2.words); SetStatus(OK); return(info); } } } } }