-
Notifications
You must be signed in to change notification settings - Fork 235
/
iquiz.pl
1595 lines (1519 loc) · 71.4 KB
/
iquiz.pl
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
##################################################################
## irssi Quiz (iQuiz) script (2010-2017) by wilk/xorandor ##
##################################################################
## Script inspired by classic mIRC scripts: "Dizzy" by Dizzy, ##
## "Mieszacz" & "Familiada" by snajperx (both with my later ##
## upgrades). ##
## Other credits: ##
## Bjoern 'fuchs' Krombholz for splitlong.pl calculations ##
##################################################################
# Tested more or less with irssi 0.8.15 & 0.8.18 and Perl 5.8.8, 5.10.1, 5.14.2, 5.16.3 & 5.18.2
# Script works with:
#
# - standard "Dizzy" files (also without "pyt"/"odp" prefixes) - used also for "Pomieszany":
#
# pyt Evaluate: 2+2=?
# odp four
# pyt Star closest to Earth?
# odp Sun
# ...
#
# - standard "Mieszacz" files (also without line numbers):
#
# 1 alpha
# 2 beta
# 3 gamma
# 4 delta
# ...
#
# - standard "Familiada" files (can have any number of answers per question, not only 10) - used also for "Multi":
#
# Planets of our Solar System:
# Mercury*Venus*Earth*Mars*Jupiter*Saturn*Uranus*Neptune
# First six alkanes:
# methane*ethane*propane*butane*pentane*hexane
# ...
# >>> To review all available commands and settings type: /quiz OR /help quiz
use strict;
use warnings;
use Irssi qw(theme_register current_theme command_bind settings_add_int settings_add_bool settings_add_str settings_get_int settings_get_bool settings_get_str settings_set_int settings_set_bool settings_set_str printformat timeout_add_once timeout_remove signal_add_last signal_remove signal_stop signal_emit);
use Time::HiRes qw(time);
our $VERSION = '170819';
our %IRSSI = (
authors => 'wilk',
name => 'iQuiz',
description => 'irssi quiz script',
license => 'GNU GPL v3 or any later version',
changed => ($VERSION =~ /^(\d\d)(\d\d)(\d\d)/) ? "20$1-$2-$3" : $VERSION,
url => 'http://iquiz.quizpl.net',
contact => 'http://mail.quizpl.net',
changes => 'see http://www.quizpl.net/viewtopic.php?f=3&t=404',
usage => 'see http://www.quizpl.net/viewtopic.php?f=3&t=587'
);
##### Hardcoded settings #####
my $_start_delay = 5000; # msec; delay between /qon and showing first question (or 0) - to let players concentrate and be ready
my $_bonus_delay = 1000; # msec; time interval to account second fast answer
my $_max_teams = 5; # int; max allowed teams (5 is reasonable)
my $_shuffle_watchdog = 10; # int; max shuffling repetitions to prevent mixed == original, but avoid infinite loop
my $_shuffle_threshold = 3; # int; below this length reshuffling is off (to prevent mixed == original)
my $_randomized_antigoogler = 0; # bool; use better, randomized antigoogler? (will increase question length)
my $_smarter_antigoogler = 1; # bool; use smarter antigoogler? (leaves some empty spaces for better line breaking)
my $_smarter_antigoogler_chunk = 2; # int; leaves empty space every after this many substitutions (for use with $_smarter_antigoogler)
my $_protect_urls = 1; # bool; turn off antigoogler if URL is detected in question?
my $_round_warn_time = 15; # sec; seconds before round end to show warning (0 = off)
my $_round_warn_coeff = 1.5; # float; round duration must be longer than coeff * $_round_warn_time to show warning (protection)
my $_qstats_ranks = 0; # bool; 0: /qstats param corresponds to number of players, 1: /qstats param corresponds to rank
my $_qstats_records = 5; # int; number of time/speed record places in /qstats (0 - off)
my $_no_hints_spam = 1; # bool; do not show fully revealed answer as a hint (except the first)?
my $_flex_separator = '/'; # char; separator used in quiz_flx_* formats
my $_team_separator = ', '; # str; team players separator eq. "nick1, nick2, nick3"
my $_display_delay = 100; # msec; workaround for display issue (response before request)
my $_next_delay = 10; # sec; default delay between questions
my $_next_delay_long = 20; # sec; default delay between questions (fam/mul) (longer delay to prevent flooding and give a breath)
my $_round_duration = 90; # sec; default round duration
my $_hints_interval = 10; # sec; default hints interval
my $_hint_alpha = '.'; # char; default substitution symbol for alphabet characters in hints (special characters are left intact)
my $_hint_digit = '.'; # char; default substitution symbol for digit characters in hints (special characters are left intact)
my $_quiz_types = 5; # (do not change)
##### Internal stuff #####
use constant { QT_STD => 1, QT_MIX => 2, QT_FAM => 3, QT_MUL => 4, QT_SCR => 5 }; # QT_MIL => 6, QT_FOR => 7
use constant { TIME_HMS => 0, TIME_S => 1, TIME_MS => 2 }; # 0: h/m/s, 1: s only, 2: s.ms
use constant { INSTANT => 1, BONUS => 2, WHISPER => 4, PREPDOTS => 1, INT => 1, BOOL => 2, STR => 3 };
my %quiz = (
chan => undef, file => '',
type => 0, tcnt => 0, # we make copies in case someone modifies settings while quiz is running
ison => 0, # /qon ... /qoff (or error while /qreload)
inq => 0, # in question
bonus => 0, # in bonus interval
standby => 0, # /qon QT_FAM ... /qon (/qoff or error in /qreload)
ended => 0, # after last question till /qoff
wantpause => 0, # pause request (/qpause during question)
paused => 0, # paused by /qpause
stime => 0, qtime => 0,
qcnt => 0, qnum => 0, hnum => 0,
score => 0, answers => 0,
lastone => '', # nick!ident@host of last answering player
tnext => undef, tround => undef, thint => undef, thinter => undef, tremind => undef, twarn => undef, tbonus => undef,
hprot => 0, rprot => 0,
data => [], # data[]{question realquestion answer answers{}}
teams => [], # teams[]{score answers}
players => {}, # players{}{nick timestamp score answers team joined besttime alltime bestspeed allspeed}
lookup => {},
dcnt => 0, # total number of currently unrevealed letters
dmax => 0, # number of currently unrevealed letters in the "most hidden" word
dots => [], hwords => [],
ignored => {},
lang => 'pl' # cached setting
);
my %settings_int = (
'quiz_type' => QT_STD,
'quiz_teams' => 2,
'quiz_delay' => $_next_delay,
'quiz_delay_long' => $_next_delay_long,
'quiz_timeout' => $_round_duration,
'quiz_hints_interval' => $_hints_interval,
'quiz_max_hints' => 0,
'quiz_words_case' => 0,
'quiz_anticheat_delay' => 3,
'quiz_first_anticheat_delay' => 7,
'quiz_points_per_answer' => 1,
'quiz_points_per_bonus' => 1,
'quiz_min_points' => 1,
'quiz_max_points' => 50,
'quiz_scoring_mode' => 4,
'quiz_ranking_type' => 3,
);
my %settings_bool = (
'quiz_antigoogler' => 1,
'quiz_autohinter' => 0,
'quiz_split_long_lines' => 1,
'quiz_show_first_hint' => 0,
'quiz_first_hint_dots' => 0,
'quiz_random_hints' => 1,
'quiz_nonrandom_first_hint' => 1,
'quiz_words_mode' => 1,
'quiz_smart_mix' => 1,
'quiz_mix_on_remind' => 1,
'quiz_strict_match' => 1,
'quiz_join_anytime' => 1,
'quiz_team_play' => 1,
'quiz_transfer_points' => 0,
'quiz_limiter' => 0,
'quiz_bonus_answer' => 0,
'quiz_asciize' => 1,
'quiz_keep_scores' => 0,
'quiz_keep_teams' => 1,
'quiz_cmd_hint' => 1,
'quiz_cmd_remind' => 1,
);
my %settings_str = (
'quiz_hint_alpha' => $_hint_alpha,
'quiz_hint_digit' => $_hint_digit,
'quiz_smart_mix_chars' => '\d()",.;:?!',
'quiz_lang' => $quiz{lang},
);
##### Theme (only channel messages are localized by default, feel free to customize here or via /format, except authorship) #####
# quiz_inf_*, quiz_wrn_* & quiz_err_* messages are irssi only - use irssi formatting and irssi color codes
# quiz_msg_* messages are sent on channel - use sprintf formatting and mIRC color codes:
# \002 - bold \003$fg(,$bg)? - color \017 - plain \026 - reverse \037 - underline
# quiz_sfx_* - appended to after some quiz_msg_*
# quiz_inc_* - not sent directly, used as inclusions
# quiz_flx_* - not sent directly, words' inflections
# Important: To prevent visual glitches use two digit color codes, i.e. \00304 instead of \0034
# Comment: In few places I've put two bold codes between color code and comma. Unfortunately some flawed parsers consider this as a background color (even without a valid number value after!) and display a mess.
my $theme = [
'quiz_inf_start', '%_iQuiz:%_ Aby uzyskac pomoc wpisz: %_/quiz%_%:%_iQuiz:%_ For english language please type: /set quiz_lang en',
'quiz_inf_start-en', '%_iQuiz:%_ Type %_/quiz%_ to get help%:%_iQuiz:%_ Aby zmienic jezyk na polski wpisz: /set quiz_lang pl',
'quiz_inf_delay', '%_iQuiz:%_ %gZmieniono opoznienie miedzy pytaniami na: %_$0%_ sek.%n',
'quiz_inf_delay-en', '%_iQuiz:%_ %gChanged delay between questions to: %_$0%_s%n',
'quiz_inf_duration', '%_iQuiz:%_ %gZmieniono czas trwania rundy na: %_$0%_ sek.%n',
'quiz_inf_duration-en', '%_iQuiz:%_ %gChanged round duration to: %_$0%_s%n',
'quiz_inf_type', '%_iQuiz:%_ %gZmieniono tryb gry na: %_$0%_%n',
'quiz_inf_type-en', '%_iQuiz:%_ %gChanged quiz type to: %_$0%_%n',
'quiz_inf_teams', '%_iQuiz:%_ %gZmieniono liczbe druzyn na: %_$0%_%n',
'quiz_inf_teams-en', '%_iQuiz:%_ %gChanged number of teams to: %_$0%_%n',
'quiz_inf_reset', '%_iQuiz:%_ %gWszystkie ustawienia zostaly przywrocone do poczatkowych wartosci%n',
'quiz_inf_reset-en', '%_iQuiz:%_ %gAll settings has been reset to default values%n',
'quiz_inf_reload', '%_iQuiz:%_ %gPlik z pytaniami zostal ponownie wczytany%n',
'quiz_inf_reload-en', '%_iQuiz:%_ %gQuestions\' file reloaded%n',
'quiz_inf_wantpause', '%_iQuiz:%_ %gQuiz zostanie wstrzymany po tym pytaniu%n',
'quiz_inf_wantpause-en', '%_iQuiz:%_ %gQuiz will be paused after current question%n',
'quiz_inf_wontpause', '%_iQuiz:%_ %gWstrzymanie quizu zostalo anulowane%n',
'quiz_inf_wontpause-en', '%_iQuiz:%_ %gQuiz suspension revoked%n',
'quiz_wrn_reload', '%_iQuiz:%_ %YZmienila sie liczba pytan (po ponownym wczytaniu)%n',
'quiz_wrn_reload-en', '%_iQuiz:%_ %YQuestions\' count mismatch after reloading%n',
'quiz_err_ison', '%_iQuiz:%_ %RQuiz jest juz uruchomiony%n',
'quiz_err_ison-en', '%_iQuiz:%_ %RQuiz is already on%n',
'quiz_err_isoff', '%_iQuiz:%_ %RQuiz nie jest jeszcze uruchomiony%n',
'quiz_err_isoff-en', '%_iQuiz:%_ %RQuiz is not started yet%n',
'quiz_err_server', '%_iQuiz:%_ %RBrak polaczenia z serwerem%n',
'quiz_err_server-en', '%_iQuiz:%_ %RNot connected to server%n',
'quiz_err_channel', '%_iQuiz:%_ %RBledna nazwa kanalu%n',
'quiz_err_channel-en', '%_iQuiz:%_ %RInvalid channel%n',
'quiz_err_nochannel', '%_iQuiz:%_ %RKanal "$0" nie jest otwarty%n',
'quiz_err_nochannel-en', '%_iQuiz:%_ %RChannel "$0" is not open%n',
'quiz_err_filename', '%_iQuiz:%_ %RBledna nazwa pliku%n',
'quiz_err_filename-en', '%_iQuiz:%_ %RInvalid filename%n',
'quiz_err_nofile', '%_iQuiz:%_ %RPlik "$0" nie zostal odnaleziony%n',
'quiz_err_nofile-en', '%_iQuiz:%_ %RFile "$0" not found%n',
'quiz_err_file', '%_iQuiz:%_ %RPlik "$0" wydaje sie byc uszkodzony%n',
'quiz_err_file-en', '%_iQuiz:%_ %RFile "$0" seems to be corrupted%n',
'quiz_err_argument', '%_iQuiz:%_ %RBledny parametr polecenia%n',
'quiz_err_argument-en', '%_iQuiz:%_ %RInvalid argument%n',
'quiz_err_noquestion', '%_iQuiz:%_ %RPoczekaj az pytanie zostanie zadane%n',
'quiz_err_noquestion-en', '%_iQuiz:%_ %RWait until question is asked%n',
'quiz_err_type', '%_iQuiz:%_ %RBledny tryb gry%n',
'quiz_err_type-en', '%_iQuiz:%_ %RInvalid quiz type%n',
'quiz_err_delay', '%_iQuiz:%_ %RBledna wartosc opoznienia miedzy pytaniami%n',
'quiz_err_delay-en', '%_iQuiz:%_ %RInvalid delay between questions%n',
'quiz_err_duration', '%_iQuiz:%_ %RBledna wartosc czasu trwania rundy%n',
'quiz_err_duration-en', '%_iQuiz:%_ %RInvalid round duration%n',
'quiz_err_teams', '%_iQuiz:%_ %RBledna liczba druzyn%n',
'quiz_err_teams-en', '%_iQuiz:%_ %RInvalid number of teams%n',
'quiz_err_ranking', '%_iQuiz:%_ %RBledna liczba graczy%n',
'quiz_err_ranking-en', '%_iQuiz:%_ %RInvalid number of players%n',
'quiz_err_nonick', '%_iQuiz:%_ %RNie znajduje nicka "$0" na kanale "$1"%n',
'quiz_err_nonick-en', '%_iQuiz:%_ %RUnable to find nick "$0" on channel "$1"%n',
'quiz_err_nonick_global', '%_iQuiz:%_ %RNie znajduje nicka "$0" na serwerze%n',
'quiz_err_nonick_global-en', '%_iQuiz:%_ %RUnable to find nick "$0" on server%n',
'quiz_err_na', '%_iQuiz:%_ %RTa funkcja jest niedostepna przy obecnych ustawieniach%n',
'quiz_err_na-en', '%_iQuiz:%_ %RThis feature is not available under current settings%n',
'quiz_msg', '%s', # custom text
'quiz_msg_start1', "\00303>>> \00310iQuiz by \002wilk\002 wystartowal \00303<<<",
'quiz_msg_start1-en', "\00303>>> \00310iQuiz by \00304wilk\00310 started \00303<<<",
'quiz_msg_start2', "\00303Polecenia: !podp, !przyp, !ile, !ile nick",
'quiz_msg_start2-en', "\00303Commands: !hint, !remind, !score, !score nick",
'quiz_msg_start2_f', "\00303Polecenia: !przyp, !ile, !ile nick, !join 1-%u", # 1: max teams
'quiz_msg_start2_f-en', "\00303Commands: !remind, !score, !score nick, !join 1-%u",
'quiz_msg_start2_m', "\00303Polecenia: !przyp, !ile, !ile nick",
'quiz_msg_start2_m-en', "\00303Commands: !remind, !score, !score nick",
'quiz_msg_stop1', "\00303>>> \00310iQuiz by \002wilk\002 zakonczony \00303<<<",
'quiz_msg_stop1-en', "\00303>>> \00310iQuiz by \002wilk\002 is over \00303<<<",
'quiz_msg_stop2', "\00303Liczba rund: \00304%u \00303Czas gry: \00304%s", # 1: rounds, 2: time_str (hms)
'quiz_msg_stop2-en', "\00303Rounds: \00304%u \00303Duration: \00304%s", # 1: rounds, 2: time_str (hms)
'quiz_msg_question', "\00308,01\037Pytanie %u/%u:\037%s", # see below
'quiz_msg_question-en', "\00308,01\037Question %u/%u:\037%s",
'quiz_msg_question_x', "\00308,01\037Haslo %u/%u:\037%s", # see below
'quiz_msg_question_x-en', "\00308,01\037Word %u/%u:\037%s",
'quiz_msg_question_fm', "\00308,01\037Pytanie %u/%u:\037%s \00303(\00304%u\00303 %s, czas: %u sek.)", # 1: round, 2: rounds, 3: question (quiz_inc_question), 4: answers, 5: quiz_flx_answers, 6: round time (s)
'quiz_msg_question_fm-en', "\00308,01\037Question %u/%u:\037%s \00303(\00304%u\00303 %s, time: %us)",
'quiz_inc_question', "\00300,02 %s \017", # 1: question (antygoogler takes first color code to harden question - must use background color if using antigoogler; if any color is used finish with "\017" to reset it)
'quiz_msg_hint', "\00303Podpowiedz: \00304%s", # 1: hint
'quiz_msg_hint-en', "\00303Hint: \00304%s",
'quiz_inc_hint_alpha', "\00310%s\00304", # 1: symbol (color codes are used to distinguish between hidden letter and real dot, but you may omit them)
'quiz_inc_hint_digit', "\00310%s\00304", # 1: symbol (same as above)
'quiz_msg_remind', "\00303Przypomnienie: %s", # 1: question (quiz_inc_question)
'quiz_msg_remind-en', "\00303Reminder: %s",
'quiz_msg_delay', "\00303Opoznienie miedzy pytaniami: \00304%u\00303 sek.", # 1: time (s)
'quiz_msg_delay-en', "\00303Delay between questions: \00304%u\00303s",
'quiz_msg_duration', "\00303Czas trwania rundy: \00304%u\00303 sek.", # 1: time (s)
'quiz_msg_duration-en', "\00303Round duration: \00304%u\00303s",
'quiz_msg_paused', "\00303Quiz zostaje \00304wstrzymany\00303 do odwolania.",
'quiz_msg_paused-en', "\00303Quiz is temporarily \00304suspended\00303 now.",
'quiz_msg_skipped', "\00303Pytanie zostalo \00304pominiete\00303.",
'quiz_msg_skipped-en', "\00303This question has been \00304skipped\00303.",
'quiz_msg_score', "\00304%s\00303\002\002, zdobyles(as) jak dotad \00304%d\00303 %s.", # 1: nick, 2: score, 3: quiz_flx_points
'quiz_msg_score-en', "\00304%s\00303\002\002, you have scored \00304%d\00303 %s so far.",
'quiz_msg_noscore', "\00304%s\00303\002\002, nie zdobyles(as) jeszcze zadnego punktu!", # 1: nick
'quiz_msg_noscore-en', "\00304%s\00303\002\002, you haven't scored any point yet!",
'quiz_msg_score_other', "\00304%s\00303 zdobyl(a) jak dotad \00304%d\00303 %s.", # see quiz_msg_score
'quiz_msg_score_other-en', "\00304%s\00303 scored \00304%d\00303 %s so far.",
'quiz_msg_noscore_other', "\00304%s\00303 nie zdobyl(a) jeszcze zadnego punktu!", # 1: nick
'quiz_msg_noscore_other-en', "\00304%s\00303 hasn't scored any point yet!",
'quiz_msg_noscores', "\00303Tablica wynikow jest jeszcze pusta.",
'quiz_msg_noscores-en', "\00303Scoreboard is still empty.",
'quiz_msg_scores', "\00303Wyniki quizu po %s i %u %s:", # 1: time_str (hms), 2: question, 3: quiz_flx_aquestions, 4: questions (total), 5: quiz_flx_fquestions (total)
'quiz_msg_scores-en', "\00303Quiz scores after %s and %u %s:",
'quiz_msg_scores_place', "\00303%u. miejsce: \00304%s\00303 - \00304%d\00303 %s [%.1f%%] (sr. czas zgadywania: %10\$.3f sek.)", # 1: place, 2: nick, 3: score, 4: quiz_flx_points, 5: score%, 6: answers, 7: quiz_flx_answers, 8: answers%, 9: best time, 10: avg time, 11: best speed, 12: avg speed, 13: spacer
'quiz_msg_scores_place-en', "\00303%u. place: \00304%s\00303 - \00304%d\00303 %s [%.1f%%] (avg. guessing time: %10\$.3fs)",
'quiz_msg_scores_place_full', "\00303%u. miejsce: \00304%s\00303 - \00304%d\00303 %s [%.1f%%] (%u %s, sr. czas zgadywania: %10\$.3f sek.)", # see quiz_msg_scores_place
'quiz_msg_scores_place_full-en', "\00303%u. place: \00304%s\00303 - \00304%d\00303 %s [%.1f%%] (%u %s, avg. guessing time: %10\$.3fs)",
'quiz_msg_team_score', "\00303Druzyna %u (%s): \00304%d\00303 %s", # 1: team, 2: players (comma separated), 3: score, 4: quiz_flx_points, 5: score%, 6: answers, 7: quiz_flx_answers, 8: answers%
'quiz_msg_team_score-en', "\00303Team %u (%s): \00304%d\00303 %s",
'quiz_msg_team_score_full', "\00303Druzyna %u (%s): \00304%d\00303 %s (%6\$u %7\$s)", # see quiz_msg_team_score
'quiz_msg_team_score_full-en', "\00303Team %u (%s): \00304%d\00303 %s (%6\$u %7\$s)",
'quiz_msg_team_join', "\00303Dolaczyles(as) do Druzyny %u (%s).", # 1: team, 2: players (comma separated)
'quiz_msg_team_join-en', "\00303You have joined to Team %u (%s).",
'quiz_msg_team_change', "\00303Zmieniles(as) druzyne na Druzyna %u (%s).", # 1: team, 2: players (comma separated)
'quiz_msg_team_change-en', "\00303You have changed your team to Team %u (%s).",
'quiz_inc_team_nick', "\00307%s\00303", # 1: nick
'quiz_msg_scores_times', "\00303Najszybsi (czas): %s", # 1: players (comma separated)
'quiz_msg_scores_times-en', "\00303Fastest players (time): %s",
'quiz_msg_scores_speeds', "\00303Najszybsi (zn/s): %s", # 1: players (comma separated)
'quiz_msg_scores_speeds-en', "\00303Fastest players (ch/s): %s",
'quiz_inc_scores_record', "\00303%u. \00304%s\00303 (%.3f)", # 1: place, 2: nick, 3: time/speed record
'quiz_msg_congrats', "\00303Brawo, \00304%s\00303! Otrzymujesz %s za odpowiedz \00304%s\00303 podana po czasie %.3f sek. (%.3f zn/s) - suma punktow: \00304%d\00303.", # 1: nick, 2: quiz_inc_got_point*, 3: answer, 4: time (ms), 5: speed (chars/s), 6: total score
'quiz_msg_congrats-en', "\00303Congrats, \00304%s\00303! You get %s for an answer \00304%s\00303 given after %.3fs (%.3f chars/s) - total points: \00304%d\00303.",
'quiz_msg_congrats_bonus', "\00303Niezle, \00304%s\00303. Ty rowniez dostajesz %s za swoja odpowiedz podana po czasie %4\$.3f sek. (%5\$.3f zn/s) - suma punktow: \00304%6\$d\00303.", # see above
'quiz_msg_congrats_bonus-en', "\00303Good one, \00304%s\00303! You also get %s for your answer given after %4\$.3fs (%5\$.3f chars/s) - total points: \00304%6\$d\00303.",
'quiz_inc_got_points', "\00304%d\00303 %s", # 1: points, 2: quiz_flx_points
'quiz_inc_got_points-en', "\00304%d\00303 %s",
'quiz_inc_got_point', "\00303%s", # 1: quiz_flx_point
'quiz_inc_got_point-en', "\00303a %s",
'quiz_inc_hours', '%u godz.', # 1: hours
'quiz_inc_hours-en', '%u hr',
'quiz_inc_minutes', '%u min.', # 1: minutes
'quiz_inc_minutes-en', '%u min',
'quiz_inc_seconds', '%u sek.', # 1: seconds
'quiz_inc_seconds-en', '%u sec',
'quiz_inc_seconds_ms', '%.3f sek.', # 1: seconds.milliseconds
'quiz_inc_seconds_ms-en', '%.3f sec',
'quiz_msg_warn_timeout', "\00307Zostalo jeszcze tylko \00304%u\00307 sek. na odpowiadanie!", # 1: time (s)
'quiz_msg_warn_timeout-en', "\00307Only \00304%u\00307s left for answering!",
'quiz_msg_all_answers', "\00303Wszystkie odpowiedzi zostaly odgadniete!",
'quiz_msg_all_answers-en', "\00303All answers were given!",
'quiz_msg_timeout', "\00303Czas na odpowiadanie uplynal!",
'quiz_msg_timeout-en', "\00303Timeout!",
'quiz_msg_ignored', "\00303Gracz \00304%s\00303 zostal dodany do listy ignorowanych!", # 1: nick
'quiz_msg_ignored-en', "\00303Player \00304%s\00303 has been blacklisted!",
'quiz_msg_unignored', "\00303Gracz \00304%s\00303 zostal usuniety z listy ignorowanych.", # 1: nick
'quiz_msg_unignored-en', "\00303Player \00304%s\00303 has been removed from blacklist.", # 1: nick
'quiz_sfx_next', "\00303Nastepne pytanie za %u sek...", # 1: time (s)
'quiz_msg_next-en', "\00303Next question in %us...",
'quiz_sfx_next_x', "\00303Nastepne haslo za %u sek...", # 1: time (s)
'quiz_msg_next_x-en', "\00303Next word in %us...",
'quiz_sfx_last', "\00307Koniec pytan!",
'quiz_msg_last-en', "\00307No more questions!",
'quiz_sfx_limit', "\00307Masz juz 50%%+1 punktow - wygrales(as), daj pograc innym. ;)",
'quiz_msg_limit-en', "\00307You have 50%%+1 points now, you've won - let others play. ;)",
'quiz_sfx_paused', "\00307Quiz zostaje wstrzymany do odwolania.",
'quiz_sfx_paused-en', "\00307Quiz is temporarily suspended now.",
# 1 point / x points / 2-4, x2-x4 points (x != 1) | 1 punkt / x punktow / 2-4, x2-x4 punkty (x != 1)
'quiz_flx_points', 'punkt/punktow/punkty',
'quiz_flx_points-en', 'point/points/points',
# 1 answer / x answers / 2-4, x2-x4 answers (x != 1) | 1 odpowiedz / x odpowiedzi / 2-4, x2-x4 odpowiedzi (x != 1)
'quiz_flx_answers', 'odpowiedz/odpowiedzi/odpowiedzi',
'quiz_flx_answers-en', 'answer/answers/answers',
# (after) 1 question / x questions / 2-4, x2-x4 questions (x != 1) | (po) 1 pytaniu / x pytaniach / 2-4, x2-x4 pytaniach (x != 1)
'quiz_flx_aquestions', 'pytaniu/pytaniach/pytaniach',
'quiz_flx_aquestions-en', 'question/questions/questions',
# (from) 1 question / x questions / 2-4, x2-x4 questions (x != 1) | (z) 1 pytania / x pytan / 2-4, x2-x4 pytan (x != 1)
'quiz_flx_fquestions', 'pytania/pytan/pytan',
'quiz_flx_fquestions-en', 'question/questions/questions',
];
my $_theme = {@$theme};
theme_register($theme);
##### Main routines #####
sub load_quiz {
my ($fname, $lines) = (shift, 0);
$quiz{data} = [];
$quiz{qcnt} = 0;
return 0 unless (open(my $fh, '<', $fname));
while (<$fh>) {
s/[\n\r]//g; # chomp is platform dependent ($/)
tr/\t/ /; # tabs to spaces
s/ {2,}/ /g; # fix double spaces
s/^ +| +$//g; # trim leading/trailing spaces/tabs
next if (/^ *$/);
if (($quiz{type} == QT_STD) || ($quiz{type} == QT_SCR)) {
if ($lines % 2) {
s/^o(dp|pd) //i; # remove format (broken as well)
$quiz{data}[++$quiz{qcnt}]{answer} = $_; # ++ only on complete question
} else {
s/^p(yt|ty) //i; # remove format (broken as well)
$quiz{data}[$quiz{qcnt} + 1]{($quiz{type} == QT_STD) ? 'question' : 'realquestion'} = $_;
}
} elsif ($quiz{type} == QT_MIX) {
s/^\d+ //; # remove format
$quiz{data}[++$quiz{qcnt}]{answer} = $_;
} elsif (($quiz{type} == QT_FAM) || ($quiz{type} == QT_MUL)) {
if ($lines % 2) {
s/ +\*/*/g; # fix format
s/\* +/*/g; # fix format
my $enum = 1;
# ++ only on complete question
%{$quiz{data}[++$quiz{qcnt}]{answers}} = map { $_ => $enum++ } split /\*/;
} else {
$quiz{data}[$quiz{qcnt} + 1]{question} = $_;
}
}
$lines++;
}
close($fh);
return $lines;
}
sub l10n {
my $text = shift;
my $lang = $quiz{lang};
if (ref($text)) {
return exists($text->{$lang}) ? $text->{$lang} : $text->{pl};
} else {
my $l10n = $text . (($lang eq 'pl') ? '' : '-' . $lang);
return exists($_theme->{$l10n}) ? $l10n : $text;
}
}
sub get_format {
my ($format, $args) = @_;
return sprintf(current_theme()->get_format(__PACKAGE__, l10n($format)), ref($args) ? @{$args} : (defined($args) ? $args : ()));
}
sub send_ui {
my ($format, @rest) = @_;
printformat(MSGLEVEL_CRAP, l10n($format), @rest);
}
sub send_ui_raw {
print CLIENTCRAP shift;
}
sub send_irc {
my (undef, undef, $flags, $nick) = @_;
$flags //= 0;
my $msg = get_format(@_);
if ($quiz{chan}{server}{connected}) {
my $data = (($flags & WHISPER) ? "NOTICE $nick" : "PRIVMSG $quiz{chan}{name}") . " :$msg"; # notice?
if ($flags & INSTANT) { # instant or queued?
$quiz{chan}{server}->send_raw_now($data);
} else {
$quiz{chan}{server}->send_raw($data);
}
if ($flags & WHISPER) { # le trick (workaround for chantext showing after owntext)
timeout_add_once($_display_delay, 'evt_delayed_show_notc', [$msg, $nick]);
} else {
timeout_add_once($_display_delay, 'evt_delayed_show_msg', $msg);
}
} else {
send_ui_raw($msg); # this helps when we got disconnected not to lose any messages (like stats)
send_ui('quiz_err_server');
}
}
sub shuffle_text {
my ($text, $shuffled, $length) = (shift, '', 0);
my @text = split(//, $text);
my @floaters;
my $smart = ($quiz{type} == QT_SCR) ? settings_get_bool('quiz_smart_mix') : 0;
if ($smart) {
my $chars = settings_get_str('quiz_smart_mix_chars'); #? quotemeta?
for (my $i = 0; $i < @text; $i++) {
if ($text[$i] !~ /^[$chars]$/) { # hypen, apostrophe, math symbols will float
push(@floaters, $i);
$length++;
}
}
$smart = 0 if ($length == length($text)); # no punctations & digits
} else {
$length = length($text);
}
return $text if ($length < 2); # skip short (and empty)
my $watchdog = ($length < $_shuffle_threshold) ? 1 : $_shuffle_watchdog;
my $same = 1;
if (!$smart) { # check same letter repetition
foreach my $char (@text) {
if ($char ne $text[0]) {
$same = 0;
last;
}
}
} else {
foreach my $id (@floaters) {
if ($text[$id] ne $text[$floaters[0]]) {
$same = 0;
last;
}
}
}
return $text if ($same); # nothing to shuffle
do {
if ($smart) {
my @shuffled = @text; # this way we have anchors already in place
my @tmp = @floaters;
foreach my $dst (@floaters) {
my $src = splice(@tmp, int(rand(@tmp)), 1);
$shuffled[$dst] = $text[$src];
}
$shuffled = join('', @shuffled);
} else {
my @tmp = @text;
$shuffled = '';
$shuffled .= splice(@tmp, int(rand(@tmp)), 1) while (@tmp);
}
} until (($text ne $shuffled) || (--$watchdog <= 0));
return $shuffled;
}
sub shuffle {
my ($text, $style) = (shift, settings_get_int('quiz_words_case'));
my $keepfmt = ($quiz{type} == QT_SCR) ? 1 : settings_get_bool('quiz_words_mode');
if ($style == 1) {
$text = lc $text;
} elsif ($style == 2) {
$text = uc $text;
} elsif ($style == 3) {
$text = join(' ', map { ucfirst lc } split(/ /, $text));
}
if ($keepfmt) {
return join(' ', map { shuffle_text($_) } split(/ /, $text));
} else {
$text =~ s/ //g;
return shuffle_text($text);
}
}
sub antigoogle {
my $text = shift;
return $text unless (settings_get_bool('quiz_antigoogler') && ($text =~ / /));
return $text if ($_protect_urls && ($text =~ m<https?://|www\.>));
my ($fg, $bg) = (get_format('quiz_inc_question', '') =~ /^\003(\d{1,2}),(\d{1,2})/);
return $text unless (defined($fg) && defined($bg));
($fg, $bg) = map { int } ($fg, $bg);
my @set = ('a'..'z', 'A'..'Z', 0..9);
my @h; my @v;
#t = \00300,01 (quiz_inc_question)
#h = \0031,01 \00301,01 \00301
#v = \0030,01 \00300,01 \00300
if ($bg < 10) {
push(@h, "\0030$bg");
push(@h, "\003$bg,0$bg", "\0030$bg,0$bg") if ($_randomized_antigoogler);
} else {
push(@h, "\003$bg");
push(@h, "\003$bg,$bg") if ($_randomized_antigoogler);
}
$bg = substr("0$bg", -2); # make sure $bg is 2-char
if ($fg < 10) {
push(@v, "\0030$fg");
push(@v, "\003$fg,$bg", "\0030$fg,$bg") if ($_randomized_antigoogler);
} else {
push(@v, "\003$fg");
push(@v, "\003$fg,$bg") if ($_randomized_antigoogler);
}
my @lines;
if (settings_get_bool('quiz_split_long_lines')) {
# very ugly, but required calculations depending on type of question
my $raw_crap = length(get_format('quiz_inc_question', ''));
my $msg_crap = $raw_crap;
if (!$quiz{inq}) {
my $suffix = ($quiz{type} == QT_MIX) ? '_x' : ((($quiz{type} == QT_FAM) || ($quiz{type} == QT_MUL)) ? '_fm' : '');
my $answers = keys %{$quiz{lookup}};
my $duration = abs(settings_get_int('quiz_timeout')) || $_round_duration; # abs in case of <0, || in case of ==0
$msg_crap += length(get_format('quiz_msg_question' . $suffix, [$quiz{qnum}, $quiz{qcnt}, '', $answers, answers_str($answers), $duration]));
} else {
$msg_crap += length(get_format('quiz_msg_remind', ''));
}
my $cutoff = 497 - length($quiz{chan}{server}{nick} . $quiz{chan}{server}{userhost} . $quiz{chan}{name});
my @words = split(/ /, $text);
$text = shift(@words);
my ($line, $subst) = (1, 1);
while (@words) {
my $ag = $h[int(rand(@h))] . $set[int(rand(@set))] . $v[int(rand(@v))];
$ag = ' ' if ($_smarter_antigoogler && ($subst % ($_smarter_antigoogler_chunk + 1) == 0));
my $word = shift(@words);
if (length($text . $ag . $word) > $cutoff - (($line == 1) ? $msg_crap : $raw_crap)) {
push(@lines, $text);
$text = $word;
$line++;
$subst = 1;
} else {
$text .= $ag . $word;
$subst++;
}
}
} else {
if ($_smarter_antigoogler) {
my @words = split(/ /, $text);
$text = shift(@words);
my $subst = 1;
while (@words) {
my $ag = $h[int(rand(@h))] . $set[int(rand(@set))] . $v[int(rand(@v))];
$ag = ' ' if ($subst++ % ($_smarter_antigoogler_chunk + 1) == 0);
$text .= $ag . shift(@words);
}
} else {
while ($text =~ / /) {
my $ag = $h[int(rand(@h))] . $set[int(rand(@set))] . $v[int(rand(@v))];
$text =~ s/ /$ag/; # one by one, not /g
}
}
}
push(@lines, $text);
return @lines;
}
sub put_dots {
my ($hint, $format, $setting, $default, $marker) = @_;
my $char = settings_get_str($setting); #? substr(settings_get_str($setting), 0, 1)
my $dot = get_format($format, ($char eq '') ? $default : $char);
$hint =~ s/$marker/$dot/g; # le trick grande finale
my ($scol, $ecol) = ($dot =~ /^(\003\d{1,2}(?:,\d{1,2})?).(\003\d{1,2}(?:,\d{1,2})?)$/);
if (defined($scol) && defined($ecol)) {
$hint =~ s/$ecol $scol/ /g; # optimize color codes
$hint =~ s/$ecol$scol//g;
$hint =~ s/$ecol$//;
}
return $hint;
}
sub make_hint {
my $dots_only = shift;
my @words = split(/ /, $quiz{data}[$quiz{qnum}]{answer});
if (!@{$quiz{dots}}) { # make first dots
@quiz{qw/dcnt dmax/} = (0) x 2;
$quiz{hwords} = [];
my ($w, $dmax) = (0) x 2;
foreach my $word (@words) {
my ($l, $hword, $dcnt) = (0, '', 0);
foreach my $letter (split(//, $word)) {
if ($letter =~ /^[a-z0-9]$/i) {
push(@{$quiz{dots}[$w]}, $l);
$hword .= ($letter =~ /^[0-9]$/) ? "\002" : "\001"; # le trick (any ASCII non-printable char)
$quiz{dcnt}++;
$dcnt++;
} else {
$hword .= $letter;
}
$l++;
}
push(@{$quiz{hwords}}, $hword);
$dmax = $dcnt if ($dcnt > $dmax);
$w++;
}
$quiz{dmax} = $dmax;
}
return '' if ($dots_only); # prep dots only
$quiz{hnum}++;
my $first_dots = settings_get_bool('quiz_first_hint_dots');
if (!$first_dots || ($quiz{hnum} > 1)) { # reveal some dots
my $random_hints = settings_get_bool('quiz_random_hints');
my $show_first = settings_get_bool('quiz_nonrandom_first_hint') && ($quiz{hnum} == ($first_dots ? 2 : 1));
my ($w, $dmax) = (0) x 2;
foreach my $r_wdots (@{$quiz{dots}}) {
if ((ref $r_wdots) && (@$r_wdots > 0)) {
my @letters = split(//, $words[$w]);
my @hletters = split(//, $quiz{hwords}[$w]);
my $sel = (!$random_hints || $show_first) ? 0 : int(rand(@$r_wdots));
$hletters[@$r_wdots[$sel]] = $letters[@$r_wdots[$sel]];
$quiz{hwords}[$w] = join('', @hletters);
splice(@$r_wdots, $sel, 1);
$quiz{dcnt}--;
$dmax = @$r_wdots if (@$r_wdots > $dmax);
}
$w++;
}
$quiz{dmax} = $dmax;
}
my $hint = join(' ', @{$quiz{hwords}});
$hint = put_dots($hint, 'quiz_inc_hint_alpha', 'quiz_hint_alpha', $_hint_alpha, "\001");
$hint = put_dots($hint, 'quiz_inc_hint_digit', 'quiz_hint_digit', $_hint_digit, "\002");
return $hint;
}
sub make_remind {
if (!$quiz{inq} || settings_get_bool('quiz_mix_on_remind')) {
if ($quiz{type} == QT_SCR) {
$quiz{data}[$quiz{qnum}]{question} = shuffle($quiz{data}[$quiz{qnum}]{realquestion});
} elsif ($quiz{type} == QT_MIX) {
$quiz{data}[$quiz{qnum}]{question} = shuffle($quiz{data}[$quiz{qnum}]{answer});
}
}
return antigoogle($quiz{data}[$quiz{qnum}]{question});
}
sub time_str {
my ($s, $mode) = @_;
my ($h, $m) = (0) x 2;
if ($mode == TIME_HMS) {
$h = int($s / 3600);
$m = int($s / 60) % 60;
$s %= 60;
}
my $str = '';
$str .= get_format('quiz_inc_hours', $h) . ' ' if ($h);
$str .= get_format('quiz_inc_minutes', $m) . ' ' if ($m);
$str .= get_format('quiz_inc_seconds' . (($mode == TIME_MS) ? '_ms' : ''), $s) if ($s || (!$h && !$m));
$str =~ s/ $//;
return $str;
}
sub flex {
my ($value, $format, $flex) = (abs(shift), shift, 0);
my @flex = split(/$_flex_separator/, get_format($format));
if ($value != 1) {
$flex++;
$flex++ if ($value =~ /^[2-4]$|[^1][2-4]$/);
}
return defined($flex[$flex]) ? $flex[$flex] : '???'; # just a precaution when user messes up
}
sub score_str { return flex(shift, 'quiz_flx_points'); } # X points
sub answers_str { return flex(shift, 'quiz_flx_answers'); } # X answers
sub aquestions_str { return flex(shift, 'quiz_flx_aquestions'); } # after X questions <- AFTER!
sub fquestions_str { return flex(shift, 'quiz_flx_fquestions'); } # from X questions <- FROM!
sub percents {
my ($val, $of) = @_;
return ($of == 0) ? 0 : $val / $of * 100;
}
sub asciize {
my $text = shift;
return $text unless (settings_get_bool('quiz_asciize'));
$text =~ s/ę/e/g; $text =~ s/ó/o/g; $text =~ s/ą/a/g; $text =~ s/ś/s/g; $text =~ s/ł/l/g;
$text =~ s/ż/z/g; $text =~ s/ź/z/g; $text =~ s/ć/c/g; $text =~ s/ń/n/g;
$text =~ s/Ę/E/g; $text =~ s/Ó/O/g; $text =~ s/Ą/A/g; $text =~ s/Ś/S/g; $text =~ s/Ł/L/g;
$text =~ s/Ż/Z/g; $text =~ s/Ź/Z/g; $text =~ s/Ć/C/g; $text =~ s/Ń/N/g;
return $text;
}
sub stop_timer {
my $timer = shift;
if ($quiz{$timer}) {
timeout_remove($quiz{$timer});
$quiz{$timer} = undef;
}
}
sub stop_question {
@quiz{qw/inq bonus hnum hprot rprot dcnt dmax/} = (0) x 7;
stop_timer($_) foreach (qw/tround thint thinter tremind twarn tbonus/);
$quiz{dots} = [];
$quiz{hwords} = [];
$quiz{lookup} = {};
}
sub stop_quiz {
stop_question();
@quiz{qw/ison standby ended wantpause paused/} = (0) x 5;
stop_timer('tnext');
$quiz{lastone} = '';
signal_remove('message public', 'sig_pubmsg');
}
sub init_next_question {
my ($msg, $flags) = @_;
$flags //= 0;
if ($quiz{qnum} >= $quiz{qcnt}) {
send_irc('quiz_msg', $msg . (($flags & BONUS) ? '' : ' ' . get_format('quiz_sfx_last')), $flags);
$quiz{ended} = 1;
} else {
my $suffix = '';
if ($quiz{wantpause}) {
$suffix = ' ' . get_format('quiz_sfx_paused') unless ($flags & BONUS);
@quiz{qw/wantpause paused/} = (0, 1);
} else {
unless ($flags & BONUS) {
my $delay;
if (($quiz{type} == QT_FAM) || ($quiz{type} == QT_MUL)) {
$delay = abs(settings_get_int('quiz_delay_long')) || $_next_delay_long; # abs in case of <0, || in case of ==0
} else {
$delay = abs(settings_get_int('quiz_delay')) || $_next_delay; # abs in case of <0, || in case of ==0
}
$suffix = ' ' . get_format('quiz_sfx_next' . (($quiz{type} == QT_MIX) ? '_x' : ''), $delay);
$quiz{tnext} = timeout_add_once($delay * 1000, 'evt_show_question', undef);
}
}
send_irc('quiz_msg', $msg . $suffix, $flags);
}
}
sub name_to_type {
my ($name, $type) = (shift, undef);
return $name if ($name =~ /^\d+$/);
my %type = (diz => 1, std => 1, sta => 1, nrm => 1, nor => 1, zwy => 1,
mie => 2, mix => 2, lit => 2,
fam => 3, dru => 3, tea => 3,
mul => 4, all => 4, wsz => 4, bez => 4,
pom => 5, scr => 5);
foreach my $key (keys %type) {
$type = $type{$key}, last if (lc($name) =~ /^$key/i);
}
return $type;
}
sub is_valid_quiz {
return (($quiz{qcnt} < 1) ||
((($quiz{type} == QT_STD) || ($quiz{type} == QT_FAM) || ($quiz{type} == QT_MUL) ||
($quiz{type} == QT_SCR)) && ($quiz{qcnt} * 2 != shift))) ? 0 : 1;
}
sub correct_answer {
my ($addr, $nick, $timestamp, $points, $answer) = @_;
@{$quiz{players}{$addr}}{qw/besttime bestspeed/} = (0) x 2 if (!exists $quiz{players}{$addr});
@{$quiz{players}{$addr}}{qw/nick timestamp/} = ($nick, $timestamp);
my $time = $timestamp - $quiz{qtime};
my $speed = length($answer) / $time;
$quiz{players}{$addr}{alltime} += $time;
$quiz{players}{$addr}{allspeed} += $speed;
$quiz{players}{$addr}{besttime} = $time if (($quiz{players}{$addr}{besttime} == 0) || ($quiz{players}{$addr}{besttime} > $time));
$quiz{players}{$addr}{bestspeed} = $speed if (($quiz{players}{$addr}{bestspeed} == 0) || ($quiz{players}{$addr}{bestspeed} < $speed));
$quiz{players}{$addr}{score} += $points;
$quiz{players}{$addr}{answers}++;
$quiz{score} += $points;
$quiz{answers}++;
if ($quiz{type} == QT_FAM) {
$quiz{players}{$addr}{team} = 0 if (!exists $quiz{players}{$addr}{team}); # team_play is on and player is an outsider (outsiders = team 0)
my $team = $quiz{players}{$addr}{team};
$quiz{teams}[$team]{score} += $points;
$quiz{teams}[$team]{answers}++;
}
}
sub hcmd {
return sprintf(' %-32s - ', shift);
}
sub hvar {
my ($setting, $type) = @_;
if ($type == INT) {
return sprintf(' %-26s : %-3d - ', $setting, settings_get_int($setting));
} elsif ($type == BOOL) {
return sprintf(' %-26s : %-3s - ', $setting, settings_get_bool($setting) ? 'on' : 'off');
} elsif ($type == STR) {
return sprintf(' %-26s : %-3s - ', $setting, settings_get_str($setting));
}
}
sub show_help {
my $type = settings_get_int('quiz_type');
my $state = l10n({ pl => 'jest wylaczony',
en => 'is offline'});
if ($quiz{ison}) {
if ($quiz{standby}) {
$state = l10n({ pl => 'oczekuje na uruchomienie',
en => 'is on standby'});
} elsif ($quiz{ended}) {
$state = l10n({ pl => 'dobiegl konca',
en => 'has finished'});
} elsif ($quiz{paused}) {
$state = l10n({ pl => 'jest wstrzymany',
en => 'is paused'});
} else {
$state = l10n({ pl => 'trwa',
en => 'is ongoing'});
}
}
send_ui_raw("%_$IRSSI{name}%_ v$VERSION by wilk (" . l10n({ pl => 'quiz obecnie',
en => 'quiz currently'}) . ": %_$state%_)");
send_ui_raw(l10n({ pl => '%_Dostepne polecenia:%_ ([] - parametr opcjonalny, <> - parametr wymagany)',
en => '%_Available commands:%_ ([] - optional argument, <> - required argument)'}));
send_ui_raw(hcmd('/qtype ' . l10n({ pl => "[1-$_quiz_types/nazwa]",
en => "[1-$_quiz_types/name]"})) . l10n({ pl => 'zmiana rodzaju quizu (bez parametru wybiera kolejny)',
en => 'change quiz type'}));
if ($type == QT_FAM) {
send_ui_raw(hcmd("/qteams <2-$_max_teams>") . l10n({ pl => 'zmiana liczby druzyn',
en => 'change number of teams'}));
send_ui_raw(hcmd('/qon ' . l10n({ pl => "[kanal] <plik> [1-$_quiz_types/nazwa] [0-$_max_teams]",
en => "[channel] <file> [1-$_quiz_types/name] [0-$_max_teams]"})) . l10n({ pl => 'rozpoczecie quizu (mozna podac rodzaj quizu i liczbe druzyn)',
en => 'start the quiz (you can provide its type and number of teams)'}));
send_ui_raw(hcmd('/qstats ' . l10n({ pl => '[miejsca]',
en => '[places]'})) . l10n({ pl => 'wyswietla ranking graczy (0: pokazuje tylko druzyny)',
en => 'display scoreboard (0: teams only)'}));
} else {
send_ui_raw(hcmd('/qon ' . l10n({ pl => "[kanal] <plik> [1-$_quiz_types/nazwa]",
en => "[channel] <file> [1-$_quiz_types/name]"})) . l10n({ pl => 'rozpoczecie quizu (mozna tez podac rodzaj quizu)',
en => 'start the quiz (you can provide its type)'}));
send_ui_raw(hcmd('/qstats ' . l10n({ pl => '[miejsca]',
en => '[places]'})) . l10n({ pl => 'wyswietla ranking graczy',
en => 'display scoreboard'}));
}
send_ui_raw(hcmd('/qhint') . l10n({ pl => 'wyswietlenie podpowiedzi',
en => 'show next hint'})) if (($type != QT_FAM) && ($type != QT_MUL));
send_ui_raw(hcmd('/qremind') . l10n({ pl => 'przypomnienie biezacego pytania',
en => 'remind current question'}));
send_ui_raw(hcmd('/qskip') . l10n({ pl => 'pominiecie biezacego pytania',
en => 'skip current question'}));
send_ui_raw(hcmd('/qpause') . l10n({ pl => 'wstrzymanie quizu (od nastepnego pytania)',
en => 'suspend the quiz'}));
send_ui_raw(hcmd('/qoff') . l10n({ pl => 'przerwanie lub zakonczenie quizu',
en => 'break or finish the quiz'}));
send_ui_raw(hcmd('/qdelay ' . l10n({ pl => '<sekundy>',
en => '<seconds>'})) . l10n({ pl => 'zmiana opoznienia miedzy pytaniami',
en => 'change delay between questions'}));
send_ui_raw(hcmd('/qtime ' . l10n({ pl => '<sekundy>',
en => '<seconds>'})) . l10n({ pl => 'zmiana czasu trwania rundy',
en => 'change round duration'})) if (($type == QT_FAM) || ($type == QT_MUL));
send_ui_raw(hcmd('/qignore <nick>') . l10n({ pl => '(od)blokowanie problematycznego gracza',
en => '(un)ignore a cheating player'}));
send_ui_raw(hcmd('/qreload') . l10n({ pl => 'ponowne wczytanie pliku z pytaniami',
en => 'reload questions'}));
send_ui_raw(hcmd('/qinit') . l10n({ pl => 'resetuje ustawienia do wartosci poczatkowych',
en => 'reset settings to default values'}));
send_ui_raw(l10n({ pl => '%_Dostepne ustawienia (/set):%_',
en => '%_Available settings (/set):%_'}));
send_ui_raw(hvar('quiz_type', INT) . l10n({ pl => 'rodzaj quizu (1: Dizzy, 2: Mieszacz/Literaki, 3: Familiada, 4: Multi (Familiada bez druzyn), 5: Pomieszany)',
en => 'quiz type (1: Dizzy, 2: Mieszacz/Literaki, 3: Familiada, 4: Multi (Familiada w/o teams), 5: Pomieszany)'}));
send_ui_raw(hvar('quiz_teams', INT) . l10n({ pl => 'liczba druzyn',
en => 'number of teams'}) . " (2-$_max_teams)") if ($type == QT_FAM);
if (($type == QT_FAM) || ($type == QT_MUL)) {
send_ui_raw(hvar('quiz_delay_long', INT) . l10n({ pl => 'opoznienie miedzy pytaniami (sek.)',
en => 'delay between questions (sec)'}));
send_ui_raw(hvar('quiz_timeout', INT) . l10n({ pl => 'czas trwania rundy (sek.)',
en => 'round duration (sec)'}));
} else {
send_ui_raw(hvar('quiz_delay', INT) . l10n({ pl => 'opoznienie miedzy pytaniami (sek.)',
en => 'delay between questions (sec)'}));
send_ui_raw(hvar('quiz_max_hints', INT) . l10n({ pl => 'limit podpowiedzi (0: bez ogr., >0: limit podp., <0: limit ukrytych znakow)',
en => 'max. number of hints (0: no limit, >0: number of hints, <0: number of hidden chars)'}));
send_ui_raw(hvar('quiz_autohinter', BOOL) . l10n({ pl => 'automatyczne podpowiedzi?',
en => 'show automatic hints?'}));
send_ui_raw(hvar('quiz_hints_interval', INT) . l10n({ pl => 'opoznienie pomiedzy auto-podpowiedziami (sek.)',
en => 'automatic hints interval (sec)'}));
}
send_ui_raw(hvar('quiz_words_case', INT) . l10n({ pl => 'styl wyrazow (0: bez zmian, 1: male, 2: DUZE, 3: Kapitaliki)',
en => 'words\' case (0: no change, 1: l-case, 2: U-case, 3: Caps)'})) if (($type == QT_MIX) || ($type == QT_SCR));
send_ui_raw(hvar('quiz_antigoogler', BOOL) . l10n({ pl => 'uzywac antygooglera do ochrony pytan?',
en => 'protect questions with antigoogler?'}));
send_ui_raw(hvar('quiz_split_long_lines', BOOL) . l10n({ pl => 'dzielic dlugie linie na czesci (nowsze irssi potrafi samo)?',
en => 'split long lines?'}));
send_ui_raw(hvar('quiz_anticheat_delay', INT) . l10n({ pl => 'czas trwania ochrony !podp/!przyp (sek.; 0: wylaczone)',
en => '!hint/!remind protection delay (sec; 0: off)'}));
send_ui_raw(hvar('quiz_first_anticheat_delay', INT) . l10n({ pl => 'czas trwania ochrony pierwszego !podp/!przyp (sek.; 0: wylaczone)',
en => 'first !hint/!remind protection delay (sec; 0: off)'}));
if (($type != QT_FAM) && ($type != QT_MUL)) {
send_ui_raw(hvar('quiz_show_first_hint', BOOL) . l10n({ pl => 'pokazywac podpowiedz razem z pytaniem?',
en => 'show questions along with first hint?'}));
send_ui_raw(hvar('quiz_first_hint_dots', BOOL) . l10n({ pl => 'pierwsza podpowiedz jako same kropki?',
en => 'first hint as dots only?'}));
send_ui_raw(hvar('quiz_random_hints', BOOL) . l10n({ pl => 'losowe odslanianie podpowiedzi? albo od lewej do prawej',
en => 'reveal random chars in hints? otherwise from left to right'}));
send_ui_raw(hvar('quiz_nonrandom_first_hint', BOOL) . l10n({ pl => 'losowe odslanianie podpowiedzi, poza pierwsza?',
en => 'reveal random chars in hints, except first hint?'}));
send_ui_raw(hvar('quiz_hint_alpha', STR) . l10n({ pl => 'znak podstawiany w podpowiedziach za litery',
en => 'character substituted for letters'}));
send_ui_raw(hvar('quiz_hint_digit', STR) . l10n({ pl => 'znak podstawiany w podpowiedziach za cyfry',
en => 'character substituted for digits'}));
}
send_ui_raw(hvar('quiz_words_mode', BOOL) . l10n({ pl => 'mieszac slowa osobno? albo wszystko razem',
en => 'scramble words separately? otherwise all together'})) if ($type == QT_MIX);
if ($type == QT_SCR) {
send_ui_raw(hvar('quiz_smart_mix', BOOL) . l10n({ pl => 'mieszac kotwiczac cyfry i niektore znaki interpunkcyjne?',
en => 'anchor some characters? (digits/punctation)'}));
send_ui_raw(hvar('quiz_smart_mix_chars', STR) . l10n({ pl => 'te znaki beda zakotwiczone (regex)',
en => 'anchor these characters (regex)'}));
}
send_ui_raw(hvar('quiz_mix_on_remind', BOOL) . l10n({ pl => 'mieszac litery przy kazdym !przyp?',
en => 'scramble letters with each !remind?'})) if (($type == QT_MIX) || ($type == QT_SCR));
if ($type == QT_FAM) {
send_ui_raw(hvar('quiz_join_anytime', BOOL) . l10n({ pl => 'wchodzenie do druzyn w dowolnej chwili?',
en => 'allow joining teams at any time?'}));
send_ui_raw(hvar('quiz_team_play', BOOL) . l10n({ pl => 'graja tylko gracze z druzyn?',
en => 'only team players can answer?'}));
send_ui_raw(hvar('quiz_transfer_points', BOOL) . l10n({ pl => 'wraz ze zmiana druzyny przenosic tez punkty?',
en => 'transfer scores when player changes a team?'}));
}
send_ui_raw(hvar('quiz_strict_match', BOOL) . l10n({ pl => 'tylko doslowne odpowiedzi? albo *dopasowane*',
en => 'only strict answers? or allow *matching*'})) if (($type != QT_FAM) && ($type != QT_MUL));
send_ui_raw(hvar('quiz_points_per_answer', INT) . l10n({ pl => 'punkty za poprawna odpowiedz',
en => 'points given for correct answer'}));
if (($type == QT_FAM) || ($type == QT_MUL)) {
send_ui_raw(hvar('quiz_min_points', INT) . l10n({ pl => 'minimum punktowe',
en => 'min. points'}));
send_ui_raw(hvar('quiz_max_points', INT) . l10n({ pl => 'maksimum punktowe',
en => 'max. points'}));
send_ui_raw(hvar('quiz_scoring_mode', INT) . l10n({ pl => 'tryb punktowania (1: ppa, 2: ppa++, 3: ppa++:max, 4: min++ppa, 5: min++ppa:max, 6: max--ppa:min, 7: max->min',
en => 'scoring mode (1: ppa, 2: ppa++, 3: ppa++:max, 4: min++ppa, 5: min++ppa:max, 6: max--ppa:min, 7: max->min'}));
} else {
send_ui_raw(hvar('quiz_bonus_answer', BOOL) . l10n({ pl => 'uznawac dodatkowa szybka odpowiedz drugiego gracza? (max. 1 sek.)',
en => 'accept second fastest answer? (within 1s)'}));
send_ui_raw(hvar('quiz_points_per_bonus', INT) . l10n({ pl => 'punkty za druga najszybsza odpowiedz',
en => 'points given for second fastest answer'}));
send_ui_raw(hvar('quiz_limiter', BOOL) . l10n({ pl => 'limitowac najlepsza osobe do 50%+1 punktow?',
en => 'limit best player to 50%+1 of total points?'}));
}
send_ui_raw(hvar('quiz_asciize', BOOL) . l10n({ pl => 'konwertowac znaki diakrytyczne w odpowiedziach do ASCII? (utf8)',
en => 'convert diacritic marks in answers to ASCII? (utf8)'}));
send_ui_raw(hvar('quiz_ranking_type', INT) . l10n({ pl => 'rodzaj rankingu (1: zwykly "1234", 2: zwarty "1223", 3: turniejowy "1224")',
en => 'ranking type (1: ordinal "1234", 2: dense "1223", 3: competition "1224")'}));
send_ui_raw(hvar('quiz_keep_scores', BOOL) . l10n({ pl => 'sumowac punkty z poprzednich quizow?',
en => 'keep scores between quizzes?'}));
send_ui_raw(hvar('quiz_keep_teams', BOOL) . l10n({ pl => 'jesli zachowujemy punkty, to pozostawiac zawodnikow w druzynach miedzy quizami?',
en => 'keep teams\' composition as well?'})) if ($type == QT_FAM);
send_ui_raw(hvar('quiz_cmd_hint', BOOL) . l10n({ pl => 'polecenie !podp jest dostepne dla graczy?',
en => 'should !hint command be available?'}));
send_ui_raw(hvar('quiz_cmd_remind', BOOL) . l10n({ pl => 'polecenie !przyp jest dostepne dla graczy?',
en => 'should !remind command be available?'}));
send_ui_raw(hvar('quiz_lang', STR) . l10n({ pl => 'wersja jezykowa komunikatow (dostepne: "pl" oraz "en")',
en => 'language version (available: "pl" and "en")'}));
}
##### Commands' handlers #####
sub cmd_start {
if ($quiz{standby}) {
$quiz{standby} = 0;
evt_show_question();
return;
}
send_ui('quiz_err_ison'), return if ($quiz{ison});
my ($args, $r_server, $r_window) = @_;
send_ui('quiz_err_server'), return if (!ref($r_server) || !$r_server->{connected});
my ($chan, $file, $type, $teams) = split(/ /, $args);
($file, $chan) = ($chan, ref($r_window) ? $r_window->{name} : undef) if (!defined $file); # single arg call?
send_ui('quiz_err_channel'), return if (!$chan || !$r_server->ischannel($chan));
{
{ package Irssi::Nick; } # should prevent irssi bug: "Can't locate package Irssi::Nick for @Irssi::Irc::Nick::ISA at ..."