/// <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}");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This handles the second phase of the connection protocol. After the player's
        /// Circle has been received, the rest should be all the other
        /// game objects (food, other players, heartbeats).
        /// </summary>
        /// <param name="obj"></param>
        private void Get_World_Information(Preserved_Socket_State obj)
        {
            try
            {
                Calculate_Movement(out movement_X, out movement_Y);               //Calculates the player's movement based on the mouse's position.
                Networking.Send(obj.socket, $"(move,{movement_X},{movement_Y})"); //Sends the movement to the Server.
                logger.LogDebug($"Sent movement: {movement_X} {movement_Y}");

                world_circle = JsonConvert.DeserializeObject <Circle>(obj.Message);

                if (world_circle.Type.ToString().Equals("heartbeat"))
                {
                    this.Invalidate();
                }

                lock (game_world)
                {
                    if (!game_world.Contains(world_circle.ID))
                    {
                        game_world.Add(world_circle.ID, world_circle);
                    }
                    else
                    {
                        if (!world_circle.Location.Equals(game_world[world_circle.ID])) //If the player location has changed from what's already stored
                        {
                            game_world.Remove(world_circle.ID);                         //Remove the old entry
                            game_world.Add(world_circle.ID, world_circle);              //And add in the new one (which is the same "object" via the ID, but in a different location)
                        }
                        if (world_circle.GetMass <= 0 && world_circle.Type.ToString().Equals("food"))
                        {
                            game_world.Remove(world_circle.ID); //Remove the old entry
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogError($"Json convert error.");
            }
            try
            {
                Networking.await_more_data(obj);
            }
            catch (SocketException e)
            {
                // If a player has died, stop drawing the game scene and display game over screen
                logger.LogInformation($"{player_circle.GetName} has been killed.");
                connected = false;
                isDead    = true;
                game_timer.Stop();
                elapsed_time = game_timer.ElapsedMilliseconds;

                // We are converting the time in milliseconds into a string of hour:min:seconds format to be added into the database
                TimeSpan converted_time = TimeSpan.FromMilliseconds(elapsed_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);
                database.Insert_Player_Data(player_name, player_circle.GetMass, rank + 1, time_played);
                database.Insert_HighScore_Data(player_name, player_circle.GetMass, rank + 1, time_played);
            }

            // If the user wishes to split, send the coordinates for split
            if (can_split)
            {
                float destination_X = player_circle.Location.X + 100;
                float destination_Y = player_circle.Location.Y + 100;
                can_split = false;
                Networking.Send(obj.socket, $"split,{destination_X},{destination_Y}");
            }

            obj.on_data_received_handler = Get_World_Information;
        }