Skip to content

Commit

Permalink
v1.5.0 (#24)
Browse files Browse the repository at this point in the history
* v1.5.0

* Update readme

* fix
  • Loading branch information
abnerfs authored Feb 18, 2024
1 parent e3296c0 commit 7128175
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class RtvConfig : ICommandConfig, IVoteConfig, IEndOfMapConfig
{
public bool Enabled { get; set; } = true;
public bool EnabledInWarmup { get; set; } = true;
public bool NominationEnabled { get; set; } = true;
public int MinPlayers { get; set; } = 0;
public int MinRounds { get; set; } = 0;
public bool ChangeMapImmediatly { get; set; } = true;
Expand All @@ -59,7 +60,7 @@ public class VotemapConfig : ICommandConfig, IVoteConfig

public class Config : IBasePluginConfig
{
public int Version { get; set; } = 7;
public int Version { get; set; } = 8;
public RtvConfig Rtv { get; set; } = new();
public VotemapConfig Votemap { get; set; } = new();
public EndOfMapConfig EndOfMapVote { get; set; } = new();
Expand Down
3 changes: 1 addition & 2 deletions Core/AsyncVoteValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
public class AsyncVoteValidator
{
private float VotePercentage = 0F;

public int RequiredVotes { get => (int)Math.Ceiling(ServerManager.ValidPlayerCount() * VotePercentage); }
public int RequiredVotes { get => (int)Math.Round(ServerManager.ValidPlayerCount() * VotePercentage); }
private IVoteConfig _config { get; set; }

public AsyncVoteValidator(IVoteConfig config)
Expand Down
14 changes: 7 additions & 7 deletions Core/ChangeMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ChangeMapManager : IPluginDependency<Plugin, Config>
private StringLocalizer _localizer;
private PluginState _pluginState;

private string? _nextMap = null;
public string? NextMap { get; private set; } = null;
private string _prefix = DEFAULT_PREFIX;
private const string DEFAULT_PREFIX = "rtv.prefix";
private bool _mapEnd = false;
Expand All @@ -42,15 +42,15 @@ public ChangeMapManager(StringLocalizer localizer, PluginState pluginState)

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

public void OnMapStart()
{
_nextMap = null;
NextMap = null;
_prefix = DEFAULT_PREFIX;
}

Expand All @@ -63,15 +63,15 @@ public bool ChangeNextMap(bool mapEnd = false)
return false;

_pluginState.MapChangeScheduled = false;
Server.PrintToChatAll(_localizer.LocalizeWithPrefixInternal(_prefix, "general.changing-map", _nextMap!));
Server.PrintToChatAll(_localizer.LocalizeWithPrefixInternal(_prefix, "general.changing-map", NextMap!));
_plugin.AddTimer(3.0F, () =>

Check warning on line 67 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 67 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!))
if (Server.IsMapValid(NextMap!))
{
Server.ExecuteCommand($"changelevel {_nextMap}");
Server.ExecuteCommand($"changelevel {NextMap}");
}
else
Server.ExecuteCommand($"ds_workshop_changelevel {_nextMap}");
Server.ExecuteCommand($"ds_workshop_changelevel {NextMap}");
});
return true;
}
Expand Down
1 change: 1 addition & 0 deletions Core/EndMapVoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void EndVote()

decimal totalVotes = Votes.Select(x => x.Value).Sum();
decimal percent = totalVotes > 0 ? winner.Value / totalVotes * 100M : 0;

if (maxVotes > 0)
{
Server.PrintToChatAll(_localizer.LocalizeWithPrefix("emv.vote-ended", winner.Key, percent, totalVotes));
Expand Down
26 changes: 25 additions & 1 deletion Core/MaxRoundsManager.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.Cvars;
using CounterStrikeSharp.API.Modules.Utils;
using System.Security.Cryptography.X509Certificates;

namespace cs2_rockthevote
{
Expand All @@ -14,6 +15,7 @@ public class MaxRoundsManager : IPluginDependency<Plugin, Config>
private ConVar? _maxRounds;
private int MaxRoundsValue => _maxRounds!.GetPrimitiveValue<int>();
public bool UnlimitedRounds => MaxRoundsValue <= 0;
private bool _lastBeforeHalf = false;
public int RemainingRounds
{
get
Expand Down Expand Up @@ -62,6 +64,14 @@ public void ClearRounds()
{
CTWins = 0;
TWins = 0;
_lastBeforeHalf = false;
}

void SwapScores()
{
var oldCtWins = CTWins;
CTWins = TWins;
TWins = oldCtWins;
}

public void RoundWin(CsTeam team)
Expand Down Expand Up @@ -95,15 +105,29 @@ public void OnLoad(Plugin plugin)
if (winner is not null)
RoundWin(winner.Value);

if (_lastBeforeHalf)
SwapScores();

_lastBeforeHalf = false;
return HookResult.Continue;
});


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

_lastBeforeHalf = true;
return HookResult.Continue;
});


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

