Skip to content

Commit

Permalink
Add map cooldown and hud menu for votemap
Browse files Browse the repository at this point in the history
  • Loading branch information
abnerfs committed Mar 17, 2024
1 parent da9d230 commit 5e83cd2
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 23 deletions.
8 changes: 8 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterStrikeSharp.API.Core;
using System.Text.Json.Serialization;

namespace cs2_rockthevote
{
Expand Down Expand Up @@ -60,6 +61,7 @@ public class VotemapConfig : ICommandConfig, IVoteConfig
public bool EnabledInWarmup { get; set; } = true;
public int MinPlayers { get; set; } = 0;
public int MinRounds { get; set; } = 0;
public bool HudMenu { get; set; } = false;
}

public class TimeleftConfig
Expand All @@ -73,6 +75,11 @@ public class NextmapConfig
}


public class MapCoolDownConfig
{

}

public class Config : IBasePluginConfig
{
public int Version { get; set; } = 9;
Expand All @@ -81,5 +88,6 @@ public class Config : IBasePluginConfig
public EndOfMapConfig EndOfMapVote { get; set; } = new();
public TimeleftConfig Timeleft { get; set; } = new();
public NextmapConfig Nextmap { get; set; } = new();
public ushort MapsInCoolDown { get; set; } = 3;
}
}
7 changes: 5 additions & 2 deletions Core/EndMapVoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Timers;
using cs2_rockthevote.Core;
using System.Data;
using System.Text;
using static CounterStrikeSharp.API.Core.Listeners;
Expand All @@ -26,20 +27,22 @@ namespace cs2_rockthevote
public class EndMapVoteManager : IPluginDependency<Plugin, Config>
{
const int MAX_OPTIONS_HUD_MENU = 6;
public EndMapVoteManager(MapLister mapLister, ChangeMapManager changeMapManager, NominationCommand nominationManager, StringLocalizer localizer, PluginState pluginState)
public EndMapVoteManager(MapLister mapLister, ChangeMapManager changeMapManager, NominationCommand nominationManager, StringLocalizer localizer, PluginState pluginState, MapCooldown mapCooldown)
{
_mapLister = mapLister;
_changeMapManager = changeMapManager;
_nominationManager = nominationManager;
_localizer = localizer;
_pluginState = pluginState;
_mapCooldown = mapCooldown;
}

private readonly MapLister _mapLister;
private readonly ChangeMapManager _changeMapManager;
private readonly NominationCommand _nominationManager;
private readonly StringLocalizer _localizer;
private PluginState _pluginState;
private MapCooldown _mapCooldown;
private Timer? Timer;

Dictionary<string, int> Votes = new();
Expand Down Expand Up @@ -182,7 +185,7 @@ public void StartVote(IEndOfMapConfig config)
if (config.HudMenu && mapsToShow > MAX_OPTIONS_HUD_MENU)
mapsToShow = MAX_OPTIONS_HUD_MENU;

var mapsScrambled = Shuffle(new Random(), _mapLister.Maps!.Select(x => x.Name).Where(x => x != Server.MapName).ToList());
var mapsScrambled = Shuffle(new Random(), _mapLister.Maps!.Select(x => x.Name).Where(x => x != Server.MapName && !_mapCooldown.IsMapInCooldown(x)).ToList());
mapsEllected = _nominationManager.NominationWinners().Concat(mapsScrambled).Distinct().ToList();

_canVote = ServerManager.ValidPlayerCount();
Expand Down
42 changes: 42 additions & 0 deletions Core/MapCooldown.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CounterStrikeSharp.API;

namespace cs2_rockthevote.Core
{
public class MapCooldown : IPluginDependency<Plugin, Config>
{
List<string> mapsOnCoolDown = new();
private ushort InCoolDown = 0;

public event EventHandler<Map[]>? EventCooldownRefreshed;

public MapCooldown(MapLister mapLister)
{
//this is called on map start
mapLister.EventMapsLoaded += (e, maps) =>
{
var map = Server.MapName;
if (InCoolDown == 0)
{
mapsOnCoolDown.Clear();
return;
}

mapsOnCoolDown.Add(map.Trim().ToLower());
if (mapsOnCoolDown.Count > InCoolDown)
mapsOnCoolDown.RemoveAt(0);

EventCooldownRefreshed?.Invoke(this, maps);
};
}

public void OnConfigParsed(Config config)
{
InCoolDown = config.MapsInCoolDown;
}

public bool IsMapInCooldown(string map)
{
return mapsOnCoolDown.IndexOf(map) > -1;
}
}
}
16 changes: 12 additions & 4 deletions Features/NominationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Menu;
using cs2_rockthevote.Core;

namespace cs2_rockthevote
{
Expand Down Expand Up @@ -33,15 +34,17 @@ public class NominationCommand : IPluginDependency<Plugin, Config>
private GameRules _gamerules;
private StringLocalizer _localizer;
private PluginState _pluginState;
private MapCooldown _mapCooldown;
private MapLister _mapLister;

public NominationCommand(MapLister mapLister, GameRules gamerules, StringLocalizer localizer, PluginState pluginState)
public NominationCommand(MapLister mapLister, GameRules gamerules, StringLocalizer localizer, PluginState pluginState, MapCooldown mapCooldown)
{
_mapLister = mapLister;
_mapLister.EventMapsLoaded += OnMapsLoaded;
_gamerules = gamerules;
_localizer = localizer;
_pluginState = pluginState;
_mapCooldown = mapCooldown;
_mapCooldown.EventCooldownRefreshed += OnMapsLoaded;
}


Expand All @@ -55,7 +58,6 @@ public void OnConfigParsed(Config config)
_config = config.Rtv;
}


public void OnMapsLoaded(object? sender, Map[] maps)
{
nominationMenu = new("Nomination");
Expand All @@ -64,7 +66,7 @@ public void OnMapsLoaded(object? sender, Map[] maps)
nominationMenu.AddMenuOption(map.Name, (CCSPlayerController player, ChatMenuOption option) =>
{
Nominate(player, option.Text);
});
}, _mapCooldown.IsMapInCooldown(map.Name));
}
}

