From d449c0d9287ed4ca198ce1a3930b2d962b96046a Mon Sep 17 00:00:00 2001 From: hburn7 Date: Fri, 10 Mar 2023 18:36:23 -0500 Subject: [PATCH] Team colors now update from !mp team --- BanchoSharp/Multiplayer/MultiplayerLobby.cs | 41 +++++++++++++++++++-- BanchoSharpTests/MultiplayerTests.cs | 4 +- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/BanchoSharp/Multiplayer/MultiplayerLobby.cs b/BanchoSharp/Multiplayer/MultiplayerLobby.cs index 3b3742e..a2b2f6b 100644 --- a/BanchoSharp/Multiplayer/MultiplayerLobby.cs +++ b/BanchoSharp/Multiplayer/MultiplayerLobby.cs @@ -373,9 +373,13 @@ private void UpdateLobbyFromBanchoBotSettingsResponse(string banchoResponse) { UpdateGameMode(banchoResponse); } - else if (IsTeamSwapNotification(banchoResponse)) + else if (IsSelfInvokedTeamSwapNotification(banchoResponse)) { - UpdatePlayerTeam(banchoResponse); + UpdatePlayerTeamSelfInvoked(banchoResponse); + } + else if (IsManualTeamSwapNotification(banchoResponse)) + { + UpdatePlayerTeamManuallyInvoked(banchoResponse); } else if (IsTeamModeNotification(banchoResponse)) { @@ -471,6 +475,7 @@ private void UpdateLobbyFromBanchoBotSettingsResponse(string banchoResponse) InvokeOnStateChanged(); } + private bool IsManualTeamSwapNotification(string banchoResponse) => banchoResponse.StartsWith("Moved") && banchoResponse.Contains("to team "); private bool IsRoomNameNotification(string banchoResponse) => banchoResponse.StartsWith("Room name:"); private bool IsTeamModeNotification(string banchoResponse) => banchoResponse.StartsWith("Team mode:"); private bool IsHostChangingMapNotification(string banchoResponse) => banchoResponse.Equals("Host is changing map..."); @@ -497,7 +502,35 @@ private void UpdateLobbyFromBanchoBotSettingsResponse(string banchoResponse) private bool IsMatchAbortedNotification(string banchoResponse) => banchoResponse.StartsWith("Aborted the match"); private bool IsMatchSizeNotification(string banchoResponse) => banchoResponse.StartsWith("Changed match to size"); private bool IsGameModeUpdateNotification(string banchoResponse) => banchoResponse.StartsWith("Changed match mode to "); - private bool IsTeamSwapNotification(string banchoResponse) => banchoResponse.Contains("changed to Red") || banchoResponse.Contains("changed to Blue"); + + private bool IsSelfInvokedTeamSwapNotification(string banchoResponse) => banchoResponse.Contains("changed to Red") || banchoResponse.Contains("changed to Blue"); + + private void UpdatePlayerTeamManuallyInvoked(string banchoResponse) + { + var splits = banchoResponse.Split(); + var player = splits[1]; + var team = splits[^1]; + + if(team != "Red" && team != "Blue") + return; + + var match = FindPlayer(player); + if(match == null) + return; + + var prevTeam = match.Team; + switch (team.ToLower()) + { + case "red": + match.Team = TeamColor.Red; + break; + case "blue": + match.Team = TeamColor.Blue; + break; + } + + OnPlayerChangedTeam?.Invoke(new PlayerChangedTeamEventArgs(match, prevTeam)); + } private void UpdateName(string banchoResponse) { @@ -512,7 +545,7 @@ private void UpdateName(string banchoResponse) InvokeOnStateChanged(); } - private void UpdatePlayerTeam(string banchoResponse) + private void UpdatePlayerTeamSelfInvoked(string banchoResponse) { string player = banchoResponse.Split()[0]; string team = banchoResponse.Split()[^1]; diff --git a/BanchoSharpTests/MultiplayerTests.cs b/BanchoSharpTests/MultiplayerTests.cs index ac98e49..8cd0e7e 100644 --- a/BanchoSharpTests/MultiplayerTests.cs +++ b/BanchoSharpTests/MultiplayerTests.cs @@ -179,7 +179,9 @@ public void TestMpSetResponseParser() [TestCase("Timper changed to Red", "Timper", TeamColor.Red)] [TestCase("Timper changed to Blue", "Timper", TeamColor.Blue)] - public void TestTeamSwapSimple(string response, string player, TeamColor team) + [TestCase("Moved Timper to team Red", "Timper", TeamColor.Red)] + [TestCase("Moved Timper to team Blue", "Timper", TeamColor.Blue)] + public void TestTeamSwap(string response, string player, TeamColor team) { _lobby.Players.Add(new MultiplayerPlayer(_lobby, "Timper", 1));