Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryCatch Finally to FinalizeMatch #59

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions PugSharp.Match/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,41 +514,51 @@ private void TryCompleteMatch()

private async Task CompleteMatchAsync()
{
_CsServer.StopDemoRecording();
try
{
_CsServer.StopDemoRecording();

var delay = 15;
var delay = 15;

if (_CsServer.GetConvar<bool>("tv_enable") || _CsServer.GetConvar<bool>("tv_enable1"))
{
// TV Delay in s
var tvDelaySeconds = Math.Max(_CsServer.GetConvar<int>("tv_delay"), _CsServer.GetConvar<int>("tv_delay1"));
_Logger.LogInformation("Waiting for sourceTV. Delay: {delay}s + 15s", tvDelaySeconds);
delay += tvDelaySeconds;
}
if (_CsServer.GetConvar<bool>("tv_enable") || _CsServer.GetConvar<bool>("tv_enable1"))
{
// TV Delay in s
var tvDelaySeconds = Math.Max(_CsServer.GetConvar<int>("tv_delay"), _CsServer.GetConvar<int>("tv_delay1"));
_Logger.LogInformation("Waiting for sourceTV. Delay: {delay}s + 15s", tvDelaySeconds);
delay += tvDelaySeconds;
}

var seriesResultParams = new SeriesResultParams(MatchInfo.Config.MatchId, MatchInfo.MatchMaps.GroupBy(x => x.Winner).MaxBy(x => x.Count())!.Key!.TeamConfig.Name, Forfeit: true, (uint)delay * 1100, MatchInfo.MatchMaps.Count(x => x.Team1Points > x.Team2Points), MatchInfo.MatchMaps.Count(x => x.Team2Points > x.Team1Points));
var finalize = _ApiProvider.FinalizeAsync(seriesResultParams, CancellationToken.None);
var seriesResultParams = new SeriesResultParams(MatchInfo.Config.MatchId, MatchInfo.MatchMaps.GroupBy(x => x.Winner).MaxBy(x => x.Count())!.Key!.TeamConfig.Name, Forfeit: true, (uint)delay * 1100, MatchInfo.MatchMaps.Count(x => x.Team1Points > x.Team2Points), MatchInfo.MatchMaps.Count(x => x.Team2Points > x.Team1Points));
var finalize = _ApiProvider.FinalizeAsync(seriesResultParams, CancellationToken.None);

while (delay > 0)
{
_Logger.LogInformation("Waiting for sourceTV. Remaining Delay: {delay}s", delay);
var delayLoopTime = Math.Min(_TimeBetweenDelayMessages, delay);
await Task.Delay(TimeSpan.FromSeconds(delayLoopTime)).ConfigureAwait(false);
delay -= delayLoopTime;
}
while (delay > 0)
{
_Logger.LogInformation("Waiting for sourceTV. Remaining Delay: {delay}s", delay);
var delayLoopTime = Math.Min(_TimeBetweenDelayMessages, delay);
await Task.Delay(TimeSpan.FromSeconds(delayLoopTime)).ConfigureAwait(false);
delay -= delayLoopTime;
}

await finalize.ConfigureAwait(false);
await finalize.ConfigureAwait(false);

if (_DemoUploader != null)
{
await _DemoUploader.UploadDemoAsync(MatchInfo.DemoFile, CancellationToken.None).ConfigureAwait(false);
}
if (_DemoUploader != null)
{
await _DemoUploader.UploadDemoAsync(MatchInfo.DemoFile, CancellationToken.None).ConfigureAwait(false);
}

DoForAll(AllMatchPlayers.ToList(), p => p.Player.Kick());
DoForAll(AllMatchPlayers.ToList(), p => p.Player.Kick());

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

MatchFinalized?.Invoke(this, new MatchFinalizedEventArgs());
}
catch (Exception ex)
{
_Logger.LogError(ex, "Unexpected error during finalize.");
}
finally
{
MatchFinalized?.Invoke(this, new MatchFinalizedEventArgs());
}
}

private void MatchLive()
Expand Down