From 044cc11e8bb6bcdac6052c652508c827be1aaff1 Mon Sep 17 00:00:00 2001 From: TheR00st3r Date: Sat, 18 Nov 2023 15:07:21 +0100 Subject: [PATCH 1/4] Reset Maps if entering VoteMap State Fix #52 --- PugSharp.Match/Match.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/PugSharp.Match/Match.cs b/PugSharp.Match/Match.cs index c2cfef71..d9cdb15f 100644 --- a/PugSharp.Match/Match.cs +++ b/PugSharp.Match/Match.cs @@ -76,9 +76,6 @@ private void Initialize(MatchInfo matchInfo) _DemoUploader = _ServiceProvider.GetRequiredService(); _DemoUploader.Initialize(MatchInfo.Config.EventulaDemoUploadUrl, MatchInfo.Config.EventulaApistatsToken); } - - _MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList(); - } public void Initialize(Config.MatchConfig matchConfig) @@ -126,6 +123,7 @@ private void InitializeStateMachine() _MatchStateMachine.Configure(MatchState.MapVote) .PermitReentryIf(MatchCommand.VoteMap, MapIsNotSelected) .PermitIf(MatchCommand.VoteMap, MatchState.TeamVote, MapIsSelected) + .OnEntry(InitializeMapsToVote) .OnEntry(SendRemainingMapsToVotingTeam) .OnExit(RemoveBannedMap); @@ -173,10 +171,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); @@ -622,6 +622,14 @@ public string CreateDotGraph() return UmlDotGraph.Format(_MatchStateMachine.GetInfo()); } + private void InitializeMapsToVote(StateMachine.Transition transition) + { + if(transition.Source == MatchState.WaitingForPlayersConnectedReady) + { + _MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList(); + } + } + private void SendRemainingMapsToVotingTeam() { // If only one map is configured @@ -759,6 +767,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()); @@ -770,8 +780,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; } From f8ed68de416218b99d66c06fb528f1b0e14f2d60 Mon Sep 17 00:00:00 2001 From: TheR00st3r Date: Sat, 18 Nov 2023 15:25:35 +0100 Subject: [PATCH 2/4] Check If No Config are availbale --- PugSharp.Match/Match.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/PugSharp.Match/Match.cs b/PugSharp.Match/Match.cs index c893e4ef..42b6ab1e 100644 --- a/PugSharp.Match/Match.cs +++ b/PugSharp.Match/Match.cs @@ -64,6 +64,11 @@ private void Initialize(MatchInfo matchInfo) throw new NotSupportedException("Initialize can onyl be called once!"); } + if (matchInfo.Config.Maplist.Length <= 0) + { + throw new NotSupportedException("Can not create Match without any maps!"); + } + SetServerCulture(matchInfo.Config.ServerLocale); MatchInfo = matchInfo; _VoteTimer.Interval = MatchInfo.Config.VoteTimeout; From d9b41a688941624b2c14e8ec29eaf8bb8eeeaa14 Mon Sep 17 00:00:00 2001 From: TheR00st3r Date: Sat, 18 Nov 2023 15:31:46 +0100 Subject: [PATCH 3/4] Exclude already played maps from vote --- PugSharp.Match/Match.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PugSharp.Match/Match.cs b/PugSharp.Match/Match.cs index 42b6ab1e..93ca8fe5 100644 --- a/PugSharp.Match/Match.cs +++ b/PugSharp.Match/Match.cs @@ -640,9 +640,10 @@ public string CreateDotGraph() private void InitializeMapsToVote(StateMachine.Transition transition) { - if(transition.Source == MatchState.WaitingForPlayersConnectedReady) + if (transition.Source == MatchState.WaitingForPlayersConnectedReady) { - _MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList(); + 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(); } } From e98951ea1474d82682776a042573734a63ce6ed2 Mon Sep 17 00:00:00 2001 From: TheR00st3r Date: Sat, 18 Nov 2023 15:33:28 +0100 Subject: [PATCH 4/4] Check for Config.NumMaps --- PugSharp.Match/Match.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PugSharp.Match/Match.cs b/PugSharp.Match/Match.cs index 93ca8fe5..4e8e3665 100644 --- a/PugSharp.Match/Match.cs +++ b/PugSharp.Match/Match.cs @@ -64,9 +64,9 @@ private void Initialize(MatchInfo matchInfo) throw new NotSupportedException("Initialize can onyl be called once!"); } - if (matchInfo.Config.Maplist.Length <= 0) + if (matchInfo.Config.Maplist.Length < matchInfo.Config.NumMaps) { - throw new NotSupportedException("Can not create Match without any maps!"); + throw new NotSupportedException($"Can not create Match without the required number of maps! At lease {matchInfo.Config.NumMaps} are required!"); } SetServerCulture(matchInfo.Config.ServerLocale);