Merge pull request #116 from mxve/dvar-votesRequired

Add sv_votesrequired
This commit is contained in:
Louve 2024-06-16 10:30:58 +02:00 committed by GitHub
commit 3a70a93b41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -2,11 +2,14 @@
#include "ClientCommand.hpp" #include "ClientCommand.hpp"
#include "MapRotation.hpp" #include "MapRotation.hpp"
#include "Vote.hpp" #include "Vote.hpp"
#include "Events.hpp"
using namespace Utils::String; using namespace Utils::String;
namespace Components namespace Components
{ {
Dvar::Var Vote::SV_VotesRequired;
std::unordered_map<std::string, Vote::CommandHandler> Vote::VoteCommands = std::unordered_map<std::string, Vote::CommandHandler> Vote::VoteCommands =
{ {
{"map_restart", HandleMapRestart}, {"map_restart", HandleMapRestart},
@ -37,6 +40,17 @@ namespace Components
Game::SV_SetConfigstring(Game::CS_VOTE_NO, VA("%i", Game::level->voteNo)); Game::SV_SetConfigstring(Game::CS_VOTE_NO, VA("%i", Game::level->voteNo));
} }
int Vote::VotesRequired()
{
auto votesRequired = Vote::SV_VotesRequired.get<int>();
if (votesRequired > 0)
{
return votesRequired;
}
return Game::level->numConnectedClients / 2 + 1;
}
bool Vote::IsInvalidVoteString(const std::string& input) bool Vote::IsInvalidVoteString(const std::string& input)
{ {
static const char* separators[] = { "\n", "\r", ";" }; static const char* separators[] = { "\n", "\r", ";" };
@ -204,7 +218,7 @@ namespace Components
return; return;
} }
if (Game::level->numConnectedClients < 2) if (Game::level->numConnectedClients < VotesRequired())
{ {
Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_VOTINGNOTENOUGHPLAYERS\"", 0x65)); Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_VOTINGNOTENOUGHPLAYERS\"", 0x65));
return; return;
@ -218,7 +232,7 @@ namespace Components
return; return;
} }
if (ent->client->sess.voteCount >= 3) if (ent->client->sess.voteCount >= VotesRequired())
{ {
Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_MAXVOTESCALLED\"", 0x65)); Game::SV_GameSendServerCommand(ent - Game::g_entities, Game::SV_CMD_CAN_IGNORE, VA("%c \"GAME_MAXVOTESCALLED\"", 0x65));
return; return;
@ -321,6 +335,10 @@ namespace Components
// Replicate g_allowVote // Replicate g_allowVote
Utils::Hook::Set<std::uint32_t>(0x5E3A4F, Game::DVAR_INTERNAL | Game::DVAR_CODINFO); Utils::Hook::Set<std::uint32_t>(0x5E3A4F, Game::DVAR_INTERNAL | Game::DVAR_CODINFO);
Events::OnDvarInit([]{
Vote::SV_VotesRequired = Game::Dvar_RegisterInt("sv_votesRequired", 0, 0, 18, Game::DVAR_NONE, "Set the amount of votes required for a vote to pass.\n0 = (players / 2) + 1");
});
ClientCommand::Add("callvote", Cmd_CallVote_f); ClientCommand::Add("callvote", Cmd_CallVote_f);
ClientCommand::Add("vote", Cmd_Vote_f); ClientCommand::Add("vote", Cmd_Vote_f);

View File

@ -11,11 +11,14 @@ namespace Components
using CommandHandler = std::function<bool(const Game::gentity_s* ent, const Command::ServerParams* params)>; using CommandHandler = std::function<bool(const Game::gentity_s* ent, const Command::ServerParams* params)>;
static std::unordered_map<std::string, CommandHandler> VoteCommands; static std::unordered_map<std::string, CommandHandler> VoteCommands;
static Dvar::Var SV_VotesRequired;
static constexpr auto* CallVoteDesc = "%c \"GAME_VOTECOMMANDSARE\x15 map_restart, map_rotate, map <mapname>, g_gametype <typename>, typemap <typename> <mapname>, " static constexpr auto* CallVoteDesc = "%c \"GAME_VOTECOMMANDSARE\x15 map_restart, map_rotate, map <mapname>, g_gametype <typename>, typemap <typename> <mapname>, "
"kick <player>, tempBanUser <player>\""; "kick <player>, tempBanUser <player>\"";
static void DisplayVote(const Game::gentity_s* ent); static void DisplayVote(const Game::gentity_s* ent);
static bool IsInvalidVoteString(const std::string& input); static bool IsInvalidVoteString(const std::string& input);
static int VotesRequired();
static bool HandleMapRestart(const Game::gentity_s* ent, const Command::ServerParams* params); static bool HandleMapRestart(const Game::gentity_s* ent, const Command::ServerParams* params);
static bool HandleMapRotate(const Game::gentity_s* ent, const Command::ServerParams* params); static bool HandleMapRotate(const Game::gentity_s* ent, const Command::ServerParams* params);