Send InventoryActions across space and time

This commit is contained in:
Sofia 2017-05-15 00:35:44 +03:00
parent 3a8eac7125
commit c1af15fb80
9 changed files with 162 additions and 1 deletions

View File

@ -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;

View File

@ -66,7 +66,6 @@ namespace Cyber.Entities.SyncBases {
/// </summary>
/// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) {
Debug.Log("Deserializing inventory!");
byte[][] ByteArray = new byte[4][];
ByteArray[0] = reader.ReadBytesAndSize();

View File

@ -0,0 +1,19 @@

namespace Cyber.Items {
/// <summary>
/// Represents an inventory action
/// </summary>
public enum InventoryAction : byte {
/// <summary>
/// Equip an item, given int is the ID of the item equipping
/// </summary>
Equip,
/// <summary>
/// Unequip the given slot, removing anything that was inside of it.
/// </summary>
Unequip
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d7c9b247469522d429ed7c2f00946978
timeCreated: 1494797212
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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);

View File

@ -0,0 +1,62 @@

using Cyber.Items;
using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
/// <summary>
/// Packet containing an inventory action of a kind.
/// </summary>
public class InventoryActionPkt : MessageBase {
/// <summary>
/// The inventory action.
/// </summary>
public InventoryAction Action;
/// <summary>
/// The related int to the <see cref="InventoryAction"/>
/// </summary>
public int RelatedInt;
/// <summary>
/// The inventory SyncBaseID this happened in. Only set by server.
/// </summary>
public int SyncBaseID;
/// <summary>
/// Creates an inventory action packet for sending.
/// </summary>
/// <param name="action">The action done.</param>
/// <param name="relatedInt"></param>
public InventoryActionPkt(InventoryAction action, int relatedInt) {
Action = action;
RelatedInt = relatedInt;
}
/// <summary>
/// Creates an inventory action packet for deserializing.
/// </summary>
public InventoryActionPkt() {}
/// <summary>
/// Deserializes the <see cref="InventoryActionPkt"/>
/// </summary>
/// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) {
Action = (InventoryAction) reader.ReadByte();
RelatedInt = reader.ReadInt32();
SyncBaseID = reader.ReadInt32();
}
/// <summary>
/// Serializes the <see cref="InventoryActionPkt"/>
/// </summary>
/// <param name="writer"></param>
public override void Serialize(NetworkWriter writer) {
writer.Write((byte) Action);
writer.Write(RelatedInt);
writer.Write(SyncBaseID);
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6f56a2a8158b89a4d8e6040c90dd76da
timeCreated: 1494797212
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -65,5 +65,10 @@ namespace Cyber.Networking {
/// </summary>
public const short FailedChecksums = 210;
/// <summary>
/// Packet containing an inventory action and an int relating somehow to this action.
/// </summary>
public const short InventoryAction = 211;
}
}

View File

@ -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<Inventory>();
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);