Add PingListPckt

This commit is contained in:
Sofia 2020-08-08 20:37:53 +03:00
parent 2777f85d61
commit 717f558a9e
4 changed files with 66 additions and 2 deletions

View File

@ -19,6 +19,7 @@ namespace NeonTea.Quakeball.Networking {
Instance = instance;
RegisterPacket(typeof(HelloPckt));
RegisterPacket(typeof(PingPckt));
RegisterPacket(typeof(PingListPckt));
RegisterPacket(typeof(SpawnPckt));
RegisterPacket(typeof(SelfIdentPckt));
RegisterPacket(typeof(NicknamePckt));

View File

@ -94,6 +94,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
} else if (packet is NicknamePckt) {
NicknamePckt nick = (NicknamePckt)packet;
if (nick.PlayerId != LocalPlayer.Id) {
if (!Players.ContainsKey(nick.PlayerId)) {
Players.Add(nick.PlayerId, new NetPlayer(nick.PlayerId));
}
Players[nick.PlayerId].Nick = nick.Nick;
}
} else if (packet is SpawnPckt) {
@ -103,8 +106,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
}
if (spawn.IsInitial) {
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();
NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
Players.Add(spawn.PlayerId, player);
Players[spawn.PlayerId].Controlled = obj;
} else if (Players[spawn.PlayerId].Controlled.IsDead) {
if (Players.ContainsKey(spawn.PlayerId)) {
HandlePlayerRespawn(spawn.PlayerId, spawn.Location);
@ -143,6 +145,13 @@ namespace NeonTea.Quakeball.Networking.Instances {
}
UpdatePingBias();
}
} else if (packet is PingListPckt) {
PingListPckt pinglist = (PingListPckt)packet;
foreach (PingInfo info in pinglist.Pings) {
if (info.PlayerId != LocalPlayer.Id) {
Players[info.PlayerId].Ping = info.Ping;
}
}
} else if (packet is HitPckt) {
HitPckt hit = (HitPckt)packet;
if (Players[hit.Target].Controlled != null) {

View File

@ -131,6 +131,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
PingPckt ping = new PingPckt(LastPingIdent);
SendReliableToAll(ping);
LastSentPing = Time.time;
PingListPckt pinglist = new PingListPckt(PlayerList);
SendReliableToAll(pinglist);
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Networking.Packets {
@ -28,4 +29,54 @@ namespace NeonTea.Quakeball.Networking.Packets {
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);
}
}
}