Skip to content

Commit

Permalink
Add work map integration (#29)
Browse files Browse the repository at this point in the history
* Add auto ws map hosting

* Add polish translation

* bump version
  • Loading branch information
abnerfs authored Feb 22, 2024
1 parent 99c7c3c commit 8995501
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 20 deletions.
26 changes: 21 additions & 5 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 CounterStrikeSharp.API.Modules.Menu;

namespace cs2_rockthevote
{
Expand All @@ -26,17 +27,27 @@ public class ChangeMapManager : IPluginDependency<Plugin, Config>
private Plugin? _plugin;
private StringLocalizer _localizer;
private PluginState _pluginState;
private MapLister _mapLister;

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

private Map[] _maps = new Map[0];

public ChangeMapManager(StringLocalizer localizer, PluginState pluginState)

public ChangeMapManager(StringLocalizer localizer, PluginState pluginState, MapLister mapLister)
{
_localizer = localizer;
_pluginState = pluginState;
_mapLister = mapLister;
_mapLister.EventMapsLoaded += OnMapsLoaded;
}

public void OnMapsLoaded(object? sender, Map[] maps)
{
_maps = maps;
}


Expand Down Expand Up @@ -66,12 +77,17 @@ public bool ChangeNextMap(bool mapEnd = false)
Server.PrintToChatAll(_localizer.LocalizeWithPrefixInternal(_prefix, "general.changing-map", NextMap!));
_plugin.AddTimer(3.0F, () =>

Check warning on line 78 in Core/ChangeMapManager.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Dereference of a possibly null reference.

Check warning on line 78 in Core/ChangeMapManager.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Dereference of a possibly null reference.
{
if (Server.IsMapValid(NextMap!))
Map map = _maps.FirstOrDefault(x => x.Name == NextMap!)!;
if (Server.IsMapValid(map.Name))
{
Server.ExecuteCommand($"changelevel {map.Name}");
}
else if(map.Id is not null)
{
Server.ExecuteCommand($"changelevel {NextMap}");
Server.ExecuteCommand($"host_workshop_map {map.Id}");
}
else
Server.ExecuteCommand($"ds_workshop_changelevel {NextMap}");
else
Server.ExecuteCommand($"ds_workshop_changelevel {map.Name}");
});
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/EndMapVoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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!.Where(x => x != Server.MapName).ToList());
var mapsScrambled = Shuffle(new Random(), _mapLister.Maps!.Select(x => x.Name).Where(x => x != Server.MapName).ToList());
mapsEllected = _nominationManager.NominationWinners().Concat(mapsScrambled).Distinct().ToList();

_canVote = ServerManager.ValidPlayerCount();
Expand Down
14 changes: 14 additions & 0 deletions Core/Map.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace cs2_rockthevote
{
public class Map
{
public string? Id { get; set; }
public string Name { get; set; }

public Map(string name, string? id)
{
Id = id?.Trim();
Name = name.Trim().ToLower();
}
}
}
13 changes: 10 additions & 3 deletions CrossCutting/MapLister.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace cs2_rockthevote
using cs2_rockthevote.Core;

namespace cs2_rockthevote
{
public class MapLister : IPluginDependency<Plugin, Config>
{
public string[]? Maps { get; private set; } = null;
public Map[]? Maps { get; private set; } = null;
public bool MapsLoaded { get; private set; } = false;

public event EventHandler<string[]>? EventMapsLoaded;
public event EventHandler<Map[]>? EventMapsLoaded;

private Plugin? _plugin;

Expand All @@ -32,6 +34,11 @@ void LoadMaps()
.Split("\n")
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x) && !x.StartsWith("//"))
.Select(mapLine =>
{
string[] args = mapLine.Split(":");
return new Map(args[0], args.Length == 2 ? args[1] : null);
})
.ToArray();

MapsLoaded = true;
Expand Down
10 changes: 5 additions & 5 deletions Features/NominationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void OnConfigParsed(Config config)
}


