/// <summary> /// Takes a valid game ID and process Boggle game logic and returns a score based on word evaluation. /// </summary> /// <param name="games"></param> /// <param name="GameID"></param> /// <returns></returns> public PlayResponse PlayWord(PlayRequest games, string GamedID, out HttpStatusCode status) { string word = games.Word; string trimmed; // Various types of condition checks for request object, played word and game ID if (word == null || (trimmed = word.Trim()).Length == 0 || games.UserToken == null) { status = Forbidden; return(null); } if (!Int32.TryParse(GamedID, out int id)) { status = Forbidden; return(null); } PlayResponse response = new PlayResponse(); using (SqlConnection conn = new SqlConnection(BoggleDB)) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { if (!IsUserTokenValid(games.UserToken, conn, trans) || id < 1 || id > GetRecentGameID(conn, trans) || !IsPlayerInGame(games.UserToken, conn, trans, id)) { status = Forbidden; return(null); } if (ComputeTimeLeft(id, conn, trans) < 1) { status = Conflict; return(null); } // If everything is fine at this point, insert the played word into DB using (SqlCommand comm = new SqlCommand("INSERT INTO Words (Word, Player, GameID, Score) VALUES (@Word, @Player, @GameID, @Score)", conn, trans)) { BoggleBoard board = FetchBoard(id, conn, trans); List <WordsPlayed> playedList = FetchWords(games.UserToken, id, conn, trans); // Compute score with gameboard and played word list int score = ComputeScore(trimmed, board, playedList); comm.Parameters.AddWithValue("@Word", trimmed); comm.Parameters.AddWithValue("@Player", games.UserToken); comm.Parameters.AddWithValue("@GameID", id); comm.Parameters.AddWithValue("@Score", score); // Compose response message response.Score = score; comm.ExecuteNonQuery(); } trans.Commit(); } } status = OK; return(response); }
/// <summary> /// Handles the request after all relevant info has been parsed out /// </summary> /// <param name="line"></param> /// <param name="p"></param> private void ProcessRequest(string line, object p = null) { String result = ""; // this handles 'make user' request if (makeUserPattern.IsMatch(firstLine)) { UserNickames name = JsonConvert.DeserializeObject <UserNickames>(line); dynamic user = new BoggleService().Register(name, out HttpStatusCode status); result = ComposeResponse(user, status); } // this handles 'join game' request else if (joinGamePattern.IsMatch(firstLine)) { GameRequest request = JsonConvert.DeserializeObject <GameRequest>(line); JoinResponse response = new BoggleService().Join(request, out HttpStatusCode status); result = ComposeResponse(response, status); } // this handles 'update game status' with brief parameter on or off else if (updateNoBriefPattern.IsMatch(firstLine) || updateBriefPattern.IsMatch(firstLine)) { Match m; string gameID = ""; string briefParam = null; string brief = ""; // if brief parameter is not provided if (updateNoBriefPattern.Match(firstLine).Success) { m = updateNoBriefPattern.Match(firstLine); gameID = m.Groups[1].ToString(); } // or not provided, parse brief parameter if (updateBriefPattern.Match(firstLine).Success) { m = updateBriefPattern.Match(firstLine); // game ID is embedded in first group of regex pattern gameID = m.Groups[1].ToString(); // brief parameter is embedded in second group of regex pattern briefParam = m.Groups[2].ToString(); // remove irrelevent strings brief = briefParam.Substring(7); } GameStatus response; HttpStatusCode status; if (briefParam == null) { response = new BoggleService().Update(gameID, "No", out status); } else { response = new BoggleService().Update(gameID, brief, out status); } result = ComposeResponse(response, status); } // this handles playword request else if (playWordPattern.IsMatch(firstLine)) { string gameID = playWordPattern.Match(firstLine).Groups[1].ToString(); PlayRequest request = JsonConvert.DeserializeObject <PlayRequest>(line); PlayResponse response = new BoggleService().PlayWord(request, gameID, out HttpStatusCode status); result = ComposeResponse(response, status); } // this handles cancel game request else if (cancelPattern.IsMatch(firstLine)) { UserObject user = JsonConvert.DeserializeObject <UserObject>(line); new BoggleService().CancelJoinRequest(user, out HttpStatusCode status); result = ComposeResponse(null, status); } // capturing whatever string requests that does not match any of the above regex patterns else { result = "HTTP/1.1 " + "403" + " Forbidden" + "\r\n\r\n"; } socket.BeginSend(result, (x, y) => { socket.Shutdown(SocketShutdown.Both); }, null); }