ClearRounds();
return HookResult.Continue;
});
Expand Down
40 changes: 40 additions & 0 deletions Features/NextMapCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CounterStrikeSharp.API.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cs2_rockthevote.Features
{
public class NextMapCommand : IPluginDependency<Plugin, Config>
{
private ChangeMapManager _changeMapManager;
private StringLocalizer _stringLocalizer;

public NextMapCommand(ChangeMapManager changeMapManager, StringLocalizer stringLocalizer)
{
_changeMapManager = changeMapManager;
_stringLocalizer = stringLocalizer;
}

public void CommandHandler(CCSPlayerController? player)
{
if (player is not null)
if (_changeMapManager.NextMap is not null)
{
player.PrintToChat(_stringLocalizer.LocalizeWithPrefix("nextmap", _changeMapManager.NextMap));
}
else
player.PrintToChat(_stringLocalizer.LocalizeWithPrefix("nextmap.decided-by-vote"));
}

public void OnLoad(Plugin plugin) {

plugin.AddCommand("nextmap", "Shows nextmap when defined", (player, info) =>
{
CommandHandler(player);
});
}
}
}
2 changes: 1 addition & 1 deletion Features/NominationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void OnMapsLoaded(object? sender, string[] maps)
public void CommandHandler(CCSPlayerController player, string map)
{
map = map.ToLower().Trim();
if (_pluginState.DisableCommands || !_config.Enabled)
if (_pluginState.DisableCommands || !_config.NominationEnabled)
{
player.PrintToChat(_localizer.LocalizeWithPrefix("general.validation.disabled"));
return;
Expand Down
13 changes: 10 additions & 3 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using cs2_rockthevote.Features;
using Microsoft.Extensions.DependencyInjection;
using static CounterStrikeSharp.API.Core.Listeners;

Expand All @@ -20,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.4.0";
public override string ModuleVersion => "1.5.0";
public override string ModuleAuthor => "abnerfs";
public override string ModuleDescription => "General purpose map voting plugin";

Expand All @@ -31,21 +32,23 @@ public partial class Plugin : BasePlugin, IPluginConfig<Config>
private readonly VotemapCommand _votemapManager;
private readonly RockTheVoteCommand _rtvManager;
private readonly TimeLeftCommand _timeLeft;

private readonly NextMapCommand _nextMap;

public Plugin(DependencyManager<Plugin, Config> dependencyManager,
NominationCommand nominationManager,
ChangeMapManager changeMapManager,
VotemapCommand voteMapManager,
RockTheVoteCommand rtvManager,
TimeLeftCommand timeLeft)
TimeLeftCommand timeLeft,
NextMapCommand nextMap)
{
_dependencyManager = dependencyManager;
_nominationManager = nominationManager;
_changeMapManager = changeMapManager;
_votemapManager = voteMapManager;
_rtvManager = rtvManager;
_timeLeft = timeLeft;
_nextMap = nextMap;
}

public Config? Config { get; set; }

Check warning on line 54 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 54 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 Expand Up @@ -86,6 +89,10 @@ public HookResult OnChat(EventPlayerChat @event, GameEventInfo info)
{
_timeLeft.CommandHandler(player);
}
else if (text.StartsWith("nextmap"))
{
_nextMap.CommandHandler(player);
}
return HookResult.Continue;
}

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Players can vote to change to an specific map by using the votemap <mapname> com
## Timeleft
Players can type `timeleft` to see how much time is left in the current map

## Nextmap
Players can type `nextmap` to see which map is going to be played next

# Features
- Reads from a custom maplist
- RTV Command
Expand All @@ -25,6 +28,7 @@ Players can type `timeleft` to see how much time is left in the current map
- Votemap chatmenu
- Supports workshop maps
- Configurable
- Nextmap command


# Limitations
Expand All @@ -46,10 +50,11 @@ Players can type `timeleft` to see how much time is left in the current map