Expand Down Expand Up @@ -123,6 +125,12 @@ void Nominate(CCSPlayerController player, string map)
return;
}

if (_mapCooldown.IsMapInCooldown(map))
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.map-played-recently"));
return;
}

if (_mapLister.Maps!.Select(x => x.Name).FirstOrDefault(x => x.ToLower() == map) is null)
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.invalid-map"));
Expand Down
34 changes: 30 additions & 4 deletions Features/VotemapCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Logging;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Menu;
using cs2_rockthevote.Core;
using Microsoft.Extensions.Localization;

namespace cs2_rockthevote
Expand All @@ -29,21 +31,25 @@ public class VotemapCommand : IPluginDependency<Plugin, Config>
{
Dictionary<string, AsyncVoteManager> VotedMaps = new();
ChatMenu? votemapMenu = null;
CenterHtmlMenu? votemapMenuHud = null;
private VotemapConfig _config = new();
private GameRules _gamerules;
private StringLocalizer _localizer;
private ChangeMapManager _changeMapManager;
private PluginState _pluginState;
private MapCooldown _mapCooldown;
private MapLister _mapLister;
private Plugin? _plugin;

public VotemapCommand(MapLister mapLister, GameRules gamerules, IStringLocalizer stringLocalizer, ChangeMapManager changeMapManager, PluginState pluginState)
public VotemapCommand(MapLister mapLister, GameRules gamerules, IStringLocalizer stringLocalizer, ChangeMapManager changeMapManager, PluginState pluginState, MapCooldown mapCooldown)
{
_mapLister = mapLister;
_mapLister.EventMapsLoaded += OnMapsLoaded;
_gamerules = gamerules;
_localizer = new StringLocalizer(stringLocalizer, "votemap.prefix");
_changeMapManager = changeMapManager;
_pluginState = pluginState;
_mapCooldown = mapCooldown;
_mapCooldown.EventCooldownRefreshed += OnMapsLoaded;
}

public void OnMapStart(string map)
Expand All @@ -60,12 +66,18 @@ public void OnConfigParsed(Config config)
public void OnMapsLoaded(object? sender, Map[] maps)
{
votemapMenu = new("Votemap");
votemapMenuHud = new("VoteMap");
foreach (var map in _mapLister.Maps!.Where(x => x.Name != Server.MapName))
{
votemapMenu.AddMenuOption(map.Name, (CCSPlayerController player, ChatMenuOption option) =>
{
AddVote(player, option.Text);
});
}, _mapCooldown.IsMapInCooldown(map.Name));

votemapMenuHud.AddMenuOption(map.Name, (CCSPlayerController player, ChatMenuOption option) =>
{
AddVote(player, option.Text);
}, _mapCooldown.IsMapInCooldown(map.Name));
}
}

