feat: most of BracketScene done, need admin auth
This commit is contained in:
@@ -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="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
|
layout_mode = 3
|
||||||
anchors_preset = 15
|
anchors_preset = 15
|
||||||
anchor_right = 1.0
|
anchor_right = 1.0
|
||||||
@@ -10,14 +12,29 @@ anchor_bottom = 1.0
|
|||||||
grow_horizontal = 2
|
grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
script = ExtResource("1_dvj3m")
|
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
|
layout_mode = 1
|
||||||
anchors_preset = 9
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
anchor_bottom = 1.0
|
anchor_bottom = 1.0
|
||||||
offset_right = 348.0
|
offset_top = 36.0
|
||||||
|
grow_horizontal = 2
|
||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
|
|
||||||
|
[node name="PlayerList" type="Tree" parent="HBoxContainer"]
|
||||||
|
custom_minimum_size = Vector2(400, 0)
|
||||||
|
layout_mode = 2
|
||||||
columns = 3
|
columns = 3
|
||||||
column_titles_visible = true
|
column_titles_visible = true
|
||||||
scroll_horizontal_enabled = false
|
scroll_horizontal_enabled = false
|
||||||
|
|
||||||
|
[node name="MatchList" type="Tree" parent="HBoxContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
size_flags_horizontal = 3
|
||||||
|
columns = 3
|
||||||
|
column_titles_visible = true
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ grow_vertical = 2
|
|||||||
placeholder_text = "Server Address"
|
placeholder_text = "Server Address"
|
||||||
script = ExtResource("1_l6cm7")
|
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
|
layout_mode = 1
|
||||||
anchors_preset = 8
|
anchors_preset = 8
|
||||||
anchor_left = 0.5
|
anchor_left = 0.5
|
||||||
@@ -43,9 +43,9 @@ grow_horizontal = 2
|
|||||||
grow_vertical = 2
|
grow_vertical = 2
|
||||||
text = "Connect"
|
text = "Connect"
|
||||||
script = ExtResource("2_ekxnf")
|
script = ExtResource("2_ekxnf")
|
||||||
addressUI = NodePath("../Address")
|
AddressUi = NodePath("../Address")
|
||||||
errorLabel = NodePath("../Label")
|
ErrorLabel = NodePath("../Label")
|
||||||
nextScene = ExtResource("3_bqqt6")
|
NextScene = ExtResource("3_bqqt6")
|
||||||
|
|
||||||
[node name="Label" type="Label" parent="."]
|
[node name="Label" type="Label" parent="."]
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
|
|||||||
@@ -4,15 +4,31 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
public partial class BracketScene : Control
|
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()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
players.SetColumnTitle(0, "Name");
|
Players.SetColumnTitle(0, "Name");
|
||||||
players.SetColumnTitle(1, "Ready");
|
Players.SetColumnTitle(1, "Ready");
|
||||||
players.SetColumnTitle(2, "Playing");
|
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.OnUpdatedPlayers += UpdatePlayers;
|
||||||
|
Connection.Instance.OnUpdatedMatches += UpdateMatches;
|
||||||
Connection.Instance.OnBecomeAdmin += BecomeAdmin;
|
Connection.Instance.OnBecomeAdmin += BecomeAdmin;
|
||||||
Connection.Instance.OnWatchGameAck += TransitionToBoard;
|
Connection.Instance.OnWatchGameAck += TransitionToBoard;
|
||||||
}
|
}
|
||||||
@@ -20,17 +36,57 @@ public partial class BracketScene : Control
|
|||||||
public override void _ExitTree()
|
public override void _ExitTree()
|
||||||
{
|
{
|
||||||
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
|
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
|
||||||
|
Connection.Instance.OnUpdatedMatches -= UpdateMatches;
|
||||||
|
Connection.Instance.OnBecomeAdmin -= BecomeAdmin;
|
||||||
|
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdatePlayers(List<PlayerData> playerList)
|
private void UpdatePlayers(List<PlayerData> playerList)
|
||||||
{
|
{
|
||||||
players.Clear();
|
Players.Clear();
|
||||||
foreach (var playerData in playerList)
|
_playerList = playerList;
|
||||||
|
var root = Players.CreateItem();
|
||||||
|
for (int i = 0; i < _playerList.Count; i++)
|
||||||
{
|
{
|
||||||
var item = players.CreateItem();
|
var item = Players.CreateItem(root);
|
||||||
item.SetText(0, playerData.username);
|
item.SetText(0, playerList[i].username);
|
||||||
item.SetText(1, playerData.isReady ? "Yes" : "No");
|
item.SetText(1, playerList[i].isReady ? "Yes" : "No");
|
||||||
item.SetText(2, playerData.isPlaying ? "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()
|
private void TransitionToBoard()
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
|
GD.Print("Watch game worked!");
|
||||||
|
GetTree().ChangeSceneToPacked(BoardScene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,20 @@ using System;
|
|||||||
|
|
||||||
public partial class ConnectButtonUI : Button
|
public partial class ConnectButtonUI : Button
|
||||||
{
|
{
|
||||||
[Export] public TextEdit addressUI;
|
[Export] public TextEdit AddressUi;
|
||||||
[Export] public Label errorLabel;
|
[Export] public Label ErrorLabel;
|
||||||
[Export] public PackedScene nextScene;
|
[Export] public PackedScene NextScene;
|
||||||
|
|
||||||
public override void _Pressed()
|
public override void _Pressed()
|
||||||
{
|
{
|
||||||
if (Connection.Instance.Connect(addressUI.Text))
|
if (Connection.Instance.Connect(AddressUi.Text))
|
||||||
{
|
{
|
||||||
GD.Print("Success!");
|
GD.Print("Success!");
|
||||||
GetTree().ChangeSceneToPacked(nextScene);
|
GetTree().ChangeSceneToPacked(NextScene);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
errorLabel.Text = "Couldn't connect to server!";
|
ErrorLabel.Text = "Couldn't connect to server!";
|
||||||
}
|
}
|
||||||
base._Pressed();
|
base._Pressed();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,478 +5,479 @@ using System.Threading;
|
|||||||
|
|
||||||
public partial class Connection : Node
|
public partial class Connection : Node
|
||||||
{
|
{
|
||||||
public const string WS_DEFAULT_ADDRESS = "wss://connect4.abunchofknowitalls.com";
|
public const string WS_DEFAULT_ADDRESS = "wss://connect4.abunchofknowitalls.com";
|
||||||
|
|
||||||
public static Connection Instance { get; private set; }
|
public static Connection Instance { get; private set; }
|
||||||
|
|
||||||
private WebSocketPeer _webSocket = new WebSocketPeer();
|
private WebSocketPeer _webSocket = new WebSocketPeer();
|
||||||
private bool _firstConnect = true;
|
private bool _firstConnect = true;
|
||||||
private Thread _gameListThread;
|
private Thread _gameListThread;
|
||||||
private bool _gameListThreadRunning;
|
private bool _gameListThreadRunning;
|
||||||
|
|
||||||
public event Action OnConnected;
|
public event Action OnConnected;
|
||||||
public event Action OnReadyAcknowledged;
|
public event Action OnReadyAcknowledged;
|
||||||
public event Action<bool> OnGameStart;
|
public event Action<bool> OnGameStart;
|
||||||
public event Action OnGameWin;
|
public event Action OnGameWin;
|
||||||
public event Action OnGameLoss;
|
public event Action OnGameLoss;
|
||||||
public event Action OnGameDraw;
|
public event Action OnGameDraw;
|
||||||
public event Action OnGameTerminated;
|
public event Action OnGameTerminated;
|
||||||
public event Action<int> OnOpponentMove;
|
public event Action<int> OnOpponentMove;
|
||||||
|
|
||||||
public event Action OnWatchGameAck;
|
public event Action OnWatchGameAck;
|
||||||
public event Action<string> OnObserveWin;
|
public event Action<string> OnObserveWin;
|
||||||
public event Action OnObserveDraw;
|
public event Action OnObserveDraw;
|
||||||
public event Action OnObserveTerminated;
|
public event Action OnObserveTerminated;
|
||||||
public event Action<string, int> OnObserveMove;
|
public event Action<string, int> OnObserveMove;
|
||||||
public event Action<List<MatchData>> OnUpdatedMatches;
|
public event Action<List<MatchData>> OnUpdatedMatches;
|
||||||
public event Action<List<PlayerData>> OnUpdatedPlayers;
|
public event Action<List<PlayerData>> OnUpdatedPlayers;
|
||||||
public event Action<List<(string, int)>> OnTournamentEnd;
|
public event Action<List<(string, int)>> OnTournamentEnd;
|
||||||
public event Action OnBecomeAdmin;
|
public event Action OnBecomeAdmin;
|
||||||
|
|
||||||
|
|
||||||
// Already prints to console
|
// Already prints to console
|
||||||
public event Action<string, string> OnError;
|
public event Action<string, string> OnError;
|
||||||
|
|
||||||
public bool IsAdmin { get; private set; }
|
public bool IsAdmin { get; private set; }
|
||||||
public bool IsPlayer { get; private set; }
|
public bool IsPlayer { get; private set; }
|
||||||
|
|
||||||
public MatchData CurrentObservingMatch { get; private set; }
|
public MatchData CurrentObservingMatch { get; private set; }
|
||||||
|
|
||||||
private bool IsSocketOpen => _webSocket.GetReadyState() == WebSocketPeer.State.Open;
|
private bool IsSocketOpen => _webSocket.GetReadyState() == WebSocketPeer.State.Open;
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
Instance = this;
|
Instance = this;
|
||||||
_webSocket.SetHeartbeatInterval(5.0);
|
_webSocket.SetHeartbeatInterval(5.0);
|
||||||
_webSocket.HeartbeatInterval = 5.0;
|
_webSocket.HeartbeatInterval = 5.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Connect(string address)
|
public bool Connect(string address)
|
||||||
{
|
{
|
||||||
if (_webSocket.GetReadyState() == WebSocketPeer.State.Open)
|
if (_webSocket.GetReadyState() == WebSocketPeer.State.Open)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Error error = _webSocket.ConnectToUrl(address);
|
Error error = _webSocket.ConnectToUrl(address);
|
||||||
if (error != Error.Ok)
|
if (error != Error.Ok)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_webSocket.Poll();
|
_webSocket.Poll();
|
||||||
|
|
||||||
while (_webSocket.GetReadyState() == WebSocketPeer.State.Connecting)
|
while (_webSocket.GetReadyState() == WebSocketPeer.State.Connecting)
|
||||||
{
|
{
|
||||||
_webSocket.Poll();
|
_webSocket.Poll();
|
||||||
Thread.Sleep(TimeSpan.FromMilliseconds(17));
|
Thread.Sleep(TimeSpan.FromMilliseconds(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_webSocket.GetReadyState() != WebSocketPeer.State.Open)
|
if (_webSocket.GetReadyState() != WebSocketPeer.State.Open)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_webSocket.SetHeartbeatInterval(5.0);
|
_webSocket.SetHeartbeatInterval(5.0);
|
||||||
_webSocket.HeartbeatInterval = 5.0;
|
_webSocket.HeartbeatInterval = 5.0;
|
||||||
_firstConnect = false;
|
_firstConnect = false;
|
||||||
return true;
|
StartGameListRefreshLoop();
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
public override void _ExitTree()
|
|
||||||
{
|
public override void _ExitTree()
|
||||||
StopGameListRefreshLoop();
|
{
|
||||||
}
|
StopGameListRefreshLoop();
|
||||||
|
}
|
||||||
public override void _PhysicsProcess(double delta)
|
|
||||||
{
|
public override void _PhysicsProcess(double delta)
|
||||||
_webSocket.Poll();
|
{
|
||||||
WebSocketPeer.State state = _webSocket.GetReadyState();
|
_webSocket.Poll();
|
||||||
if (state == WebSocketPeer.State.Closed && !_firstConnect)
|
WebSocketPeer.State state = _webSocket.GetReadyState();
|
||||||
{
|
if ((state == WebSocketPeer.State.Closed || state == WebSocketPeer.State.Closing) && !_firstConnect)
|
||||||
StopGameListRefreshLoop();
|
{
|
||||||
GetTree().Quit();
|
StopGameListRefreshLoop();
|
||||||
}
|
GD.PrintErr("Connection lost.");
|
||||||
|
//GetTree().Quit();
|
||||||
if (IsSocketOpen)
|
}
|
||||||
{
|
|
||||||
StartGameListRefreshLoop();
|
if (IsSocketOpen)
|
||||||
while (_webSocket.GetAvailablePacketCount() > 0)
|
{
|
||||||
{
|
while (_webSocket.GetAvailablePacketCount() > 0)
|
||||||
string message = _webSocket.GetPacket().GetStringFromUtf8();
|
{
|
||||||
HandleServerMessage(message);
|
string message = _webSocket.GetPacket().GetStringFromUtf8();
|
||||||
}
|
HandleServerMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Player commands
|
|
||||||
public void SendConnect(string clientId)
|
// Player commands
|
||||||
{
|
public void SendConnect(string clientId)
|
||||||
if (string.IsNullOrWhiteSpace(clientId))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(clientId))
|
||||||
GD.PrintErr("Client ID is required to CONNECT.");
|
{
|
||||||
return;
|
GD.PrintErr("Client ID is required to CONNECT.");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
clientId = clientId.Trim();
|
|
||||||
|
clientId = clientId.Trim();
|
||||||
if (clientId.Contains(":"))
|
|
||||||
{
|
if (clientId.Contains(":"))
|
||||||
GD.PrintErr("Client ID cannot contain ':' characters.");
|
{
|
||||||
return;
|
GD.PrintErr("Client ID cannot contain ':' characters.");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
SendCommand("CONNECT", clientId);
|
|
||||||
}
|
SendCommand("CONNECT", clientId);
|
||||||
|
}
|
||||||
public void SendReady()
|
|
||||||
{
|
public void SendReady()
|
||||||
SendCommand("READY");
|
{
|
||||||
}
|
SendCommand("READY");
|
||||||
|
}
|
||||||
public void SendPlay(int column)
|
|
||||||
{
|
public void SendPlay(int column)
|
||||||
SendCommand("PLAY", column.ToString());
|
{
|
||||||
}
|
SendCommand("PLAY", column.ToString());
|
||||||
|
}
|
||||||
// Observer commands
|
|
||||||
public void UpdateGameList()
|
// Observer commands
|
||||||
{
|
public void UpdateGameList()
|
||||||
SendCommand("GAME", "LIST");
|
{
|
||||||
}
|
SendCommand("GAME", "LIST");
|
||||||
|
}
|
||||||
private void StartGameListRefreshLoop()
|
|
||||||
{
|
private void StartGameListRefreshLoop()
|
||||||
if (_gameListThreadRunning)
|
{
|
||||||
{
|
if (_gameListThreadRunning)
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
_gameListThreadRunning = true;
|
|
||||||
_gameListThread = new Thread(() =>
|
_gameListThreadRunning = true;
|
||||||
{
|
_gameListThread = new Thread(() =>
|
||||||
while (_gameListThreadRunning)
|
{
|
||||||
{
|
while (_gameListThreadRunning)
|
||||||
if (IsSocketOpen)
|
{
|
||||||
{
|
if (IsSocketOpen)
|
||||||
UpdateGameList();
|
{
|
||||||
UpdatePlayerList();
|
UpdateGameList();
|
||||||
}
|
UpdatePlayerList();
|
||||||
|
}
|
||||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
|
||||||
}
|
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||||
})
|
}
|
||||||
{
|
})
|
||||||
IsBackground = true
|
{
|
||||||
};
|
IsBackground = true
|
||||||
_gameListThread.Start();
|
};
|
||||||
}
|
_gameListThread.Start();
|
||||||
|
}
|
||||||
private void StopGameListRefreshLoop()
|
|
||||||
{
|
private void StopGameListRefreshLoop()
|
||||||
if (!_gameListThreadRunning)
|
{
|
||||||
{
|
if (!_gameListThreadRunning)
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
_gameListThreadRunning = false;
|
|
||||||
_gameListThread?.Join();
|
_gameListThreadRunning = false;
|
||||||
_gameListThread = null;
|
_gameListThread?.Join();
|
||||||
}
|
_gameListThread = null;
|
||||||
|
}
|
||||||
public void UpdatePlayerList()
|
|
||||||
{
|
public void UpdatePlayerList()
|
||||||
SendCommand("PLAYER", "LIST");
|
{
|
||||||
}
|
SendCommand("PLAYER", "LIST");
|
||||||
|
}
|
||||||
public void SendWatchGame(int matchID)
|
|
||||||
{
|
public void SendWatchGame(int matchID)
|
||||||
SendCommand("GAME", "WATCH:" + matchID);
|
{
|
||||||
}
|
SendCommand("GAME", "WATCH:" + matchID);
|
||||||
|
}
|
||||||
public void AdminAuth(string password)
|
|
||||||
{
|
public void AdminAuth(string password)
|
||||||
if (IsAdmin) return;
|
{
|
||||||
SendCommand("ADMIN", "AUTH:" + password);
|
if (IsAdmin) return;
|
||||||
}
|
SendCommand("ADMIN", "AUTH:" + password);
|
||||||
|
}
|
||||||
|
|
||||||
// Admin commands
|
|
||||||
public void KickPlayer(string playerId)
|
// Admin commands
|
||||||
{
|
public void KickPlayer(string playerId)
|
||||||
if (!IsAdmin) return;
|
{
|
||||||
SendCommand("ADMIN", "KICK:" + playerId);
|
if (!IsAdmin) return;
|
||||||
}
|
SendCommand("ADMIN", "KICK:" + playerId);
|
||||||
|
}
|
||||||
public void StartTournament()
|
|
||||||
{
|
public void StartTournament()
|
||||||
if (!IsAdmin) return;
|
{
|
||||||
SendCommand("TOURNAMENT", "START");
|
if (!IsAdmin) return;
|
||||||
}
|
SendCommand("TOURNAMENT", "START");
|
||||||
|
}
|
||||||
public void TerminateGame()
|
|
||||||
{
|
public void TerminateGame()
|
||||||
if (!IsAdmin) return;
|
{
|
||||||
SendCommand("GAME", "TERMINATE");
|
if (!IsAdmin) return;
|
||||||
}
|
SendCommand("GAME", "TERMINATE");
|
||||||
|
}
|
||||||
public void SetTournamentWait(float waitTime)
|
|
||||||
{
|
public void SetTournamentWait(float waitTime)
|
||||||
if (!IsAdmin) return;
|
{
|
||||||
SendCommand("TOURNAMENT", "WAIT:" + waitTime);
|
if (!IsAdmin) return;
|
||||||
}
|
SendCommand("TOURNAMENT", "WAIT:" + waitTime);
|
||||||
|
}
|
||||||
private void SendCommand(string header, string body = "")
|
|
||||||
{
|
private void SendCommand(string header, string body = "")
|
||||||
if (!IsSocketOpen)
|
{
|
||||||
{
|
if (!IsSocketOpen)
|
||||||
GD.PrintErr($"Cannot send {header}, socket is not open.");
|
{
|
||||||
return;
|
GD.PrintErr($"Cannot send {header}, socket is not open.");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
|
|
||||||
_webSocket.SendText(payload);
|
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
|
||||||
}
|
_webSocket.SendText(payload);
|
||||||
|
}
|
||||||
private void HandleServerMessage(string message)
|
|
||||||
{
|
private void HandleServerMessage(string message)
|
||||||
if (string.IsNullOrWhiteSpace(message))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(message))
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
message = message.Trim();
|
|
||||||
|
message = message.Trim();
|
||||||
string header;
|
|
||||||
string body;
|
string header;
|
||||||
int separatorIndex = message.IndexOf(':');
|
string body;
|
||||||
if (separatorIndex >= 0)
|
int separatorIndex = message.IndexOf(':');
|
||||||
{
|
if (separatorIndex >= 0)
|
||||||
header = message.Substring(0, separatorIndex).Trim();
|
{
|
||||||
body = message.Substring(separatorIndex + 1).Trim();
|
header = message.Substring(0, separatorIndex).Trim();
|
||||||
}
|
body = message.Substring(separatorIndex + 1).Trim();
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
header = message.Trim();
|
{
|
||||||
body = string.Empty;
|
header = message.Trim();
|
||||||
}
|
body = string.Empty;
|
||||||
|
}
|
||||||
header = header.ToUpperInvariant();
|
|
||||||
|
header = header.ToUpperInvariant();
|
||||||
switch (header)
|
|
||||||
{
|
switch (header)
|
||||||
case "CONNECT":
|
{
|
||||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
case "CONNECT":
|
||||||
{
|
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
||||||
IsPlayer = true;
|
{
|
||||||
OnConnected?.Invoke();
|
IsPlayer = true;
|
||||||
}
|
OnConnected?.Invoke();
|
||||||
|
}
|
||||||
break;
|
|
||||||
case "READY":
|
break;
|
||||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
case "READY":
|
||||||
{
|
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
||||||
OnReadyAcknowledged?.Invoke();
|
{
|
||||||
}
|
OnReadyAcknowledged?.Invoke();
|
||||||
|
}
|
||||||
break;
|
|
||||||
case "GAME":
|
break;
|
||||||
HandleGameMessage(body);
|
case "GAME":
|
||||||
break;
|
HandleGameMessage(body);
|
||||||
case "PLAYER":
|
break;
|
||||||
HandlePlayerList(body);
|
case "PLAYER":
|
||||||
break;
|
HandlePlayerList(body);
|
||||||
case "OPPONENT":
|
break;
|
||||||
if (int.TryParse(body, out int column))
|
case "OPPONENT":
|
||||||
{
|
if (int.TryParse(body, out int column))
|
||||||
OnOpponentMove?.Invoke(column);
|
{
|
||||||
}
|
OnOpponentMove?.Invoke(column);
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
GD.PrintErr($"Invalid opponent column: {body}");
|
{
|
||||||
}
|
GD.PrintErr($"Invalid opponent column: {body}");
|
||||||
|
}
|
||||||
break;
|
|
||||||
case "ADMIN":
|
break;
|
||||||
if (body == "AUTH:ACK")
|
case "ADMIN":
|
||||||
{
|
if (body == "AUTH:ACK")
|
||||||
IsAdmin = true;
|
{
|
||||||
OnBecomeAdmin?.Invoke();
|
IsAdmin = true;
|
||||||
}
|
OnBecomeAdmin?.Invoke();
|
||||||
|
}
|
||||||
break;
|
|
||||||
case "TOURNAMENT":
|
break;
|
||||||
HandleTournamentMessage(body);
|
case "TOURNAMENT":
|
||||||
break;
|
HandleTournamentMessage(body);
|
||||||
case "ERROR":
|
break;
|
||||||
HandleErrorMessage(body);
|
case "ERROR":
|
||||||
GD.PrintErr($"Error: {body}");
|
HandleErrorMessage(body);
|
||||||
break;
|
GD.PrintErr($"Error: {body}");
|
||||||
default:
|
break;
|
||||||
GD.Print($"Unhandled server message: {message}");
|
default:
|
||||||
break;
|
GD.Print($"Unhandled server message: {message}");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void HandleTournamentMessage(string body)
|
|
||||||
{
|
private void HandleTournamentMessage(string body)
|
||||||
if (string.IsNullOrWhiteSpace(body))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(body))
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
string[] segments = body.Split(':');
|
|
||||||
string command = segments[0].Trim().ToUpperInvariant();
|
string[] segments = body.Split(':');
|
||||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
string command = segments[0].Trim().ToUpperInvariant();
|
||||||
|
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||||
switch (command)
|
|
||||||
{
|
switch (command)
|
||||||
case "END":
|
{
|
||||||
{
|
case "END":
|
||||||
List<(string, int)> playerScoreboard = new List<(string, int)>();
|
{
|
||||||
string[] entries = segments[1].Split("|");
|
List<(string, int)> playerScoreboard = new List<(string, int)>();
|
||||||
foreach (string entry in entries)
|
string[] entries = segments[1].Split("|");
|
||||||
{
|
foreach (string entry in entries)
|
||||||
string[] data = entry.Split(',');
|
{
|
||||||
playerScoreboard.Add((data[0], int.Parse(data[1])));
|
string[] data = entry.Split(',');
|
||||||
}
|
playerScoreboard.Add((data[0], int.Parse(data[1])));
|
||||||
|
}
|
||||||
OnTournamentEnd?.Invoke(playerScoreboard);
|
|
||||||
break;
|
OnTournamentEnd?.Invoke(playerScoreboard);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void HandlePlayerList(string body)
|
|
||||||
{
|
private void HandlePlayerList(string body)
|
||||||
if (string.IsNullOrWhiteSpace(body))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(body))
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
string[] segments = body.Split(':');
|
|
||||||
string command = segments[0].Trim().ToUpperInvariant();
|
string[] segments = body.Split(':');
|
||||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
string command = segments[0].Trim().ToUpperInvariant();
|
||||||
|
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||||
switch (command)
|
|
||||||
{
|
switch (command)
|
||||||
case "LIST":
|
{
|
||||||
{
|
case "LIST":
|
||||||
List<PlayerData> players = new List<PlayerData>();
|
{
|
||||||
|
List<PlayerData> players = new List<PlayerData>();
|
||||||
if (segments.Length < 2)
|
|
||||||
{
|
if (segments.Length < 2)
|
||||||
OnUpdatedPlayers?.Invoke(players);
|
{
|
||||||
break;
|
OnUpdatedPlayers?.Invoke(players);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
string[] entries = segments[1].Split("|");
|
|
||||||
foreach (string entry in entries)
|
string[] entries = segments[1].Split("|");
|
||||||
{
|
foreach (string entry in entries)
|
||||||
string[] data = entry.Split(',');
|
{
|
||||||
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
|
string[] data = entry.Split(',');
|
||||||
}
|
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
|
||||||
|
}
|
||||||
OnUpdatedPlayers?.Invoke(players);
|
|
||||||
break;
|
OnUpdatedPlayers?.Invoke(players);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void HandleGameMessage(string body)
|
|
||||||
{
|
private void HandleGameMessage(string body)
|
||||||
if (string.IsNullOrWhiteSpace(body))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(body))
|
||||||
return;
|
{
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
string[] segments = body.Split(':');
|
|
||||||
string command = segments[0].Trim().ToUpperInvariant();
|
string[] segments = body.Split(':');
|
||||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
string command = segments[0].Trim().ToUpperInvariant();
|
||||||
|
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||||
if (IsPlayer)
|
|
||||||
{
|
if (IsPlayer)
|
||||||
switch (command)
|
{
|
||||||
{
|
switch (command)
|
||||||
case "START":
|
{
|
||||||
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
case "START":
|
||||||
OnGameStart?.Invoke(isFirst);
|
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||||
break;
|
OnGameStart?.Invoke(isFirst);
|
||||||
case "WINS":
|
break;
|
||||||
OnGameWin?.Invoke();
|
case "WINS":
|
||||||
break;
|
OnGameWin?.Invoke();
|
||||||
case "LOSS":
|
break;
|
||||||
OnGameLoss?.Invoke();
|
case "LOSS":
|
||||||
break;
|
OnGameLoss?.Invoke();
|
||||||
case "DRAW":
|
break;
|
||||||
OnGameDraw?.Invoke();
|
case "DRAW":
|
||||||
break;
|
OnGameDraw?.Invoke();
|
||||||
case "TERMINATED":
|
break;
|
||||||
OnGameTerminated?.Invoke();
|
case "TERMINATED":
|
||||||
break;
|
OnGameTerminated?.Invoke();
|
||||||
default:
|
break;
|
||||||
GD.Print($"Unhandled GAME message: {body}");
|
default:
|
||||||
break;
|
GD.Print($"Unhandled GAME message: {body}");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
else // Regular observer/admin
|
}
|
||||||
{
|
else // Regular observer/admin
|
||||||
switch (command)
|
{
|
||||||
{
|
switch (command)
|
||||||
case "WIN":
|
{
|
||||||
OnObserveWin?.Invoke(segments[1]);
|
case "WIN":
|
||||||
break;
|
OnObserveWin?.Invoke(segments[1]);
|
||||||
case "MOVE":
|
break;
|
||||||
OnObserveMove?.Invoke(segments[1], int.Parse(segments[2]));
|
case "MOVE":
|
||||||
break;
|
OnObserveMove?.Invoke(segments[1], int.Parse(segments[2]));
|
||||||
case "DRAW":
|
break;
|
||||||
OnObserveDraw?.Invoke();
|
case "DRAW":
|
||||||
break;
|
OnObserveDraw?.Invoke();
|
||||||
case "TERMINATED":
|
break;
|
||||||
OnObserveTerminated?.Invoke();
|
case "TERMINATED":
|
||||||
break;
|
OnObserveTerminated?.Invoke();
|
||||||
case "LIST":
|
break;
|
||||||
List<MatchData> matches = new List<MatchData>();
|
case "LIST":
|
||||||
|
List<MatchData> matches = new List<MatchData>();
|
||||||
if (segments.Length < 2)
|
|
||||||
{
|
if (segments.Length < 2)
|
||||||
OnUpdatedMatches?.Invoke(matches);
|
{
|
||||||
break;
|
OnUpdatedMatches?.Invoke(matches);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
string[] entries = segments[1].Split("|");
|
|
||||||
foreach (string entry in entries)
|
string[] entries = segments[1].Split("|");
|
||||||
{
|
foreach (string entry in entries)
|
||||||
string[] data = entry.Split(',');
|
{
|
||||||
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
|
string[] data = entry.Split(',');
|
||||||
}
|
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
|
||||||
|
}
|
||||||
OnUpdatedMatches?.Invoke(matches);
|
|
||||||
break;
|
OnUpdatedMatches?.Invoke(matches);
|
||||||
case "WATCH":
|
break;
|
||||||
string[] game = segments[2].Split(",");
|
case "WATCH":
|
||||||
CurrentObservingMatch = new MatchData(int.Parse(game[0]), game[1], game[2]);
|
string[] game = segments[2].Split(",");
|
||||||
OnWatchGameAck?.Invoke();
|
CurrentObservingMatch = new MatchData(int.Parse(game[0]), game[1], game[2]);
|
||||||
break;
|
OnWatchGameAck?.Invoke();
|
||||||
default:
|
break;
|
||||||
GD.Print($"Unhandled GAME message: {body}");
|
default:
|
||||||
break;
|
GD.Print($"Unhandled GAME message: {body}");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void HandleErrorMessage(string body)
|
|
||||||
{
|
private void HandleErrorMessage(string body)
|
||||||
if (string.IsNullOrWhiteSpace(body))
|
{
|
||||||
{
|
if (string.IsNullOrWhiteSpace(body))
|
||||||
OnError?.Invoke("UNKNOWN", string.Empty);
|
{
|
||||||
return;
|
OnError?.Invoke("UNKNOWN", string.Empty);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
string[] segments = body.Split(':');
|
|
||||||
string code = segments.Length > 0 ? segments[0].Trim().ToUpperInvariant() : "UNKNOWN";
|
string[] segments = body.Split(':');
|
||||||
string detail = segments.Length > 1 ? string.Join(":", segments, 1, segments.Length - 1).Trim() : string.Empty;
|
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);
|
|
||||||
}
|
OnError?.Invoke(code, detail);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user