public Game StatusLong() { Game game = new Game(); game.Board = BoggleBoard.ToString(); game.TimeLimit = TimeLimit; game.Player1 = Player1.ActiveLongUser(); game.Player2 = Player2.ActiveLongUser(); int left = (int)(StartTime.AddSeconds((double)TimeLimit) - DateTime.Now).TotalSeconds; if (GameState == "completed" || left <= 0) { TimeLeft = 0; GameState = "completed"; game.Player1 = Player1.CompletedLongUser(); game.Player2 = Player2.CompletedLongUser(); } else { TimeLeft = left; } game.TimeLeft = TimeLeft; game.GameState = GameState; return(game); }
/// <summary> /// Returns score of given word whilist accounting for words played /// </summary> /// <param name="word"></param> /// <param name="board"></param> /// <param name="playerWordList"></param> /// <returns></returns> private int GetWordScore(string word, BoggleBoard board, List <string> playerWordList) { PlayedWord playedWord = new PlayedWord(word); if (word.Length < 3) { return(0); } // Check if word can be formed if (!board.CanBeFormed(word)) { return(-1); } // Check if word is legal string contents = File.ReadAllText("dictionary.txt"); if (contents.Contains(word)) { if (playerWordList.Contains(playedWord.Word)) { return(0); } switch (word.Length) { case 3: case 4: return(1); case 5: return(2); case 6: return(3); case 7: return(5); // In this case, default is being used for if word length > 7 // Must be a better way to do this default: return(11); } } return(-1); }
public PlayWordDetails PlayWord(string GameID, PlayWordDetails PlayWordDetails, out HttpStatusCode status) { string word = PlayWordDetails.Word.Trim(); PlayedWord playedWord = new PlayedWord(word); if (word == "" || word == null || word.Length > 30) { status = Forbidden; return(null); } lock (sync) { using (SqlConnection connection = new SqlConnection(BoggleDB)) { connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) { // Check if game exists using (SqlCommand command = new SqlCommand( "select * from Games where Games.GameID = @GameID", connection, transaction)) { command.Parameters.AddWithValue("@GameID", GameID); using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.HasRows) { status = Forbidden; reader.Close(); transaction.Commit(); return(null); } // Game exists, Pending reader.Read(); if (reader["Player2"].ToString() == "") { status = Conflict; reader.Close(); transaction.Commit(); return(null); } // completed DateTime start = (DateTime)reader["StartTime"]; int timeLimit = (int)reader["TimeLimit"]; if ((start.AddSeconds((double)timeLimit) - DateTime.Now).TotalSeconds <= 0) { status = Conflict; reader.Close(); transaction.Commit(); return(null); } // User is in this game if (reader["Player1"].ToString() == PlayWordDetails.UserToken || reader["Player2"].ToString() == PlayWordDetails.UserToken) { BoggleBoard board = new BoggleBoard(reader["Board"].ToString()); reader.Close(); List <string> words = new List <string>(); // Get all words using (SqlCommand wordCommand = new SqlCommand( "select Word from Words where Player = @Player", connection, transaction)) { wordCommand.Parameters.AddWithValue("@Player", PlayWordDetails.UserToken); // Get list of words played using (SqlDataReader wordReader = wordCommand.ExecuteReader()) { while (wordReader.Read()) { words.Add(wordReader["Word"].ToString()); } wordReader.Close(); } int score = GetWordScore(PlayWordDetails.Word, board, words); // Add word to word table using (SqlCommand addWordCommand = new SqlCommand( "insert into Words (Word, GameID, Player, Score) " + "values(@Word, @GameID, @Player, @Score)", connection, transaction)) { addWordCommand.Parameters.AddWithValue("@Word", PlayWordDetails.Word); addWordCommand.Parameters.AddWithValue("@GameID", GameID); addWordCommand.Parameters.AddWithValue("@Player", PlayWordDetails.UserToken); addWordCommand.Parameters.AddWithValue("@Score", score); if (addWordCommand.ExecuteNonQuery() != 1) { throw new Exception("Query failed unexpectedly"); } status = OK; transaction.Commit(); return(new PlayWordDetails(score)); } } } // User is not in this game else { status = Forbidden; reader.Close(); transaction.Commit(); return(null); } } } } } } }
public Game() { BoggleBoard = new BoggleBoard(); Board = BoggleBoard.ToString(); }