/// <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> /// Registers the client to a userToken. Creates them in AllPlayer as well. /// </summary> /// <param name="user">Class that contains the nickname they want to be known as</param> /// <returns></returns> public TokenScoreGameIDReturn RegisterUser(UserInfo user) { lock (sync) { if (user.Nickname == null || user.Nickname.Trim().Length == 0) { SetStatus(Forbidden); return(null); } else { SetStatus(Created); string userID = Guid.NewGuid().ToString(); AllPlayers.Add(userID, user); AllPlayers[userID].UserToken = userID; AllPlayers[userID].Nickname = user.Nickname; TokenScoreGameIDReturn result = new TokenScoreGameIDReturn(); result.UserToken = userID; return(result); } } }
/// <summary> /// Allows the client to join a game, or create one if needed /// </summary> /// <param name="info">Class that contains the UserToken and TimeLimit</param> /// <returns></returns> public TokenScoreGameIDReturn JoinGame(GameJoin info) { lock (sync){ if (info.UserToken == null || info.UserToken.Trim().Length == 0 || !AllPlayers.ContainsKey(info.UserToken)) { SetStatus(Forbidden); return(null); } int test; if (!int.TryParse(info.TimeLimit, out test)) { SetStatus(Forbidden); return(null); } if (test < 5 || test > 120) { SetStatus(Forbidden); return(null); } foreach (KeyValuePair <string, GameStatus> game in AllGames) { if (game.Value.GameState == "pending") { if (game.Value.Player1.UserToken == info.UserToken) { SetStatus(Conflict); return(null); } } } TokenScoreGameIDReturn var = new TokenScoreGameIDReturn(); foreach (KeyValuePair <string, GameStatus> game in AllGames) { if (game.Value.GameState == "pending") { if (game.Value.Player1 != null) { game.Value.Player2 = AllPlayers[info.UserToken]; SetStatus(Created); setupGame(info.TimeLimit, game.Key); var.GameID = game.Key; return(var); } else { game.Value.Player1 = AllPlayers[info.UserToken]; SetStatus(Accepted); var.GameID = game.Key; return(var); } } } gameID += 1; SetStatus(Accepted); AllGames.Add(gameID.ToString(), new GameStatus()); SetStatus(Accepted); AllGames[gameID.ToString()].Player1 = AllPlayers[info.UserToken]; AllGames[gameID.ToString()].GameState = "pending"; AllGames[gameID.ToString()].TimeLimit = info.TimeLimit; var.GameID = gameID.ToString(); return(var); } }