File size: 82,353 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
/**
 * Wi-Fi chat-plugin. Only works in a room with id 'wifi'
 * Handles giveaways in the formats: question, lottery, gts
 * Written by dhelmise and bumbadadabum, based on the original
 * plugin as written by Codelegend, SilverTactic, DanielCranham
 */

import { FS, Utils } from '../../lib';

Punishments.addRoomPunishmentType({
	type: 'GIVEAWAYBAN',
	desc: 'banned from giveaways',
});

const DAY = 24 * 60 * 60 * 1000;
const RECENT_THRESHOLD = 30 * 24 * 60 * 60 * 1000;

const DATA_FILE = 'config/chat-plugins/wifi.json';

type Game = 'SwSh' | 'BDSP' | 'SV';

interface GiveawayData {
	targetUserID: string;
	ot: string;
	tid: string;
	game: Game;
	prize: PokemonSet;
	ivs: string[];
	ball: string;
	extraInfo: string;
	/** Staff handling it. */
	claimed?: ID;
}

interface QuestionGiveawayData extends GiveawayData {
	question: string;
	answers: string[];
}

interface LotteryGiveawayData extends GiveawayData {
	winners: number;
}

interface WifiData {
	whitelist: string[];
	stats: { [k: string]: number[] };
	storedGiveaways: { question: QuestionGiveawayData[], lottery: LotteryGiveawayData[] };
	submittedGiveaways: { question: QuestionGiveawayData[], lottery: LotteryGiveawayData[] };
}

const defaults: WifiData = {
	whitelist: [],
	stats: {},
	storedGiveaways: {
		question: [],
		lottery: [],
	},
	submittedGiveaways: {
		question: [],
		lottery: [],
	},
};

export let wifiData: WifiData = (() => {
	try {
		return JSON.parse(FS(DATA_FILE).readSync());
	} catch (e: any) {
		if (e.code !== 'ENOENT') throw e;
		return defaults;
	}
})();

function saveData() {
	FS(DATA_FILE).writeUpdate(() => JSON.stringify(wifiData));
}

// Convert old file type
if (!wifiData.stats && !wifiData.storedGiveaways && !wifiData.submittedGiveaways) {
	// we cast under the assumption that it's the old file format
	const stats = { ...wifiData } as unknown as { [k: string]: number[] };
	wifiData = { ...defaults, stats };
	saveData();
}
// ensure the whitelist exists for those who might have the conversion above but not the stats
if (!wifiData.whitelist) wifiData.whitelist = [];

const statNames = ["HP", "Atk", "Def", "SpA", "SpD", "Spe"];

const gameName: { [k in Game]: string } = {
	SwSh: 'Sword/Shield',
	BDSP: 'Brilliant Diamond/Shining Pearl',
	SV: 'Scarlet/Violet',
};
const gameidToGame: { [k: string]: Game } = {
	swsh: 'SwSh',
	bdsp: 'BDSP',
	sv: 'SV',
};

abstract class Giveaway extends Rooms.SimpleRoomGame {
	override readonly gameid = 'giveaway' as ID;
	abstract type: string;
	gaNumber: number;
	host: User;
	giver: User;
	room: Room;
	ot: string;
	tid: string;
	game: Game;
	ivs: string[];
	prize: PokemonSet;
	phase: string;
	ball: string;
	extraInfo: string;
	/**
	 * IP:userid
	 */
	joined: Map<string, ID>;
	timer: NodeJS.Timeout | null;
	pokemonID: ID;
	sprite: Chat.VNode;

	constructor(
		host: User, giver: User, room: Room, ot: string, tid: string, ivs: string[],
		prize: PokemonSet, game: Game = 'SV', ball: string, extraInfo: string
	) {
		// Make into a sub-game if the gts ever opens up again
		super(room);
		this.gaNumber = room.nextGameNumber();
		this.host = host;
		this.giver = giver;
		this.room = room;
		this.ot = ot;
		this.tid = tid;
		this.ball = ball;
		this.extraInfo = extraInfo;
		this.game = game;
		this.ivs = ivs;
		this.prize = prize;
		this.phase = 'pending';

		this.joined = new Map();

		this.timer = null;

		[this.pokemonID, this.sprite] = Giveaway.getSprite(prize);
	}

	destroy() {
		this.clearTimer();
		super.destroy();
	}

	generateReminder(joined?: boolean): string | Chat.VNode;
	generateReminder() {
		return '';
	}

	getStyle() {
		const css: { [k: string]: string | { [k: string]: string } } = { class: "broadcast-blue" };
		if (this.game === 'BDSP') css.style = { background: '#aa66a9', color: '#fff' };
		if (this.game === 'SV') css.style = { background: '#CD5C5C', color: '#fff' };
		return css;
	}

	sendToUser(user: User, content: string | Chat.VNode) {
		user.sendTo(
			this.room,
			Chat.html`|uhtmlchange|giveaway${this.gaNumber}${this.phase}|${<div {...this.getStyle()}>{content}</div>}`
		);
	}

	send(content: string | Chat.VNode, isStart = false) {
		this.room.add(Chat.html`|uhtml|giveaway${this.gaNumber}${this.phase}|${<div {...this.getStyle()}>{content}</div>}`);
		if (isStart) this.room.add(`|c:|${Math.floor(Date.now() / 1000)}|~|It's ${this.game} giveaway time!`);
		this.room.update();
	}

	changeUhtml(content: string | Chat.VNode) {
		this.room.uhtmlchange(`giveaway${this.gaNumber}${this.phase}`, Chat.html`${<div {...this.getStyle()}>{content}</div>}`);
		this.room.update();
	}

	clearTimer() {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}

	checkJoined(user: User) {
		for (const [ip, id] of this.joined) {
			if (user.latestIp === ip && !Config.noipchecks) return ip;
			if (user.previousIDs.includes(id)) return id;
		}
		return false;
	}

	kickUser(user: User) {
		for (const [ip, id] of this.joined) {
			if (user.latestIp === ip && !Config.noipchecks || user.previousIDs.includes(id)) {
				this.sendToUser(user, this.generateReminder());
				this.joined.delete(ip);
			}
		}
	}

	checkExcluded(user: User) {
		return (
			user === this.giver ||
			!Config.noipchecks && this.giver.ips.includes(user.latestIp) ||
			this.giver.previousIDs.includes(toID(user))
		);
	}

	static checkCanCreate(context: Chat.CommandContext, targetUser: User, type: string) {
		const user = context.user;
		const isCreate = type === 'create';
		const isForSelf = targetUser.id === user.id;
		if (wifiData.whitelist.includes(user.id) && isCreate && isForSelf) {
			// it being true doesn't matter here, it's just clearer that the user _is_ allowed
			// and it ensures execution stops here so the creation can proceed
			return true;
		}
		if (isCreate && !(isForSelf && user.can('show', null, context.room!))) {
			context.checkCan('warn', null, context.room!);
		}
		if (!user.can('warn', null, context.room!) && !isCreate && !isForSelf) {
			throw new Chat.ErrorMessage(`You can't ${type} giveways for other users.`);
		}
	}

	static checkBanned(room: Room, user: User) {
		return Punishments.hasRoomPunishType(room, toID(user), 'GIVEAWAYBAN');
	}

	static ban(room: Room, user: User, reason: string, duration: number) {
		Punishments.roomPunish(room, user, {
			type: 'GIVEAWAYBAN',
			id: toID(user),
			expireTime: Date.now() + duration,
			reason,
		});
	}

	static unban(room: Room, user: User) {
		Punishments.roomUnpunish(room, user.id, 'GIVEAWAYBAN', false);
	}

	static getSprite(set: PokemonSet): [ID, Chat.VNode] {
		const species = Dex.species.get(set.species);
		let spriteid = species.spriteid;
		if (species.cosmeticFormes) {
			for (const forme of species.cosmeticFormes.map(toID)) {
				if (toID(set.species).includes(forme)) {
					spriteid += '-' + forme.slice(species.baseSpecies.length);
					break; // We don't want to end up with deerling-summer-spring
				}
			}
		}
		if (!spriteid.includes('-') && species.forme) { // for stuff like unown letters
			spriteid += '-' + toID(species.forme);
		}
		const shiny = set.shiny ? '-shiny' : '';

		const validFemale = [
			'abomasnow', 'aipom', 'ambipom', 'beautifly', 'bibarel', 'bidoof', 'blaziken', 'buizel', 'cacturne', 'camerupt', 'combee',
			'combusken', 'croagunk', 'donphan', 'dustox', 'finneon', 'floatzel', 'frillish', 'gabite', 'garchomp', 'gible', 'girafarig',
			'gligar', 'golbat', 'gulpin', 'heracross', 'hippopotas', 'hippowdon', 'houndoom', 'indeedee', 'jellicent', 'kerfluffle', 'kitsunoh',
			'kricketot', 'kricketune', 'ledian', 'ledyba', 'ludicolo', 'lumineon', 'luxio', 'luxray', 'magikarp', 'mamoswine', 'medicham',
			'meditite', 'meganium', 'meowstic', 'milotic', 'murkrow', 'nidoran', 'numel', 'nuzleaf', 'octillery', 'pachirisu', 'pikachu',
			'pikachu-starter', 'piloswine', 'politoed', 'protowatt', 'pyroar', 'quagsire', 'raticate', 'rattata', 'relicanth', 'rhydon',
			'rhyperior', 'roselia', 'roserade', 'rotom', 'scizor', 'scyther', 'shiftry', 'shinx', 'sneasel', 'snover', 'staraptor', 'staravia',
			'starly', 'steelix', 'sudowoodo', 'swalot', 'tangrowth', 'torchic', 'toxicroak', 'unfezant', 'unown', 'ursaring', 'voodoom',
			'weavile', 'wobbuffet', 'wooper', 'xatu', 'zubat',
		];
		if (set.gender === 'F' && validFemale.includes(species.id)) spriteid += '-f';
		return [
			species.id,
			<img src={`/sprites/ani${shiny}/${spriteid}.gif`} />,
		];
	}

	static updateStats(pokemonIDs: Set<string>) {
		for (const mon of pokemonIDs) {
			if (!wifiData.stats[mon]) wifiData.stats[mon] = [];
			wifiData.stats[mon].push(Date.now());
		}
		saveData();
	}

