private void HandlePacket(NetworkMessage msg) { switch (msg.msgType) { case (PktType.TextMessage): TextMessagePkt TextMsg = new TextMessagePkt(); TextMsg.Deserialize(msg.reader); Term.Println(TextMsg.Message); break; case (PktType.Identity): IdentityPkt Identity = new IdentityPkt(); Identity.Deserialize(msg.reader); var Conn = new CConnectedPlayer(Identity.ConnectionID); if (Identity.Owned) { Player = Conn; } else { Debug.Log(Conn.ConnectionID + " connected!"); Term.Println(Conn.ConnectionID + " connected!"); } Players.Add(Conn.ConnectionID, Conn); break; case (PktType.MassIdentity): IntListPkt Identities = new IntListPkt(); Identities.Deserialize(msg.reader); foreach (int currId in Identities.IntList) { Players.Add(currId, new CConnectedPlayer(currId)); } break; case (PktType.SpawnEntity): SpawnEntityPkt SpawnPkt = new SpawnEntityPkt(); SpawnPkt.Deserialize(msg.reader); EntityType EntityType = SpawnPkt.EntityType; // Check if you are the owner and if they are spawning an NPC if (SpawnPkt.OwnerID == Player.ConnectionID && EntityType == EntityType.NPC) { // Change it into a PC instead. EntityType = EntityType.PC; } GameObject Obj = Spawner.Spawn(EntityType, SpawnPkt.Position, SpawnPkt.SyncBaseIDList); if (SpawnPkt.OwnerID > -1) { Players[SpawnPkt.OwnerID].Character = Obj.GetComponent <Character>(); } if (SpawnPkt.OwnerID == Player.ConnectionID) { gameObject.AddComponent <ClientSyncer>(); } break; case (PktType.MoveCreature): MoveCreaturePkt MoveCreature = new MoveCreaturePkt(); MoveCreature.Deserialize(msg.reader); SyncBase SyncBase = Spawner.SyncDB.Get(MoveCreature.SyncBaseID); if (SyncBase != null || SyncBase is Character) { Character Character = (Character)SyncBase; Character.Move(MoveCreature.Direction); } else { Debug.LogError("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature"); Term.Println("SyncBase " + MoveCreature.SyncBaseID + " is not a Creature"); } break; case (PktType.Interact): InteractionPkt Interaction = new InteractionPkt(); Interaction.Deserialize(msg.reader); if (Interaction.OwnerSyncBaseID == Player.Character.ID) { break; } SyncBase Target = Spawner.SyncDB.Get(Interaction.InteractSyncBaseID); if (Target != null && Target is Interactable) { ((Interactable)Target).Interact( Spawner.SyncDB.Get(Interaction.OwnerSyncBaseID), Interaction.InteractionType); } else { Term.Println("Server has sent an erroneus SyncBase ID!"); } break; case (PktType.Sync): SyncHandler.HandleSyncPkt(msg); break; case (PktType.StaticObjectIds): IntListPkt StaticIds = new IntListPkt(); StaticIds.Deserialize(msg.reader); Spawner.SyncDB.SetStaticObjectsIDs(StaticIds.IntList); break; case (PktType.Disconnect): DisconnectPkt Disconnect = new DisconnectPkt(); Disconnect.Deserialize(msg.reader); Term.Println(Disconnect.ConnectionID + " disconnected!"); Spawner.Remove(Players[Disconnect.ConnectionID].Character.gameObject); Players.Remove(Disconnect.ConnectionID); break; case (PktType.InventoryAction): InventoryActionPkt InventoryActionPkt = new InventoryActionPkt(); InventoryActionPkt.Deserialize(msg.reader); SyncBase CurrSyncBase = Spawner.SyncDB.Get(InventoryActionPkt.SyncBaseID); Inventory Inventory; if (CurrSyncBase is Inventory) { Inventory = (Inventory)CurrSyncBase; } else { break; } Inventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.IntList); break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType); break; } }
private void HandlePacket(NetworkMessage msg) { switch (msg.msgType) { case PktType.TextMessage: TextMessagePkt TextMsg = new TextMessagePkt(); TextMsg.Deserialize(msg.reader); Term.Println(TextMsg.Message); break; case PktType.MoveCreature: MoveCreaturePkt MoveCreature = new MoveCreaturePkt(); MoveCreature.Deserialize(msg.reader); // Check if the player is allowed to move this character Character Controlled = Players[msg.conn.connectionId].Character.GetComponent <Character>(); if (Controlled.ID != MoveCreature.SyncBaseID) { break; } Controlled.Move(MoveCreature.Direction); foreach (var Player in Players) { if (Player.Value.Connection.connectionId == msg.conn.connectionId) { continue; } NetworkServer.SendToClient(Player.Value.Connection.connectionId, PktType.MoveCreature, MoveCreature); } break; case PktType.Interact: InteractionPkt Interaction = new InteractionPkt(); Interaction.Deserialize(msg.reader); Character Sender = Players[msg.conn.connectionId].Character; SyncBase Target = Spawner.SyncDB.Get(Interaction.InteractSyncBaseID); Interaction.OwnerSyncBaseID = Sender.ID; if (Target != null && Target is Interactable) { Interactable Interacted = (Interactable)Target; Vector3 Delta = Interacted.gameObject.transform.position - Sender.gameObject.transform.position; float ServerInteractionDistance = Sender.InteractionDistance + Sender.MovementSpeed * 0.5f; if (Delta.magnitude <= ServerInteractionDistance) { Interacted.Interact(Sender, Interaction.InteractionType); NetworkServer.SendToAll(PktType.Interact, Interaction); if (Interacted.GetInteractableSyncdata().RequiresSyncing) { Syncer.DirtSyncBase(Interacted.ID); } } } else { Term.Println("Client has reported an erronous SyncBase ID!"); } break; case PktType.ClientSync: ServerSyncHandler.HandleSyncPkt(msg); break; case PktType.Disconnect: msg.conn.Disconnect(); break; case PktType.FailedChecksums: IntListPkt FailedSyncs = new IntListPkt(); FailedSyncs.Deserialize(msg.reader); foreach (int SyncBaseId in FailedSyncs.IntList) { Syncer.DirtSyncBase(SyncBaseId); } break; case PktType.InventoryAction: InventoryActionPkt InventoryActionPkt = new InventoryActionPkt(); InventoryActionPkt.Deserialize(msg.reader); Character Character = Players[msg.conn.connectionId].Character; Inventory CurrInventory = Character.GetComponent <Inventory>(); InventoryActionPkt.SyncBaseID = CurrInventory.ID; if (CurrInventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.IntList)) { SendToAll(PktType.InventoryAction, InventoryActionPkt); } break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType); break; } }