Skip to content

Commit

Permalink
Merge pull request #16 from nosoop/multivotes
Browse files Browse the repository at this point in the history
Hotfix for 2022-06-22
  • Loading branch information
sapphonie authored Jun 23, 2022
2 parents deb200c + da64e52 commit 46dd550
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
18 changes: 16 additions & 2 deletions addons/sourcemod/scripting/nativevotes.sp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ int g_ClientVotes[MAXPLAYERS+1];
bool g_bRevoting[MAXPLAYERS+1];
char g_LeaderList[1024];

ConVar sv_vote_holder_may_vote_no;

// Map list stuffs

#define STRINGTABLE_NAME "ServerMapCycle"
Expand Down Expand Up @@ -262,6 +264,8 @@ public void OnPluginStart()

AddCommandListener(Command_Vote, "vote"); // All games, command listeners aren't case sensitive

sv_vote_holder_may_vote_no = FindConVar("sv_vote_holder_may_vote_no");

// The new version of the CallVote system is TF2 only
if (Game_AreVoteCommandsSupported())
{
Expand Down Expand Up @@ -655,14 +659,20 @@ public void OnMapEnd()

public Action Command_Vote(int client, const char[] command, int argc)
{
#if defined LOG
char voteString[128];
GetCmdArgString(voteString, sizeof(voteString));
LogMessage("Client %N ran a vote command: %s", client, voteString);
#endif

// If we're not running a vote, return the vote control back to the server
if (!Internal_IsVoteInProgress() || g_ClientVotes[client] != VOTE_PENDING)
{
return Plugin_Continue;
}

char option[32];
GetCmdArg(1, option, sizeof(option));
char option[64];
GetCmdArgString(option, sizeof(option));

int item = Game_ParseVote(option);

Expand Down Expand Up @@ -1119,6 +1129,10 @@ void EndVoting()
NativeVote vote = g_hCurVote;
Internal_Reset();

#if defined LOG
LogMessage("Voting done");
#endif

/* Send vote info */
OnVoteResults(vote, votes, num_votes, num_items, client_list, num_clients);
OnEnd(vote, MenuEnd_VotingDone);
Expand Down
95 changes: 88 additions & 7 deletions addons/sourcemod/scripting/nativevotes/game.sp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ static ConVar g_Cvar_AutoBalance;

static ConVar g_Cvar_HideDisabledIssues;

/**
* TODO(UPDATE): For now we only support one vote from NativeVotes at a time.
*
* Ideally we peek and poke at the game's `s_nVoteIdx` and increment it accordingly, but we also
* have to store an internal list of active votes.
*/
static int s_nNativeVoteIdx = 0;

bool Game_IsGameSupported(char[] engineName="", int maxlength=0)
{
g_EngineVersion = GetEngineVersion();
Expand Down Expand Up @@ -485,16 +493,24 @@ int Game_ParseVote(const char[] option)
{
int item = NATIVEVOTES_VOTE_INVALID;

#if defined LOG
LogMessage("Parsing vote option %s", option);
#endif

switch(g_EngineVersion)
{
case Engine_Left4Dead, Engine_Left4Dead2:
{
item = L4DL4D2_ParseVote(option);
}

case Engine_CSGO, Engine_TF2:
case Engine_CSGO:
{
item = CSGO_ParseVote(option);
}
case Engine_TF2:
{
item = TF2CSGO_ParseVote(option);
item = TF2_ParseVote(option);
}
}

Expand Down Expand Up @@ -821,16 +837,23 @@ void Game_DisplayCallVoteFail(int client, NativeVotesCallFailType reason, int ti

void Game_ClientSelectedItem(NativeVote vote, int client, int item)
{
#if defined LOG
LogMessage("Client %N selected item %d", client, item);
#endif
switch(g_EngineVersion)
{
case Engine_Left4Dead, Engine_Left4Dead2:
{
L4DL4D2_ClientSelectedItem(client, item);
}

case Engine_CSGO, Engine_TF2:
case Engine_CSGO:
{
CSGO_ClientSelectedItem(vote, client, item);
}
case Engine_TF2:
{
TF2CSGO_ClientSelectedItem(vote, client, item);
TF2_ClientSelectedItem(vote, client, item);
}
/*
case Engine_Left4Dead:
Expand Down Expand Up @@ -2102,7 +2125,7 @@ static bool L4D2_CheckVotePassType(NativeVotesPassType passType)
// TF2 and CSGO functions are still together in case Valve moves TF2 to protobufs.

// NATIVEVOTES_VOTE_INVALID means parse failed
static int TF2CSGO_ParseVote(const char[] option)
static int CSGO_ParseVote(const char[] option)
{
// option1 <-- 7 characters exactly
if (strlen(option) != 7)
Expand All @@ -2113,11 +2136,44 @@ static int TF2CSGO_ParseVote(const char[] option)
return StringToInt(option[6]) - 1;
}

static void TF2CSGO_ClientSelectedItem(NativeVote vote, int client, int item)
// NATIVEVOTES_VOTE_INVALID means parse failed
static int TF2_ParseVote(const char[] option)
{
// the update on 2022-06-22 changed the params passed to the `vote` command
// previously it was a single string "optionN", where N was the option to be selected
// now it's two arguments "X optionN", where X is the vote index being acted on
char arg1[8];
int iArg2 = BreakString(option, arg1, sizeof(arg1));

char argOption[64];
strcopy(argOption, sizeof(argOption), option[iArg2]);

int voteidx = StringToInt(arg1);
// option1 <-- 7 characters exactly
if (strlen(argOption) != 7)
{
return NATIVEVOTES_VOTE_INVALID;
}

return StringToInt(argOption[6]) - 1;
}

static void CSGO_ClientSelectedItem(NativeVote vote, int client, int item)
{
Event castEvent = CreateEvent("vote_cast");

castEvent.SetInt("team", Data_GetTeam(vote));
castEvent.SetInt("entityid", client);
castEvent.SetInt("vote_option", item);
castEvent.Fire();
}

static void TF2_ClientSelectedItem(NativeVote vote, int client, int item)
{
Event castEvent = CreateEvent("vote_cast");

castEvent.SetInt("team", Data_GetTeam(vote));
castEvent.SetInt("voteidx", s_nNativeVoteIdx); // TODO(UPDATE): this was added in 2022-06-22 - figure out what the client voted for
castEvent.SetInt("entityid", client);
castEvent.SetInt("vote_option", item);
castEvent.Fire();
Expand Down Expand Up @@ -2221,6 +2277,15 @@ static void TF2CSGO_DisplayVote(NativeVote vote, int[] clients, int num_clients)
{
SetEntProp(g_VoteController, Prop_Send, "m_nVoteOptionCount", 0, _, i);
}

// TODO(UPDATE): M-M-M-MULTIVOTE
// we need unique vote indices; HUD elements for previous votes aren't cleaned up (?)
// the game implements this as `this->m_nVoteIdx = s_nVoteIdx++`
s_nNativeVoteIdx = GetEntProp(g_VoteController, Prop_Send, "m_nVoteIdx");
#if defined LOG
PrintToServer("Starting vote index: %d (controller: %d)", s_nNativeVoteIdx, GetEntProp(g_VoteController, Prop_Send, "m_nVoteIdx"));
#endif
SetEntProp(g_VoteController, Prop_Send, "m_nVoteIdx", s_nNativeVoteIdx + 1); // TODO(UPDATE)
}

// According to Source SDK 2013, vote_options is only sent for a multiple choice vote.
Expand All @@ -2244,6 +2309,7 @@ static void TF2CSGO_DisplayVote(NativeVote vote, int[] clients, int num_clients)
optionsEvent.SetString(option, display);
}
optionsEvent.SetInt("count", itemCount);
optionsEvent.SetInt("voteidx", s_nNativeVoteIdx); // TODO(UPDATE)
optionsEvent.Fire();
}

Expand All @@ -2254,6 +2320,13 @@ static void TF2CSGO_DisplayVote(NativeVote vote, int[] clients, int num_clients)
SetEntProp(g_VoteController, Prop_Send, "m_nPotentialVotes", num_clients);
}

// required to allow the initiator to vote on their own issue
// ValveSoftware/Source-1-Games#3934
if (sv_vote_holder_may_vote_no && vote.Initiator <= MaxClients)
{
sv_vote_holder_may_vote_no.ReplicateToClient(vote.Initiator, "1");
}

MenuAction actions = Data_GetActions(vote);

for (int i = 0; i < num_clients; ++i)
Expand Down Expand Up @@ -2297,7 +2370,8 @@ static void TF2CSGO_DisplayVote(NativeVote vote, int[] clients, int num_clients)
else
{
BfWrite bfStart = UserMessageToBfWrite(voteStart);
bfStart.WriteByte(team);
bfStart.WriteByte(team);
bfStart.WriteNum(s_nNativeVoteIdx);
bfStart.WriteByte(Data_GetInitiator(vote));
bfStart.WriteString(translation);
if (bCustom && changeTitle == Plugin_Changed)
Expand All @@ -2310,6 +2384,7 @@ static void TF2CSGO_DisplayVote(NativeVote vote, int[] clients, int num_clients)
}
bfStart.WriteBool(bYesNo);
}

EndMessage();
}

Expand Down Expand Up @@ -2367,6 +2442,7 @@ static void TF2CSGO_SendOptionsToClient(NativeVote vote, int client)
optionsEvent.SetString(option, display);
}
optionsEvent.SetInt("count", itemCount);
optionsEvent.SetInt("voteidx", s_nNativeVoteIdx); // TODO(UPDATE)
optionsEvent.FireToClient(client);
// FireToClient does not close the handle, so we call Cancel() to do that for us.
optionsEvent.Cancel();
Expand Down Expand Up @@ -2407,6 +2483,7 @@ static void TF2_VotePass(const char[] translation, const char[] details, int tea
}

votePass.WriteByte(team);
votePass.WriteNum(s_nNativeVoteIdx);
votePass.WriteString(translation);
votePass.WriteString(details);

Expand All @@ -2428,6 +2505,7 @@ static void TF2_VoteFail(int[] clients, int numClients, int reason, int team)
BfWrite voteFailed = UserMessageToBfWrite(StartMessage("VoteFailed", clients, numClients, USERMSG_RELIABLE));

voteFailed.WriteByte(team);
voteFailed.WriteNum(s_nNativeVoteIdx); // TODO(UPDATE)
voteFailed.WriteByte(reason);

EndMessage();
Expand Down Expand Up @@ -2508,7 +2586,10 @@ static void TF2CSGO_ResetVote()
if (CheckVoteController())
{
if (g_EngineVersion == Engine_TF2)
{
SetEntProp(g_VoteController, Prop_Send, "m_iActiveIssueIndex", INVALID_ISSUE);
SetEntProp(g_VoteController, Prop_Send, "m_nVoteIdx", -1); // TODO(UPDATE)
}

for (int i = 0; i < 5; i++)
{
Expand Down

0 comments on commit 46dd550

Please sign in to comment.