Skip to content

Commit

Permalink
Add end of map vote functionality (#21)
Browse files Browse the repository at this point in the history
* move files

* WIP

* More wip

* wip

* Finish
  • Loading branch information
abnerfs authored Feb 16, 2024
1 parent db4d6a5 commit e7ec4dc
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 87 deletions.
27 changes: 21 additions & 6 deletions CrossCutting/Config.cs → Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,33 @@ public interface IVoteConfig
}


public class EndOfMapConfig
public interface IEndOfMapConfig
{
public int MapsToShow { get; set; } = 5;
public bool ChangeMapImmediatly { get; set; } = true;
public int VoteDuration { get; set; } = 30;
public int MapsToShow { get; set; }
public bool ChangeMapImmediatly { get; set; }
public int VoteDuration { get; set; }
public int VotePercentage { get; set; }
}

public class RtvConfig : EndOfMapConfig, ICommandConfig, IVoteConfig
public class EndOfMapConfig : IVoteConfig, IEndOfMapConfig
{
public bool Enabled { get; set; } = true;
public int MapsToShow { get; set; } = 6;
public bool ChangeMapImmediatly { get; set; } = false;
public int VoteDuration { get; set; } = 30;
public int VotePercentage { get; set; } = 60;
}

public class RtvConfig : ICommandConfig, IVoteConfig, IEndOfMapConfig
{
public bool Enabled { get; set; } = true;
public bool EnabledInWarmup { get; set; } = true;
public int MinPlayers { get; set; } = 0;
public int MinRounds { get; set; } = 0;
public bool ChangeMapImmediatly { get; set; } = true;
public int MapsToShow { get; set; } = 6;
public int VoteDuration { get; set; } = 30;
public int VotePercentage { get; set; } = 60;
}

public class VotemapConfig : ICommandConfig, IVoteConfig
Expand All @@ -42,10 +55,12 @@ public class VotemapConfig : ICommandConfig, IVoteConfig
public int MinRounds { get; set; } = 0;
}


public class Config : IBasePluginConfig
{
public int Version { get; set; } = 5;
public int Version { get; set; } = 6;
public RtvConfig Rtv { get; set; } = new();
public VotemapConfig Votemap { get; set; } = new();
public EndOfMapConfig EndOfMapVote { get; set; } = new();
}
}
20 changes: 17 additions & 3 deletions Core/ChangeMapManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core;
using System.Numerics;

namespace cs2_rockthevote
{
Expand All @@ -27,9 +28,10 @@ public class ChangeMapManager : IPluginDependency<Plugin, Config>
private StringLocalizer _localizer;
private PluginState _pluginState;

private string? _nextMap { get; set; } = null;
private string? _nextMap = null;
private string _prefix = DEFAULT_PREFIX;
private const string DEFAULT_PREFIX = "rtv.prefix";
private bool _mapEnd = false;


public ChangeMapManager(StringLocalizer localizer, PluginState pluginState)
Expand All @@ -39,11 +41,12 @@ public ChangeMapManager(StringLocalizer localizer, PluginState pluginState)
}


public void ScheduleMapChange(string map, string prefix = DEFAULT_PREFIX)
public void ScheduleMapChange(string map, bool mapEnd = false, string prefix = DEFAULT_PREFIX)
{
_nextMap = map;
_prefix = prefix;
_pluginState.MapChangeScheduled = true;
_mapEnd = mapEnd;
}

public void OnMapStart()
Expand All @@ -52,8 +55,11 @@ public void OnMapStart()
_prefix = DEFAULT_PREFIX;
}

public bool ChangeNextMap()
public bool ChangeNextMap(bool mapEnd = false)
{
if (mapEnd != _mapEnd)
return false;

if (!_pluginState.MapChangeScheduled)
return false;

Expand All @@ -74,6 +80,14 @@ public bool ChangeNextMap()
public void OnLoad(Plugin plugin)
{
_plugin = plugin;
plugin.RegisterEventHandler<EventCsWinPanelMatch>((ev, info) =>
{
if (_pluginState.MapChangeScheduled)
{
ChangeNextMap(true);
}
return HookResult.Continue;
});
}
}
}
18 changes: 12 additions & 6 deletions Commands/EndMapVoteManager.cs → Core/EndMapVoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CounterStrikeSharp.API.Modules.Timers;
using System.Data;
using System.Text;
using static CounterStrikeSharp.API.Core.Listeners;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;

