Exemple #1
0
        /// <summary>
        /// Sends data of all connected players
        /// </summary>
        public static void SyncCurrentPlayers()
        {
            foreach (ServerPlayer ply in players)
            {
                // Send data about every player except their own player to the client
                outMsg = server.CreateMessage();
                outMsg.Write(ServerClientInterface.SYNC_CHUNK);
                outMsg.Write(ply.Identifier);
                outMsg.Write(ply.UserName);
                outMsg.Write(ServerClientInterface.TeamToByte(ply.CurrentTeam));
                outMsg.Write(ply.Position.X);
                outMsg.Write(ply.Position.Y);
                outMsg.Write(ply.Rotation);
                outMsg.Write(WeaponData.WeaponToByte(ply.CurrentWeapon.Weapon));
                outMsg.Write(ServerClientInterface.StateToByte(ply.State));
                server.SendToAll(outMsg, NetDeliveryMethod.ReliableSequenced);
            }

            // Let everyone know the sync is complete
            outMsg = server.CreateMessage();
            outMsg.Write(ServerClientInterface.SYNC_COMPLETE);
            server.SendToAll(outMsg, NetDeliveryMethod.ReliableSequenced);
        }
Exemple #2
0
 /// <summary>
 /// Sets the team of the player
 /// </summary>
 /// <param name="team"></param>
 public void SetTeam(byte team)
 {
     CurrentTeam = ServerClientInterface.ByteToTeam(team);
 }
Exemple #3
0
        /// <summary>
        /// Updates and processes requests by users
        /// </summary>
        public static void UpdateNetwork()
        {
            // TODO : Updates the network, recieves input.
            while ((msg = server.ReadMessage()) != null)
            {
                outMsg = server.CreateMessage();
                ServerPlayer player;
                byte         code;
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                    switch ((NetConnectionStatus)msg.ReadByte())
                    {
                    case NetConnectionStatus.Connected:
                        // If someone has successfully connected to the server, check
                        // if there are too many connected clients and prevent entry from
                        // the client if the number of users exceed the max number of players
                        if (numPlayers > maxPlayers)
                        {
                            msg.SenderConnection.Deny("Server Is Full");
                        }

                        // initialize handshake with the client. Give them a unique identifier
                        // which allows the server to differ between multiple clients
                        outMsg.Write(ServerClientInterface.HANDSHAKE);

                        // Send the message
                        server.SendMessage(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableSequenced);
                        break;

                    case NetConnectionStatus.Disconnected:
                        // Get the player that just disconnected
                        player =
                            players.Find(
                                ply => ply.ConnectionIdentifier == msg.SenderConnection.RemoteUniqueIdentifier);

                        // Tell everyone that the user disconnected
                        outMsg.Write(ServerClientInterface.PLAYER_DISCONNECTED);
                        outMsg.Write(player.Identifier);
                        server.SendToAll(outMsg, NetDeliveryMethod.ReliableSequenced);

                        // Subtract number of players of the associated team
                        switch (player.CurrentTeam)
                        {
                        case ServerClientInterface.Team.CounterTerrorist:
                            numCts--;
                            break;

                        case ServerClientInterface.Team.Terrorist:
                            numTs--;
                            break;
                        }
                        // Subtract one less player on the server
                        numPlayers--;

                        Console.WriteLine("\"" + player.UserName + "\" has left the server");

                        // Remove the player from the server
                        players.Remove(player);
                        break;
                    }
                    break;

                case NetIncomingMessageType.Data:
                    // Get identifier byte used to determine message type
                    code = msg.ReadByte();
                    switch (code)
                    {
                    // A user requested to retrieve information of all players on server
                    case ServerClientInterface.REQUEST_SYNC:
                        string username = msg.ReadString();
                        // Set up a new player in the server
                        player = new ServerPlayer(username, playerIdentifier, msg.SenderConnection.RemoteUniqueIdentifier);
                        players.Add(player);
                        playerIdentifier++;
                        numPlayers++;

                        // Let the client know their information was recieved and processed
                        outMsg.Write(ServerClientInterface.HANDSHAKE_COMPLETE);
                        outMsg.Write(player.Identifier);
                        server.SendMessage(outMsg, msg.SenderConnection, NetDeliveryMethod.ReliableSequenced);

                        // Resend data about players to everyone in order to stay in sync
                        SyncCurrentPlayers();
                        Console.WriteLine("\"" + username + "\" has joined the server");
                        break;

                    case ServerClientInterface.CHANGE_TEAM:
                        // Find the player with the matching unique identifier
                        player = players.Find(
                            ply => ply.ConnectionIdentifier == msg.SenderConnection.RemoteUniqueIdentifier);

                        // Change their team
                        player.SetTeam(msg.ReadByte());

                        // Increase the number of players currently on the
                        // associated team
                        switch (player.CurrentTeam)
                        {
                        case ServerClientInterface.Team.CounterTerrorist:
                            numCts++;
                            break;

                        case ServerClientInterface.Team.Terrorist:
                            numTs++;
                            break;
                        }

                        // Tell everyone else that the player had switched teams and
                        // what team they switched to
                        outMsg.Write(ServerClientInterface.CHANGE_TEAM);
                        outMsg.Write(player.Identifier);
                        outMsg.Write(ServerClientInterface.TeamToByte(player.CurrentTeam));
                        server.SendToAll(outMsg, NetDeliveryMethod.ReliableSequenced);

                        Console.WriteLine("\"" + player.UserName + "\" joined the " + player.CurrentTeam);
                        break;

                    // Movement Processing for 8 directions
                    case ServerClientInterface.MOVE_UP:
                    case ServerClientInterface.MOVE_DOWN:
                    case ServerClientInterface.MOVE_LEFT:
                    case ServerClientInterface.MOVE_RIGHT:
                    case ServerClientInterface.MOVE_UPLEFT:
                    case ServerClientInterface.MOVE_UPRIGHT:
                    case ServerClientInterface.MOVE_DOWNRIGHT:
                    case ServerClientInterface.MOVE_DOWNLEFT:
                        Move(code, msg.SenderConnection.RemoteUniqueIdentifier);
                        break;

                    // Rotation processing
                    case ServerClientInterface.ROTATE_PLAYER:
                        Rotate(msg.ReadFloat(), msg.SenderConnection.RemoteUniqueIdentifier);
                        break;

                    // Weapon buying processing
                    case ServerClientInterface.BUY_WEAPON:
                        byte wep = msg.ReadByte();
                        SpawnWeapon(msg.SenderConnection.RemoteUniqueIdentifier, wep);
                        break;

                    // Weapon firing processing
                    case ServerClientInterface.FIRE_WEAPON:
                        FireWeapon(msg.SenderConnection.RemoteUniqueIdentifier);
                        break;

                    // Flashbang exploding processing
                    case ServerClientInterface.EXPLODE_FLASHBANG:
                        outMsg.Write(ServerClientInterface.EXPLODE_FLASHBANG);
                        server.SendToAll(outMsg, NetDeliveryMethod.UnreliableSequenced);
                        break;

                    // User requested respawn
                    case ServerClientInterface.REQUEST_RESPAWN:
                        // Client requested respawn
                        player =
                            players.Find(
                                ply => ply.ConnectionIdentifier == msg.SenderConnection.RemoteUniqueIdentifier);

                        // If they are dead, respawn the player
                        if (player.State == ServerClientInterface.PlayerState.Dead)
                        {
                            RespawnPlayer(player);
                        }
                        break;
                    }
                    break;
                }
            }
        }