/// <summary> /// Given the proper parameters, it runs the correct method in our BoggleServer and sends the response message. /// This is essentially covering the functionality of IBoggleService from before. /// </summary> /// <param name="Type"></param> /// <param name="GameID"></param> /// <param name="IsBrief"></param> /// <param name="content"></param> private void ParseMessage(string Type, string Url, string GameID, string IsBrief, dynamic content) { HttpStatusCode status; // Each method we call should return the object we to JSON encoded if (Type == "POST") { // CreateUser if (Url == "users") { UserID ReturnID = server.CreateUser(content, out status); CompileMessage(status, ReturnID); } // JoinGame else if (Url == "games") { GameIDReturn IDReturn = server.JoinGame(content, out status); CompileMessage(status, IDReturn); } } else if (Type == "PUT") { // CancelJoinRequest if (Url == "games" && GameID == string.Empty) { server.CancelJoinRequest(content, out status); CompileMessage(status, null); } // PlayWord else { ScoreReturn Score = server.PlayWord(content, GameID, out status); CompileMessage(status, Score); } } // GetGameStatus else { Game CurrentGame = new Game(); CurrentGame = server.GetGameStatus(GameID, IsBrief, out status); CompileMessage(status, CurrentGame); } }
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); } }