/// Play a word in a game. /// If Word is null or empty or longer than 30 characters when trimmed, or if GameID or UserToken is invalid, or if UserToken is not a player in the /// game identified by GameID, responds with response code 403 (Forbidden). /// Otherwise, if the game state is anything other than "active", responds with response code 409 (Conflict). /// 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. public WordScore PlayWord(WordToPlay w, string gameID) { lock (sync) { if (w.Word == null || w.Word.Trim().Length > 30 || w.UserToken == null || !players.ContainsKey(w.UserToken) || gameID == null || !(games.TryGetValue(gameID, out GameStatus temp) || (temp.Player1.UserToken != w.UserToken || temp.Player2.UserToken != w.UserToken))) { SetStatus(Forbidden); return(null); } else { games.TryGetValue(gameID, out GameStatus g); if (!g.GameState.Equals("active")) { SetStatus(Conflict); return(null); } else { updateTime(gameID); Words wordPlay = new Words(); wordPlay.Word = w.Word; // Generate boggle board board = new BoggleBoard(g.Board); players.TryGetValue(w.UserToken, out Player p); // Score 0 if the word is less than 3 characters or -1 if it doesn't exist in dic. wordPlay.Score = board.CanBeFormed(wordPlay.Word) ? GetScore(wordPlay.Word) : -1; if (wordPlay.Word.Length < 3) { wordPlay.Score = 0; } else { foreach (Words word in p.WordsPlayed) { if (word.Word.ToUpper().Equals(wordPlay.Word.ToUpper())) { wordPlay.Score = 0; } } } // Set the appropriate score for each word WordScore scoreWord = new WordScore(); scoreWord.Score = wordPlay.Score; // Update the players' scores p.Score += wordPlay.Score; p.WordsPlayed.Add(wordPlay); SetStatus(OK); return(scoreWord); } } } }
/// Play a word in a game. /// If Word is null or empty or longer than 30 characters when trimmed, or if GameID or UserToken is invalid, or if UserToken is not a player in the /// game identified by GameID, responds with response code 403 (Forbidden). /// Otherwise, if the game state is anything other than "active", responds with response code 409 (Conflict). /// 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. public WordScore PlayWord(WordToPlay w, string gameID) { int timeLeft; if (w.Word == null || w.Word.Equals("") || w.Word.Trim().Length > 30 || w.UserToken == null || !tryGetPlayer(w.UserToken, out Player p) || gameID == null || !int.TryParse(gameID, out int GameID) || !((timeLeft = getTimeLeft(GameID)) is int) || !tryGetGame(GameID, out GameStatus g) || (!w.UserToken.Equals(g.Player1.UserToken) && !w.UserToken.Equals(g.Player2.UserToken))) { SetStatus(Forbidden); return(null); } else { if (!g.GameState.Equals("active")) { SetStatus(Conflict); return(null); } else { Words wordPlay = new Words(); wordPlay.Word = w.Word; // Generate boggle board BoggleBoard board = new BoggleBoard(g.Board); // Score 0 if the word is less than 3 characters or -1 if it doesn't exist in dic. wordPlay.Score = board.CanBeFormed(wordPlay.Word) ? GetScore(wordPlay.Word) : -1; if (wordPlay.Word.Length < 3) { wordPlay.Score = 0; } else { foreach (Words word in getWordsPlayed(w.UserToken, GameID)) { if (word.Word.ToUpper().Equals(wordPlay.Word.ToUpper())) { wordPlay.Score = 0; } } } // Set the appropriate score for each word WordScore scoreWord = new WordScore(); scoreWord.Score = wordPlay.Score; // Update the players' played words addWordToList(w, GameID, scoreWord.Score); SetStatus(OK); return(scoreWord); } } }
/// <summary> /// Determines how to recieve the incoming request and what type of object needs to be created. /// </summary> /// <param name="ss"></param> /// <param name="contentLength"></param> /// <param name="methodName"></param> private string GetObject(String s) { switch (methodName) { // Used for creating a user. case "CreateUser": UserInfo user = JsonConvert.DeserializeObject <UserInfo>(s); UserTokenObject token = service.CreateUser(user); string result = JsonConvert.SerializeObject(token, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(result); // Used for joing a game. case "JoinGame": JoinGameInfo info = JsonConvert.DeserializeObject <JoinGameInfo>(s); GameiD id = service.JoinGame(info); string gID = JsonConvert.SerializeObject(id, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(gID); // Used for cancelling a join request. case "CancelJoinRequest": Cancel cancel = JsonConvert.DeserializeObject <Cancel>(s); service.CancelJoinRequest(cancel); return(""); // Used for playing a word case "PlayWord": WordCheck word = JsonConvert.DeserializeObject <WordCheck>(s); WordScore score = service.PlayWord(gameID, word); string wordPlayed = JsonConvert.SerializeObject(score, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); return(wordPlayed); default: return(""); } }
/// <summary> /// Takes the word submitted by the client and scores it for the client, returning it to them. /// </summary> /// <param name="words">class that containes the userToken and word that the client is submitting</param> /// <param name="GameID">ID of the game that the word is being submitted for</param> /// <returns></returns> public TokenScoreGameIDReturn playWord(UserGame words, string GameID) { lock (sync) { if (words.UserToken == null || words.UserToken.Trim().Length == 0 || !AllPlayers.ContainsKey(words.UserToken)) { SetStatus(Forbidden); return(null); } if (words.Word == null || words.Word.Trim().Length == 0 || !AllGames.ContainsKey(GameID)) { SetStatus(Forbidden); return(null); } if (AllGames[GameID].GameState != "active") { SetStatus(Conflict); return(null); } if (AllPlayers[words.UserToken].personalList == null) { AllPlayers[words.UserToken].personalList = new List <WordScore>(); } int userScore; int.TryParse(AllPlayers[words.UserToken].Score, out userScore); int WordScoreResult = ScoreWord(words.Word, words.UserToken, GameID); WordScore totalResult = new WordScore(); totalResult.Word = words.Word; totalResult.Score = WordScoreResult; AllPlayers[words.UserToken].personalList.Add(totalResult); AllPlayers[words.UserToken].Score = (userScore + WordScoreResult).ToString(); TokenScoreGameIDReturn var = new TokenScoreGameIDReturn(); var.Score = WordScoreResult.ToString(); SetStatus(OK); return(var); } }
/// <summary> /// server response for for play word /// </summary> /// <param name="body"></param> /// <param name="gameid"></param> private void PlayWord(string body, string gameid) { HttpStatusCode httpStatus; PlayedWord word = JsonConvert.DeserializeObject <PlayedWord>(body); WordScore score = service.playWord(gameid, word, out httpStatus); string res = ("HTTP/1.1 " + (int)httpStatus + " " + httpStatus + "\r\n"); if ((int)httpStatus / 100 == 2) { string result = JsonConvert.SerializeObject(score); res += ("Content-Type: application/json\r\n"); res += ("Content-Length: " + Encoding.UTF8.GetByteCount(result) + "\r\n"); res += "\r\n"; res += result; } else { res += "\r\n"; } socket.BeginSend(res, (x, y) => { socket.Shutdown(SocketShutdown.Both); }, null); }
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 given the GameID and the word object /// </summary> /// <param name="GameID"></param> /// <param name="word"></param> /// <returns></returns> public WordScore playWord(string GameID, PlayedWord word, out HttpStatusCode httpStatus) { httpStatus = OK; Words newWord = new Words(); WordScore pw = new WordScore(); GameInfo pg = new GameInfo(); GameInfo g = new GameInfo(); if (String.IsNullOrEmpty(word.Word) || String.IsNullOrEmpty(GameID) || word.Word.Trim().Length > 30) { httpStatus = Forbidden; pw.Score = -1; return(pw); } try { pendingGames.TryGetValue(GameID.ToString(), out pg); if (pg.GameState.Equals("pending")) { httpStatus = Conflict; pw.Score = -1; return(pw); } } catch (Exception) { } games.TryGetValue(GameID.ToString(), out g); if (!g.GameState.Equals("active")) { httpStatus = Conflict; pw.Score = -1; return(pw); } if (getTimeLeft(g) <= 0) { httpStatus = Conflict; pw.Score = 0; return(pw); } if (!word.UserToken.Equals(g.Player1.UserToken.ToString()) && !word.UserToken.Equals(g.Player2.UserToken.ToString())) { httpStatus = Forbidden; pw.Score = 0; return(pw); } if (new BoggleBoard(g.Board).CanBeFormed(word.Word.Trim())) { if (word.UserToken.Equals(g.Player1.UserToken)) { foreach (var w in g.Player1.WordsPlayed) { if (w.Word.Equals(word.Word.Trim())) { newWord.Score = 0; newWord.Word = word.Word.Trim(); g.Player1.WordsPlayed.Add(newWord); httpStatus = OK; pw.Score = newWord.Score; return(pw); } } if (WordValid(word.Word)) { newWord.Score = ScoreWord(word.Word); newWord.Word = word.Word.Trim(); g.Player1.WordsPlayed.Add(newWord); g.Player1.Score += newWord.Score; httpStatus = OK; pw.Score = newWord.Score; return(pw); } } else { foreach (var w in g.Player2.WordsPlayed) { if (w.Word.Equals(word.Word.Trim())) { newWord.Score = 0; newWord.Word = word.Word.Trim(); g.Player2.WordsPlayed.Add(newWord); httpStatus = OK; pw.Score = newWord.Score; return(pw); } } if (WordValid(word.Word)) { newWord.Score = ScoreWord(word.Word); newWord.Word = word.Word.Trim(); g.Player2.WordsPlayed.Add(newWord); g.Player2.Score += newWord.Score; httpStatus = OK; pw.Score = newWord.Score; return(pw); } } } else { if (word.UserToken.Equals(g.Player1.UserToken)) { newWord.Score = ScoreWord(word.Word); newWord.Word = word.Word.Trim(); g.Player1.WordsPlayed.Add(newWord); g.Player1.Score += newWord.Score; httpStatus = OK; pw.Score = newWord.Score; return(pw); } else { newWord.Score = ScoreWord(word.Word); newWord.Word = word.Word.Trim(); g.Player2.WordsPlayed.Add(newWord); g.Player2.Score += newWord.Score; httpStatus = OK; pw.Score = newWord.Score; return(pw); } } return(pw); }