feat: theming pass, reworked connection flow...

- bracket view theming (pending rework)
- some light refactoring
- TOURNAMENT:CANCEL command (pending server side implementation)
- logic for waiting for last moves in games to wait for chip to drop
- transition back to main menu on disconnects, disconnect tolerance
This commit is contained in:
2025-12-07 01:24:13 -05:00
Unverified
parent 7ee74478c3
commit 95c3cef1be
12 changed files with 344 additions and 151 deletions

View File

@@ -1,29 +1,31 @@
using Godot;
using System;
using System.Collections.Generic;
public partial class AdminControls : HBoxContainer
{
[Export] public Button BecomeAdmin;
[Export] public Button StartTournament;
[Export] public Button CancelTournament;
[Export] public Label Label;
[Export] public Slider Timeout;
public override void _Ready()
{
Connection.Instance.OnBecomeAdmin += OnBecomeAdmin;
Connection.Instance.OnTournamentEnd += OnEndTournament;
Connection.Instance.OnTournamentEnd += _ => StartTournament.Show();
Connection.Instance.OnStartTournamentAck += () => StartTournament.Hide();
StartTournament.Pressed += StartTournamentCommand;
if (!Connection.Instance.IsAdmin || Connection.Instance.ActiveTournament)
{
StartTournament.Hide();
}
StartTournament.Pressed += () => Connection.Instance.StartTournament();
CancelTournament.Pressed += () => Connection.Instance.CancelTournament();
if (!Connection.Instance.IsAdmin)
{
StartTournament.Hide();
CancelTournament.Hide();
Label.Hide();
Timeout.Hide();
}
else if (Connection.Instance.ActiveTournament)
{
StartTournament.Hide();
}
Timeout.Value = Connection.Instance.CurrentWaitTimeout;
@@ -51,23 +53,12 @@ public partial class AdminControls : HBoxContainer
public override void _ExitTree()
{
Connection.Instance.OnBecomeAdmin -= OnBecomeAdmin;
Connection.Instance.OnTournamentEnd -= OnEndTournament;
}
private void StartTournamentCommand()
{
Connection.Instance.StartTournament();
}
private void OnEndTournament(List<(string, int)> playerScoreboard)
{
StartTournament.Show();
ShowTournamentScoreboard(playerScoreboard);
}
private void ShowAuthPopup()
{
var authWindow = new Window();
authWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
authWindow.AlwaysOnTop = true;
authWindow.MaximizeDisabled = true;
authWindow.Unresizable = true;
@@ -106,39 +97,6 @@ public partial class AdminControls : HBoxContainer
GetTree().Root.AddChild(authWindow);
}
private void ShowTournamentScoreboard(List<(string, int)> playerScoreboard)
{
var scoreboardWindow = new Window();
scoreboardWindow.AlwaysOnTop = true;
scoreboardWindow.MaximizeDisabled = true;
scoreboardWindow.Unresizable = true;
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
scoreboardWindow.Size = new Vector2I(256, 512);
scoreboardWindow.CloseRequested += () =>
{
GetTree().Root.RemoveChild(scoreboardWindow);
};
var tree = new Tree();
tree.HideRoot = true;
tree.Columns = 2;
tree.ColumnTitlesVisible = true;
tree.SetColumnTitle(0, "Player");
tree.SetColumnTitle(1, "Score");
var root = tree.CreateItem();
foreach ((string, int) entry in playerScoreboard)
{
var item = tree.CreateItem(root);
item.SetText(0, entry.Item1);
item.SetText(1, entry.Item2.ToString());
}
scoreboardWindow.AddChild(tree);
GetTree().Root.AddChild(scoreboardWindow);
}
private void OnBecomeAdmin()
{
BecomeAdmin.Hide();
@@ -146,6 +104,10 @@ public partial class AdminControls : HBoxContainer
{
StartTournament.Show();
}
else
{
CancelTournament.Show();
}
Label.Show();
Timeout.Show();
}

View File

@@ -1,5 +1,4 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,6 +6,7 @@ public partial class BoardScreen : Node2D
{
[Export] private AudioStream endingSfx;
[Export] private Theme theme;
private const string RED_CHIP_PATH = "res://scenes/red_chip.tscn";
private const string YELLOW_CHIP_PATH = "res://scenes/yellow_chip.tscn";
@@ -30,6 +30,10 @@ public partial class BoardScreen : Node2D
private RigidBody2D[,] chips = new RigidBody2D[6, 7]; // 6 rows 7 cols | 0, 0 is top left
private bool _lastMove = false;
private float _lastMoveTimer = 2.5f;
private string _winner = "";
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
// Node initialization
@@ -62,11 +66,12 @@ public partial class BoardScreen : Node2D
player2Card.GetNode<Label>("Status").Hide();
}
Connection.Instance.OnObserveWin += ObserveWin;
Connection.Instance.OnObserveDraw += ObserveDraw;
Connection.Instance.OnObserveTerminated += ObserveTerminated;
Connection.Instance.OnObserveWin += winner => { _lastMove = true; _winner = winner; };
Connection.Instance.OnObserveDraw += () => _lastMove = true;
Connection.Instance.OnObserveTerminated += () => PopupMessage("Match Terminated");
Connection.Instance.OnObserveMove += ObserveMove;
Connection.Instance.OnTournamentEnd += ShowTournamentScoreboard;
Connection.Instance.OnWSDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
}
public override void _Process(double delta)
@@ -90,13 +95,27 @@ public partial class BoardScreen : Node2D
{
currentTimeout -= delta;
}
if (_lastMove)
{
_lastMoveTimer -= (float) delta;
}
if (_lastMoveTimer <= 0.0f)
{
if (_winner == "")
{
PopupMessage("Draw!");
}
else
{
PopupMessage(_winner + " wins!");
}
}
}
public override void _ExitTree()
{
Connection.Instance.OnObserveWin -= ObserveWin;
Connection.Instance.OnObserveDraw -= ObserveDraw;
Connection.Instance.OnObserveTerminated -= ObserveTerminated;
Connection.Instance.OnObserveMove -= ObserveMove;
Connection.Instance.OnTournamentEnd -= ShowTournamentScoreboard;
}
@@ -116,27 +135,13 @@ public partial class BoardScreen : Node2D
Connection.Instance.PreviousMoves.Add((username, column));
}
private void ObserveWin(string winner)
{
PopupMessage(winner + " wins!");
}
private void ObserveDraw()
{
PopupMessage("Draw!");
}
private void ObserveTerminated()
{
PopupMessage("Match Terminated");
}
private void PopupMessage(string message)
{
var popup = new Popup();
popup.AlwaysOnTop = true;
popup.PopupCentered();
popup.Size = new Vector2I(200, 100);
popup.Theme = GD.Load<Theme>("res://assets/theme.tres");
var text = new Label();
text.Text = message;
var sfx = new AudioStreamPlayer();

View File

@@ -1,5 +1,4 @@
using Godot;
using System;
using System.Collections.Generic;
public partial class BracketScene : Control
@@ -32,6 +31,8 @@ public partial class BracketScene : Control
Connection.Instance.OnUpdatedPlayers += UpdatePlayers;
Connection.Instance.OnUpdatedMatches += UpdateMatches;
Connection.Instance.OnWatchGameAck += TransitionToBoard;
Connection.Instance.OnTournamentEnd += ShowTournamentScoreboard;
Connection.Instance.OnWSDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
}
public override void _ExitTree()
@@ -39,6 +40,7 @@ public partial class BracketScene : Control
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
Connection.Instance.OnUpdatedMatches -= UpdateMatches;
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
Connection.Instance.OnTournamentEnd -= ShowTournamentScoreboard;
}
private void UpdatePlayers(List<PlayerData> playerList)
@@ -106,4 +108,39 @@ public partial class BracketScene : Control
{
GetTree().ChangeSceneToFile(BOARD_SCENE_PATH);
}
private void ShowTournamentScoreboard(List<(string, int)> playerScoreboard)
{
var scoreboardWindow = new Window();
scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
scoreboardWindow.AlwaysOnTop = true;
scoreboardWindow.MaximizeDisabled = true;
scoreboardWindow.Unresizable = true;
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
scoreboardWindow.Size = new Vector2I(256, 512);
scoreboardWindow.CloseRequested += () =>
{
GetTree().Root.RemoveChild(scoreboardWindow);
};
var tree = new Tree();
tree.HideRoot = true;
tree.Columns = 2;
tree.ColumnTitlesVisible = true;
tree.Theme = GD.Load<Theme>("res://assets/theme.tres");
tree.SetColumnTitle(0, "Player");
tree.SetColumnTitle(1, "Score");
var root = tree.CreateItem();
foreach ((string, int) entry in playerScoreboard)
{
var item = tree.CreateItem(root);
item.SetText(0, entry.Item1);
item.SetText(1, entry.Item2.ToString());
}
scoreboardWindow.AddChild(tree);
GetTree().Root.AddChild(scoreboardWindow);
}
}

