/// <summary> /// Creates the HTML string that will be used to get the tables for all games /// </summary> /// <returns></returns> public string allGamesTableCreator() { // Connect to the DB using (MySqlConnection conn = new MySqlConnection(connectionString)) { try { // Open a connection conn.Open(); MySqlCommand command = conn.CreateCommand(); command.CommandText = "Select GameID, Duration From Games"; //Dictionary used to hold all games that have been played Dictionary <uint, GameModel> games = new Dictionary <uint, GameModel>(); // Execute the command and cycle through the DataReader object using (MySqlDataReader reader = command.ExecuteReader()) { //Create a gameModel out of the current game and add it to the games dictionary while (reader.Read()) { uint currGameID = uint.Parse(reader["GameID"].ToString()); GameModel currentGame = new GameModel(currGameID, uint.Parse(reader["Duration"].ToString())); games.Add(currGameID, currentGame); } } command.CommandText = "Select PlayerID, Game, Score, Accuracy From Players"; // Execute the command and cycle through the DataReader object using (MySqlDataReader reader = command.ExecuteReader()) { //Add every player to the game that they played while (reader.Read()) { String playerID = reader["PlayerID"].ToString(); uint score = uint.Parse(reader["Score"].ToString()); uint accuracy = uint.Parse(reader["Accuracy"].ToString()); PlayerModel newPlayer = new PlayerModel(playerID, score, accuracy); games[uint.Parse(reader["Game"].ToString())].AddPlayer(playerID, score, accuracy); } } return(WebViews.GetAllGames(games)); } catch (Exception e) { return("Error: " + e.Message); } } }
/// <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()); } }