From 717f558a9e9aef9edfecd4febd064ae6f5ffbb25 Mon Sep 17 00:00:00 2001 From: teascade Date: Sat, 8 Aug 2020 20:37:53 +0300 Subject: [PATCH] Add PingListPckt --- Assets/Scripts/Networking/GameProtocol.cs | 1 + Assets/Scripts/Networking/Instances/Client.cs | 13 ++++- Assets/Scripts/Networking/Instances/Server.cs | 3 ++ Assets/Scripts/Networking/Packets/PingPckt.cs | 51 +++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Networking/GameProtocol.cs b/Assets/Scripts/Networking/GameProtocol.cs index 1b1d2f4..8347d50 100644 --- a/Assets/Scripts/Networking/GameProtocol.cs +++ b/Assets/Scripts/Networking/GameProtocol.cs @@ -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)); diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index 3cac4f2..3b99c24 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -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(); - 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) { diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index 7de81c5..30ad72a 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -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); } } diff --git a/Assets/Scripts/Networking/Packets/PingPckt.cs b/Assets/Scripts/Networking/Packets/PingPckt.cs index 150f274..196bdf8 100644 --- a/Assets/Scripts/Networking/Packets/PingPckt.cs +++ b/Assets/Scripts/Networking/Packets/PingPckt.cs @@ -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 Pings = new List(); + + public PingListPckt() { } + public PingListPckt(List 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); + } + } } \ No newline at end of file