示例#1
0
 ///Begins loop of accepting new clients
 private void StartServer()
 {
     Networking.StartServer(AcceptNewClients, 11000);
 }
示例#2
0
 public void startServer()
 {
     Networking.StartServer(FirstContact, 11000);
 }
示例#3
0
        /// <summary>
        /// We will receive the player's name and assign an ID number
        /// At this time we will also send to the client world size, the ID number
        /// and the walls
        /// </summary>
        /// <param name="client"></param>
        private void GetPlayerInfo(SocketState client)
        {
            //Check if error occured and write message to console
            if (client.ErrorOccured)
            {
                Console.WriteLine(client.ErrorMessage);
            }

            //Set the new callback action
            client.OnNetworkAction = GetActionDataFromClient;

            //Get the player name
            string playerName = client.GetData().Trim('\n');

            //Create a new tank representing the player at a random location
            Tank newPlayer = new Tank(playerNumber, playerName, RandomLocationGenerator())
            {
                //Allows the tank to fire upon spawn
                FrameCount = framesBetweenShot + 1
            };

            //We don't spawn on walls or powerups
            while (CheckForCollision(newPlayer, 1))
            {
                newPlayer.Location = new Vector2D(RandomLocationGenerator());
            }

            //Add player to our connections
            lock (connections)
            {
                //Send ID and worldsize info
                Networking.Send(client.TheSocket, playerNumber.ToString() + "\n" + serverWorld.Size.ToString() + "\n");
                //Add socket state to the collection of players with their ID number
                connections.Add(client, playerNumber);
            }

            Console.WriteLine("Player " + playerNumber.ToString() + ": " + playerName + " has connected.");

            //Add player to server world
            lock (serverWorld.Tanks)
            {
                serverWorld.Tanks.Add(newPlayer.GetID(), newPlayer);
                //Increase player ID number
                playerNumber++;
            }

            //Create a string builder info to serialize and send all the walls
            StringBuilder wallinfo = new StringBuilder();

            foreach (Wall wall in serverWorld.Walls.Values)
            {
                wallinfo.Append(JsonConvert.SerializeObject(wall) + "\n");
            }

            //Send walls to the client
            Networking.Send(client.TheSocket, wallinfo.ToString());

            //Empty the socket state of data
            client.ClearData();

            //Begin receive loop
            Networking.GetData(client);
        }
示例#4
0
        /// <summary>
        /// This method is called once per frame according to the settings.
        /// This method will update any projectiles, powerups and beams,
        /// then send all the information to each client in a Json message.
        /// Tank status is also updated
        /// </summary>
        private void UpdateWorld()
        {
            //Check proj collisions w/ tank or wall
            UpdateProjectileState();

            //Create powerups to the world
            GeneratePowerUp();

            //Build JSON and send to each client
            StringBuilder newWorld = new StringBuilder();

            //Prepare JSON messages to be sent to each client
            lock (serverWorld.PowerUps)
            {
                foreach (PowerUp power in serverWorld.PowerUps.Values)
                {
                    newWorld.Append(JsonConvert.SerializeObject(power) + "\n");
                    //Check if powerup was collected and remove
                    if (power.collected)
                    {
                        serverWorld.PowerUps.Remove(power.getID());
                    }
                }
            }

            //JSON for tanks and firing logic
            lock (serverWorld.Tanks)
            {
                foreach (Tank t in serverWorld.Tanks.Values)
                {
                    newWorld.Append(JsonConvert.SerializeObject(t) + "\n");

                    //Increment the framecounter for tank cooldown if it has fired
                    if (t.HasFired)
                    {
                        t.FrameCount++;
                    }

                    //If the framecount exceeds frames per shot, we can reset and fire again
                    if (t.FrameCount > framesBetweenShot)
                    {
                        t.FrameCount = 0;
                        t.HasFired   = false;
                    }

                    //Increment waitForRespawn level if waiting
                    if (t.HealthLevel <= 0)
                    {
                        t.WaitRespawn++;
                        //Tank is immediately alive so that animation plays only once
                        t.HasDied = false;
                    }

                    //If player died, respawn after appropriate time
                    if (t.WaitRespawn >= respawnRate)
                    {
                        RespawnPlayer(t);
                    }

                    //Check if tank has disconnected
                    if (t.HasDisconnected)
                    {
                        serverWorld.Tanks.Remove(t.GetID());
                    }
                }
            }

            //JSON for projectiles
            lock (serverWorld.Projectiles)
            {
                foreach (Projectile p in serverWorld.Projectiles.Values)
                {
                    newWorld.Append(JsonConvert.SerializeObject(p) + "\n");
                    //Remove from server world if dead
                    if (p.Died)
                    {
                        serverWorld.Projectiles.Remove(p.getID());
                    }
                }
            }

            //JSON for beams
            lock (serverWorld.Beams)
            {
                foreach (Beam b in serverWorld.Beams.Values)
                {
                    newWorld.Append(JsonConvert.SerializeObject(b) + "\n");
                    // Remove beam as soon as it has been shot for one frame
                    serverWorld.Beams.Remove(b.GetID());
                }
            }

            //JSON for powerups
            lock (serverWorld.PowerUps)
            {
                foreach (PowerUp p in serverWorld.PowerUps.Values)
                {
                    newWorld.Append(JsonConvert.SerializeObject(p) + "\n");
                    //Remove from server world if collected
                    if (p.collected)
                    {
                        serverWorld.PowerUps.Remove(p.getID());
                    }
                }
            }

            //Send the JSON message out to every connected client
            lock (connections)
            {
                foreach (SocketState clients in connections.Keys)
                {
                    //Send only to connected  clients
                    if (clients.TheSocket.Connected)
                    {
                        Networking.Send(clients.TheSocket, newWorld.ToString());
                    }
                }
            }
        }