	// Wi-Fi uses special IV syntax to show hyper trained IVs
	static convertIVs(setObj: PokemonSet, ivs: string[]) {
		let set = Teams.exportSet(setObj);
		let ivsStr = '';
		if (ivs.length) {
			const convertedIVs = { hp: '31', atk: '31', def: '31', spa: '31', spd: '31', spe: '31' };
			for (const [i, iv] of ivs.entries()) {
				const numStr = iv.trim().split(' ')[0];
				const statName = statNames[i];
				convertedIVs[toID(statName) as StatID] = numStr;
			}
			const array = Object.keys(convertedIVs).map((x, i) => `${convertedIVs[x as StatID]} ${statNames[i]}`);
			ivsStr = `IVs: ${array.join(' / ')}  `;
		}
		if (ivsStr) {
			if (/\nivs:/i.test(set)) {
				const arr = set.split('\n');
				const index = arr.findIndex(x => /^ivs:/i.test(x));
				arr[index] = ivsStr;
				set = arr.join('\n');
			} else if (/nature\n/i.test(set)) {
				const arr = set.split('\n');
				const index = arr.findIndex(x => /nature$/i.test(x));
				arr.splice(index + 1, 0, ivsStr);
				set = arr.join('\n');
			} else {
				set += `\n${ivsStr}`;
			}
		}
		return set;
	}

	generateWindow(rightSide: Chat.VNode | string): Chat.VNode {
		const set = Giveaway.convertIVs(this.prize, this.ivs);
		return <center>
			<h3>It's {this.game} giveaway time!</h3>
			<small>Giveaway started by {this.host.name}</small>
			<table style={{ marginLeft: 'auto', marginRight: 'auto' }}>
				<tr>
					<td colSpan={2} style={{ textAlign: 'center' }}>
						<strong>Giver:</strong> {this.giver.name}<br />
						<strong>OT:</strong> {this.ot}, <strong>TID:</strong> {this.tid}
					</td>
				</tr>
				<tr>
					<td style={{ textAlign: 'center', width: '45%' }}>
						<psicon item={this.ball} /> {this.sprite} <psicon item={this.ball} /><br />
						<Chat.JSX.FormatText isTrusted>{set}</Chat.JSX.FormatText>
					</td>
					<td style={{ textAlign: 'center', width: '45%' }}>{rightSide}</td>
				</tr>
				{!!this.extraInfo?.trim().length && <tr>
					<td colSpan={2} style={{ textAlign: 'center' }}>
						<strong>Extra Information</strong><br />
						<Chat.JSX.FormatText isTrusted>{this.extraInfo.trim().replace(/<br \/>/g, '\n')}</Chat.JSX.FormatText>
					</td>
				</tr>}
			</table>
			<p style={{ textAlign: 'center', fontSize: '7pt', fontWeight: 'bold' }}>
				<u>Note:</u> You must have a Switch, Pok&eacute;mon {gameName[this.game]}, {}
				and NSO to receive the prize. {}
				Do not join if you are currently unable to trade. Do not enter if you have already won this exact Pok&eacute;mon, {}
				unless it is explicitly allowed.
			</p>
		</center>;
	}
}

export class QuestionGiveaway extends Giveaway {
	type: string;
	question: string;
	answers: string[];
	/** userid: number of guesses */
	answered: Utils.Multiset<string>;
	winner: User | null;

	constructor(
		host: User, giver: User, room: Room, ot: string, tid: string, game: Game, ivs: string[],
		prize: PokemonSet, question: string, answers: string[], ball: string, extraInfo: string
	) {
		super(host, giver, room, ot, tid, ivs, prize, game, ball, extraInfo);
		this.type = 'question';
		this.phase = 'pending';

		this.question = question;
		this.answers = QuestionGiveaway.sanitizeAnswers(answers);
		this.answered = new Utils.Multiset();
		this.winner = null;
		this.send(this.generateWindow('The question will be displayed in one minute! Use /guess to answer.'), true);

		this.timer = setTimeout(() => this.start(), 1000 * 60);
	}

	static splitTarget(
		target: string, sep = '|', context: Chat.CommandContext,
		user: User, type: 'create' | 'store' | 'submit'
	) {
		let [
			giver, ot, tid, game, question, answers, ivs, ball, extraInfo, ...prize
		] = target.split(sep).map(param => param.trim());
		if (!(giver && ot && tid && prize?.length && question && answers?.split(',').length)) {
			context.parse(`/help giveaway`);
			throw new Chat.Interruption();
		}
		const targetUser = Users.get(giver);
		if (!targetUser?.connected) throw new Chat.ErrorMessage(`User '${giver}' is not online.`);

		Giveaway.checkCanCreate(context, targetUser, type);

		if (!!ivs && ivs.split('/').length !== 6) {
			throw new Chat.ErrorMessage(`If you provide IVs, they must be provided for all stats.`);
		}
		if (!game) game = 'SV';
		game = gameidToGame[toID(game)] || game as Game;
		if (!game || !['SV', 'BDSP', 'SwSh'].includes(game)) {
			throw new Chat.ErrorMessage(`The game must be "SV," "BDSP," or "SwSh".`);
		}
		if (!ball) ball = 'pokeball';
		if (!toID(ball).endsWith('ball')) ball = toID(ball) + 'ball';
		if (!Dex.items.get(ball).isPokeball) {
			throw new Chat.ErrorMessage(`${Dex.items.get(ball).name} is not a Pok\u00e9 Ball.`);
		}
		tid = toID(tid);
		if (isNaN(parseInt(tid)) || tid.length < 5 || tid.length > 6) throw new Chat.ErrorMessage("Invalid TID");
		if (!targetUser.autoconfirmed) {
			throw new Chat.ErrorMessage(`User '${targetUser.name}' needs to be autoconfirmed to give something away.`);
		}
		if (Giveaway.checkBanned(context.room!, targetUser)) {
			throw new Chat.ErrorMessage(`User '${targetUser.name}' is giveaway banned.`);
		}
		return {
			targetUser, ot, tid, game: game as Game, question, answers: answers.split(','),
			ivs: ivs.split('/'), ball, extraInfo, prize: prize.join('|'),
		};
	}

	generateQuestion() {
		return this.generateWindow(<>
			<p style={{ textAlign: 'center', fontSize: '13pt' }}>Giveaway Question: <b>{this.question}</b></p>
			<p style={{ textAlign: 'center' }}>use /guess to answer.</p>
		</>);
	}

