Skip to content

Commit

Permalink
Merge pull request #58 from Lan2Play/feature/FixBo2+
Browse files Browse the repository at this point in the history
Feature/fix bo2+
  • Loading branch information
TheR00st3r authored Nov 18, 2023
2 parents 78f54a9 + e98951e commit e280e5f
Showing 1 changed file with 20 additions and 5 deletions.
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

0 comments on commit e280e5f

Please sign in to comment.