```json
{
"Version": 7,
"Version": 8,
"Rtv": {
"Enabled": true,
"EnabledInWarmup": true,
"NominationEnabled": true,
"MinPlayers": 0,
"MinRounds": 0,
"ChangeMapImmediatly": true,
Expand Down
6 changes: 4 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"rtv.votes-reached": "Number of votes reached, starting vote...",
"rtv.disabled": "Rtv is disabled right now",
"emv.you-voted": "You voted in {0}",
"emv.vote-ended": "Vote ended the next map will be {green}{0}{default} ({1}% of {2} vote(s))",
"emv.vote-ended": "Vote ended the next map will be {green}{0}{default} ({1:N2}% of {2} vote(s))",
"emv.vote-ended-no-votes": "No votes, the next map will be {green}{0}",
"general.changing-map": "Changing map to {green}{0}",
"general.changing-map-next-round": "The map will be changed to {green}{0}{default} in the next round...",
"emv.hud.menu-title": "Vote for the next map:",
"emv.hud.hud-timer": "Vote for the next map: {0}s",
"emv.hud.finished": "Vote finished, next map: {0}"
"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"
}
6 changes: 4 additions & 2 deletions lang/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"rtv.votes-reached": "Número de votos atingido, iniciando votação...",
"rtv.disabled": "Rtv está desativado no momento",
"emv.you-voted": "Você votou em {0}",
"emv.vote-ended": "Votação encerrada, o próximo mapa será {green}{0}{default} ({1}% de {2} voto(s))",
"emv.vote-ended": "Votação encerrada, o próximo mapa será {green}{0}{default} ({1:N2}% de {2} voto(s))",
"emv.vote-ended-no-votes": "Sem votos, o próximo mapa será {green}{0}",
"general.changing-map": "Mudando o mapa para {green}{0}",
"general.changing-map-next-round": "O mapa será mudado para {green}{0}{default} no próximo round...",
"emv.hud.menu-title": "Vote para o próximo mapa:",
"emv.hud.hud-timer": "Vote para o próximo mapa: {0}s",
"emv.hud.finished": "Votação encerrada, próximo mapa: {0}"
"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"
}
6 changes: 4 additions & 2 deletions lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"rtv.votes-reached": "Количество голосов набрано. Начало голосования",
"rtv.disabled": "Сейчас смена карты выключена",
"emv.you-voted": "Вы проголосовали за {0}",
"emv.vote-ended": "Голосование завершено. Следующей картой будет {green}{0}{default} ({1}% из {2} голосов)",
"emv.vote-ended": "Голосование завершено. Следующей картой будет {green}{0}{default} ({1:N2}% из {2} голосов)",
"emv.vote-ended-no-votes": "Нет голосов. Следующая карта будет {green}{0}",
"general.changing-map": "Смена карты на {green}{0}",
"general.changing-map-next-round": "Карта будет изменена на {green}{0}{default} в следующем раунде",
"emv.hud.menu-title": "Голосование за следующую карту:",
"emv.hud.hud-timer": "Голосование за следующую карту: {0}s",
"emv.hud.finished": "Голосование завершено. Следующая карта: {0}"
"emv.hud.finished": "Голосование завершено. Следующая карта: {0}",
"nextmap": "Следующей картой будет {green}{0}",
"nextmap.decided-by-vote": "Следующая карта будет определена голосованием"
}
6 changes: 4 additions & 2 deletions lang/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"rtv.votes-reached": "Oy sayısına ulaşıldı, oylamaya başlıyor...",
"rtv.disabled": "Rtv şu anda devre dışı",
"emv.you-voted": "{0} oy verdiniz",
"emv.vote-ended": "Oylama sona erdi, sonraki harita {green}{0}{default} olacak ({2} oydan %{1}'i)",
"emv.vote-ended": "Oylama sona erdi, sonraki harita {green}{0}{default} olacak ({2} oydan %{1:N2}'i)",
"emv.vote-ended-no-votes": "Oy yok, sonraki harita {green}{0} olacak",
"general.changing-map": "Harita {green}{0} olarak değiştiriliyor",
"general.changing-map-next-round": "Harita bir sonraki turda {green}{0}{default} olarak değiştirilecek...",
"emv.hud.menu-title": "Bir sonraki haritaya oy verin:",
"emv.hud.hud-timer": "Sonraki haritaya oy verin: {0}",
"emv.hud.finished": "Oylama tamamlandı, sonraki harita: {0}"
"emv.hud.finished": "Oylama tamamlandı, sonraki harita: {0}",
"nextmap": "Sonraki harita {green}{0} olacak",
"nextmap.decided-by-vote": "Sonraki haritaya oylamayla karar verilecek"
}
6 changes: 4 additions & 2 deletions lang/ua.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"rtv.votes-reached": "Досягнуто кількість голосів, починається голосування...",
"rtv.disabled": "Rtv вимкнено зараз",
"emv.you-voted": "Ви проголосували за {0}",
"emv.vote-ended": "Голосування завершено, наступна карта - {green}{0}{default} ({1}% з {2} голосів)",
"emv.vote-ended": "Голосування завершено, наступна карта - {green}{0}{default} ({1:N2}% з {2} голосів)",
"emv.vote-ended-no-votes": "Немає голосів, наступна карта - {green}{0}",
"general.changing-map": "Зміна карти на {green}{0}",
"general.changing-map-next-round": "Карта буде змінена на {green}{0}{default} у наступному раунді...",
"emv.hud.menu-title": "Голосуйте за наступну карту:",
"emv.hud.hud-timer": "Голосуйте за наступну карту: {0}с",
"emv.hud.finished": "Голосування завершено, наступна карта: {0}"
"emv.hud.finished": "Голосування завершено, наступна карта: {0}",
"nextmap": "Наступна карта буде {green}{0}",
"nextmap.decided-by-vote": "Наступна карта буде визначена голосуванням"
}

0 comments on commit 7128175

Please sign in to comment.