namespace cs2_rockthevote
Expand Down Expand Up @@ -47,13 +48,14 @@ public EndMapVoteManager(MapLister mapLister, ChangeMapManager changeMapManager,

List<string> mapsEllected = new();

private EndOfMapConfig? _config = null;
private IEndOfMapConfig? _config = null;
private int _canVote = 0;
private Plugin? _plugin;

public void OnLoad(Plugin plugin)
{
_plugin = plugin;
plugin.RegisterListener<OnTick>(VoteDisplayTick);
}

public void OnMapStart(string map)
Expand Down Expand Up @@ -115,14 +117,15 @@ public void VoteDisplayTick()

void EndVote()
{
bool mapEnd = _config is EndOfMapConfig;
KillTimer();
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;
decimal percent = totalVotes > 0 ? winner.Value / totalVotes * 100M : 0;
if (maxVotes > 0)
{
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("emv.vote-ended", winner.Key, percent, totalVotes));
Expand All @@ -133,11 +136,14 @@ void EndVote()
}

PrintCenterTextAll(_localizer.Localize("emv.hud.finished", winner.Key));
_changeMapManager.ScheduleMapChange(winner.Key);
_changeMapManager.ScheduleMapChange(winner.Key, mapEnd: mapEnd);
if (_config!.ChangeMapImmediatly)
_changeMapManager.ChangeNextMap();
_changeMapManager.ChangeNextMap(mapEnd);
else
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("general.changing-map-next-round", winner.Key));
{
if (!mapEnd)
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("general.changing-map-next-round", winner.Key));
}
}

IList<T> Shuffle<T>(Random rng, IList<T> array)
Expand All @@ -153,7 +159,7 @@ IList<T> Shuffle<T>(Random rng, IList<T> array)
return array;
}

public void StartVote(EndOfMapConfig config)
public void StartVote(IEndOfMapConfig config)
{
Votes.Clear();
_pluginState.EofVoteHappening = true;
Expand Down
115 changes: 115 additions & 0 deletions Core/MaxRoundsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;

namespace cs2_rockthevote
{
public class MaxRoundsManager : IPluginDependency<Plugin, Config>
{
private int CTWins = 0;
private int TWins = 0;

private GameRules _gameRules;
private ConVar? _maxRounds;
private int MaxRoundsValue => _maxRounds!.GetPrimitiveValue<int>();
public bool UnlimitedRounds => MaxRoundsValue <= 0;
public int RemainingRounds
{
get
{
var played = MaxRoundsValue - _gameRules.TotalRoundsPlayed;
if (played < 0)
return 0;

return played;
}
}

public int RemainingWins
{
get
{
return MaxWins - CurrentHighestWins;
}
}

public int MaxWins
{
get
{
if (MaxRoundsValue <= 0)
return 0;

return ((int)Math.Floor(MaxRoundsValue / 2M)) + 1;
}
}

public int CurrentHighestWins => CTWins > TWins ? CTWins : TWins;

public MaxRoundsManager(GameRules gameRules)
{
_gameRules = gameRules;
}

void LoadCvar()
{
_maxRounds = ConVar.Find("mp_maxrounds");
}


public void ClearRounds()
{
CTWins = 0;
TWins = 0;
}

public void RoundWin(CsTeam team)
{
if (team == CsTeam.CounterTerrorist)
{
CTWins++;

}
else if (team == CsTeam.Terrorist)
{
TWins++;
}
//Server.PrintToChatAll($"T Wins {TWins}, CTWins {CTWins}");
}

public void OnMapStart(string map)
{
LoadCvar();
ClearRounds();
}

public void OnLoad(Plugin plugin)
{
plugin.RegisterEventHandler<EventRoundEnd>((@event, info) =>
{
if (@event is null)
return HookResult.Continue;

CsTeam? winner = Enum.IsDefined(typeof(CsTeam), (byte)@event.Winner) ? (CsTeam)@event.Winner : null;
if (winner is not null)
RoundWin(winner.Value);

return HookResult.Continue;
});


plugin.RegisterEventHandler<EventRoundAnnounceMatchStart>((@event, info) =>
{
if (@event is null)
return HookResult.Continue;

ClearRounds();
return HookResult.Continue;
});

LoadCvar();
ClearRounds();
}
}
}
58 changes: 58 additions & 0 deletions Core/TimeLimitManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Cvars;

namespace cs2_rockthevote.Core
{
public class TimeLimitManager : IPluginDependency<Plugin, Config>
{
private GameRules _gameRules;

private ConVar? _timeLimit;

private decimal TimeLimitValue => (decimal)_timeLimit!.GetPrimitiveValue<float>() * 60M;

public bool UnlimitedTime => TimeLimitValue <= 0;

public decimal TimePlayed
{
get
{
if (_gameRules.WarmupRunning)
return 0;

return (decimal)(Server.CurrentTime - _gameRules.GameStartTime);
}
}

public decimal TimeRemaining
{
get
{
if (UnlimitedTime || TimePlayed > TimeLimitValue)
return 0;

return TimeLimitValue - TimePlayed;
}
}

public TimeLimitManager(GameRules gameRules)
{
_gameRules = gameRules;
}

void LoadCvar()
{
_timeLimit = ConVar.Find("mp_timelimit");
}

public void OnMapStart(string map)
{
LoadCvar();
}

public void OnLoad(Plugin plugin)
{
LoadCvar();
}
}
}
Loading

0 comments on commit e7ec4dc

Please sign in to comment.