Ping measurement and display (console: ping)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+66
-1
@@ -64,6 +64,9 @@ namespace MSCO
|
||||
Steamworks.CSteamID pendingLobby; // Einladung, die noch angenommen werden muss
|
||||
Steamworks.CSteamID steamHost; // Client: Steam-ID des Hosts
|
||||
float nextPing;
|
||||
float pingMs = -1f; // geglättete Laufzeit zum Server/Host (-1 = noch unbekannt)
|
||||
const string PrefShowPing = "MSCO_ShowPing";
|
||||
GUIStyle pingStyle;
|
||||
float savingMessageUntil;
|
||||
GUIStyle hudStyle, savingStyle;
|
||||
Texture2D hudBackground;
|
||||
@@ -221,6 +224,48 @@ namespace MSCO
|
||||
return list;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Ping
|
||||
|
||||
/// <summary>Laufzeit zum Server/Host in ms (-1 = unbekannt, z.B. alter Server oder man ist selbst Host).</summary>
|
||||
public float PingMs { get { return pingMs; } }
|
||||
|
||||
/// <summary>Ping-Anzeige oben rechts (Konsole: "ping"), bleibt gespeichert.</summary>
|
||||
public bool ShowPing
|
||||
{
|
||||
get { return PlayerPrefs.GetInt(PrefShowPing, 0) == 1; }
|
||||
set { PlayerPrefs.SetInt(PrefShowPing, value ? 1 : 0); }
|
||||
}
|
||||
|
||||
/// <summary>Text für Anzeige und Konsole.</summary>
|
||||
public string PingText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (session == null) return "not connected";
|
||||
if (session.IsHost) return "you are the host";
|
||||
return pingMs < 0f ? "measuring..." : Mathf.RoundToInt(pingMs) + " ms";
|
||||
}
|
||||
}
|
||||
|
||||
void DrawPing()
|
||||
{
|
||||
if (!ShowPing || session == null) return;
|
||||
if (pingStyle == null)
|
||||
{
|
||||
pingStyle = UiTheme.Panel9(new Color(0.06f, 0.06f, 0.07f, 0.85f), 6);
|
||||
pingStyle.fontSize = 12;
|
||||
pingStyle.fontStyle = FontStyle.Bold;
|
||||
pingStyle.alignment = TextAnchor.MiddleCenter;
|
||||
pingStyle.padding = new RectOffset(10, 10, 0, 0);
|
||||
}
|
||||
// Farbe nach Qualität: grün < 80 ms, gelb < 150 ms, sonst rot
|
||||
Color color = pingMs < 0f ? UiTheme.Muted : pingMs < 80f ? UiTheme.Online : pingMs < 150f ? UiTheme.Pending : UiTheme.Danger;
|
||||
pingStyle.normal.textColor = color;
|
||||
var content = new GUIContent("PING " + PingText);
|
||||
float width = pingStyle.CalcSize(content).x;
|
||||
GUI.Box(new Rect(Screen.width - width - 12, 8, width, 24), content, pingStyle);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Cheats (Admin-Menü)
|
||||
|
||||
/// <summary>Cheats nur im Spiel und nur für Server-Admins bzw. den P2P-Host.</summary>
|
||||
@@ -637,7 +682,8 @@ namespace MSCO
|
||||
if (Time.realtimeSinceStartup >= nextPing)
|
||||
{
|
||||
nextPing = Time.realtimeSinceStartup + PingInterval;
|
||||
session.SendToAll(MsgType.Ping);
|
||||
float sentAt = Time.realtimeSinceStartup;
|
||||
session.SendToAll(MsgType.Ping, NetSession.Build(w => w.Write(sentAt))); // Zeitstempel für die Ping-Messung
|
||||
}
|
||||
|
||||
foreach (NetEvent e in session.Poll())
|
||||
@@ -704,6 +750,7 @@ namespace MSCO
|
||||
if (session != null && session.IsSteam) steam.Leave();
|
||||
session = null;
|
||||
handshakeDone = false;
|
||||
pingMs = -1f;
|
||||
teleportToHost = false;
|
||||
dedicated = false;
|
||||
connectingToServer = false;
|
||||
@@ -752,6 +799,22 @@ namespace MSCO
|
||||
{
|
||||
BinaryReader r = m.Reader();
|
||||
|
||||
// Ping-Messung: der Host schickt den Zeitstempel sofort zurück, der Client rechnet die Laufzeit aus
|
||||
if (m.Type == MsgType.Ping)
|
||||
{
|
||||
if (session.IsHost && m.Payload.Length >= 4) session.SendTo(peerId, MsgType.Pong, m.Payload);
|
||||
return;
|
||||
}
|
||||
if (m.Type == MsgType.Pong)
|
||||
{
|
||||
if (!session.IsHost && m.Payload.Length >= 4)
|
||||
{
|
||||
float rtt = (Time.realtimeSinceStartup - r.ReadSingle()) * 1000f;
|
||||
if (rtt >= 0f && rtt < 60000f) pingMs = pingMs < 0f ? rtt : Mathf.Lerp(pingMs, rtt, 0.4f);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsGameMessage(m.Type))
|
||||
{
|
||||
int from;
|
||||
@@ -1374,6 +1437,7 @@ namespace MSCO
|
||||
if (MenuHelper.InMenu)
|
||||
{
|
||||
lobby.Draw(this);
|
||||
DrawPing();
|
||||
overlays.Draw(this, false);
|
||||
return;
|
||||
}
|
||||
@@ -1383,6 +1447,7 @@ namespace MSCO
|
||||
GUI.Label(new Rect(0, Screen.height / 2f - 40, Screen.width, 80), "SAVING GAME...", savingStyle);
|
||||
|
||||
if (session != null) DrawHud();
|
||||
DrawPing();
|
||||
overlays.Draw(this, session != null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user