/// <summary> /// This handles the HTTP request and makes the web page depending on the request /// </summary> /// <param name="ss">Socket state for the connection</param> public static void ServeHttpRequest(SocketState ss) { if (ss.ErrorOccured == true) { Console.WriteLine("Error occured while accepting: \"" + ss.ErrorMessage + "\""); return; } string request = ss.GetData(); Console.WriteLine(request); //Player request if (request.Contains("GET /games?player=")) { //Finds the player name with substring int start = request.IndexOf("=") + 1; int length = request.IndexOf(" HTTP/1.1") - start; string name = request.Substring(start, length); //Gets all of the players in the form of a dictionary Dictionary <uint, PlayerModel> playersDictionary = DatabaseController.GetAllPlayerGames(name); //Creates list of sessions that the player has been in by getting the game durations from the database List <SessionModel> SessionList = new List <SessionModel>(); foreach (KeyValuePair <uint, PlayerModel> player in playersDictionary) { SessionList.Add(new SessionModel(player.Key, DatabaseController.GetGameDuration(player.Key), player.Value.Score, player.Value.Accuracy)); } //Sends the list so it can be formatted into a table Networking.SendAndClose(ss.TheSocket, WebViews.GetPlayerGames(name, SessionList)); } //Games request else if (request.Contains("GET /games HTTP/1.1")) { //Creates a table with each of the games and all of their data Networking.SendAndClose(ss.TheSocket, WebViews.GetAllGames(DatabaseController.GetAllGames())); } //If there aren't any slashes it goes to the home page else if (request.Contains("GET / HTTP/1.1")) { Networking.SendAndClose(ss.TheSocket, WebViews.GetHomePage(0)); } //Otherwise it throws a 404 error else { Networking.SendAndClose(ss.TheSocket, WebViews.Get404()); } }
/// <summary> /// Creates the HTML string that will be used to get the table for any individual /// player /// </summary> /// <param name="name">The name of the player</param> /// <returns></returns> public string individualTableCreator(String name) { // Connect to the DB using (MySqlConnection conn = new MySqlConnection(connectionString)) { try { // Open a connection conn.Open(); MySqlCommand command = conn.CreateCommand(); command.CommandText = "Select PlayerID, Game, Score, Accuracy From Players"; //Lists used to hold information about the players List <SessionModel> games = new List <SessionModel>(); List <uint> currGameIDs = new List <uint>(); List <uint> currGameDurs = new List <uint>(); List <uint> currGameScore = new List <uint>(); List <uint> currGameAccuracy = new List <uint>(); // Execute the command and cycle through the DataReader object using (MySqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { //Add all the player data to its aplicable table if (reader["PlayerID"].Equals(name)) { currGameIDs.Add(uint.Parse(reader["Game"].ToString())); currGameScore.Add(uint.Parse(reader["Score"].ToString())); currGameAccuracy.Add(uint.Parse(reader["Accuracy"].ToString())); } } } //If the player hasn't played any games, the url is invalid if (currGameIDs.Count == 0) { return(errorMessage()); } //Get the duration for each game the player has played foreach (uint i in currGameIDs) { command.CommandText = "Select Duration From Games Where GameID=" + "'" + i + "'" + ""; currGameDurs.Add(uint.Parse(command.ExecuteScalar().ToString())); } //Turn each game the player has played into a session model //Add those to the games list for (int i = 0; i < currGameIDs.Count; i++) { SessionModel newGame = new SessionModel(currGameIDs[i], currGameDurs[i], currGameScore[i], currGameAccuracy[i]); games.Add(newGame); } return(WebViews.GetPlayerGames(name, games)); } catch (Exception e) { return("Error: " + e.Message); } } }