Cyber/Assets/Scripts/Networking/Serverside/ServerSyncHandler.cs

52 lines
1.9 KiB
C#
Raw Normal View History

2017-05-11 21:00:52 +02:00

using Cyber.Entities.SyncBases;
using Cyber.Networking.Messages;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Serverside {
2017-05-11 21:20:10 +02:00
/// <summary>
/// The Server sync handler handles sync packets coming from the client, see <see cref="PktType.ClientSync"/>.
/// </summary>
2017-05-11 21:00:52 +02:00
public class ServerSyncHandler {
private Dictionary<int, int> LastSyncIDReceived = new Dictionary<int, int>();
private Dictionary<int, SConnectedPlayer> Players;
2017-05-11 21:20:10 +02:00
/// <summary>
/// Creates a nwe Server Sync handler for handling sync packets that are coming from the client.
/// </summary>
/// <param name="players"></param>
2017-05-11 21:00:52 +02:00
public ServerSyncHandler(Dictionary<int, SConnectedPlayer> players) {
Players = players;
}
/// <summary>
/// Handle the given sync from a client. Must be checked to be <see cref="PktType.ClientSync"/> first.
/// </summary>
/// <param name="message"></param>
public void HandleSyncPkt(NetworkMessage message) {
ClientSyncPkt SyncPkt = new ClientSyncPkt();
SyncPkt.Deserialize(message.reader);
int LastID = -1;
if (LastSyncIDReceived.ContainsKey(message.conn.connectionId)) {
LastID = LastSyncIDReceived[message.conn.connectionId];
}
if (SyncPkt.ClientSyncID > LastID) {
SyncPkt.ReadTheRest(message.reader);
LastSyncIDReceived[message.conn.connectionId] = SyncPkt.ClientSyncID;
Character PlayerCharacter = Players[message.conn.connectionId].Character;
PlayerCharacter.Move(SyncPkt.MoveDirection);
PlayerCharacter.SetRotation(SyncPkt.Rotation);
Debug.Log("MoveDirection: " + SyncPkt.MoveDirection);
}
// Disregard the package, it's too old.
}
}
}