misc: fmt
This commit is contained in:
@@ -6,14 +6,14 @@ 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;
|
||||
}
|
||||
switch (type) {
|
||||
case "RoundRobin":
|
||||
return TournamentType.RoundRobin;
|
||||
case "false":
|
||||
return TournamentType.None;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection Instance { get; private set; }
|
||||
@@ -74,100 +74,98 @@ public partial class Connection : Node {
|
||||
private float refreshGamePlayerListTimer = 5.0f;
|
||||
|
||||
public override void _Ready() {
|
||||
Instance = this;
|
||||
webSocket.SetHeartbeatInterval(5.0);
|
||||
webSocket.HeartbeatInterval = 5.0;
|
||||
Instance.OnWsDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
|
||||
Instance = this;
|
||||
webSocket.SetHeartbeatInterval(5.0);
|
||||
webSocket.HeartbeatInterval = 5.0;
|
||||
Instance.OnWsDisconnect += () => GetTree().ChangeSceneToFile("res://scenes/main_menu.tscn");
|
||||
}
|
||||
|
||||
public void Connect(string address) {
|
||||
isConnecting = true;
|
||||
LastUsedConnectionAddress = address;
|
||||
if (isConnected) {
|
||||
return;
|
||||
}
|
||||
isConnecting = true;
|
||||
LastUsedConnectionAddress = address;
|
||||
if (isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
Error error = webSocket.ConnectToUrl(address);
|
||||
if (error != Error.Ok) {
|
||||
LastError = error.ToString();
|
||||
}
|
||||
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 (isConnecting) {
|
||||
isConnecting = false;
|
||||
isConnected = true;
|
||||
LastError = "";
|
||||
OnWsConnectionSuccess?.Invoke();
|
||||
UpdateGameList();
|
||||
UpdatePlayerList();
|
||||
GetReservations();
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
} else if (refreshGamePlayerListTimer <= 0.0f) {
|
||||
UpdateGameList();
|
||||
UpdatePlayerList();
|
||||
GetReservations();
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
} else {
|
||||
refreshGamePlayerListTimer -= (float)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) {
|
||||
UpdateGameList();
|
||||
UpdatePlayerList();
|
||||
refreshGamePlayerListTimer = 5.0f;
|
||||
} 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) {
|
||||
var children = GetTree().Root.GetChildren();
|
||||
foreach (var child in children) {
|
||||
if (child.Name.ToString() == "BracketView") {
|
||||
shouldShowTournamentResults = false;
|
||||
ShowTournamentScoreboard(lastScoreboard);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (state == WebSocketPeer.State.Connecting) {
|
||||
// Do nothing
|
||||
} else if (state == WebSocketPeer.State.Closing) {
|
||||
// Do nothing
|
||||
} else if (state == WebSocketPeer.State.Closed) {
|
||||
if (isConnecting) {
|
||||
isConnecting = false;
|
||||
OnWsConnectionFailed?.Invoke();
|
||||
} else if (isConnected) {
|
||||
isConnected = false;
|
||||
IsAdmin = false;
|
||||
CurrentWaitTimeout = 5.0;
|
||||
ActiveTournament = TournamentType.None;
|
||||
DemoMode = false;
|
||||
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();
|
||||
}
|
||||
}
|
||||
if (shouldShowTournamentResults) {
|
||||
var children = GetTree().Root.GetChildren();
|
||||
foreach (var child in children) {
|
||||
if (child.Name.ToString() == "BracketView") {
|
||||
shouldShowTournamentResults = false;
|
||||
ShowTournamentScoreboard(lastScoreboard);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (state == WebSocketPeer.State.Connecting) {
|
||||
// Do nothing
|
||||
} else if (state == WebSocketPeer.State.Closing) {
|
||||
// Do nothing
|
||||
} else if (state == WebSocketPeer.State.Closed) {
|
||||
if (isConnecting) {
|
||||
isConnecting = false;
|
||||
OnWsConnectionFailed?.Invoke();
|
||||
} else if (isConnected) {
|
||||
isConnected = false;
|
||||
IsAdmin = false;
|
||||
CurrentWaitTimeout = 5.0;
|
||||
ActiveTournament = TournamentType.None;
|
||||
DemoMode = false;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Player commands
|
||||
public void SendConnect(string clientId) {
|
||||
if (string.IsNullOrWhiteSpace(clientId)) {
|
||||
GD.PrintErr("Client ID is required to CONNECT.");
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(clientId)) {
|
||||
GD.PrintErr("Client ID is required to CONNECT.");
|
||||
return;
|
||||
}
|
||||
|
||||
clientId = clientId.Trim();
|
||||
clientId = clientId.Trim();
|
||||
|
||||
if (clientId.Contains(":")) {
|
||||
GD.PrintErr("Client ID cannot contain ':' characters.");
|
||||
return;
|
||||
}
|
||||
if (clientId.Contains(":")) {
|
||||
GD.PrintErr("Client ID cannot contain ':' characters.");
|
||||
return;
|
||||
}
|
||||
|
||||
sendCommand("CONNECT", clientId);
|
||||
sendCommand("CONNECT", clientId);
|
||||
}
|
||||
|
||||
public void SendDisconnect() { sendCommand("DISCONNECT"); }
|
||||
@@ -179,8 +177,8 @@ public partial class Connection : Node {
|
||||
public void UpdatePlayerList() { sendCommand("PLAYER", "LIST"); }
|
||||
public void SendWatchGame(int matchID) { sendCommand("GAME", "WATCH:" + matchID); }
|
||||
public void AdminAuth(string password) {
|
||||
if (IsAdmin) return;
|
||||
sendCommand("ADMIN", "AUTH:" + password);
|
||||
if (IsAdmin) return;
|
||||
sendCommand("ADMIN", "AUTH:" + password);
|
||||
}
|
||||
public void GetMoveWait() { sendCommand("GET", "MOVE_WAIT"); }
|
||||
public void GetTournamentStatus() { sendCommand("GET", "TOURNAMENT_STATUS"); }
|
||||
@@ -189,23 +187,23 @@ public partial class Connection : Node {
|
||||
|
||||
// Admin commands
|
||||
public void KickPlayer(string playerId) {
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("ADMIN", "KICK:" + playerId);
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("ADMIN", "KICK:" + playerId);
|
||||
}
|
||||
|
||||
public void StartTournament(string tournamentType = "RoundRobin") {
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("TOURNAMENT", "START:" + tournamentType);
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("TOURNAMENT", "START:" + tournamentType);
|
||||
}
|
||||
|
||||
public void CancelTournament() {
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("TOURNAMENT", "CANCEL");
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("TOURNAMENT", "CANCEL");
|
||||
}
|
||||
|
||||
public void TerminateGame(int matchID) {
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("GAME", "TERMINATE:" + matchID);
|
||||
if (!IsAdmin) return;
|
||||
sendCommand("GAME", "TERMINATE:" + matchID);
|
||||
}
|
||||
|
||||
public void AwardGameWinner(int matchID, string winnerUsername) {
|
||||
@@ -215,21 +213,21 @@ public partial class Connection : Node {
|
||||
}
|
||||
|
||||
public void SetMoveWait(float waitTime) {
|
||||
if (!IsAdmin) return;
|
||||
CurrentWaitTimeout = waitTime;
|
||||
sendCommand("SET", "MOVE_WAIT:" + waitTime);
|
||||
if (!IsAdmin) return;
|
||||
CurrentWaitTimeout = waitTime;
|
||||
sendCommand("SET", "MOVE_WAIT:" + waitTime);
|
||||
}
|
||||
|
||||
public void SetDemoMode(bool demoMode) {
|
||||
if (!IsAdmin) return;
|
||||
DemoMode = demoMode;
|
||||
sendCommand("SET", "DEMO_MODE:" + 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);
|
||||
if (!IsAdmin) return;
|
||||
MaxTimeout = maxTimeout;
|
||||
sendCommand("SET", "MAX_TIMEOUT:" + maxTimeout);
|
||||
}
|
||||
|
||||
public void AddReservation(string player1Username, string player2Username) {
|
||||
@@ -248,155 +246,151 @@ public partial class Connection : Node {
|
||||
}
|
||||
|
||||
private void sendCommand(string header, string body = "") {
|
||||
if (!IsSocketOpen) {
|
||||
GD.PrintErr($"Cannot send {header}, socket is not open.");
|
||||
return;
|
||||
}
|
||||
if (!IsSocketOpen) {
|
||||
GD.PrintErr($"Cannot send {header}, socket is not open.");
|
||||
return;
|
||||
}
|
||||
|
||||
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
|
||||
webSocket.SendText(payload);
|
||||
string payload = string.IsNullOrEmpty(body) ? header : $"{header}:{body}";
|
||||
webSocket.SendText(payload);
|
||||
}
|
||||
|
||||
private void handleServerMessage(string message) {
|
||||
if (string.IsNullOrWhiteSpace(message)) {
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
message = message.Trim();
|
||||
message = message.Trim();
|
||||
|
||||
string header;
|
||||
string body;
|
||||
int separatorIndex = message.IndexOf(':');
|
||||
if (separatorIndex >= 0) {
|
||||
header = message.Substring(0, separatorIndex).Trim();
|
||||
body = message.Substring(separatorIndex + 1).Trim();
|
||||
} else {
|
||||
header = message.Trim();
|
||||
body = string.Empty;
|
||||
}
|
||||
string header;
|
||||
string body;
|
||||
int separatorIndex = message.IndexOf(':');
|
||||
if (separatorIndex >= 0) {
|
||||
header = message.Substring(0, separatorIndex).Trim();
|
||||
body = message.Substring(separatorIndex + 1).Trim();
|
||||
} else {
|
||||
header = message.Trim();
|
||||
body = string.Empty;
|
||||
}
|
||||
|
||||
header = header.ToUpperInvariant();
|
||||
header = header.ToUpperInvariant();
|
||||
|
||||
switch (header) {
|
||||
case "CONNECT":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
IsPlayer = true;
|
||||
OnConnected?.Invoke();
|
||||
}
|
||||
switch (header) {
|
||||
case "CONNECT":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
IsPlayer = true;
|
||||
OnConnected?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
case "READY":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
OnReadyAcknowledged?.Invoke();
|
||||
}
|
||||
break;
|
||||
case "READY":
|
||||
if (body.Equals("ACK", StringComparison.OrdinalIgnoreCase)) {
|
||||
OnReadyAcknowledged?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
case "GAME":
|
||||
handleGameMessage(body);
|
||||
break;
|
||||
case "PLAYER":
|
||||
handlePlayerList(body);
|
||||
break;
|
||||
case "OPPONENT":
|
||||
if (int.TryParse(body, out int column)) {
|
||||
OnOpponentMove?.Invoke(column);
|
||||
} else {
|
||||
GD.PrintErr($"Invalid opponent column: {body}");
|
||||
}
|
||||
break;
|
||||
case "GAME":
|
||||
handleGameMessage(body);
|
||||
break;
|
||||
case "PLAYER":
|
||||
handlePlayerList(body);
|
||||
break;
|
||||
case "OPPONENT":
|
||||
if (int.TryParse(body, out int column)) {
|
||||
OnOpponentMove?.Invoke(column);
|
||||
} else {
|
||||
GD.PrintErr($"Invalid opponent column: {body}");
|
||||
}
|
||||
|
||||
break;
|
||||
case "ADMIN":
|
||||
if (body == "AUTH:ACK") {
|
||||
IsAdmin = true;
|
||||
GetMoveWait();
|
||||
GetTournamentStatus();
|
||||
GetReservations();
|
||||
OnBecomeAdmin?.Invoke();
|
||||
}
|
||||
break;
|
||||
case "ADMIN":
|
||||
if (body == "AUTH:ACK") {
|
||||
IsAdmin = true;
|
||||
GetMoveWait();
|
||||
GetTournamentStatus();
|
||||
OnBecomeAdmin?.Invoke();
|
||||
}
|
||||
|
||||
break;
|
||||
case "TOURNAMENT":
|
||||
handleTournamentMessage(body);
|
||||
break;
|
||||
case "RESERVATION":
|
||||
handleReservationMessage(body);
|
||||
break;
|
||||
case "GET":
|
||||
string data = body.Split(":")[1];
|
||||
if (body.StartsWith("MOVE_WAIT")) {
|
||||
CurrentWaitTimeout = double.Parse(data);
|
||||
} else if (body.StartsWith("MAX_TIMEOUT")) {
|
||||
MaxTimeout = double.Parse(data);
|
||||
} else if (body.StartsWith("DEMO_MODE")) {
|
||||
DemoMode = bool.Parse(data);
|
||||
} else if (body.StartsWith("TOURNAMENT_STATUS")) {
|
||||
TournamentType? type = parseTournamentType(data);
|
||||
break;
|
||||
case "TOURNAMENT":
|
||||
handleTournamentMessage(body);
|
||||
break;
|
||||
case "GET":
|
||||
string data = body.Split(":")[1];
|
||||
if (body.StartsWith("MOVE_WAIT")) {
|
||||
CurrentWaitTimeout = double.Parse(data);
|
||||
} else if (body.StartsWith("MAX_TIMEOUT")) {
|
||||
MaxTimeout = double.Parse(data);
|
||||
} 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}");
|
||||
}
|
||||
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);
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled server message: {message}");
|
||||
break;
|
||||
}
|
||||
OnGetDataAcks?.Invoke();
|
||||
break;
|
||||
case "SET":
|
||||
OnSetDataAcks?.Invoke();
|
||||
break;
|
||||
case "ERROR":
|
||||
GD.PrintErr(message);
|
||||
OnError?.Invoke(message);
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled server message: {message}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTournamentMessage(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
string[] segments = body.Split(':');
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||
|
||||
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) {
|
||||
string[] data = entry.Split(',');
|
||||
playerScoreboard.Add((data[0], int.Parse(data[1])));
|
||||
}
|
||||
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) {
|
||||
string[] data = entry.Split(',');
|
||||
playerScoreboard.Add((data[0], int.Parse(data[1])));
|
||||
}
|
||||
|
||||
lastScoreboard = playerScoreboard;
|
||||
shouldShowTournamentResults = true;
|
||||
OnTournamentEnd?.Invoke();
|
||||
break;
|
||||
}
|
||||
case "START": {
|
||||
TournamentType? type = parseTournamentType(argument);
|
||||
if (type == null) {
|
||||
GD.PrintErr($"Unhandled tournament type: {argument}");
|
||||
} else {
|
||||
ActiveTournament = type.Value;
|
||||
OnStartTournament?.Invoke();
|
||||
}
|
||||
lastScoreboard = playerScoreboard;
|
||||
shouldShowTournamentResults = true;
|
||||
OnTournamentEnd?.Invoke();
|
||||
break;
|
||||
}
|
||||
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 = TournamentType.None;
|
||||
OnCancelTournamentAck?.Invoke();
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "CANCEL": {
|
||||
ActiveTournament = TournamentType.None;
|
||||
OnCancelTournamentAck?.Invoke();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleReservationMessage(string body) {
|
||||
@@ -453,151 +447,151 @@ public partial class Connection : Node {
|
||||
}
|
||||
|
||||
private void handlePlayerList(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
|
||||
string[] segments = body.Split(':');
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string[] segments = body.Split(':');
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
|
||||
switch (command) {
|
||||
case "LIST": {
|
||||
List<PlayerData> players = new List<PlayerData>();
|
||||
switch (command) {
|
||||
case "LIST": {
|
||||
List<PlayerData> players = new List<PlayerData>();
|
||||
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedPlayers?.Invoke(players);
|
||||
break;
|
||||
}
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedPlayers?.Invoke(players);
|
||||
break;
|
||||
}
|
||||
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
|
||||
}
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
players.Add(new PlayerData(data[0], bool.Parse(data[1]), bool.Parse(data[2])));
|
||||
}
|
||||
|
||||
OnUpdatedPlayers?.Invoke(players);
|
||||
break;
|
||||
}
|
||||
}
|
||||
OnUpdatedPlayers?.Invoke(players);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleGameMessage(string body) {
|
||||
if (string.IsNullOrWhiteSpace(body)) {
|
||||
return;
|
||||
}
|
||||
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;
|
||||
string[] segments = body.Split(':');
|
||||
string command = segments[0].Trim().ToUpperInvariant();
|
||||
string argument = segments.Length > 1 ? segments[1].Trim() : string.Empty;
|
||||
|
||||
if (IsPlayer) {
|
||||
switch (command) {
|
||||
case "START":
|
||||
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||
OnGameStart?.Invoke(isFirst);
|
||||
break;
|
||||
case "WINS":
|
||||
OnGameWin?.Invoke();
|
||||
break;
|
||||
case "LOSS":
|
||||
OnGameLoss?.Invoke();
|
||||
break;
|
||||
case "DRAW":
|
||||
OnGameDraw?.Invoke();
|
||||
break;
|
||||
case "TERMINATED":
|
||||
OnGameTerminated?.Invoke();
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled GAME message: {body}");
|
||||
break;
|
||||
}
|
||||
} else // Regular observer/admin
|
||||
{
|
||||
switch (command) {
|
||||
case "WIN":
|
||||
OnObserveWin?.Invoke(segments[1]);
|
||||
break;
|
||||
case "MOVE":
|
||||
OnObserveMove?.Invoke(segments[1], int.Parse(segments[2]));
|
||||
break;
|
||||
case "DRAW":
|
||||
OnObserveDraw?.Invoke();
|
||||
break;
|
||||
case "TERMINATED":
|
||||
OnObserveTerminated?.Invoke();
|
||||
break;
|
||||
case "LIST":
|
||||
List<MatchData> matches = new List<MatchData>();
|
||||
if (IsPlayer) {
|
||||
switch (command) {
|
||||
case "START":
|
||||
bool isFirst = argument == "1" || argument.Equals("TRUE", StringComparison.OrdinalIgnoreCase);
|
||||
OnGameStart?.Invoke(isFirst);
|
||||
break;
|
||||
case "WINS":
|
||||
OnGameWin?.Invoke();
|
||||
break;
|
||||
case "LOSS":
|
||||
OnGameLoss?.Invoke();
|
||||
break;
|
||||
case "DRAW":
|
||||
OnGameDraw?.Invoke();
|
||||
break;
|
||||
case "TERMINATED":
|
||||
OnGameTerminated?.Invoke();
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled GAME message: {body}");
|
||||
break;
|
||||
}
|
||||
} else // Regular observer/admin
|
||||
{
|
||||
switch (command) {
|
||||
case "WIN":
|
||||
OnObserveWin?.Invoke(segments[1]);
|
||||
break;
|
||||
case "MOVE":
|
||||
OnObserveMove?.Invoke(segments[1], int.Parse(segments[2]));
|
||||
break;
|
||||
case "DRAW":
|
||||
OnObserveDraw?.Invoke();
|
||||
break;
|
||||
case "TERMINATED":
|
||||
OnObserveTerminated?.Invoke();
|
||||
break;
|
||||
case "LIST":
|
||||
List<MatchData> matches = new List<MatchData>();
|
||||
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedMatches?.Invoke(matches);
|
||||
break;
|
||||
}
|
||||
if (segments.Length < 2) {
|
||||
OnUpdatedMatches?.Invoke(matches);
|
||||
break;
|
||||
}
|
||||
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
|
||||
}
|
||||
string[] entries = segments[1].Split("|");
|
||||
foreach (string entry in entries) {
|
||||
string[] data = entry.Split(',');
|
||||
matches.Add(new MatchData(int.Parse(data[0]), data[1], data[2]));
|
||||
}
|
||||
|
||||
OnUpdatedMatches?.Invoke(matches);
|
||||
break;
|
||||
case "WATCH":
|
||||
CurrentObservingMatch = null;
|
||||
PreviousMoves.Clear();
|
||||
string[] activeMatchData = segments[2].Split("|");
|
||||
if (activeMatchData.IsEmpty()) {
|
||||
string[] matchData = segments[2].Split(',');
|
||||
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]);
|
||||
} 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++) {
|
||||
string[] moveData = activeMatchData[i].Split(',');
|
||||
PreviousMoves.Add((moveData[0], int.Parse(moveData[1])));
|
||||
}
|
||||
}
|
||||
OnUpdatedMatches?.Invoke(matches);
|
||||
break;
|
||||
case "WATCH":
|
||||
CurrentObservingMatch = null;
|
||||
PreviousMoves.Clear();
|
||||
string[] activeMatchData = segments[2].Split("|");
|
||||
if (activeMatchData.IsEmpty()) {
|
||||
string[] matchData = segments[2].Split(',');
|
||||
CurrentObservingMatch = new MatchData(int.Parse(matchData[0]), matchData[1], matchData[2]);
|
||||
} 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++) {
|
||||
string[] moveData = activeMatchData[i].Split(',');
|
||||
PreviousMoves.Add((moveData[0], int.Parse(moveData[1])));
|
||||
}
|
||||
}
|
||||
|
||||
OnWatchGameAck?.Invoke();
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled GAME message: {body}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
OnWatchGameAck?.Invoke();
|
||||
break;
|
||||
default:
|
||||
GD.Print($"Unhandled GAME message: {body}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowTournamentScoreboard(List<(string, int)> playerScoreboard) {
|
||||
var scoreboardWindow = new Window();
|
||||
scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
scoreboardWindow.AlwaysOnTop = true;
|
||||
scoreboardWindow.MaximizeDisabled = true;
|
||||
scoreboardWindow.Unresizable = true;
|
||||
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
|
||||
scoreboardWindow.Size = new Vector2I(256, 512);
|
||||
scoreboardWindow.CloseRequested += () => { GetTree().Root.RemoveChild(scoreboardWindow); };
|
||||
var scoreboardWindow = new Window();
|
||||
scoreboardWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
scoreboardWindow.AlwaysOnTop = true;
|
||||
scoreboardWindow.MaximizeDisabled = true;
|
||||
scoreboardWindow.Unresizable = true;
|
||||
scoreboardWindow.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
|
||||
scoreboardWindow.Size = new Vector2I(256, 512);
|
||||
scoreboardWindow.CloseRequested += () => { GetTree().Root.RemoveChild(scoreboardWindow); };
|
||||
|
||||
var tree = new Tree();
|
||||
tree.HideRoot = true;
|
||||
tree.Columns = 2;
|
||||
tree.ColumnTitlesVisible = true;
|
||||
tree.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
tree.GrowHorizontal = Control.GrowDirection.Both;
|
||||
tree.GrowVertical = Control.GrowDirection.Both;
|
||||
tree.SetColumnTitle(0, "Player");
|
||||
tree.SetColumnTitle(1, "Score");
|
||||
var root = tree.CreateItem();
|
||||
var tree = new Tree();
|
||||
tree.HideRoot = true;
|
||||
tree.Columns = 2;
|
||||
tree.ColumnTitlesVisible = true;
|
||||
tree.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
tree.GrowHorizontal = Control.GrowDirection.Both;
|
||||
tree.GrowVertical = Control.GrowDirection.Both;
|
||||
tree.SetColumnTitle(0, "Player");
|
||||
tree.SetColumnTitle(1, "Score");
|
||||
var root = tree.CreateItem();
|
||||
|
||||
foreach ((string, int) entry in playerScoreboard) {
|
||||
var item = tree.CreateItem(root);
|
||||
item.SetText(0, entry.Item1);
|
||||
item.SetText(1, entry.Item2.ToString());
|
||||
}
|
||||
foreach ((string, int) entry in playerScoreboard) {
|
||||
var item = tree.CreateItem(root);
|
||||
item.SetText(0, entry.Item1);
|
||||
item.SetText(1, entry.Item2.ToString());
|
||||
}
|
||||
|
||||
scoreboardWindow.AddChild(tree);
|
||||
tree.SetAnchorsPreset(Control.LayoutPreset.FullRect);
|
||||
scoreboardWindow.AddChild(tree);
|
||||
tree.SetAnchorsPreset(Control.LayoutPreset.FullRect);
|
||||
|
||||
GetTree().Root.AddChild(scoreboardWindow);
|
||||
GetTree().Root.AddChild(scoreboardWindow);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user