Expand Down Expand Up @@ -113,7 +125,10 @@ public void CommandHandler(CCSPlayerController? player, string map)

public void OpenVotemapMenu(CCSPlayerController player)
{
MenuManager.OpenChatMenu(player!, votemapMenu!);
if (_config.HudMenu)
MenuManager.OpenCenterHtmlMenu(_plugin, player, votemapMenuHud!);

Check warning on line 129 in Features/VotemapCommand.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Possible null reference argument for parameter 'plugin' in 'void MenuManager.OpenCenterHtmlMenu(BasePlugin plugin, CCSPlayerController player, CenterHtmlMenu menu)'.

Check warning on line 129 in Features/VotemapCommand.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Possible null reference argument for parameter 'plugin' in 'void MenuManager.OpenCenterHtmlMenu(BasePlugin plugin, CCSPlayerController player, CenterHtmlMenu menu)'.
else
MenuManager.OpenChatMenu(player, votemapMenu!);
}

void AddVote(CCSPlayerController player, string map)
Expand All @@ -124,6 +139,12 @@ void AddVote(CCSPlayerController player, string map)
return;
}

if (_mapCooldown.IsMapInCooldown(map))
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.map-played-recently"));
return;
}

if (_mapLister.Maps!.FirstOrDefault(x => x.Name.ToLower() == map) is null)
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.invalid-map"));
Expand Down Expand Up @@ -164,5 +185,10 @@ public void PlayerDisconnected(CCSPlayerController player)
foreach (var map in VotedMaps)
map.Value.RemoveVote(userId);
}