示例#5
0
        /// <summary>
        /// Method that informs the server
        /// </summary>
        public void Process()
        {
            lock (world)
            {
                String movingDir = "";

                //If the keyPressed was up, then it sets moving dir to up
                if (keyPressedUp)
                {
                    movingDir = "up";
                }
                //If the keyPressed was left, then it sets moving dir to left
                if (keyPressedLeft)
                {
                    movingDir = "left";
                }
                //If the keyPressed was right, then it sets moving dir to right
                if (keyPressedRight)
                {
                    movingDir = "right";
                }
                //If the keyPressed was down, then it sets moving dir to down
                if (keyPressedDown)
                {
                    movingDir = "down";
                }
                //If the keyPressed was not pressed, then it sets moving dir to none
                if (!keyPressedLeft && !keyPressedDown && !keyPressedRight && !keyPressedUp)
                {
                    movingDir = "none";
                }


                String fireType = "";

                //If the left mouse was pressed, sets fireType to main
                if (leftMousePressed)
                {
                    fireType = "main";
                }
                //If the right mouse was pressed, sets fireType to alt
                else if (rightMousePressed)
                {
                    fireType = "alt";
                }
                //If the left mouse and right mouse were not pressed, sets fireType to none
                else if (!leftMousePressed && !rightMousePressed)
                {
                    fireType = "none";
                }

                //Creates a new vector with the xCoord, and yCoord and normalizes the new vector created
                vec = new Vector2D(xCoord, yCoord);
                vec.Normalize();

                //Passes this information in a ControlCommands
                ControlCommands c = new ControlCommands(movingDir, fireType, vec);

                //Serializes the control commands
                string serializedString = JsonConvert.SerializeObject(c);

                //If the server is open, then it passes the serialized string to the server
                if (theServer != null)
                {
                    Networking.Send(theServer.TheSocket, serializedString + "\n");
                }
            }
        }
示例#6
0
 /// <summary>
 /// Begins the process of connecting to the server
 /// </summary>
 /// <param name="addr"></param>
 public void Connect(string address, string name)
 {
     PlayerName = name;
     Networking.ConnectToServer(OnConnect, address, 11000);
 }
示例#7
0
 /// <summary>
 /// This method starts the tcp connection
 /// </summary>
 public void StartServer()
 {
     // This begins an "event loop"
     Networking.StartServer(HandleHttpConnection, Constant.WebPortNum);
     Console.WriteLine("WebServer is running");
 }
示例#8
0
        //Send ControlCommand to server
        private void SendControlCommand(SocketState state)
        {
            string jsonData = JsonConvert.SerializeObject(command);

            Networking.Send(state.TheSocket, jsonData + "\n");
        }
        private void SendStartupInfo(SocketState state, int playerID, int worldSize)
        {
            string message = playerID + "\n" + worldSize + "\n";

            Networking.Send(state.TheSocket, message);
        }
 public TcpListener StartGameServer()
 {
     return(Networking.StartServer(NewClientConnected, 11000));
 }
示例#11
0
 public TcpListener StartServerWebView()
 {
     return(Networking.StartServer(HandleHttpConnection, 80));
 }