public ScoreReturn PlayWord(WordInfo InputObject, string GameID) { lock (sync) { Game CurrentGame; ScoreReturn Score = new ScoreReturn(); Score.Score = 0; int internalscore = 0; //All the failure cases for bad input. if (InputObject.Word == null || InputObject.Word.Trim().Length == 0) { SetStatus(Forbidden); return(Score); } // Playing a word in a pending game. if ((GameList.Keys.Count + 1).ToString() == GameID) { SetStatus(Conflict); return(Score); } // Invalid GameID if (!GameList.TryGetValue(Int32.Parse(GameID), out CurrentGame) || !UserIDs.ContainsKey(InputObject.UserToken)) { SetStatus(Forbidden); return(Score); } else if (CurrentGame.Player1Token != InputObject.UserToken && CurrentGame.Player2Token != InputObject.UserToken) { SetStatus(Forbidden); return(Score); } else if (CurrentGame.GameState != "active") { SetStatus(Conflict); return(Score); } else { CurrentGame = new Game(); GameList.TryGetValue(Int32.Parse(GameID), out CurrentGame); string word = InputObject.Word.Trim(); BoggleBoard Board = new BoggleBoard(CurrentGame.Board); // If its player 1 playing the word. if (CurrentGame.Player1Token == InputObject.UserToken) { if (word.Length < 3) { internalscore = 0; // repeated code across branches, can be cleaned up later. this is to fix branching issues, will need to be done with player 2 as well WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player1.WordsPlayed.Add(CurrentPair); } else if (Board.CanBeFormed(word)) { foreach (WordScore obj in CurrentGame.Player1.WordsPlayed) { if (obj.Word == word) { internalscore = 0; break; } } if (word.Length == 3 || word.Length == 4) { internalscore = 1; } else if (word.Length == 5) { internalscore = 2; } else if (word.Length == 6) { internalscore = 3; } else if (word.Length == 7) { internalscore = 5; } else if (word.Length > 7) { internalscore = 11; } WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player1.WordsPlayed.Add(CurrentPair); CurrentGame.Player1.Score += internalscore; } else { internalscore = -1; WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player1.WordsPlayed.Add(CurrentPair); CurrentGame.Player1.Score += internalscore; } GameList[Int32.Parse(GameID)] = CurrentGame; } //If its player 2 playing the word. if (CurrentGame.Player2Token == InputObject.UserToken) { if (word.Length < 3) { internalscore = 0; // repeated code across branches, can be cleaned up later. this is to fix branching issues, will need to be done with player 2 as well WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player2.WordsPlayed.Add(CurrentPair); } else if (Board.CanBeFormed(word)) { foreach (WordScore obj in CurrentGame.Player1.WordsPlayed) { if (obj.Word == word) { internalscore = 0; break; } } if (word.Length == 3 || word.Length == 4) { internalscore = 1; } else if (word.Length == 5) { internalscore = 2; } else if (word.Length == 6) { internalscore = 3; } else if (word.Length == 7) { internalscore = 5; } else if (word.Length > 7) { internalscore = 11; } WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player2.WordsPlayed.Add(CurrentPair); CurrentGame.Player2.Score += internalscore; } else { internalscore = -1; WordScore CurrentPair = new WordScore(); CurrentPair.Score = internalscore; CurrentPair.Word = word; CurrentGame.Player2.WordsPlayed.Add(CurrentPair); CurrentGame.Player2.Score += internalscore; } GameList[Int32.Parse(GameID)] = CurrentGame; } } // Records the word as being played. SetStatus(OK); Score.Score = internalscore; return(Score); } }
/// <summary> /// Play a word in a game. /// /// (1) If Word is null or empty when trimmed, or if GameID or UserToken is /// missing or invalid, or if UserToken is not a player in the game /// identified by GameID, responds with response code 403 (Forbidden). /// (2) Otherwise, if the game state is anything other than "active", responds /// with response code 409 (Conflict). /// (3) Otherwise, records the trimmed Word as being played by UserToken in the /// game identified by GameID. Returns the score for Word in the context of /// the game (e.g. if Word has been played before the score is zero). Responds /// with status 200 (OK). Note: The word is not case sensitive. /// </summary> /// <param name="wordPlayed">Contains the body of the request</param> /// <param name="GameID">ID of the game</param> /// <returns>The score of the played word</returns> public PlayWordReturnInfo PlayWord(string GameID, WordInfo wordPlayed) { // Check the validity GameInfo currentGameInfo = GetGameInfo(GameID); bool isInvalidObject = (wordPlayed.Word == null || wordPlayed.UserToken == null || GameID == null || currentGameInfo == null); // If the passed object contains non-null elements, begin processing it if (!isInvalidObject) { string word = wordPlayed.Word.Trim().ToUpper(); // If Word is null or empty when trimmed, or if GameID or UserToken is // missing or invalid, or if UserToken is not a player in the game identified // by GameID, responds with response code 403 (Forbidden). if (String.IsNullOrEmpty(word) || String.IsNullOrWhiteSpace(word) || currentGameInfo == null || String.IsNullOrEmpty(GameID) || (currentGameInfo.Player1Token != wordPlayed.UserToken && currentGameInfo.Player2Token != wordPlayed.UserToken)) { SetStatus(Forbidden); return(null); } // Otherwise, if the game state is anything other than "active", responds // with response code 409 (Conflict). else if (currentGameInfo.GameState != "active") { SetStatus(Conflict); return(null); } // Otherwise, records the trimmed Word as being played by this player in this // game identified by the GameID. else { // Form a new board from the game's board and retrieve the player's info BoggleBoard board = new BoggleBoard(currentGameInfo.Board); // If the word was already played, add it as an attempt and // return the score is 0 (since duplicate word). if (WasWordPlayed(wordPlayed.UserToken, GameID, wordPlayed.Word)) { SetStatus(OK); AddWordToDB(wordPlayed.Word, wordPlayed.UserToken, GameID, 0); PlayWordReturnInfo info = new PlayWordReturnInfo(); info.Score = "0"; return(info); } // Now, if the word was never played before, first ensure that the // word is valid on the board. If so, add the word to DB and return // this play's score. else if (board.CanBeFormed(wordPlayed.Word)) { // Compute the score and add it to the Words DB as the player's play // in this game identified the passed GameID int score = scoreWord(wordPlayed.Word, GameID, wordPlayed.UserToken); AddWordToDB(wordPlayed.Word, wordPlayed.UserToken, GameID, score); SetStatus(OK); PlayWordReturnInfo info = new PlayWordReturnInfo(); info.Score = score + ""; return(info); } // Otherwise, this never-before-played word cannot be formed on this // game's board. As such, add this word to DB and return this play's // score which will be -1. else { AddWordToDB(wordPlayed.Word, wordPlayed.UserToken, GameID, -1); SetStatus(OK); PlayWordReturnInfo info = new PlayWordReturnInfo(); info.Score = "-1"; return(info); } } } // The dynamic object sent with the PUT request contained a null object else { SetStatus(Forbidden); return(null); } }