forked from pokepark/PokemonRaidBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_vote.php
576 lines (508 loc) · 25.4 KB
/
keys_vote.php
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
/**
* Keys vote.
* @param $raid
* @return array
*/
function keys_vote($raid)
{
global $config;
// Init keys_time array.
$keys_time = [];
// Get current UTC time and raid UTC times.
$now = utcnow();
$end_time = $raid['end_time'];
$start_time = $raid['start_time'];
// Write to log.
debug_log($now, 'UTC NOW:');
debug_log($end_time, 'UTC END:');
debug_log($start_time, 'UTC START:');
// Raid ended already.
if ($end_time < $now) {
if($config->RAID_ENDED_HIDE_KEYS) {
$keys = [];
}else {
$keys = [
[
[
'text' => getPublicTranslation('raid_done'),
'callback_data' => $raid['id'] . ':vote_refresh:1'
]
]
];
}
// Raid is still running.
} else {
// Get current pokemon
$raid_pokemon_id = $raid['pokemon'];
$raid_pokemon_form_id = $raid['pokemon_form'];
$raid_pokemon = $raid_pokemon_id . "-" . $raid_pokemon_form_id;
// Get raid level
$raid_level = $raid['level'];
// Hide buttons for raid levels and pokemon
$hide_buttons_raid_level = explode(',', $config->RAID_POLL_HIDE_BUTTONS_RAID_LEVEL);
$hide_buttons_pokemon = explode(',', $config->RAID_POLL_HIDE_BUTTONS_POKEMON);
// Show buttons to users?
if(in_array($raid_level, $hide_buttons_raid_level) || in_array(($raid_pokemon_id . "-" . get_pokemon_form_name($raid_pokemon_id,$raid_pokemon_form_id)), $hide_buttons_pokemon) || in_array($raid_pokemon_id, $hide_buttons_pokemon)) {
$keys = [];
} else {
// Extra Keys
$buttons_alone = [
'text' => EMOJI_SINGLE,
'callback_data' => $raid['id'] . ':vote_extra:0'
];
$buttons_extra = [
'text' => '+ ' . EMOJI_IN_PERSON,
'callback_data' => $raid['id'] . ':vote_extra:in_person'
];
$buttons_extra_alien = [
'text' => '+ ' . EMOJI_ALIEN,
'callback_data' => $raid['id'] . ':vote_extra:alien'
];
// Can invite key
$buttons_can_inv = [
'text' => EMOJI_CAN_INVITE,
'callback_data' => $raid['id'] . ':vote_can_invite:0'
];
// Remote Raid Pass key
$buttons_remote = [
'text' => EMOJI_REMOTE,
'callback_data' => $raid['id'] . ':vote_remote:0'
];
// Want invite key
$buttons_inv_plz = [
'text' => EMOJI_WANT_INVITE,
'callback_data' => $raid['id'] . ':vote_want_invite:0'
];
// Team and level keys.
$buttons_teamlvl = [
[
[
'text' => 'Team',
'callback_data' => $raid['id'] . ':vote_team:0'
],
[
'text' => 'Lvl +',
'callback_data' => $raid['id'] . ':vote_level:up'
],
[
'text' => 'Lvl -',
'callback_data' => $raid['id'] . ':vote_level:down'
]
]
];
// Ex-Raid Invite key
if ($raid['event'] == EVENT_ID_EX) {
$buttons_ex_inv = [
'text' => EMOJI_INVITE,
'callback_data' => $raid['id'] . ':vote_invite:0'
];
}else {
$buttons_ex_inv = [];
}
// Show icon, icon + text or just text.
// Icon.
if($config->RAID_VOTE_ICONS && !$config->RAID_VOTE_TEXT) {
$text_here = EMOJI_HERE;
$text_late = EMOJI_LATE;
$text_done = TEAM_DONE;
$text_cancel = TEAM_CANCEL;
// Icon + text.
} else if($config->RAID_VOTE_ICONS && $config->RAID_VOTE_TEXT) {
$text_here = EMOJI_HERE . getPublicTranslation('here');
$text_late = EMOJI_LATE . getPublicTranslation('late');
$text_done = TEAM_DONE . getPublicTranslation('done');
$text_cancel = TEAM_CANCEL . getPublicTranslation('cancellation');
// Text.
} else {
$text_here = getPublicTranslation('here');
$text_late = getPublicTranslation('late');
$text_done = getPublicTranslation('done');
$text_cancel = getPublicTranslation('cancellation');
}
// Status keys.
$buttons_alarm = [
'text' => EMOJI_ALARM,
'callback_data' => $raid['id'] . ':vote_status:alarm'
];
$buttons_here = [
'text' => $text_here,
'callback_data' => $raid['id'] . ':vote_status:arrived'
];
$buttons_late = [
'text' => $text_late,
'callback_data' => $raid['id'] . ':vote_status:late'
];
$buttons_done = [
'text' => $text_done,
'callback_data' => $raid['id'] . ':vote_status:raid_done'
];
$buttons_cancel = [
'text' => $text_cancel,
'callback_data' => $raid['id'] . ':vote_status:cancel'
];
if(!$config->AUTO_REFRESH_POLLS) {
$buttons_refresh = [
'text' => EMOJI_REFRESH,
'callback_data' => $raid['id'] . ':vote_refresh:0'
];
}else {
$buttons_refresh = [];
}
if($raid['event_vote_key_mode'] == 1) {
$keys_time = [
[
'text' => getPublicTranslation("Participate"),
'callback_data' => $raid['id'] . ':vote_time:0'
]
];
}else {
if($raid['event_time_slots'] > 0) {
$RAID_SLOTS = $raid['event_time_slots'];
}else {
$RAID_SLOTS = $config->RAID_SLOTS;
}
// Get current time.
$now_helper = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$now_helper = $now_helper->format('Y-m-d H:i') . ':00';
$dt_now = new DateTimeImmutable($now_helper, new DateTimeZone('UTC'));
// Get direct start slot
$direct_slot = new DateTimeImmutable($start_time, new DateTimeZone('UTC'));
// Get first raidslot rounded up to the next 5 minutes
// Get minute and convert modulo raidslot
$five_slot = new DateTimeImmutable($start_time, new DateTimeZone('UTC'));
$minute = $five_slot->format("i");
$minute = $minute % 5;
// Count minutes to next 5 multiple minutes if necessary
if($minute != 0)
{
// Count difference
$diff = 5 - $minute;
// Add difference
$five_slot = $five_slot->add(new DateInterval("PT".$diff."M"));
}
// Add $config->RAID_FIRST_START minutes to five minutes slot
//$five_plus_slot = new DateTime($five_slot, new DateTimeZone('UTC'));
$five_plus_slot = $five_slot;
$five_plus_slot = $five_plus_slot->add(new DateInterval("PT".$config->RAID_FIRST_START."M"));
// Get first regular raidslot
// Get minute and convert modulo raidslot
$first_slot = new DateTimeImmutable($start_time, new DateTimeZone('UTC'));
$minute = $first_slot->format("i");
$minute = $minute % $config->RAID_SLOTS;
// Count minutes to next raidslot multiple minutes if necessary
if($minute != 0)
{
// Count difference
$diff = $config->RAID_SLOTS - $minute;
// Add difference
$first_slot = $first_slot->add(new DateInterval("PT".$diff."M"));
}
// Compare times slots to add them to keys.
// Example Scenarios:
// Raid 1: Start = 17:45, $config->RAID_FIRST_START = 10, $config->RAID_SLOTS = 15
// Raid 2: Start = 17:36, $config->RAID_FIRST_START = 10, $config->RAID_SLOTS = 15
// Raid 3: Start = 17:35, $config->RAID_FIRST_START = 10, $config->RAID_SLOTS = 15
// Raid 4: Start = 17:31, $config->RAID_FIRST_START = 10, $config->RAID_SLOTS = 15
// Raid 5: Start = 17:40, $config->RAID_FIRST_START = 10, $config->RAID_SLOTS = 15
// Raid 6: Start = 17:32, $config->RAID_FIRST_START = 5, $config->RAID_SLOTS = 5
// Write slots to log.
debug_log($direct_slot, 'Direct start slot:');
debug_log($five_slot, 'Next 5 Minute slot:');
debug_log($first_slot, 'First regular slot:');
// Add first slot only, as all slot times are identical
if($direct_slot == $five_slot && $direct_slot == $first_slot) {
// Raid 1: 17:45 (17:45 == 17:45 && 17:45 == 17:45)
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add either five and first slot or only first slot based on RAID_FIRST_START
} else if($direct_slot == $five_slot && $five_slot < $first_slot) {
// Raid 3: 17:35 == 17:35 && 17:35 < 17:45
// Raid 5: 17:40 == 17:40 && 17:40 < 17:45
// Add next five minutes slot and first regular slot
if($five_plus_slot <= $first_slot) {
// Raid 3: 17:35, 17:45 (17:35 + 10min <= 17:45)
// Add five minutes slot
if($five_slot >= $dt_now) {
$slot = $five_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add only first regular slot
} else {
// Raid 5: 17:45
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// Add direct slot and first slot
} else if($direct_slot < $five_slot && $five_slot == $first_slot) {
// Raid 6: 17:32 < 17:35 && 17:35 == 17:35
// Some kind of special case for a low value of RAID_SLOTS
// Add direct slot?
if($config->RAID_DIRECT_START) {
if($direct_slot >= $dt_now) {
$slot = $direct_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add either all 3 slots (direct slot, five minutes slot and first regular slot) or
// 2 slots (direct slot and first slot) as $config->RAID_FIRST_START does not allow the five minutes slot to be added
} else if($direct_slot < $five_slot && $five_slot < $first_slot) {
// Raid 2: 17:36 < 17:40 && 17:40 < 17:45
// Raid 4: 17:31 < 17:35 && 17:35 < 17:45
// Add all 3 slots
if($five_plus_slot <= $first_slot) {
// Raid 4: 17:31, 17:35, 17:45
// Add direct slot?
if($config->RAID_DIRECT_START) {
if($direct_slot >= $dt_now) {
$slot = $direct_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// Add five minutes slot
if($five_slot >= $dt_now) {
$slot = $five_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
// Add direct slot and first regular slot
} else {
// Raid 2: 17:36, 17:45
// Add direct slot?
if($config->RAID_DIRECT_START) {
if($direct_slot >= $dt_now) {
$slot = $direct_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// Add first slot
if($first_slot >= $dt_now) {
$slot = $first_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// We missed all possible cases or forgot to include them in future else-if-clauses :D
// Try to add at least the direct slot.
} else {
// Add direct slot?
if($config->RAID_DIRECT_START) {
if($first_slot >= $dt_now) {
$slot = $direct_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
}
// Init last slot time.
$last_slot = new DateTimeImmutable($start_time, new DateTimeZone('UTC'));
// Get regular slots
// Start with second slot as first slot is already added to keys.
$second_slot = $first_slot->add(new DateInterval("PT".$RAID_SLOTS."M"));
$dt_end = new DateTimeImmutable($end_time, new DateTimeZone('UTC'));
$regular_slots = new DatePeriod($second_slot, new DateInterval('PT'.$RAID_SLOTS.'M'), $dt_end);
// Add regular slots.
foreach($regular_slots as $slot){
$slot_end = $slot->add(new DateInterval('PT'.$config->RAID_LAST_START.'M'));
// Slot + $config->RAID_LAST_START before end_time?
if($slot_end < $dt_end) {
debug_log($slot, 'Regular slot:');
// Add regular slot.
if($slot >= $dt_now) {
$slot = $slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
// Set last slot for later.
$last_slot = new DateTimeImmutable($slot, new DateTimeZone('UTC'));
} else {
// Set last slot for later.
$slot = $slot->format('Y-m-d H:i:s');
$last_slot = new DateTimeImmutable($slot, new DateTimeZone('UTC'));
}
}
}
// Add raid last start slot
// Set end_time to last extra slot, subtract $config->RAID_LAST_START minutes and round down to earlier 5 minutes.
$last_extra_slot = $dt_end;
$last_extra_slot = $last_extra_slot->sub(new DateInterval('PT'.$config->RAID_LAST_START.'M'));
$s = 5 * 60;
$last_extra_slot = $last_extra_slot->setTimestamp($s * floor($last_extra_slot->getTimestamp() / $s));
//$time_to_last_slot = $last_extra_slot->diff($last_slot)->format("%a");
// Last extra slot not conflicting with last slot and time to last regular slot larger than RAID_LAST_START?
//if($last_extra_slot > $last_slot && $time_to_last_slot > $config->RAID_LAST_START)
// Log last and last extra slot.
debug_log($last_slot, 'Last slot:');
debug_log($last_extra_slot, 'Last extra slot:');
// Last extra slot not conflicting with last slot
if($last_extra_slot > $last_slot) {
// Add last extra slot
if($last_extra_slot >= $dt_now) {
$slot = $last_extra_slot->format('Y-m-d H:i:s');
$keys_time[] = array(
'text' => dt2time($slot),
'callback_data' => $raid['id'] . ':vote_time:' . utctime($slot, 'YmdHis')
);
}
}
// Attend raid at any time
if($config->RAID_ANYTIME)
{
$keys_time[] = array(
'text' => getPublicTranslation('anytime'),
'callback_data' => $raid['id'] . ':vote_time:0'
);
}
}
// Add time keys.
$buttons_time = inline_key_array($keys_time, 4);
// Hidden participants?
if($config->RAID_POLL_HIDE_USERS_TIME > 0) {
if($config->RAID_ANYTIME) {
$hide_users_sql = "AND (attend_time > (UTC_TIMESTAMP() - INTERVAL " . $config->RAID_POLL_HIDE_USERS_TIME . " MINUTE) OR attend_time = '".ANYTIME."')";
} else {
$hide_users_sql = "AND attend_time > (UTC_TIMESTAMP() - INTERVAL " . $config->RAID_POLL_HIDE_USERS_TIME . " MINUTE)";
}
} else {
$hide_users_sql = "";
}
// Get participants
$rs = my_query(
"
SELECT count(attend_time) AS count,
sum(pokemon = '0') AS count_any_pokemon,
sum(pokemon = '{$raid_pokemon}') AS count_raid_pokemon
FROM attendance
WHERE raid_id = {$raid['id']}
$hide_users_sql
AND attend_time IS NOT NULL
AND raid_done != 1
AND cancel != 1
"
);
$row = $rs->fetch();
// Count participants and participants by pokemon
$count_pp = $row['count'];
$count_any_pokemon = $row['count_any_pokemon'];
$count_raid_pokemon = $row['count_raid_pokemon'];
// Write to log.
debug_log('Participants for raid with ID ' . $raid['id'] . ': ' . $count_pp);
debug_log('Participants who voted for any pokemon: ' . $count_any_pokemon);
debug_log('Participants who voted for ' . $raid_pokemon . ': ' . $count_raid_pokemon);
// Zero Participants? Show only time buttons!
if($count_pp == 0) {
$keys = $buttons_time;
} else {
// Init keys pokemon array.
$buttons_pokemon = [];
// Show pokemon keys only if the raid boss is an egg
if(in_array($raid_pokemon_id, $GLOBALS['eggs'])) {
// Get pokemon from database
$raid_spawn = dt2time($raid['spawn'], 'Y-m-d H:i'); // Convert utc spawntime to local time
$raid_bosses = get_raid_bosses($raid_spawn, $raid_level);
// Get eggs.
$eggs = $GLOBALS['eggs'];
if(count($raid_bosses) > 2) {
// Add key for each raid level
foreach($raid_bosses as $pokemon) {
if(in_array($pokemon['pokedex_id'], $eggs)) continue;
$buttons_pokemon[] = array(
'text' => get_local_pokemon_name($pokemon['pokedex_id'], $pokemon['pokemon_form_id'], true),
'callback_data' => $raid['id'] . ':vote_pokemon:' . $pokemon['pokedex_id'] . '-' . $pokemon['pokemon_form_id']
);
}
// Add button if raid boss does not matter
$buttons_pokemon[] = array(
'text' => getPublicTranslation('any_pokemon'),
'callback_data' => $raid['id'] . ':vote_pokemon:0'
);
// Finally add pokemon to keys
$buttons_pokemon = inline_key_array($buttons_pokemon, 2);
}
}
// Init keys array
$keys = [];
if($raid['event_poll_template'] != null) $template = json_decode($raid['event_poll_template']);
else $template = $config->RAID_POLL_UI_TEMPLATE;
$r=0;
foreach($template as $row) {
foreach($row as $key) {
$v_name = 'buttons_'.$key;
if($key == 'teamlvl' or $key == 'pokemon' or $key == 'time') {
// Some button variables are "blocks" of keys, process them here
if(empty(${$v_name})) continue;
foreach(${$v_name} as $teamlvl) {
if(!isset($keys[$r])) $keys[$r] = [];
$keys[$r] = array_merge($keys[$r],$teamlvl);
$r++;
}
$r--;
}else {
if(empty(${$v_name})) continue;
$keys[$r][] = ${$v_name};
}
}
if(!empty($keys[$r][0])) $r++;
}
}
}
}
// Return the keys.
return $keys;
}
?>