Exemplo n.º 1
0
        public static void SendPlayerConnect(ClientData player)
        {
            List<byte> packet = new List<byte>();

            packet.AddRange(BitConverter.GetBytes((ushort)1)); // Packet type
            packet.AddRange(BitConverter.GetBytes((ushort)player.id)); // Client id
            packet.AddRange(BitConverter.GetBytes((double)player.xPos)); // Client x pos
            packet.AddRange(BitConverter.GetBytes((double)player.zPos)); // Client z pos
            packet.AddRange(BitConverter.GetBytes((ushort)player.name.Length)); // Client name length
            packet.AddRange(Encoding.ASCII.GetBytes(player.name)); // Client name

            // Send data to every client, if connected
            foreach (ClientData client in Server.clients)
            {
                if (client.online && client.clientSocket.Connected && client != player)
                {
                    client.clientSocket.Send(packet.ToArray(), 0, packet.Count, SocketFlags.None);
                }
            }
        }
Exemplo n.º 2
0
        public static void PlayerConnected(string message, ClientData data, Socket client)
        {
            List<byte> packet = new List<byte>();

            packet.AddRange(BitConverter.GetBytes((ushort)0)); // Packettype
            packet.AddRange(BitConverter.GetBytes((ushort)data.id)); // Player ID
            packet.AddRange(BitConverter.GetBytes((ushort)message.Length)); // Message for player length
            packet.AddRange(Encoding.ASCII.GetBytes(message)); // Message for player

            // Add player pos
            packet.AddRange(BitConverter.GetBytes((double)data.xPos)); // Player xPos
            packet.AddRange(BitConverter.GetBytes((double)data.zPos)); // Player zPos

            // Add every player that is online, his wanted position and his current position to player packet
            int amountOfPlayers = 0;
            List<byte> playerPacket = new List<byte>();
            foreach (ClientData player in Server.clients)
            {
                if (player.online && player != data)
                {
                    amountOfPlayers++;
                    playerPacket.AddRange(BitConverter.GetBytes((ushort)player.id));
                    playerPacket.AddRange(BitConverter.GetBytes((double)player.xCurrPos));
                    playerPacket.AddRange(BitConverter.GetBytes((double)player.zCurrPos));
                    playerPacket.AddRange(BitConverter.GetBytes((double)player.xPos));
                    playerPacket.AddRange(BitConverter.GetBytes((double)player.zPos));
                }
            }

            // Add amountOfPlayer and playerPacket to packet
            packet.AddRange(BitConverter.GetBytes((ushort)amountOfPlayers));
            packet.AddRange(playerPacket);

            // Send packet
            client.Send(packet.ToArray(), SocketFlags.None);

            // Send data to all connected clients that this client joined
            PacketSender.SendPlayerConnect(data);
        }
Exemplo n.º 3
0
        public ClientLevels(ClientData _client)
        {
            // Add client to this object
            client = _client;

            // Add xp ranges
            neededXP.AddRange(new List<int>() { 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470 }); // Done till lvl 20

            // Add levels and xp
            for (int i = 0; i < 4; i++)
            {
                levels.Add(1);
                xp.Add(0);
            }
        }
Exemplo n.º 4
0
        public static void SendTextMessage(bool game, ClientData player, string sender, string message)
        {
            List<byte> packet = new List<byte>();

            packet.AddRange(BitConverter.GetBytes((ushort)4)); // Packet type
            packet.AddRange(BitConverter.GetBytes(game)); // Is it a game message
            packet.AddRange(BitConverter.GetBytes((ushort)sender.Length)); // Player name length
            packet.AddRange(BitConverter.GetBytes((ushort)message.Length)); // Player message length
            packet.AddRange(Encoding.ASCII.GetBytes(sender)); // Player name
            packet.AddRange(Encoding.ASCII.GetBytes(message)); // Player message

            // Send data to every client, if connected, if player is empty
            if (player == null)
            {
                foreach (ClientData client in Server.clients)
                {
                    if (client.online && client.clientSocket.Connected)
                    {
                        client.clientSocket.Send(packet.ToArray(), 0, packet.Count, SocketFlags.None);
                    }
                }
            }
            else // Send to the player specified in player variable
                player.clientSocket.Send(packet.ToArray(), SocketFlags.None);
        }
Exemplo n.º 5
0
 public static void SendStatsUpdate(ClientData client, byte[] packet)
 {
     // Send packet to client, if connected
     if (client.online && client.clientSocket.Connected)
     {
         client.clientSocket.Send(packet, 0, packet.Length, SocketFlags.None);
     }
 }
Exemplo n.º 6
0
        public static void SendPlayerDisconnect(ClientData player)
        {
            List<byte> packet = new List<byte>();

            packet.AddRange(BitConverter.GetBytes((ushort)2)); // Packet type
            packet.AddRange(BitConverter.GetBytes((ushort)player.id)); // Client id

            // Send data to every client, if connected
            foreach (ClientData client in Server.clients)
            {
                if (client.online && client.clientSocket.Connected && client != player)
                {
                    client.clientSocket.Send(packet.ToArray(), 0, packet.Count, SocketFlags.None);
                }
            }
        }