public void OnMapsLoaded(object? sender, string[] maps)
public void OnMapsLoaded(object? sender, Map[] maps)
{
nominationMenu = new("Nomination");
foreach (var map in _mapLister.Maps!.Where(x => x != Server.MapName))
foreach (var map in _mapLister.Maps!.Where(x => x.Name != Server.MapName))
{
nominationMenu.AddMenuOption(map, (CCSPlayerController player, ChatMenuOption option) =>
nominationMenu.AddMenuOption(map.Name, (CCSPlayerController player, ChatMenuOption option) =>
{
Nominate(player, option.Text);
});
Expand Down Expand Up @@ -107,7 +107,7 @@ public void CommandHandler(CCSPlayerController player, string map)

public void OpenNominationMenu(CCSPlayerController player)
{
ChatMenus.OpenMenu(player!, nominationMenu!);
MenuManager.OpenChatMenu(player!, nominationMenu!);
}

void Nominate(CCSPlayerController player, string map)
Expand All @@ -118,7 +118,7 @@ void Nominate(CCSPlayerController player, string map)
return;
}

if (_mapLister.Maps!.FirstOrDefault(x => x.ToLower() == map) is null)
if (_mapLister.Maps!.Select(x => x.Name).FirstOrDefault(x => x.ToLower() == map) is null)
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.invalid-map"));
return;
Expand Down
10 changes: 5 additions & 5 deletions Features/VotemapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public void OnConfigParsed(Config config)
}


public void OnMapsLoaded(object? sender, string[] maps)
public void OnMapsLoaded(object? sender, Map[] maps)
{
votemapMenu = new("Votemap");
foreach (var map in _mapLister.Maps!.Where(x => x != Server.MapName))
foreach (var map in _mapLister.Maps!.Where(x => x.Name != Server.MapName))
{
votemapMenu.AddMenuOption(map, (CCSPlayerController player, ChatMenuOption option) =>
votemapMenu.AddMenuOption(map.Name, (CCSPlayerController player, ChatMenuOption option) =>
{
AddVote(player, option.Text);
});
Expand Down Expand Up @@ -108,7 +108,7 @@ public void CommandHandler(CCSPlayerController player, string map)

public void OpenVotemapMenu(CCSPlayerController player)
{
ChatMenus.OpenMenu(player!, votemapMenu!);
MenuManager.OpenChatMenu(player!, votemapMenu!);
}

void AddVote(CCSPlayerController player, string map)
Expand All @@ -119,7 +119,7 @@ void AddVote(CCSPlayerController player, string map)
return;
}

if (_mapLister.Maps!.FirstOrDefault(x => x.ToLower() == map) is null)
if (_mapLister.Maps!.FirstOrDefault(x => x.Name.ToLower() == map) is null)
{
player!.PrintToChat(_localizer.LocalizeWithPrefix("general.invalid-map"));
return;
Expand Down
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.6.1";
public override string ModuleVersion => "1.7.0";
public override string ModuleAuthor => "abnerfs";
public override string ModuleDescription => "General purpose map voting plugin";

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Players can type `nextmap` to see which map is going to be played next
- Configurable
- Nextmap command

## Adding workshop maps
In order to add workshop maps you need to know it's id and add as following in the maplist.txt file <mapname>:<workshop-id>

```
de_thera:3121217565
de_dust2
```

# Limitations
- Plugins is still under development and a lot of functionality is still going to be added in the future.
Expand Down
40 changes: 40 additions & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"rtv.prefix": "{red}[RockTheVote]{default}",
"votemap.prefix": "{red}[Votemap]{default}",
"timeleft.prefix": "{red}[Timeleft]{default}",
"timeleft.remaining-rounds": "Pozostało {0} rund(y)",
"timeleft.remaining-time-hour": "Pozostały czas {0}:{1}:{2}",
"timeleft.remaining-time-minute": "Pozostały czas {0} minut(y) i {1} sekund(y)",
"timeleft.remaining-time-second": "Pozostały czas {0} sekund(y)",
"timeleft.no-time-limit": "Nie ma limitu czasowego",
"timeleft.last-round": "To jest ostatnia runda",
"timeleft.time-over": "Czas minął, to jest ostatnia runda",
"votemap.player-voted": "Gracz {green}{0}{default} zagłosował na {green}{1}{default}",
"votemap.already-voted": "Już zagłosowałeś na {green}{0}{default}",
"votemap.in-the-map": "na mapie {0}",
"votemap.changing-map": "Zmieniam mapę na {green}{0}",
"votemap.changing-map-next-round": "Mapa zostanie zmieniona na {green}{0}{default} w następnej rundzie...",
"general.votes-needed": "({0} zagłosowało, potrzeba {1})",
"general.invalid-map": "Nieprawidłowa mapa",
"nominate.nominated": "Gracz {green}{0}{default} nominował mapę {green}{1}{default}, obecnie ma {2} głos(y)",
"nominate.already-nominated": "Już nominowałeś mapę {green}{0}{default}, ma {1} głos(y)",
"general.validation.current-map": "Nie możesz wybrać obecnej mapy",
"general.validation.minimum-rounds": "Minimalna liczba rund do użycia tej komendy to {0}",
"general.validation.warmup": "Komenda wyłączona podczas rozgrzewki.",
"general.validation.minimum-players": "Minimalna liczba graczy do użycia tej komendy to {0}",
"general.validation.disabled": "Komenda obecnie wyłączona",
"rtv.rocked-the-vote": "Gracz {green}{0}{default} chce rozpocząć głosowanie",
"rtv.already-rocked-the-vote": "Już rozpocząłeś głosowanie",
"rtv.votes-reached": "Osiągnięto wymaganą liczbę głosów, rozpoczynam głosowanie...",
"rtv.disabled": "RTV jest obecnie wyłączone",
"emv.you-voted": "Zagłosowałeś na {0}",
"emv.vote-ended": "Głosowanie zakończone, następną mapą będzie {green}{0}{default} ({1:N2}% z {2} głosów)",
"emv.vote-ended-no-votes": "Brak głosów, następną mapą będzie {green}{0}",
"general.changing-map": "Zmieniam mapę na {green}{0}",
"general.changing-map-next-round": "Mapa zostanie zmieniona na {green}{0}{default} w następnej rundzie...",
"emv.hud.menu-title": "Głosuj na następną mapę:",
"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"
}
1 change: 1 addition & 0 deletions maplist.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//Put your map list here
de_thera:3121217565
de_vertigo
cs_italy
de_inferno
Expand Down

0 comments on commit 8995501

Please sign in to comment.