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

Feature/fix bo2+ #58

Merged
merged 6 commits 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
25 changes: 20 additions & 5 deletions PugSharp.Match/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ private void Initialize(MatchInfo matchInfo)
throw new NotSupportedException("Initialize can onyl be called once!");
}

if (matchInfo.Config.Maplist.Length < matchInfo.Config.NumMaps)
{
throw new NotSupportedException($"Can not create Match without the required number of maps! At lease {matchInfo.Config.NumMaps} are required!");
}

SetServerCulture(matchInfo.Config.ServerLocale);
MatchInfo = matchInfo;
_VoteTimer.Interval = MatchInfo.Config.VoteTimeout;
Expand All @@ -78,8 +83,6 @@ private void Initialize(MatchInfo matchInfo)
_DemoUploader = _ServiceProvider.GetRequiredService<DemoUploader>();
_DemoUploader.Initialize(MatchInfo.Config.EventulaDemoUploadUrl, MatchInfo.Config.EventulaApistatsToken);
}

_MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList();
}

public void Initialize(Config.MatchConfig matchConfig)
Expand Down Expand Up @@ -127,6 +130,7 @@ private void InitializeStateMachine()
_MatchStateMachine.Configure(MatchState.MapVote)
.PermitReentryIf(MatchCommand.VoteMap, MapIsNotSelected)
.PermitIf(MatchCommand.VoteMap, MatchState.TeamVote, MapIsSelected)
.OnEntry(InitializeMapsToVote)
.OnEntry(SendRemainingMapsToVotingTeam)
.OnExit(RemoveBannedMap);

Expand Down Expand Up @@ -174,10 +178,12 @@ private void InitializeStateMachine()
.OnExit(UnpauseMatch);

_MatchStateMachine.Configure(MatchState.MapCompleted)
.PermitDynamic(MatchCommand.CompleteMatch, () => AllMapsArePlayed() ? MatchState.MatchCompleted : MatchState.WaitingForPlayersConnectedReady)
.PermitIf(MatchCommand.CompleteMatch, MatchState.MatchCompleted, AllMapsArePlayed)
.PermitIf(MatchCommand.CompleteMatch, MatchState.WaitingForPlayersConnectedReady, NotAllMapsArePlayed)
.OnEntry(SendMapResults)
.OnEntry(TryCompleteMatch);


_MatchStateMachine.Configure(MatchState.MatchCompleted)
.OnEntryAsync(CompleteMatchAsync);

Expand Down Expand Up @@ -632,6 +638,15 @@ public string CreateDotGraph()
return UmlDotGraph.Format(_MatchStateMachine.GetInfo());
}

private void InitializeMapsToVote(StateMachine<MatchState, MatchCommand>.Transition transition)
{
if (transition.Source == MatchState.WaitingForPlayersConnectedReady)
{
var playedMaps = MatchInfo.MatchMaps.Select(x => x.MapName).Where(x => !string.IsNullOrEmpty(x));
_MapsToSelect = MatchInfo.Config.Maplist.Except(playedMaps!, StringComparer.Ordinal).Select(x => new Vote(x)).ToList();
}
}

private void SendRemainingMapsToVotingTeam()
{
if (_MapsToSelect == null)
Expand Down Expand Up @@ -780,6 +795,8 @@ private bool AllPlayersAreReady()

private bool AllTeamsUnpaused() => !MatchInfo.MatchTeam1.IsPaused && !MatchInfo.MatchTeam2.IsPaused;

private bool NotAllMapsArePlayed() => !AllMapsArePlayed();

private bool AllMapsArePlayed()
{
var teamWithMostWins = MatchInfo.MatchMaps.Where(x => x.Winner != null).GroupBy(x => x.Winner).MaxBy(x => x.Count());
Expand All @@ -791,8 +808,6 @@ private bool AllMapsArePlayed()

var wins = teamWithMostWins.Count();
var requiredWins = Math.Ceiling(MatchInfo.Config.NumMaps / 2d);
_Logger.LogInformation("{team} has most wins: {wins} of {requiredWins}", teamWithMostWins.Key.TeamConfig.Name, wins, requiredWins);

return wins >= requiredWins;
}

Expand Down