feat: most of BracketScene done, need admin auth

This commit is contained in:
2025-12-03 21:36:33 -05:00
Unverified
parent 42cea6a1c4
commit 4e2c4bda5d
5 changed files with 577 additions and 501 deletions

View File

@@ -1,8 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://rl33x81cxlh0"]
[gd_scene load_steps=4 format=3 uid="uid://rl33x81cxlh0"]
[ext_resource type="Script" uid="uid://dm25u0a2lqk2x" path="res://scripts/BracketScene.cs" id="1_dvj3m"]
[ext_resource type="Texture2D" uid="uid://ckmfi0cjgxgyk" path="res://assets/sprites/RedChip.png" id="2_7c11m"]
[ext_resource type="PackedScene" uid="uid://m542qwlp7hl7" path="res://scenes/board_screen.tscn" id="3_1511b"]
[node name="BracketView" type="Control" node_paths=PackedStringArray("players")]
[node name="BracketView" type="Control" node_paths=PackedStringArray("Players", "Matches")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
@@ -10,14 +12,29 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_dvj3m")
players = NodePath("Tree")
Players = NodePath("HBoxContainer/PlayerList")
Matches = NodePath("HBoxContainer/MatchList")
JoinButton = ExtResource("2_7c11m")
BoardScene = ExtResource("3_1511b")
[node name="Tree" type="Tree" parent="."]
[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 9
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = 348.0
offset_top = 36.0
grow_horizontal = 2
grow_vertical = 2
[node name="PlayerList" type="Tree" parent="HBoxContainer"]
custom_minimum_size = Vector2(400, 0)
layout_mode = 2
columns = 3
column_titles_visible = true
scroll_horizontal_enabled = false
[node name="MatchList" type="Tree" parent="HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
columns = 3
column_titles_visible = true

View File

@@ -28,7 +28,7 @@ grow_vertical = 2
placeholder_text = "Server Address"
script = ExtResource("1_l6cm7")
[node name="Button" type="Button" parent="." node_paths=PackedStringArray("addressUI", "errorLabel")]
[node name="Button" type="Button" parent="." node_paths=PackedStringArray("AddressUi", "ErrorLabel")]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
@@ -43,9 +43,9 @@ grow_horizontal = 2
grow_vertical = 2
text = "Connect"
script = ExtResource("2_ekxnf")
addressUI = NodePath("../Address")
errorLabel = NodePath("../Label")
nextScene = ExtResource("3_bqqt6")
AddressUi = NodePath("../Address")
ErrorLabel = NodePath("../Label")
NextScene = ExtResource("3_bqqt6")
[node name="Label" type="Label" parent="."]
layout_mode = 1

View File

@@ -4,15 +4,31 @@ using System.Collections.Generic;
public partial class BracketScene : Control
{
[Export] public Tree players;
[Export] public Tree Players;
[Export] public Tree Matches;
[Export] public Texture2D JoinButton;
[Export] public PackedScene BoardScene;
private List<PlayerData> _playerList;
private List<MatchData> _matchList;
public override void _Ready()
{
players.SetColumnTitle(0, "Name");
players.SetColumnTitle(1, "Ready");
players.SetColumnTitle(2, "Playing");
Players.SetColumnTitle(0, "Name");
Players.SetColumnTitle(1, "Ready");
Players.SetColumnTitle(2, "Playing");
Players.HideRoot = true;
Players.ButtonClicked += KickPlayer;
Matches.SetColumnTitle(0, "Match");
Matches.SetColumnTitle(1, "Red");
Matches.SetColumnTitle(2, "Yellow");
Matches.HideRoot = true;
Matches.ButtonClicked += WatchGame;
Connection.Instance.OnUpdatedPlayers += UpdatePlayers;
Connection.Instance.OnUpdatedMatches += UpdateMatches;
Connection.Instance.OnBecomeAdmin += BecomeAdmin;
Connection.Instance.OnWatchGameAck += TransitionToBoard;
}
@@ -20,17 +36,57 @@ public partial class BracketScene : Control
public override void _ExitTree()
{
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
Connection.Instance.OnUpdatedMatches -= UpdateMatches;
Connection.Instance.OnBecomeAdmin -= BecomeAdmin;
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
}
private void UpdatePlayers(List<PlayerData> playerList)
{
players.Clear();
foreach (var playerData in playerList)
Players.Clear();
_playerList = playerList;
var root = Players.CreateItem();
for (int i = 0; i < _playerList.Count; i++)
{
var item = players.CreateItem();
item.SetText(0, playerData.username);
item.SetText(1, playerData.isReady ? "Yes" : "No");
item.SetText(2, playerData.isPlaying ? "Yes" : "No");
var item = Players.CreateItem(root);
item.SetText(0, playerList[i].username);
item.SetText(1, playerList[i].isReady ? "Yes" : "No");
item.SetText(2, playerList[i].isPlaying ? "Yes" : "No");
if (Connection.Instance.IsAdmin)
{
item.AddButton(0, JoinButton, i, false, "Kick"); // TODO
}
}
}
private void UpdateMatches(List<MatchData> matchList)
{
Matches.Clear();
_matchList = matchList;
var root = Matches.CreateItem();
for (int i = 0; i < matchList.Count; i++)
{
var item = Matches.CreateItem(root);
item.SetText(0, matchList[i].matchId.ToString());
item.SetText(1, matchList[i].player1);
item.SetText(2, matchList[i].player2);
item.AddButton(0, JoinButton, i, false, "Watch");
}
}
private void WatchGame(TreeItem item, long column, long id, long mouseButtonIndex)
{
if (mouseButtonIndex == 1 && column == 0)
{
Connection.Instance.SendWatchGame(_matchList[(int) id].matchId);
}
}
private void KickPlayer(TreeItem item, long column, long id, long mouseButtonIndex)
{
if (mouseButtonIndex == 1 && column == 0)
{
Connection.Instance.KickPlayer(_playerList[(int) id].username);
}
}
@@ -42,5 +98,7 @@ public partial class BracketScene : Control
private void TransitionToBoard()
{
// TODO
GD.Print("Watch game worked!");
GetTree().ChangeSceneToPacked(BoardScene);
}
}

View File

@@ -3,20 +3,20 @@ using System;
public partial class ConnectButtonUI : Button
{
[Export] public TextEdit addressUI;
[Export] public Label errorLabel;
[Export] public PackedScene nextScene;
[Export] public TextEdit AddressUi;
[Export] public Label ErrorLabel;
[Export] public PackedScene NextScene;
public override void _Pressed()
{
if (Connection.Instance.Connect(addressUI.Text))
if (Connection.Instance.Connect(AddressUi.Text))
{
GD.Print("Success!");
GetTree().ChangeSceneToPacked(nextScene);
GetTree().ChangeSceneToPacked(NextScene);
}
else
{
errorLabel.Text = "Couldn't connect to server!";
ErrorLabel.Text = "Couldn't connect to server!";
}
base._Pressed();
}

View File

@@ -69,7 +69,7 @@ public partial class Connection : Node
while (_webSocket.GetReadyState() == WebSocketPeer.State.Connecting)
{
_webSocket.Poll();
Thread.Sleep(TimeSpan.FromMilliseconds(17));
Thread.Sleep(TimeSpan.FromMilliseconds(5));
}
if (_webSocket.GetReadyState() != WebSocketPeer.State.Open)
@@ -80,6 +80,7 @@ public partial class Connection : Node
_webSocket.SetHeartbeatInterval(5.0);
_webSocket.HeartbeatInterval = 5.0;
_firstConnect = false;
StartGameListRefreshLoop();
return true;
}
@@ -92,15 +93,15 @@ public partial class Connection : Node
{
_webSocket.Poll();
WebSocketPeer.State state = _webSocket.GetReadyState();
if (state == WebSocketPeer.State.Closed && !_firstConnect)
if ((state == WebSocketPeer.State.Closed || state == WebSocketPeer.State.Closing) && !_firstConnect)
{
StopGameListRefreshLoop();
GetTree().Quit();
GD.PrintErr("Connection lost.");
//GetTree().Quit();
}
if (IsSocketOpen)
{
StartGameListRefreshLoop();
while (_webSocket.GetAvailablePacketCount() > 0)
{
string message = _webSocket.GetPacket().GetStringFromUtf8();