View File

@@ -1,23 +1,19 @@
using Godot;
using System;
public partial class ConnectButtonUI : Button
{
[Export] public TextEdit AddressUi;
[Export] public TextEdit AddressField;
[Export] public Label ErrorLabel;
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
public override void _Ready()
{
Connection.Instance.OnWSConnectionSuccess += () => GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
Connection.Instance.OnWSConnectionFailed += () => ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError;
}
public override void _Pressed()
{
if (Connection.Instance.Connect(AddressUi.Text))
{
GD.Print("Success!");
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
}
else
{
ErrorLabel.Text = "Couldn't connect to server!";
}
base._Pressed();
Connection.Instance.Connect(AddressField.Text);
}
}

View File

@@ -9,8 +9,7 @@ public partial class Connection : Node
public static Connection Instance { get; private set; }
private WebSocketPeer _webSocket = new WebSocketPeer();
private bool _firstConnect = true;
private WebSocketPeer _webSocket = new ();
private Thread _gameListThread;
private bool _gameListThreadRunning;
@@ -33,15 +32,18 @@ public partial class Connection : Node
public event Action OnStartTournamentAck;
public event Action<List<(string, int)>> OnTournamentEnd;
public event Action OnBecomeAdmin;
public event Action OnWSConnectionSuccess;
public event Action OnWSConnectionFailed;
public event Action OnWSDisconnect;
// Already prints to console
public event Action<string, string> OnError;
public event Action<string> OnError;
public bool IsAdmin { get; private set; }
public bool IsPlayer { get; private set; }
public bool ActiveTournament { get; private set; }
public List<(string, int)> PreviousMoves { get; private set; } = new List<(string, int)>();
public List<(string, int)> PreviousMoves { get; private set; } = [];
public double CurrentWaitTimeout { get; private set; } = 5.0;
@@ -49,6 +51,11 @@ public partial class Connection : Node
private bool IsSocketOpen => _webSocket.GetReadyState() == WebSocketPeer.State.Open;
private bool _connecting = false;
private bool _connected = false;
public String LastError = "";
public override void _Ready()
{
Instance = this;
@@ -56,37 +63,19 @@ public partial class Connection : Node
_webSocket.HeartbeatInterval = 5.0;
}
public bool Connect(string address)
public void Connect(string address)
{
if (_webSocket.GetReadyState() == WebSocketPeer.State.Open)
_connecting = true;
if (_connected)
{
return false;
return;
}
Error error = _webSocket.ConnectToUrl(address);
if (error != Error.Ok)
{
return false;
LastError = error.ToString();
}
_webSocket.Poll();
while (_webSocket.GetReadyState() == WebSocketPeer.State.Connecting)
{
_webSocket.Poll();
Thread.Sleep(TimeSpan.FromMilliseconds(5));
}
if (_webSocket.GetReadyState() != WebSocketPeer.State.Open)
{
return false;
}
_webSocket.SetHeartbeatInterval(5.0);
_webSocket.HeartbeatInterval = 5.0;
_firstConnect = false;
StartGameListRefreshLoop();
return true;
}
public override void _ExitTree()
@@ -94,24 +83,50 @@ public partial class Connection : Node
StopGameListRefreshLoop();
}
public override void _PhysicsProcess(double delta)
public override void _Process(double delta)
{
_webSocket.Poll();
WebSocketPeer.State state = _webSocket.GetReadyState();
if ((state == WebSocketPeer.State.Closed || state == WebSocketPeer.State.Closing) && !_firstConnect)
{
StopGameListRefreshLoop();
GD.PrintErr("Connection lost.");
//GetTree().Quit();
}
if (IsSocketOpen)
if (state == WebSocketPeer.State.Open)
{
if (_connecting)
{
_connecting = false;
_connected = true;
OnWSConnectionSuccess?.Invoke();
StartGameListRefreshLoop();
}
while (_webSocket.GetAvailablePacketCount() > 0)
{
string message = _webSocket.GetPacket().GetStringFromUtf8();
HandleServerMessage(message);
}
}
else if (state == WebSocketPeer.State.Connecting)
{
// Do nothing
}
else if (state == WebSocketPeer.State.Closing)
{
// Do nothing
}
else if (state == WebSocketPeer.State.Closed)
{
if (_connecting)
{
_connecting = false;
OnWSConnectionFailed?.Invoke();
}
else if (_connected)
{
_connected = false;
StopGameListRefreshLoop();
var code = _webSocket.GetCloseCode();
var reason = _webSocket.GetCloseReason();
GD.PrintErr("WebSocket closed with code: " + code + ", reason " + reason + ". Clean: " + (code != -1));
OnWSDisconnect?.Invoke();
}
}
}
@@ -220,6 +235,12 @@ public partial class Connection : Node
SendCommand("TOURNAMENT", "START");
}
public void CancelTournament()
{
if (!IsAdmin) return;
SendCommand("TOURNAMENT", "CANCEL");
}
public void TerminateGame(int matchID)
{
if (!IsAdmin) return;
@@ -317,8 +338,8 @@ public partial class Connection : Node
HandleTournamentMessage(body);
break;
case "ERROR":
HandleErrorMessage(body);
GD.PrintErr($"Error: {body}");
GD.PrintErr(message);
OnError?.Invoke(message);
break;
default:
GD.Print($"Unhandled server message: {message}");
@@ -495,19 +516,4 @@ public partial class Connection : Node
}
}
}
private void HandleErrorMessage(string body)
{
if (string.IsNullOrWhiteSpace(body))
{
OnError?.Invoke("UNKNOWN", string.Empty);
return;
}
string[] segments = body.Split(':');
string code = segments.Length > 0 ? segments[0].Trim().ToUpperInvariant() : "UNKNOWN";
string detail = segments.Length > 1 ? string.Join(":", segments, 1, segments.Length - 1).Trim() : string.Empty;
OnError?.Invoke(code, detail);
}
}