feat: all new networking changes (except reservations), huge refactor and fmt
This commit is contained in:
@@ -2,3 +2,9 @@ root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
tab_width = 2
|
||||
|
||||
[*.{cs,vb}]
|
||||
dotnet_diagnostic.CA1050.severity = none
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class AddressUI : TextEdit
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
public partial class AddressUI : TextEdit {
|
||||
public override void _Ready() {
|
||||
Text = Connection.WS_DEFAULT_ADDRESS;
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,10 @@ public partial class AdminControls : HBoxContainer
|
||||
{
|
||||
Connection.Instance.OnBecomeAdmin += UpdateUI;
|
||||
Connection.Instance.OnTournamentEnd += UpdateUI;
|
||||
Connection.Instance.OnStartTournamentAck += UpdateUI;
|
||||
Connection.Instance.OnStartTournament += UpdateUI;
|
||||
Connection.Instance.OnCancelTournamentAck += UpdateUI;
|
||||
Connection.Instance.OnGetDataAcks += UpdateUI;
|
||||
Connection.Instance.OnSetDataAcks += UpdateUI;
|
||||
|
||||
StartTournament.Pressed += () => Connection.Instance.StartTournament();
|
||||
CancelTournament.Pressed += () => Connection.Instance.CancelTournament();
|
||||
@@ -23,7 +24,7 @@ public partial class AdminControls : HBoxContainer
|
||||
|
||||
Timeout.ValueChanged += value =>
|
||||
{
|
||||
Connection.Instance.SetTournamentWait((float)value);
|
||||
Connection.Instance.SetMoveWait((float)value);
|
||||
var time = Connection.Instance.CurrentWaitTimeout.ToString();
|
||||
if (time.Length > 3)
|
||||
{
|
||||
@@ -32,16 +33,17 @@ public partial class AdminControls : HBoxContainer
|
||||
Label.Text = "Wait To Move: " + time + "s ";
|
||||
};
|
||||
|
||||
BecomeAdmin.Pressed += ShowAuthPopup;
|
||||
BecomeAdmin.Pressed += showAuthPopup;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnBecomeAdmin -= UpdateUI;
|
||||
Connection.Instance.OnTournamentEnd -= UpdateUI;
|
||||
Connection.Instance.OnStartTournamentAck -= UpdateUI;
|
||||
Connection.Instance.OnStartTournament -= UpdateUI;
|
||||
Connection.Instance.OnCancelTournamentAck -= UpdateUI;
|
||||
Connection.Instance.OnGetDataAcks -= UpdateUI;
|
||||
Connection.Instance.OnSetDataAcks -= UpdateUI;
|
||||
}
|
||||
|
||||
private void UpdateUI()
|
||||
@@ -66,7 +68,7 @@ public partial class AdminControls : HBoxContainer
|
||||
StartTournament.Hide();
|
||||
CancelTournament.Hide();
|
||||
}
|
||||
else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament)
|
||||
else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament != TournamentType.None)
|
||||
{
|
||||
StartTournament.Hide();
|
||||
CancelTournament.Show();
|
||||
@@ -87,7 +89,7 @@ public partial class AdminControls : HBoxContainer
|
||||
Label.Text = "Wait To Move: " + time + "s ";
|
||||
}
|
||||
|
||||
private void ShowAuthPopup()
|
||||
private void showAuthPopup()
|
||||
{
|
||||
var authWindow = new Window();
|
||||
authWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class BackButton : TextureButton
|
||||
{
|
||||
public partial class BackButton : TextureButton {
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
|
||||
public override void _Pressed()
|
||||
{
|
||||
public override void _Pressed() {
|
||||
TransitionToBracket();
|
||||
base._Pressed();
|
||||
}
|
||||
|
||||
private void TransitionToBracket()
|
||||
{
|
||||
private void TransitionToBracket() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
using Godot;
|
||||
using System.Linq;
|
||||
|
||||
public partial class BoardScreen : Node2D
|
||||
{
|
||||
public partial class BoardScreen : Node2D {
|
||||
[Export] private AudioStream endingSfx;
|
||||
[Export] private Theme theme;
|
||||
|
||||
@@ -38,8 +37,10 @@ public partial class BoardScreen : Node2D
|
||||
player1Card = GetNode<Node2D>("Player1Card");
|
||||
player2Card = GetNode<Node2D>("Player2Card");
|
||||
|
||||
player1Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player1Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player1Card);
|
||||
player2Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player2Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player2Card);
|
||||
player1Card.GetNode<Label>("Name").Resized += () =>
|
||||
setPlayerCardScale((player1Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player1Card);
|
||||
player2Card.GetNode<Label>("Name").Resized += () =>
|
||||
setPlayerCardScale((player2Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player2Card);
|
||||
|
||||
redChip = GD.Load<PackedScene>(RED_CHIP_PATH);
|
||||
ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH);
|
||||
@@ -48,18 +49,15 @@ public partial class BoardScreen : Node2D
|
||||
player1Card.GetNode<Label>("Name").Text = matchData.player1;
|
||||
player2Card.GetNode<Label>("Name").Text = matchData.player2;
|
||||
|
||||
if (Connection.Instance.PreviousMoves.Count == 0)
|
||||
{
|
||||
if (Connection.Instance.PreviousMoves.Count == 0) {
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else if (Connection.Instance.PreviousMoves.Last().Item1 == matchData.player1)
|
||||
{
|
||||
else if (Connection.Instance.PreviousMoves.Last().Item1 == matchData.player1) {
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
@@ -67,98 +65,82 @@ public partial class BoardScreen : Node2D
|
||||
Connection.Instance.OnObserveWin += OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw += OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated += OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove += ObserveMove;
|
||||
Connection.Instance.OnObserveMove += OnObserveMove;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f)
|
||||
{
|
||||
public override void _Process(double delta) {
|
||||
if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f) {
|
||||
var move = Connection.Instance.PreviousMoves[0];
|
||||
Connection.Instance.PreviousMoves.RemoveAt(0);
|
||||
if (move.Item1 == matchData.player1)
|
||||
{
|
||||
if (move.Item1 == matchData.player1) {
|
||||
spawnRed(move.Item2);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
spawnYellow(move.Item2);
|
||||
}
|
||||
|
||||
currentTimeout = MOVE_TIMEOUT_BEFORE_PLACE;
|
||||
}
|
||||
else if (currentTimeout >= 0.0f)
|
||||
{
|
||||
else if (currentTimeout >= 0.0f) {
|
||||
currentTimeout -= delta;
|
||||
}
|
||||
|
||||
if (_lastMove)
|
||||
{
|
||||
if (_lastMove) {
|
||||
_lastMoveTimer -= (float)delta;
|
||||
}
|
||||
|
||||
if (_lastMoveTimer <= 0.0f)
|
||||
{
|
||||
if (_winner == "")
|
||||
{
|
||||
PopupMessage("Draw!");
|
||||
if (_lastMoveTimer <= 0.0f) {
|
||||
if (_winner == "") {
|
||||
showPopupMessage("Draw!");
|
||||
}
|
||||
else
|
||||
{
|
||||
PopupMessage(_winner + " wins!");
|
||||
else {
|
||||
showPopupMessage(_winner + " wins!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnObserveWin -= OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw -= OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated -= OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove -= ObserveMove;
|
||||
Connection.Instance.OnObserveMove -= OnObserveMove;
|
||||
}
|
||||
|
||||
private void OnObserveWin(string winner)
|
||||
{
|
||||
private void OnObserveWin(string winner) {
|
||||
_lastMove = true;
|
||||
_winner = winner;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
private void OnObserveDraw()
|
||||
{
|
||||
private void OnObserveDraw() {
|
||||
_lastMove = true;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
private void OnObserveTerminated()
|
||||
{
|
||||
PopupMessage("Match Terminated");
|
||||
private void OnObserveTerminated() {
|
||||
showPopupMessage("Match Terminated");
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
private void ObserveMove(string username, int column)
|
||||
{
|
||||
if (username == matchData.player1)
|
||||
{
|
||||
private void OnObserveMove(string username, int column) {
|
||||
if (username == matchData.player1) {
|
||||
if (!_lastMove)
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
if (!_lastMove)
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
Connection.Instance.PreviousMoves.Add((username, column));
|
||||
}
|
||||
|
||||
private void PopupMessage(string message)
|
||||
{
|
||||
private void showPopupMessage(string message) {
|
||||
var popup = new Popup();
|
||||
popup.AlwaysOnTop = true;
|
||||
popup.Size = new Vector2I(200, 100);
|
||||
@@ -181,11 +163,10 @@ public partial class BoardScreen : Node2D
|
||||
popup.PopupCentered();
|
||||
sfx.Play();
|
||||
popup.Show();
|
||||
TransitionToBracket();
|
||||
transitionToBracket();
|
||||
}
|
||||
|
||||
private void TransitionToBracket()
|
||||
{
|
||||
private void transitionToBracket() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
|
||||
@@ -195,15 +176,16 @@ public partial class BoardScreen : Node2D
|
||||
* or
|
||||
* Will return row of board in which chip can be placed
|
||||
*/
|
||||
public int canPlaceOnCol(int col) {
|
||||
private int canPlaceOnCol(int col) {
|
||||
if (col < 0 || col > 6) // Col out of range
|
||||
return -1;
|
||||
|
||||
return getNextAvailRow(col);
|
||||
}
|
||||
|
||||
public int getNextAvailRow(int col) {
|
||||
for(int i = chips.GetLength(0) - 1; i >= 0; i--) { // Start at bottom
|
||||
private int getNextAvailRow(int col) {
|
||||
for (int i = chips.GetLength(0) - 1; i >= 0; i--) {
|
||||
// Start at bottom
|
||||
if (chips[i, col] == null)
|
||||
return i;
|
||||
}
|
||||
@@ -220,7 +202,8 @@ public partial class BoardScreen : Node2D
|
||||
|
||||
RigidBody2D newNode = redChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col), -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col),
|
||||
-(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
@@ -234,7 +217,8 @@ public partial class BoardScreen : Node2D
|
||||
|
||||
RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col), -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col),
|
||||
-(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
@@ -247,12 +231,13 @@ public partial class BoardScreen : Node2D
|
||||
cardCenter.Scale = new Vector2(x, 2);
|
||||
cardCenter.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX, cardCenter.Position.Y);
|
||||
|
||||
cardRight.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX * 2 + 8, cardRight.Position.Y); // 8 is a magic number (im too lazy) for the size of the card edge multipled by 2
|
||||
cardRight.Position =
|
||||
new Vector2(CARD_CENTER_X_DEFAULT + offX * 2 + 8,
|
||||
cardRight.Position.Y); // 8 is a magic number (im too lazy) for the size of the card edge multipled by 2
|
||||
}
|
||||
}
|
||||
|
||||
enum Direction
|
||||
{
|
||||
enum Direction {
|
||||
UP,
|
||||
UP_RIGHT,
|
||||
RIGHT,
|
||||
|
||||
@@ -1,109 +1,93 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class BracketScene : Control
|
||||
{
|
||||
public partial class BracketScene : Control {
|
||||
[Export] public Tree Players;
|
||||
[Export] public Tree Matches;
|
||||
[Export] public Texture2D WatchButton;
|
||||
[Export] public Texture2D TerminateKickButton;
|
||||
private const string BOARD_SCENE_PATH = "res://scenes/game.tscn";
|
||||
|
||||
private List<PlayerData> _playerList;
|
||||
private List<MatchData> _matchList;
|
||||
private List<PlayerData> playerList;
|
||||
private List<MatchData> matchList;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
public override void _Ready() {
|
||||
Players.SetColumnTitle(0, "Name");
|
||||
Players.SetColumnTitle(1, "Ready");
|
||||
Players.SetColumnTitle(2, "Playing");
|
||||
Players.HideRoot = true;
|
||||
Players.ButtonClicked += KickPlayer;
|
||||
Players.ButtonClicked += kickPlayer;
|
||||
|
||||
|
||||
Matches.SetColumnTitle(0, "Match");
|
||||
Matches.SetColumnTitle(1, "Red");
|
||||
Matches.SetColumnTitle(2, "Yellow");
|
||||
Matches.HideRoot = true;
|
||||
Matches.ButtonClicked += WatchGame;
|
||||
Matches.ButtonClicked += TerminateGame;
|
||||
Matches.ButtonClicked += watchGame;
|
||||
Matches.ButtonClicked += terminateGame;
|
||||
|
||||
Connection.Instance.OnUpdatedPlayers += UpdatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches += UpdateMatches;
|
||||
Connection.Instance.OnWatchGameAck += TransitionToBoard;
|
||||
Connection.Instance.OnUpdatedPlayers += updatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches += updateMatches;
|
||||
Connection.Instance.OnWatchGameAck += transitionToBoard;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches -= UpdateMatches;
|
||||
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnUpdatedPlayers -= updatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches -= updateMatches;
|
||||
Connection.Instance.OnWatchGameAck -= transitionToBoard;
|
||||
}
|
||||
|
||||
private void UpdatePlayers(List<PlayerData> playerList)
|
||||
{
|
||||
private void updatePlayers(List<PlayerData> newPlayerList) {
|
||||
Players.Clear();
|
||||
_playerList = playerList;
|
||||
_playerList.Sort((a, b) => a.username.CompareTo(b.username));
|
||||
playerList = newPlayerList;
|
||||
playerList.Sort((a, b) => a.username.CompareTo(b.username));
|
||||
var root = Players.CreateItem();
|
||||
for (int i = 0; i < _playerList.Count; i++)
|
||||
{
|
||||
for (int i = 0; i < playerList.Count; i++) {
|
||||
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.SetText(0, newPlayerList[i].username);
|
||||
item.SetText(1, newPlayerList[i].isReady ? "Yes" : "No");
|
||||
item.SetText(2, newPlayerList[i].isPlaying ? "Yes" : "No");
|
||||
if (Connection.Instance.IsAdmin) {
|
||||
item.AddButton(0, TerminateKickButton, i, false, "Kick");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMatches(List<MatchData> matchList)
|
||||
{
|
||||
private void updateMatches(List<MatchData> newMatchList) {
|
||||
Matches.Clear();
|
||||
_matchList = matchList;
|
||||
matchList = newMatchList;
|
||||
var root = Matches.CreateItem();
|
||||
for (int i = 0; i < matchList.Count; i++)
|
||||
{
|
||||
for (int i = 0; i < newMatchList.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.SetText(0, newMatchList[i].matchId.ToString());
|
||||
item.SetText(1, newMatchList[i].player1);
|
||||
item.SetText(2, newMatchList[i].player2);
|
||||
item.AddButton(0, WatchButton, i, false, "Watch");
|
||||
if (Connection.Instance.IsAdmin)
|
||||
{
|
||||
if (Connection.Instance.IsAdmin) {
|
||||
item.AddButton(0, TerminateKickButton, 128 + i, false, "Terminate");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void WatchGame(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0 && id < 128)
|
||||
{
|
||||
Connection.Instance.SendWatchGame(_matchList[(int) id].matchId);
|
||||
private void watchGame(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0 && id < 128) {
|
||||
Connection.Instance.SendWatchGame(matchList[(int)id].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void TerminateGame(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && _matchList[(int) id - 128] != null)
|
||||
{
|
||||
Connection.Instance.TerminateGame(_matchList[(int) id - 128].matchId);
|
||||
private void terminateGame(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && matchList[(int)id - 128] != null) {
|
||||
Connection.Instance.TerminateGame(matchList[(int)id - 128].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void KickPlayer(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0)
|
||||
{
|
||||
Connection.Instance.KickPlayer(_playerList[(int) id].username);
|
||||
private void kickPlayer(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0) {
|
||||
Connection.Instance.KickPlayer(playerList[(int)id].username);
|
||||
}
|
||||
}
|
||||
|
||||
private void TransitionToBoard()
|
||||
{
|
||||
private void transitionToBoard() {
|
||||
GetTree().ChangeSceneToFile(BOARD_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +1,50 @@
|
||||
using Godot;
|
||||
|
||||
public partial class ConnectButtonUI : Button
|
||||
{
|
||||
public partial class ConnectButtonUI : Button {
|
||||
[Export] public TextEdit AddressField;
|
||||
[Export] public Label ErrorLabel;
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
public override void _Ready() {
|
||||
Connection.Instance.OnWsConnectionSuccess += OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed += OnConnectionFailed;
|
||||
|
||||
if (Connection.Instance.LastUsedConnectionAddress.Length > 0)
|
||||
{
|
||||
if (Connection.Instance.LastUsedConnectionAddress.Length > 0) {
|
||||
AddressField.Text = Connection.Instance.LastUsedConnectionAddress;
|
||||
}
|
||||
|
||||
if (Connection.Instance.LastError.Length > 0)
|
||||
{
|
||||
if (Connection.Instance.LastError.Length > 0) {
|
||||
ErrorLabel.Text = Connection.Instance.LastError;
|
||||
}
|
||||
|
||||
AddressField.GuiInput += e =>
|
||||
{
|
||||
if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed())
|
||||
{
|
||||
if (inputEventKey.KeyLabel == Key.Enter)
|
||||
{
|
||||
AddressField.GuiInput += e => {
|
||||
if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) {
|
||||
if (inputEventKey.KeyLabel == Key.Enter) {
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
|
||||
if (inputEventKey.KeyLabel == Key.Space)
|
||||
{
|
||||
if (inputEventKey.KeyLabel == Key.Space) {
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnWsConnectionSuccess -= OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed -= OnConnectionFailed;
|
||||
}
|
||||
|
||||
public override void _Pressed()
|
||||
{
|
||||
public override void _Pressed() {
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
}
|
||||
|
||||
private void OnConnectionSuccess()
|
||||
{
|
||||
private void OnConnectionSuccess() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
|
||||
private void OnConnectionFailed()
|
||||
{
|
||||
private void OnConnectionFailed() {
|
||||
ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,24 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
public partial class Connection : Node
|
||||
{
|
||||
public partial class Connection : Node {
|
||||
public const string WS_DEFAULT_ADDRESS = "wss://connect4.abunchofknowitalls.com";
|
||||
|
||||
private static TournamentType? parseTournamentType(string type) {
|
||||
switch (type) {
|
||||
case "RoundRobin":
|
||||
return TournamentType.RoundRobin;
|
||||
case "false":
|
||||
return TournamentType.None;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection Instance { get; private set; }
|
||||
|
||||
private WebSocketPeer _webSocket = new ();
|
||||
private Thread _gameListThread;
|
||||
private bool _gameListThreadRunning;
|
||||
|
||||
private WebSocketPeer webSocket = new WebSocketPeer();
|
||||
public event Action OnConnected;
|
||||
public event Action OnReadyAcknowledged;
|
||||
public event Action<bool> OnGameStart;
|
||||
@@ -29,11 +35,12 @@ public partial class Connection : Node
|
||||
public event Action<string, int> OnObserveMove;
|
||||
public event Action<List<MatchData>> OnUpdatedMatches;
|
||||
public event Action<List<PlayerData>> OnUpdatedPlayers;
|
||||
public event Action OnStartTournamentAck;
|
||||
public event Action OnStartTournament;
|
||||
public event Action OnTournamentEnd;
|
||||
public event Action OnCancelTournamentAck;
|
||||
public event Action OnBecomeAdmin;
|
||||
public event Action OnGetDataAcks;
|
||||
public event Action OnSetDataAcks;
|
||||
|
||||
public event Action OnWsConnectionSuccess;
|
||||
public event Action OnWsConnectionFailed;
|
||||
@@ -44,116 +51,100 @@ public partial class Connection : Node
|
||||
|
||||
public bool IsAdmin { get; private set; }
|
||||
public bool IsPlayer { get; private set; }
|
||||
public bool ActiveTournament { get; private set; }
|
||||
public TournamentType ActiveTournament { get; private set; }
|
||||
public bool DemoMode { get; private set; }
|
||||
public List<(string, int)> PreviousMoves { get; private set; } = [];
|
||||
public double CurrentWaitTimeout { get; private set; } = 5.0;
|
||||
public double MaxTimeout { get; private set; } = 30.0;
|
||||
public MatchData CurrentObservingMatch { get; private set; }
|
||||
public string LastUsedConnectionAddress { get; private set; } = "";
|
||||
public string LastError { get; private set; } = "";
|
||||
|
||||
private bool IsSocketOpen => _webSocket.GetReadyState() == WebSocketPeer.State.Open;
|
||||
private bool IsSocketOpen => webSocket.GetReadyState() == WebSocketPeer.State.Open;
|
||||
|
||||
private bool _connecting = false;
|
||||
private bool _connected = false;
|
||||
private List<(string, int)> _lastScoreboard = [];
|
||||
private bool _shouldShowTournamentResults = false;
|
||||
private float _refreshGamePlayerListTimer = 5.0f;
|
||||
private bool isConnecting = false;
|
||||
private bool isConnected = false;
|
||||
private List<(string, int)> lastScoreboard = [];
|
||||
private bool shouldShowTournamentResults = false;
|
||||
private float refreshGamePlayerListTimer = 5.0f;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
public override void _Ready() {
|
||||
Instance = this;
|
||||
_webSocket.SetHeartbeatInterval(5.0);
|
||||
_webSocket.HeartbeatInterval = 5.0;
|
||||
webSocket.SetHeartbeatInterval(5.0);
|
||||
webSocket.HeartbeatInterval = 5.0;
|
||||
Instance.OnWsDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
|
||||
}
|
||||
|
||||
public void Connect(string address)
|
||||
{
|
||||
_connecting = true;
|
||||
public void Connect(string address) {
|
||||
isConnecting = true;
|
||||
LastUsedConnectionAddress = address;
|
||||
if (_connected)
|
||||
{
|
||||
if (isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
Error error = _webSocket.ConnectToUrl(address);
|
||||
if (error != Error.Ok)
|
||||
{
|
||||
Error error = webSocket.ConnectToUrl(address);
|
||||
if (error != Error.Ok) {
|
||||
LastError = error.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
_webSocket.Poll();
|
||||
WebSocketPeer.State state = _webSocket.GetReadyState();
|
||||
if (state == WebSocketPeer.State.Open)
|
||||
{
|
||||
if (_connecting)
|
||||
{
|
||||
_connecting = false;
|
||||
_connected = true;
|
||||
public override void _Process(double delta) {
|
||||
webSocket.Poll();
|
||||
WebSocketPeer.State state = webSocket.GetReadyState();
|
||||
if (state == WebSocketPeer.State.Open) {
|
||||
if (isConnecting) {
|
||||
isConnecting = false;
|
||||
isConnected = true;
|
||||
LastError = "";
|
||||
OnWsConnectionSuccess?.Invoke();
|
||||
UpdateGameList();
|
||||
UpdatePlayerList();
|
||||
_refreshGamePlayerListTimer = 5.0f;
|
||||
} else if (_refreshGamePlayerListTimer <= 0.0f)
|
||||
{
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
}
|
||||
else if (refreshGamePlayerListTimer <= 0.0f) {
|
||||
UpdateGameList();
|
||||
UpdatePlayerList();
|
||||
_refreshGamePlayerListTimer = 5.0f;
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
_refreshGamePlayerListTimer -= (float) delta;
|
||||
else {
|
||||
refreshGamePlayerListTimer -= (float)delta;
|
||||
}
|
||||
|
||||
while (_webSocket.GetAvailablePacketCount() > 0)
|
||||
{
|
||||
string message = _webSocket.GetPacket().GetStringFromUtf8();
|
||||
HandleServerMessage(message);
|
||||
while (webSocket.GetAvailablePacketCount() > 0) {
|
||||
string message = webSocket.GetPacket().GetStringFromUtf8();
|
||||
handleServerMessage(message);
|
||||
}
|
||||
|
||||
if (_shouldShowTournamentResults)
|
||||
{
|
||||
if (shouldShowTournamentResults) {
|
||||
var children = GetTree().Root.GetChildren();
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child.Name.ToString() == "BracketView")
|
||||
{
|
||||
_shouldShowTournamentResults = false;
|
||||
ShowTournamentScoreboard(_lastScoreboard);
|
||||
foreach (var child in children) {
|
||||
if (child.Name.ToString() == "BracketView") {
|
||||
shouldShowTournamentResults = false;
|
||||
ShowTournamentScoreboard(lastScoreboard);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (state == WebSocketPeer.State.Connecting)
|
||||
{
|
||||
else if (state == WebSocketPeer.State.Connecting) {
|
||||
// Do nothing
|
||||
}
|
||||
else if (state == WebSocketPeer.State.Closing)
|
||||
{
|
||||
else if (state == WebSocketPeer.State.Closing) {
|
||||
// Do nothing
|
||||
}
|
||||
else if (state == WebSocketPeer.State.Closed)
|
||||
{
|
||||
if (_connecting)
|
||||
{
|
||||
_connecting = false;
|
||||
else if (state == WebSocketPeer.State.Closed) {
|
||||
if (isConnecting) {
|
||||
isConnecting = false;
|
||||
OnWsConnectionFailed?.Invoke();
|
||||
}
|
||||
else if (_connected)
|
||||
{
|
||||
_connected = false;
|
||||
else if (isConnected) {
|
||||
isConnected = false;
|
||||
IsAdmin = false;
|
||||
CurrentWaitTimeout = 5.0;
|
||||
ActiveTournament = false;
|
||||
ActiveTournament = TournamentType.None;
|
||||
DemoMode = false;
|
||||
_refreshGamePlayerListTimer = 5.0f;
|
||||
var code = _webSocket.GetCloseCode();
|
||||
var reason = _webSocket.GetCloseReason();
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
var code = webSocket.GetCloseCode();
|
||||
var reason = webSocket.GetCloseReason();
|
||||
LastError = "Unexpected Disconnect. Reason: " + reason + ", Code: " + code;
|
||||
GD.PrintErr("WebSocket closed with code: " + code + ", reason " + reason + ". Clean: " + (code != -1));
|
||||
OnWsDisconnect?.Invoke();
|
||||
@@ -162,116 +153,120 @@ public partial class Connection : Node
|
||||
}
|
||||
|
||||
// Player commands
|
||||
public void SendConnect(string clientId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(clientId))
|
||||
{
|
||||
public void SendConnect(string clientId) {
|
||||
if (string.IsNullOrWhiteSpace(clientId)) {
|
||||
GD.PrintErr("Client ID is required to CONNECT.");
|
||||
return;
|
||||
}
|
||||
|
||||
clientId = clientId.Trim();
|
||||
|
||||
if (clientId.Contains(":"))
|
||||
{
|
||||
if (clientId.Contains(":")) {
|
||||
GD.PrintErr("Client ID cannot contain ':' characters.");
|
||||
return;
|
||||
}
|
||||
|
||||
SendCommand("CONNECT", clientId);
|
||||
sendCommand("CONNECT", clientId);
|
||||
}
|
||||
|
||||
public void SendReady()
|
||||
{
|
||||
SendCommand("READY");
|
||||
public void SendDisconnect() {
|
||||
sendCommand("DISCONNECT");
|
||||
}
|
||||
|
||||
public void SendPlay(int column)
|
||||
{
|
||||
SendCommand("PLAY", column.ToString());
|
||||
public void SendReady() {
|
||||
sendCommand("READY");
|
||||
}
|
||||
|
||||
public void SendPlay(int column) {
|
||||
sendCommand("PLAY", column.ToString());
|
||||
}
|
||||
|
||||
// Observer commands
|
||||
public void UpdateGameList()
|
||||
{
|
||||
SendCommand("GAME", "LIST");
|
||||
public void UpdateGameList() {
|
||||
sendCommand("GAME", "LIST");
|
||||
}
|
||||
|
||||
public void UpdatePlayerList()
|
||||
{
|
||||
SendCommand("PLAYER", "LIST");
|
||||
public void UpdatePlayerList() {
|
||||
sendCommand("PLAYER", "LIST");
|
||||
}
|
||||
|
||||
public void SendWatchGame(int matchID)
|
||||
{
|
||||
SendCommand("GAME", "WATCH:" + matchID);
|
||||
public void SendWatchGame(int matchID) {
|
||||
sendCommand("GAME", "WATCH:" + matchID);
|
||||
}
|
||||
|
||||
public void AdminAuth(string password)
|
||||
{
|
||||
public void AdminAuth(string password) {
|
||||
if (IsAdmin) return;
|
||||
SendCommand("ADMIN", "AUTH:" + password);
|
||||
sendCommand("ADMIN", "AUTH:" + password);
|
||||
}
|
||||
|
||||
public void GetMoveWait()
|
||||
{
|
||||
SendCommand("GET", "MOVE_WAIT");
|
||||
public void GetMoveWait() {
|
||||
sendCommand("GET", "MOVE_WAIT");
|
||||
}
|
||||
|
||||
public void GetTournamentStatus()
|
||||
{
|
||||
SendCommand("GET", "TOURNAMENT_STATUS");
|
||||
public void GetTournamentStatus() {
|
||||
sendCommand("GET", "TOURNAMENT_STATUS");
|
||||
}
|
||||
|
||||
public void GetDemoMode() {
|
||||
sendCommand("GET", "DEMO_MODE");
|
||||
}
|
||||
|
||||
public void GetMaxTimeout() {
|
||||
sendCommand("GET", "MAX_TIMEOUT");
|
||||
}
|
||||
|
||||
|
||||
// Admin commands
|
||||
public void KickPlayer(string playerId)
|
||||
{
|
||||
public void KickPlayer(string playerId) {
|
||||
if (!IsAdmin) return;
|
||||
SendCommand("ADMIN", "KICK:" + playerId);
|
||||
sendCommand("ADMIN", "KICK:" + playerId);
|
||||
}
|
||||
|
||||
public void StartTournament()
|
||||
{
|
||||
public void StartTournament(string tournamentType = "RoundRobin") {
|
||||
if (!IsAdmin) return;
|
||||
SendCommand("TOURNAMENT", "START");
|
||||
sendCommand("TOURNAMENT", "START:" + tournamentType);
|
||||
}
|
||||
|
||||
public void CancelTournament()
|
||||
{
|
||||
public void CancelTournament() {
|
||||
if (!IsAdmin) return;
|
||||
SendCommand("TOURNAMENT", "CANCEL");
|
||||
sendCommand("TOURNAMENT", "CANCEL");
|
||||
}
|
||||
|
||||
public void TerminateGame(int matchID)
|
||||
{
|
||||
public void TerminateGame(int matchID) {
|
||||
if (!IsAdmin) return;
|
||||
SendCommand("GAME", "TERMINATE:" + matchID);
|
||||
sendCommand("GAME", "TERMINATE:" + matchID);
|
||||
}
|
||||
|
||||
public void SetTournamentWait(float waitTime)
|
||||
{
|
||||
public void SetMoveWait(float waitTime) {
|
||||
if (!IsAdmin) return;
|
||||
CurrentWaitTimeout = waitTime;
|
||||
SendCommand("TOURNAMENT", "WAIT:" + waitTime);
|
||||
sendCommand("SET", "MOVE_WAIT:" + waitTime);
|
||||
}
|
||||
|
||||
private void SendCommand(string header, string body = "")
|
||||
{
|
||||
if (!IsSocketOpen)
|
||||
{
|
||||
public void SetDemoMode(bool demoMode) {
|
||||
if (!IsAdmin) return;
|
||||
DemoMode = demoMode;
|
||||
sendCommand("SET", "DEMO_MODE:" + demoMode);
|
||||
}
|
||||
|
||||
public void SetMaxTimeout(float maxTimeout) {
|
||||
if (!IsAdmin) return;
|
||||
MaxTimeout = maxTimeout;
|
||||
sendCommand("SET", "MAX_TIMEOUT:" + maxTimeout);
|
||||
}
|
||||
|
||||
private void sendCommand(string header, string body = "") {
|
||||
if (!IsSocketOpen) {
|
||||
GD.PrintErr($"Cannot send {header}, socket is not open.");
|
||||
return;
|
||||
}
|
||||
|
||||
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
|
||||
_webSocket.SendText(payload);
|
||||
webSocket.SendText(payload);
|
||||
}
|
||||
|
||||
private void HandleServerMessage(string message)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
private void handleServerMessage(string message) {
|
||||
if (string.IsNullOrWhiteSpace(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -280,56 +275,48 @@ public partial class Connection : Node
|
||||
string header;
|
||||
string body;
|
||||
int separatorIndex = message.IndexOf(':');
|
||||
if (separatorIndex >= 0)
|
||||
{
|
||||
if (separatorIndex >= 0) {
|
||||
header = message.Substring(0, separatorIndex).Trim();
|
||||
body = message.Substring(separatorIndex + 1).Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
header = message.Trim();
|
||||
body = string.Empty;
|
||||
}
|
||||
|
||||
header = header.ToUpperInvariant();
|
||||
|
||||
switch (header)
|
||||
{
|
||||
switch (header) {
|
||||
case "CONNECT":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
IsPlayer = true;
|
||||
OnConnected?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
case "READY":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
OnReadyAcknowledged?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
case "GAME":
|
||||
HandleGameMessage(body);
|
||||
handleGameMessage(body);
|
||||
break;
|
||||
case "PLAYER":
|
||||
HandlePlayerList(body);
|
||||
handlePlayerList(body);
|
||||
break;
|
||||
case "OPPONENT":
|
||||
if (int.TryParse(body, out int column))
|
||||
{
|
||||
if (int.TryParse(body, out int column)) {
|
||||
OnOpponentMove?.Invoke(column);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
GD.PrintErr($"Invalid opponent column: {body}");
|
||||
}
|
||||
|
||||
break;
|
||||
case "ADMIN":
|
||||
if (body == "AUTH:ACK")
|
||||
{
|
||||
if (body == "AUTH:ACK") {
|
||||
IsAdmin = true;
|
||||
GetMoveWait();
|
||||
GetTournamentStatus();
|
||||
@@ -338,28 +325,38 @@ public partial class Connection : Node
|
||||
|
||||
break;
|
||||
case "TOURNAMENT":
|
||||
HandleTournamentMessage(body);
|
||||
handleTournamentMessage(body);
|
||||
break;
|
||||
case "GET":
|
||||
if (body.StartsWith("MOVE_WAIT"))
|
||||
{
|
||||
CurrentWaitTimeout = double.Parse(body.Split(":")[1]);
|
||||
string data = body.Split(":")[1];
|
||||
if (body.StartsWith("MOVE_WAIT")) {
|
||||
CurrentWaitTimeout = double.Parse(data);
|
||||
}
|
||||
else if (body.StartsWith("TOURNAMENT_STATUS"))
|
||||
{
|
||||
string status = body.Split(":")[1];
|
||||
if (status != "DEMO")
|
||||
{
|
||||
ActiveTournament = bool.Parse(status);
|
||||
else if (body.StartsWith("MAX_TIMEOUT")) {
|
||||
MaxTimeout = double.Parse(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
ActiveTournament = false;
|
||||
DemoMode = true;
|
||||
else if (body.StartsWith("DEMO_MODE")) {
|
||||
DemoMode = bool.Parse(data);
|
||||
}
|
||||
else if (body.StartsWith("TOURNAMENT_STATUS")) {
|
||||
TournamentType? type = parseTournamentType(data);
|
||||
|
||||
if (type == null) {
|
||||
GD.PrintErr($"Unhandled tournament type: {data}");
|
||||
}
|
||||
else {
|
||||
ActiveTournament = type.Value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
GD.PrintErr($"Unhandled data get: {body}");
|
||||
}
|
||||
|
||||
OnGetDataAcks?.Invoke();
|
||||
break;
|
||||
case "SET":
|
||||
OnSetDataAcks?.Invoke();
|
||||
break;
|
||||
case "ERROR":
|
||||
GD.PrintErr(message);
|
||||
OnError?.Invoke(message);
|
||||
@@ -370,10 +367,8 @@ public partial class Connection : Node
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleTournamentMessage(string body)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
private void handleTournamentMessage(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -381,65 +376,60 @@ public partial class Connection : Node
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case "END":
|
||||
{
|
||||
ActiveTournament = false;
|
||||
switch (command) {
|
||||
case "END": {
|
||||
ActiveTournament = TournamentType.None;
|
||||
List<(string, int)> playerScoreboard = new List<(string, int)>();
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries)
|
||||
{
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
playerScoreboard.Add((data[0], int.Parse(data[1])));
|
||||
}
|
||||
|
||||
_lastScoreboard = playerScoreboard;
|
||||
_shouldShowTournamentResults = true;
|
||||
lastScoreboard = playerScoreboard;
|
||||
shouldShowTournamentResults = true;
|
||||
OnTournamentEnd?.Invoke();
|
||||
break;
|
||||
}
|
||||
case "START":
|
||||
{
|
||||
ActiveTournament = true;
|
||||
OnStartTournamentAck?.Invoke();
|
||||
case "START": {
|
||||
TournamentType? type = parseTournamentType(argument);
|
||||
if (type == null) {
|
||||
GD.PrintErr($"Unhandled tournament type: {argument}");
|
||||
}
|
||||
else {
|
||||
ActiveTournament = type.Value;
|
||||
OnStartTournament?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "CANCEL":
|
||||
{
|
||||
ActiveTournament = false;
|
||||
case "CANCEL": {
|
||||
ActiveTournament = TournamentType.None;
|
||||
OnCancelTournamentAck?.Invoke();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePlayerList(string body)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
private void handlePlayerList(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
|
||||
string[] segments = body.Split(':');
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||
|
||||
switch (command)
|
||||
{
|
||||
case "LIST":
|
||||
{
|
||||
switch (command) {
|
||||
case "LIST": {
|
||||
List<PlayerData> players = new List<PlayerData>();
|
||||
|
||||
if (segments.Length < 2)
|
||||
{
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedPlayers?.Invoke(players);
|
||||
break;
|
||||
}
|
||||
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries)
|
||||
{
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
|
||||
}
|
||||
@@ -450,10 +440,8 @@ public partial class Connection : Node
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleGameMessage(string body)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
private void handleGameMessage(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -461,10 +449,8 @@ public partial class Connection : Node
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||
|
||||
if (IsPlayer)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
if (IsPlayer) {
|
||||
switch (command) {
|
||||
case "START":
|
||||
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||
OnGameStart?.Invoke(isFirst);
|
||||
@@ -488,8 +474,7 @@ public partial class Connection : Node
|
||||
}
|
||||
else // Regular observer/admin
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
switch (command) {
|
||||
case "WIN":
|
||||
OnObserveWin?.Invoke(segments[1]);
|
||||
break;
|
||||
@@ -505,15 +490,13 @@ public partial class Connection : Node
|
||||
case "LIST":
|
||||
List<MatchData> matches = new List<MatchData>();
|
||||
|
||||
if (segments.Length < 2)
|
||||
{
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedMatches?.Invoke(matches);
|
||||
break;
|
||||
}
|
||||
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries)
|
||||
{
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
|
||||
}
|
||||
@@ -524,21 +507,19 @@ public partial class Connection : Node
|
||||
CurrentObservingMatch = null;
|
||||
PreviousMoves.Clear();
|
||||
string[] activeMatchData = segments[2].Split("|");
|
||||
if (activeMatchData.IsEmpty())
|
||||
{
|
||||
if (activeMatchData.IsEmpty()) {
|
||||
string[] matchData = segments[2].Split(',');
|
||||
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
string[] matchData = activeMatchData[0].Split(',');
|
||||
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]);
|
||||
for (int i = 1; i < activeMatchData.Length; i++)
|
||||
{
|
||||
for (int i = 1; i < activeMatchData.Length; i++) {
|
||||
string[] moveData = activeMatchData[i].Split(',');
|
||||
PreviousMoves.Add((moveData[0], int.Parse(moveData[1])));
|
||||
}
|
||||
}
|
||||
|
||||
OnWatchGameAck?.Invoke();
|
||||
break;
|
||||
default:
|
||||
@@ -548,8 +529,7 @@ public partial class Connection : Node
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowTournamentScoreboard(List<(string, int)> playerScoreboard)
|
||||
{
|
||||
public void ShowTournamentScoreboard(List<(string, int)> playerScoreboard) {
|
||||
var scoreboardWindow = new Window();
|
||||
scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
scoreboardWindow.AlwaysOnTop = true;
|
||||
@@ -557,10 +537,7 @@ public partial class Connection : Node
|
||||
scoreboardWindow.Unresizable = true;
|
||||
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
|
||||
scoreboardWindow.Size = new Vector2I(256, 512);
|
||||
scoreboardWindow.CloseRequested += () =>
|
||||
{
|
||||
GetTree().Root.RemoveChild(scoreboardWindow);
|
||||
};
|
||||
scoreboardWindow.CloseRequested += () => { GetTree().Root.RemoveChild(scoreboardWindow); };
|
||||
|
||||
var tree = new Tree();
|
||||
tree.HideRoot = true;
|
||||
@@ -573,8 +550,7 @@ public partial class Connection : Node
|
||||
tree.SetColumnTitle(1, "Score");
|
||||
var root = tree.CreateItem();
|
||||
|
||||
foreach ((string, int) entry in playerScoreboard)
|
||||
{
|
||||
foreach ((string, int) entry in playerScoreboard) {
|
||||
var item = tree.CreateItem(root);
|
||||
item.SetText(0, entry.Item1);
|
||||
item.SetText(1, entry.Item2.ToString());
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
public class MatchData
|
||||
{
|
||||
public class MatchData {
|
||||
public int matchId { get; private set; }
|
||||
public string player1 { get; private set; }
|
||||
public string player2 { get; private set; }
|
||||
|
||||
public MatchData(int matchId, string player1, string player2)
|
||||
{
|
||||
public MatchData(int matchId, string player1, string player2) {
|
||||
this.matchId = matchId;
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
public class PlayerData
|
||||
{
|
||||
public class PlayerData {
|
||||
public string username { get; private set; }
|
||||
public bool isReady { get; private set; }
|
||||
public bool isPlaying { get; private set; }
|
||||
|
||||
public PlayerData(string username, bool isReady, bool isPlaying)
|
||||
{
|
||||
public PlayerData(string username, bool isReady, bool isPlaying) {
|
||||
this.username = username;
|
||||
this.isReady = isReady;
|
||||
this.isPlaying = isPlaying;
|
||||
|
||||
5
scripts/TournamentType.cs
Normal file
5
scripts/TournamentType.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
public enum TournamentType {
|
||||
None,
|
||||
RoundRobin
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user