public void OnLoad(Plugin plugin)
{
_plugin = plugin;
}
}
}
2 changes: 1 addition & 1 deletion Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
public partial class Plugin : BasePlugin, IPluginConfig<Config>
{
public override string ModuleName => "RockTheVote";
public override string ModuleVersion => "1.7.7";
public override string ModuleVersion => "1.8.0";
public override string ModuleAuthor => "abnerfs";
public override string ModuleDescription => "General purpose map voting plugin";

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Feeling like paying me a ☕? Go ahead and donate:
- Changes in the config file will require you to reload the plugin or restart the server (change the map won't work).
- Maps that will be used in RTV/nominate/votemap/end of map vote are located in addons/counterstrikesharp/configs/plugins/RockTheVote/maplist.txt

## General config
| Config | Description | Default Value | Min | Max |
| -------------- | -------------------------------------------------------------------------------- | ------------- | --- | --- |
| MapsInCoolDown | Number of maps that can't be used in vote because they have been played recently | 3 | 0 | |

## RockTheVote
Players can type rtv to request the map to be changed, once a number of votes is reached (by default 60% of players in the server) a vote will start for the next map, this vote lasts up to 30 seconds (hardcoded for now), in the end server changes to the winner map.

Expand Down
3 changes: 2 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Vote for the next map: {0}s",
"emv.hud.finished": "Vote finished, next map: {0}",
"nextmap": "Next map will be {green}{0}",
"nextmap.decided-by-vote": "Next map will be decided by vote"
"nextmap.decided-by-vote": "Next map will be decided by vote",
"general.validation.map-played-recently": "Map has been played recently"
}
7 changes: 4 additions & 3 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
"rtv.already-rocked-the-vote": "Vous avez déjà lancé le vote",
"rtv.votes-reached": "Nombre de votes atteints, lancement du vote...",
"rtv.disabled": "Rtv est désactivé pour le moment",
"emv.you-voted": "Vous avez voté pour {0}",
"emv.you-voted": "Vous avez voté pour {0}",
"emv.vote-ended": "Le vote est terminé, la prochaine carte sera {green}{0}{default} ({1:N2}% de {2} vote(s))",
"emv.vote-ended-no-votes": "Pas de votes, la prochaine carte sera {green}{0}",
"emv.vote-ended-no-votes": "Pas de votes, la prochaine carte sera {green}{0}",
"general.changing-map": "Changement de carte vers {green}{0}",
"general.changing-map-next-round": "La carte changera pour {green}{0}{default} au prochain tour...",
"emv.hud.menu-title": "Votez pour la prochaine carte :",
"emv.hud.hud-timer": "Votez pour la prochaine carte : {0}s",
"emv.hud.finished": "Vote terminé, prochaine carte : {0}",
"nextmap": "La prochaine carte sera {green}{0}",
"nextmap.decided-by-vote": "La prochaine carte sera choisie par vote"
"nextmap.decided-by-vote": "La prochaine carte sera choisie par vote",
"general.validation.map-played-recently": "La carte a été récemment jouée"
}
3 changes: 2 additions & 1 deletion lang/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Szavazz a következő pályára: {0}mp",
"emv.hud.finished": "A szavazás végetért, a köv. pálya: {0}",
"nextmap": "A köv. pálya ez lesz: {green}{0}",
"nextmap.decided-by-vote": "A következő pálya majd az automatikus szavazás útján fog eldőlni"
"nextmap.decided-by-vote": "A következő pálya majd az automatikus szavazás útján fog eldőlni",
"general.validation.map-played-recently": "A térkép nemrégiben játszották"
}
3 changes: 2 additions & 1 deletion lang/lv.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Balso par nākošo karti: {0}s",
"emv.hud.finished": "Balsošana beidzās, nākošā karte: {0}",
"nextmap": "Nākošā karte būs {green}{0}",
"nextmap.decided-by-vote": "Nākošā karte tiks izvēlētā pēc balsošanas"
"nextmap.decided-by-vote": "Nākošā karte tiks izvēlētā pēc balsošanas",
"general.validation.map-played-recently": "A térkép nemrégiben játszották"
}
3 changes: 2 additions & 1 deletion lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Głosuj na następną mapę: {0}s",
"emv.hud.finished": "Głosowanie zakończone, następna mapa: {0}",
"nextmap": "Następną mapą będzie {green}{0}",
"nextmap.decided-by-vote": "Następna mapa zostanie wybrana głosowaniem"
"nextmap.decided-by-vote": "Następna mapa zostanie wybrana głosowaniem",
"general.validation.map-played-recently": "Mapa została niedawno rozegrana"
}
3 changes: 2 additions & 1 deletion lang/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Vote para o próximo mapa: {0}s",
"emv.hud.finished": "Votação encerrada, próximo mapa: {0}",
"nextmap": "O próximo mapa será {green}{0}",
"nextmap.decided-by-vote": "O próximo mapa será decidido pelo voto"
"nextmap.decided-by-vote": "O próximo mapa será decidido pelo voto",
"general.validation.map-played-recently": "O mapa foi jogado recentemente"
}
3 changes: 2 additions & 1 deletion lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Голосование за следующую карту: {0}s",
"emv.hud.finished": "Голосование завершено. Следующая карта: {0}",
"nextmap": "Следующей картой будет {green}{0}",
"nextmap.decided-by-vote": "Следующая карта будет определена голосованием"
"nextmap.decided-by-vote": "Следующая карта будет определена голосованием",
"general.validation.map-played-recently": "Карта была недавно сыграна"
}
3 changes: 2 additions & 1 deletion lang/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Sonraki haritaya oy verin: {0}",
"emv.hud.finished": "Oylama tamamlandı, sonraki harita: {0}",
"nextmap": "Sonraki harita {green}{0} olacak",
"nextmap.decided-by-vote": "Sonraki haritaya oylamayla karar verilecek"
"nextmap.decided-by-vote": "Sonraki haritaya oylamayla karar verilecek",
"general.validation.map-played-recently": "Harita yakın zamanda oynandı"
}
3 changes: 2 additions & 1 deletion lang/ua.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "Голосуйте за наступну карту: {0}с",
"emv.hud.finished": "Голосування завершено, наступна карта: {0}",
"nextmap": "Наступна карта буде {green}{0}",
"nextmap.decided-by-vote": "Наступна карта буде визначена голосуванням"
"nextmap.decided-by-vote": "Наступна карта буде визначена голосуванням",
"general.validation.map-played-recently": "Карта була недавно зіграна"
}
3 changes: 2 additions & 1 deletion lang/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
"emv.hud.hud-timer": "投票下一张地图: {0}s",
"emv.hud.finished": "投票完成,下一张地图为: {0}",
"nextmap": "下一张地图为 {green}{0}",
"nextmap.decided-by-vote": "下一张地图将由投票决定"
"nextmap.decided-by-vote": "下一张地图将由投票决定",
"general.validation.map-played-recently": "地图最近已经被玩过"
}

0 comments on commit 5e83cd2

Please sign in to comment.