	start() {
		this.changeUhtml(<p style={{ textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' }}>
			The giveaway has started! Scroll down to see the question.
		</p>);
		this.phase = 'started';
		this.send(this.generateQuestion());
		this.timer = setTimeout(() => this.end(false), 1000 * 60 * 5);
	}

	choose(user: User, guess: string) {
		if (this.phase !== 'started') return user.sendTo(this.room, "The giveaway has not started yet.");

		if (this.checkJoined(user) && ![...this.joined.values()].includes(user.id)) {
			return user.sendTo(this.room, "You have already joined the giveaway.");
		}
		if (Giveaway.checkBanned(this.room, user)) return user.sendTo(this.room, "You are banned from entering giveaways.");
		if (this.checkExcluded(user)) return user.sendTo(this.room, "You are disallowed from entering the giveaway.");

		if (this.answered.get(user.id) >= 3) {
			return user.sendTo(
				this.room,
				"You have already guessed three times. You cannot guess anymore in this.giveaway."
			);
		}

		const sanitized = toID(guess);

		for (const answer of this.answers.map(toID)) {
			if (answer === sanitized) {
				this.winner = user;
				this.clearTimer();
				return this.end(false);
			}
		}

		this.joined.set(user.latestIp, user.id);
		this.answered.add(user.id);
		if (this.answered.get(user.id) >= 3) {
			user.sendTo(
				this.room,
				`Your guess '${guess}' is wrong. You have used up all of your guesses. Better luck next time!`
			);
		} else {
			user.sendTo(this.room, `Your guess '${guess}' is wrong. Try again!`);
		}
	}

	change(value: string, user: User, answer = false) {
		if (user.id !== this.host.id) return user.sendTo(this.room, "Only the host can edit the giveaway.");
		if (this.phase !== 'pending') {
			return user.sendTo(this.room, "You cannot change the question or answer once the giveaway has started.");
		}
		if (!answer) {
			this.question = value;
			return user.sendTo(this.room, `The question has been changed to ${value}.`);
		}
		const ans = QuestionGiveaway.sanitizeAnswers(value.split(',').map(val => val.trim()));
		if (!ans.length) {
			return user.sendTo(this.room, "You must specify at least one answer and it must not contain any special characters.");
		}
		this.answers = ans;
		user.sendTo(this.room, `The answer${Chat.plural(ans, "s have", "has")} been changed to ${ans.join(', ')}.`);
	}

	end(force: boolean) {
		const style = { textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' };
		if (force) {
			this.clearTimer();
			this.changeUhtml(<p style={style}>The giveaway was forcibly ended.</p>);
			this.room.send("The giveaway was forcibly ended.");
		} else {
			if (!this.winner) {
				this.changeUhtml(<p style={style}>The giveaway was forcibly ended.</p>);
				this.room.send("The giveaway has been forcibly ended as no one has answered the question.");
			} else {
				this.changeUhtml(<p style={style}>The giveaway has ended! Scroll down to see the answer.</p>);
				this.phase = 'ended';
				this.clearTimer();
				this.room.modlog({
					action: 'GIVEAWAY WIN',
					userid: this.winner.id,
					note: `${this.giver.name}'s giveaway for a "${this.prize.species}" (OT: ${this.ot} TID: ${this.tid} Nature: ${this.prize.nature} Ball: ${this.ball}${this.extraInfo ? ` Other box info: ${this.extraInfo}` : ''})`,
				});
				this.send(this.generateWindow(<>
					<p style={{ textAlign: 'center', fontSize: '12pt' }}>
						<b>{this.winner.name}</b> won the giveaway! Congratulations!
					</p>
					<p style={{ textAlign: 'center' }}>
						{this.question}<br />
						Correct answer{Chat.plural(this.answers)}: {this.answers.join(', ')}
					</p>
				</>));
				this.winner.sendTo(
					this.room,
					`|raw|You have won the giveaway. PM <b>${Utils.escapeHTML(this.giver.name)}</b> to claim your prize!`
				);
				if (this.winner.connected) {
					this.winner.popup(`You have won the giveaway. PM **${this.giver.name}** to claim your prize!`);
				}
				if (this.giver.connected) this.giver.popup(`${this.winner.name} has won your question giveaway!`);
				Giveaway.updateStats(new Set([this.pokemonID]));
			}
		}

		this.destroy();
	}

	static sanitize(str: string) {
		return str.toLowerCase().replace(/[^a-z0-9 .-]+/ig, "").trim();
	}

	static sanitizeAnswers(answers: string[]) {
		return answers.map(
			val => QuestionGiveaway.sanitize(val)
		).filter(
			(val, index, array) => toID(val).length && array.indexOf(val) === index
		);
	}

	checkExcluded(user: User) {
		if (user === this.host) return true;
		if (this.host.ips.includes(user.latestIp) && !Config.noipchecks) return true;
		if (this.host.previousIDs.includes(toID(user))) return true;
		return super.checkExcluded(user);
	}
}

export class LotteryGiveaway extends Giveaway {
	type: string;
	winners: User[];
	maxWinners: number;

	constructor(
		host: User, giver: User, room: Room, ot: string, tid: string, ivs: string[],
		game: Game, prize: PokemonSet, winners: number, ball: string, extraInfo: string
	) {
		super(host, giver, room, ot, tid, ivs, prize, game, ball, extraInfo);

		this.type = 'lottery';
		this.phase = 'pending';

		this.winners = [];

		this.maxWinners = winners || 1;

		this.send(this.generateReminder(false), true);

		this.timer = setTimeout(() => this.drawLottery(), 1000 * 60 * 2);
	}

	static splitTarget(
		target: string, sep = '|', context: Chat.CommandContext,
		user: User, type: 'create' | 'store' | 'submit'
	) {
		let [giver, ot, tid, game, winners, ivs, ball, extraInfo, ...prize] = target.split(sep).map(param => param.trim());
		if (!(giver && ot && tid && prize?.length)) {
			context.parse(`/help giveaway`);
			throw new Chat.Interruption();
		}
		const targetUser = Users.get(giver);
		if (!targetUser?.connected) throw new Chat.ErrorMessage(`User '${giver}' is not online.`);

		Giveaway.checkCanCreate(context, user, type);

		if (!!ivs && ivs.split('/').length !== 6) {
			throw new Chat.ErrorMessage(`If you provide IVs, they must be provided for all stats.`);
		}
		if (!game) game = 'SV';
		game = gameidToGame[toID(game)] || game as Game;
		if (!game || !['SV', 'BDSP', 'SwSh'].includes(game)) {
			throw new Chat.ErrorMessage(`The game must be "SV," "BDSP," or "SwSh".`);
		}
		if (!ball) ball = 'pokeball';
		if (!toID(ball).endsWith('ball')) ball = toID(ball) + 'ball';
		if (!Dex.items.get(ball).isPokeball) {
			throw new Chat.ErrorMessage(`${Dex.items.get(ball).name} is not a Pok\u00e9 Ball.`);
		}
		tid = toID(tid);
		if (isNaN(parseInt(tid)) || tid.length < 5 || tid.length > 6) throw new Chat.ErrorMessage("Invalid TID");
		if (!targetUser.autoconfirmed) {
			throw new Chat.ErrorMessage(`User '${targetUser.name}' needs to be autoconfirmed to give something away.`);
		}
		if (Giveaway.checkBanned(context.room!, targetUser)) {
			throw new Chat.ErrorMessage(`User '${targetUser.name}' is giveaway banned.`);
		}

		let numWinners = 1;
		if (winners) {
			numWinners = parseInt(winners);
			if (isNaN(numWinners) || numWinners < 1 || numWinners > 5) {
				throw new Chat.ErrorMessage("The lottery giveaway can have a minimum of 1 and a maximum of 5 winners.");
			}
		}
		return {
			targetUser, ot, tid, game: game as Game, winners: numWinners,
			ivs: ivs.split('/'), ball, extraInfo, prize: prize.join('|'),
		};
	}

	generateReminder(joined = false) {
		const cmd = (joined ? 'Leave' : 'Join');
		return this.generateWindow(<>
			The lottery drawing will occur in 2 minutes, and with {Chat.count(this.maxWinners, "winners")}!<br />
			<button class="button" name="send" value={`/giveaway ${toID(cmd)}lottery`}><strong>{cmd}</strong></button>
		</>);
	}

	display() {
		const joined = this.generateReminder(true);
		const notJoined = this.generateReminder();

		for (const i in this.room.users) {
			const thisUser = this.room.users[i];
			if (this.checkJoined(thisUser)) {
				this.sendToUser(thisUser, joined);
			} else {
				this.sendToUser(thisUser, notJoined);
			}
		}
	}

	addUser(user: User) {
		if (this.phase !== 'pending') return user.sendTo(this.room, "The join phase of the lottery giveaway has ended.");

		if (!user.named) return user.sendTo(this.room, "You need to choose a name before joining a lottery giveaway.");
		if (this.checkJoined(user)) return user.sendTo(this.room, "You have already joined the giveaway.");
		if (Giveaway.checkBanned(this.room, user)) return user.sendTo(this.room, "You are banned from entering giveaways.");
		if (this.checkExcluded(user)) return user.sendTo(this.room, "You are disallowed from entering the giveaway.");

		this.joined.set(user.latestIp, user.id);
		this.sendToUser(user, this.generateReminder(true));
		user.sendTo(this.room, "You have successfully joined the lottery giveaway.");
	}

	removeUser(user: User) {
		if (this.phase !== 'pending') return user.sendTo(this.room, "The join phase of the lottery giveaway has ended.");
		if (!this.checkJoined(user)) return user.sendTo(this.room, "You have not joined the lottery giveaway.");
		for (const [ip, id] of this.joined) {
			if (ip === user.latestIp && !Config.noipchecks || id === user.id) {
				this.joined.delete(ip);
			}
		}
		this.sendToUser(user, this.generateReminder(false));
		user.sendTo(this.room, "You have left the lottery giveaway.");
	}

	drawLottery() {
		this.clearTimer();

		const userlist = [...this.joined.values()];
		if (userlist.length === 0) {
			this.changeUhtml(<p style={{ textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' }}>
				The giveaway was forcibly ended.
			</p>);
			this.room.send("The giveaway has been forcibly ended as there are no participants.");
			return this.destroy();
		}

		while (this.winners.length < this.maxWinners && userlist.length > 0) {
			const winner = Users.get(userlist.splice(Math.floor(Math.random() * userlist.length), 1)[0]);
			if (!winner) continue;
			this.winners.push(winner);
		}
		this.end();
	}

	end(force = false) {
		const style = { textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' };
		if (force) {
			this.clearTimer();
			this.changeUhtml(<p style={style}>The giveaway was forcibly ended.</p>);
			this.room.send("The giveaway was forcibly ended.");
		} else {
			this.changeUhtml(<p style={style}>
				The giveaway has ended! Scroll down to see the winner{Chat.plural(this.winners)}.
			</p>);
			this.phase = 'ended';
			const winnerNames = this.winners.map(winner => winner.name).join(', ');
			this.room.modlog({
				action: 'GIVEAWAY WIN',
				note: `${winnerNames} won ${this.giver.name}'s giveaway for "${this.prize.species}" (OT: ${this.ot} TID: ${this.tid} Nature: ${this.prize.nature} Ball: ${this.ball}${this.extraInfo ? ` Other box info: ${this.extraInfo}` : ''})`,
			});
			this.send(this.generateWindow(<>
				<p style={{ textAlign: 'center', fontSize: '10pt', fontWeight: 'bold' }}>Lottery Draw</p>
				<p style={{ textAlign: 'center' }}>{Chat.count(this.joined.size, 'users')} joined the giveaway.<br />
					Our lucky winner{Chat.plural(this.winners)}: <b>{winnerNames}</b>!<br />Congratulations!</p>
			</>));
			this.room.sendMods(`|c|~|Participants: ${[...this.joined.values()].join(', ')}`);
			for (const winner of this.winners) {
				winner.sendTo(
					this.room,
					`|raw|You have won the lottery giveaway! PM <b>${this.giver.name}</b> to claim your prize!`
				);
				if (winner.connected) {
					winner.popup(`You have won the lottery giveaway! PM **${this.giver.name}** to claim your prize!`);
				}
			}
			if (this.giver.connected) this.giver.popup(`The following users have won your lottery giveaway:\n${winnerNames}`);
			Giveaway.updateStats(new Set([this.pokemonID]));
		}
		this.destroy();
	}
}

export class GTS extends Rooms.SimpleRoomGame {
	override readonly gameid = 'gts' as ID;
	gtsNumber: number;
	room: Room;
	giver: User;
	left: number;
	summary: string;
	deposit: string;
	lookfor: string;
	pokemonID: ID;
	sprite: Chat.VNode;
	sent: string[];
	noDeposits: boolean;
	timer: NodeJS.Timeout | null;

	constructor(
		room: Room, giver: User, amount: number,
		summary: string, deposit: string, lookfor: string
	) {
		// Always a sub-game so tours etc can be ran while GTS games are running
		super(room, true);
		this.gtsNumber = room.nextGameNumber();
		this.room = room;
		this.giver = giver;
		this.left = amount;
		this.summary = summary;
		this.deposit = GTS.linkify(Utils.escapeHTML(deposit));
		this.lookfor = lookfor;

		// Deprecated, just typed like this to prevent errors, will rewrite when GTS is planned to be used again
		[this.pokemonID, this.sprite] = Giveaway.getSprite({ species: summary } as PokemonSet);

		this.sent = [];
		this.noDeposits = false;

		this.timer = setInterval(() => this.send(this.generateWindow()), 1000 * 60 * 5);
		this.send(this.generateWindow());
	}

	send(content: string) {
		this.room.add(Chat.html`|uhtml|gtsga${this.gtsNumber}|${<div class="broadcast-blue">{content}</div>}`);
		this.room.update();
	}

	changeUhtml(content: string) {
		this.room.uhtmlchange(`gtsga${this.gtsNumber}`, Chat.html`${<div class="broadcast-blue">{content}</div>}`);
		this.room.update();
	}

	clearTimer() {
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}

	generateWindow() {
		const sentModifier = this.sent.length ? 5 : 0;
		const rightSide = this.noDeposits ?
			<strong>
				More Pok&eacute;mon have been deposited than there are prizes in this giveaway and new deposits will not be accepted. {}
				If you have already deposited a Pok&eacute;mon, please be patient, and do not withdraw your Pok&eacute;mon.
			</strong> : <>
				To participate, deposit <strong>{this.deposit}</strong> into the GTS and look for <strong>{this.lookfor}</strong>
			</>;
		return <>
			<p style={{ textAlign: 'center', fontSize: '14pt', fontWeight: 'bold', marginBottom: '2px' }}>
				There is a GTS giveaway going on!
			</p>
			<p style={{ textAlign: 'center', fontSize: '10pt', marginTop: 0 }}>
				Hosted by: {this.giver.name} | Left: <b>{this.left}</b>
			</p>
			<table style={{ margin: 'inherit auto' }}>
				<tr>
					{!!sentModifier && <td style={{ textAlign: 'center', width: '10%' }}>
						<b>Last winners:</b><br />
						{this.sent.join(<br />)}
					</td>}
					<td style={{ textAlign: 'center', width: '15%' }}>{this.sprite}</td>
					<td style={{ textAlign: 'center', width: `${40 - sentModifier}%` }}>
						<Chat.JSX.FormatText isTrusted>{this.summary}</Chat.JSX.FormatText>
					</td>
					<td style={{ textAlign: 'center', width: `${35 - sentModifier}%` }}>{rightSide}</td>
				</tr>
			</table>
		</>;
	}

	updateLeft(num: number) {
		this.left = num;
		if (this.left < 1) return this.end();

		this.changeUhtml(this.generateWindow());
	}

	updateSent(ign: string) {
		this.left--;
		if (this.left < 1) return this.end();

		this.sent.push(ign);
		if (this.sent.length > 5) this.sent.shift();

		this.changeUhtml(this.generateWindow());
	}

	stopDeposits() {
		this.noDeposits = true;

		this.room.send(Chat.html`|html|${<p style={{ textAlign: 'center', fontSize: '11pt' }}>
			More Pok&eacute;mon have been deposited than there are prizes in this giveaway and new deposits will not be accepted. {}
			If you have already deposited a Pok&eacute;mon, please be patient, and do not withdraw your Pok&eacute;mon.
		</p>}`);
		this.changeUhtml(this.generateWindow());
	}

	end(force = false) {
		if (force) {
			this.clearTimer();
			this.changeUhtml(
				<p style={{ textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' }}>The GTS giveaway was forcibly ended.</p>
			);
			this.room.send("The GTS giveaway was forcibly ended.");
		} else {
			this.clearTimer();
			this.changeUhtml(
				<p style={{ textAlign: 'center', fontSize: '13pt', fontWeight: 'bold' }}>The GTS giveaway has finished.</p>
			);
			this.room.modlog({
				action: 'GTS FINISHED',
				userid: this.giver.id,
				note: `their GTS giveaway for "${this.summary}"`,
			});
			this.send(<p style={{ textAlign: 'center', fontSize: '11pt' }}>
				The GTS giveaway for a "<strong>{this.lookfor}</strong>" has finished.
			</p>);
			Giveaway.updateStats(new Set([this.pokemonID]));
		}
		this.room.subGame = null;
		return this.left;
	}

	// This currently doesn't match some of the edge cases the other pokemon matching function does account for
	// (such as Type: Null). However, this should never be used as a fodder mon anyway,
	// so I don't see a huge need to implement it.
	static linkify(text: string) {
		const parsed = toID(text);

		for (const species of Dex.species.all()) {
			const id = species.id;
			const regexp = new RegExp(`\\b${id}\\b`, 'ig');
			const res = regexp.exec(parsed);
			if (res) {
				const num = String(species.num).padStart(3, '0');
				return <>
					{text.slice(0, res.index)}
					<a href={`http://www.serebii.net/pokedex-sm/location/${num}.shtml`}>
						{text.slice(res.index, res.index + res[0].length)}
					</a>
					{text.slice(res.index + res[0].length)}
				</>;
			}
		}
		return text;
	}
}

function hasSubmittedGiveaway(user: User) {
	for (const [key, giveaways] of Object.entries(wifiData.submittedGiveaways)) {
		for (const [index, giveaway] of giveaways.entries()) {
			if (user.id === giveaway.targetUserID) {
				return { index, type: key as 'question' | 'lottery' };
			}
		}
	}
	return null;
}

export const handlers: Chat.Handlers = {
	onDisconnect(user) {
		const giveaway = hasSubmittedGiveaway(user);
		if (giveaway) {
			wifiData.submittedGiveaways[giveaway.type].splice(giveaway.index, 1);
			saveData();
		}
	},
	onPunishUser(type, user, room) {
		const game = room?.getGame(LotteryGiveaway) || room?.getGame(QuestionGiveaway);
		if (game) {
			game.kickUser(user);
		}
	},
};

export const commands: Chat.ChatCommands = {
	gts: {
		new: 'start',
		create: 'start',
		start(target, room, user) {
			room = this.room = Rooms.search('wifi') || null;
			if (!room) {
				throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
			}
			if (room.getGame(GTS, true)) {
				throw new Chat.ErrorMessage(`There is already a GTS Giveaway going on.`);
			}
			// GTS is currently deprecated until it's no longer behind a paywall
			return this.parse(`/help gts`);
			/*
			const [giver, amountStr, summary, deposit, lookfor] = target.split(target.includes('|') ? '|' : ',').map(
				param => param.trim()
			);
			if (!(giver && amountStr && summary && deposit && lookfor)) {
				return this.errorReply("Invalid arguments specified - /gts start giver | amount | summary | deposit | lookfor");
			}
			const amount = parseInt(amountStr);
			if (!amount || amount < 20 || amount > 100) {
				return this.errorReply("Please enter a valid amount. For a GTS giveaway, you need to give away at least 20 mons, and no more than 100.");
			}
			const targetUser = Users.get(giver);
			if (!targetUser?.connected) return this.errorReply(`User '${giver}' is not online.`);
			this.checkCan('warn', null, room);
			if (!targetUser.autoconfirmed) {
				return this.errorReply(`User '${targetUser.name}' needs to be autoconfirmed to host a giveaway.`);
			}
			if (Giveaway.checkBanned(room, targetUser)) return this.errorReply(`User '${targetUser.name}' is giveaway banned.`);

			room.subGame = new GTS(room, targetUser, amount, summary, deposit, lookfor);

			this.privateModAction(`${user.name} started a GTS giveaway for ${targetUser.name} with ${amount} Pokémon`);
			this.modlog('GTS GIVEAWAY', null, `for ${targetUser.getLastId()} with ${amount} Pokémon`);
			*/
		},
		left(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			const game = this.requireGame(GTS, true);
			if (!user.can('warn', null, room) && user !== game.giver) {
				throw new Chat.ErrorMessage("Only the host or a staff member can update GTS giveaways.");
			}
			if (!target) {
				this.runBroadcast();
				let output = `The GTS giveaway from ${game.giver} has ${game.left} Pokémon remaining!`;
				if (game.sent.length) output += `Last winners: ${game.sent.join(', ')}`;
				return this.sendReply(output);
			}
			const newamount = parseInt(target);
			if (isNaN(newamount)) return this.errorReply("Please enter a valid amount.");
			if (newamount > game.left) return this.errorReply("The new amount must be lower than the old amount.");
			if (newamount < game.left - 1) {
				this.modlog(`GTS GIVEAWAY`, null, `set from ${game.left} to ${newamount} left`);
			}

			game.updateLeft(newamount);
		},
		sent(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			const game = this.requireGame(GTS, true);
			if (!user.can('warn', null, room) && user !== game.giver) {
				return this.errorReply("Only the host or a staff member can update GTS giveaways.");
			}

			if (!target || target.length > 12) return this.errorReply("Please enter a valid IGN.");

			game.updateSent(target);
		},
		full(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			const game = this.requireGame(GTS, true);
			if (!user.can('warn', null, room) && user !== game.giver) {
				return this.errorReply("Only the host or a staff member can update GTS giveaways.");
			}
			if (game.noDeposits) return this.errorReply("The GTS giveaway was already set to not accept deposits.");

			game.stopDeposits();
		},
		end(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			const game = this.requireGame(GTS, true);
			this.checkCan('warn', null, room);

			if (target && target.length > 300) {
				return this.errorReply("The reason is too long. It cannot exceed 300 characters.");
			}
			const amount = game.end(true);
			if (target) target = `: ${target}`;
			this.modlog('GTS END', null, `with ${amount} left${target}`);
			this.privateModAction(`The giveaway was forcibly ended by ${user.name} with ${amount} left${target}`);
		},
	},
	gtshelp: [
		`GTS giveaways are currently disabled. If you are a Room Owner and would like them to be re-enabled, contact dhelmise.`,
	],
	ga: 'giveaway',
	giveaway: {
		help: '',
		''() {
			this.runBroadcast();
			this.run('giveawayhelp');
		},
		view: {
			''(target, room, user) {
				this.room = room = Rooms.search('wifi') || null;
				if (!room) throw new Chat.ErrorMessage(`The Wi-Fi room doesn't exist on this server.`);
				this.checkCan('warn', null, room);
				this.parse(`/j view-giveaways-default`);
			},
			stored(target, room, user) {
				this.room = room = Rooms.search('wifi') || null;
				if (!room) throw new Chat.ErrorMessage(`The Wi-Fi room doesn't exist on this server.`);
				this.checkCan('warn', null, room);
				this.parse(`/j view-giveaways-stored`);
			},
			submitted(target, room, user) {
				this.room = room = Rooms.search('wifi') || null;
				if (!room) throw new Chat.ErrorMessage(`The Wi-Fi room doesn't exist on this server.`);
				this.checkCan('warn', null, room);
				this.parse(`/j view-giveaways-submitted`);
			},
		},
		rm: 'remind',
		remind(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			this.runBroadcast();
			if (room.getGame(QuestionGiveaway)) {
				const game = room.getGame(QuestionGiveaway)!;
				if (game.phase !== 'started') {
					throw new Chat.ErrorMessage(`The giveaway has not started yet.`);
				}
				game.send(game.generateQuestion());
			} else if (room.getGame(LotteryGiveaway)) {
				room.getGame(LotteryGiveaway)!.display();
			} else {
				throw new Chat.ErrorMessage(`There is no giveaway going on right now.`);
			}
		},
		leavelotto: 'join',
		leavelottery: 'join',
		leave: 'join',
		joinlotto: 'join',
		joinlottery: 'join',
		join(target, room, user, conn, cmd) {
			room = this.requireRoom('wifi' as RoomID);
			this.checkChat();
			if (user.semilocked) return;
			const giveaway = this.requireGame(LotteryGiveaway);
			if (cmd.includes('join')) {
				giveaway.addUser(user);
			} else {
				giveaway.removeUser(user);
			}
		},
		monthban: 'ban',
		permaban: 'ban',
		ban(target, room, user, connection, cmd) {
			if (!target) return false;
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('warn', null, room);

			const { targetUser, rest: reason } = this.requireUser(target, { allowOffline: true });
			if (reason.length > 300) {
				return this.errorReply("The reason is too long. It cannot exceed 300 characters.");
			}
			if (Punishments.hasRoomPunishType(room, targetUser.name, 'GIVEAWAYBAN')) {
				return this.errorReply(`User '${targetUser.name}' is already giveawaybanned.`);
			}

			const duration = cmd === 'monthban' ? 30 * DAY : cmd === 'permaban' ? 3650 * DAY : 7 * DAY;
			Giveaway.ban(room, targetUser, reason, duration);

			(room.getGame(LotteryGiveaway) || room.getGame(QuestionGiveaway))?.kickUser(targetUser);

			const action = cmd === 'monthban' ? 'MONTHGIVEAWAYBAN' : cmd === 'permaban' ? 'PERMAGIVEAWAYBAN' : 'GIVEAWAYBAN';
			this.modlog(action, targetUser, reason);
			const reasonMessage = reason ? ` (${reason})` : ``;
			const durationMsg = cmd === 'monthban' ? ' for a month' : cmd === 'permaban' ? ' permanently' : '';
			this.privateModAction(`${targetUser.name} was banned from entering giveaways${durationMsg} by ${user.name}.${reasonMessage}`);
		},
		unban(target, room, user) {
			if (!target) return false;
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('warn', null, room);

			const { targetUser } = this.requireUser(target, { allowOffline: true });
			if (!Giveaway.checkBanned(room, targetUser)) {
				return this.errorReply(`User '${targetUser.name}' isn't banned from entering giveaways.`);
			}

			Giveaway.unban(room, targetUser);
			this.privateModAction(`${targetUser.name} was unbanned from entering giveaways by ${user.name}.`);
			this.modlog('GIVEAWAYUNBAN', targetUser, null, { noip: 1, noalts: 1 });
		},
		new: 'create',
		start: 'create',
		create: {
			''(target, room, user) {
				room = this.requireRoom('wifi' as RoomID);
				if (!user.can('show', null, room)) this.checkCan('warn', null, room);
				this.parse('/j view-giveaways-create');
			},
			question(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				if (room.game) {
					throw new Chat.ErrorMessage(`There is already a room game (${room.game.constructor.name}) going on.`);
				}
				// Syntax: giver|ot|tid|game|question|answer1,answer2,etc|ivs/format/like/this|pokeball|packed set
				const {
					targetUser, ot, tid, game, question, answers, ivs, ball, extraInfo, prize,
				} = QuestionGiveaway.splitTarget(target, '|', this, user, 'create');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				room.game = new QuestionGiveaway(user, targetUser, room, ot, tid, game, ivs, set, question, answers, ball, extraInfo);

				this.privateModAction(`${user.name} started a question giveaway for ${targetUser.name}.`);
				this.modlog('QUESTION GIVEAWAY', null, `for ${targetUser.getLastId()} (OT: ${ot} TID: ${tid} Nature: ${(room.game as LotteryGiveaway).prize.nature} Ball: ${ball}${extraInfo ? ` Other box info: ${extraInfo}` : ''})`);
			},
			lottery(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				if (room.game) throw new Chat.ErrorMessage(`There is already a room game (${room.game.constructor.name}) going on.`);
				// Syntax: giver|ot|tid|game|# of winners|ivs/like/this|pokeball|info|packed set
				const {
					targetUser, ot, tid, game, winners, ivs, ball, prize, extraInfo,
				} = LotteryGiveaway.splitTarget(target, '|', this, user, 'create');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				room.game = new LotteryGiveaway(user, targetUser, room, ot, tid, ivs, game, set, winners, ball, extraInfo);

				this.privateModAction(`${user.name} started a lottery giveaway for ${targetUser.name}.`);
				this.modlog('LOTTERY GIVEAWAY', null, `for ${targetUser.getLastId()} (OT: ${ot} TID: ${tid} Nature: ${(room.game as LotteryGiveaway).prize.nature} Ball: ${ball}${extraInfo ? ` Other box info: ${extraInfo}` : ''})`);
			},
		},
		stop: 'end',
		end(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			if (!room.game?.constructor.name.includes('Giveaway')) {
				throw new Chat.ErrorMessage(`There is no giveaway going on at the moment.`);
			}
			const game = room.game as LotteryGiveaway | QuestionGiveaway;
			if (user.id !== game.host.id) this.checkCan('warn', null, room);

			if (target && target.length > 300) {
				return this.errorReply("The reason is too long. It cannot exceed 300 characters.");
			}
			game.end(true);
			this.modlog('GIVEAWAY END', null, target);
			if (target) target = `: ${target}`;
			this.privateModAction(`The giveaway was forcibly ended by ${user.name}${target}`);
		},
		guess(target, room, user) {
			this.parse(`/guess ${target}`);
		},
		changeanswer: 'changequestion',
		changequestion(target, room, user, connection, cmd) {
			room = this.requireRoom('wifi' as RoomID);
			const giveaway = this.requireGame(QuestionGiveaway);
			target = target.trim();
			if (!target) throw new Chat.ErrorMessage("You must include a question or an answer.");
			giveaway.change(target, user, cmd.includes('answer'));
		},
		showanswer: 'viewanswer',
		viewanswer(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			const giveaway = this.requireGame(QuestionGiveaway);
			if (user.id !== giveaway.host.id && user.id !== giveaway.giver.id) return;

			this.sendReply(`The giveaway question is ${giveaway.question}.\nThe answer${Chat.plural(giveaway.answers, 's are', ' is')} ${giveaway.answers.join(', ')}.`);
		},
		save: 'store',
		store: {
			''(target, room, user) {
				room = this.requireRoom('wifi' as RoomID);
				this.checkCan('warn', null, room);
				this.parse('/j view-giveaways-stored-add');
			},
			question(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				const {
					targetUser, ot, tid, game, prize, question, answers, ball, extraInfo, ivs,
				} = QuestionGiveaway.splitTarget(target, '|', this, user, 'store');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				if (!wifiData.storedGiveaways.question) wifiData.storedGiveaways.question = [];
				const data = { targetUserID: targetUser.id, ot, tid, game, prize: set, question, answers, ivs, ball, extraInfo };
				wifiData.storedGiveaways.question.push(data);
				saveData();

				this.privateModAction(`${user.name} saved a question giveaway for ${targetUser.name}.`);
				this.modlog('QUESTION GIVEAWAY SAVE');
			},
			lottery(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				const {
					targetUser, ot, tid, game, prize, winners, ball, extraInfo, ivs,
				} = LotteryGiveaway.splitTarget(target, '|', this, user, 'store');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				if (!wifiData.storedGiveaways.lottery) wifiData.storedGiveaways.lottery = [];
				const data = { targetUserID: targetUser.id, ot, tid, game, prize: set, winners, ball, extraInfo, ivs };
				wifiData.storedGiveaways.lottery.push(data);
				saveData();

				this.privateModAction(`${user.name} saved a lottery giveaway for ${targetUser.name}.`);
				this.modlog('LOTTERY GIVEAWAY SAVE');
			},
		},
		submit: {
			''(target, room, user) {
				room = this.requireRoom('wifi' as RoomID);
				this.checkChat();
				this.parse('/j view-giveaways-submitted-add');
			},
			question(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				const {
					targetUser, ot, tid, game, prize, question, answers, ball, extraInfo, ivs,
				} = QuestionGiveaway.splitTarget(target, '|', this, user, 'submit');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				if (!wifiData.submittedGiveaways.question) wifiData.submittedGiveaways.question = [];
				const data = { targetUserID: targetUser.id, ot, tid, game, prize: set, question, answers, ball, extraInfo, ivs };
				wifiData.submittedGiveaways.question.push(data);
				saveData();

				this.sendReply(`You have submitted a question giveaway for ${set.species}. If you log out or go offline, the giveaway won't go through.`);
				const message = `|tempnotify|pendingapprovals|Pending question giveaway request!` +
					`|${user.name} has requested to start a question giveaway for ${set.species}.|new question giveaway request`;
				room.sendRankedUsers(message, '%');
				room.sendMods(
					Chat.html`|uhtml|giveaway-request-${user.id}|${<div class="infobox">
						{user.name} wants to start a question giveaway for {set.species}<br />
						<button class="button" name="send" value="/j view-giveaways-submitted">View pending giveaways</button>
					</div>}`
				);
			},
			lottery(target, room, user) {
				room = this.room = Rooms.search('wifi') || null;
				if (!room) {
					throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
				}
				const {
					targetUser, ot, tid, game, prize, winners, ball, extraInfo, ivs,
				} = LotteryGiveaway.splitTarget(target, '|', this, user, 'submit');
				const set = Teams.import(prize)?.[0];
				if (!set) throw new Chat.ErrorMessage(`Please submit the prize in the form of a PS set importable.`);

				if (!wifiData.submittedGiveaways.lottery) wifiData.submittedGiveaways.lottery = [];
				const data = { targetUserID: targetUser.id, ot, tid, game, prize: set, winners, ball, extraInfo, ivs };
				wifiData.submittedGiveaways.lottery.push(data);
				saveData();

				this.sendReply(`You have submitted a lottery giveaway for ${set.species}. If you log out or go offline, the giveaway won't go through.`);
				const message = `|tempnotify|pendingapprovals|Pending lottery giveaway request!` +
					`|${user.name} has requested to start a lottery giveaway for ${set.species}.|new lottery giveaway request`;
				room.sendRankedUsers(message, '%');
				room.sendMods(Chat.html`|uhtml|giveaway-request-${user.id}|${<div class="infobox">
					{user.name} wants to start a lottery giveaway for {set.species}<br />
					<button class="button" name="send" value="/j view-giveaways-submitted">View pending giveaways</button>
				</div>}`);
			},
		},
		approve(target, room, user) {
			room = this.room = Rooms.search('wifi') || null;
			if (!room) {
				throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
			}
			const targetUser = Users.get(target);
			if (!targetUser?.connected) {
				this.refreshPage('giveaways-submitted');
				throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} is offline, so their giveaway can't be run.`);
			}
			const hasGiveaway = hasSubmittedGiveaway(targetUser);
			if (!hasGiveaway) {
				this.refreshPage('giveaways-submitted');
				throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} doesn't have any submitted giveaways.`);
			}
			const giveaway = wifiData.submittedGiveaways[hasGiveaway.type][hasGiveaway.index];
			if (hasGiveaway.type === 'question') {
				const data = giveaway as QuestionGiveawayData;
				this.parse(`/giveaway create question ${data.targetUserID}|${data.ot}|${data.tid}|${data.game}|${data.question}|${data.answers.join(',')}|${data.ivs.join('/')}|${data.ball}|${data.extraInfo}|${Teams.pack([data.prize])}`);
			} else {
				const data = giveaway as LotteryGiveawayData;
				this.parse(`/giveaway create lottery ${data.targetUserID}|${data.ot}|${data.tid}|${data.game}|${data.winners}|${data.ivs.join('/')}|${data.ball}|${data.extraInfo}|${Teams.pack([data.prize])}`);
			}
			wifiData.submittedGiveaways[hasGiveaway.type].splice(hasGiveaway.index, 1);
			saveData();
			this.refreshPage(`giveaways-submitted`);
			targetUser.send(`${user.name} has approved your ${hasGiveaway.type} giveaway!`);
			this.privateModAction(`${user.name} approved a ${hasGiveaway.type} giveaway by ${targetUser.name}.`);
			this.modlog(`GIVEAWAY APPROVE ${hasGiveaway.type.toUpperCase()}`, targetUser, null, { noalts: true, noip: true });
		},
		deny: 'delete',
		delete(target, room, user, connection, cmd) {
			room = this.room = Rooms.search('wifi') || null;
			if (!room) {
				throw new Chat.ErrorMessage(`This command must be used in the Wi-Fi room.`);
			}
			if (!target) return this.parse('/help giveaway');
			const del = cmd === 'delete';
			if (del) {
				const [type, indexStr] = target.split(',');
				const index = parseInt(indexStr) - 1;
				if (!type || !indexStr || index <= -1 || !['question', 'lottery'].includes(toID(type)) || isNaN(index)) {
					return this.parse(`/help giveaway`);
				}
				const typedType = toID(type) as 'question' | 'lottery';
				const giveaway = wifiData.storedGiveaways[typedType][index];
				if (!giveaway) {
					throw new Chat.ErrorMessage(
						`There is no giveaway at index ${index}. Indices must be integers between 0 and ${wifiData.storedGiveaways[typedType].length - 1}.`
					);
				}
				wifiData.storedGiveaways[typedType].splice(index, 1);
				saveData();
				this.privateModAction(`${user.name} deleted a ${typedType} giveaway by ${giveaway.targetUserID}.`);
				this.modlog(`GIVEAWAY DELETE ${typedType.toUpperCase()}`);
			} else {
				const { targetUser, rest: reason } = this.splitUser(target);
				if (!targetUser?.connected) {
					throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} is offline, so their giveaway can't be run.`);
				}
				const hasGiveaway = hasSubmittedGiveaway(targetUser);
				if (!hasGiveaway) {
					this.refreshPage('giveaways-submitted');
					throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} doesn't have any submitted giveaways.`);
				}
				wifiData.submittedGiveaways[hasGiveaway.type].splice(hasGiveaway.index, 1);
				saveData();
				targetUser?.send(`Staff have rejected your giveaway${reason ? `: ${reason}` : '.'}`);
				this.privateModAction(`${user.name} denied a ${hasGiveaway.type} giveaway by ${targetUser.name}.`);
				this.modlog(`GIVEAWAY DENY ${hasGiveaway.type.toUpperCase()}`, targetUser, reason || null, { noalts: true, noip: true });
			}
			this.refreshPage(del ? `giveaways-stored` : 'giveaways-submitted');
		},
		unwhitelist: 'whitelist',
		whitelist(target, room, user, connection, cmd) {
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('warn', null, room);
			const targetId = toID(target);
			if (!targetId) return this.parse(`/help giveaway whitelist`);
			if (cmd.includes('un')) {
				const idx = wifiData.whitelist.indexOf(targetId);
				if (idx < 0) {
					return this.errorReply(`'${targetId}' is not whitelisted.`);
				}
				wifiData.whitelist.splice(idx, 1);
				this.privateModAction(`${user.name} removed '${targetId}' from the giveaway whitelist.`);
				this.modlog(`GIVEAWAY UNWHITELIST`, targetId);
				saveData();
			} else {
				if (wifiData.whitelist.includes(targetId)) {
					return this.errorReply(`'${targetId}' is already whitelisted.`);
				}
				wifiData.whitelist.push(targetId);
				this.privateModAction(`${user.name} added ${targetId} to the giveaway whitelist.`);
				this.modlog(`GIVEAWAY WHITELIST`, targetId);
				saveData();
			}
		},
		whitelisthelp: [
			`/giveaway whitelist [user] - Allow the given [user] to make giveaways without staff help. Requires: % @ # ~`,
			`/giveaway unwhitelist [user] - Remove the given user from the giveaway whitelist. Requires: % @ # ~`,
		],
		whitelisted(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('warn', null, room);
			const buf = [<strong>Currently whitelisted users</strong>, <br />];
			if (!wifiData.whitelist.length) {
				buf.push(<div class="message-error">None.</div>);
			} else {
				buf.push(wifiData.whitelist.map(n => <username>{n}</username>));
			}
			this.sendReplyBox(<>{buf}</>);
		},
		claim(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('mute', null, room);
			const { targetUser } = this.requireUser(target);
			const hasGiveaway = hasSubmittedGiveaway(targetUser);
			if (!hasGiveaway) {
				this.refreshPage('giveaways-submitted');
				throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} doesn't have any submitted giveaways.`);
			}
			// we ensure it exists above
			const giveaway = wifiData.submittedGiveaways[hasGiveaway.type][hasGiveaway.index];
			if (giveaway.claimed) throw new Chat.ErrorMessage(`That giveaway is already claimed by ${giveaway.claimed}.`);
			giveaway.claimed = user.id;
			Chat.refreshPageFor('giveaways-submitted', room);
			this.privateModAction(`${user.name} claimed ${targetUser.name}'s giveaway`);
			saveData();
		},
		unclaim(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			this.checkCan('mute', null, room);
			const { targetUser } = this.requireUser(target);
			const hasGiveaway = hasSubmittedGiveaway(targetUser);
			if (!hasGiveaway) {
				this.refreshPage('giveaways-submitted');
				throw new Chat.ErrorMessage(`${targetUser?.name || toID(target)} doesn't have any submitted giveaways.`);
			}
			// we ensure it exists above
			const giveaway = wifiData.submittedGiveaways[hasGiveaway.type][hasGiveaway.index];
			if (!giveaway.claimed) throw new Chat.ErrorMessage(`That giveaway is not claimed.`);
			delete giveaway.claimed;
			Chat.refreshPageFor('giveaways-submitted', room);
			saveData();
		},
		count(target, room, user) {
			room = this.requireRoom('wifi' as RoomID);
			if (!Dex.species.get(target).exists) {
				throw new Chat.ErrorMessage(`No Pok\u00e9mon entered. Proper syntax: /giveaway count pokemon`);
			}
			target = Dex.species.get(target).id;
			this.runBroadcast();

			const count = wifiData.stats[target];

			if (!count) return this.sendReplyBox("This Pokémon has never been given away.");
			const recent = count.filter(val => val + RECENT_THRESHOLD > Date.now()).length;

			this.sendReplyBox(`This Pokémon has been given away ${Chat.count(count, "times")}, a total of ${Chat.count(recent, "times")} in the past month.`);
		},
	},
	giveawayhelp(target, room, user) {
		room = this.requireRoom('wifi' as RoomID);
		this.runBroadcast();
		const buf = [];
		if (user.can('show', null, room)) {
			buf.push(<details><summary>Staff commands</summary>
				<code>/giveaway create</code> - Pulls up a page to create a giveaway. Requires: + % @ # ~<br />
				<code>
					/giveaway create question Giver | OT | TID | Game | Question | Answer 1, Answer 2, Answer 3 | IV/IV/IV/IV/IV/IV | Pok&eacute; Ball | Extra Info | Prize
				</code> - Start a new question giveaway (voices can only host their own). Requires: + % @ # ~<br />
				<code>
					/giveaway create lottery Giver | OT | TID | Game | # of Winners | IV/IV/IV/IV/IV/IV | Pok&eacute; Ball | Extra Info | Prize
				</code> - Start a new lottery giveaway (voices can only host their own). Requires: + % @ # ~<br />
				<code>
					/giveaway changequestion/changeanswer
				</code> - Changes the question/answer of a question giveaway. Requires: Being giveaway host<br />
				<code>/giveaway viewanswer</code> - Shows the answer of a question giveaway. Requires: Being giveaway host/giver<br />
				<code>
					/giveaway ban [user], [reason]
				</code> - Temporarily bans [user] from entering giveaways. Requires: % @ # ~<br />
				<code>/giveaway end</code> - Forcibly ends the current giveaway. Requires: % @ # ~<br />
				<code>/giveaway count [pokemon]</code> - Shows how frequently a certain Pok&eacute;mon has been given away.<br />
				<code>/giveaway whitelist [user]</code> - Allow the given [user] to make giveaways. Requires: % @ # ~<br />
				<code>/giveaway unwhitelist [user]</code> - Remove the given user from the giveaway whitelist. Requires: % @ # ~
			</details>);
		}
		// Giveaway stuff
		buf.push(<details><summary>Giveaway participation commands</summary>
			<code>/guess [target]</code> - Guesses an answer for a question giveaway.<br />
			<code>
				/giveaway submit
			</code> - Allows users to submit giveaways. They must remain online after submitting for it to go through.<br />
			<code>/giveaway viewanswer</code> - Guesses an answer for a question giveaway. Requires: Giveaway host/giver<br />
			<code>/giveaway remind</code> - Shows the details of the current giveaway.<br />
			<code>/giveaway join/leave</code> - Joins/leaves a lottery giveaway.
		</details>);
		this.sendReplyBox(<>{buf}</>);
	},
};

function makePageHeader(user: User, pageid?: string) {
	const titles: { [k: string]: string } = {
		create: `Create`,
		stored: `View Stored`,
		'stored-add': 'Store',
		submitted: `View Submitted`,
		'submitted-add': `Submit`,
	};
	const icons: Record<keyof typeof titles, Chat.VNode> = {
		create: <i class="fa fa-sticky-note"></i>,
		stored: <i class="fa fa-paste"></i>,
		'stored-add': <i class="fa fa-paste"></i>,
		submitted: <i class="fa fa-inbox"></i>,
		'submitted-add': <i class="fa fa-inbox"></i>,
	};
	const buf = [];
	buf.push(<button
		class="button" style={{ float: 'right' }} name="send"
		value={`/j view-giveaways${pageid?.trim() ? `-${pageid.trim()}` : ''}`}
	>
		<i class="fa fa-refresh"></i> Refresh
	</button>);
	buf.push(<h1>Wi-Fi Giveaways</h1>);
	const urls = [];
	const room = Rooms.get('wifi')!; // we validate before using that wifi exists
	for (const i in titles) {
		if (urls.length) urls.push(' / ');
		if (!user.can('mute', null, room) && i !== 'submitted-add') {
			continue;
		}
		const title = titles[i];
		const icon = icons[i];
		if (pageid === i) {
			urls.push(<>{icon} <strong>{title}</strong></>);
		} else {
			urls.push(<>{icon} <a href={`/view-giveaways-${i}`} target="replace">{title}</a></>);
		}
	}
	buf.push(<>{[urls]}</>, <hr />);
	return <center>{buf}</center>;
}

function formatFakeButton(url: string, text: Chat.VNode): Chat.VNode {
	return <a class="button" style={{ textDecoration: 'inherit' }} target="replace" href={url}>{text}</a>;
}

function generatePokeballDropdown() {
	const pokeballs = Dex.items.all().filter(item => item.isPokeball).sort((a, b) => a.num - b.num);
	const pokeballsObj = [<option value="">Please select a Pok&eacute; Ball</option>];
	for (const pokeball of pokeballs) {
		pokeballsObj.push(<option value={pokeball.id}>{pokeball.name}</option>);
	}
	return <><label for="ball">Pok&eacute; Ball type: </label><select name="ball">{pokeballsObj}</select></>;
}

export const pages: Chat.PageTable = {
	giveaways: {
		''() {
			this.title = `[Giveaways]`;
			if (!Rooms.search('wifi')) return <h1>There is no Wi-Fi room on this server.</h1>;
			this.checkCan('warn', null, Rooms.search('wifi')!);
			return <div class="pad">{makePageHeader(this.user)}</div>;
		},
		create(args, user) {
			this.title = `[Create Giveaways]`;
			const wifi = Rooms.search('wifi');
			if (!wifi) return <h1>There is no Wi-Fi room on this server.</h1>;
			if (!(user.can('show', null, wifi) || wifiData.whitelist.includes(user.id))) {
				this.checkCan('warn', null, wifi);
			}
			const [type] = args;
			return <div class="pad">{makePageHeader(this.user, 'create')}{(() => {
				if (!type || !['lottery', 'question'].includes(type)) {
					return <center>
						<h2>Pick a Giveaway type</h2>
						{
							formatFakeButton(`/view-giveaways-create-lottery`, <><i class="fa fa-random"></i> Lottery</>)
						} | {
							formatFakeButton(`/view-giveaways-create-question`, <><i class="fa fa-question"></i> Question</>)
						}
					</center>;
				}
				switch (type) {
				case 'lottery':
					return <>
						<h2>Make a Lottery Giveaway</h2>
						<form data-submitsend="/giveaway create lottery {giver}|{ot}|{tid}|{game}|{winners}|{ivs}|{ball}|{info}|{set}">
							<label for="giver">Giver: </label><input name="giver" /><br /><br />
							<label for="ot">OT: </label><input name="ot" /><br /><br />
							<label for="tid">TID: </label><input name="tid" /><br /><br />
							Game: <div>
								<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
								<input type="radio" id="swsh" name="game" value="swsh" /><label for="swsh">SwSh</label>
								<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
							</div><br />
							<label for="winners">Number of winners: </label><input name="winners" /><br /><br />
							{generatePokeballDropdown()}<br /><br />
							<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
							<label for="set">Prize:</label><br />
							<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable" name="set"></textarea>
							<br /><br />
							<label for="info">Additional information (if any):</label><br />
							<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
							<br /><br />
							<button class="button" type="submit">Create Lottery Giveaway</button>
						</form>
					</>;
				case 'question':
					return <>
						<h2>Make a Question Giveaway</h2>
						<form
							data-submitsend="/giveaway create question {giver}|{ot}|{tid}|{game}|{question}|{answers}|{ivs}|{ball}|{info}|{set}"
						>
							<label for="giver">Giver:</label><input name="giver" /><br /><br />
							<label for="ot">OT:</label><input name="ot" /><br /><br />
							<label for="tid">TID:</label><input name="tid" /><br /><br />
							Game: <div>
								<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
								<input type="radio" id="swsh" name="game"value="swsh" /><label for="swsh">SwSh</label>
								<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
							</div><br />
							<label for="question">Question:</label><input name="question" /><br /><br />
							<label for="answers">Answers (separated by comma):</label><input name="answers" /><br /><br />
							{generatePokeballDropdown()}<br /><br />
							<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
							<label for="set"></label>
							<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable here" name="set"></textarea>
							<br /><br />
							<label for="info">Additional information (if any):</label><br />
							<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
							<br /><br />
							<button class="button" type="submit">Create Question Giveaway</button>
						</form>
					</>;
				}
			})()}</div>;
		},
		stored(args, user) {
			this.title = `[Stored Giveaways]`;
			if (!Rooms.search('wifi')) return <h1>There is no Wi-Fi room on this server.</h1>;
			this.checkCan('warn', null, Rooms.search('wifi')!);
			const [add, type] = args;
			const giveaways = [
				...(wifiData.storedGiveaways?.lottery || []),
				...(wifiData.storedGiveaways?.question || []),
			];
			const adding = add === 'add';
			if (!giveaways.length && !adding) {
				return <div class="pad">
					{makePageHeader(this.user, adding ? 'stored-add' : 'stored')}
					<h2>There are no giveaways stored</h2>
				</div>;
			}
			return <div class="pad">
				{makePageHeader(this.user, adding ? 'stored-add' : 'stored')}
				{(() => {
					if (!adding) {
						const buf = [];
						for (let giveaway of giveaways) {
							if (wifiData.storedGiveaways.lottery.includes(giveaway as LotteryGiveawayData)) {
								giveaway = giveaway as LotteryGiveawayData;
								const targetUser = Users.get(giveaway.targetUserID);
								buf.push(<div class="infobox">
									<h3 style={{ textAlign: 'center' }}>Lottery</h3>
									<hr />
									<strong>Game:</strong> {gameName[giveaway.game]}<br />
									<strong>Giver:</strong> {giveaway.targetUserID}, {}
									<strong>OT:</strong> {giveaway.ot}, <strong>TID:</strong> {giveaway.tid}<br />
									<strong># of winners:</strong> {giveaway.winners}<br />
									<strong>Pok&eacute; Ball:</strong> <psicon item={giveaway.ball} />
									<details>
										<summary><psicon pokemon={giveaway.prize.species} /> Prize</summary>
										<Chat.JSX.FormatText isTrusted>{Giveaway.convertIVs(giveaway.prize, giveaway.ivs)}</Chat.JSX.FormatText>
									</details>
									{!!giveaway.extraInfo?.trim() && <>
										<hr />
										<details>
											<summary>Extra Info</summary>
											<Chat.JSX.FormatText isTrusted>{giveaway.extraInfo.trim()}</Chat.JSX.FormatText>
										</details>
									</>}
									<hr />
									<button
										class="button" name="send" value={
											`/giveaway delete lottery,${wifiData.storedGiveaways.lottery.indexOf(giveaway) + 1}`
										}
									><i class="fa fa-trash"></i> Delete giveaway</button>
									{!targetUser?.connected ? (
										<button title="The giver is offline" disabled class="button disabled" style={{ float: 'right' }}>
											Create giveaway
										</button>
									) : (
										<button
											class="button" style={{ float: 'right' }} name="send" value={
												`/giveaway create lottery ${giveaway.targetUserID}|${giveaway.ot}|${giveaway.tid}|${giveaway.game}|${giveaway.winners}|${giveaway.ivs.join('/')}|${giveaway.ball}|${giveaway.extraInfo.trim().replace(/\n/g, '<br />')}|${Teams.pack([giveaway.prize])}`
											}
										>Create giveaway</button>
									)}
								</div>);
							} else {
								giveaway = giveaway as QuestionGiveawayData;
								const targetUser = Users.get(giveaway.targetUserID);
								buf.push(<div class="infobox">
									<h3 style={{ textAlign: 'center' }}>Lottery</h3>
									<hr />
									<strong>Game:</strong> {gameName[giveaway.game]}<br />
									<strong>Giver:</strong> {giveaway.targetUserID}, {}
									<strong>OT:</strong> {giveaway.ot}, <strong>TID:</strong> {giveaway.tid}<br />
									<strong>Question:</strong> {giveaway.question}<br />
									<strong>Answer{Chat.plural(giveaway.answers.length, "s")}:</strong> {giveaway.answers.join(', ')}<br />
									<strong>Pok&eacute; Ball:</strong> <psicon item={giveaway.ball} />
									<details>
										<summary><psicon pokemon={giveaway.prize.species} /> Prize</summary>
										<Chat.JSX.FormatText isTrusted>{Giveaway.convertIVs(giveaway.prize, giveaway.ivs)}</Chat.JSX.FormatText>
									</details>
									{!!giveaway.extraInfo?.trim() && <>
										<hr />
										<details>
											<summary>Extra Info</summary>
											<Chat.JSX.FormatText isTrusted>{giveaway.extraInfo.trim()}</Chat.JSX.FormatText>
										</details>
									</>}
									<hr />
									<button
										class="button" name="send"
										value={`/giveaway delete question,${wifiData.storedGiveaways.question.indexOf(giveaway) + 1}`}
									>
										<i class="fa fa-trash"></i> Delete giveaway
									</button>
									{!targetUser?.connected ? (
										<button title="The giver is offline" disabled class="button disabled" style={{ float: 'right' }}>
											Create giveaway
										</button>
									) : (
										<button
											class="button" style={{ float: 'right' }} name="send" value={
												`/giveaway create question ${giveaway.targetUserID}|${giveaway.ot}|${giveaway.tid}|${giveaway.game}|${giveaway.question}|${giveaway.answers.join(',')}|${giveaway.ivs.join('/')}|${giveaway.ball}|${giveaway.extraInfo.trim().replace(/\n/g, '<br />')}|${Teams.pack([giveaway.prize])}`
											}
										>Create giveaway</button>
									)}
								</div>);
							}
						}
						return <><h2>Stored Giveaways</h2>{buf}</>;
					} else {
						return <><h2>Store a Giveaway</h2>
							{(() => {
								if (!type || !['question', 'lottery'].includes(type)) {
									return <center>
										<h3>Pick a giveaway type</h3>
										{
											formatFakeButton(`/view-giveaways-stored-add-lottery`, <><i class="fa fa-random"></i> Lottery</>)
										} | {
											formatFakeButton(`/view-giveaways-stored-add-question`, <><i class="fa fa-question"></i> Question</>)
										}
									</center>;
								}
								switch (type) {
								case 'lottery':
									return <form data-submitsend="/giveaway store lottery {giver}|{ot}|{tid}|{game}|{winners}|{ivs}|{ball}|{info}|{set}">
										<label for="giver">Giver: </label><input name="giver" /><br /><br />
										<label for="ot">OT: </label><input name="ot" /><br /><br />
										<label for="tid">TID: </label><input name="tid" /><br /><br />
										Game: <div>
											<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
											<input type="radio" id="swsh" name="game"value="swsh" /><label for="swsh">SwSh</label>
											<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
										</div><br />
										<label for="winners">Number of winners: </label><input name="winners" /><br /><br />
										{generatePokeballDropdown()}<br /><br />
										<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
										<label for="set">Prize:</label><br />
										<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable" name="set"></textarea>
										<br /><br />
										<label for="info">Additional information (if any):</label><br />
										<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
										<br /><br />
										<button class="button" type="submit">Store Lottery Giveaway</button>
									</form>;
								case 'question':
									return <form
										data-submitsend="/giveaway store question {giver}|{ot}|{tid}|{game}|{question}|{answers}|{ivs}|{ball}|{info}|{set}"
									>
										<label for="giver">Giver:</label><input name="giver" /><br /><br />
										<label for="ot">OT:</label><input name="ot" /><br /><br />
										<label for="tid">TID:</label><input name="tid" /><br /><br />
										Game: <div>
											<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
											<input type="radio" id="swsh" name="game"value="swsh" /><label for="swsh">SwSh</label>
											<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
										</div><br />
										<label for="question">Question:</label><input name="question" /><br /><br />
										<label for="answers">Answers (separated by comma):</label><input name="answers" /><br /><br />
										{generatePokeballDropdown()}<br /><br />
										<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
										<label for="set"></label>
										<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable here" name="set"></textarea>
										<br /><br />
										<label for="info">Additional information (if any):</label><br />
										<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
										<br /><br />
										<button class="button" type="submit">Store Question Giveaway</button>
									</form>;
								}
							})()}
						</>;
					}
				})()}
			</div>;
		},
		submitted(args, user) {
			this.title = `[Submitted Giveaways]`;
			if (!Rooms.search('wifi')) return <h1>There is no Wi-Fi room on this server.</h1>;
			const [add, type] = args;
			const adding = add === 'add';
			if (!adding) this.checkCan('warn', null, Rooms.get('wifi')!);
			const giveaways = [
				...(wifiData.submittedGiveaways?.lottery || []),
				...(wifiData.submittedGiveaways?.question || []),
			];
			if (!giveaways.length && !adding) {
				return <div class="pad">
					{makePageHeader(this.user, args[0] === 'add' ? 'submitted-add' : 'submitted')}
					<h2>There are no submitted giveaways.</h2>
				</div>;
			}
			return <div class="pad">
				{makePageHeader(this.user, args[0] === 'add' ? 'submitted-add' : 'submitted')}
				{(() => {
					if (!adding) {
						const buf = [];
						for (let giveaway of giveaways) {
							const claimCmd = giveaway.claimed === user.id ?
								`/giveaway unclaim ${giveaway.targetUserID}` :
								`/giveaway claim ${giveaway.targetUserID}`;
							const claimedTitle = giveaway.claimed === user.id ?
								"Unclaim" : giveaway.claimed ?
									`Claimed by ${giveaway.claimed}` : `Claim`;
							const disabled = giveaway.claimed && giveaway.claimed !== user.id ? " disabled" : "";
							buf.push(<div class="infobox">
								{(() => {
									if (wifiData.submittedGiveaways.lottery.includes(giveaway as LotteryGiveawayData)) {
										giveaway = giveaway as LotteryGiveawayData;
										return <>
											<h3 style={{ textAlign: 'center' }}>Lottery</h3>
											<hr />
											<strong>Game:</strong> {gameName[giveaway.game]}, <strong>Giver:</strong> {giveaway.targetUserID}, {}
											<strong>OT:</strong> {giveaway.ot}, <strong>TID:</strong> {giveaway.tid}, {}
											<strong># of winners:</strong> {giveaway.winners}
											{!!giveaway.claimed && <><br /><strong>Claimed:</strong> {giveaway.claimed}</>}<br />
											<strong>Pok&eacute; Ball:</strong> <psicon item={giveaway.ball} />
											<details>
												<summary><psicon pokemon={giveaway.prize.species} /> Prize</summary>
												<Chat.JSX.FormatText isTrusted>{Giveaway.convertIVs(giveaway.prize, giveaway.ivs)}</Chat.JSX.FormatText>
											</details>
											{!!giveaway.extraInfo?.trim() && <>
												<hr />
												<details>
													<summary>Extra Info</summary>
													<Chat.JSX.FormatText isTrusted>{giveaway.extraInfo.trim()}</Chat.JSX.FormatText>
												</details>
											</>}
										</>;
									} else {
										giveaway = giveaway as QuestionGiveawayData;
										return <>
											<h3 style={{ textAlign: 'center' }}>Question</h3>
											<hr />
											<strong>Game:</strong> {gameName[giveaway.game]}, <strong>Giver:</strong> {giveaway.targetUserID}, {}
											<strong>OT:</strong> {giveaway.ot}, <strong>TID:</strong> {giveaway.tid}
											{!!giveaway.claimed && <><br /><strong>Claimed:</strong> {giveaway.claimed}</>}<br />
											<strong>Question:</strong> {giveaway.question}<br />
											<strong>Answer{Chat.plural(giveaway.answers.length, "s")}:</strong> {giveaway.answers.join(', ')}<br />
											<strong>Pok&eacute; Ball:</strong> <psicon item={giveaway.ball} />
											<details>
												<summary><psicon pokemon={giveaway.prize.species} /> Prize</summary>
												<Chat.JSX.FormatText isTrusted>{Giveaway.convertIVs(giveaway.prize, giveaway.ivs)}</Chat.JSX.FormatText>
											</details>
											{!!giveaway.extraInfo?.trim() && <>
												<hr />
												<details>
													<summary>Extra Info</summary>
													<Chat.JSX.FormatText isTrusted>{giveaway.extraInfo.trim()}</Chat.JSX.FormatText>
												</details>
											</>}
										</>;
									}
								})()}
								<hr />
								{!Users.get(giveaway.targetUserID)?.connected ? <>
									<button title="The giver is offline" class="button disabled" disabled>
										<i class="fa fa-times-circle"></i> Deny giveaway
									</button>
									<button style={{ textAlign: 'center' }} class={`button${disabled}`} name="send" value={`/msgroom wifi,${claimCmd}`}>
										{claimedTitle}
									</button>
									<button title="The giver is offline" disabled class="button disabled" style={{ float: 'right' }}>
										Create giveaway
									</button>
								</> : <>
									<button class="button" name="send" value={`/giveaway deny ${giveaway.targetUserID}`}>
										<i class="fa fa-times-circle"></i> Deny giveaway
									</button>
									<button style={{ textAlign: 'center' }} class={`button${disabled}`} name="send" value={`/msgroom wifi,${claimCmd}`}>
										{claimedTitle}
									</button>
									<button class="button" style={{ float: 'right' }} name="send" value={`/giveaway approve ${giveaway.targetUserID}`}>
										Create giveaway
									</button>
								</>}
							</div>);
						}
						return <><h2>Submitted Giveaways</h2>{buf}</>;
					} else {
						return <>
							<h2>Submit a Giveaway</h2>
							{(() => {
								if (!type || !['question', 'lottery'].includes(type)) {
									return <center>
										<h3>Pick a giveaway type</h3>
										{
											formatFakeButton(`/view-giveaways-submitted-add-lottery`, <><i class="fa fa-random"></i> Lottery</>)
										} | {
											formatFakeButton(`/view-giveaways-submitted-add-question`, <><i class="fa fa-question"></i> Question</>)
										}
									</center>;
								}
								switch (type) {
								case 'lottery':
									return <form data-submitsend="/giveaway submit lottery {giver}|{ot}|{tid}|{game}|{winners}|{ivs}|{ball}|{info}|{set}">
										<label for="giver">Giver: </label><input name="giver" /><br /><br />
										<label for="ot">OT: </label><input name="ot" /><br /><br />
										<label for="tid">TID: </label><input name="tid" /><br /><br />
										Game: <div>
											<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
											<input type="radio" id="swsh" name="game"value="swsh" /><label for="swsh">SwSh</label>
											<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
										</div><br />
										<label for="winners">Number of winners: </label><input name="winners" /><br /><br />
										{generatePokeballDropdown()}<br /><br />
										<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
										<label for="set">Prize:</label><br />
										<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable" name="set"></textarea>
										<br /><br />
										<label for="info">Additional information (provide a link of proof here):</label><br />
										<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
										<br /><br />
										<button class="button" type="submit">Submit Lottery Giveaway</button>
									</form>;
								case 'question':
									return <form
										data-submitsend="/giveaway submit question {giver}|{ot}|{tid}|{game}|{question}|{answers}|{ivs}|{ball}|{info}|{set}"
									>
										<label for="giver">Giver:</label><input name="giver" /><br /><br />
										<label for="ot">OT:</label><input name="ot" /><br /><br />
										<label for="tid">TID:</label><input name="tid" /><br /><br />
										Game: <div>
											<input type="radio" id="bdsp" name="game" value="bdsp" /><label for="bdsp">BDSP</label>
											<input type="radio" id="swsh" name="game"value="swsh" /><label for="swsh">SwSh</label>
											<input type="radio" id="sv" name="game" value="sv" checked /><label for="sv">SV</label>
										</div><br />
										<label for="question">Question:</label><input name="question" /><br /><br />
										<label for="answers">Answers (separated by comma):</label><input name="answers" /><br /><br />
										{generatePokeballDropdown()}<br /><br />
										<label for="ivs">IVs (Formatted like "1/30/31/X/HT/30"): </label><input name="ivs" /><br /><br />
										<label for="set"></label>
										<textarea style={{ width: '70%', height: '300px' }} placeholder="Paste set importable" name="set"></textarea>
										<br /><br />
										<label for="info">Additional information (provide a link of proof here):</label><br />
										<textarea style={{ width: '50%', height: '100px' }} placeholder="Add any additional info" name="info"></textarea>
										<br /><br />
										<button class="button" type="submit">Submit Question Giveaway</button>
									</form>;
								}
							})()}
						</>;
					}
				})()}
			</div>;
		},
	},
};

Chat.multiLinePattern.register(`/giveaway (create|new|start|store|submit|save) (question|lottery) `);