This repository was archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathLevel.php
More file actions
1437 lines (1299 loc) · 39.4 KB
/
Level.php
File metadata and controls
1437 lines (1299 loc) · 39.4 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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?hh // strict
class Level extends Model implements Importable, Exportable {
protected static string $MC_KEY = 'level:';
protected static Map<string, string>
$MC_KEYS = Map {
'LEVEL_BY_COUNTRY' => 'level_by_country',
'ALL_LEVELS' => 'all_levels',
'ALL_ACTIVE_LEVELS' => 'active_levels',
'ALL_LEVELS_COUNTRY_MAP' => 'all_levels_country_map',
};
private function __construct(
private int $id,
private int $active,
private string $type,
private string $title,
private string $description,
private int $entity_id,
private int $category_id,
private int $points,
private int $bonus,
private int $bonus_dec,
private int $bonus_fix,
private string $flag,
private string $hint,
private int $penalty,
private string $created_ts,
) {}
public function getId(): int {
return $this->id;
}
public function getActive(): bool {
return $this->active === 1;
}
public function getType(): string {
return $this->type;
}
public function getTitle(): string {
return mb_convert_encoding($this->title, 'UTF-8');
}
public function getDescription(): string {
return mb_convert_encoding($this->description, 'UTF-8');
}
public function getEntityId(): int {
return $this->entity_id;
}
public function getCategoryId(): int {
return $this->category_id;
}
public function getPoints(): int {
return $this->points;
}
public function getBonus(): int {
return $this->bonus;
}
public function getBonusDec(): int {
return $this->bonus_dec;
}
public function getBonusFix(): int {
return $this->bonus_fix;
}
public function getFlag(): string {
return $this->flag;
}
public function getHint(): string {
return $this->hint;
}
public function getPenalty(): int {
return $this->penalty;
}
public function getCreatedTs(): string {
return $this->created_ts;
}
private static function levelFromRow(Map<string, string> $row): Level {
return new Level(
intval(must_have_idx($row, 'id')),
intval(must_have_idx($row, 'active')),
must_have_idx($row, 'type'),
must_have_idx($row, 'title'),
must_have_idx($row, 'description'),
intval(must_have_idx($row, 'entity_id')),
intval(must_have_idx($row, 'category_id')),
intval(must_have_idx($row, 'points')),
intval(must_have_idx($row, 'bonus')),
intval(must_have_idx($row, 'bonus_dec')),
intval(must_have_idx($row, 'bonus_fix')),
must_have_idx($row, 'flag'),
must_have_idx($row, 'hint'),
intval(must_have_idx($row, 'penalty')),
must_have_idx($row, 'created_ts'),
);
}
// Retrieve the level that is using one country
public static async function genWhoUses(
int $country_id,
bool $refresh = false,
): Awaitable<?Level> {
$mc_result = self::getMCRecords('LEVEL_BY_COUNTRY');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$level_by_country = Map {};
$result = await $db->queryf('SELECT * FROM levels WHERE active = 1');
foreach ($result->mapRows() as $row) {
$level_by_country->add(
Pair {intval($row->get('entity_id')), self::levelFromRow($row)},
);
}
self::setMCRecords('LEVEL_BY_COUNTRY', $level_by_country);
if ($level_by_country->contains($country_id)) {
return $level_by_country->get($country_id);
} else {
return null;
}
} else {
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
if ($mc_result->contains($country_id)) {
return $mc_result->get($country_id);
} else {
return null;
}
}
}
// Import levels.
public static async function importAll(
array<string, array<string, mixed>> $elements,
): Awaitable<bool> {
foreach ($elements as $level) {
$title = must_have_string($level, 'title');
$type = must_have_string($level, 'type');
$entity_iso_code = must_have_string($level, 'entity_iso_code') or await Country::genRandomAvailableCountry()->getIsoCode();
$c = must_have_string($level, 'category');
$exist = await self::genAlreadyExist($type, $title, $entity_iso_code);
list($entity_exist, $category_exist) = await \HH\Asio\va(
Country::genCheckExists($entity_iso_code),
Category::genCheckExists($c),
); // TODO: Combine Awaits
if (!$exist && $entity_exist && $category_exist) {
list($entity, $category) = await \HH\Asio\va(
Country::genCountry($entity_iso_code),
Category::genSingleCategoryByName($c),
); // TODO: Combine Awaits
$level_id = await self::genCreate(
$type,
$title,
must_have_string($level, 'description'),
$entity->getId(),
$category->getId(),
must_have_int($level, 'points'),
must_have_int($level, 'bonus'),
must_have_int($level, 'bonus_dec'),
must_have_int($level, 'bonus_fix'),
must_have_string($level, 'flag'),
must_have_string($level, 'hint'),
must_have_int($level, 'penalty'),
);
if (array_key_exists('links', $level)) {
$links = must_have_idx($level, 'links');
invariant(is_array($links), 'links must be of type array');
foreach ($links as $link) {
await Link::genCreate($link, $level_id); // TODO: Combine Awaits
}
}
if (array_key_exists('attachments', $level)) {
$attachments = must_have_idx($level, 'attachments');
invariant(
is_array($attachments),
'attachments must be of type array',
);
foreach ($attachments as $attachment) {
await Attachment::genImportAttachments(
$level_id,
$attachment['filename'],
$attachment['type'],
); // TODO: Combine Awaits
}
}
}
}
return true;
}
// Export levels.
public static async function exportAll(
): Awaitable<array<string, array<string, mixed>>> {
$all_levels_data = array();
$all_levels = await self::genAllLevels();
foreach ($all_levels as $level) {
list($entity, $category, $links, $attachments) = await \HH\Asio\va(
Country::gen($level->getEntityId()),
Category::genSingleCategory($level->getCategoryId()),
Link::genAllLinks($level->getId()),
Attachment::genAllAttachments($level->getId()),
); // TODO: Combine Awaits
$link_array = array();
foreach ($links as $link) {
$link_array[] = $link->getLink();
}
$attachment_array = array();
foreach ($attachments as $attachment) {
$attachment_array[] = [
'filename' => $attachment->getFilename(),
'type' => $attachment->getType(),
];
}
$one_level = array(
'type' => $level->getType(),
'title' => $level->getTitle(),
'active' => $level->getActive(),
'description' => $level->getDescription(),
'entity_iso_code' => $entity->getIsoCode(),
'category' => $category->getCategory(),
'points' => $level->getPoints(),
'bonus' => $level->getBonus(),
'bonus_dec' => $level->getBonusDec(),
'bonus_fix' => $level->getBonusFix(),
'flag' => $level->getFlag(),
'hint' => $level->getHint(),
'penalty' => $level->getPenalty(),
'links' => $link_array,
'attachments' => $attachment_array,
);
array_push($all_levels_data, $one_level);
}
return array('levels' => $all_levels_data);
}
// Check to see if the level is active.
public static async function genCheckStatus(
int $level_id,
bool $refresh = false,
): Awaitable<bool> {
$mc_result = self::getMCRecords('ALL_ACTIVE_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$active_levels = Map {};
$result = await $db->queryf(
'SELECT * FROM levels WHERE active = 1 ORDER BY id',
);
foreach ($result->mapRows() as $row) {
$active_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_ACTIVE_LEVELS', $active_levels);
if ($active_levels->contains($level_id)) {
return true;
} else {
return false;
}
} else {
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
if ($mc_result->contains($level_id)) {
return true;
} else {
return false;
}
}
}
// Check to see if the level is a base.
public static async function genCheckBase(
int $level_id,
bool $refresh = false,
): Awaitable<bool> {
$mc_result = self::getMCRecords('ALL_ACTIVE_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$active_levels = Map {};
$result = await $db->queryf(
'SELECT * FROM levels WHERE active = 1 ORDER BY id',
);
foreach ($result->mapRows() as $row) {
$active_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_ACTIVE_LEVELS', $active_levels);
if ($active_levels->contains($level_id)) {
$level = $active_levels->get($level_id);
invariant($level instanceof Level, 'level should be type of Level');
if ($level->type == 'base') {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
if ($mc_result->contains($level_id)) {
$level = $mc_result->get($level_id);
invariant($level instanceof Level, 'level should be type of Level');
if ($level->type === 'base') {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
// Create a team and return the created level id.
public static async function genCreate(
string $type,
string $title,
string $description,
int $entity_id,
int $category_id,
int $points,
int $bonus,
int $bonus_dec,
int $bonus_fix,
string $flag,
string $hint,
int $penalty,
): Awaitable<int> {
$db = await self::genDb();
if ($entity_id === 0) {
$ent_id = await Country::genRandomAvailableCountryId();
} else {
$ent_id = $entity_id;
}
await $db->queryf(
'INSERT INTO levels '.
'(type, title, description, entity_id, category_id, points, bonus, bonus_dec, bonus_fix, flag, hint, penalty, active, created_ts) '.
'VALUES (%s, %s, %s, %d, %d, %d, %d, %d, %d, %s, %s, %d, %d, NOW())',
$type,
$title,
$description,
$ent_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus_fix,
$flag,
$hint,
$penalty,
0, // active
);
// Mark entity as used
await Country::genSetUsed($ent_id, true);
// Return the newly created level_id
$result =
await $db->queryf(
'SELECT id FROM levels WHERE title = %s AND description = %s AND entity_id = %d AND flag = %s AND category_id = %d LIMIT 1',
$title,
$description,
$ent_id,
$flag,
$category_id,
);
self::invalidateMCRecords();
invariant($result->numRows() === 1, 'Expected exactly one result');
$country_id = await self::genCountryIdForLevel(
intval(must_have_idx($result->mapRows()[0], 'id')),
);
$country = await Country::gen($country_id);
await \HH\Asio\va(
Announcement::genCreateAuto($country->getName()." added!"),
ActivityLog::genAdminLog("added", "Country", $country_id),
);
ActivityLog::invalidateMCRecords('ALL_ACTIVITY');
return intval(must_have_idx($result->mapRows()[0], 'id'));
}
// Create a flag level.
public static async function genCreateFlag(
string $title,
string $description,
string $flag,
int $entity_id,
int $category_id,
int $points,
int $bonus,
int $bonus_dec,
string $hint,
int $penalty,
): Awaitable<int> {
return await self::genCreate(
'flag',
$title,
$description,
$entity_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus,
$flag,
$hint,
$penalty,
);
}
// Update a flag level.
public static async function genUpdateFlag(
string $title,
string $description,
string $flag,
int $entity_id,
int $category_id,
int $points,
int $bonus,
int $bonus_dec,
string $hint,
int $penalty,
int $level_id,
): Awaitable<void> {
await self::genUpdate(
$title,
$description,
$entity_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus,
$flag,
$hint,
$penalty,
$level_id,
);
}
// Create a quiz level.
public static async function genCreateQuiz(
string $title,
string $question,
string $answer,
int $entity_id,
int $points,
int $bonus,
int $bonus_dec,
string $hint,
int $penalty,
): Awaitable<int> {
$db = await self::genDb();
$result = await $db->queryf(
'SELECT id FROM categories WHERE category = %s LIMIT 1',
'Quiz',
);
$category_id = intval(must_have_idx($result->mapRows()[0], 'id'));
return await self::genCreate(
'quiz',
$title,
$question,
$entity_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus,
$answer,
$hint,
$penalty,
);
}
// Update a quiz level.
public static async function genUpdateQuiz(
string $title,
string $question,
string $answer,
int $entity_id,
int $points,
int $bonus,
int $bonus_dec,
string $hint,
int $penalty,
int $level_id,
): Awaitable<void> {
$db = await self::genDb();
$result = await $db->queryf(
'SELECT id FROM categories WHERE category = %s LIMIT 1',
'Quiz',
);
$category_id = intval(must_have_idx($result->mapRows()[0], 'id'));
await self::genUpdate(
$title,
$question,
$entity_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus,
$answer,
$hint,
$penalty,
$level_id,
);
}
// Create a base level.
public static async function genCreateBase(
string $title,
string $description,
int $entity_id,
int $category_id,
int $points,
int $bonus,
string $hint,
int $penalty,
): Awaitable<int> {
return await self::genCreate(
'base',
$title,
$description,
$entity_id,
$category_id,
$points,
$bonus,
0,
$bonus,
'',
$hint,
$penalty,
);
}
// Update a base level.
public static async function genUpdateBase(
string $title,
string $description,
int $entity_id,
int $category_id,
int $points,
int $bonus,
string $hint,
int $penalty,
int $level_id,
): Awaitable<void> {
await self::genUpdate(
$title,
$description,
$entity_id,
$category_id,
$points,
$bonus,
0,
$bonus,
'',
$hint,
$penalty,
$level_id,
);
}
// Update level.
public static async function genUpdate(
string $title,
string $description,
int $entity_id,
int $category_id,
int $points,
int $bonus,
int $bonus_dec,
int $bonus_fix,
string $flag,
string $hint,
int $penalty,
int $level_id,
): Awaitable<void> {
$db = await self::genDb();
if ($entity_id === 0) {
$ent_id = await Country::genRandomAvailableCountryId();
} else {
$ent_id = $entity_id;
}
$result =
await $db->queryf(
'UPDATE levels SET title = %s, description = %s, entity_id = %d, category_id = %d, points = %d, '.
'bonus = %d, bonus_dec = %d, bonus_fix = %d, flag = %s, hint = %s, '.
'penalty = %d WHERE id = %d LIMIT 1',
$title,
$description,
$ent_id,
$category_id,
$points,
$bonus,
$bonus_dec,
$bonus_fix,
$flag,
$hint,
$penalty,
$level_id,
);
// Make sure entities are consistent
await Country::genUsedAdjust();
if ($result->numRowsAffected() > 0) {
$country_id = await self::genCountryIdForLevel($level_id);
$country = await Country::gen($country_id);
await \HH\Asio\va(
ActivityLog::genAdminLog("updated", "Country", $country_id),
Announcement::genCreateAuto($country->getName()." updated!"),
);
self::invalidateMCRecords(); // Invalidate Memcached Level data.
ActivityLog::invalidateMCRecords('ALL_ACTIVITY'); // Invalidate Memcached ActivityLog data.
}
}
// Delete level.
public static async function genDelete(int $level_id): Awaitable<void> {
$db = await self::genDb();
// Free country first.
$level = await self::gen($level_id);
await Country::genSetUsed($level->getEntityId(), false);
// Remove team points for level
$scores = await ScoreLog::genAllScoresByLevel($level_id);
$level_delete_queries = Vector {};
foreach ($scores as $score) {
$team_id = $score->getTeamId();
$points = $score->getPoints();
$level_delete_queries->add(
sprintf(
'UPDATE teams SET points = points - %d WHERE id = %d',
$points,
$team_id,
),
);
}
// Remove hint penalties from teams points for level
$hints = await HintLog::genAllHintsByLevel($level_id);
foreach ($hints as $hint) {
$team_id = $hint->getTeamId();
$penalty = $hint->getPenalty();
$level_delete_queries->add(
sprintf(
'UPDATE teams SET points = points + %d WHERE id = %d',
$penalty,
$team_id,
),
);
}
// Delete all references to level
$level_delete_queries->addAll(
Set {
sprintf('DELETE FROM levels WHERE id = %d LIMIT 1', $level_id),
sprintf('DELETE FROM hints_log WHERE level_id = %d', $level_id),
sprintf('DELETE FROM scores_log WHERE level_id = %d', $level_id),
sprintf('DELETE FROM failures_log WHERE level_id = %d', $level_id),
},
);
await $db->multiQuery($level_delete_queries);
self::invalidateMCRecords();
Control::invalidateMCRecords();
MultiTeam::invalidateMCRecords();
HintLog::invalidateMCRecords();
ScoreLog::invalidateMCRecords();
}
// Enable or disable level by passing 1 or 0.
public static async function genSetStatus(
int $level_id,
bool $active,
): Awaitable<void> {
$db = await self::genDb();
$result = await $db->queryf(
'UPDATE levels SET active = %d WHERE id = %d LIMIT 1',
(int) $active,
$level_id,
);
if ($result->numRowsAffected() > 0) {
$action = ($active === true) ? "enabled" : "disabled";
$country_id = await self::genCountryIdForLevel($level_id);
$country = await Country::gen($country_id);
await \HH\Asio\va(
ActivityLog::genAdminLog($action, "Country", $country_id),
Announcement::genCreateAuto($country->getName().' '.$action.'!'),
);
self::invalidateMCRecords();
ActivityLog::invalidateMCRecords('ALL_ACTIVITY');
}
}
// Enable or disable levels by type.
public static async function genSetStatusType(
bool $active,
string $type,
): Awaitable<void> {
$db = await self::genDb();
$results = await $db->queryf(
'UPDATE levels SET active = %d WHERE type = %s',
(int) $active,
$type,
);
if ($results->numRowsAffected() > 0) {
self::invalidateMCRecords();
}
}
// Enable or disable all levels.
public static async function genSetStatusAll(
bool $active,
string $type,
): Awaitable<void> {
$db = await self::genDb();
if ($type === 'all') {
$result = await $db->queryf(
'SELECT id FROM levels WHERE active = %d AND id >0',
(int) !$active,
);
} else {
$result = await $db->queryf(
'SELECT id FROM levels WHERE active = %d AND type = %s',
(int) !$active,
$type,
);
}
foreach ($result->mapRows() as $row) {
await self::genSetStatus(intval($row->get('id')), $active); // TODO: Combine Awaits
}
}
// All levels.
public static async function genAllLevels(
bool $refresh = false,
): Awaitable<array<Level>> {
$mc_result = self::getMCRecords('ALL_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$all_levels = Map {};
$result = await $db->queryf('SELECT * FROM levels ORDER BY id');
foreach ($result->mapRows() as $row) {
$all_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_LEVELS', new Map($all_levels));
$levels = array();
$levels = $all_levels->toValuesArray();
return $levels;
} else {
$levels = array();
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
$levels = $mc_result->toValuesArray();
return $levels;
}
}
public static async function genAllLevelsCountryMap(
bool $refresh = false,
): Awaitable<Map<int, Level>> {
$mc_result = self::getMCRecords('ALL_LEVELS_COUNTRY_MAP');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$all_levels = Map {};
$result = await $db->queryf('SELECT * FROM levels ORDER BY id');
foreach ($result->mapRows() as $row) {
$all_levels->add(
Pair {intval($row->get('entity_id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_LEVELS_COUNTRY_MAP', new Map($all_levels));
return $all_levels;
} else {
$levels = array();
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
return $mc_result;
}
}
// All levels by status.
public static async function genAllActiveLevels(
bool $refresh = false,
): Awaitable<array<Level>> {
$mc_result = self::getMCRecords('ALL_ACTIVE_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$active_levels = Map {};
$result = await $db->queryf(
'SELECT * FROM levels WHERE active = 1 ORDER BY id',
);
foreach ($result->mapRows() as $row) {
$active_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_ACTIVE_LEVELS', $active_levels);
$levels = array();
$levels = $active_levels->toValuesArray();
return $levels;
} else {
$levels = array();
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
$levels = $mc_result->toValuesArray();
return $levels;
}
}
// All levels by status.
public static async function genAllActiveBases(
bool $refresh = false,
): Awaitable<array<Level>> {
$mc_result = self::getMCRecords('ALL_ACTIVE_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$active_levels = Map {};
$result = await $db->queryf(
'SELECT * FROM levels WHERE active = 1 ORDER BY id',
);
foreach ($result->mapRows() as $row) {
$active_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_ACTIVE_LEVELS', $active_levels);
$levels = array();
$levels = $active_levels->toValuesArray();
$bases = array();
foreach ($levels as $level) {
if ($level->type === 'base') {
$bases[] = $level;
}
}
return $bases;
} else {
$levels = array();
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
$levels = $mc_result->toValuesArray();
$bases = array();
foreach ($levels as $level) {
if ($level->type === 'base') {
$bases[] = $level;
}
}
return $bases;
}
}
// All levels by type.
public static async function genAllTypeLevels(
string $type,
bool $refresh = false,
): Awaitable<array<Level>> {
$mc_result = self::getMCRecords('ALL_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$all_levels = Map {};
$result = await $db->queryf('SELECT * FROM levels ORDER BY id');
foreach ($result->mapRows() as $row) {
$all_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_LEVELS', $all_levels);
$levels = array();
$levels = $all_levels->toValuesArray();
$type_levels = array();
foreach ($levels as $level) {
if ($level->type === $type) {
$type_levels[] = $level;
}
}
return $type_levels;
} else {
$levels = array();
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
$levels = $mc_result->toValuesArray();
$type_levels = array();
foreach ($levels as $level) {
if ($level->type === $type) {
$type_levels[] = $level;
}
}
return $type_levels;
}
}
// All quiz levels.
public static async function genAllQuizLevels(): Awaitable<array<Level>> {
return await self::genAllTypeLevels('quiz');
}
// All base levels.
public static async function genAllBaseLevels(): Awaitable<array<Level>> {
return await self::genAllTypeLevels('base');
}
// All flag levels.
public static async function genAllFlagLevels(): Awaitable<array<Level>> {
return await self::genAllTypeLevels('flag');
}
// Get a single level.
public static async function gen(
int $level_id,
bool $refresh = false,
): Awaitable<Level> {
$mc_result = self::getMCRecords('ALL_LEVELS');
if (!$mc_result || count($mc_result) === 0 || $refresh) {
$db = await self::genDb();
$all_levels = Map {};
$result = await $db->queryf('SELECT * FROM levels ORDER BY id');
foreach ($result->mapRows() as $row) {
$all_levels->add(
Pair {intval($row->get('id')), self::levelFromRow($row)},
);
}
self::setMCRecords('ALL_LEVELS', $all_levels);
invariant(
$all_levels->contains($level_id) !== false,
'level not found',
);
invariant(
$all_levels->contains($level_id) !== false,
'level not found',
);
$level = $all_levels->get($level_id);
invariant($level instanceof Level, 'level should be of type Level');
return $level;
} else {
invariant(
$mc_result instanceof Map,
'cache return should be of type Map',
);
invariant($mc_result->contains($level_id) !== false, 'level not found');
$level = $mc_result->get($level_id);
invariant($level instanceof Level, 'level should be of type Level');
return $level;
}
}
// Check if flag is correct.