public WordScore PlayWord(PlayedWord data, string gameID) { lock (sync) { if (gameID == null || data.UserToken == null || !users.ContainsKey(data.UserToken) || (!games.ContainsKey(gameID) && gameID != pendingGame) || data.Word == null || data.Word.Trim().Length == 0) { SetStatus(Forbidden); return(null); } if (gameID == pendingGame) { SetStatus(Conflict); return(null); } BoggleGame game = games[gameID]; if (game.GameState != "active") { SetStatus(Conflict); return(null); } try { int score = game.PlayWord(data.UserToken, data.Word.Trim()); SetStatus(OK); return(new WordScore() { Score = score }); } catch (Exception) { SetStatus(Forbidden); return(null); } } }
public Game JoinGame(GameRequest data) { lock (sync) { if (data.UserToken == null || !users.ContainsKey(data.UserToken) || data.TimeLimit < 5 || data.TimeLimit > 120) { SetStatus(Forbidden); return(null); } else if (data.UserToken == pendingPlayer) { SetStatus(Conflict); return(null); } else if (pendingPlayer == null) { pendingPlayer = data.UserToken; pendingTimeLimit = data.TimeLimit; SetStatus(Accepted); return(new Game() { GameID = pendingGame }); } else { games[pendingGame] = new BoggleGame(pendingPlayer, data.UserToken, dictionary, (pendingTimeLimit + data.TimeLimit) / 2); pendingPlayer = null; Game result = new Game() { GameID = pendingGame }; pendingGame = "G" + (Int32.Parse(pendingGame.Substring(1)) + 1); SetStatus(Created); return(result); } } }
public Status Status(string game, string brief) { // Validate incoming parameters if (game == null) { SetStatus(Forbidden); return(null); } long gameID; if (!Int64.TryParse(game, out gameID)) { SetStatus(Forbidden); return(null); } using (SqlConnection conn = new SqlConnection(BoggleDB)) { conn.Open(); SqlTransaction trans = conn.BeginTransaction(); try { string player1, player2; int timeLimit; DateTime startTime; string board; // Get information about the game in which the word is to be played SqlCommand command = new SqlCommand("select GameID, Player1, Player2, TimeLimit, StartTime, Board from Games where GameID=@GameID", conn, trans); command.Parameters.AddWithValue("@GameID", gameID); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { player1 = (string)reader["Player1"]; player2 = (reader["Player2"] is DBNull) ? null : (string)reader["Player2"]; timeLimit = (int)reader["TimeLimit"]; object o = reader["StartTime"]; startTime = (reader["StartTime"] is DBNull) ? DateTime.Now : (DateTime)reader["StartTime"]; board = (reader["Board"] is DBNull) ? null : (string)reader["Board"]; } else { SetStatus(Forbidden); return(null); } } Status status = new Status(); if (player2 == null) { status.GameState = "pending"; return(status); } status.TimeLeft = BoggleGame.GetTimeLeft(startTime, timeLimit); if (status.TimeLeft == 0) { status.GameState = "completed"; } else { status.GameState = "active"; } command = new SqlCommand("select GameID, Player, Word, Score from Words where GameID=@GameID and Player=@Player order by Id", conn, trans); command.Parameters.AddWithValue("@GameID", gameID); command.Parameters.AddWithValue("@Player", player1); List <WordAndScore> words1 = null; if (brief != "yes") { words1 = new List <WordAndScore>(); } int score1 = 0; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string word = (string)reader["Word"]; int score = (int)reader["Score"]; score1 += score; if (brief != "yes") { words1.Add(new WordAndScore() { Word = word, Score = score }); } } } command = new SqlCommand("select GameID, Player, Word, Score from Words where GameID=@GameID and Player=@Player order by Id", conn, trans); command.Parameters.AddWithValue("@GameID", gameID); command.Parameters.AddWithValue("@Player", player2); List <WordAndScore> words2 = null; if (brief != "yes") { words2 = new List <WordAndScore>(); } int score2 = 0; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string word = (string)reader["Word"]; int score = (int)reader["Score"]; score2 += score; if (brief != "yes") { words2.Add(new WordAndScore() { Word = word, Score = score }); } } } status.Player1 = new Player(); status.Player1.Score = score1; status.Player2 = new Player(); status.Player2.Score = score2; if (brief != "yes") { status.Board = board; status.TimeLimit = timeLimit; command = new SqlCommand("select Users1.Nickname as Nickname1, Users2.Nickname as Nickname2 from Users as Users1, Users as Users2 where Users1.UserToken=@Player1 and Users2.UserToken=@Player2", conn, trans); command.Parameters.AddWithValue("@Player1", player1); command.Parameters.AddWithValue("@Player2", player2); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { status.Player1.Nickname = (string)reader["Nickname1"]; status.Player2.Nickname = (string)reader["Nickname2"]; } } if (status.GameState != "active") { status.Player1.WordsPlayed = words1; status.Player2.WordsPlayed = words2; } } return(status); } catch (Exception) { trans.Rollback(); SetStatus(InternalServerError); return(null); } finally { if (trans.Connection != null) { trans.Commit(); } } } }
public WordScore PlayWord(PlayedWord data, string game) { // Validate incoming parameters if (game == null || data.UserToken == null || data.Word == null || data.Word.Trim().Length == 0) { SetStatus(Forbidden); return(null); } // The GameID must be an integer long gameID; if (!Int64.TryParse(game, out gameID)) { SetStatus(Forbidden); return(null); } using (SqlConnection conn = new SqlConnection(BoggleDB)) { conn.Open(); SqlTransaction trans = conn.BeginTransaction(); try { // Get information about the game in which the word is to be played SqlCommand command = new SqlCommand("select GameID, Player1, Player2, Board, TimeLimit, StartTime from Games where GameID=@GameID and (Player1 = @Player or Player2 = @Player)", conn, trans); command.Parameters.AddWithValue("@GameID", gameID); command.Parameters.AddWithValue("@Player", data.UserToken); //string player; string board; int timeLimit; DateTime startTime; using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { //player = (string)(((string)reader["Player1"] == data.UserToken) ? reader["Player1"] : reader["Player2"]); if (reader["Player2"] is DBNull) { SetStatus(Conflict); return(null); } board = (string)reader["Board"]; timeLimit = (int)reader["TimeLimit"]; startTime = (DateTime)reader["StartTime"]; if (reader["Player2"] is DBNull) { SetStatus(Conflict); return(null); } } else { SetStatus(Forbidden); return(null); } } if (BoggleGame.GetTimeLeft(startTime, timeLimit) <= 0) { SetStatus(Conflict); return(null); } int score = BoggleGame.WordValue(data.Word.Trim(), board, dictionary); if (score == -2) { SetStatus(Forbidden); return(null); } command = new SqlCommand("select Word from Words where GameID=@GameID and Player=@Player and Word=@Word", conn, trans); command.Parameters.AddWithValue("@GameID", gameID); command.Parameters.AddWithValue("@Player", data.UserToken); command.Parameters.AddWithValue("@Word", data.Word.Trim()); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { score = 0; } } command = new SqlCommand("insert into Words (Word, GameID, Player, Score) values(@Word, @GameID, @Player, @Score)", conn, trans); command.Parameters.AddWithValue("@Word", data.Word); command.Parameters.AddWithValue("@GameID", gameID); command.Parameters.AddWithValue("@Player", data.UserToken); command.Parameters.AddWithValue("@Score", score); command.ExecuteNonQuery(); SetStatus(OK); return(new WordScore() { Score = score }); } catch (Exception) { trans.Rollback(); SetStatus(InternalServerError); return(null); } finally { if (trans.Connection != null) { trans.Commit(); } } } }