Skip to content

Commit

Permalink
Use chatmenu for votes, add nominate (#3)
Browse files Browse the repository at this point in the history
* Use chatmenu and add nominate, huge rewrite

* update readme
  • Loading branch information
abnerfs authored Jan 13, 2024
1 parent aadd1a0 commit 2980535
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 243 deletions.
51 changes: 51 additions & 0 deletions AsyncVoteManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

namespace cs2_rockthevote
{
public enum VoteResult {
Added,
AlreadyAddedBefore,
VotesAlreadyReached,
VotesReached
}

public class AsyncVoteManager
{
private List<int> votes = new();
public int VoteCount => votes.Count;
public int RequiredVotes => VoteValidator.RequiredVotes;

public AsyncVoteManager(AsyncVoteValidator voteValidator)
{
VoteValidator = voteValidator;
}

private readonly AsyncVoteValidator VoteValidator;

public bool VotesAlreadyReached { get; set; } = false;

public VoteResult AddVote(int userId)
{
if (VotesAlreadyReached)
return VoteResult.VotesAlreadyReached;

if (votes.IndexOf(userId) != -1)
return VoteResult.AlreadyAddedBefore;

votes.Add(userId);
if(VoteValidator.CheckVotes(votes.Count))
{
VotesAlreadyReached = true;
return VoteResult.VotesReached;
}

return VoteResult.Added;
}

public void RemoveVote(int userId)
{
var index = votes.IndexOf(userId);
if(index > -1)
votes.RemoveAt(index);
}
}
}
22 changes: 22 additions & 0 deletions AsyncVoteValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace cs2_rockthevote
{
public class AsyncVoteValidator
{
private float VotePercentage = 0F;

private readonly ServerManager Server;

public int RequiredVotes { get => (int)Math.Ceiling(Server.ValidPlayerCount * VotePercentage); }

public AsyncVoteValidator(int votePercentage, ServerManager server)
{
VotePercentage = votePercentage / 100F;
Server = server;
}

public bool CheckVotes(int numberOfVotes)
{
return numberOfVotes >= RequiredVotes;
}
}
}
15 changes: 4 additions & 11 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
using CounterStrikeSharp.API.Core;
using System.Text.Json.Serialization;

namespace cs2_rockthevote
{
public class Config : IBasePluginConfig
{
[JsonPropertyName("MinPlayers")]
public int MinPlayers { get; set; } = 0;

[JsonPropertyName("VotePercentage")]
public decimal VotePercentage { get; set; } = 0.6M;

[JsonPropertyName("Language")]
public string Language { get; set; } = "en";

public int Version { get; set; } = 1;
public int Version { get; set; } = 2;
public int RtvVotePercentage { get; set; } = 60;
public int RtvMinPlayers { get; set; } = 0;
public bool DisableVotesInWarmup { get; set; } = false;
}
}
25 changes: 25 additions & 0 deletions NominationManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

namespace cs2_rockthevote
{
public class NominationManager
{
Dictionary<int, string> Nominations = new();

public void Nominate(int userId, string map)
{
Nominations[userId] = map;
}

public List<string> Votes()
{
return Nominations
.Select(x => x.Value)
.Distinct()
.Select(map => new KeyValuePair<string, int>(map, Nominations.Select(x => x.Value == map).Count()))
.OrderByDescending(x => x.Value)
.Select(x => x.Key)
.Take(5)
.ToList();
}
}
}
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# CS2 Rock The Vote
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) the map will end in the end of the round and trigger a next map vote (that you need to configure yourself using CS2 built in nextmap vote system)
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.

# Features
- Reads from a custom maplist
- nominate command

# Limitations
- I haven't tested this with a server with more than 1 player, so this is a WIP version, I will address all feedback and issues that appear.
- This is intended to be used alongside the built in map vote system in CS2 so you need to configure end of map vote in CS2 yourself.
- For now only English and Brazilian Portuguese are supported languages, adding translations require recompiling the plugin for now, this will likely change in the near future, feel fre to open a PR adding a new language, I will be glad to review and recompile the plugin myself.
- Previous version relied on the official CS2 vote system, I pivoted this idea in favor of adding nominate, I will probably create another plugin with the original idea as soon as I figure out how to do the nominate command that way.

# Requirements
[Latest release of Counter Strike Sharp](https://github.com/roflmuffin/CounterStrikeSharp)
Expand All @@ -20,9 +24,16 @@ Players can type rtv to request the map to be changed, once a number of votes is

```json
{
"MinPlayers": 0, // Number of players required to enable the command
"VotePercentage": 0.6, // Percentage of votes required to change the map
"Language": "en", // The language, for now only en and pt are valid values
"Version": 1 // Don't chang this
"Version": 2,
"RtvVotePercentage": 60,
"RtvMinPlayers": 0,
"DisableVotesInWarmup": false
}
```

Maps that will be used in RTV are located in addons/counterstrikesharp/configs/plugins/RockTheVote/maplist.txt

# TODO
- Add minimum rounds to use commands.
- Add votemap.
- Translations support
Loading

0 comments on commit 2980535

Please sign in to comment.