Beispiel #1
0
        /// <summary>
        /// Callback for web server - builds webpages based off of queries received from the browser
        /// </summary>
        private void CreateWebPage(Preserved_State_Object state)
        {
            string query  = Regex.Split(state.data.ToString(), "\r\n")[0];
            string score  = @"GET /scores";
            string games  = @"GET /games\?player=";
            string eaten  = @"GET /eaten\?id=";
            string ending = @" HTTP/1.1";

            if (Regex.IsMatch(query, score))
            {
                /* Upon receiving "GET /scores HTTP/1.1" the server will send back an HTML web page containing a table of information reporting all recorded scores.
                 * This should include, the length of time alive, the maximum mass, the highest rank, and the number of cubes eaten.
                 * The HTML table should have one row for each player/game in the database and one column for each of the above required pieces of information.
                 * A superior solution would create links in this table to the information described below (e.g., clicking on a players name would invoke case 2 below).*/

                string dbQuery = "Select * from Players";

                Network.Send(state.socket, StatsHTML(new MySqlCommand(dbQuery), 1, "AgCubio Stats | High Scores"), true);
            }
            else if (Regex.IsMatch(query, games))
            {
                /*Upon receiving "GET /games?player=Joe HTTP/1.1" the server will send back an HTML web page containing a table of information reporting all games by the player "Joe".
                 * There should be one row for each game played by the player named in the line of text (e.g., "Joe" in the example above) and a column for each piece of information.
                 * In addition to the above information, the time of death should be shown and the number of players eaten should be shown.
                 * A superior solution would also have links to the main score table page and to the list of eaten players for a particular game.*/

                query = Regex.Replace(query, "(" + games + ")|(" + ending + ")", ""); // Get the player name

                string dbQuery = "Select * from Players where name = '" + query + "'";

                Network.Send(state.socket, StatsHTML(new MySqlCommand(dbQuery), 2, "AgCubio Stats | Player: " + query), true);
            }
            else if (Regex.IsMatch(query, eaten))
            {
                /*Upon receiving "GET /eaten?id=35 HTTP/1.1" the server should send back an HTML page containing information about the specified game session (e.g., 35 in this example).
                 * The page should contain all information about the players game, but most importantly highlight the names of players who were eaten. A superior solution would turn "eaten player" names into links to their high scores.
                 * If there the specified game does not exist, treat this as an "anything else" case as discussed below. As always, a superior solution will have links from this page to other related pages.*/

                query = Regex.Replace(query, "(" + eaten + ")|(" + ending + ")", ""); // Get the game id

                string dbQuery = "Select * from Eaten natural join Players where GameId = " + query;

                Network.Send(state.socket, StatsHTML(new MySqlCommand(dbQuery), 3, "AgCubio Stats | Game ID: " + query), true);
            }
            else
            {
                // Show an error page if the first line of text the browser sends the server is invalid
                Network.Send(state.socket, HTMLGenerator.GenerateError("Invalid web address"), true);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Queries the database and returns a large string of html for the webpage, based on the query
        /// </summary>
        private string StatsHTML(MySqlCommand query, int queryNum = 0, string title = "")
        {
            string html = "";

            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                try
                {
                    // Open a connection
                    conn.Open();
                    query.Connection = conn;
                    string body;

                    // Execute the command and cycle through the DataReader object
                    using (MySqlDataReader reader = query.ExecuteReader())
                    {
                        string        table;
                        StringBuilder rows = new StringBuilder();

                        if (queryNum == 1) // High Scores web page
                        {
                            rows.Append(HTMLGenerator.TableRow(HTMLGenerator.TableHData("Game Id") +
                                                               HTMLGenerator.TableHData("Name") +
                                                               HTMLGenerator.TableHData("Lifetime") +
                                                               HTMLGenerator.TableHData("Max Mass") +
                                                               HTMLGenerator.TableHData("Highest Rank") +
                                                               HTMLGenerator.TableHData("Number of Cubes Eaten")));

                            while (reader.Read())
                            {
                                string id          = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/eaten?id=" + reader["GameId"].ToString(), reader["GameId"].ToString()));
                                string name        = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/games?player=" + reader["Name"].ToString(), reader["Name"].ToString()));
                                string lifetime    = HTMLGenerator.TableData(reader["Lifetime"].ToString());
                                string maxmass     = HTMLGenerator.TableData(reader["MaxMass"].ToString());
                                string highestRank = HTMLGenerator.TableData(reader["HighestRank"].ToString());
                                string cubesEaten  = HTMLGenerator.TableData(reader["CubesEaten"].ToString());

                                rows.Append(HTMLGenerator.TableRow(id + name + lifetime + maxmass + highestRank + cubesEaten));
                            }
                        }
                        else if (queryNum == 2) // Game Id Stats web page
                        {
                            rows.Append(HTMLGenerator.TableRow(HTMLGenerator.TableHData("GameId") +
                                                               HTMLGenerator.TableHData("Name") +
                                                               HTMLGenerator.TableHData("Lifetime") +
                                                               HTMLGenerator.TableHData("Max Mass") +
                                                               HTMLGenerator.TableHData("Highest Rank") +
                                                               HTMLGenerator.TableHData("Number of Cubes Eaten") +
                                                               HTMLGenerator.TableHData("Time Of Death") +
                                                               HTMLGenerator.TableHData("Number of Players Eaten")));

                            while (reader.Read())
                            {
                                string id            = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/eaten?id=" + reader["GameId"].ToString(), reader["GameId"].ToString()));
                                string name          = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/games?player=" + reader["Name"].ToString(), reader["Name"].ToString()));
                                string lifetime      = HTMLGenerator.TableData(reader["Lifetime"].ToString());
                                string maxmass       = HTMLGenerator.TableData(reader["MaxMass"].ToString());
                                string highestRank   = HTMLGenerator.TableData(reader["HighestRank"].ToString());
                                string cubesEaten    = HTMLGenerator.TableData(reader["CubesEaten"].ToString());
                                string timeofdeath   = HTMLGenerator.TableData(reader["TimeofDeath"].ToString());
                                string numplayerseat = HTMLGenerator.TableData(reader["NumPlayersEaten"].ToString());

                                rows.Append(HTMLGenerator.TableRow(id + name + lifetime + maxmass + highestRank + cubesEaten + timeofdeath + numplayerseat));
                            }
                        }
                        else if (queryNum == 3) // Player Name Stats web page
                        {
                            rows.Append(HTMLGenerator.TableRow(HTMLGenerator.TableHData("GameId") +
                                                               HTMLGenerator.TableHData("Name") +
                                                               HTMLGenerator.TableHData("Lifetime") +
                                                               HTMLGenerator.TableHData("Max Mass") +
                                                               HTMLGenerator.TableHData("Highest Rank") +
                                                               HTMLGenerator.TableHData("Number of Cubes Eaten") +
                                                               HTMLGenerator.TableHData("Time Of Death") +
                                                               HTMLGenerator.TableHData("Number of Players Eaten") +
                                                               HTMLGenerator.TableHData("Eaten Player Names")));

                            int           add    = 0;
                            StringBuilder eaten  = new StringBuilder();
                            string        others = "";

                            while (reader.Read())
                            {
                                string id            = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/eaten?id=" + reader["GameId"].ToString(), reader["GameId"].ToString()));
                                string name          = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/games?player=" + reader["Name"].ToString(), reader["Name"].ToString()));
                                string lifetime      = HTMLGenerator.TableData(reader["Lifetime"].ToString());
                                string maxmass       = HTMLGenerator.TableData(reader["MaxMass"].ToString());
                                string highestRank   = HTMLGenerator.TableData(reader["HighestRank"].ToString());
                                string cubesEaten    = HTMLGenerator.TableData(reader["CubesEaten"].ToString());
                                string timeofdeath   = HTMLGenerator.TableData(reader["TimeofDeath"].ToString());
                                string numplayerseat = HTMLGenerator.TableData(reader["NumPlayersEaten"].ToString());
                                string eatenname     = HTMLGenerator.TableData(HTMLGenerator.GenerateLink("http://localhost:11100/games?player=" + reader["EatenPlayer"].ToString(), reader["EatenPlayer"].ToString()));
                                eaten.Append("<p>" + HTMLGenerator.GenerateLink("http://localhost:11100/games?player=" + reader["EatenPlayer"].ToString(), reader["EatenPlayer"].ToString()) + "</p>");
                                if (add < 1)
                                {
                                    others = id + name + lifetime + maxmass + highestRank + cubesEaten + timeofdeath + numplayerseat;
                                }
                                add++;
                            }

                            others += HTMLGenerator.TableData(eaten.ToString());
                            rows.Append(HTMLGenerator.TableRow(others));
                        }

                        // Assemble all the html together
                        table = HTMLGenerator.Table(rows.ToString());
                        body  = HTMLGenerator.Body(table, title);
                        html  = HTMLGenerator.GenerateWebpage(title, body);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    html = HTMLGenerator.GenerateError("Something went wrong in the database query");
                }
            }

            return(html);
        }