Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hburn7 committed Apr 15, 2023
1 parent 117381f commit c17ee71
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
48 changes: 23 additions & 25 deletions BanchoSharp/BanchoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ public class BanchoClient : IBanchoClient
/// Invoker interface responsible for processing messages sent by BanchoBot.
/// </summary>
private IBanchoBotEventInvoker _banchoBotEventInvoker;
private int _deploysInPeriod;
private readonly int _messagesThreshold;
private readonly int _rateLimitIntervalSeconds;
private StreamReader? _reader;
private DateTime _resetPeriod = DateTime.MinValue;
private TcpClient? _tcp;
private StreamWriter? _writer;

private DateTime _resetPeriod = DateTime.MinValue;
private int _deploysInPeriod = 0;
private int _messagesThreshold;
private int _rateLimitIntervalSeconds;

public event Action? OnPingReceived;
public BanchoClientConfig ClientConfig { get; set; }
public IBanchoBotEvents BanchoBotEvents { get; private set; }
Expand Down Expand Up @@ -106,7 +104,7 @@ public async Task JoinChannelAsync(string name)
else if (name.StartsWith("#"))
{
await Execute($"JOIN {name}");

var channel = AddChannel(name);
OnChannelJoined?.Invoke(channel);
}
Expand All @@ -116,19 +114,6 @@ public async Task JoinChannelAsync(string name)
}
}

public async Task JoinTournamentLobbyAsync(IMultiplayerLobby lobby)
{
if (ContainsChannel(lobby.ChannelName))
{
return;
}

await Execute($"JOIN {lobby.ChannelName}");
Channels.Add(lobby);
Logger.Debug($"Added tournament lobby channel to memory: {lobby}");
OnChannelJoined?.Invoke(lobby);
}

public async Task PartChannelAsync(string name)
{
await Execute($"PART {name}");
Expand Down Expand Up @@ -184,6 +169,19 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public async Task JoinTournamentLobbyAsync(IMultiplayerLobby lobby)
{
if (ContainsChannel(lobby.ChannelName))
{
return;
}

await Execute($"JOIN {lobby.ChannelName}");
Channels.Add(lobby);
Logger.Debug($"Added tournament lobby channel to memory: {lobby}");
OnChannelJoined?.Invoke(lobby);
}

private void RemoveChannel(string channelName)
{
var ch = GetChannel(channelName);
Expand Down Expand Up @@ -278,7 +276,7 @@ private void RegisterEvents()
{
channel ??= AddChannel(priv.Recipient);
}

if (channel.MessageHistory == null)
{
Logger.Warn($"Failed to append to MessageHistory for {priv}");
Expand Down Expand Up @@ -360,7 +358,7 @@ private async Task Execute(string message)
}

await _writer!.WriteLineAsync(message);

// Rate limiter
if (_resetPeriod < DateTime.Now)
{
Expand All @@ -377,7 +375,7 @@ private async Task Execute(string message)
await Task.Delay(_resetPeriod - DateTime.Now);
}
}

OnDeploy?.Invoke(message);
}

Expand Down Expand Up @@ -461,7 +459,7 @@ public BanchoClient(BanchoClientConfig clientConfig)
{
_rateLimitIntervalSeconds = clientConfig.IsBot ? 60 : 5;
_messagesThreshold = clientConfig.IsBot ? 300 : 10;

ClientConfig = clientConfig;

if (ClientConfig.IgnoredCommands != null)
Expand All @@ -483,7 +481,7 @@ public BanchoClient(BanchoClientConfig clientConfig)
public BanchoClient()
{
ClientConfig = new BanchoClientConfig(new IrcCredentials());

_messagesThreshold = ClientConfig.IsBot ? 300 : 10;
_rateLimitIntervalSeconds = ClientConfig.IsBot ? 60 : 5;

Expand Down
5 changes: 3 additions & 2 deletions BanchoSharp/BanchoClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ public BanchoClientConfig(IIrcCredentials credentials, LogLevel logLevel = LogLe
public BanchoClientConfig(IIrcCredentials credentials, string[]? ignoredCommands,
bool saveMessages = true, LogLevel logLevel = LogLevel.Info, string host = "irc.ppy.sh", int port = 6667,
bool isBot = false)
: this(credentials, logLevel, saveMessages, host, port, isBot)
: this(credentials, logLevel, saveMessages, host, port,
isBot)
{
IgnoredCommands = ignoredCommands;
}

public bool IsBot { get; }
public IIrcCredentials Credentials { get; set; }
public bool SaveMessags { get; }
public string Host { get; }
public int Port { get; }
public bool IsBot { get; }
public string[]? IgnoredCommands { get; set; } =
{
"QUIT", "PART", "JOIN", "MODE", "366", "353", "333"
Expand Down
4 changes: 2 additions & 2 deletions BanchoSharp/BanchoSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE.txt" Pack="true" PackagePath="LICENSE.txt" />
<None Include="..\LICENSE.txt" Pack="true" PackagePath="LICENSE.txt"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Humanizer" Version="2.14.1"/>
</ItemGroup>

</Project>
19 changes: 10 additions & 9 deletions BanchoSharp/Messaging/MpSetParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BanchoSharp.Messaging;

/// <summary>
/// Responsible for handling the parsing of BanchoBot's response to !mp set
/// Responsible for handling the parsing of BanchoBot's response to !mp set
/// </summary>
public class MpSetResponseParser
{
Expand All @@ -12,16 +12,19 @@ public class MpSetResponseParser
public MpSetResponseParser(string banchoResponse)
{
_response = banchoResponse;

IsMpSetResponse = _response.StartsWith("Changed match settings to ");
if (!IsMpSetResponse)
{
return;
}

ResolvedConfiguration = Parse();
}

public bool IsMpSetResponse { get; }
public MpSetConfig? ResolvedConfiguration { get; }

private MpSetConfig? Parse()
{
// Regex to match the format, win condition, and size
Expand All @@ -35,6 +38,7 @@ public MpSetResponseParser(string banchoResponse)
{
return null;
}

if (tokens.Length == 1)
{
// Only the format was set
Expand All @@ -58,11 +62,11 @@ public MpSetResponseParser(string banchoResponse)
{
Logger.Warn($"Failed to parse {sizeParse} as an integer. Size unable to be determined.");
}

config.Format = (LobbyFormat)Enum.Parse(typeof(LobbyFormat), tokens[1].Trim());
config.WinCondition = (WinCondition)Enum.Parse(typeof(WinCondition), tokens[2].Trim());
}

return config;
}
catch (Exception e)
Expand All @@ -71,14 +75,11 @@ public MpSetResponseParser(string banchoResponse)
return null;
}
}

public bool IsMpSetResponse { get; }
public MpSetConfig? ResolvedConfiguration { get; private set; }
}

public struct MpSetConfig
{
public LobbyFormat Format { get; set; } // AKA teammode in docs
public LobbyFormat Format { get; set; } // AKA teammode in docs
public WinCondition? WinCondition { get; set; } // AKA scoremode in docs
public int? Size { get; set; }
}
Loading

0 comments on commit c17ee71

Please sign in to comment.