Skip to content

Commit

Permalink
Merge pull request #2 from imi-tat0r/custom-vote-percentage
Browse files Browse the repository at this point in the history
- Added MinVotePercentage
- Added more languages
  • Loading branch information
imi-tat0r authored Mar 28, 2024
2 parents 4ba3fc1 + a5e4136 commit f323571
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CS2-CustomVotes.Shared/CS2-CustomVotes.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="mkdir $(SolutionDir)Public\addons\counterstrikesharp\shared\CS2-CustomVotes.Shared" IgnoreExitCode="true" />
<Exec Command="xcopy /Y /R $(TargetDir)*.* $(SolutionDir)Public\addons\counterstrikesharp\shared\CS2-CustomVotes.Shared\" />
</Target>
</Project>
3 changes: 3 additions & 0 deletions CS2-CustomVotes.Shared/ICustomVoteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ public interface ICustomVoteApi
{
public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style);
public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style);

public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style, int minVotePercentage);
public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style, int minVotePercentage);
public void RemoveCustomVote(string name);
}
6 changes: 6 additions & 0 deletions CS2-CustomVotes/CS2-CustomVotes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
<ProjectReference Include="..\CS2-CustomVotes.Shared\CS2-CustomVotes.Shared.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="mkdir $(SolutionDir)Public\addons\counterstrikesharp\plugins\CS2-CustomVotes\lang" IgnoreExitCode="true" />
<Exec Command="xcopy /Y /R $(TargetDir)*.* $(SolutionDir)Public\addons\counterstrikesharp\plugins\CS2-CustomVotes\" />
<Exec Command="xcopy /Y /R $(ProjectDir)lang\*.* $(SolutionDir)Public\addons\counterstrikesharp\plugins\CS2-CustomVotes\lang\" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;

namespace CS2_CustomVotes.Helpers;
namespace CS2_CustomVotes.Extensions;

public static class Config
public static class ConfigExtensions
{
private static readonly string AssemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "";
private static readonly string CfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{AssemblyName}/{AssemblyName}.json";
public static readonly string CfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{AssemblyName}/{AssemblyName}.json";

public static void Update<T>(this T config) where T : BasePluginConfig, new()
{
Expand Down
7 changes: 6 additions & 1 deletion CS2-CustomVotes/Models/ActiveVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public KeyValuePair<string, List<uint>> GetWinningOption()
return new KeyValuePair<string, List<uint>>(Vote.DefaultOption, new List<uint>());

var winningOption = OptionVotes.MaxBy(x => x.Value.Count);
return winningOption;
var totalVotes = OptionVotes.Sum(x => x.Value.Count);

if (Vote.MinVotePercentage < 0 || winningOption.Value.Count >= totalVotes * Vote.MinVotePercentage / 100)
return winningOption;

return new KeyValuePair<string, List<uint>>(Vote.DefaultOption, new List<uint>());
}
}
5 changes: 4 additions & 1 deletion CS2-CustomVotes/Models/CustomVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public class CustomVote

[JsonPropertyName("Style")]
public string Style { get; set; } = null!;


[JsonPropertyName("MinVotePercentage")]
public int MinVotePercentage { get; set; } = -1;

[JsonPropertyName("Permission")]
public Permission Permission { get; set; } = new();

Expand Down
27 changes: 21 additions & 6 deletions CS2-CustomVotes/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using CounterStrikeSharp.API.Core;
using System.Text.Json;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CS2_CustomVotes.Extensions;
using CS2_CustomVotes.Factories;
using CS2_CustomVotes.Helpers;
using CS2_CustomVotes.Models;
using CS2_CustomVotes.Services;
using CS2_CustomVotes.Shared;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,7 +19,7 @@ public class CustomVotes : BasePlugin, IPluginConfig<CustomVotesConfig>
{
public override string ModuleName => "Custom Votes";
public override string ModuleDescription => "Allows you to create custom votes for your server.";
public override string ModuleVersion => "1.0.0";
public override string ModuleVersion => "1.0.1";
public override string ModuleAuthor => "imi-tat0r";

public CustomVotesConfig Config { get; set; } = null!;
Expand Down Expand Up @@ -58,9 +61,21 @@ public void OnConfigParsed(CustomVotesConfig config)
config.Update();
}

