diff --git a/MCGalaxy/Commands/CPE/CmdPing.cs b/MCGalaxy/Commands/CPE/CmdPing.cs index f468a2230..a04ccdd2d 100644 --- a/MCGalaxy/Commands/CPE/CmdPing.cs +++ b/MCGalaxy/Commands/CPE/CmdPing.cs @@ -30,18 +30,20 @@ namespace MCGalaxy.Commands.Chatting { if (p.IsSuper) { p.Message("Super users cannot measure their own ping."); return; } if (!p.hasTwoWayPing) { - p.Message("Your client does not support measuring ping. You may need to update it."); + p.Message("Your client does not support measuring ping."); + } else if (p.Ping.Measures() == 0) { + p.Message("No ping measurements yet. Try again in a bit."); } else { - p.Message(p.Ping.Format()); + p.Message(p.Ping.Format()); } } else { - if (!CheckExtraPerm(p, data, 1)) return; + if (!CheckExtraPerm(p, data, 1)) return; Player[] players = PlayerInfo.Online.Items; p.Message("Ping/latency list for online players:"); foreach (Player pl in players) { if (!Entities.CanSee(data, p, pl)) continue; - if (pl.Ping.AveragePingMilliseconds() == 0) continue; + if (pl.Ping.Measures() == 0) continue; p.Message(pl.ColoredName + " %S- " + pl.Ping.Format()); } } diff --git a/MCGalaxy/Network/Utils/PingList.cs b/MCGalaxy/Network/Utils/PingList.cs index 017c7d22c..8521dea48 100644 --- a/MCGalaxy/Network/Utils/PingList.cs +++ b/MCGalaxy/Network/Utils/PingList.cs @@ -65,42 +65,47 @@ namespace MCGalaxy.Network { } - /// Gets lowest (best) ping in milliseconds, or 0 if no ping measures. - public int LowestPingMilliseconds() { - double totalMs = 100000000; - foreach (PingEntry ping in Entries) { - if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue; - totalMs = Math.Min(totalMs, ping.Latency); - } - return (int)totalMs; + bool Valid(int i) { + PingEntry e = Entries[i]; + return e.TimeSent.Ticks != 0 && e.TimeReceived.Ticks != 0; } - /// Gets average ping in milliseconds, or 0 if no ping measures. - public int AveragePingMilliseconds() { - double totalMs = 0; + public int Measures() { int measures = 0; - foreach (PingEntry ping in Entries) { - if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue; - totalMs += ping.Latency; measures++; + for (int i = 0; i < Entries.Length; i++) { + if (Valid(i)) measures++; } - return measures == 0 ? 0 : (int)(totalMs / measures); + return measures; } - /// Gets worst ping in milliseconds, or 0 if no ping measures. - public double HighestPingMilliseconds() { - double totalMs = 0; - foreach (PingEntry ping in Entries) { - if (ping.TimeSent.Ticks == 0 || ping.TimeReceived.Ticks == 0) continue; - totalMs = Math.Max(totalMs, ping.Latency); + public int LowestPing() { + double ms = 100000000; + for (int i = 0; i < Entries.Length; i++) { + if (Valid(i)) { ms = Math.Min(ms, Entries[i].Latency); } } - return (int)totalMs; + return (int)ms; + } + + public int AveragePing() { + double ms = 0; + int measures = 0; + for (int i = 0; i < Entries.Length; i++) { + if (Valid(i)) { ms += Entries[i].Latency; measures++; } + } + return measures == 0 ? 0 : (int)(ms / measures); + } + + public int HighestPing() { + double ms = 0; + for (int i = 0; i < Entries.Length; i++) { + if (Valid(i)) { ms = Math.Max(ms, Entries[i].Latency); } + } + return (int)ms; } public string Format() { return string.Format("Lowest ping {0}ms, average {1}ms, highest {2}ms", - LowestPingMilliseconds(), - AveragePingMilliseconds(), - HighestPingMilliseconds()); + LowestPing(), AveragePing(), HighestPing()); } } } \ No newline at end of file