11 Commits

15 changed files with 1504 additions and 1136 deletions

View File

@@ -2,3 +2,9 @@ root = true
[*] [*]
charset = utf-8 charset = utf-8
indent_style = space
indent_size = 2
tab_width = 2
[*.{cs,vb}]
dotnet_diagnostic.CA1050.severity = none

View File

@@ -3,7 +3,6 @@
name="macOS" name="macOS"
platform="macOS" platform="macOS"
runnable=true runnable=true
advanced_options=true
dedicated_server=false dedicated_server=false
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
@@ -11,6 +10,11 @@ include_filter=""
exclude_filter="" exclude_filter=""
export_path="../../Downloads/connect4-moderator-observer.dmg" export_path="../../Downloads/connect4-moderator-observer.dmg"
patches=PackedStringArray() patches=PackedStringArray()
patch_delta_encoding=false
patch_delta_compression_level_zstd=19
patch_delta_min_reduction=0.1
patch_delta_include_filters="*"
patch_delta_exclude_filters=""
encryption_include_filters="" encryption_include_filters=""
encryption_exclude_filters="" encryption_exclude_filters=""
seed=0 seed=0
@@ -25,6 +29,7 @@ binary_format/architecture="universal"
custom_template/debug="" custom_template/debug=""
custom_template/release="" custom_template/release=""
debug/export_console_wrapper=0 debug/export_console_wrapper=0
application/liquid_glass_icon="res://icon.icon"
application/icon="uid://dd7lvnidxr5ss" application/icon="uid://dd7lvnidxr5ss"
application/icon_interpolation=0 application/icon_interpolation=0
application/bundle_identifier="com.abunchofknowitalls.connect4" application/bundle_identifier="com.abunchofknowitalls.connect4"
@@ -32,7 +37,7 @@ application/signature=""
application/app_category="Games" application/app_category="Games"
application/short_version="" application/short_version=""
application/version="" application/version=""
application/copyright="RPI Minds and Machines" application/copyright="RPI Minds & Machines"
application/copyright_localized={} application/copyright_localized={}
application/min_macos_version_x86_64="10.12" application/min_macos_version_x86_64="10.12"
application/min_macos_version_arm64="11.00" application/min_macos_version_arm64="11.00"
@@ -185,10 +190,10 @@ privacy/collected_data/browsing_history/collected=false
privacy/collected_data/browsing_history/linked_to_user=false privacy/collected_data/browsing_history/linked_to_user=false
privacy/collected_data/browsing_history/used_for_tracking=false privacy/collected_data/browsing_history/used_for_tracking=false
privacy/collected_data/browsing_history/collection_purposes=0 privacy/collected_data/browsing_history/collection_purposes=0
privacy/collected_data/search_hhistory/collected=false privacy/collected_data/search_history/collected=false
privacy/collected_data/search_hhistory/linked_to_user=false privacy/collected_data/search_history/linked_to_user=false
privacy/collected_data/search_hhistory/used_for_tracking=false privacy/collected_data/search_history/used_for_tracking=false
privacy/collected_data/search_hhistory/collection_purposes=0 privacy/collected_data/search_history/collection_purposes=0
privacy/collected_data/user_id/collected=false privacy/collected_data/user_id/collected=false
privacy/collected_data/user_id/linked_to_user=false privacy/collected_data/user_id/linked_to_user=false
privacy/collected_data/user_id/used_for_tracking=false privacy/collected_data/user_id/used_for_tracking=false
@@ -255,13 +260,16 @@ rm -rf \"{temp_dir}\""
dotnet/include_scripts_content=false dotnet/include_scripts_content=false
dotnet/include_debug_symbols=false dotnet/include_debug_symbols=false
dotnet/embed_build_outputs=false dotnet/embed_build_outputs=false
privacy/collected_data/search_hhistory/collected=false
privacy/collected_data/search_hhistory/linked_to_user=false
privacy/collected_data/search_hhistory/used_for_tracking=false
privacy/collected_data/search_hhistory/collection_purposes=0
[preset.1] [preset.1]
name="Windows (x86_64)" name="Windows (x86_64)"
platform="Windows Desktop" platform="Windows Desktop"
runnable=true runnable=true
advanced_options=true
dedicated_server=false dedicated_server=false
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
@@ -269,6 +277,11 @@ include_filter=""
exclude_filter="" exclude_filter=""
export_path="../../Downloads/connect4-moderator-observer (win-x86_64).exe" export_path="../../Downloads/connect4-moderator-observer (win-x86_64).exe"
patches=PackedStringArray() patches=PackedStringArray()
patch_delta_encoding=false
patch_delta_compression_level_zstd=19
patch_delta_min_reduction=0.1
patch_delta_include_filters="*"
patch_delta_exclude_filters=""
encryption_include_filters="" encryption_include_filters=""
encryption_exclude_filters="" encryption_exclude_filters=""
seed=0 seed=0
@@ -332,7 +345,6 @@ dotnet/embed_build_outputs=true
name="Windows (arm64)" name="Windows (arm64)"
platform="Windows Desktop" platform="Windows Desktop"
runnable=false runnable=false
advanced_options=true
dedicated_server=false dedicated_server=false
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
@@ -340,6 +352,11 @@ include_filter=""
exclude_filter="" exclude_filter=""
export_path="../../Downloads/connect4-moderator-observer (win-arm64).exe" export_path="../../Downloads/connect4-moderator-observer (win-arm64).exe"
patches=PackedStringArray() patches=PackedStringArray()
patch_delta_encoding=false
patch_delta_compression_level_zstd=19
patch_delta_min_reduction=0.1
patch_delta_include_filters="*"
patch_delta_exclude_filters=""
encryption_include_filters="" encryption_include_filters=""
encryption_exclude_filters="" encryption_exclude_filters=""
seed=0 seed=0
@@ -403,7 +420,6 @@ dotnet/embed_build_outputs=true
name="Linux (x86_64)" name="Linux (x86_64)"
platform="Linux" platform="Linux"
runnable=true runnable=true
advanced_options=true
dedicated_server=false dedicated_server=false
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
@@ -411,6 +427,11 @@ include_filter=""
exclude_filter="" exclude_filter=""
export_path="../../Downloads/connect4-moderator-observer (linux-x86_64).out" export_path="../../Downloads/connect4-moderator-observer (linux-x86_64).out"
patches=PackedStringArray() patches=PackedStringArray()
patch_delta_encoding=false
patch_delta_compression_level_zstd=19
patch_delta_min_reduction=0.1
patch_delta_include_filters="*"
patch_delta_exclude_filters=""
encryption_include_filters="" encryption_include_filters=""
encryption_exclude_filters="" encryption_exclude_filters=""
seed=0 seed=0
@@ -449,7 +470,6 @@ dotnet/embed_build_outputs=true
name="Linux (arm64)" name="Linux (arm64)"
platform="Linux" platform="Linux"
runnable=false runnable=false
advanced_options=true
dedicated_server=false dedicated_server=false
custom_features="" custom_features=""
export_filter="all_resources" export_filter="all_resources"
@@ -457,6 +477,11 @@ include_filter=""
exclude_filter="" exclude_filter=""
export_path="../../Downloads/connect4-moderator-observer (linux-arm64).out" export_path="../../Downloads/connect4-moderator-observer (linux-arm64).out"
patches=PackedStringArray() patches=PackedStringArray()
patch_delta_encoding=false
patch_delta_compression_level_zstd=19
patch_delta_min_reduction=0.1
patch_delta_include_filters="*"
patch_delta_exclude_filters=""
encryption_include_filters="" encryption_include_filters=""
encryption_exclude_filters="" encryption_exclude_filters=""
seed=0 seed=0

View File

@@ -8,12 +8,16 @@
config_version=5 config_version=5
[animation]
compatibility/default_parent_skeleton_in_mesh_instance_3d=true
[application] [application]
config/name="Connect4 Observer" config/name="Connect4 Observer"
config/version="1.0.0" config/version="1.0.1"
run/main_scene="uid://dcx5nvs0pa7me" run/main_scene="uid://dcx5nvs0pa7me"
config/features=PackedStringArray("4.5", "C#", "Forward Plus") config/features=PackedStringArray("4.6", "C#", "Forward Plus")
boot_splash/image="uid://dd7lvnidxr5ss" boot_splash/image="uid://dd7lvnidxr5ss"
config/icon="uid://dd7lvnidxr5ss" config/icon="uid://dd7lvnidxr5ss"

View File

@@ -1,10 +1,6 @@
using Godot; using Godot;
using System; using System;
public partial class AddressUI : TextEdit public partial class AddressUI : TextEdit {
{ public override void _Ready() { Text = Connection.WS_DEFAULT_ADDRESS; }
public override void _Ready()
{
Text = Connection.WS_DEFAULT_ADDRESS;
}
} }

View File

