-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenuVote.as
497 lines (388 loc) · 12.4 KB
/
MenuVote.as
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//
// Helper class for creating custom votes
//
enum MENU_VOTE_STATES {
MVOTE_NOT_STARTED,
MVOTE_IN_PROGRESS,
MVOTE_FINISHED
}
// reason an option was chosen
enum MENU_VOTE_RESULT_REASONS {
MVOTE_RESULT_NORMAL, // option was chosen because it had the most votes
MVOTE_RESULT_TIED, // option was randomly chosen between tied options
MVOTE_RESULT_NO_VOTES, // option was randomly chosen because no one voted
MVOTE_RESULT_PERCENT_FAIL, // vote failed because no option had the percentage of votes required
}
funcdef void MenuVoteFinishCallback(MenuVote::MenuVote@ voteMenu, MenuOption@ chosenOption, int resultReason);
funcdef void MenuVoteThinkCallback(MenuVote::MenuVote@ voteMenu, int secondsLeft);
funcdef void MenuVoteOptionCallback(MenuVote::MenuVote@ voteMenu, MenuOption@ chosenOption, CBasePlayer@ plr);
class MenuOption {
string label;
string value;
bool isVotable = true;
MenuOption() {}
MenuOption(string label) {
this.label = label;
this.value = label;
}
MenuOption(string label, string value) {
this.label = label;
this.value = value;
}
}
class MenuVoteParams {
string title = "Vote"; // Menu title
array<MenuOption> options = { // Custom options
MenuOption("Yes"),
MenuOption("No")
};
int voteTime = 10; // Time in seconds to display the vote
int percentNeeded = -1; // Percentage of votes needed for an option to be chosen (0-100)
MenuOption percentFailOption = options[1]; // Default option chosen if no option has enough percentage of votes
MenuVoteThinkCallback@ thinkCallback = null;
MenuVoteFinishCallback@ finishCallback = null;
MenuVoteOptionCallback@ optionCallback = null;
}
// Menus need to be defined globally when the plugin is loaded or else paging doesn't work.
// Each player needs their own menu or else paging breaks when someone else opens the menu.
// These also need to be modified directly (not via a local var reference).
array<CTextMenu@> g_menus = {
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null
};
namespace MenuVote
{
CScheduledFunction@ g_menuTimer = null;
MenuVote@ g_activeVote = null;
bool g_hooks_registered = false;
string lotsOfNewlines = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
float g_resultTime = 1.5f;
void voteMenuCallback(CTextMenu@ menu, CBasePlayer@ plr, int page, const CTextMenuItem@ item) {
if (item is null or plr is null or !plr.IsConnected()) {
return;
}
int option = 0;
item.m_pUserData.retrieve(option);
// game crash if menu reopend on the same frame
g_Scheduler.SetTimeout("handleVoteDelay", 0.0f, EHandle(plr), option);
}
void handleVoteDelay(EHandle h_plr, int option) {
if (g_activeVote !is null and h_plr.IsValid()) {
g_activeVote.handleVote(cast<CBasePlayer@>(h_plr.GetEntity()), option);
}
}
void voteThink() {
if (g_activeVote !is null) {
g_activeVote.think();
}
}
void voteCleanup() {
if (g_activeVote !is null) {
g_activeVote.reset();
}
}
HookReturnCode MapChange() {
g_Scheduler.RemoveTimer(g_menuTimer);
return HOOK_CONTINUE;
}
HookReturnCode ClientLeave(CBasePlayer@ plr) {
if (g_activeVote !is null && g_activeVote.status == MVOTE_IN_PROGRESS) {
g_activeVote.handlePlayerLeave(plr);
}
return HOOK_CONTINUE;
}
class MenuVote {
int status = MVOTE_NOT_STARTED;
MenuVoteParams voteParams;
private array<int> playerVotes;
private array<bool> playerWatching; // players who reopened the menu
private int secondsLeft;
private MenuOption selectedOption;
private int blinkTime = 0;
string voteStarterId; // steam id of the player who started the vote
MenuVote() {}
void reset() {
playerVotes.resize(0);
playerVotes.resize(33);
playerWatching.resize(0);
playerWatching.resize(33);
status = MVOTE_NOT_STARTED;
blinkTime = 0;
selectedOption = MenuOption();
g_Scheduler.RemoveTimer(g_menuTimer);
}
void start(MenuVoteParams voteParams, CBasePlayer@ voteStarter) {
this.voteParams = voteParams;
if (!g_hooks_registered) {
g_Hooks.RegisterHook(Hooks::Game::MapChange, @MapChange);
g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect, @ClientLeave);
g_hooks_registered = true;
}
if (g_activeVote !is null) {
g_activeVote.cancel();
}
reset();
status = MVOTE_IN_PROGRESS;
@g_activeVote = @this;
update();
secondsLeft = voteParams.voteTime;
@g_menuTimer = g_Scheduler.SetTimeout("voteThink", 0.0f);
voteStarterId = "";
if (voteStarter !is null) {
voteStarterId = getPlayerUniqueId(voteStarter);
}
for (uint i = 0; i < 33; i++) {
playerWatching[i] = true;
}
}
void handleVote(CBasePlayer@ plr, int option) {
if (status != MVOTE_IN_PROGRESS) {
return;
}
if (voteParams.options[option-1].isVotable) {
if (playerVotes[plr.entindex()] == option) {
playerVotes[plr.entindex()] = 0;
option = 0;
} else {
playerVotes[plr.entindex()] = option;
}
}
if (voteParams.optionCallback !is null) {
if (option > 0) {
voteParams.optionCallback(@this, @voteParams.options[option-1], plr);
} else {
voteParams.optionCallback(@this, null, plr);
}
}
update();
}
void handlePlayerLeave(CBasePlayer@ plr) {
playerVotes[plr.entindex()] = 0;
playerWatching[plr.entindex()] = false;
update();
}
void update() {
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr !is null and plr.IsConnected()) {
update(plr);
}
}
blinkTime++;
}
void update(CBasePlayer@ plr) {
int eidx = plr.entindex();
int bestVotes = getHighestVotecount();
bool anyoneVoted = bestVotes > -1;
bool shouldBlinkSelectedOption = status == MVOTE_FINISHED && (blinkTime % 2 == 0);
bool percentBased = voteParams.percentNeeded >= 0;
int totalVotes = getTotalVotes();
@g_menus[eidx] = CTextMenu(@voteMenuCallback);
g_menus[eidx].SetTitle(lotsOfNewlines + "\\y" + voteParams.title);
for (uint i = 0; i < voteParams.options.length(); i++) {
int thisOption = i+1;
int voteCount = getOptionVotes(thisOption);
string label = voteParams.options[i].label;
string value = voteParams.options[i].value;
bool blinkThisOption = shouldBlinkSelectedOption && selectedOption.value == value && selectedOption.label == label;
int percent = getVotePercent(voteCount);
bool isBestOption = percentBased ? (percent >= voteParams.percentNeeded) : (voteCount == bestVotes);
if (percentBased) {
label = (blinkThisOption ? "\\d" : "\\w") + label;
if (voteCount > 0) {
label += " \\d(" + percent + "%)";
}
}
else if (voteCount > 0 || blinkThisOption) {
if (blinkThisOption) {
label = "\\d" + label;
} else if (isBestOption) {
label = "\\w" + label;
} else {
label = "\\r" + label;
}
label += " \\d(" + voteCount + ")";
} else {
label = (anyoneVoted ? "\\r" : "\\w") + label;
}
if (playerVotes[eidx] == thisOption) {
label += " X";
}
if (i == voteParams.options.length()-1) {
if (status != MVOTE_FINISHED) {
string timeleft = "\n\n" + (secondsLeft+1) + " seconds left";
label += "\\y" + timeleft;
} else {
label += "\n\n";
}
label += lotsOfNewlines; // hide the broken Exit button
}
label += "\\y";
g_menus[eidx].AddItem(label, any(thisOption));
}
g_menus[eidx].Register();
int menuTime = status == MVOTE_FINISHED ? 1 : 2;
if (isPlayerWatching(plr)) {
g_menus[eidx].Open(menuTime, 0, plr);
}
}
bool isPlayerWatching(CBasePlayer@ plr) {
return playerVotes[plr.entindex()] == 0 or playerWatching[plr.entindex()];
}
void reopen(CBasePlayer@ plr) {
if (plr is null) {
return;
}
g_menus[plr.entindex()].Open(0, 0, plr);
playerWatching[plr.entindex()] = true;
}
void closeMenu(CBasePlayer@ plr) {
playerWatching[plr.entindex()] = false;
if (playerVotes[plr.entindex()] == 0) {
playerVotes[plr.entindex()] = -1;
}
}
void cancel() {
@g_menus[0] = CTextMenu(@voteMenuCallback);
g_menus[0].SetTitle("\\yVote cancelled...");
g_menus[0].AddItem(" ", any(""));
g_menus[0].Register();
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr !is null) {
if (isPlayerWatching(plr)) {
g_menus[0].Open(2, 0, plr);
}
}
}
reset();
}
void think() {
if (status == MVOTE_NOT_STARTED) {
return; // cancelled
}
if (status == MVOTE_FINISHED) {
update(); // blink the selected option
if (voteParams.thinkCallback !is null) {
voteParams.thinkCallback(@this, 0);
}
return;
}
if (secondsLeft == 0) {
finishVote();
return;
}
if (voteParams.thinkCallback !is null) {
voteParams.thinkCallback(@this, secondsLeft);
}
secondsLeft--;
update();
@g_menuTimer = g_Scheduler.SetTimeout("voteThink", 1.0f);
}
string getTitle() {
return voteParams.title;
}
string getResultString() {
string result = voteParams.title;
for (uint i = 0; i < voteParams.options.size(); i++) {
if (!voteParams.options[i].isVotable) {
continue;
}
string box = (selectedOption.value == voteParams.options[i].value) ? "[x]" : "[ ]";
int voteCount = getOptionVotes(i+1);
result += " " + box + " " + voteParams.options[i].label;
if (voteCount > 0) {
result += " (" + voteCount + ")";
}
}
return result;
}
int getOptionVotes(int option) {
int voteCount = 0;
for (uint k = 0; k < playerVotes.size(); k++) {
voteCount += (playerVotes[k] == option) ? 1 : 0;
}
return voteCount;
}
int getTotalVotes() {
int voteCount = 0;
for (uint k = 0; k < playerVotes.size(); k++) {
voteCount += (playerVotes[k] > 0) ? 1 : 0;
}
return voteCount;
}
int getVotePercent(int votes) {
int totalVotes = getTotalVotes();
if (totalVotes == 0) {
return 0;
}
return int((float(votes) / float(totalVotes))*100);
}
int getOptionVotePercent(string label) {
for (uint i = 0; i < voteParams.options.length(); i++) {
if (voteParams.options[i].label == label) {
return getVotePercent(getOptionVotes(i+1));
}
}
return -1;
}
int getOptionVotePercentByValue(string value) {
for (uint i = 0; i < voteParams.options.length(); i++) {
if (voteParams.options[i].value == value) {
return getVotePercent(getOptionVotes(i+1));
}
}
return -1;
}
// return number of votes for the map with the most votes (for highlighting/tie-breaking)
int getHighestVotecount() {
int bestVotes = -1;
for (uint i = 0; i < voteParams.options.length(); i++) {
int voteCount = getOptionVotes(i+1);
if (voteCount == 0) {
continue;
}
if (voteCount > bestVotes) {
bestVotes = voteCount;
}
}
return bestVotes;
}
void finishVote() {
array<MenuOption> bestOptions;
int bestVotes = getHighestVotecount();
for (uint i = 0; i < voteParams.options.length(); i++) {
int voteCount = getOptionVotes(i+1);
if (voteParams.options[i].isVotable && voteCount >= bestVotes) {
bestOptions.insertLast(voteParams.options[i]);
}
}
selectedOption = bestOptions[0];
int selectReason = MVOTE_RESULT_NORMAL;
if (bestOptions.size() > 1) {
selectedOption = bestOptions[Math.RandomLong(0, bestOptions.size()-1)];
selectReason = bestVotes == -1 ? MVOTE_RESULT_NO_VOTES : MVOTE_RESULT_TIED;
}
if (voteParams.percentNeeded >= 0) {
if (getVotePercent(bestVotes) < voteParams.percentNeeded) {
selectedOption = voteParams.percentFailOption;
selectReason = MVOTE_RESULT_PERCENT_FAIL;
}
}
// blink the selected option
int b = 0;
for (float d = 0; d < g_resultTime; d += 0.25f) {
g_Scheduler.SetTimeout("voteThink", d);
}
g_Scheduler.SetTimeout("voteCleanup", (g_resultTime+1)*0.25f);
status = MVOTE_FINISHED;
g_PlayerFuncs.ClientPrintAll(HUD_PRINTCENTER, "");
if (voteParams.finishCallback !is null) {
voteParams.finishCallback(@this, selectedOption, selectReason);
}
}
}
}