public static void HandlePlayerDisconnect(int ClientID, byte[] PacketData) { //read the packet ByteBuffer.ByteBuffer PacketReader = new ByteBuffer.ByteBuffer(); PacketReader.WriteBytes(PacketData); int PacketType = PacketReader.ReadInteger(); PacketReader.Dispose(); //Find this clients info Client DisconnectedClient = ClientManager.Clients[ClientID]; string Account = DisconnectedClient.AccountName; string Character = DisconnectedClient.CurrentCharacterName; Vector3 Position = DisconnectedClient.CharacterPosition; //Remove them from the physics scene Globals.space.Remove(DisconnectedClient.ServerCollider); //Open up the database and backup their characters world position var DB = Globals.database; var Rec = DB.recorder; string UpdatePos = "UPDATE characters SET XPosition='" + Position.X + "', YPosition='" + Position.Y + "', ZPosition='" + Position.Z + "' WHERE CharacterName='" + Character + "'"; Rec.Open(UpdatePos, DB.connection, DB.cursorType, DB.lockType); //Remove them from the client list, then tell all the other active players they have logged out ClientManager.Clients.Remove(ClientID); List <Client> ActiveClients = ClientManager.GetAllActiveClients(); PacketSender.SendListRemoveOtherPlayer(ActiveClients, Character); Console.WriteLine(Account + ":" + Character + " has disconnected"); }
//tells a client where all the active entities are when they are first entering the server public static void SendActiveEntities(int ClientID) { //Initialise the header data of the network packet ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteInteger((int)ServerPacketType.SpawnActiveEntityList); //Find out how many active entities there are and write the count value in the packet data int EntityCount = EntityManager.ActiveEntities.Count; PacketWriter.WriteInteger(EntityCount); //Loop through each of the entities that are active in the scene right now List <ServerEntity> EntityList = EntityManager.GetEntityList(); foreach (ServerEntity entity in EntityList) { //We need to save each entities ID, Type, and World Position PacketWriter.WriteString(entity.ID); PacketWriter.WriteString(entity.Type); PacketWriter.WriteFloat(entity.entity.Position.X); PacketWriter.WriteFloat(entity.entity.Position.Y); PacketWriter.WriteFloat(entity.entity.Position.Z); } //Once the packet has all the information, close it and send it off to the client ClientManager.SendPacketTo(ClientID, PacketWriter.ToArray()); PacketWriter.Dispose(); }
//tells a client to enter into the game world public static void SendPlayerEnterWorld(int ClientID) { Client Client = ClientManager.Clients[ClientID]; ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteInteger((int)ServerPacketType.PlayerEnterWorld); //write the packet type //we also want to pass information about all of the other clients already playing the game right now List <Client> OtherPlayers = ClientManager.GetActiveClientsExceptFor(ClientID); PacketWriter.WriteInteger(OtherPlayers.Count); //how many other players are in the game foreach (Client Other in OtherPlayers) { //Provide the information for each other player who is in the game PacketWriter.WriteString(Other.CurrentCharacterName); //characters name //characters position PacketWriter.WriteFloat(Other.CharacterPosition.X); PacketWriter.WriteFloat(Other.CharacterPosition.Y); PacketWriter.WriteFloat(Other.CharacterPosition.Z); } //send the packet to the client ClientManager.SendPacketTo(ClientID, PacketWriter.ToArray()); PacketWriter.Dispose(); }
//Packetnum = 5 void HandleGetPlayersInRoom(int index, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); int packetnum = buffer.ReadInt(); int roomIndex = buffer.ReadInt(); int[] clientsInRoom = Network.instance.roomHandler.GetPlayersInRoom(roomIndex); buffer.Clear(); buffer.WriteInt(4); int count = 0; foreach (int clientIndex in clientsInRoom) { if (clientIndex != -1) { count++; } } buffer.WriteInt(count); foreach (int clientIndex in clientsInRoom) { if (clientIndex != -1) { string username = Network.Clients[clientIndex].player.GetUsername(); buffer.WriteString(username); } } Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
//PacketNum = 13 void HandleLeaveGame(int index, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); int packetnum = buffer.ReadInt(); int gameRoomIndex = Network.Clients[index].player.GetGameRoomIndex(); Network.instance.gameHandler.LeaveGame(gameRoomIndex, index); buffer.Clear(); buffer.WriteInt(13); Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); int[] playersInRoom = Network.instance.gameHandler.GetPlayersInGame(gameRoomIndex, index); buffer.Clear(); buffer.WriteInt(11); buffer.WriteInt(index); buffer.WriteInt(Network.Clients[index].player.GetTeamNumber()); foreach (int clientIndex in playersInRoom) { Network.Clients[clientIndex].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
void OnReceiveUdpData(IAsyncResult result) { try { IPEndPoint IpEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] readBytes = UdpClient.EndReceive(result, ref IpEndPoint); if (UdpClient == null) { return; } ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(readBytes); int index = buffer.ReadInt(); if (Network.Clients[index] != null) { byte[] croppedData = readBytes.Skip(4).ToArray(); Clients[index].UdpIP = IpEndPoint; ServerHandlePackets.instance.HandleUdpData(index, croppedData); } UdpClient.BeginReceive(new AsyncCallback(OnReceiveUdpData), null); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } }
//Sends a packet to the client with the matching connection ID public static void SendPacketTo(int ClientID, byte[] PacketData) { ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteBytes(PacketData); Clients[ClientID].ClientStream.BeginWrite(PacketWriter.ToArray(), 0, PacketWriter.ToArray().Length, null, null); PacketWriter.Dispose(); }
//Trys to create a new character for the user and tells them how it went public static void HandleCreateCharacterRequest(int ClientID, byte[] PacketData) { //Open the packet and extract all the relevant information, then close it Console.WriteLine("checking if we can create a new character for this player"); ByteBuffer.ByteBuffer PacketReader = new ByteBuffer.ByteBuffer(); PacketReader.WriteBytes(PacketData); int PacketType = PacketReader.ReadInteger(); string AccountName = PacketReader.ReadString(); string CharacterName = PacketReader.ReadString(); bool IsMale = PacketReader.ReadInteger() == 1; Console.WriteLine(AccountName + " wants to register " + CharacterName + " as a new character"); PacketReader.Dispose(); //Query the database to check if this character name has already been taken by someone else bool CharacterExists = Globals.database.DoesCharacterExist(ClientID, CharacterName); //If this character already exists, send the reply message telling them the name is already taken if (CharacterExists) { PacketSender.SendCreateCharacterReply(ClientID, false, "This character name is already taken"); return; } //Otherwise, we need to create this new character and save it into the database registered under this users account Globals.database.RegisterNewCharacter(AccountName, CharacterName, IsMale); PacketSender.SendCreateCharacterReply(ClientID, true, "Character Created"); }
//client wants to enter into the game world with their selected character public static void HandleEnterWorldRequest(int ClientID, byte[] PacketData) { //Extract information from the packet and save it into this clients class in the client manager list ByteBuffer.ByteBuffer PacketReader = new ByteBuffer.ByteBuffer(); PacketReader.WriteBytes(PacketData); int PacketType = PacketReader.ReadInteger(); Client Client = ClientManager.Clients[ClientID]; Client.AccountName = PacketReader.ReadString(); Client.InGame = true; Client.CurrentCharacterName = PacketReader.ReadString(); Client.CharacterPosition = new Vector3(PacketReader.ReadFloat(), PacketReader.ReadFloat(), PacketReader.ReadFloat()); PacketReader.Dispose(); //Send this player into the world with their character now Console.WriteLine(Client.AccountName + ":" + Client.CurrentCharacterName + " has entered the world"); PacketSender.SendPlayerEnterWorld(ClientID); //Give information to the new player about any entities that are active in the world PacketSender.SendActiveEntities(ClientID); //Spawn a representation of this player in the server physics scene Client.ServerCollider = new Sphere(Client.CharacterPosition, 1); Globals.space.Add(Client.ServerCollider); Globals.game.ModelDrawer.Add(Client.ServerCollider); //Any other players who are already playing the game need to be told to spawn this client List <Client> OtherActivePlayers = ClientManager.GetActiveClientsExceptFor(ClientID); PacketSender.SendListSpawnOther(OtherActivePlayers, Client.CurrentCharacterName, Client.CharacterPosition); }
private static void LoginRequest(ConnectionType type, int index, byte[] data) { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); ReadHeader(ref buffer); string username = buffer.ReadString(); string password = buffer.ReadString(); Response r = Database.instance.Login(username, password); switch (r) { case Response.SUCCESSFUL: Network.instance.Clients[index].LoggedIn = true; Network.instance.Clients[index].Username = username; Database.instance.LogActivity(username, Activity.LOGIN, Network.instance.Clients[index].SessionID); SendData.LoginResponse(index, 1); SendData.WhiteListConfirmation(Network.instance.Clients[index].IP); break; case Response.UNSUCCESSFUL: SendData.LoginResponse(index, 0); break; case Response.ERROR: SendData.LoginResponse(index, 2); break; default: break; } }
private static void CharacterListRequest(ConnectionType type, int index, byte[] data) { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); ReadHeader(ref buffer); string username = buffer.ReadString(); Response r = Database.instance.GetCharacters(username, index); switch (r) { case Response.SUCCESSFUL: SendData.CharacterList(index, true); break; case Response.UNSUCCESSFUL: SendData.CharacterList(index, true); break; case Response.ERROR: SendData.CharacterList(index, false); break; default: break; } }
private static void ConnectivityData(ConnectionType type, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); ReadHeader(ref buffer); int Count = buffer.ReadInteger(); for (int i = 0; i < Count; i++) { int Character_ID = buffer.ReadInteger(); float TCP_Throughput = buffer.ReadFloat(); int TCP_Packets_Received = buffer.ReadInteger(); int TCP_Packets_Sent = buffer.ReadInteger(); float TCP_Latency = buffer.ReadFloat(); float UDP_Throughput = buffer.ReadFloat(); int UDP_Packets_Received = buffer.ReadInteger(); int UDP_Packets_Sent = buffer.ReadInteger(); float UDP_Latency = buffer.ReadFloat(); DateTime LogStart = Convert.ToDateTime(buffer.ReadString()); DateTime LogFinish = Convert.ToDateTime(buffer.ReadString()); Data.tbl_Connectivity.Add(new _Connectivity(Character_ID, LogStart, TCP_Latency, TCP_Throughput, TCP_Packets_Sent, TCP_Packets_Received, UDP_Latency, UDP_Throughput, UDP_Packets_Sent, UDP_Packets_Received, true)); } } catch (Exception e) { Log.log("Connectivity Data threw an error " + e.Message); } }
private static void UpdatePlayerData(ConnectionType type, byte[] data) { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); ReadHeader(ref buffer); // ID int Character_ID = buffer.ReadInteger(); // Position float x = buffer.ReadFloat(); float y = buffer.ReadFloat(); float z = buffer.ReadFloat(); float r = buffer.ReadFloat(); // Camera Position float cx = buffer.ReadFloat(); float cy = buffer.ReadFloat(); float cz = buffer.ReadFloat(); float cr = buffer.ReadFloat(); // Experience int Level = buffer.ReadInteger(); int Experience = buffer.ReadInteger(); // Player Data.tbl_Characters[Character_ID].Pos_X = x; Data.tbl_Characters[Character_ID].Pos_Y = y; Data.tbl_Characters[Character_ID].Pos_Z = z; Data.tbl_Characters[Character_ID].Rotation_Y = r; // Camera Data.tbl_Characters[Character_ID].Camera_Pos_X = cx; Data.tbl_Characters[Character_ID].Camera_Pos_Y = cy; Data.tbl_Characters[Character_ID].Camera_Pos_Z = cz; Data.tbl_Characters[Character_ID].Camera_Rotation_Y = cr; // Experience Data.tbl_Characters[Character_ID].Character_Level = Level; Data.tbl_Characters[Character_ID].Experience = Experience; }
//tells each in the list of clients about every entity in that list public static void SendListEntityUpdates(List <Client> ClientList, List <BaseEntity> EntityList) { //Console.WriteLine("send entity updates"); ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteInteger((int)ServerPacketType.SendEntityUpdates); PacketWriter.WriteInteger(EntityList.Count); foreach (var BaseEntity in EntityList) { //ID PacketWriter.WriteString(BaseEntity.ID); //Position PacketWriter.WriteFloat(BaseEntity.Position.X); PacketWriter.WriteFloat(BaseEntity.Position.Y); PacketWriter.WriteFloat(BaseEntity.Position.Z); //Rotation PacketWriter.WriteFloat(BaseEntity.Rotation.X); PacketWriter.WriteFloat(BaseEntity.Rotation.Y); PacketWriter.WriteFloat(BaseEntity.Rotation.Z); PacketWriter.WriteFloat(BaseEntity.Rotation.W); } foreach (Client Client in ClientList) { ClientManager.SendPacketTo(Client.ClientID, PacketWriter.ToArray()); } PacketWriter.Dispose(); }
public static void StompResponse(int index, List <DamageResponse> value) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.StompResponse, ref buffer); buffer.WriteString(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")); buffer.WriteInteger(value.Count); for (int i = 0; i < value.Count; i++) { buffer.WriteInteger(value[i].NPC_Entity_ID); buffer.WriteInteger(value[i].New_HP); buffer.WriteInteger(value[i].Damage); buffer.WriteByte(value[i].Crit ? (byte)1 : (byte)0); } Log.log("Sending Stomp Response packet to client..", Log.LogType.SENT); sendData(ConnectionType.CLIENT, ClientSendPacketNumbers.StompResponse.ToString(), index, buffer.ToArray()); } catch (Exception e) { Log.log("Building Stomp Response packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
//Gets updated character information data from one of the connect clients, this needs to be sent out to everyone else so they know where that character is at public static void HandlePlayerUpdate(int ClientID, byte[] PacketData) { //Extract the new position data from the network packet ByteBuffer.ByteBuffer PacketReader = new ByteBuffer.ByteBuffer(); PacketReader.WriteBytes(PacketData); int PacketType = PacketReader.ReadInteger(); string CharacterName = PacketReader.ReadString(); Vector3 CharacterPosition = new Vector3(PacketReader.ReadFloat(), PacketReader.ReadFloat(), PacketReader.ReadFloat()); Quaternion CharacterRotation = new Quaternion(PacketReader.ReadFloat(), PacketReader.ReadFloat(), PacketReader.ReadFloat(), PacketReader.ReadFloat()); PacketReader.Dispose(); //Update the position in the server physics scene Client Client = ClientManager.Clients[ClientID]; Client.ServerCollider.Position = CharacterPosition; //Store the players new information in their assigned client object ClientManager.Clients[ClientID].CharacterPosition = CharacterPosition; //Send this new data out to all of the other connected clients List <Client> OtherClients = ClientManager.GetOtherClients(ClientID); foreach (Client OtherClient in OtherClients) { PacketSender.SendPlayerUpdatePosition(OtherClient.ClientID, CharacterName, CharacterPosition, CharacterRotation); } }
//public void StartConnectionTimer() { // checkConnectionTimer.Elapsed += new ElapsedEventHandler(SendCheckConnection); // checkConnectionTimer.Interval = 2000; // checkConnectionTimer.Enabled = true; //} //private void SendCheckConnection(object source, ElapsedEventArgs arg) { // ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); // buffer.WriteInt(23); // Console.WriteLine("in"); // foreach(Client client in Clients) { // if(client.TcpClient != null) { // client.TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); // client.TcpStream.Read() // } // } //} void OnClientConnect(IAsyncResult result) { TcpClient client = ServerSocket.EndAcceptTcpClient(result); client.NoDelay = false; ServerSocket.BeginAcceptTcpClient(OnClientConnect, null); for (int i = 0; i < Settings.MAX_PLAYERS; i++) { if (Clients[i].TcpClient == null) { Clients[i].TcpClient = client; Clients[i].Index = i; Clients[i].IP = (IPEndPoint)client.Client.RemoteEndPoint; Clients[i].Start(); Console.WriteLine("Incoming connection from " + Clients[i].IP.ToString() + " || index: " + i); //send welcome messages ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteInt(1); buffer.WriteString("Welcome to Server!"); buffer.WriteInt(i); client.GetStream().BeginWrite(buffer.BuffToArray(), 0, buffer.Length(), null, null); return; } } }
public static void UpdatePlayerData(Player player) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)SyncServerSendPacketNumbers.UpdatePlayerData, ref buffer); // ID buffer.WriteInteger(player.Character_ID); // Position buffer.WriteFloat(player.position.x); buffer.WriteFloat(player.position.y); buffer.WriteFloat(player.position.z); buffer.WriteFloat(player.r); // Camera Position buffer.WriteFloat(player.Camera_Pos_X); buffer.WriteFloat(player.Camera_Pos_Y); buffer.WriteFloat(player.Camera_Pos_Z); buffer.WriteFloat(player.Camera_Rotation_Y); // Experience buffer.WriteInteger(player.Level); buffer.WriteInteger(player.experience); buffer.WriteInteger(player.Max_HP); buffer.WriteInteger(player.Strength); buffer.WriteInteger(player.Agility); sendData(ConnectionType.SYNCSERVER, SyncServerSendPacketNumbers.UpdatePlayerData.ToString(), -1, buffer.ToArray()); } catch (Exception e) { Log.log("Building Update Player Data packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
public static void CharacterList(int index, bool success) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.CharacterList, ref buffer); buffer.WriteByte((success) ? (byte)1 : (byte)0); buffer.WriteInteger(Network.instance.Clients[index].Characters.Count); if (success) { foreach (Character c in Network.instance.Clients[index].Characters) { buffer.WriteString(c.Name); buffer.WriteInteger(c.Level); buffer.WriteInteger(c.Gender); } } sendData(ConnectionType.CLIENT, (int)ClientSendPacketNumbers.CharacterList, index, buffer.ToArray()); } catch (Exception e) { Log.log("Building Character List packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
public static void ConnectivityData(Dictionary <int, Connectivity> ConnectivityData) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)SyncServerSendPacketNumbers.ConnectivityData, ref buffer); buffer.WriteInteger(ConnectivityData.Count); foreach (KeyValuePair <int, Connectivity> c in ConnectivityData) { buffer.WriteInteger(c.Value.Character_ID); buffer.WriteFloat(c.Value.TCP_Throughput); buffer.WriteInteger(c.Value.TCP_PacketsReceived); buffer.WriteInteger(c.Value.TCP_PacketsSent); buffer.WriteFloat(c.Value.TCP_Latency); buffer.WriteFloat(c.Value.UDP_Throughput); buffer.WriteInteger(c.Value.UDP_PacketsReceived); buffer.WriteInteger(c.Value.UDP_PacketsSent); buffer.WriteFloat(c.Value.UDP_Latency); buffer.WriteString(c.Value.LogStart.ToString()); buffer.WriteString(c.Value.LogFinish.ToString()); } sendData(ConnectionType.SYNCSERVER, SyncServerSendPacketNumbers.ConnectivityData.ToString(), -1, buffer.ToArray()); } catch (Exception e) { Log.log("Building Connectivity Data packet failed. > " + e.Message, Log.LogType.ERROR); } }
//Packetnum = 2 void HandleCreateRoom(int index, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); if (Network.instance.roomHandler.AllRoomsTaken()) { buffer.WriteInt(3); buffer.WriteInt(-1); buffer.WriteInt(-1); Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); return; } buffer.WriteBytes(data); int packetnum = buffer.ReadInt(); int maxPlayers = buffer.ReadInt(); int roomIndex = Network.instance.roomHandler.CreateRoom(index, maxPlayers); buffer.Clear(); buffer.WriteInt(3); buffer.WriteInt((roomIndex != -1)? 1:0); buffer.WriteInt(roomIndex); if (roomIndex != -1) { Network.Clients[index].player.SetRoomNumber(roomIndex); } Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void QuestReturn(int index, QuestReturn qr, int quest_Log_ID) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.QuestReturn, ref buffer); buffer.WriteString(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")); if (qr.Null) { buffer.WriteByte(1); } else { buffer.WriteByte(0); buffer.WriteInteger((int)qr.Status); buffer.WriteInteger(qr.Quest_ID); buffer.WriteInteger(quest_Log_ID); buffer.WriteInteger(qr.NPC_ID); buffer.WriteString(qr.Title); buffer.WriteString(qr.Text); buffer.WriteInteger(World.instance.GetQuestLog(Network.instance.Clients[index].Character_ID, qr.Quest_ID).ObjectiveProgress); buffer.WriteInteger(qr.Target); } Log.log("Sending Quest Return packet to client..", Log.LogType.SENT); sendData(ConnectionType.CLIENT, ClientSendPacketNumbers.QuestReturn.ToString(), index, buffer.ToArray()); } catch (Exception e) { Log.log("Building Quest Return packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
//Packetnum = 10 void HandleGetPlayersInGame(int index, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); int packetnum = buffer.ReadInt(); int gameRoomIndex = Network.Clients[index].player.GetGameRoomIndex(); int[] playersInRoom = Network.instance.gameHandler.GetAlivePlayersInGame(gameRoomIndex, index); buffer.Clear(); buffer.WriteInt(7); buffer.WriteInt(playersInRoom.Length); foreach (int clientIndex in playersInRoom) { Player player = Network.Clients[clientIndex].player; buffer.WriteInt(player.GetId()); buffer.WriteInt(player.GetTeamNumber()); buffer.WriteString(player.GetUsername()); buffer.WriteFloat(player.GetPosX()); buffer.WriteFloat(player.GetPosY()); buffer.WriteFloat(player.GetPosZ()); buffer.WriteFloat(player.GetRotY()); } Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void CollectableInteractConfirm(int index, Collectable col, Quest_Log ql = null) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.CollectableInteractConfirm, ref buffer); buffer.WriteString(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")); buffer.WriteInteger(col.Entity_ID); buffer.WriteByte(col.Active ? (byte)1 : (byte)0); if (ql != null) { buffer.WriteByte(1); buffer.WriteInteger(ql.Quest_ID); buffer.WriteInteger(ql.ObjectiveProgress); buffer.WriteInteger((int)ql.Status); } else { buffer.WriteByte(0); } Log.log("Sending Collectable Interaction Confirm packet to client..", Log.LogType.SENT); sendData(ConnectionType.CLIENT, ClientSendPacketNumbers.CollectableInteractConfirm.ToString(), index, buffer.ToArray()); } catch (Exception e) { Log.log("Building Collectable Interaction Confirm packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
void HandleWaitForAllPlayersReceiveGameReady(int index, byte[] data) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); buffer.ReadInt(); int gameIndex = buffer.ReadInt(); bool ready = Network.instance.gameHandler.PlayerReady(gameIndex, NumberOfConnectedClients); Console.WriteLine("ready? : " + ready); if (!waiting.Contains(index)) { waiting.Add(index); } //if (ready) { // buffer.Clear(); // buffer.WriteInt(19); // foreach (int clientIndex in waiting) { // Network.Clients[clientIndex].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); // } // Network.Clients[index].TcpStream.Write(buffer.BuffToArray(), 0, buffer.Length()); // waiting = new List<int>(); //} //else { // waiting.Add(index); //} } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static void CollectableToggle(int Collectable_Entity_ID, bool Active) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.CollectableToggle, ref buffer); buffer.WriteString(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")); buffer.WriteInteger(Collectable_Entity_ID); buffer.WriteByte(Active ? (byte)1 : (byte)0); Log.log("Sending Collectable Interaction Confirm packet to clients..", Log.LogType.SENT); for (int i = 0; i < Network.instance.Clients.Length; i++) { if (Network.instance.Clients[i].Connected && Network.instance.Clients[i].Socket != null && Network.instance.Clients[i].Socket.Connected && Network.instance.Clients[i].InGame()) { sendData(ConnectionType.CLIENT, ClientSendPacketNumbers.CollectableToggle.ToString(), i, buffer.ToArray()); } } } catch (Exception e) { Log.log("Building Collectable Toggle packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
//tells each in the list of clients about every entity in that list public static void SendListEntityUpdates(List <Client> ClientList, List <ServerEntity> EntityList) { //Define a network packet which lists the updated targets for each entity in the given list ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteInteger((int)ServerPacketType.SendEntityUpdates); //The client will need to know how much entity updates are in the network packet PacketWriter.WriteInteger(EntityList.Count); //Now write in the required data for every entity in the list foreach (var Entity in EntityList) { //Entity ID PacketWriter.WriteString(Entity.ID); //New Entity Position PacketWriter.WriteFloat(Entity.entity.Position.X); PacketWriter.WriteFloat(Entity.entity.Position.Y); PacketWriter.WriteFloat(Entity.entity.Position.Z); //Their rotation values too PacketWriter.WriteFloat(Entity.entity.Orientation.X); PacketWriter.WriteFloat(Entity.entity.Orientation.Y); PacketWriter.WriteFloat(Entity.entity.Orientation.Z); PacketWriter.WriteFloat(Entity.entity.Orientation.W); } //The packet is ready, now send it to everyone in the list foreach (Client Client in ClientList) { ClientManager.SendPacketTo(Client.ClientID, PacketWriter.ToArray()); } //Close up the packet and finish off PacketWriter.Dispose(); }
public static void AttackResponse(int index, int Character_ID, NPC npc, int Damage) { try { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); BuildBasePacket((int)ClientSendPacketNumbers.AttackResponse, ref buffer); buffer.WriteString(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")); buffer.WriteInteger(Character_ID); buffer.WriteInteger(npc.Entity_ID); buffer.WriteInteger(Damage); buffer.WriteInteger(npc.Current_HP); Quest_Log ql = World.instance.GetQuestLogByNPCID(Character_ID, npc.NPC_ID); if (ql != null && npc.Current_HP <= 0) { ql.Increment(); buffer.WriteByte(1); buffer.WriteInteger(ql.Quest_ID); buffer.WriteInteger(ql.ObjectiveProgress); buffer.WriteInteger((int)ql.Status); } else { buffer.WriteByte(0); } Log.log("Sending Attack Response packet to client..", Log.LogType.SENT); sendData(ConnectionType.CLIENT, ClientSendPacketNumbers.AttackResponse.ToString(), index, buffer.ToArray()); } catch (Exception e) { Log.log("Building Attack Response packet failed. > " + e.Message, Log.LogType.ERROR); return; } }
//tells a client the info for each character they have created public static void SendCharacterData(int ClientID, string AccountName) { ByteBuffer.ByteBuffer PacketWriter = new ByteBuffer.ByteBuffer(); PacketWriter.WriteInteger((int)ServerPacketType.SendCharacterData); //First we need to look up in the database how many characters this user has created so far int CharacterCount = Globals.database.GetCharacterCount(ClientID, AccountName); PacketWriter.WriteInteger(CharacterCount); //Now loop through and add all the information for each character that has already been created for (int i = 0; i < CharacterCount; i++) { //Get the name of each of the players characters one at a time string CharacterName = Globals.database.GetCharacterName(AccountName, i + 1); //Get all the data for this character from the database CharacterData Data = Globals.database.GetCharacterData(CharacterName); //Save all of this information into the packet PacketWriter.WriteString(Data.Account); PacketWriter.WriteFloat(Data.Position.X); PacketWriter.WriteFloat(Data.Position.Y); PacketWriter.WriteFloat(Data.Position.Z); PacketWriter.WriteString(Data.Name); PacketWriter.WriteInteger(Data.Experience); PacketWriter.WriteInteger(Data.ExperienceToLevel); PacketWriter.WriteInteger(Data.Level); PacketWriter.WriteInteger(Data.IsMale ? 1 : 0); } //Send the packet to the client ClientManager.SendPacketTo(ClientID, PacketWriter.ToArray()); PacketWriter.Dispose(); }
private static void Heal(ConnectionType type, int index, byte[] data) { ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer(); buffer.WriteBytes(data); ReadHeader(ref buffer); if (DateTime.Now > World.instance.players[Network.instance.Clients[index].Character_ID].HealCooldownTime) { int Healed = 0; if (World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP + World.HealAmount > World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP) { int Current_HP = World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP; int Max_HP = World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP; Healed = Max_HP - Current_HP; World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP = World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP; } else { World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP += World.HealAmount; Healed = World.HealAmount; } SendData.Heal(index, World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP, Healed); World.instance.players[Network.instance.Clients[index].Character_ID].HealCooldownTime = DateTime.Now.AddSeconds(World.SpellCooldown); } }