@@ -1,20 +1,19 @@
using Godot; using Godot;
public partial class AdminControls : HBoxContainer public partial class AdminControls : HBoxContainer {
{
[Export] public Button BecomeAdmin; [Export] public Button BecomeAdmin;
[Export] public Button StartTournament; [Export] public Button StartTournament;
[Export] public Button CancelTournament; [Export] public Button CancelTournament;
[Export] public Label Label; [Export] public Label Label;
[Export] public Slider Timeout; [Export] public Slider Timeout;
public override void _Ready() public override void _Ready() {
{
Connection.Instance.OnBecomeAdmin += UpdateUI; Connection.Instance.OnBecomeAdmin += UpdateUI;
Connection.Instance.OnTournamentEnd += UpdateUI; Connection.Instance.OnTournamentEnd += UpdateUI;
Connection.Instance.OnStartTournamentAck += UpdateUI; Connection.Instance.OnStartTournament += UpdateUI;
Connection.Instance.OnCancelTournamentAck += UpdateUI; Connection.Instance.OnCancelTournamentAck += UpdateUI;
Connection.Instance.OnGetDataAcks += UpdateUI; Connection.Instance.OnGetDataAcks += UpdateUI;
Connection.Instance.OnSetDataAcks += UpdateUI;
StartTournament.Pressed += () => Connection.Instance.StartTournament(); StartTournament.Pressed += () => Connection.Instance.StartTournament();
CancelTournament.Pressed += () => Connection.Instance.CancelTournament(); CancelTournament.Pressed += () => Connection.Instance.CancelTournament();
@@ -23,72 +22,60 @@ public partial class AdminControls : HBoxContainer
Timeout.ValueChanged += value => Timeout.ValueChanged += value =>
{ {
Connection.Instance.SetTournamentWait((float)value); Connection.Instance.SetMoveWait((float)value);
var time = Connection.Instance.CurrentWaitTimeout.ToString(); var time = Connection.Instance.CurrentWaitTimeout.ToString();
if (time.Length > 3) if (time.Length > 3) {
{
time = time.Substring(0, 3); time = time.Substring(0, 3);
} }
Label.Text = "Wait To Move: " + time + "s "; Label.Text = "Wait To Move: " + time + "s ";
}; };
BecomeAdmin.Pressed += ShowAuthPopup; BecomeAdmin.Pressed += showAuthPopup;
} }
public override void _ExitTree() public override void _ExitTree() {
{
Connection.Instance.OnBecomeAdmin -= UpdateUI; Connection.Instance.OnBecomeAdmin -= UpdateUI;
Connection.Instance.OnTournamentEnd -= UpdateUI; Connection.Instance.OnTournamentEnd -= UpdateUI;
Connection.Instance.OnStartTournamentAck -= UpdateUI; Connection.Instance.OnStartTournament -= UpdateUI;
Connection.Instance.OnCancelTournamentAck -= UpdateUI; Connection.Instance.OnCancelTournamentAck -= UpdateUI;
Connection.Instance.OnGetDataAcks -= UpdateUI; Connection.Instance.OnGetDataAcks -= UpdateUI;
Connection.Instance.OnSetDataAcks -= UpdateUI;
} }
private void UpdateUI() private void UpdateUI() {
{ if (!Connection.Instance.IsAdmin) {
if (!Connection.Instance.IsAdmin)
{
BecomeAdmin.Show(); BecomeAdmin.Show();
StartTournament.Hide(); StartTournament.Hide();
CancelTournament.Hide(); CancelTournament.Hide();
Label.Hide(); Label.Hide();
Timeout.Hide(); Timeout.Hide();
} } else {
else
{
BecomeAdmin.Hide(); BecomeAdmin.Hide();
Label.Show(); Label.Show();
Timeout.Show(); Timeout.Show();
} }
if (Connection.Instance.IsAdmin && Connection.Instance.DemoMode) if (Connection.Instance.IsAdmin && Connection.Instance.DemoMode) {
{
StartTournament.Hide(); StartTournament.Hide();
CancelTournament.Hide(); CancelTournament.Hide();
} } else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament != TournamentType.None) {
else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament)
{
StartTournament.Hide(); StartTournament.Hide();
CancelTournament.Show(); CancelTournament.Show();
} } else if (Connection.Instance.IsAdmin) {
else if (Connection.Instance.IsAdmin)
{
StartTournament.Show(); StartTournament.Show();
CancelTournament.Hide(); CancelTournament.Hide();
} }
Timeout.Value = Connection.Instance.CurrentWaitTimeout; Timeout.Value = Connection.Instance.CurrentWaitTimeout;
var time = Connection.Instance.CurrentWaitTimeout.ToString(); var time = Connection.Instance.CurrentWaitTimeout.ToString();
if (time.Length > 3) if (time.Length > 3) {
{
time = time.Substring(0, 3); time = time.Substring(0, 3);
} }
Label.Text = "Wait To Move: " + time + "s "; Label.Text = "Wait To Move: " + time + "s ";
} }
private void ShowAuthPopup() private void showAuthPopup() {
{
var authWindow = new Window(); var authWindow = new Window();
authWindow.Theme = GD.Load<Theme>("res://assets/theme.tres"); authWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
authWindow.AlwaysOnTop = true; authWindow.AlwaysOnTop = true;
@@ -115,17 +102,14 @@ public partial class AdminControls : HBoxContainer
passwordBox.GuiInput += e => passwordBox.GuiInput += e =>
{ {
if (passwordBox.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) if (passwordBox.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) {
{ if (inputEventKey.KeyLabel == Key.Enter) {
if (inputEventKey.KeyLabel == Key.Enter)
{
Connection.Instance.AdminAuth(passwordBox.Text); Connection.Instance.AdminAuth(passwordBox.Text);
GetTree().Root.CallDeferred(Node.MethodName.RemoveChild, authWindow); GetTree().Root.CallDeferred(Node.MethodName.RemoveChild, authWindow);
GetViewport().SetInputAsHandled(); GetViewport().SetInputAsHandled();
} }
if (inputEventKey.KeyLabel == Key.Space) if (inputEventKey.KeyLabel == Key.Space) {
{
GetViewport().SetInputAsHandled(); GetViewport().SetInputAsHandled();
} }
} }

View File

@@ -1,18 +1,13 @@
using Godot; using Godot;
using System; using System;
public partial class BackButton : TextureButton public partial class BackButton : TextureButton {
{
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn"; private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
public override void _Pressed() public override void _Pressed() {
{ transitionToBracket();
TransitionToBracket();
base._Pressed(); base._Pressed();
} }
private void TransitionToBracket() private void transitionToBracket() { GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH); }
{
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
}
} }

View File

@@ -1,8 +1,7 @@
using Godot; using Godot;
using System.Linq; using System.Linq;
public partial class BoardScreen : Node2D public partial class BoardScreen : Node2D {
{
[Export] private AudioStream endingSfx; [Export] private AudioStream endingSfx;
[Export] private Theme theme; [Export] private Theme theme;
@@ -38,8 +37,10 @@ public partial class BoardScreen : Node2D
player1Card = GetNode<Node2D>("Player1Card"); player1Card = GetNode<Node2D>("Player1Card");
player2Card = GetNode<Node2D>("Player2Card"); player2Card = GetNode<Node2D>("Player2Card");
player1Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player1Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player1Card); player1Card.GetNode<Label>("Name").Resized += () =>
player2Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player2Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player2Card); 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); redChip = GD.Load<PackedScene>(RED_CHIP_PATH);
ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH); ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH);
@@ -48,18 +49,13 @@ public partial class BoardScreen : Node2D
player1Card.GetNode<Label>("Name").Text = matchData.player1; player1Card.GetNode<Label>("Name").Text = matchData.player1;
player2Card.GetNode<Label>("Name").Text = matchData.player2; player2Card.GetNode<Label>("Name").Text = matchData.player2;
if (Connection.Instance.PreviousMoves.Count == 0) if (Connection.Instance.PreviousMoves.Count == 0) {
{
player1Card.GetNode<Label>("Status").Show(); player1Card.GetNode<Label>("Status").Show();
player2Card.GetNode<Label>("Status").Hide(); 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(); player1Card.GetNode<Label>("Status").Hide();
player2Card.GetNode<Label>("Status").Show(); player2Card.GetNode<Label>("Status").Show();
} } else {
else
{
player1Card.GetNode<Label>("Status").Show(); player1Card.GetNode<Label>("Status").Show();
player2Card.GetNode<Label>("Status").Hide(); player2Card.GetNode<Label>("Status").Hide();
} }
@@ -67,98 +63,78 @@ public partial class BoardScreen : Node2D
Connection.Instance.OnObserveWin += OnObserveWin; Connection.Instance.OnObserveWin += OnObserveWin;
Connection.Instance.OnObserveDraw += OnObserveDraw; Connection.Instance.OnObserveDraw += OnObserveDraw;
Connection.Instance.OnObserveTerminated += OnObserveTerminated; Connection.Instance.OnObserveTerminated += OnObserveTerminated;
Connection.Instance.OnObserveMove += ObserveMove; Connection.Instance.OnObserveMove += OnObserveMove;
} }
public override void _Process(double delta) public override void _Process(double delta) {
{ if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f) {
if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f)
{
var move = Connection.Instance.PreviousMoves[0]; var move = Connection.Instance.PreviousMoves[0];
Connection.Instance.PreviousMoves.RemoveAt(0); Connection.Instance.PreviousMoves.RemoveAt(0);
if (move.Item1 == matchData.player1) if (move.Item1 == matchData.player1) {
{
spawnRed(move.Item2); spawnRed(move.Item2);
} } else {
else
{
spawnYellow(move.Item2); spawnYellow(move.Item2);
} }
currentTimeout = MOVE_TIMEOUT_BEFORE_PLACE; currentTimeout = MOVE_TIMEOUT_BEFORE_PLACE;
} } else if (currentTimeout >= 0.0f) {
else if (currentTimeout >= 0.0f)
{
currentTimeout -= delta; currentTimeout -= delta;
} }
if (_lastMove) if (_lastMove) {
{ _lastMoveTimer -= (float)delta;
_lastMoveTimer -= (float) delta;
} }
if (_lastMoveTimer <= 0.0f) if (_lastMoveTimer <= 0.0f) {
{ if (_winner == "") {
if (_winner == "") showPopupMessage("Draw!");
{ } else {
PopupMessage("Draw!"); showPopupMessage(_winner + " wins!");
}
else
{
PopupMessage(_winner + " wins!");
} }
} }
} }
public override void _ExitTree() public override void _ExitTree() {
{
Connection.Instance.OnObserveWin -= OnObserveWin; Connection.Instance.OnObserveWin -= OnObserveWin;
Connection.Instance.OnObserveDraw -= OnObserveDraw; Connection.Instance.OnObserveDraw -= OnObserveDraw;
Connection.Instance.OnObserveTerminated -= OnObserveTerminated; Connection.Instance.OnObserveTerminated -= OnObserveTerminated;
Connection.Instance.OnObserveMove -= ObserveMove; Connection.Instance.OnObserveMove -= OnObserveMove;
} }
private void OnObserveWin(string winner) private void OnObserveWin(string winner) {
{
_lastMove = true; _lastMove = true;
_winner = winner; _winner = winner;
player1Card.GetNode<Label>("Status").Hide(); player1Card.GetNode<Label>("Status").Hide();
player2Card.GetNode<Label>("Status").Hide(); player2Card.GetNode<Label>("Status").Hide();
} }
private void OnObserveDraw() private void OnObserveDraw() {
{
_lastMove = true; _lastMove = true;
player1Card.GetNode<Label>("Status").Hide(); player1Card.GetNode<Label>("Status").Hide();
player2Card.GetNode<Label>("Status").Hide(); player2Card.GetNode<Label>("Status").Hide();
} }
private void OnObserveTerminated() private void OnObserveTerminated() {
{ showPopupMessage("Match Terminated");
PopupMessage("Match Terminated");
player1Card.GetNode<Label>("Status").Hide(); player1Card.GetNode<Label>("Status").Hide();
player2Card.GetNode<Label>("Status").Hide(); player2Card.GetNode<Label>("Status").Hide();
} }
private void ObserveMove(string username, int column) private void OnObserveMove(string username, int column) {
{ if (username == matchData.player1) {
if (username == matchData.player1)
{
if (!_lastMove) if (!_lastMove)
player2Card.GetNode<Label>("Status").Show(); player2Card.GetNode<Label>("Status").Show();
player1Card.GetNode<Label>("Status").Hide(); player1Card.GetNode<Label>("Status").Hide();
} } else {
else
{
if (!_lastMove) if (!_lastMove)
player1Card.GetNode<Label>("Status").Show(); player1Card.GetNode<Label>("Status").Show();
player2Card.GetNode<Label>("Status").Hide(); player2Card.GetNode<Label>("Status").Hide();
} }
Connection.Instance.PreviousMoves.Add((username, column)); Connection.Instance.PreviousMoves.Add((username, column));
} }
private void PopupMessage(string message) private void showPopupMessage(string message) {
{
var popup = new Popup(); var popup = new Popup();
popup.AlwaysOnTop = true; popup.AlwaysOnTop = true;
popup.Size = new Vector2I(200, 100); popup.Size = new Vector2I(200, 100);
@@ -174,20 +150,17 @@ public partial class BoardScreen : Node2D
text.GrowVertical = Control.GrowDirection.Both; text.GrowVertical = Control.GrowDirection.Both;
text.HorizontalAlignment = HorizontalAlignment.Center; text.HorizontalAlignment = HorizontalAlignment.Center;
text.VerticalAlignment = VerticalAlignment.Center; text.VerticalAlignment = VerticalAlignment.Center;
text.AnchorsPreset = (int) Control.LayoutPreset.FullRect; text.AnchorsPreset = (int)Control.LayoutPreset.FullRect;
popup.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen; popup.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
popup.PopupHide += () => popup.QueueFree(); popup.PopupHide += () => popup.QueueFree();
GetTree().Root.AddChild(popup); GetTree().Root.AddChild(popup);
popup.PopupCentered(); popup.PopupCentered();
sfx.Play(); sfx.Play();
popup.Show(); popup.Show();
TransitionToBracket(); transitionToBracket();
} }
private void TransitionToBracket() private void transitionToBracket() { GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH); }
{
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
}
/* /*
* Determines if the column can have a new chip placed * Determines if the column can have a new chip placed
@@ -195,16 +168,17 @@ public partial class BoardScreen : Node2D
* or * or
* Will return row of board in which chip can be placed * 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 if (col < 0 || col > 6) // Col out of range
return -1; return -1;
return getNextAvailRow(col); return getNextAvailRow(col);
} }
public int getNextAvailRow(int col) { private int getNextAvailRow(int col) {
for(int i = chips.GetLength(0) - 1; i >= 0; i--) { // Start at bottom for (int i = chips.GetLength(0) - 1; i >= 0; i--) {
if(chips[i, col] == null) // Start at bottom
if (chips[i, col] == null)
return i; return i;
} }
@@ -213,28 +187,30 @@ public partial class BoardScreen : Node2D
private void spawnRed(int col) { private void spawnRed(int col) {
int row = canPlaceOnCol(col); int row = canPlaceOnCol(col);
if(row == -1) { if (row == -1) {
GD.Print("Invalid Placement!"); GD.Print("Invalid Placement!");
return; return;
} }
RigidBody2D newNode = redChip.Instantiate<RigidBody2D>(); RigidBody2D newNode = redChip.Instantiate<RigidBody2D>();
AddChild(newNode); 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; chips[row, col] = newNode;
} }
private void spawnYellow(int col) { private void spawnYellow(int col) {
int row = canPlaceOnCol(col); int row = canPlaceOnCol(col);
if(row == -1) { if (row == -1) {
GD.Print("Invalid Placement!"); GD.Print("Invalid Placement!");
return; return;
} }
RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>(); RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>();
AddChild(newNode); 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; chips[row, col] = newNode;
} }
@@ -247,12 +223,13 @@ public partial class BoardScreen : Node2D
cardCenter.Scale = new Vector2(x, 2); cardCenter.Scale = new Vector2(x, 2);
cardCenter.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX, cardCenter.Position.Y); 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,
UP_RIGHT, UP_RIGHT,
RIGHT, RIGHT,

View File

@@ -1,109 +1,91 @@
using Godot; using Godot;
using System.Collections.Generic; 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 Tree Matches;
[Export] public Texture2D WatchButton; [Export] public Texture2D WatchButton;
[Export] public Texture2D TerminateKickButton; [Export] public Texture2D TerminateKickButton;
private const string BOARD_SCENE_PATH = "res://scenes/game.tscn"; private const string BOARD_SCENE_PATH = "res://scenes/game.tscn";
private List<PlayerData> _playerList; private List<PlayerData> playerList;
private List<MatchData> _matchList; 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.HideRoot = true;
Players.ButtonClicked += KickPlayer; Players.ButtonClicked += kickPlayer;
Matches.SetColumnTitle(0, "Match"); Matches.SetColumnTitle(0, "Match");
Matches.SetColumnTitle(1, "Red"); Matches.SetColumnTitle(1, "Red");
Matches.SetColumnTitle(2, "Yellow"); Matches.SetColumnTitle(2, "Yellow");
Matches.HideRoot = true; Matches.HideRoot = true;
Matches.ButtonClicked += WatchGame; Matches.ButtonClicked += watchGame;
Matches.ButtonClicked += TerminateGame; Matches.ButtonClicked += terminateGame;
Connection.Instance.OnUpdatedPlayers += UpdatePlayers; Connection.Instance.OnUpdatedPlayers += updatePlayers;
Connection.Instance.OnUpdatedMatches += UpdateMatches; Connection.Instance.OnUpdatedMatches += updateMatches;
Connection.Instance.OnWatchGameAck += TransitionToBoard; Connection.Instance.OnWatchGameAck += transitionToBoard;
} }
public override void _ExitTree() public override void _ExitTree() {
{ Connection.Instance.OnUpdatedPlayers -= updatePlayers;
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers; Connection.Instance.OnUpdatedMatches -= updateMatches;
Connection.Instance.OnUpdatedMatches -= UpdateMatches; Connection.Instance.OnWatchGameAck -= transitionToBoard;
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
} }
private void UpdatePlayers(List<PlayerData> playerList) private void updatePlayers(List<PlayerData> newPlayerList) {
{
Players.Clear(); Players.Clear();
_playerList = playerList; playerList = newPlayerList;
_playerList.Sort((a, b) => a.username.CompareTo(b.username)); playerList.Sort((a, b) => a.Username.CompareTo(b.Username));
var root = Players.CreateItem(); 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); var item = Players.CreateItem(root);
item.SetText(0, playerList[i].username); item.SetText(0, newPlayerList[i].Username);
item.SetText(1, playerList[i].isReady ? "Yes" : "No"); item.SetText(1, newPlayerList[i].IsReady ? "Yes" : "No");
item.SetText(2, playerList[i].isPlaying ? "Yes" : "No"); item.SetText(2, newPlayerList[i].IsPlaying ? "Yes" : "No");
if (Connection.Instance.IsAdmin) if (Connection.Instance.IsAdmin) {
{
item.AddButton(0, TerminateKickButton, i, false, "Kick"); item.AddButton(0, TerminateKickButton, i, false, "Kick");
} }
} }
} }
private void UpdateMatches(List<MatchData> matchList) private void updateMatches(List<MatchData> newMatchList) {
{
Matches.Clear(); Matches.Clear();
_matchList = matchList; matchList = newMatchList;
var root = Matches.CreateItem(); 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); var item = Matches.CreateItem(root);
item.SetText(0, matchList[i].matchId.ToString()); item.SetText(0, newMatchList[i].matchId.ToString());
item.SetText(1, matchList[i].player1); item.SetText(1, newMatchList[i].player1);
item.SetText(2, matchList[i].player2); item.SetText(2, newMatchList[i].player2);
item.AddButton(0, WatchButton, i, false, "Watch"); item.AddButton(0, WatchButton, i, false, "Watch");
if (Connection.Instance.IsAdmin) if (Connection.Instance.IsAdmin) {
{
item.AddButton(0, TerminateKickButton, 128 + i, false, "Terminate"); item.AddButton(0, TerminateKickButton, 128 + i, false, "Terminate");
} }
} }
} }
private void WatchGame(TreeItem item, long column, long id, long mouseButtonIndex) private void watchGame(TreeItem item, long column, long id, long mouseButtonIndex) {
{ if (mouseButtonIndex == 1 && column == 0 && id < 128) {
if (mouseButtonIndex == 1 && column == 0 && id < 128) Connection.Instance.SendWatchGame(matchList[(int)id].matchId);
{
Connection.Instance.SendWatchGame(_matchList[(int) id].matchId);
} }
} }
private void TerminateGame(TreeItem item, long column, long id, long mouseButtonIndex) private void terminateGame(TreeItem item, long column, long id, long mouseButtonIndex) {
{ if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && matchList[(int)id - 128] != null) {
if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && _matchList[(int) id - 128] != null) Connection.Instance.TerminateGame(matchList[(int)id - 128].matchId);
{
Connection.Instance.TerminateGame(_matchList[(int) id - 128].matchId);
} }
} }
private void KickPlayer(TreeItem item, long column, long id, long mouseButtonIndex) private void kickPlayer(TreeItem item, long column, long id, long mouseButtonIndex) {
{ if (mouseButtonIndex == 1 && column == 0) {
if (mouseButtonIndex == 1 && column == 0) Connection.Instance.KickPlayer(playerList[(int)id].Username);
{
Connection.Instance.KickPlayer(_playerList[(int) id].username);
} }
} }
private void TransitionToBoard() private void transitionToBoard() { GetTree().ChangeSceneToFile(BOARD_SCENE_PATH); }
{
GetTree().ChangeSceneToFile(BOARD_SCENE_PATH);
}
} }

View File

@@ -0,0 +1,404 @@
using System;
using System.Numerics;
/// <summary>
/// Estimates Red/Yellow/Draw chances from a Connect 4 board state.
///
/// Implementation notes:
/// - Uses a near-perfect solver core (negamax + alpha-beta + transposition table) on a standard 7x6 bitboard.
/// - Converts the exact (perfect-play) score into a "chess.com-like" practical win% using:
/// (1) a sigmoid mapping of engine score -> win probability,
/// (2) an optional complexity adjustment based on how many moves preserve the best outcome.
/// </summary>
public static class Connect4WinProbability {
public const int Width = 7;
public const int Height = 6;
/// <summary>
/// Cell content.
/// </summary>
public enum Cell {
None = 0,
Red = 1,
Yellow = 2,
}
public readonly record struct Chances(double RedWinChance, double YellowWinChance, double DrawChance) {
public Chances Normalize() {
var sum = RedWinChance + YellowWinChance + DrawChance;
if (sum <= 0) return new Chances(0.5, 0.5, 0.0);
return new Chances(RedWinChance / sum, YellowWinChance / sum, DrawChance / sum);
}
}
/// <summary>
/// Estimate win/draw chances for the given board.
///
/// Expected board shape is [7,6]. The first index is column (0..6) and the second is row (0..5).
/// Row orientation is auto-detected: this method will accept either row=0 bottom or row=0 top,
/// as long as the position is gravity-valid.
///
/// <paramref name="toMove"/> must be Red or Yellow.
/// </summary>
/// <param name="board">2D array [7,6].</param>
/// <param name="toMove">Who is to play next.</param>
/// <param name="nodeBudget">Maximum explored nodes before falling back to the best-so-far estimate.</param>
/// <param name="enableComplexityAdjustment">If true, adjusts probabilities using move uniqueness/fragility.</param>
public static Chances Evaluate(Cell[,] board, Cell toMove, int nodeBudget = 350_000, bool enableComplexityAdjustment = true) {
if (board == null) throw new ArgumentNullException(nameof(board));
if (board.GetLength(0) != Width || board.GetLength(1) != Height)
throw new ArgumentException($"Board must be [{Width},{Height}]", nameof(board));
if (toMove is not Cell.Red and not Cell.Yellow)
throw new ArgumentException("toMove must be Cell.Red or Cell.Yellow", nameof(toMove));
if (!TryParseBoard(board, out var redBits, out var yellowBits)) {
// If the board is invalid, avoid lying with a confident number.
return new Chances(0.45, 0.45, 0.10).Normalize();
}
var mask = redBits | yellowBits;
var nbMoves = BitOperations.PopCount(mask);
// If someone has already won (shouldn't happen in a "next move" position, but observers might see it).
if (HasAlignment(redBits) && HasAlignment(yellowBits)) {
// Illegal: both cannot have 4-in-a-row in a legal game.
return new Chances(0.45, 0.45, 0.10).Normalize();
}
if (HasAlignment(redBits)) return new Chances(1.0, 0.0, 0.0);
if (HasAlignment(yellowBits)) return new Chances(0.0, 1.0, 0.0);
var position = Position.FromBitboards(mask, toMove == Cell.Red ? redBits : yellowBits);
// Solve the exact perfect-play score.
var tt = new TranspositionTable(1 << 20);
var solver = new Solver(tt, nodeBudget);
int bestScore = solver.Negamax(position, alpha: -Position.MaxScore, beta: Position.MaxScore);
// Optional complexity: score all immediate child moves (only up to 7) to see how "fragile" the outcome is.
int legalMoves = 0;
int bestMoves = 0;
int drawingMoves = 0;
if (enableComplexityAdjustment) {
for (int col = 0; col < Width; col++) {
if (!position.CanPlay(col)) continue;
legalMoves++;
var child = position;
child.Play(col);
// Reuse the same TT for speed.
int score = -solver.Negamax(child, alpha: -Position.MaxScore, beta: Position.MaxScore);
if (score == bestScore) bestMoves++;
if (score == 0) drawingMoves++;
}
if (legalMoves == 0) {
// Board full.
return new Chances(0.0, 0.0, 1.0);
}
} else {
for (int col = 0; col < Width; col++) if (position.CanPlay(col)) legalMoves++;
if (legalMoves == 0) return new Chances(0.0, 0.0, 1.0);
bestMoves = Math.Max(1, legalMoves / 2);
drawingMoves = 0;
}
var (pCurrentWin, pDraw) = ScoreToPracticalProbabilities(bestScore, nbMoves, legalMoves, bestMoves, drawingMoves);
var pCurrentLoss = Math.Max(0.0, 1.0 - pDraw - pCurrentWin);
// Map from current-player POV to Red/Yellow.
Chances result = toMove == Cell.Red
? new Chances(pCurrentWin, pCurrentLoss, pDraw)
: new Chances(pCurrentLoss, pCurrentWin, pDraw);
return result.Normalize();
}
private static (double pCurrentWin, double pDraw) ScoreToPracticalProbabilities(
int score,
int nbMoves,
int legalMoves,
int bestMoves,
int drawingMoves
) {
// Normalize score by the maximum possible magnitude at this ply.
// The classic perfect-solver scoring is within [-21, 21] on a 7x6 board.
var maxAtPly = Math.Max(1, (Width * Height + 1 - nbMoves) / 2); // similar to gamesolver.org tutorial scoring
double s = Math.Clamp(score / (double)maxAtPly, -1.0, 1.0);
// Base win probability ignoring draws: a sigmoid curve similar in spirit to chess eval->win% mappings.
const double sigmoidScale = 3.0;
double pWinNoDraw = Sigmoid(s * sigmoidScale);
// Complexity/fragility: if only a few moves preserve the best outcome, the practical win% should be less extreme.
// complexity = 0 means many best moves (easy), 1 means only one best move (fragile).
double complexity = 1.0;
if (legalMoves > 0) {
complexity = 1.0 - Math.Clamp(bestMoves / (double)legalMoves, 0.0, 1.0);
}
// Blend toward 50% based on complexity.
// (If there are many good moves, keep the evaluation confident; if there's only one, flatten it.)
double flatten = 0.60 * complexity;
pWinNoDraw = Lerp(pWinNoDraw, 0.5, flatten);
// Draw propensity.
// - If perfect play draws (score == 0), put a significant mass on draw, more so if many moves keep it drawn.
// - If perfect play is decisive, keep draw small but non-zero (practical mistakes can still drift to a draw).
double drawMoveRatio = legalMoves > 0 ? (drawingMoves / (double)legalMoves) : 0.0;
double pDraw;
if (score == 0) {
pDraw = 0.55 + 0.35 * drawMoveRatio; // 0.55..0.90
// If draw is very "fragile" (few drawing moves), reduce draw slightly.
pDraw -= 0.10 * complexity;
} else {
// Keep it small, but let it rise a bit for positions where many moves still lead to a theoretical draw.
pDraw = 0.02 + 0.10 * drawMoveRatio;
// If the position is very complex, increase draw slightly (practical play drifts).
pDraw += 0.03 * complexity;
}
pDraw = Math.Clamp(pDraw, 0.0, 0.90);
// Combine.
double pWin = (1.0 - pDraw) * pWinNoDraw;
pWin = Math.Clamp(pWin, 0.0, 1.0 - pDraw);
return (pWin, pDraw);
}
private static double Sigmoid(double x) => 1.0 / (1.0 + Math.Exp(-x));
private static double Lerp(double a, double b, double t) => a + (b - a) * Math.Clamp(t, 0.0, 1.0);
private static bool TryParseBoard(Cell[,] board, out ulong redBits, out ulong yellowBits) {
// We accept either row=0 bottom OR row=0 top as long as it is gravity-valid.
// We try both and select the first valid representation.
if (TryParseBoard(board, row0IsBottom: true, out redBits, out yellowBits)) return true;
if (TryParseBoard(board, row0IsBottom: false, out redBits, out yellowBits)) return true;
redBits = 0;
yellowBits = 0;
return false;
}
private static bool TryParseBoard(Cell[,] board, bool row0IsBottom, out ulong redBits, out ulong yellowBits) {
redBits = 0;
yellowBits = 0;
for (int col = 0; col < Width; col++) {
bool seenEmptyBelow = false;
for (int rowIdx = 0; rowIdx < Height; rowIdx++) {
int row = row0IsBottom ? rowIdx : (Height - 1 - rowIdx);
var cell = board[col, row];
if (cell == Cell.None) {
seenEmptyBelow = true;
continue;
}
if (seenEmptyBelow) {
// A disc is "floating" above an empty cell in this interpretation.
return false;
}
int bitRow = rowIdx; // bottom=0
ulong bit = 1UL << (col * (Height + 1) + bitRow);
if (cell == Cell.Red) redBits |= bit;
else if (cell == Cell.Yellow) yellowBits |= bit;
else return false;
}
}
// Additional sanity: overlap check.
return (redBits & yellowBits) == 0;
}
private static bool HasAlignment(ulong pos) {
// Checks 4-in-a-row for a bitboard with (Height+1)=7 stride per column.
// Shifts correspond to:
// - 1: vertical
// - (Height+1): horizontal
// - (Height+1)+1: diagonal /
// - (Height+1)-1: diagonal \
int h1 = Height + 1;
// vertical
if (HasFour(pos, 1)) return true;
// horizontal
if (HasFour(pos, h1)) return true;
// diag / (up-right)
if (HasFour(pos, h1 + 1)) return true;
// diag \ (down-right)
if (HasFour(pos, h1 - 1)) return true;
return false;
}
private static bool HasFour(ulong pos, int shift) {
ulong m = pos & (pos >> shift);
return (m & (m >> (2 * shift))) != 0;
}
private struct Position {
// Bitboard representation (Pascal Pons / Tromp style):
// - mask: all occupied cells
// - current: stones of the player to move
public ulong Mask;
public ulong Current;
public const int MaxScore = (Width * Height + 1) / 2; // 21
private static readonly ulong[] BottomMask = new ulong[Width];
private static readonly ulong[] TopMask = new ulong[Width];
private static readonly ulong[] ColumnMask = new ulong[Width];
private static readonly ulong BoardMask;
static Position() {
for (int c = 0; c < Width; c++) {
BottomMask[c] = 1UL << (c * (Height + 1));
TopMask[c] = 1UL << (c * (Height + 1) + (Height - 1));
ulong colMask = 0;
for (int r = 0; r < Height; r++) colMask |= 1UL << (c * (Height + 1) + r);
ColumnMask[c] = colMask;
}
ulong bm = 0;
for (int c = 0; c < Width; c++) bm |= ColumnMask[c];
BoardMask = bm;
}
public static Position FromBitboards(ulong mask, ulong currentToMoveBits) {
return new Position { Mask = mask, Current = currentToMoveBits };
}
public int NbMoves() => BitOperations.PopCount(Mask);
public bool CanPlay(int col) {
if ((uint)col >= Width) return false;
return (Mask & TopMask[col]) == 0;
}
public void Play(int col) {
// Switch side-to-move by XOR with mask (classic trick).
Current ^= Mask;
// Drop a disc into the given column.
Mask |= Mask + BottomMask[col];
// Ensure Mask only contains board cells.
Mask &= BoardMask;
}
public bool IsWinningMove(int col) {
// Compute the position of the current player AFTER playing this column.
ulong pos = Current;
ulong m = Mask;
// play into column: get the bit for the new disc
ulong newMask = (m | (m + BottomMask[col])) & BoardMask;
ulong moveBit = newMask ^ m;
pos |= moveBit;
return HasAlignment(pos);
}
public ulong Key() {
// Mix mask + current into a stable 64-bit key.
// Good enough for a fixed-size transposition table.
unchecked {
ulong x = Mask * 6364136223846793005UL + 1442695040888963407UL;
return x ^ (Current * 11400714819323198485UL);
}
}
}
private sealed class TranspositionTable {
private struct Entry {
public ulong Key;
public sbyte Value;
public byte Used;
}
private readonly Entry[] _entries;
private readonly int _mask;
public TranspositionTable(int sizePowerOfTwo) {
if (sizePowerOfTwo <= 0 || (sizePowerOfTwo & (sizePowerOfTwo - 1)) != 0)
throw new ArgumentException("TT size must be a power of two", nameof(sizePowerOfTwo));
_entries = new Entry[sizePowerOfTwo];
_mask = sizePowerOfTwo - 1;
}
public bool TryGet(ulong key, out int value) {
ref var e = ref _entries[(int)key & _mask];
if (e.Used != 0 && e.Key == key) {
value = e.Value;
return true;
}
value = 0;
return false;
}
public void Put(ulong key, int value) {
ref var e = ref _entries[(int)key & _mask];
e.Key = key;
e.Value = (sbyte)Math.Clamp(value, -127, 127);
e.Used = 1;
}
}
private sealed class Solver {
private readonly TranspositionTable _tt;
private readonly int _nodeBudget;
private int _nodes;
public Solver(TranspositionTable tt, int nodeBudget) {
_tt = tt;
_nodeBudget = Math.Max(10_000, nodeBudget);
_nodes = 0;
}
public int Negamax(Position p, int alpha, int beta) {
// Budget guard: if we run out, return a conservative estimate.
if (_nodes++ > _nodeBudget) return 0;
int moves = p.NbMoves();
if (moves >= Width * Height) return 0; // draw by full board
// Tight theoretical bounds for this ply (helps alpha-beta).
int max = (Width * Height + 1 - moves) / 2;
int min = -(Width * Height - moves) / 2;
if (alpha < min) alpha = min;
if (beta > max) beta = max;
if (alpha >= beta) return alpha;
// Immediate win check.
for (int col = 0; col < Width; col++) {
if (!p.CanPlay(col)) continue;
if (p.IsWinningMove(col)) return max;
}
ulong key = p.Key();
if (_tt.TryGet(key, out int cached)) return cached;
int best = min;
// Center-first move ordering (classic Connect 4 heuristic).
// Order: 3,4,2,5,1,6,0
Span<int> order = stackalloc int[Width] { 3, 4, 2, 5, 1, 6, 0 };
for (int i = 0; i < order.Length; i++) {
int col = order[i];
if (!p.CanPlay(col)) continue;
var child = p;
child.Play(col);
int score = -Negamax(child, -beta, -alpha);
if (score > best) best = score;
if (score > alpha) alpha = score;
if (alpha >= beta) break;
}
_tt.Put(key, best);
return best;
}
}
}

View File

@@ -1,62 +1,47 @@
using Godot; using Godot;
public partial class ConnectButtonUI : Button public partial class ConnectButtonUI : Button {
{
[Export] public TextEdit AddressField; [Export] public TextEdit AddressField;
[Export] public Label ErrorLabel; [Export] public Label ErrorLabel;
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn"; 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.OnWsConnectionSuccess += OnConnectionSuccess;
Connection.Instance.OnWsConnectionFailed += OnConnectionFailed; Connection.Instance.OnWsConnectionFailed += OnConnectionFailed;
if (Connection.Instance.LastUsedConnectionAddress.Length > 0) if (Connection.Instance.LastUsedConnectionAddress.Length > 0) {
{
AddressField.Text = Connection.Instance.LastUsedConnectionAddress; AddressField.Text = Connection.Instance.LastUsedConnectionAddress;
} }
if (Connection.Instance.LastError.Length > 0) if (Connection.Instance.LastError.Length > 0) {
{
ErrorLabel.Text = Connection.Instance.LastError; ErrorLabel.Text = Connection.Instance.LastError;
} }
AddressField.GuiInput += e => AddressField.GuiInput += e =>
{ {
if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) {
{ if (inputEventKey.KeyLabel == Key.Enter) {
if (inputEventKey.KeyLabel == Key.Enter)
{
Connection.Instance.Connect(AddressField.Text); Connection.Instance.Connect(AddressField.Text);
GetViewport().SetInputAsHandled(); GetViewport().SetInputAsHandled();
} }
if (inputEventKey.KeyLabel == Key.Space) if (inputEventKey.KeyLabel == Key.Space) {
{
GetViewport().SetInputAsHandled(); GetViewport().SetInputAsHandled();
} }
} }
}; };
} }
public override void _ExitTree() public override void _ExitTree() {
{
Connection.Instance.OnWsConnectionSuccess -= OnConnectionSuccess; Connection.Instance.OnWsConnectionSuccess -= OnConnectionSuccess;
Connection.Instance.OnWsConnectionFailed -= OnConnectionFailed; Connection.Instance.OnWsConnectionFailed -= OnConnectionFailed;
} }
public override void _Pressed() public override void _Pressed() { Connection.Instance.Connect(AddressField.Text); }
{
Connection.Instance.Connect(AddressField.Text);
}
private void OnConnectionSuccess() private void OnConnectionSuccess() { GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH); }
{
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
}
private void OnConnectionFailed() private void OnConnectionFailed() {
{
ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError; ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError;
} }
} }

View File

@@ -1,18 +1,24 @@
using Godot; using Godot;
using System; using System;
using System.Collections.Generic; 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"; 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; } public static Connection Instance { get; private set; }
private WebSocketPeer _webSocket = new (); private WebSocketPeer webSocket = new WebSocketPeer();
private Thread _gameListThread;
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;
@@ -29,11 +35,14 @@ public partial class Connection : Node
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 OnStartTournamentAck; public event Action OnStartTournament;
public event Action OnTournamentEnd; public event Action OnTournamentEnd;
public event Action OnCancelTournamentAck; public event Action OnCancelTournamentAck;
public event Action OnBecomeAdmin; public event Action OnBecomeAdmin;
public event Action OnGetDataAcks; public event Action OnGetDataAcks;
public event Action OnSetDataAcks;
public event Action<List<(string player1, string player2)>> OnUpdatedReservations;
public event Action OnWsConnectionSuccess; public event Action OnWsConnectionSuccess;
public event Action OnWsConnectionFailed; public event Action OnWsConnectionFailed;
@@ -44,116 +53,97 @@ public partial class Connection : Node
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 bool ActiveTournament { get; private set; } public TournamentType ActiveTournament { get; private set; }
public bool DemoMode { get; private set; } public bool DemoMode { get; private set; }
public List<(string player1, string player2)> Reservations { get; private set; } = [];
public List<(string, int)> PreviousMoves { get; private set; } = []; public List<(string, int)> PreviousMoves { get; private set; } = [];
public double CurrentWaitTimeout { get; private set; } = 5.0; public double CurrentWaitTimeout { get; private set; } = 5.0;
public double MaxTimeout { get; private set; } = 30.0;
public MatchData CurrentObservingMatch { get; private set; } public MatchData CurrentObservingMatch { get; private set; }
public string LastUsedConnectionAddress { get; private set; } = ""; public string LastUsedConnectionAddress { get; private set; } = "";
public string LastError { 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 isConnecting = false;
private bool _connected = false; private bool isConnected = false;
private List<(string, int)> _lastScoreboard = []; private List<(string, int)> lastScoreboard = [];
private bool _shouldShowTournamentResults = false; private bool shouldShowTournamentResults = false;
private float _refreshGamePlayerListTimer = 5.0f; private float refreshGamePlayerListTimer = 5.0f;
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;
Instance.OnWsDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn"); Instance.OnWsDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
} }
public void Connect(string address) public void Connect(string address) {
{ isConnecting = true;
_connecting = true;
LastUsedConnectionAddress = address; LastUsedConnectionAddress = address;
if (_connected) if (isConnected) {
{
return; return;
} }
Error error = _webSocket.ConnectToUrl(address); Error error = webSocket.ConnectToUrl(address);
if (error != Error.Ok) if (error != Error.Ok) {
{
LastError = error.ToString(); LastError = error.ToString();
} }
} }
public override void _Process(double delta) public override void _Process(double delta) {
{ webSocket.Poll();
_webSocket.Poll(); WebSocketPeer.State state = webSocket.GetReadyState();
WebSocketPeer.State state = _webSocket.GetReadyState(); if (state == WebSocketPeer.State.Open) {
if (state == WebSocketPeer.State.Open) if (isConnecting) {
{ isConnecting = false;
if (_connecting) isConnected = true;
{
_connecting = false;
_connected = true;
LastError = ""; LastError = "";
OnWsConnectionSuccess?.Invoke(); OnWsConnectionSuccess?.Invoke();
UpdateGameList(); UpdateGameList();
UpdatePlayerList(); UpdatePlayerList();
_refreshGamePlayerListTimer = 5.0f; refreshGamePlayerListTimer = 5.0f;
} else if (_refreshGamePlayerListTimer <= 0.0f) } else if (refreshGamePlayerListTimer <= 0.0f) {
{
UpdateGameList(); UpdateGameList();
UpdatePlayerList(); UpdatePlayerList();
_refreshGamePlayerListTimer = 5.0f; refreshGamePlayerListTimer = 5.0f;
} } else {
else refreshGamePlayerListTimer -= (float)delta;
{
_refreshGamePlayerListTimer -= (float) delta;
} }
while (_webSocket.GetAvailablePacketCount() > 0) while (webSocket.GetAvailablePacketCount() > 0) {
{ string message = webSocket.GetPacket().GetStringFromUtf8();
string message = _webSocket.GetPacket().GetStringFromUtf8(); handleServerMessage(message);
HandleServerMessage(message);
} }
if (_shouldShowTournamentResults) if (shouldShowTournamentResults) {
{
var children = GetTree().Root.GetChildren(); var children = GetTree().Root.GetChildren();
foreach (var child in children) foreach (var child in children) {
{ if (child.Name.ToString() == "BracketView") {
if (child.Name.ToString() == "BracketView") shouldShowTournamentResults = false;
{ ShowTournamentScoreboard(lastScoreboard);
_shouldShowTournamentResults = false;
ShowTournamentScoreboard(_lastScoreboard);
} }
} }
} }
} } else if (state == WebSocketPeer.State.Connecting) {
else if (state == WebSocketPeer.State.Connecting)
{
// Do nothing // Do nothing
} } else if (state == WebSocketPeer.State.Closing) {
else if (state == WebSocketPeer.State.Closing)
{
// Do nothing // Do nothing
} } else if (state == WebSocketPeer.State.Closed) {
else if (state == WebSocketPeer.State.Closed) if (isConnecting) {
{ isConnecting = false;
if (_connecting)
{
_connecting = false;
OnWsConnectionFailed?.Invoke(); OnWsConnectionFailed?.Invoke();
} } else if (isConnected) {
else if (_connected) isConnected = false;
{
_connected = false;
IsAdmin = false; IsAdmin = false;
CurrentWaitTimeout = 5.0; CurrentWaitTimeout = 5.0;
ActiveTournament = false; ActiveTournament = TournamentType.None;
DemoMode = false; DemoMode = false;
_refreshGamePlayerListTimer = 5.0f; refreshGamePlayerListTimer = 5.0f;
var code = _webSocket.GetCloseCode(); var code = webSocket.GetCloseCode();
var reason = _webSocket.GetCloseReason(); var reason = webSocket.GetCloseReason();
LastError = "Unexpected Disconnect. Reason: " + reason + ", Code: " + code; LastError = "Unexpected Disconnect. Reason: " + reason + ", Code: " + code;
GD.PrintErr("WebSocket closed with code: " + code + ", reason " + reason + ". Clean: " + (code != -1)); GD.PrintErr("WebSocket closed with code: " + code + ", reason " + reason + ". Clean: " + (code != -1));
OnWsDisconnect?.Invoke(); OnWsDisconnect?.Invoke();
@@ -162,116 +152,111 @@ public partial class Connection : Node
} }
// Player commands // Player commands
public void SendConnect(string clientId) public void SendConnect(string clientId) {
{ if (string.IsNullOrWhiteSpace(clientId)) {
if (string.IsNullOrWhiteSpace(clientId))
{
GD.PrintErr("Client ID is required to CONNECT."); GD.PrintErr("Client ID is required to CONNECT.");
return; return;
} }
clientId = clientId.Trim(); clientId = clientId.Trim();
if (clientId.Contains(":")) if (clientId.Contains(":")) {
{
GD.PrintErr("Client ID cannot contain ':' characters."); GD.PrintErr("Client ID cannot contain ':' characters.");
return; return;
} }
SendCommand("CONNECT", clientId); sendCommand("CONNECT", clientId);
} }
public void SendReady() public void SendDisconnect() { sendCommand("DISCONNECT"); }
{ public void SendReady() { sendCommand("READY"); }
SendCommand("READY"); public void SendPlay(int column) { sendCommand("PLAY", column.ToString()); }
}
public void SendPlay(int column)
{
SendCommand("PLAY", column.ToString());
}
// Observer commands // Observer commands
public void UpdateGameList() public void UpdateGameList() { sendCommand("GAME", "LIST"); }
{ public void UpdatePlayerList() { sendCommand("PLAYER", "LIST"); }
SendCommand("GAME", "LIST"); public void SendWatchGame(int matchID) { sendCommand("GAME", "WATCH:" + matchID); }
} public void AdminAuth(string password) {
public void UpdatePlayerList()
{
SendCommand("PLAYER", "LIST");
}
public void SendWatchGame(int matchID)
{
SendCommand("GAME", "WATCH:" + matchID);
}
public void AdminAuth(string password)
{
if (IsAdmin) return; if (IsAdmin) return;
SendCommand("ADMIN", "AUTH:" + password); sendCommand("ADMIN", "AUTH:" + password);
} }
public void GetMoveWait() { sendCommand("GET", "MOVE_WAIT"); }
public void GetMoveWait() public void GetTournamentStatus() { sendCommand("GET", "TOURNAMENT_STATUS"); }
{ public void GetDemoMode() { sendCommand("GET", "DEMO_MODE"); }
SendCommand("GET", "MOVE_WAIT"); public void GetMaxTimeout() { sendCommand("GET", "MAX_TIMEOUT"); }
}
public void GetTournamentStatus()
{
SendCommand("GET", "TOURNAMENT_STATUS");
}
// Admin commands // Admin commands
public void KickPlayer(string playerId) public void KickPlayer(string playerId) {
{
if (!IsAdmin) return; if (!IsAdmin) return;
SendCommand("ADMIN", "KICK:" + playerId); sendCommand("ADMIN", "KICK:" + playerId);
} }
public void StartTournament() public void StartTournament(string tournamentType = "RoundRobin") {
{
if (!IsAdmin) return; if (!IsAdmin) return;
SendCommand("TOURNAMENT", "START"); sendCommand("TOURNAMENT", "START:" + tournamentType);
} }
public void CancelTournament() public void CancelTournament() {
{
if (!IsAdmin) return; if (!IsAdmin) return;
SendCommand("TOURNAMENT", "CANCEL"); sendCommand("TOURNAMENT", "CANCEL");
} }
public void TerminateGame(int matchID) public void TerminateGame(int matchID) {
{
if (!IsAdmin) return; if (!IsAdmin) return;
SendCommand("GAME", "TERMINATE:" + matchID); sendCommand("GAME", "TERMINATE:" + matchID);
} }
public void SetTournamentWait(float waitTime) public void AwardGameWinner(int matchID, string winnerUsername) {
{ if (!IsAdmin) return;
if (string.IsNullOrWhiteSpace(winnerUsername)) return;
sendCommand("GAME", "AWARD:" + matchID + ":" + winnerUsername.Trim());
}
public void SetMoveWait(float waitTime) {
if (!IsAdmin) return; if (!IsAdmin) return;
CurrentWaitTimeout = waitTime; CurrentWaitTimeout = waitTime;
SendCommand("TOURNAMENT", "WAIT:" + waitTime); sendCommand("SET", "MOVE_WAIT:" + waitTime);
} }
private void SendCommand(string header, string body = "") public void SetDemoMode(bool demoMode) {
{ if (!IsAdmin) return;
if (!IsSocketOpen) DemoMode = demoMode;
{ sendCommand("SET", "DEMO_MODE:" + demoMode);
}
public void SetMaxTimeout(float maxTimeout) {
if (!IsAdmin) return;
MaxTimeout = maxTimeout;
sendCommand("SET", "MAX_TIMEOUT:" + maxTimeout);
}
public void AddReservation(string player1Username, string player2Username) {
if (!IsAdmin) return;
sendCommand("RESERVATION", $"ADD:{player1Username},{player2Username}");
}
public void DeleteReservation(string player1Username, string player2Username) {
if (!IsAdmin) return;
sendCommand("RESERVATION", $"DELETE:{player1Username},{player2Username}");
}
public void GetReservations() {
if (!IsAdmin) return;
sendCommand("RESERVATION", "GET");
}
private void sendCommand(string header, string body = "") {
if (!IsSocketOpen) {
GD.PrintErr($"Cannot send {header}, socket is not open."); GD.PrintErr($"Cannot send {header}, socket is not open.");
return; return;
} }
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}"; string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
_webSocket.SendText(payload); webSocket.SendText(payload);
} }
private void HandleServerMessage(string message) private void handleServerMessage(string message) {
{ if (string.IsNullOrWhiteSpace(message)) {
if (string.IsNullOrWhiteSpace(message))
{
return; return;
} }
@@ -280,56 +265,46 @@ public partial class Connection : Node
string header; string header;
string body; string body;
int separatorIndex = message.IndexOf(':'); int separatorIndex = message.IndexOf(':');
if (separatorIndex >= 0) if (separatorIndex >= 0) {
{
header = message.Substring(0, separatorIndex).Trim(); header = message.Substring(0, separatorIndex).Trim();
body = message.Substring(separatorIndex + 1).Trim(); body = message.Substring(separatorIndex + 1).Trim();
} } else {
else
{
header = message.Trim(); header = message.Trim();
body = string.Empty; body = string.Empty;
} }
header = header.ToUpperInvariant(); header = header.ToUpperInvariant();
switch (header) switch (header) {
{
case "CONNECT": case "CONNECT":
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
{
IsPlayer = true; IsPlayer = true;
OnConnected?.Invoke(); OnConnected?.Invoke();
} }
break; break;
case "READY": case "READY":
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
{
OnReadyAcknowledged?.Invoke(); OnReadyAcknowledged?.Invoke();
} }
break; break;
case "GAME": case "GAME":
HandleGameMessage(body); handleGameMessage(body);
break; break;
case "PLAYER": case "PLAYER":
HandlePlayerList(body); handlePlayerList(body);
break; break;
case "OPPONENT": case "OPPONENT":
if (int.TryParse(body, out int column)) 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; break;
case "ADMIN": case "ADMIN":
if (body == "AUTH:ACK") if (body == "AUTH:ACK") {
{
IsAdmin = true; IsAdmin = true;
GetMoveWait(); GetMoveWait();
GetTournamentStatus(); GetTournamentStatus();
@@ -338,28 +313,33 @@ public partial class Connection : Node
break; break;
case "TOURNAMENT": case "TOURNAMENT":
HandleTournamentMessage(body); handleTournamentMessage(body);
break; break;
case "GET": case "GET":
if (body.StartsWith("MOVE_WAIT")) string data = body.Split(":")[1];
{ if (body.StartsWith("MOVE_WAIT")) {
CurrentWaitTimeout = double.Parse(body.Split(":")[1]); CurrentWaitTimeout = double.Parse(data);
} } else if (body.StartsWith("MAX_TIMEOUT")) {
else if (body.StartsWith("TOURNAMENT_STATUS")) MaxTimeout = double.Parse(data);
{ } else if (body.StartsWith("DEMO_MODE")) {
string status = body.Split(":")[1]; DemoMode = bool.Parse(data);
if (status != "DEMO") } else if (body.StartsWith("TOURNAMENT_STATUS")) {
{ TournamentType? type = parseTournamentType(data);
ActiveTournament = bool.Parse(status);
} if (type == null) {
else GD.PrintErr($"Unhandled tournament type: {data}");
{ } else {
ActiveTournament = false; ActiveTournament = type.Value;
DemoMode = true;
} }
} else {
GD.PrintErr($"Unhandled data get: {body}");
} }
OnGetDataAcks?.Invoke(); OnGetDataAcks?.Invoke();
break; break;
case "SET":
OnSetDataAcks?.Invoke();
break;
case "ERROR": case "ERROR":
GD.PrintErr(message); GD.PrintErr(message);
OnError?.Invoke(message); OnError?.Invoke(message);
@@ -370,10 +350,8 @@ public partial class Connection : Node
} }
} }
private void HandleTournamentMessage(string body) private void handleTournamentMessage(string body) {
{ if (string.IsNullOrWhiteSpace(body)) {
if (string.IsNullOrWhiteSpace(body))
{
return; return;
} }
@@ -381,65 +359,112 @@ public partial class Connection : Node
string command = segments[0].Trim().ToUpperInvariant(); string command = segments[0].Trim().ToUpperInvariant();
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty; string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
switch (command) switch (command) {
{ case "END": {
case "END": ActiveTournament = TournamentType.None;
{
ActiveTournament = false;
List<(string, int)> playerScoreboard = new List<(string, int)>(); List<(string, int)> playerScoreboard = new List<(string, int)>();
string[] entries = segments[1].Split("|"); string[] entries = segments[1].Split("|");
foreach (string entry in entries) foreach (string entry in entries) {
{
string[] data = entry.Split(','); string[] data = entry.Split(',');
playerScoreboard.Add((data[0], int.Parse(data[1]))); playerScoreboard.Add((data[0], int.Parse(data[1])));
} }
_lastScoreboard = playerScoreboard; lastScoreboard = playerScoreboard;
_shouldShowTournamentResults = true; shouldShowTournamentResults = true;
OnTournamentEnd?.Invoke(); OnTournamentEnd?.Invoke();
break; break;
} }
case "START": case "START": {
{ TournamentType? type = parseTournamentType(argument);
ActiveTournament = true; if (type == null) {
OnStartTournamentAck?.Invoke(); GD.PrintErr($"Unhandled tournament type: {argument}");
} else {
ActiveTournament = type.Value;
OnStartTournament?.Invoke();
}
break; break;
} }
case "CANCEL": case "CANCEL": {
{ ActiveTournament = TournamentType.None;
ActiveTournament = false;
OnCancelTournamentAck?.Invoke(); OnCancelTournamentAck?.Invoke();
break; break;
} }
} }
} }
private void HandlePlayerList(string body) private void handleReservationMessage(string body) {
{ if (string.IsNullOrWhiteSpace(body)) {
if (string.IsNullOrWhiteSpace(body))
{
return; return;
} }
string[] segments = body.Split(':'); string[] segments = body.Split(':');
string command = segments[0].Trim().ToUpperInvariant(); string command = segments[0].Trim().ToUpperInvariant();
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
switch (command) switch (command) {
{ case "ADD": {
case "LIST": if (segments.Length < 2) break;
{ string[] users = segments[1].Split(',');
if (users.Length != 2) break;
var p1 = users[0];
var p2 = users[1];
Reservations.Add((p1, p2));
OnUpdatedReservations?.Invoke(new List<(string player1, string player2)>(Reservations));
break;
}
case "DELETE": {
if (segments.Length < 2) break;
string[] users = segments[1].Split(',');
if (users.Length != 2) break;
var p1 = users[0];
var p2 = users[1];
Reservations.RemoveAll(r => r.player1 == p1 && r.player2 == p2);
OnUpdatedReservations?.Invoke(new List<(string player1, string player2)>(Reservations));
break;
}
case "LIST": {
var reservations = new List<(string player1, string player2)>();
if (segments.Length >= 2 && !string.IsNullOrWhiteSpace(segments[1])) {
string[] entries = segments[1].Split('|');
foreach (string entry in entries) {
string[] users = entry.Split(',');
if (users.Length != 2) continue;
reservations.Add((users[0], users[1]));
}
}
Reservations = reservations;
OnUpdatedReservations?.Invoke(new List<(string player1, string player2)>(Reservations));
break;
}
default:
GD.PrintErr($"Unhandled RESERVATION message: {body}");
break;
}
}
private void handlePlayerList(string body) {
if (string.IsNullOrWhiteSpace(body)) {
return;
}
string[] segments = body.Split(':');
string command = segments[0].Trim().ToUpperInvariant();
switch (command) {
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); OnUpdatedPlayers?.Invoke(players);
break; break;
} }
string[] entries = segments[1].Split("|"); string[] entries = segments[1].Split("|");
foreach (string entry in entries) foreach (string entry in entries) {
{
string[] data = entry.Split(','); string[] data = entry.Split(',');
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2]))); players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
} }
@@ -450,10 +475,8 @@ public partial class Connection : Node
} }
} }
private void HandleGameMessage(string body) private void handleGameMessage(string body) {
{ if (string.IsNullOrWhiteSpace(body)) {
if (string.IsNullOrWhiteSpace(body))
{
return; return;
} }
@@ -461,10 +484,8 @@ public partial class Connection : Node
string command = segments[0].Trim().ToUpperInvariant(); string command = segments[0].Trim().ToUpperInvariant();
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty; string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
if (IsPlayer) if (IsPlayer) {
{ switch (command) {
switch (command)
{
case "START": case "START":
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase); bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
OnGameStart?.Invoke(isFirst); OnGameStart?.Invoke(isFirst);
@@ -485,11 +506,9 @@ public partial class Connection : Node
GD.Print($"Unhandled GAME message: {body}"); GD.Print($"Unhandled GAME message: {body}");
break; break;
} }
} } else // Regular observer/admin
else // Regular observer/admin
{
switch (command)
{ {
switch (command) {
case "WIN": case "WIN":
OnObserveWin?.Invoke(segments[1]); OnObserveWin?.Invoke(segments[1]);
break; break;
@@ -505,15 +524,13 @@ public partial class Connection : Node
case "LIST": case "LIST":
List<MatchData> matches = new List<MatchData>(); List<MatchData> matches = new List<MatchData>();
if (segments.Length < 2) if (segments.Length < 2) {
{
OnUpdatedMatches?.Invoke(matches); OnUpdatedMatches?.Invoke(matches);
break; break;
} }
string[] entries = segments[1].Split("|"); string[] entries = segments[1].Split("|");
foreach (string entry in entries) foreach (string entry in entries) {
{
string[] data = entry.Split(','); string[] data = entry.Split(',');
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2])); matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
} }
@@ -524,21 +541,18 @@ public partial class Connection : Node
CurrentObservingMatch = null; CurrentObservingMatch = null;
PreviousMoves.Clear(); PreviousMoves.Clear();
string[] activeMatchData = segments[2].Split("|"); string[] activeMatchData = segments[2].Split("|");
if (activeMatchData.IsEmpty()) if (activeMatchData.IsEmpty()) {
{
string[] matchData = segments[2].Split(','); string[] matchData = segments[2].Split(',');
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]); CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]);
} } else {
else
{
string[] matchData = activeMatchData[0].Split(','); string[] matchData = activeMatchData[0].Split(',');
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]); 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(','); string[] moveData = activeMatchData[i].Split(',');
PreviousMoves.Add((moveData[0], int.Parse(moveData[1]))); PreviousMoves.Add((moveData[0], int.Parse(moveData[1])));
} }
} }
OnWatchGameAck?.Invoke(); OnWatchGameAck?.Invoke();
break; break;
default: default:
@@ -548,8 +562,7 @@ public partial class Connection : Node
} }
} }
public void ShowTournamentScoreboard(List<(string, int)> playerScoreboard) public void ShowTournamentScoreboard(List<(string, int)> playerScoreboard) {
{
var scoreboardWindow = new Window(); var scoreboardWindow = new Window();
scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres"); scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
scoreboardWindow.AlwaysOnTop = true; scoreboardWindow.AlwaysOnTop = true;
@@ -557,10 +570,7 @@ public partial class Connection : Node
scoreboardWindow.Unresizable = true; scoreboardWindow.Unresizable = true;
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen; scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
scoreboardWindow.Size = new Vector2I(256, 512); scoreboardWindow.Size = new Vector2I(256, 512);
scoreboardWindow.CloseRequested += () => scoreboardWindow.CloseRequested += () => { GetTree().Root.RemoveChild(scoreboardWindow); };
{
GetTree().Root.RemoveChild(scoreboardWindow);
};
var tree = new Tree(); var tree = new Tree();
tree.HideRoot = true; tree.HideRoot = true;
@@ -573,8 +583,7 @@ public partial class Connection : Node
tree.SetColumnTitle(1, "Score"); tree.SetColumnTitle(1, "Score");
var root = tree.CreateItem(); var root = tree.CreateItem();
foreach ((string, int) entry in playerScoreboard) foreach ((string, int) entry in playerScoreboard) {
{
var item = tree.CreateItem(root); var item = tree.CreateItem(root);
item.SetText(0, entry.Item1); item.SetText(0, entry.Item1);
item.SetText(1, entry.Item2.ToString()); item.SetText(1, entry.Item2.ToString());

View File

@@ -1,11 +1,9 @@
public class MatchData public class MatchData {
{
public int matchId { get; private set; } public int matchId { get; private set; }
public string player1 { get; private set; } public string player1 { get; private set; }
public string player2 { 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.matchId = matchId;
this.player1 = player1; this.player1 = player1;
this.player2 = player2; this.player2 = player2;

View File

@@ -1,13 +1,11 @@
public class PlayerData public class PlayerData {
{ public string Username { get; private set; }
public string username { get; private set; } public bool IsReady { get; private set; }
public bool isReady { get; private set; } public bool IsPlaying { 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) {
{ Username = username;
this.username = username; IsReady = isReady;
this.isReady = isReady; IsPlaying = isPlaying;
this.isPlaying = isPlaying;
} }
} }

View File

@@ -0,0 +1,4 @@
public enum TournamentType {
None,
RoundRobin
}

View File

@@ -0,0 +1 @@
uid://dwb6ioubllb24