Ejemplo n.º 1
0
        /// <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());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Second callback method for after the server has made a connection
        /// </summary>
        /// <param name="state"></param>
        private static void ServeHttpRequest(SocketState state)
        {
            string request = state.GetData();

            //If the request is for an individual player, create their table
            if (request.Contains("GET /games?player="))
            {
                String playerNameRequest = request.Substring(21, request.IndexOf("\n") - 34);
                String dataToSend        = mainDataControl.individualTableCreator(playerNameRequest);
                Networking.SendAndClose(state.TheSocket, dataToSend);
            }
            //If the request is for all games, generate all the tables
            else if (request.Contains("GET /games"))
            {
                String dataToSend = mainDataControl.allGamesTableCreator();
                Networking.SendAndClose(state.TheSocket, dataToSend);
            }
            //If the request is for anything else, send them to the homepage
            else
            {
                Networking.SendAndClose(state.TheSocket, mainDataControl.sendToHomePage());
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Detects the incoming messages of new players when they connect then calls Get Data in order to create an event loop to continue listening for
        /// new incoming players
        /// </summary>
        /// <param name="state">Connection of incoming player.</param>
        private void ProcessMessage(SocketState state)
        {
            string data;
            long   ID = state.ID;

            string[] movingCommands;

            if (state.ErrorOccured)
            {
                PlayerDisconnect(state);
                Networking.SendAndClose(state.TheSocket, "error");
                //Networking.GetData(state);
                return;
            }

            data = state.GetData();
            state.ClearData();
            if (String.IsNullOrEmpty(data))
            {
                Networking.GetData(state);
                return;
            }


            //checks to see if user is sending a user command or name
            if (data.StartsWith("{\"moving\""))
            {
                movingCommands = data.Split(',');
                foreach (string s in movingCommands)
                {
                    string[] command = s.Split(':');
                    foreach (string t in command)
                    {
                        string tempString      = t.Replace("\"", String.Empty);
                        string tempString2     = tempString.Replace("{", String.Empty);
                        string stringToCompare = tempString2.Replace("}", String.Empty);
                        switch (stringToCompare)
                        {
                        case "none":
                            break;

                        case "up":
                            MoveTank(new Vector2D(0, playerSpeed * -1), new Vector2D(0, -1), players[ID]);
                            break;

                        case "down":
                            MoveTank(new Vector2D(0, playerSpeed), new Vector2D(0, 1), players[ID]);
                            break;

                        case "left":
                            MoveTank(new Vector2D(playerSpeed * -1, 0), new Vector2D(-1, 0), players[ID]);
                            break;

                        case "right":
                            MoveTank(new Vector2D(playerSpeed, 0), new Vector2D(1, 0), players[ID]);
                            break;

                        case "main":
                            FireProjectile(players[ID]);
                            break;

                        case "alt":
                            FireBeam(players[ID]);
                            break;

                        case "x":
                            if (double.TryParse(command[2], out double anglex))
                            {
                                turretAimx = anglex;
                            }
                            break;

                        case "y":
                            if (command[1].Length > 4)
                            {
                                if (double.TryParse(command[1].Substring(0, command[1].Length - 5), out double angley))
                                {
                                    turretAimy = angley;
                                }
                                players[ID].SetAim(new Vector2D(turretAimx, turretAimy));
                            }
                            break;
                        }
                    }
                }
            }
            else if (!players.ContainsKey(ID))
            {
                Console.WriteLine("Player " + state.ID.ToString() + " Connected.");
                CreateTank(data, ID);
                Sendwalls(state);
                Networking.GetData(state);
                return;
            }
            Networking.GetData(state);
        }