public override void Unload(bool hotReload)
[ConsoleCommand("css_reload_cfg", "Reload the config in the current session without restarting the server")]
[RequiresPermissions("@css/generic")]
[CommandHelper(minArgs: 0, whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
public void OnReloadConfigCommand(CCSPlayerController? player, CommandInfo info)
{
base.Unload(hotReload);
var config = File.ReadAllText(ConfigExtensions.CfgPath);
try
{
OnConfigParsed(JsonSerializer.Deserialize<CustomVotesConfig>(config,
new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip })!);
}
catch (Exception e)
{
info.ReplyToCommand($"[DiscordChatSync] Failed to reload config: {e.Message}");
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions CS2-CustomVotes/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ public class CustomVotesConfig : BasePluginConfig
{ "Disable", new("{Red}Disable", new List<string> { "sv_cheats 0" })}
},
DefaultOption = "Disable",
Style = "center"
Style = "center",
MinVotePercentage = 50,
Permission = new Permission
{
RequiresAll = false,
Permissions = new List<string>()
}
}
};

[JsonPropertyName("ConfigVersion")]
public override int Version { get; set; } = 1;
public override int Version { get; set; } = 2;
}
15 changes: 13 additions & 2 deletions CS2-CustomVotes/Services/CustomVoteApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ public CustomVoteApi(IVoteManager voteManager)

public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style)
{
_voteManager.AddVote(name, new List<string>(), description, defaultOption, timeToVote, options, style);
_voteManager.AddVote(name, new List<string>(), description, defaultOption, timeToVote, options, style, -1);
}

public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote,
Dictionary<string, VoteOption> options, string style)
{
_voteManager.AddVote(name, aliases, description, defaultOption, timeToVote, options, style);
_voteManager.AddVote(name, aliases, description, defaultOption, timeToVote, options, style, -1);
}

public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style, int minVotePercentage)
{
_voteManager.AddVote(name, new List<string>(), description, defaultOption, timeToVote, options, style, minVotePercentage);
}

public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote,
Dictionary<string, VoteOption> options, string style, int minVotePercentage)
{
_voteManager.AddVote(name, aliases, description, defaultOption, timeToVote, options, style, minVotePercentage);
}

public void RemoveCustomVote(string name)
Expand Down
7 changes: 4 additions & 3 deletions CS2-CustomVotes/Services/VoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace CS2_CustomVotes.Services;
public interface IVoteManager
{
public void AddVote(CustomVote vote);
public void AddVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style = "center");
public void AddVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style = "center", int minVotePercentage = 50);
public void RemoveVote(string name);

public bool StartVote(CCSPlayerController? player, string name, out string baseName);
Expand Down Expand Up @@ -76,7 +76,7 @@ public void AddVote(CustomVote vote)

