/// <summary>
        /// Helper method to create a dataset to store the table information
        /// from the TimeInFirst SQL table and builds a portion of the http
        /// body message with the gathered
        /// information
        /// </summary>
        /// <returns>The http message portion containing a table of the information</returns>
        private static string Time_In_First_Table_Info()
        {
            AgarioDatabase database      = new AgarioDatabase();
            DataSet        time_in_first = database.Get_First_Times();

            return(Build_Table_Data(time_in_first, "TimeInFirst"));
        }
        /// <summary>
        /// Helper method to create a dataset to store the table information
        /// from the specified player's SQL table and builds a portion of
        /// the http body message with the gathered information
        /// </summary>
        /// <returns>The http message portion containing a table of the information</returns>
        private static string Player_Table_Info(string player_name)
        {
            AgarioDatabase database         = new AgarioDatabase();
            DataSet        player_score_set = database.Get_Player_Data(player_name);

            return(Build_Table_Data(player_score_set, $"{player_name}Data"));
        }
        /// <summary>
        /// Helper method to create a dataset to store the table information
        /// from the High Scores SQL table and builds a portion of
        /// the http body message with the gathered information
        /// </summary>
        /// <returns>The http message portion containing a table of the information</returns>
        private static string High_Score_Table_Info()
        {
            AgarioDatabase database       = new AgarioDatabase();
            DataSet        high_score_set = database.Get_HighScores();

            return(Build_Table_Data(high_score_set, "HighScores"));
        }
示例#4
0
        public Client_and_GUI(ILogger logger)
        {
            rank         = 0;
            game_timer   = new Stopwatch();
            elapsed_time = 0;
            database     = new Database.AgarioDatabase();
            this.logger  = logger;
            game_world   = new World(logger);

            InitializeComponent();
        }
        /// <summary>
        ///   When a request comes in (from a browser) this method will
        ///   be called by the Networking code.  When a full message has been
        ///   read (as defined by an empty line in the overall message) send
        ///   a response based on the request.
        /// </summary>
        /// <param name="network_message_state"> provided by the Networking code, contains socket and message</param>
        private static void RequestFromBrowserHandler(Preserved_Socket_State network_message_state)
        {
            Console.WriteLine($"{++counter,4}: {network_message_state.Message}");
            try
            {
                if (network_message_state.Message.Equals("GET / HTTP/1.1\r"))
                {
                    string main_page = Build_Main_Page();
                    Send_And_Close_Connection(network_message_state.socket, main_page);
                }
                else if (network_message_state.Message.Equals("GET /highscores HTTP/1.1\r"))
                {
                    string high_scores = Build_HighScore_Page();
                    Send_And_Close_Connection(network_message_state.socket, high_scores);
                }
                else if (network_message_state.Message.Equals("GET /timeinfirst HTTP/1.1\r"))
                {
                    string first_place_length = Build_First_Place_Length_Page();
                    Send_And_Close_Connection(network_message_state.socket, first_place_length);
                }
                else if (network_message_state.Message.Contains("scores"))
                {
                    string   split_character = "/";
                    string[] split_message   = Regex.Split(network_message_state.Message, split_character);

                    if (split_message.Length == 4)
                    {
                        // grabs the player name from the network message, minus " HTTP"
                        string name = split_message[2].Substring(0, split_message[2].Length - 5);
                        string sent_player_database = Build_Player_Page(name);
                        Send_And_Close_Connection(network_message_state.socket, sent_player_database);
                    }
                    else if (split_message.Length == 8)
                    {
                        float  mass;
                        int    rank;
                        long   start_time;
                        long   end_time;
                        string name = split_message[2];

                        float.TryParse(split_message[3], out mass);
                        int.TryParse(split_message[4], out rank);
                        long.TryParse(split_message[5], out start_time);
                        long.TryParse(split_message[6].Substring(0, split_message[6].Length - 5), out end_time);
                        long total_time = end_time - start_time;

                        TimeSpan converted_time = TimeSpan.FromMilliseconds(total_time);
                        string   time_played    = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", converted_time.Hours, converted_time.Minutes, converted_time.Seconds, converted_time.Milliseconds);

                        // Insert networking message into sql table
                        AgarioDatabase database = new AgarioDatabase();
                        database.Insert_Player_Data(name, mass, rank, time_played);
                        database.Insert_HighScore_Data(name, mass, rank, time_played);

                        string sent_player_database = Build_Confirmation_Page(name);
                        Send_And_Close_Connection(network_message_state.socket, sent_player_database);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine($"Something went wrong... this is a bad error message. {exception}");
            }
        }