quakeball/Assets/Scripts/Networking/Packets/PingPckt.cs
2020-08-08 20:37:53 +03:00

82 lines
2.3 KiB
C#

using System.Collections.Generic;
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Networking.Packets {
public class PingPckt : Packet {
public byte Identifier;
public bool ServerReceived = false;
public bool ClientReceived = false;
public PingPckt() { }
public PingPckt(byte identifier) {
Identifier = identifier;
}
public override void Read(ByteBuffer buffer) {
Identifier = buffer.Read();
byte result = buffer.Read();
ServerReceived = (result & 1) == 1;
ClientReceived = (result & 2) == 1;
}
public override void Write(ByteBuffer buffer) {
buffer.Write(Identifier);
byte serverFlag = ServerReceived ? (byte)1 : (byte)0;
byte clientFlag = ClientReceived ? (byte)2 : (byte)0;
byte total = (byte)(serverFlag | clientFlag);
buffer.Write(total);
}
}
public class PingListPckt : Packet {
public List<PingInfo> Pings = new List<PingInfo>();
public PingListPckt() { }
public PingListPckt(List<NetPlayer> players) {
foreach (NetPlayer p in players) {
Pings.Add(new PingInfo(p.Id, p.Ping));
}
}
public override void Read(ByteBuffer buffer) {
Pings.Clear();
int count = buffer.ReadInt();
for (int i = 0; i < count; i++) {
PingInfo info = new PingInfo(0, 0);
info.Read(buffer);
Pings.Add(info);
}
}
public override void Write(ByteBuffer buffer) {
buffer.Write(Pings.Count);
foreach (PingInfo info in Pings) {
info.Write(buffer);
}
}
}
public class PingInfo : Serializable {
public ulong PlayerId;
public float Ping;
public PingInfo(ulong playerid, float ping) {
PlayerId = playerid;
Ping = ping;
}
public void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
Ping = buffer.ReadFloat();
}
public void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
buffer.Write(Ping);
}
}
}