Example #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());
            }
        }
Example #2
0
        public static void Main()
        {
            //Read settings from file
            ReadSettingFile(@"..\\..\\..\\Resources\settings.xml");

            //Start the server and web server
            Networking.StartServer(ReceivePlayerName, 11000);
            Networking.StartServer(HandleHttpConnection, 80);

            Console.WriteLine("Server is running. Accepting clients.");

            //Start the main loop
            Thread MainThread = new Thread(FrameLoop);

            MainThread.Start();

            //Saves the game to the database and waits for an input
            Console.ReadLine();
            MainThread.Abort();
            DatabaseController.SaveGameToDatabase(TheWorld);
            Console.WriteLine("Saved game to database");
            Console.ReadLine();
        }