vote.ExecuteCommand();
}
public void AddVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style = "center")
public void AddVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style = "center", int minVotePercentage = 50)
{
var customVote = new CustomVote
{
Expand All @@ -86,7 +86,8 @@ public void AddVote(string name, List<string> aliases, string description, strin
DefaultOption = defaultOption,
TimeToVote = timeToVote,
Options = options,
Style = style
Style = style,
MinVotePercentage = minVotePercentage,
};
AddVote(customVote);
}
Expand Down
9 changes: 9 additions & 0 deletions CS2-CustomVotes/lang/cz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"vote.disabled": "Speciální hlasování jsou {DarkBlue}vypnuta{Default}.",
"vote.active": "Hlasování již probíhá.",
"vote.started": "Hráč {DarkBlue}{0}{Default} spustil hlasování pro {DarkBlue}{1}{Default}.",
"vote.cooldown": "Prosím, počkejte {DarkBlue}{0}{Default} sekund mezi hlasováním.",
"vote.already_voted": "Již jste hlasovali.",
"vote.finished_with": "Hlasování pro {DarkBlue}{0}{Default} skončilo s výsledkem {DarkBlue}{1}{Default} ({2} hlasy).",
"vote.no_permission": "Nemáte potřebné oprávnění k zahájení tohoto hlasování."
}
9 changes: 9 additions & 0 deletions CS2-CustomVotes/lang/pt-pt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"vote.disabled": "Os votos personalizados estão {DarkBlue}DESATIVADOS{Default}!",
"vote.active": "Já está a decorrer uma votação!",
"vote.started": "O jogador {DarkBlue}{0}{Default} começou uma votação para {DarkBlue}{1}{Default}",
"vote.cooldown": "Por favor espere {DarkBlue}{0}{Default} segundos entre as votações!",
"vote.already_voted": "Já votou!",
"vote.finished_with": "Votar para {DarkBlue}{0}{Default} acaba em {DarkBlue}{1}{Default} ({2} votos).",
"vote.no_permission": "Não tem a permissão necessária para iniciar esta votação..."
}
9 changes: 9 additions & 0 deletions CS2-CustomVotes/lang/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"vote.disabled": "Custom Votes {DarkBlue}отключен{Default}.",
"vote.active": "Голосование уже началось.",
"vote.started": "Игрок {DarkBlue}{0}{Default} начал голосование {DarkBlue}{1}{Default}.",
"vote.cooldown": "Пожалуйста, подождите {DarkBlue}{0}{Default} секунд между голосованиями.",
"vote.already_voted": "Вы уже проголосовали.",
"vote.finished_with": "Голосование {DarkBlue}{0}{Default} закончилось с {DarkBlue}{1}{Default} ({2} голосов).",
"vote.no_permission": "У вас нет прав для начала этого голосования."
}
10 changes: 7 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Copyright ev0lve Digital](https://img.shields.io/badge/Copyright-ev0lve%20Digital-blue) ![GitHub License](https://img.shields.io/github/license/imi-tat0r/CS2-CustomVotes) ![Issues](https://img.shields.io/github/issues/imi-tat0r/CS2-CustomVotes) ![Downloads](https://img.shields.io/github/downloads/imi-tat0r/CS2-CustomVotes/total) ![Stars](https://img.shields.io/github/stars/imi-tat0r/CS2-CustomVotes)

# CS2 Custom Votes (1.0.0)
# CS2 Custom Votes (1.0.1)
![image](https://du.hurenso.hn/r/X4jQaq.png) ![image](https://du.hurenso.hn/r/81Imq8.png)

# About
Expand Down Expand Up @@ -58,13 +58,14 @@ Every vote options can trigger multiple commands at once allowing for a wide ran
},
"DefaultOption": "Disable", // default option (must be a key from the "Options" object)
"Style": "chat", // Menu style - "center" or "chat" (will be overridden by the global ForceStyle if not "none")
"MinVotePercentage": -1, // minimum percentage of votes required to pass the vote (-1 behaves like 50%)
"Permission": {
"RequiresAll": false, // if true, all permissions must be present to vote
"Permissions": [] // list of permissions required to start this vote (empty list allows everyone to start the vote)
}
}
],
"ConfigVersion": 1
"ConfigVersion": 2
}
```

Expand All @@ -75,6 +76,8 @@ public interface ICustomVoteApi
{
public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style);
public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style);
public void AddCustomVote(string name, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style, int minVotePercentage);
public void AddCustomVote(string name, List<string> aliases, string description, string defaultOption, float timeToVote, Dictionary<string, VoteOption> options, string style, int minVotePercentage);
public void RemoveCustomVote(string name);
}

Expand All @@ -89,7 +92,8 @@ customVotesApi.AddCustomVote(
{ "Enable", new VoteOption("{Green}Enable", new List<string> { "sv_cheats 1" })},
{ "Disable", new VoteOption("{Red}Disable", new List<string> { "sv_cheats 0" })},
},
"chat"); // Menu style - "center" or "chat"
"chat", // Menu style - "center" or "chat"
-1); // minimum percentage of votes required to pass the vote (-1 behaves like 50%)
customVotesApi.RemoveCustomVote("cheats");
```
Expand Down

0 comments on commit f323571

Please sign in to comment.