forked from SirPlease/L4D2-Competitive-Rework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltinvotes_test.sp
More file actions
279 lines (219 loc) · 8.14 KB
/
Copy pathbuiltinvotes_test.sp
File metadata and controls
279 lines (219 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <builtinvotes>
DataPack
g_hCallBackPack = null;
Handle
g_hVoteHandler = null;
ConVar
g_hCvarBlockBVote = null,
g_hCvarDisplayBVoteResult = null;
enum
{
eBlockNone = 0,
eBlockOnCreation,
eBlockAtStart
};
enum
{
eVoteFail = 0,
eVotePass
};
stock const char g_sBuiltinVoteCreateError[][] =
{
"eBuiltinVoteErrorNone",
"eBlockedWithForward",
"eInvalidParamBuiltinVoteType"
};
public Plugin myinfo =
{
name = "[L4D2] BuiltinVotes test",
author = "A1m`",
description = "Plugin to test the extension 'builtinvotes'",
version = "2.5",
url = "https://github.com/L4D-Community/builtinvotes"
};
public void OnPluginStart()
{
g_hCvarBlockBVote = CreateConVar("sm_block_builtinvotes", \
"0", \
"Block all votes from extension 'builtinvotes' (used for debugging). 1 - block voting on creation, 2 - block voting at start.", \
_, true, 0.0, true, 2.0 \
);
g_hCvarDisplayBVoteResult = CreateConVar("sm_display_builtinvotes_result", \
"1", \
"Display the result of voting at the end of voting. 0 - not display, 1 - display.", \
_, true, 0.0, true, 1.0 \
);
RegAdminCmd("sm_bv_test", Cmd_BuiltinVotesTest, ADMFLAG_GENERIC);
RegAdminCmd("sm_bv_close_data", Cmd_BuiltinVotesCloseData, ADMFLAG_GENERIC);
RegAdminCmd("sm_getvotecontroller", Cmd_GetGameVoteController, ADMFLAG_GENERIC);
}
Action Cmd_GetGameVoteController(int iClient, int iArgs)
{
int iEntIndex = Game_GetVoteController();
char sEntityName[64];
FormatEx(sEntityName, sizeof(sEntityName), "Unknown entity");
if (iEntIndex > MaxClients && IsValidEntity(iEntIndex)) {
GetEntityClassname(iEntIndex, sEntityName, sizeof(sEntityName));
}
PrintToChat(iClient, "Entity name: %s, entity index: %d!", sEntityName, iEntIndex);
return Plugin_Handled;
}
public Action OnBuiltinVoteCreate(Handle hPlugin, const char[] sPluginName)
{
PrintToChatAll("[OnBuiltinVoteCreate] hPlugin: %x, sPluginName: %s", hPlugin, sPluginName);
if (hPlugin != null) {
char sPlName[32], sPlAuthor[32], sPlDescription[64], sPlVersion[16], sPlUrl[256];
GetPluginInfo(hPlugin, PlInfo_Name, sPlName, sizeof(sPlName));
GetPluginInfo(hPlugin, PlInfo_Author, sPlAuthor, sizeof(sPlAuthor));
GetPluginInfo(hPlugin, PlInfo_Description, sPlDescription, sizeof(sPlDescription));
GetPluginInfo(hPlugin, PlInfo_Version, sPlVersion, sizeof(sPlVersion));
GetPluginInfo(hPlugin, PlInfo_URL, sPlUrl, sizeof(sPlUrl));
PrintToChatAll("[OnBuiltinVoteCreate] Plugin name: %s ", sPlName);
PrintToChatAll("[OnBuiltinVoteCreate] Plugin author: %s ", sPlAuthor);
PrintToChatAll("[OnBuiltinVoteCreate] Plugin description: %s ", sPlDescription);
PrintToChatAll("[OnBuiltinVoteCreate] Plugin version: %s ", sPlVersion);
PrintToChatAll("[OnBuiltinVoteCreate] Plugin url: %s ", sPlUrl);
PrintToChatAll("[OnBuiltinVoteCreate] Plugin status: %d", GetPluginStatus(hPlugin));
}
return (g_hCvarBlockBVote.IntValue == eBlockOnCreation) ? Plugin_Handled : Plugin_Continue;
}
public Action OnBuiltinVoteStart(Handle hVote, Handle hPlugin, const char[] sPluginName)
{
int iInitiator = GetBuiltinVoteInitiator(hVote);
char sInitiatorName[32] = "Unknown";
// Server (0) and clients (1-32)
if (iInitiator >= 0 && iInitiator <= MaxClients && IsClientInGame(iInitiator)) {
GetClientName(iInitiator, sInitiatorName, sizeof(sInitiatorName));
}
PrintToChatAll("[OnBuiltinVoteStart] Initiator: %s (%d), hPlugin: %x, sPluginName: %s", sInitiatorName, iInitiator, hPlugin, sPluginName);
if (hPlugin != null) {
char sPlName[32], sPlAuthor[32], sPlDescription[64], sPlVersion[16], sPlUrl[256];
GetPluginInfo(hPlugin, PlInfo_Name, sPlName, sizeof(sPlName));
GetPluginInfo(hPlugin, PlInfo_Author, sPlAuthor, sizeof(sPlAuthor));
GetPluginInfo(hPlugin, PlInfo_Description, sPlDescription, sizeof(sPlDescription));
GetPluginInfo(hPlugin, PlInfo_Version, sPlVersion, sizeof(sPlVersion));
GetPluginInfo(hPlugin, PlInfo_URL, sPlUrl, sizeof(sPlUrl));
PrintToChatAll("[OnBuiltinVoteStart] Plugin name: %s ", sPlName);
PrintToChatAll("[OnBuiltinVoteStart] Plugin author: %s ", sPlAuthor);
PrintToChatAll("[OnBuiltinVoteStart] Plugin description: %s ", sPlDescription);
PrintToChatAll("[OnBuiltinVoteStart] Plugin version: %s ", sPlVersion);
PrintToChatAll("[OnBuiltinVoteStart] Plugin url: %s ", sPlUrl);
PrintToChatAll("[OnBuiltinVoteStart] Plugin status: %d", GetPluginStatus(hPlugin));
}
return (g_hCvarBlockBVote.IntValue == eBlockAtStart) ? Plugin_Handled : Plugin_Continue;
}
Action Cmd_BuiltinVotesCloseData(int iClient, int iArgs)
{
if (g_hCallBackPack == null) {
PrintToChat(iClient, "User data was not passed to the callback!");
return Plugin_Handled;
}
delete g_hCallBackPack;
g_hCallBackPack = null;
PrintToChat(iClient, "User data was successfully deleted!");
return Plugin_Handled;
}
Action Cmd_BuiltinVotesTest(int iClient, int iArgs)
{
StartBuiltinVote(iClient, (iArgs == 1));
return Plugin_Handled;
}
void StartBuiltinVote(const int iInitiator, bool bPassData = false)
{
if (!IsNewBuiltinVoteAllowed()) {
PrintToChat(iInitiator, "Builtinvote cannot be started now.");
return;
}
int iNumPlayers;
int[] iPlayers = new int[MaxClients];
for (int i = 1; i <= MaxClients; i++) {
if (!IsClientInGame(i) || IsFakeClient(i)) {
continue;
}
iPlayers[iNumPlayers++] = i;
}
eBuiltinVoteCreateError iError;
g_hVoteHandler = CreateBuiltinVoteEx(VoteActionHandler, BuiltinVoteType_Custom_YesNo, BuiltinVoteAction_Cancel | BuiltinVoteAction_VoteEnd | BuiltinVoteAction_End, iError);
if (g_hVoteHandler == null) {
PrintToChatAll("Failed to create vote. Reason %s (%d)!", g_sBuiltinVoteCreateError[iError], iError);
return;
}
char sBuffer[64];
Format(sBuffer, sizeof(sBuffer), "Builtinvote test!");
SetBuiltinVoteArgument(g_hVoteHandler, sBuffer);
SetBuiltinVoteInitiator(g_hVoteHandler, iInitiator);
if (bPassData) {
g_hCallBackPack = new DataPack();
g_hCallBackPack.WriteString("UserData 1");
g_hCallBackPack.WriteString("UserData 2");
g_hCallBackPack.WriteCell(1);
g_hCallBackPack.WriteCell(2);
SetBuiltinVoteResultCallback(g_hVoteHandler, VoteResultHandlerUserData, g_hCallBackPack, BV_DATA_HNDL_CLOSE);
} else {
SetBuiltinVoteResultCallback(g_hVoteHandler, VoteResultHandler);
}
DisplayBuiltinVote(g_hVoteHandler, iPlayers, iNumPlayers, 20);
//FakeClientCommand(iInitiator, "Vote Yes");
}
void VoteActionHandler(Handle hVote, BuiltinVoteAction iAction, int iParam1, int iParam2)
{
switch (iAction) {
case BuiltinVoteAction_End: {
g_hVoteHandler = null;
g_hCallBackPack = null;
delete hVote;
}
case BuiltinVoteAction_Cancel: {
DisplayBVoteResult(hVote, eVoteFail);
}
}
}
void VoteResultHandlerUserData(Handle hVote, int iNumVotes, int iNumClients, \
const int[][] iClientInfo, int iNumItems, const int[][] iItemInfo, DataPack hPack)
{
for (int i = 0; i < iNumItems; i++) {
if (iItemInfo[i][BUILTINVOTEINFO_ITEM_INDEX] != BUILTINVOTES_VOTE_YES) {
continue;
}
if (iItemInfo[i][BUILTINVOTEINFO_ITEM_VOTES] > (iNumClients / 2)) {
hPack.Reset();
char sBuffer1[32], sBuffer2[32];
hPack.ReadString(sBuffer1, sizeof(sBuffer1));
hPack.ReadString(sBuffer2, sizeof(sBuffer2));
int iBuff1 = hPack.ReadCell();
int iBuff2 = hPack.ReadCell();
PrintToChatAll("String1: %s, String2: %s, int1: %d, int2: %d", sBuffer1, sBuffer2, iBuff1, iBuff2);
DisplayBVoteResult(hVote, eVotePass, "Builtinvote test end...");
return;
}
}
DisplayBVoteResult(hVote, eVoteFail);
}
void VoteResultHandler(Handle hVote, int iNumVotes, int iNumClients, \
const int[][] iClientInfo, int iNumItems, const int[][] iItemInfo)
{
for (int i = 0; i < iNumItems; i++) {
if (iItemInfo[i][BUILTINVOTEINFO_ITEM_INDEX] != BUILTINVOTES_VOTE_YES) {
continue;
}
if (iItemInfo[i][BUILTINVOTEINFO_ITEM_VOTES] > (iNumClients / 2)) {
DisplayBVoteResult(hVote, eVotePass, "Builtinvote test end...");
return;
}
}
DisplayBVoteResult(hVote, eVoteFail);
}
void DisplayBVoteResult(Handle hVote, int iResultType, char[] sPassArg = "")
{
if (!g_hCvarDisplayBVoteResult.BoolValue) {
return;
}
if (iResultType == eVotePass) {
DisplayBuiltinVotePass(hVote, sPassArg);
return;
}
DisplayBuiltinVoteFail(hVote, BuiltinVoteFail_Loses);
}