Skip to content

Commit

Permalink
Fix map winner selection
Browse files Browse the repository at this point in the history
  • Loading branch information
abnerfs committed Feb 12, 2024
1 parent 8b44409 commit 747e56c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
42 changes: 28 additions & 14 deletions Commands/EndMapVoteManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Plugin;
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Timers;
using System.Data;
Expand All @@ -9,6 +8,21 @@

namespace cs2_rockthevote
{
//public partial class Plugin
//{

// [ConsoleCommand("votebot", "Votes to rock the vote")]
// public void VoteBot(CCSPlayerController? player, CommandInfo? command)
// {
// var bot = ServerManager.ValidPlayers().FirstOrDefault(x => x.IsBot);
// if(bot is not null)
// {
// _endmapVoteManager.MapVoted(bot, "de_dust2");
// }
// }

//}

public class EndMapVoteManager : IPluginDependency<Plugin, Config>
{
public EndMapVoteManager(MapLister mapLister, ChangeMapManager changeMapManager, NominationCommand nominationManager, StringLocalizer localizer, PluginState pluginState)
Expand Down Expand Up @@ -96,17 +110,20 @@ void VoteDisplayTick(int time)
void EndVote()
{
KillTimer();
var winner = Votes.OrderByDescending(x => x.Value).First();
var totalVotes = Votes.Select(x => x.Value).Sum();
var percent = totalVotes > 0 ? (winner.Value / totalVotes) * 100 : 0;
if (percent > 0)
decimal maxVotes = Votes.Select(x => x.Value).Max();
IEnumerable<KeyValuePair<string, int>> potentialWinners = Votes.Where(x => x.Value == maxVotes);
Random rnd = new();
KeyValuePair<string, int> winner = potentialWinners.ElementAt(rnd.Next(0, potentialWinners.Count()));

decimal totalVotes = Votes.Select(x => x.Value).Sum();
decimal percent = totalVotes > 0 ? (winner.Value / totalVotes) * 100M : 0;
//Server.PrintToChatAll($"Total {totalVotes}, winner {winner.Value}, Percent {percent}");
if (maxVotes > 0)
{
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("emv.vote-ended", winner.Key, percent, totalVotes));
}
else
{
var rnd = new Random();
winner = Votes.ElementAt(rnd.Next(0, Votes.Count));
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("emv.vote-ended-no-votes", winner.Key));
}

Expand All @@ -133,6 +150,7 @@ IList<T> Shuffle<T>(Random rng, IList<T> array)

public void StartVote(EndOfMapConfig config)
{
Votes.Clear();
_pluginState.EofVoteHappening = true;
_config = config;
var mapsToShow = _config!.MapsToShow == 0 ? 5 : _config!.MapsToShow;
Expand All @@ -147,13 +165,9 @@ public void StartVote(EndOfMapConfig config)
menu.AddMenuOption(map, (player, option) => MapVoted(player, map));
}

foreach (var player in Utilities.GetPlayers())
{
if (player.IsValid)
{
ChatMenus.OpenMenu(player, menu);
}
}
foreach (var player in ServerManager.ValidPlayers())
MenuManager.OpenChatMenu(player, menu);

timeLeft = _config.VoteDuration;
Timer = _plugin!.AddTimer(1.0F, () =>
{
Expand Down
7 changes: 5 additions & 2 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
public partial class Plugin : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "RockTheVote";
public override string ModuleVersion => "1.1.3";
public override string ModuleVersion => "1.1.4";
public override string ModuleAuthor => "abnerfs";
public override string ModuleDescription => "General purpose map voting plugin";

Expand All @@ -32,14 +32,16 @@ public partial class Plugin : BasePlugin, IPluginConfig<Config>
private readonly RockTheVoteCommand _rtvManager;
private readonly GameRules _gameRules;
private readonly TimeLeftCommand _timeLeft;
private readonly EndMapVoteManager _endmapVoteManager;

public Plugin(DependencyManager<Plugin, Config> dependencyManager,
NominationCommand nominationManager,
ChangeMapManager changeMapManager,
VotemapCommand voteMapManager,
RockTheVoteCommand rtvManager,
GameRules gameRules,
TimeLeftCommand timeLeft)
TimeLeftCommand timeLeft,
EndMapVoteManager endmapVoteManager)
{
_dependencyManager = dependencyManager;
_nominationManager = nominationManager;
Expand All @@ -48,6 +50,7 @@ public Plugin(DependencyManager<Plugin, Config> dependencyManager,
_rtvManager = rtvManager;
_gameRules = gameRules;
_timeLeft = timeLeft;
_endmapVoteManager = endmapVoteManager;
}

public Config? Config { get; set; }

Check warning on line 56 in Plugin.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Nullability of reference types in return type of 'Config? Plugin.Config.get' doesn't match implicitly implemented member 'Config IPluginConfig<Config>.Config.get' (possibly because of nullability attributes).

Check warning on line 56 in Plugin.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Nullability of reference types in return type of 'Config? Plugin.Config.get' doesn't match implicitly implemented member 'Config IPluginConfig<Config>.Config.get' (possibly because of nullability attributes).
Expand Down

0 comments on commit 747e56c

Please sign in to comment.