Skip to content

Commit

Permalink
Add FinalizedEvent Fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
TheR00st3r committed Nov 18, 2023
1 parent 1eec025 commit 6d7aa3b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.private_members_with_underscore.symbols = private_fields
dotnet_naming_rule.private_members_with_underscore.style = prefix_underscore
dotnet_naming_rule.private_members_with_underscore.severity = suggestion

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
Expand All @@ -76,6 +80,12 @@ dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, meth
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

dotnet_naming_style.prefix_underscore.capitalization = pascal_case
dotnet_naming_style.prefix_underscore.required_prefix = _

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
Expand All @@ -92,3 +102,5 @@ dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_readonly_field = true:suggestion
dotnet_style_qualification_for_field = false:silent
7 changes: 4 additions & 3 deletions PugSharp.Match/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class Match : IDisposable

public MatchInfo MatchInfo { get; private set; } = default!;

public event EventHandler<MatchFinalizedEventArgs>? MatchFinalized;

public IEnumerable<MatchPlayer> AllMatchPlayers => MatchInfo?.MatchTeam1.Players.Concat(MatchInfo.MatchTeam2.Players) ?? Enumerable.Empty<MatchPlayer>();

public Match(IServiceProvider serviceProvider, ILogger<Match> logger, IApiProvider apiProvider, ITextHelper textHelper, ICsServer csServer)
Expand Down Expand Up @@ -546,8 +548,7 @@ private async Task CompleteMatchAsync()

await _ApiProvider.FreeServerAsync(CancellationToken.None).ConfigureAwait(false);

// TODO Application Melden, dass Match aufgeräumt werden kann (Dispose, abnullen)
//_Application.CleanUpMatch();
MatchFinalized?.Invoke(this, new MatchFinalizedEventArgs());
}

private void MatchLive()
Expand Down Expand Up @@ -720,7 +721,7 @@ private MatchTeam GetOtherTeam(MatchTeam team)
private void ShowMenuToTeam(MatchTeam team, string title, IEnumerable<MenuOption> options)
{
DoForAll(team.Players.Select(p => p.Player).ToList(), p => p.ShowMenu(title, options));
}
}

private void SwitchVotingTeam()
{
Expand Down
5 changes: 5 additions & 0 deletions PugSharp.Match/MatchFinalizedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PugSharp.Match;

public class MatchFinalizedEventArgs : EventArgs
{
}
44 changes: 42 additions & 2 deletions PugSharp/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Application : IApplication
public string PugSharpDirectory { get; }

private Match.Match? _Match;
private bool _DisposedValue;
private readonly CurrentRoundState _CurrentRountState = new();

/// <summary>
Expand Down Expand Up @@ -824,15 +825,26 @@ public void OnCommandStopMatch(CCSPlayerController? player, CommandInfo command)
}
var resetMap = _Match.MatchInfo.Config.VoteMap;
_Match.Dispose();
_Match = null;
StopMatch();
ResetServer(resetMap);
command.ReplyToCommand("Match stopped!");
},
command,
player);
}

private void StopMatch()
{
if (_Match == null)
{
return;
}

_Match.MatchFinalized -= OnMatchFinalized;
_Match.Dispose();
_Match = null;
}

[ConsoleCommand("css_loadconfig", "Load a match config")]
[ConsoleCommand("ps_loadconfig", "Load a match config")]
[RequiresPermissions("@pugsharp/matchadmin")]
Expand Down Expand Up @@ -1262,16 +1274,24 @@ private void InitializeMatch(MatchConfig matchConfig)
{
ResetForMatch(matchConfig);
_Match = _ServiceProvider.GetRequiredService<Match.Match>();
_Match.MatchFinalized += OnMatchFinalized;
_Match.Initialize(matchConfig);
}

private void InitializeMatch(MatchInfo matchInfo, string roundBackupFile)
{
ResetForMatch(matchInfo.Config);
_Match = _ServiceProvider.GetRequiredService<Match.Match>();
_Match.MatchFinalized += OnMatchFinalized;
_Match.Initialize(matchInfo, roundBackupFile);
}

private void OnMatchFinalized(object? sender, MatchFinalizedEventArgs e)
{
StopMatch();
}


private void ResetForMatch(MatchConfig matchConfig)
{
ResetServer(matchConfig.VoteMap);
Expand Down Expand Up @@ -1318,6 +1338,26 @@ private void ResetServer(string map)
_CsServer.SwitchMap(map);
}

protected virtual void Dispose(bool disposing)
{
if (!_DisposedValue)
{
if (disposing)
{
StopMatch();
}

_DisposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}


#endregion
}
6 changes: 4 additions & 2 deletions PugSharp/IApplication.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace PugSharp;
using System;

namespace PugSharp;

/// <summary>
/// Application interface
/// </summary>
public interface IApplication
public interface IApplication : IDisposable
{
/// <summary>
/// Initialize event registrations and more
Expand Down
1 change: 1 addition & 0 deletions PugSharp/PugSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public override void Load(bool hotReload)
public override void Unload(bool hotReload)
{
// Remove reference
_Application?.Dispose();
_Application = null;

// Dispose service provider
Expand Down

0 comments on commit 6d7aa3b

Please sign in to comment.