From c1af15fb8010472083b91d4c599b9233fdd9f937 Mon Sep 17 00:00:00 2001 From: teascade Date: Mon, 15 May 2017 00:35:44 +0300 Subject: [PATCH] Send InventoryActions across space and time --- Assets/Scripts/Controls/InventoryInterface.cs | 5 ++ .../Scripts/Entities/SyncBases/Inventory.cs | 1 - Assets/Scripts/Items/InventoryAction.cs | 19 ++++++ Assets/Scripts/Items/InventoryAction.cs.meta | 12 ++++ .../Scripts/Networking/Clientside/Client.cs | 25 ++++++++ .../Networking/Messages/InventoryActionPkt.cs | 62 +++++++++++++++++++ .../Messages/InventoryActionPkt.cs.meta | 12 ++++ Assets/Scripts/Networking/PktType.cs | 5 ++ .../Scripts/Networking/Serverside/Server.cs | 22 +++++++ 9 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Items/InventoryAction.cs create mode 100644 Assets/Scripts/Items/InventoryAction.cs.meta create mode 100644 Assets/Scripts/Networking/Messages/InventoryActionPkt.cs create mode 100644 Assets/Scripts/Networking/Messages/InventoryActionPkt.cs.meta diff --git a/Assets/Scripts/Controls/InventoryInterface.cs b/Assets/Scripts/Controls/InventoryInterface.cs index 800f533..493429c 100644 --- a/Assets/Scripts/Controls/InventoryInterface.cs +++ b/Assets/Scripts/Controls/InventoryInterface.cs @@ -5,6 +5,9 @@ using Cyber.Util; using Cyber.Console; using Cyber.Entities.SyncBases; using Cyber.Items; +using Cyber.Networking.Clientside; +using Cyber.Networking; +using Cyber.Networking.Messages; namespace Cyber.Controls { @@ -142,8 +145,10 @@ namespace Cyber.Controls { Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot); if (Equipped != null && Equipped.ID == SelectedItem.ID) { Inventory.Equipped.ClearSlot(SelectedItem.Slot); + Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Unequip, (int) SelectedItem.Slot)); } else { Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem); + Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Equip, SelectedItem.ID)); } } ItemGridSelectedIndex = CurrentIndex; diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs b/Assets/Scripts/Entities/SyncBases/Inventory.cs index 6b1830a..4391e20 100644 --- a/Assets/Scripts/Entities/SyncBases/Inventory.cs +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs @@ -66,7 +66,6 @@ namespace Cyber.Entities.SyncBases { /// /// public override void Deserialize(NetworkReader reader) { - Debug.Log("Deserializing inventory!"); byte[][] ByteArray = new byte[4][]; ByteArray[0] = reader.ReadBytesAndSize(); diff --git a/Assets/Scripts/Items/InventoryAction.cs b/Assets/Scripts/Items/InventoryAction.cs new file mode 100644 index 0000000..874d84b --- /dev/null +++ b/Assets/Scripts/Items/InventoryAction.cs @@ -0,0 +1,19 @@ + +namespace Cyber.Items { + + /// + /// Represents an inventory action + /// + public enum InventoryAction : byte { + + /// + /// Equip an item, given int is the ID of the item equipping + /// + Equip, + + /// + /// Unequip the given slot, removing anything that was inside of it. + /// + Unequip + } +} diff --git a/Assets/Scripts/Items/InventoryAction.cs.meta b/Assets/Scripts/Items/InventoryAction.cs.meta new file mode 100644 index 0000000..33f8d14 --- /dev/null +++ b/Assets/Scripts/Items/InventoryAction.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: d7c9b247469522d429ed7c2f00946978 +timeCreated: 1494797212 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/Clientside/Client.cs b/Assets/Scripts/Networking/Clientside/Client.cs index 9e0ba25..abd5326 100644 --- a/Assets/Scripts/Networking/Clientside/Client.cs +++ b/Assets/Scripts/Networking/Clientside/Client.cs @@ -1,6 +1,7 @@ using Cyber.Console; using Cyber.Entities; using Cyber.Entities.SyncBases; +using Cyber.Items; using Cyber.Networking.Messages; using System.Collections.Generic; using UnityEngine; @@ -161,6 +162,7 @@ namespace Cyber.Networking.Clientside { NetClient.RegisterHandler(PktType.Interact, HandlePacket); NetClient.RegisterHandler(PktType.StaticObjectIds, HandlePacket); NetClient.RegisterHandler(PktType.Disconnect, HandlePacket); + NetClient.RegisterHandler(PktType.InventoryAction, HandlePacket); NetClient.RegisterHandler(MsgType.Connect, OnConnected); NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected); @@ -265,6 +267,29 @@ namespace Cyber.Networking.Clientside { 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; + } + + switch (InventoryActionPkt.Action) { + case InventoryAction.Equip: + Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt); + Inventory.Equipped.SetSlot(Item.Slot, Item); + break; + case InventoryAction.Unequip: + EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt; + Inventory.Equipped.ClearSlot(Slot); + break; + } + break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType); diff --git a/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs b/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs new file mode 100644 index 0000000..3c7b32f --- /dev/null +++ b/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs @@ -0,0 +1,62 @@ + +using Cyber.Items; +using UnityEngine.Networking; + +namespace Cyber.Networking.Messages { + + /// + /// Packet containing an inventory action of a kind. + /// + public class InventoryActionPkt : MessageBase { + + /// + /// The inventory action. + /// + public InventoryAction Action; + + /// + /// The related int to the + /// + public int RelatedInt; + + /// + /// The inventory SyncBaseID this happened in. Only set by server. + /// + public int SyncBaseID; + + /// + /// Creates an inventory action packet for sending. + /// + /// The action done. + /// + public InventoryActionPkt(InventoryAction action, int relatedInt) { + Action = action; + RelatedInt = relatedInt; + } + + /// + /// Creates an inventory action packet for deserializing. + /// + public InventoryActionPkt() {} + + /// + /// Deserializes the + /// + /// + public override void Deserialize(NetworkReader reader) { + Action = (InventoryAction) reader.ReadByte(); + RelatedInt = reader.ReadInt32(); + SyncBaseID = reader.ReadInt32(); + } + + /// + /// Serializes the + /// + /// + public override void Serialize(NetworkWriter writer) { + writer.Write((byte) Action); + writer.Write(RelatedInt); + writer.Write(SyncBaseID); + } + } +} diff --git a/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs.meta b/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs.meta new file mode 100644 index 0000000..f80439c --- /dev/null +++ b/Assets/Scripts/Networking/Messages/InventoryActionPkt.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6f56a2a8158b89a4d8e6040c90dd76da +timeCreated: 1494797212 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/PktType.cs b/Assets/Scripts/Networking/PktType.cs index 644ba6b..1bf92b7 100644 --- a/Assets/Scripts/Networking/PktType.cs +++ b/Assets/Scripts/Networking/PktType.cs @@ -65,5 +65,10 @@ namespace Cyber.Networking { /// public const short FailedChecksums = 210; + /// + /// Packet containing an inventory action and an int relating somehow to this action. + /// + public const short InventoryAction = 211; + } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs index a56d980..e8e1cac 100644 --- a/Assets/Scripts/Networking/Serverside/Server.cs +++ b/Assets/Scripts/Networking/Serverside/Server.cs @@ -1,6 +1,7 @@ using Cyber.Console; using Cyber.Entities; using Cyber.Entities.SyncBases; +using Cyber.Items; using Cyber.Networking.Messages; using System.Collections.Generic; using UnityEngine; @@ -149,6 +150,7 @@ namespace Cyber.Networking.Serverside { NetworkServer.RegisterHandler(PktType.ClientSync, HandlePacket); NetworkServer.RegisterHandler(PktType.Disconnect, HandlePacket); NetworkServer.RegisterHandler(PktType.FailedChecksums, HandlePacket); + NetworkServer.RegisterHandler(PktType.InventoryAction, HandlePacket); NetworkServer.RegisterHandler(MsgType.Connect, OnConnected); NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected); @@ -240,6 +242,26 @@ namespace Cyber.Networking.Serverside { Syncer.DirtSyncBase(SyncBaseId); } break; + case PktType.InventoryAction: + InventoryActionPkt InventoryActionPkt = new InventoryActionPkt(); + InventoryActionPkt.Deserialize(msg.reader); + + Inventory CurrInventory = Players[msg.conn.connectionId].Character.GetComponent(); + InventoryActionPkt.SyncBaseID = CurrInventory.ID; + + switch (InventoryActionPkt.Action) { + case InventoryAction.Equip: + Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt); + CurrInventory.Equipped.SetSlot(Item.Slot, Item); + break; + case InventoryAction.Unequip: + EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt; + CurrInventory.Equipped.ClearSlot(Slot); + break; + } + + SendToAll(PktType.InventoryAction, InventoryActionPkt); + break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType);