Network and potentially sync interactions

This commit is contained in:
Sofia 2017-05-11 04:01:33 +03:00
parent f92a1d6f8c
commit 8f8aec70e3
6 changed files with 77 additions and 20 deletions

View File

@ -13,7 +13,8 @@ namespace Cyber.Entities {
public class SyncDB : MonoBehaviour { public class SyncDB : MonoBehaviour {
private static readonly Type[] SyncableClasses = new Type[] { private static readonly Type[] SyncableClasses = new Type[] {
typeof(Character) typeof(Character),
typeof(BlinkyBox)
}; };
private int IDCounter = 0; private int IDCounter = 0;
@ -21,6 +22,8 @@ namespace Cyber.Entities {
private Dictionary<Type, List<int>> CategorizedDatabase = new Dictionary<Type, List<int>>(); private Dictionary<Type, List<int>> CategorizedDatabase = new Dictionary<Type, List<int>>();
private Dictionary<Type, SyncHandletype> SyncHandletypes = new Dictionary<Type, SyncHandletype>(); private Dictionary<Type, SyncHandletype> SyncHandletypes = new Dictionary<Type, SyncHandletype>();
private List<int> StaticSyncBaseIDList = new List<int>();
/// <summary> /// <summary>
/// Add an entity to the database with the given IDs. /// Add an entity to the database with the given IDs.
/// </summary> /// </summary>
@ -32,16 +35,8 @@ namespace Cyber.Entities {
for (int i = 0; i < SyncableClasses.Length; i++) { for (int i = 0; i < SyncableClasses.Length; i++) {
SyncBase Syncable = (SyncBase)gameObject.GetComponent(SyncableClasses[i]); SyncBase Syncable = (SyncBase)gameObject.GetComponent(SyncableClasses[i]);
if (Syncable != null) { if (Syncable != null) {
Syncable.ID = ids[Index]; Syncable.ID = ids[Index++];
Database[ids[Index++]] = Syncable; AddSyncBaseToDatabase(Syncable);
if (Server.IsRunning()) {
Type Type = Syncable.GetType();
if (!CategorizedDatabase.ContainsKey(Type)) {
CategorizedDatabase.Add(Type, new List<int>());
SyncHandletypes.Add(Type, Syncable.GetSyncHandletype());
}
CategorizedDatabase[Type].Add(Syncable.ID);
}
} }
} }
} }
@ -134,5 +129,49 @@ namespace Cyber.Entities {
} }
return ID; return ID;
} }
/// <summary>
/// Sets static objects for all objects in the world. This method should be called once per game launch ever.
/// </summary>
/// <param name="idList">The list of id's to be set. If null, will create new ids.</param>
public void SetStaticObjectsIDs(int[] idList = null) {
SyncBase[] SyncBases = FindObjectsOfType<SyncBase>();
Array.Sort(SyncBases, (a, b) => {
Vector3 APos = a.gameObject.transform.position;
float AComparison = APos.x + APos.y + APos.z + Array.IndexOf(SyncableClasses, a);
Vector3 BPos = b.gameObject.transform.position;
float BComparison = BPos.x + BPos.y + BPos.z + Array.IndexOf(SyncableClasses, b);
return AComparison.CompareTo(BComparison);
});
if (idList == null) {
foreach (SyncBase SyncBase in SyncBases) {
SyncBase.ID = CreateID();
AddSyncBaseToDatabase(SyncBase);
StaticSyncBaseIDList.Add(SyncBase.ID);
}
} else {
for (int i = 0; i < Math.Min(SyncBases.Length, idList.Length); i++) {
SyncBases[i].ID = idList[i];
AddSyncBaseToDatabase(SyncBases[i]);
}
}
}
public int[] GetStaticSyncBaseIDList() {
return StaticSyncBaseIDList.ToArray();
}
private void AddSyncBaseToDatabase(SyncBase syncBase) {
Database[syncBase.ID] = syncBase;
if (Server.IsRunning()) {
Type Type = syncBase.GetType();
if (!CategorizedDatabase.ContainsKey(Type)) {
CategorizedDatabase.Add(Type, new List<int>());
SyncHandletypes.Add(Type, syncBase.GetSyncHandletype());
}
CategorizedDatabase[Type].Add(syncBase.ID);
}
}
} }
} }

View File

@ -128,6 +128,7 @@ namespace Cyber.Networking.Clientside {
NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket); NetClient.RegisterHandler(PktType.MoveCreature, HandlePacket);
NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket); NetClient.RegisterHandler(PktType.SyncPacket, HandlePacket);
NetClient.RegisterHandler(PktType.InteractPkt, HandlePacket); NetClient.RegisterHandler(PktType.InteractPkt, HandlePacket);
NetClient.RegisterHandler(PktType.StaticObjectIdsPkt, HandlePacket);
NetClient.RegisterHandler(MsgType.Connect, OnConnected); NetClient.RegisterHandler(MsgType.Connect, OnConnected);
NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected); NetClient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
@ -161,7 +162,7 @@ namespace Cyber.Networking.Clientside {
Players.Add(Conn.ConnectionID, Conn); Players.Add(Conn.ConnectionID, Conn);
break; break;
case (PktType.MassIdentity): case (PktType.MassIdentity):
MassIdentityPkt Identities = new MassIdentityPkt(); IntListPkt Identities = new IntListPkt();
Identities.Deserialize(msg.reader); Identities.Deserialize(msg.reader);
foreach (int currId in Identities.IdList) { foreach (int currId in Identities.IdList) {
Players.Add(currId, new CConnectedPlayer(currId)); Players.Add(currId, new CConnectedPlayer(currId));
@ -210,6 +211,11 @@ namespace Cyber.Networking.Clientside {
case (PktType.SyncPacket): case (PktType.SyncPacket):
SyncHandler.HandleSyncPkt(msg); SyncHandler.HandleSyncPkt(msg);
break; break;
case (PktType.StaticObjectIdsPkt):
IntListPkt StaticIds = new IntListPkt();
StaticIds.Deserialize(msg.reader);
Spawner.SyncDB.SetStaticObjectsIDs(StaticIds.IdList);
break;
default: default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType); Debug.LogError("Received an unknown packet, id: " + msg.msgType);
Term.Println("Received an unknown packet, id: " + msg.msgType); Term.Println("Received an unknown packet, id: " + msg.msgType);

View File

@ -4,27 +4,27 @@ using UnityEngine.Networking;
namespace Cyber.Networking.Messages { namespace Cyber.Networking.Messages {
/// <summary> /// <summary>
/// Packet containing a list of ID's of players currently connected. /// Packet containing integers, used in many packet types, such as <see cref="PktType.MassIdentity"/> and <see cref="PktType.StaticObjectIdsPkt"/>.
/// </summary> /// </summary>
public class MassIdentityPkt : MessageBase { public class IntListPkt : MessageBase {
/// <summary> /// <summary>
/// List of Connection ID's to send /// List of Integers.
/// </summary> /// </summary>
public int[] IdList; public int[] IdList;
/// <summary> /// <summary>
/// Create a Mass Identity packet used to send a list of currently online connections. /// Create a packet containing integers.
/// </summary> /// </summary>
/// <param name="idList"></param> /// <param name="idList"></param>
public MassIdentityPkt(int[] idList) { public IntListPkt(int[] idList) {
IdList = idList; IdList = idList;
} }
/// <summary> /// <summary>
/// Parameter-less constructor using when deserializing the message. /// Parameter-less constructor using when deserializing the message.
/// </summary> /// </summary>
public MassIdentityPkt() { public IntListPkt() {
} }

View File

@ -18,6 +18,7 @@ namespace Cyber.Networking {
/// <summary> /// <summary>
/// Packet containing the identification details about everyone on the server before the client connected. /// Packet containing the identification details about everyone on the server before the client connected.
/// This packet contains an <see cref="IntListPkt"/>.
/// </summary> /// </summary>
public const short MassIdentity = 202; public const short MassIdentity = 202;
@ -43,5 +44,11 @@ namespace Cyber.Networking {
/// </summary> /// </summary>
public const short InteractPkt = 206; public const short InteractPkt = 206;
/// <summary>
/// Packet containing an id list of static objects existing in the ready game.
/// This packet contains an <see cref="IntListPkt"/>.
/// </summary>
public const short StaticObjectIdsPkt = 207;
} }
} }

View File

@ -113,6 +113,8 @@ namespace Cyber.Networking.Serverside {
Spawner = GetComponent<Spawner>(); Spawner = GetComponent<Spawner>();
Spawner.SyncDB.SetStaticObjectsIDs();
ConnectionConfig Config = new ConnectionConfig(); ConnectionConfig Config = new ConnectionConfig();
NetworkChannelID.ReliableSequenced = Config.AddChannel(QosType.ReliableSequenced); NetworkChannelID.ReliableSequenced = Config.AddChannel(QosType.ReliableSequenced);
NetworkChannelID.UnreliableSequenced = Config.AddChannel(QosType.UnreliableSequenced); NetworkChannelID.UnreliableSequenced = Config.AddChannel(QosType.UnreliableSequenced);
@ -218,7 +220,7 @@ namespace Cyber.Networking.Serverside {
} }
// Then send the client a list of all other clients // Then send the client a list of all other clients
NetworkServer.SendToClient(Id, PktType.MassIdentity, new MassIdentityPkt(IdList)); NetworkServer.SendToClient(Id, PktType.MassIdentity, new IntListPkt(IdList));
// Add the player to the list // Add the player to the list
SConnectedPlayer Player = new SConnectedPlayer(msg.conn.connectionId); SConnectedPlayer Player = new SConnectedPlayer(msg.conn.connectionId);
@ -228,7 +230,7 @@ namespace Cyber.Networking.Serverside {
NetworkServer.SendToClient(msg.conn.connectionId, NetworkServer.SendToClient(msg.conn.connectionId,
PktType.Identity, new IdentityPkt(msg.conn.connectionId, true)); PktType.Identity, new IdentityPkt(msg.conn.connectionId, true));
// Spawn the player and collet it's IDs // Spawn the player and collect it's IDs
Vector3 Position = new Vector3(0, 0, 0); Vector3 Position = new Vector3(0, 0, 0);
GameObject Obj = Spawner.Spawn(EntityType.NPC, Position); GameObject Obj = Spawner.Spawn(EntityType.NPC, Position);
int[] EntityIdList = Spawner.SyncDB.GetEntityIDs(Obj); int[] EntityIdList = Spawner.SyncDB.GetEntityIDs(Obj);
@ -236,6 +238,9 @@ namespace Cyber.Networking.Serverside {
NetworkServer.SendToAll(PktType.SpawnEntity, new SpawnEntityPkt(EntityType.NPC, Position, EntityIdList, Id)); NetworkServer.SendToAll(PktType.SpawnEntity, new SpawnEntityPkt(EntityType.NPC, Position, EntityIdList, Id));
// Send ID's of every existing static SyncBase object in the world.
NetworkServer.SendToClient(Id, PktType.StaticObjectIdsPkt, new IntListPkt(Spawner.SyncDB.GetStaticSyncBaseIDList()));
// Send every entity to the player who just connected. // Send every entity to the player who just connected.
foreach (var Entry in Players) { foreach (var Entry in Players) {
if (Entry.Key == Id) { if (Entry.Key == Id) {