Skip to content

Commit

Permalink
Merge pull request #33 from Lan2Play/feature/fix_issue_32
Browse files Browse the repository at this point in the history
Skip map vote if only one map is configured
  • Loading branch information
MD-V authored Nov 15, 2023
2 parents cf0e46e + 2643bc2 commit 3c9381e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
44 changes: 39 additions & 5 deletions PugSharp.Match.Tests/MatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PugSharp.Config;
using PugSharp.Match.Contract;
using PugSharp.Translation;
using System.Collections;

namespace PugSharp.Match.Tests
{
Expand Down Expand Up @@ -76,12 +77,38 @@ public async Task MatchTest()
IPlayer player2 = CreatePlayerSub(1, 1);

ConnectPlayers(matchPlayers, match, player1, player2);
await SetPlayersReady(match, player1, player2);
await SetPlayersReady(match, player1, player2, MatchState.MapVote);
IPlayer votePlayer = VoteForMap(config, match, player1, player2);
await VoteTeam(matchCallback, config, match, player1, player2, votePlayer);
PauseUnpauseMatch(matchCallback, match, player1);
}

[Fact]
public async Task MatchTestWithOneMap()
{
var matchPlayers = new List<IPlayer>();
var apiProvider = Substitute.For<IApiProvider>();
var textHelper = Substitute.For<ITextHelper>();
var matchCallback = Substitute.For<IMatchCallback>();

matchCallback.LoadAllPlayers().Returns(matchPlayers);

MatchConfig config = CreateExampleConfig(new List<string> { "de_dust2" });

var match = new Match(matchCallback, apiProvider, textHelper, config);

Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);

IPlayer player1 = CreatePlayerSub(0, 0);
IPlayer player2 = CreatePlayerSub(1, 1);

ConnectPlayers(matchPlayers, match, player1, player2);
await SetPlayersReady(match, player1, player2, MatchState.TeamVote);
await VoteTeam(matchCallback, config, match, player1, player2, player1);
PauseUnpauseMatch(matchCallback, match, player1);
}


private static void PauseUnpauseMatch(IMatchCallback matchCallback, Match match, IPlayer player1)
{
match.SetPlayerDisconnected(player1);
Expand Down Expand Up @@ -133,12 +160,12 @@ private static IPlayer VoteForMap(MatchConfig config, Match match, IPlayer playe
return votePlayer;
}

private static async Task SetPlayersReady(Match match, IPlayer player1, IPlayer player2)
private static async Task SetPlayersReady(Match match, IPlayer player1, IPlayer player2, MatchState expectedMatchStateAfterReady)
{
await match.TogglePlayerIsReadyAsync(player1).ConfigureAwait(false);
Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);
await match.TogglePlayerIsReadyAsync(player2).ConfigureAwait(false);
Assert.Equal(MatchState.MapVote, match.CurrentState);
Assert.Equal(expectedMatchStateAfterReady, match.CurrentState);
}

private static void ConnectPlayers(List<IPlayer> matchPlayers, Match match, IPlayer player1, IPlayer player2)
Expand All @@ -152,8 +179,15 @@ private static void ConnectPlayers(List<IPlayer> matchPlayers, Match match, IPla
Assert.Equal(MatchState.WaitingForPlayersConnectedReady, match.CurrentState);
}

private static MatchConfig CreateExampleConfig()
private static MatchConfig CreateExampleConfig(IEnumerable<string>? mapList = null)
{
IEnumerable<string> mapListInternal = new List<string> { "de_dust2", "de_overpass", "de_inferno" };

if (mapList != null)
{
mapListInternal = mapList;
}

return new MatchConfig
{
MatchId = "1337",
Expand All @@ -176,7 +210,7 @@ private static MatchConfig CreateExampleConfig()
{ 1,"Def" },
},
},
Maplist = new string[] { "de_dust2", "de_overpass", "de_inferno" },
Maplist = mapListInternal.ToArray(),
};
}

Expand Down
37 changes: 31 additions & 6 deletions PugSharp.Match/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,14 @@ public string CreateDotGraph()

private void SendRemainingMapsToVotingTeam()
{
// If only one map is configured
if (MatchInfo.Config.Maplist.Length == 1)
{
_MapsToSelect = MatchInfo.Config.Maplist.Select(x => new Vote(x)).ToList();
TryFireState(MatchCommand.VoteMap);
return;
}

SwitchVotingTeam();

_MapsToSelect.ForEach(m => m.Votes.Clear());
Expand All @@ -585,13 +593,20 @@ private void SendRemainingMapsToVotingTeam()

private void RemoveBannedMap()
{
_VoteTimer.Stop();
if (_VoteTimer.Enabled)
{
_VoteTimer.Stop();
}

var mapToBan = _MapsToSelect.MaxBy(m => m.Votes.Count);
_MapsToSelect.Remove(mapToBan!);
_MapsToSelect.ForEach(x => x.Votes.Clear());
//Only ban map if theres more than one
if (_MapsToSelect.Count > 1)
{
var mapToBan = _MapsToSelect.MaxBy(m => m.Votes.Count);
_MapsToSelect.Remove(mapToBan!);
_MapsToSelect.ForEach(x => x.Votes.Clear());

_MatchCallback.SendMessage(_TextHelper.GetText(nameof(Resources.PugSharp_Match_BannedMap), mapToBan!.Name, _CurrentMatchTeamToVote?.TeamConfig.Name));
_MatchCallback.SendMessage(_TextHelper.GetText(nameof(Resources.PugSharp_Match_BannedMap), mapToBan!.Name, _CurrentMatchTeamToVote?.TeamConfig.Name));
}

if (_MapsToSelect.Count == 1)
{
Expand Down Expand Up @@ -720,7 +735,17 @@ private void SetAllPlayersNotReady()
private bool MapIsSelected()
{
// The SelectedCount is checked when the Votes are done but the map is still in the list
return _MapsToSelect.Count == 2;
return _MapsToSelect.Count <= 2;
}

private bool OneMapConfigured()

Check warning on line 741 in PugSharp.Match/Match.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Remove the unused private method 'OneMapConfigured'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
{
if (MatchInfo.Config.Maplist.Count() == 1)
{
return true;
}

return false;
}

private bool MapIsNotSelected()
Expand Down
4 changes: 2 additions & 2 deletions PugSharp.Translation/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c9381e

Please sign in to comment.