RustToDust/client/LobbyScreen.cs

562 lines
23 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Globalization;
using Godot;
using RustToDust.Core;
namespace RustToDust.Client;
/// <summary>
/// Authoritative pre-match orbital lobby overlay (<see cref="MatchPhase.Lobby"/>).
/// Displays the 12 contender slots, ready states, host-only map/seed controls,
/// live 5-second orbital insertion countdown banner, and launch/leave actions.
/// </summary>
public sealed partial class LobbyScreen : CanvasLayer
{
private sealed class SlotRowView
{
public required PanelContainer Frame;
public required Label IndexLabel;
public required Label SquadBadge;
public required Label NameLabel;
public required Label RoleBadge;
public required Label ReadyBadge;
}
private static readonly Color[] SquadColors =
{
Color.FromHtml("#52e3a8"),
ImperialChrome.CyanCol,
ImperialChrome.GoldCol,
ImperialChrome.OrangeCol,
ImperialChrome.OliveCol,
Color.FromHtml("#d67cff")
};
private readonly SlotRowView[] _slotViews = new SlotRowView[MatchState.ContenderCount];
private Control _root = null!;
private ImperialChromePanel _panel = null!;
private Label _modeBadge = null!;
private PanelContainer _countdownBanner = null!;
private Label _countdownLabel = null!;
private Label _rosterSummaryLabel = null!;
private OptionButton _mapOption = null!;
private LineEdit _seedEdit = null!;
private OptionButton _squadOption = null!;
private Button _applyConfigBtn = null!;
private Button _randomSeedBtn = null!;
private Label _authorityNoteLabel = null!;
private Label _netStatusLabel = null!;
private Button _switchSquadBtn = null!;
private Button _readyBtn = null!;
private Button _launchBtn = null!;
private Button _leaveBtn = null!;
private IClientArenaSession? _activeSession;
private int? _lastSyncedSeed;
private byte? _lastSyncedMap;
private byte? _lastSyncedSquadSize;
public LobbyScreen()
{
Layer = 20;
}
public event Action? LeaveRequested;
public bool IsTextInputFocused => Visible && _seedEdit is not null && _seedEdit.HasFocus();
public override void _Ready()
{
BuildUi();
SetLobbyVisible(false);
}
public void SetLobbyVisible(bool visible)
{
Visible = visible;
if (_root is not null)
{
_root.Visible = visible;
}
if (!visible)
{
_activeSession = null;
_lastSyncedSeed = null;
_lastSyncedMap = null;
_lastSyncedSquadSize = null;
_seedEdit?.ReleaseFocus();
}
}
public void Sync(IClientArenaSession session)
{
_activeSession = session;
var net = session.ConnectionStatus;
var lobby = session.LobbyState;
_modeBadge.Text = net.Mode switch
{
NetworkMode.ListenHost => net.StateLabel,
NetworkMode.RemoteClient => net.Connected
? $"CONNECTED • PING {net.PingMs} ms"
: (net.LastError is not null ? $"DISCONNECTED: {net.LastError}" : "CONNECTING TO LOBBY..."),
_ => "SOLO"
};
_modeBadge.AddThemeColorOverride("font_color",
net.LastError is not null ? ImperialChrome.RedCol : (net.Connected ? ImperialChrome.CyanCol : ImperialChrome.OrangeCol));
if (lobby is null)
{
_countdownLabel.Text = net.LastError is not null
? $"CONNECTION LOST — {net.LastError}"
: "ESTABLISHING RUDP UPLINK TO SERVER LOBBY...";
_countdownLabel.AddThemeColorOverride("font_color", net.LastError is not null ? ImperialChrome.RedCol : ImperialChrome.OrangeCol);
_rosterSummaryLabel.Text = "Awaiting authoritative lobby roster...";
_readyBtn.Disabled = true;
_switchSquadBtn.Disabled = true;
_launchBtn.Visible = false;
_mapOption.Disabled = true;
_seedEdit.Editable = false;
_squadOption.Disabled = true;
_applyConfigBtn.Disabled = true;
_randomSeedBtn.Disabled = true;
_authorityNoteLabel.Text = "Waiting for server snapshot...";
_netStatusLabel.Text = net.StateLabel;
return;
}
int localId = session.LocalContenderId;
bool isHost = lobby.HostSlotId.HasValue && lobby.HostSlotId.Value == localId;
bool localReady = false;
byte localSquadId = 0;
int humanCount = 0;
int readyCount = 0;
int aiCount = 0;
byte squadSize = (byte)Math.Clamp((int)lobby.SquadSize, 1, 4);
for (int i = 0; i < _slotViews.Length; i++)
{
var view = _slotViews[i];
if (i < lobby.Slots.Count)
{
var slot = lobby.Slots[i];
bool isLocalSlot = slot.SlotId == localId;
bool isHostSlot = lobby.HostSlotId.HasValue && slot.SlotId == lobby.HostSlotId.Value;
if (isLocalSlot) localSquadId = slot.SquadId;
view.IndexLabel.Text = $"SLOT {slot.SlotId + 1:00}";
if (squadSize > 1)
{
view.SquadBadge.Text = $"[SQ {slot.SquadId + 1}]";
view.SquadBadge.AddThemeColorOverride("font_color", SquadColors[slot.SquadId % SquadColors.Length]);
}
else
{
view.SquadBadge.Text = "[FFA]";
view.SquadBadge.AddThemeColorOverride("font_color", ImperialChrome.TextMuted);
}
if (slot.IsHuman)
{
humanCount++;
if (slot.IsReady) readyCount++;
if (isLocalSlot) localReady = slot.IsReady;
view.NameLabel.Text = string.IsNullOrWhiteSpace(slot.Callsign) ? $"Pilot {slot.SlotId + 1}" : slot.Callsign;
view.NameLabel.AddThemeColorOverride("font_color", isLocalSlot ? ImperialChrome.TextLight : Colors.White);
view.RoleBadge.Text = isHostSlot
? (isLocalSlot ? "[HOST / YOU]" : "[HOST]")
: (isLocalSlot ? "[YOU]" : "[PILOT]");
view.RoleBadge.AddThemeColorOverride("font_color", isHostSlot ? ImperialChrome.GoldCol : ImperialChrome.CyanCol);
view.ReadyBadge.Text = slot.IsReady ? "[READY]" : "[WAIT]";
view.ReadyBadge.AddThemeColorOverride("font_color", slot.IsReady ? ImperialChrome.OliveCol : ImperialChrome.OrangeCol);
ApplyRowFrame(view.Frame, isLocalSlot ? ImperialChrome.CyanCol : ImperialChrome.BorderCol, highlighted: isLocalSlot);
}
else
{
aiCount++;
view.NameLabel.Text = "AI BACKFILL";
view.NameLabel.AddThemeColorOverride("font_color", ImperialChrome.TextMuted);
view.RoleBadge.Text = "[BOT]";
view.RoleBadge.AddThemeColorOverride("font_color", ImperialChrome.TextMuted);
view.ReadyBadge.Text = "[ARMED]";
view.ReadyBadge.AddThemeColorOverride("font_color", ImperialChrome.TextMuted);
ApplyRowFrame(view.Frame, new Color(ImperialChrome.BorderCol.R, ImperialChrome.BorderCol.G, ImperialChrome.BorderCol.B, 0.45f), highlighted: false);
}
}
}
_rosterSummaryLabel.Text = $"HUMANS: {humanCount}/{MatchState.ContenderCount} • READY: {readyCount}/{Math.Max(1, humanCount)} • AI BACKFILL: {aiCount}";
// Countdown banner
if (lobby.CountdownSeconds is float cd)
{
int ceilSec = Math.Max(1, (int)MathF.Ceiling(cd));
_countdownLabel.Text = $"ORBITAL INSERTION IN {ceilSec}...";
_countdownLabel.AddThemeColorOverride("font_color", ceilSec <= 2 ? ImperialChrome.OrangeCol : ImperialChrome.GoldCol);
_panel.SetAccentColor(ImperialChrome.GoldCol);
}
else
{
string readyHint = humanCount < lobby.MinHumansToAutoStart
? $"NEED {lobby.MinHumansToAutoStart} HUMANS FOR AUTO-LAUNCH"
: "ALL PILOTS READY UP TO BEGIN DROP";
_countdownLabel.Text = isHost
? $"PRE-MATCH STANDBY • {readyHint} (OR LAUNCH DROP)"
: $"PRE-MATCH STANDBY • {readyHint}";
_countdownLabel.AddThemeColorOverride("font_color", ImperialChrome.CyanCol);
_panel.SetAccentColor(ImperialChrome.CyanCol);
}
// Sync Map, Seed & SquadSize without clobbering active typing by host
if (_lastSyncedMap != lobby.SelectedMap)
{
_lastSyncedMap = lobby.SelectedMap;
_mapOption.Select((ArenaMapKind)lobby.SelectedMap == ArenaMapKind.Desert ? 1 : 0);
}
if (_lastSyncedSeed != lobby.SelectedSeed && (!_seedEdit.HasFocus() || !isHost))
{
_lastSyncedSeed = lobby.SelectedSeed;
_seedEdit.Text = lobby.SelectedSeed.ToString(CultureInfo.InvariantCulture);
}
if (_lastSyncedSquadSize != squadSize)
{
_lastSyncedSquadSize = squadSize;
_squadOption.Select(squadSize - 1);
}
// Strict host authority gating
_mapOption.Disabled = !isHost;
_seedEdit.Editable = isHost;
_squadOption.Disabled = !isHost;
_applyConfigBtn.Disabled = !isHost;
_randomSeedBtn.Disabled = !isHost;
_authorityNoteLabel.Text = isHost
? "HOST AUTHORITY: You control drop zone, seed, squad mode, and forced launch."
: "READ-ONLY: Only the match host can edit map, seed, squad mode, or force launch.";
_authorityNoteLabel.AddThemeColorOverride("font_color", isHost ? ImperialChrome.GoldCol : ImperialChrome.TextMuted);
_switchSquadBtn.Disabled = squadSize <= 1;
_switchSquadBtn.Text = squadSize > 1
? $"SWITCH SQUAD [SQ {localSquadId + 1}]"
: "SWITCH SQUAD (FFA)";
_readyBtn.Disabled = false;
_readyBtn.Text = localReady ? "READY [ON] — CLICK TO UNREADY" : "READY UP";
ImperialChrome.StyleButton(_readyBtn, localReady ? ImperialChrome.OliveCol : ImperialChrome.CyanCol, 14);
_launchBtn.Visible = isHost;
_launchBtn.Disabled = !isHost;
_launchBtn.Text = lobby.ForceLaunched ? "LAUNCHING NOW..." : "LAUNCH DROP";
_netStatusLabel.Text = $"{net.StateLabel} • MAP: {((ArenaMapKind)lobby.SelectedMap == ArenaMapKind.Sion ? "SION 3KM" : "DESERT RING")} • SEED {lobby.SelectedSeed}";
}
private void BuildUi()
{
_root = new Control
{
Name = "LobbyScreenRoot",
MouseFilter = Control.MouseFilterEnum.Stop
};
_root.SetAnchorsPreset(Control.LayoutPreset.FullRect);
AddChild(_root);
var scrim = new ColorRect
{
Color = new Color(0.03f, 0.04f, 0.03f, 0.36f),
MouseFilter = Control.MouseFilterEnum.Stop
};
scrim.SetAnchorsPreset(Control.LayoutPreset.FullRect);
_root.AddChild(scrim);
_panel = ImperialChrome.CreatePanel(ImperialChrome.CyanCol, cornerLen: 18f, borderPx: 2f, bgAlpha: 0.92f, padH: 18, padV: 16);
_panel.Name = "LobbyScreenPanel";
_panel.AnchorLeft = 0.5f;
_panel.AnchorRight = 0.5f;
_panel.AnchorTop = 0.5f;
_panel.AnchorBottom = 0.5f;
_panel.OffsetLeft = -515f;
_panel.OffsetRight = 515f;
_panel.OffsetTop = -330f;
_panel.OffsetBottom = 330f;
_root.AddChild(_panel);
var vbox = new VBoxContainer();
vbox.AddThemeConstantOverride("separation", 10);
_panel.AddChild(vbox);
// Header row
var headerRow = new HBoxContainer();
headerRow.AddThemeConstantOverride("separation", 12);
vbox.AddChild(headerRow);
var titleLbl = ImperialChrome.CreateLabel("PRE-MATCH ORBITAL LOBBY", 20, ImperialChrome.TextLight);
titleLbl.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
headerRow.AddChild(titleLbl);
_modeBadge = ImperialChrome.CreateLabel("HOST :3076", 12, ImperialChrome.CyanCol, HorizontalAlignment.Right);
headerRow.AddChild(_modeBadge);
// Countdown Banner
_countdownBanner = new PanelContainer();
var cdStyle = new StyleBoxFlat
{
BgColor = ImperialChrome.BgInner,
BorderColor = ImperialChrome.GoldCol,
ContentMarginLeft = 12,
ContentMarginRight = 12,
ContentMarginTop = 8,
ContentMarginBottom = 8
};
cdStyle.SetBorderWidthAll(1);
_countdownBanner.AddThemeStyleboxOverride("panel", cdStyle);
vbox.AddChild(_countdownBanner);
_countdownLabel = ImperialChrome.CreateLabel("PRE-MATCH STANDBY", 15, ImperialChrome.CyanCol, HorizontalAlignment.Center);
_countdownBanner.AddChild(_countdownLabel);
// Body: Two columns (Left Roster | Right Config & Actions)
var bodyRow = new HBoxContainer
{
SizeFlagsVertical = Control.SizeFlags.ExpandFill
};
bodyRow.AddThemeConstantOverride("separation", 16);
vbox.AddChild(bodyRow);
// Left Column: Roster (Slot 01..12)
var leftCol = new VBoxContainer
{
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill,
SizeFlagsStretchRatio = 1.35f
};
leftCol.AddThemeConstantOverride("separation", 4);
bodyRow.AddChild(leftCol);
leftCol.AddChild(ImperialChrome.CreateLabel("CONTENDER ROSTER (SLOTS 01 – 12)", 12, ImperialChrome.OrangeCol));
for (int i = 0; i < _slotViews.Length; i++)
{
var frame = new PanelContainer();
ApplyRowFrame(frame, ImperialChrome.BorderCol, highlighted: false);
var row = new HBoxContainer();
row.AddThemeConstantOverride("separation", 8);
frame.AddChild(row);
var idxLbl = ImperialChrome.CreateLabel($"SLOT {i + 1:00}", 11, ImperialChrome.TextMuted);
idxLbl.CustomMinimumSize = new Vector2(60f, 0f);
row.AddChild(idxLbl);
var sqLbl = ImperialChrome.CreateLabel("[SQ 1]", 11, SquadColors[0]);
sqLbl.CustomMinimumSize = new Vector2(54f, 0f);
row.AddChild(sqLbl);
var nameLbl = ImperialChrome.CreateLabel("AI BACKFILL", 12, ImperialChrome.TextMuted);
nameLbl.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
nameLbl.ClipText = true;
row.AddChild(nameLbl);
var roleLbl = ImperialChrome.CreateLabel("[BOT]", 11, ImperialChrome.TextMuted);
roleLbl.CustomMinimumSize = new Vector2(92f, 0f);
roleLbl.HorizontalAlignment = HorizontalAlignment.Right;
row.AddChild(roleLbl);
var readyLbl = ImperialChrome.CreateLabel("[ARMED]", 11, ImperialChrome.TextMuted);
readyLbl.CustomMinimumSize = new Vector2(72f, 0f);
readyLbl.HorizontalAlignment = HorizontalAlignment.Right;
row.AddChild(readyLbl);
leftCol.AddChild(frame);
_slotViews[i] = new SlotRowView
{
Frame = frame,
IndexLabel = idxLbl,
SquadBadge = sqLbl,
NameLabel = nameLbl,
RoleBadge = roleLbl,
ReadyBadge = readyLbl
};
}
_rosterSummaryLabel = ImperialChrome.CreateLabel("HUMANS: 1/12 • READY: 0/1 • AI BACKFILL: 11", 11, ImperialChrome.OliveCol);
leftCol.AddChild(_rosterSummaryLabel);
// Right Column: Drop Zone Config & Launch Actions
var rightCol = new VBoxContainer
{
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill,
SizeFlagsStretchRatio = 1.0f
};
rightCol.AddThemeConstantOverride("separation", 10);
bodyRow.AddChild(rightCol);
rightCol.AddChild(ImperialChrome.CreateLabel("DROP ZONE CONFIGURATION", 12, ImperialChrome.GoldCol));
rightCol.AddChild(ImperialChrome.MakeRule());
var mapRow = new HBoxContainer();
mapRow.AddThemeConstantOverride("separation", 8);
rightCol.AddChild(mapRow);
var mapLbl = ImperialChrome.CreateLabel("MAP", 12, ImperialChrome.TextMuted);
mapLbl.CustomMinimumSize = new Vector2(68f, 0f);
mapRow.AddChild(mapLbl);
_mapOption = new OptionButton { SizeFlagsHorizontal = Control.SizeFlags.ExpandFill };
_mapOption.AddItem("SION (3KM ALPINE)", 0);
_mapOption.AddItem("DESERT RING", 1);
ImperialChrome.StyleOptionButton(_mapOption, ImperialChrome.OliveCol, 13);
_mapOption.ItemSelected += _ => ApplyConfigFromUi();
mapRow.AddChild(_mapOption);
var seedRow = new HBoxContainer();
seedRow.AddThemeConstantOverride("separation", 8);
rightCol.AddChild(seedRow);
var seedLbl = ImperialChrome.CreateLabel("SEED", 12, ImperialChrome.TextMuted);
seedLbl.CustomMinimumSize = new Vector2(68f, 0f);
seedRow.AddChild(seedLbl);
_seedEdit = new LineEdit
{
Text = "42",
MaxLength = 12,
SizeFlagsHorizontal = Control.SizeFlags.ExpandFill
};
ImperialChrome.StyleLineEdit(_seedEdit, ImperialChrome.GoldCol, 13);
_seedEdit.TextSubmitted += _ => ApplyConfigFromUi();
seedRow.AddChild(_seedEdit);
_randomSeedBtn = ImperialChrome.CreateButton("RND", ImperialChrome.BorderCol, 12, 30f);
_randomSeedBtn.CustomMinimumSize = new Vector2(54f, 30f);
_randomSeedBtn.Pressed += () =>
{
int nextSeed = Random.Shared.Next(1, 999999);
_seedEdit.Text = nextSeed.ToString(CultureInfo.InvariantCulture);
ApplyConfigFromUi();
};
seedRow.AddChild(_randomSeedBtn);
var squadRow = new HBoxContainer();
squadRow.AddThemeConstantOverride("separation", 8);
rightCol.AddChild(squadRow);
var squadLbl = ImperialChrome.CreateLabel("SQUAD", 12, ImperialChrome.TextMuted);
squadLbl.CustomMinimumSize = new Vector2(68f, 0f);
squadRow.AddChild(squadLbl);
_squadOption = new OptionButton { SizeFlagsHorizontal = Control.SizeFlags.ExpandFill };
_squadOption.AddItem("FFA (1)", 0);
_squadOption.AddItem("DUO (2)", 1);
_squadOption.AddItem("TRIO (3)", 2);
_squadOption.AddItem("QUAD (4)", 3);
_squadOption.Select(2);
ImperialChrome.StyleOptionButton(_squadOption, ImperialChrome.CyanCol, 13);
_squadOption.ItemSelected += _ => ApplyConfigFromUi();
squadRow.AddChild(_squadOption);
_applyConfigBtn = ImperialChrome.CreateButton("APPLY MAP, SEED & SQUAD", ImperialChrome.GoldCol, 12, 32f);
_applyConfigBtn.Pressed += ApplyConfigFromUi;
rightCol.AddChild(_applyConfigBtn);
_authorityNoteLabel = ImperialChrome.CreateLabel("HOST AUTHORITY", 11, ImperialChrome.TextMuted);
_authorityNoteLabel.AutowrapMode = TextServer.AutowrapMode.Word;
rightCol.AddChild(_authorityNoteLabel);
rightCol.AddChild(ImperialChrome.MakeRule());
var spacer = new Control { SizeFlagsVertical = Control.SizeFlags.ExpandFill };
rightCol.AddChild(spacer);
_switchSquadBtn = ImperialChrome.CreateButton("SWITCH SQUAD [SQ 1]", SquadColors[0], 13, 36f);
_switchSquadBtn.Pressed += OnSwitchSquadPressed;
rightCol.AddChild(_switchSquadBtn);
_readyBtn = ImperialChrome.CreateButton("READY UP", ImperialChrome.CyanCol, 14, 42f);
_readyBtn.Pressed += OnReadyPressed;
rightCol.AddChild(_readyBtn);
_launchBtn = ImperialChrome.CreateButton("LAUNCH DROP", ImperialChrome.OliveCol, 14, 42f);
_launchBtn.Pressed += OnLaunchPressed;
rightCol.AddChild(_launchBtn);
_leaveBtn = ImperialChrome.CreateButton("LEAVE TO MENU", ImperialChrome.RedCol, 13, 36f);
_leaveBtn.Pressed += () => LeaveRequested?.Invoke();
rightCol.AddChild(_leaveBtn);
_netStatusLabel = ImperialChrome.CreateLabel(string.Empty, 11, ImperialChrome.TextMuted);
_netStatusLabel.AutowrapMode = TextServer.AutowrapMode.Word;
vbox.AddChild(_netStatusLabel);
}
private void OnReadyPressed()
{
if (_activeSession is null || _activeSession.LobbyState is not { } lobby) return;
int localId = _activeSession.LocalContenderId;
bool currentReady = false;
for (int i = 0; i < lobby.Slots.Count; i++)
{
if (lobby.Slots[i].SlotId == localId)
{
currentReady = lobby.Slots[i].IsReady;
break;
}
}
_activeSession.SetLobbyReady(!currentReady);
}
private void OnSwitchSquadPressed()
{
if (_activeSession is null || _activeSession.LobbyState is not { } lobby) return;
byte squadSize = (byte)Math.Clamp((int)lobby.SquadSize, 1, 4);
if (squadSize <= 1) return;
int maxSquads = Math.Max(1, (MatchState.ContenderCount + squadSize - 1) / squadSize);
int localId = _activeSession.LocalContenderId;
byte curSq = 0;
for (int i = 0; i < lobby.Slots.Count; i++)
{
if (lobby.Slots[i].SlotId == localId)
{
curSq = lobby.Slots[i].SquadId;
break;
}
}
byte nextSq = (byte)((curSq + 1) % maxSquads);
_activeSession.RequestLobbySquad(nextSq);
}
private void OnLaunchPressed()
{
_activeSession?.RequestLobbyStart();
}
private void ApplyConfigFromUi()
{
if (_activeSession is null || _activeSession.LobbyState is not { } lobby) return;
if (!lobby.HostSlotId.HasValue || lobby.HostSlotId.Value != _activeSession.LocalContenderId) return;
var mapKind = _mapOption.Selected == 1 ? ArenaMapKind.Desert : ArenaMapKind.Sion;
int seed = int.TryParse(_seedEdit.Text.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int parsed)
? parsed
: lobby.SelectedSeed;
byte squadSize = (byte)Math.Clamp(_squadOption.Selected + 1, 1, 4);
_seedEdit.ReleaseFocus();
_activeSession.RequestLobbyConfig(seed, mapKind, squadSize);
}
private static void ApplyRowFrame(PanelContainer frame, Color borderColor, bool highlighted)
{
var style = new StyleBoxFlat
{
BgColor = highlighted ? ImperialChrome.BgHover : ImperialChrome.BgInner,
BorderColor = borderColor,
ContentMarginLeft = 8,
ContentMarginRight = 8,
ContentMarginTop = 4,
ContentMarginBottom = 4
};
style.SetBorderWidthAll(1);
frame.AddThemeStyleboxOverride("panel", style);
}
}