-
Notifications
You must be signed in to change notification settings - Fork 3
/
frogesport.tcl
2173 lines (2106 loc) · 115 KB
/
frogesport.tcl
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
###########################################################################
### Frogesport ###
###########################################################################
###
#
# Copyright 2011-2014 Zmegolaz <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################
### You don't need to edit anything in this file. ###
###########################################################################
# Create the bindings
# Admin commands
bind pub * "!answer" ::frogesport::answer
bind pub * "!check" ::frogesport::check
bind pub * "!clear" ::frogesport::clear
bind pub * "!pause" ::frogesport::clear
bind pub * "!continue" ::frogesport::continue
bind pub * "!punish" ::frogesport::punish
bind pub * "!reward" ::frogesport::reward
bind pub * "!startmess" ::frogesport::startmess
bind pub * "!startquiz" ::frogesport::startquiz
bind pub * "!stopmess" ::frogesport::stopmess
bind pub * "!stopquiz" ::frogesport::stopquiz
# Admin commands from PM
bind msg * "addq" ::frogesport::msgaddq
bind msg * "checkq" ::frogesport::msgcheckq
bind msg * "checku" ::frogesport::msgchecku
bind msg * "delq" ::frogesport::msgdelq
bind msg * "help" ::frogesport::msghelp
bind msg * "hjälp" ::frogesport::msghelp
bind msg * "modq" ::frogesport::msgmodq
bind msg * "modu" ::frogesport::msgmodu
bind msg * "newseason" ::frogesport::msgnewseason
bind msg * "rapporter" ::frogesport::msgreports
bind msg * "rapport" ::frogesport::msgreports
bind msg * "recs" ::frogesport::msgrecs
bind msg * "reports" ::frogesport::msgreports
bind msg * "report" ::frogesport::msgreports
bind msg * "updateclasses" ::frogesport::msgupdateclasses
# User commands
bind pub * "!addq" ::frogesport::recommendq
bind pub * "!clearqueue" ::frogesport::clearqueue
bind pub * "!compare" ::frogesport::compare
bind pub * "!jämför" ::frogesport::compare
bind pub * "!comparetot" ::frogesport::comparetot
bind pub * "!jämförtot" ::frogesport::comparetot
bind pub * "!help" ::frogesport::help
bind pub * "!hjälp" ::frogesport::help
bind pub * "!queue" ::frogesport::queueques
bind pub * "!köa" ::frogesport::queueques
bind pub * "!stats" ::frogesport::stats
bind pub * "!magi" ::frogesport::spell
bind pub * "!ping" ::frogesport::ping
bind pub * "!recommend" ::frogesport::recommendq
bind pub * "!rensakö" ::frogesport::clearqueue
bind pub * "!report" ::frogesport::report
bind pub * "!spell" ::frogesport::spell
bind pub * "!suggest" ::frogesport::recommendq
bind pub * "!föreslå" ::frogesport::recommendq
bind pub * "!förslag" ::frogesport::recommendq
bind pub * "!hof" ::frogesport::hof
bind pub * "!hofarmesnitt" ::frogesport::topclantotalaverage
bind pub * "!hofarme" ::frogesport::topclantotal
bind pub * "!hofclan" ::frogesport::topclantotal
bind pub * "!hofclanavg" ::frogesport::topclantotalaverage
bind pub * "!tid" ::frogesport::time
bind pub * "!time" ::frogesport::time
bind pub * "!top10" ::frogesport::top10
bind pub * "!topfast" ::frogesport::topfast
bind pub * "!top10arme" ::frogesport::topclanseason
bind pub * "!top10armesnitt" ::frogesport::topclanseasonaverage
bind pub * "!top10clan" ::frogesport::topclanseason
bind pub * "!top10clanavg" ::frogesport::topclanseasonaverage
bind pub * "!toptid" ::frogesport::topfast
bind pub * "!topkpm" ::frogesport::topkpm
bind pub * "!topstreak" ::frogesport::topstreak
bind pub * "!version" ::frogesport::version
# User commands from PM
bind msg * "arme" ::frogesport::msgclan
bind msg * "armé" ::frogesport::msgclan
bind msg * "clan" ::frogesport::msgclan
bind msg * "clearqueue" ::frogesport::msgclearqueue
bind msg * "recommend" ::frogesport::msgrecommendq
bind msg * "suggest" ::frogesport::msgrecommendq
bind msg * "föreslå" ::frogesport::msgrecommendq
bind msg * "förslag" ::frogesport::msgrecommendq
bind msg * "köa" ::frogesport::msgqueueques
bind msg * "queue" ::frogesport::msgqueueques
bind msg * "rensakö" ::frogesport::msgclearqueue
# We need the mysqltcl package
package require mysqltcl
namespace eval ::frogesport {
variable version "1.10"
# Include the config file
if {[file exists scripts/frogesport/frogesport-config.tcl]} {
source scripts/frogesport/frogesport-config.tcl
} else {
putlog "Frogesport not loaded: You have to create a config file! Check the example."
return
}
# Disconnect and reconnect to the database. A kind of cleanup/reset of the connection, if the database for some reason has gone.
proc checkdb { } {
if {[info exists ::frogesport::mysql_conn]} {
::mysql::close $::frogesport::mysql_conn
unset ::frogesport::mysql_conn
}
variable mysql_conn [::mysql::connect -db $::frogesport::mysql_dbname -host $::frogesport::mysql_host -user $::frogesport::mysql_user -password $::frogesport::mysql_pass]
}
checkdb
# Run some configuration checks
if {$::frogesport::s_close_behind > [expr $::frogesport::s_question_time-1]} {
variable s_close_behind [expr $::frogesport::s_question_time-1]
}
# All times are supposed to be in ms
variable question_time [expr $::frogesport::s_question_time*1000]
variable clue_time [expr $::frogesport::s_clue_time*1000]
variable time_answer [expr $::frogesport::s_time_answer*1000]
variable pinginterval [expr $::frogesport::s_pinginterval*1000]
variable close_behind [expr $::frogesport::s_close_behind*1000]
# Bot ID should be only the decimals of the configured ID was divided by 100000
if {$::frogesport::i_bot_id < 0 || $::frogesport::i_bot_id > 99999} {
putlog "Frogesport not loaded: Invalid bot ID."
return
}
variable bot_id [format "%05d" $::frogesport::i_bot_id]
# Fix percent if someone set it with a % sign
variable clue_percent [string map {"%" ""} $::frogesport::clue_percent]
# Set and fix some variables
variable season_code ""
if {![info exists quesqueue]} {
variable quesqueue ""
}
# Load ::frogesport::admins and ::frogesport::prizes
variable dbadmins [::mysql::sel $::frogesport::mysql_conn "SELECT user_nick FROM users WHERE user_class=0" -list]
variable prizes [::mysql::sel $::frogesport::mysql_conn "SELECT * FROM prizes" -list]
# Remember the classes
variable classes [::mysql::sel $::frogesport::mysql_conn "SELECT * FROM classes" -list]
# procedure to authenticate admins
proc checkauth { nick } {
switch $::frogesport::auth_method {
"1" {
# Check if the user is op in the channel and if the user is class 0 in the database
if {[isop $nick $::frogesport::running_chan] && [lsearch -nocase $::frogesport::dbadmins $nick] != "-1"} {
return 1
}
}
"2" {
# Check if the user is op in the channel and is in the configured list of admins
if {[isop $nick $::frogesport::running_chan] && [lsearch -nocase $::frogesport::admins $nick] != "-1"} {
return 1
}
}
"3" {
# Check if the user is op in the admin channel
if {[isop $nick $::frogesport::admin_chan]} {
return 1
}
}
default {
putlog "Incorrect authentication method"
}
}
# If the authentication method chosen didn't return 1, return 0
return 0
}
# Procedure to check the class of all users
proc msgupdateclasses { nick host hand arg } {
# Only admins are allowed to use this procedure
if {![checkauth $nick]} {
return
}
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_class=(SELECT cid FROM classes WHERE clas_points<=user_points_total ORDER BY clas_points DESC LIMIT 1) WHERE user_class!='0'"
if {$nick != ""} {
putserv "PRIVMSG $nick :Alla användares klasser stämmer nu."
}
}
# Update all users classes on rehash or startup. We have to put it here, since the procedure have to be defined before we can call it
msgupdateclasses "" "" "" ""
# Send a message every half hour, maybe to tell people why the bot is stopped
proc startmess { nick host hand chan arg } {
if {![checkauth $nick]} {
return
}
if {[llength $arg] < 2} {
putserv "NOTICE $nick :!startmess <intervall> <meddelande>"
return
}
# Stop the previous message if it's already running
if {[info exists ::frogesport::messageId]} {
after cancel $::frogesport::messageId
}
# How often? In minutes.
variable s_periodic_message [lindex $arg 0]
# What should the message be?
variable periodic_message [lrange $arg 1 end]
sendmess
}
proc sendmess {} {
putserv "PRIVMSG $::frogesport::running_chan :$::frogesport::periodic_message"
variable messageId [after [expr $::frogesport::s_periodic_message*60000] ::frogesport::sendmess]
}
proc stopmess { nick host hand chan arg } {
if {![checkauth $nick]} {
return
}
after cancel $::frogesport::messageId
}
# Start the bot
proc startquiz { nick host hand chan arg } {
# Only admins are allowed to use this procedure
if {![checkauth $nick]} {
return
}
if {![string equal -nocase $chan $::frogesport::running_chan]} {
putserv "PRIVMSG $chan :\003${::frogesport::color_text},${::frogesport::color_background}Det här är inte kanalen som boten är confad att köras i!"
return
}
# We won't start the quiz if it's already running
if {[info exists ::frogesport::is_running] && $::frogesport::is_running} {
putserv "PRIVMSG $chan :\003${::frogesport::color_text},${::frogesport::color_background}Boten körs redan i $::frogesport::running_chan!"
return
}
# Restart the temporary ID counter
variable cur_temp_id 0
# Remember that we are running
variable is_running 1
# Create or empty the previous winner variable
variable currentcorrect ""
# Clear all the temporary IDs in the database for this bot ID
::mysql::exec $::frogesport::mysql_conn "UPDATE questions SET ques_tempid='0.0' WHERE ques_tempid LIKE '%[::mysql::escape $::frogesport::mysql_conn $::frogesport::bot_id]'"
# Binding for detecting answers (matches everything)
bind pubm * {*} ::frogesport::checkanswer
# Ask the first question
askquestion
}
# Clear the question queue.
proc clearqueue { nick host hand chan arg } {
msgclearqueue $nick $host $hand $arg $chan
}
proc msgclearqueue { nick host hand arg {sendto ""} } {
# Where should we send the replies?
if {$sendto == ""} {
set sendto $nick
}
# Check if the user is allowed to clear the queue.
switch $::frogesport::queueques_who {
0 {
return
}
1 {
if {![checkauth $nick]} {
return
}
}
2 {
if {![checkauth $nick] && ![isop $nick $::frogesport::running_chan]} {
return
}
}
3 {
if {![checkauth $nick] && ![isop $nick $::frogesport::running_chan] && ![isvoice $nick $::frogesport::running_chan]} {
return
}
}
}
set ::frogesport::quesqueue ""
putserv "PRIVMSG $sendto :\003${::frogesport::color_text},${::frogesport::color_background}Frågekön rensad."
}
# Put specific questions in the queue
proc queueques { nick host hand chan arg } {
msgqueueques $nick $host $hand $arg $chan
}
proc msgqueueques { nick host hand arg {sendto ""} } {
# Where should we send the replies?
if {$sendto == ""} {
set sendto $nick
}
# Check if the user is allowed to add questions.
# 0: Nobody.
# 1: Admins (see auth_method above.)
# 2: OPs and admins.
# 3: Voiced users, OPs and admins.
# 4: Anyone.
switch $::frogesport::queueques_who {
0 {
return
}
1 {
if {![checkauth $nick]} {
return
}
}
2 {
if {![checkauth $nick] && ![isop $nick $::frogesport::running_chan]} {
return
}
}
3 {
if {![checkauth $nick] && ![isop $nick $::frogesport::running_chan] && ![isvoice $nick $::frogesport::running_chan]} {
return
}
}
}
# Check if the question queue is too long.
if {[llength $::frogesport::quesqueue] >= $::frogesport::queueques_num} {
putserv "PRIVMSG $sendto :\003${::frogesport::color_text},${::frogesport::color_background}Ingen fråga tillagd, kön är full."
return
}
# Time to process the arguments.
if {[llength [split $arg]] == 1} {
# Only one argument.
if {[regexp "^\[0-9\]+\$" $arg]} {
# That argument is numerical, so just put that ID in the queue and return.
lappend ::frogesport::quesqueue [split $arg]
putserv "PRIVMSG $sendto :\003${::frogesport::color_text},${::frogesport::color_background}En fråga köad."
return
}
}
# There are more than one argument, or that one argument is non numerical.
if {[regexp "^\[0-9\]+\$" [lindex [split $arg] 0]]} {
set numques [lindex [split $arg] 0]
# Remove the number from the argument.
set arg [lrange [split $arg] 1 end]
} else {
# If we don't have any number, we should add 1/3 of the maximum queue length.
set numques [expr int(ceil($::frogesport::queueques_num/3))]
}
# Check if we're allowed to add this many questions to the queue.
if {$numques > $::frogesport::queueques_num - [llength $::frogesport::quesqueue]} {
set numques [expr $::frogesport::queueques_num - [llength $::frogesport::quesqueue]]
}
# The first argument is (now) non numerical and what we should search for.
switch $::frogesport::queueques_searchwhere {
1 {
set tempques [::mysql::sel $::frogesport::mysql_conn "SELECT qid FROM questions WHERE ques_category LIKE '%[::mysql::escape $::frogesport::mysql_conn $arg]%' ORDER BY RAND() LIMIT $numques" -list]
}
2 {
set tempques [::mysql::sel $::frogesport::mysql_conn "SELECT qid FROM questions WHERE ques_question LIKE '%[::mysql::escape $::frogesport::mysql_conn $arg]%' ORDER BY RAND() LIMIT $numques" -list]
}
3 {
set tempques [::mysql::sel $::frogesport::mysql_conn "SELECT qid FROM questions WHERE ques_category LIKE '%[::mysql::escape $::frogesport::mysql_conn $arg]%' OR ques_question LIKE '%[::mysql::escape $::frogesport::mysql_conn $arg]%' ORDER BY RAND() LIMIT $numques" -list]
}
}
switch [llength $tempques] {
0 {
putserv "PRIVMSG $sendto :\003${::frogesport::color_statsnumber},${::frogesport::color_background}Inga\003${::frogesport::color_text} frågor hittade."
}
1 {
putserv "PRIVMSG $sendto :\003${::frogesport::color_statsnumber},${::frogesport::color_background}En\003${::frogesport::color_text} fråga köad."
lappend ::frogesport::quesqueue {*}$tempques
}
default {
putserv "PRIVMSG $sendto :\003${::frogesport::color_statsnumber},${::frogesport::color_background}[llength $tempques]\003${::frogesport::color_text} frågor köade."
lappend ::frogesport::quesqueue {*}$tempques
}
}
}
# Ask a question
proc askquestion { } {
# Set the current temporary ID
variable cur_temp_id [expr $::frogesport::cur_temp_id+1]
# Check if we have a question in the question queue.
set question ""
while {[llength $::frogesport::quesqueue] && $question == ""} {
set question [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT qid, ques_category, ques_question FROM questions WHERE qid='[lindex $::frogesport::quesqueue 0]'" -list] 0]
# Remove the question we just got from the queue.
set ::frogesport::quesqueue [lrange $::frogesport::quesqueue 1 end]
}
# OK, if we didn't get a question from the queue, get one at random.
if {$question == ""} {
# Get the number of questions we have and multiply it by a random number to get a random offset
set offset [::mysql::sel $::frogesport::mysql_conn "SELECT floor(RAND() * COUNT(*)) from questions" -list]
# Get the question, use the previous offset to get a random one
set question [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT qid, ques_category, ques_question FROM questions LIMIT [lindex $offset 0],1" -list] 0]
}
# Set the temporary ID on the question in the database
::mysql::exec $::frogesport::mysql_conn "UPDATE questions SET ques_tempid='$::frogesport::cur_temp_id.$::frogesport::bot_id' WHERE qid='[lindex $question 0]'"
# Get the answers, the default ones first if there are any, otherwise a random order.
variable answers [::mysql::sel $::frogesport::mysql_conn "SELECT answ_answer FROM answers WHERE answ_question='[lindex $question 0]' ORDER BY answ_prefer DESC, RAND()" -flatlist]
# Ask the question
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_text},${::frogesport::color_background}\037$::frogesport::cur_temp_id\037: [lindex $question 1]: [lindex $question 2]"
# Remember the permanent ID of the question for later use.
variable cur_perm_id [lindex $question 0]
# The current question is not answered
variable answered 0
# Set current time, to be able to measure the time it took for the user to answer
variable question_start_time [clock clicks -milliseconds]
# Start the clue timer
variable c_after_id [after $::frogesport::clue_time ::frogesport::giveclue]
# Set the question timeout
variable q_after_id [after $::frogesport::time_answer ::frogesport::nocorrect]
}
proc giveclue { {answer ""} {clue_number 0} } {
# Check if we should continue on a previous clue
if { $answer == "" } {
# Set the answer to the first one. They are in the correct order from the SQL query
set answer [lindex $::frogesport::answers 0]
}
# Split it in words
# Check if the clue is four digits, these should have a special clue
if {[string length $answer] == "4" && [string is integer $answer]} {
set maskedclue "[string index $answer 0]_[string index $answer 2]_"
} else {
set words [split $answer]
foreach word $words {
# Count the letters
set letters [string length $word]
# Determine the number of characters to keep
set keepchars [expr int($letters*[lindex $::frogesport::clue_percent $clue_number]/100)]
# Create the word
set maskedword [string range $word 0 $keepchars][string repeat "_" [expr $letters-$keepchars-1]]
if {[string length $maskedword] == 1} {
set maskedword "_"
}
lappend maskedclue $maskedword
}
# Join the list to one string
set maskedclue [join $maskedclue]
}
# Send it
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_text},${::frogesport::color_background}Ledtråd: $maskedclue"
# Check if we have more clues planned
if {[llength $::frogesport::clue_percent] > [expr $clue_number+1]} {
variable c_after_id [after $::frogesport::clue_time [list ::frogesport::giveclue $answer [expr $clue_number+1]]]
}
}
proc nocorrect { } {
# Should the bot tell all the correct answers?
set correctanswer ""
if {$::frogesport::give_answer == "1"} {
if {[llength $::frogesport::answers] > 1} {
set correctanswer " De rätta svaren var: [join $::frogesport::answers ", "]"
} else {
set correctanswer " Det rätta svaret var: [join $::frogesport::answers ", "]"
}
}
# No one can get a correct answer for this.
variable answered 1
# Start the close behind timer if it's enabled.
if {$::frogesport::s_close_behind} {
variable close_behind_time [clock clicks -milliseconds]
variable close_behind_id [after $::frogesport::close_behind ::frogesport::closebehind]
# Remember that we are collecting nicks
variable close_behind_collecting 1
# Noone answered correctly, everyone is eligeble to be second
variable last_correct_nick ""
# We have to set this here to be able to search this list later
variable correct_close_nick ""
}
# If someone for some reason set the clue timer higher than the answer time, clear the clue timer
after cancel $::frogesport::c_after_id
# Tell everyone the time is up
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_text},${::frogesport::color_background}Ingen svarade rätt inom $::frogesport::s_time_answer sekunder.$correctanswer"
# Start the timer for the next question
variable nq_after_id [after $::frogesport::question_time ::frogesport::askquestion]
# If someone had a streak, they don't anymore
variable currentcorrect ""
}
proc checkanswer { nick host hand chan arg } {
# If we're not running but the bind for some reason is still here, unbind it. This should never happen.
if {!$::frogesport::is_running} {
catch { unbind pubm * {*} ::frogesport::checkanswer }
return
}
set origarg [string trim $arg]
# Replace *, ?, [ and ] with the escaped characters
set arg [string map { "*" "\\\*" "?" "\\\?" "\[" "\\\[" "\]" "\\\]" } [string trim $arg]]
# Check if the answer was correct
if {[info exists ::frogesport::answers] && [lsearch -nocase $::frogesport::answers $arg] >= 0} {
# Check whether or not this is the first correct answer
if {$::frogesport::answered} {
# Remember the nick if the close behind feature is enabled, we're collecting nicks, it's this users first close behind answer and the user wasn't the first answerer
if {$::frogesport::s_close_behind && $::frogesport::close_behind_collecting && [lsearch $::frogesport::correct_close_nick $nick] < 0 && $nick != $::frogesport::last_correct_nick} {
if {![info exists ::frogesport::correct_close]} {
variable correct_close ""
}
lappend ::frogesport::correct_close_nick $nick
lappend ::frogesport::correct_close_time [expr double([clock clicks -milliseconds]-$::frogesport::close_behind_time)/1000]
}
} else {
# Check if the user answered a question in another channel a short while ago
# Get new user information
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass, user_lastactive_chan, user_kpm_max, clan_name FROM users LEFT JOIN clanmembers ON uid=clme_uid LEFT JOIN clans ON clme_clid=clid AND clme_member='yes' WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
if {[lindex $curuser 10] != $::frogesport::running_chan && [expr [lindex $curuser 8]+$::frogesport::s_channel_switch_time] > [clock seconds]} {
putserv "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick \003${::frogesport::color_text}hade rätt, men måste vänta [expr [lindex $curuser 8]+$::frogesport::s_channel_switch_time-[clock seconds]] sekunder till för att kunna svara i den här kanalen."
return
}
set uid [lindex $curuser 0]
# Remember that we have got a correct answer
variable answered 1
# How long did it take for the user to answer?
set answertime [expr double([clock clicks -milliseconds]-$::frogesport::question_start_time)/1000]
# How many keys per minute was that?
set answerkpm [expr double(round(1000*([string length $origarg]/($answertime/60))))/1000]
# Stop the pending clue and that no answer was given procedures
after cancel $::frogesport::c_after_id
after cancel $::frogesport::q_after_id
# Start the close behind timer if it's enabled.
if {$::frogesport::s_close_behind} {
variable close_behind_time [clock clicks -milliseconds]
variable close_behind_id [after $::frogesport::close_behind ::frogesport::closebehind]
# Remember that we are collecting nicks
variable close_behind_collecting 1
# Remember who answered correctly
variable last_correct_nick $nick
# Empty some variables
variable correct_close_nick ""
variable correct_close_time ""
}
# Is this a new user?
if {$curuser == ""} {
# We have a new user!
::mysql::exec $::frogesport::mysql_conn "INSERT INTO users (user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_lastactive_chan, user_kpm_max)\
VALUES ('[::mysql::escape $::frogesport::mysql_conn $nick]', 1, 1, '$answertime', 1, 1, 1, [clock seconds], '[::mysql::escape $::frogesport::mysql_conn $::frogesport::running_chan]', '$answerkpm')"
set uid [::mysql::insertid $::frogesport::mysql_conn]
variable currentcorrect [list $nick "1"]
set rankmess ""
} else {
# The user's been here before, check for a streak
if {[string equal $nick [lindex $::frogesport::currentcorrect 0]]} {
# The user is on a streak, add the user to the current streakmeater
variable currentcorrect [list $nick [expr [lindex $::frogesport::currentcorrect 1]+1]]
} else {
# This use is not on a streak, reset the variable
variable currentcorrect [list $nick "1"]
}
# Check if we should update the users fastest time
set updatetime ""
if { $answertime < [lindex $curuser 4] } {
set updatetime ", user_time='$answertime'"
}
# Check if we should update the kpm record
set updatekpmmax ""
if { $answerkpm > [lindex $curuser 11] } {
set updatekpmmax ", user_kpm_max='$answerkpm'"
}
# Check if we should update the users longest streak
set updatestreak ""
if { [lindex $::frogesport::currentcorrect 1] > [lindex $curuser 5] } {
set updatestreak ", user_inarow='[lindex $::frogesport::currentcorrect 1]'"
}
# Check if we should update the class
set updateclass ""
set updatemana ""
if {[lsearch -index 1 $::frogesport::classes [expr [lindex $curuser 3]+1]] >= 0 && [lindex $curuser 7] != "0"} {
set newclass [::mysql::sel $::frogesport::mysql_conn "SELECT * FROM classes WHERE clas_points='[expr [lindex $curuser 3]+1]'" -flatlist]
set updateclass ", user_class='[lindex $newclass 0]'"
# We should also add mana, this isn't done in the next if statement because we still use the old class
set updatemana ", user_mana=user_mana+1"
}
# Check if we should add more mana
if {[lindex $curuser 6] < [lindex [lindex $::frogesport::classes [lindex $curuser 7]] 3]} {
set updatemana ", user_mana=user_mana+1"
}
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_points_season=user_points_season+1, user_points_total=user_points_total+1, user_lastactive='[clock seconds]', user_lastactive_chan='[::mysql::escape $::frogesport::mysql_conn $::frogesport::running_chan]'$updatetime$updatestreak$updateclass$updatemana$updatekpmmax WHERE uid='$uid'"
::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(user_points_season) FROM users WHERE user_points_season>'[expr [lindex $curuser 2]+1]'"
set rank [expr [lindex [::mysql::fetch $::frogesport::mysql_conn] 0]+1]
::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(user_points_season) FROM users WHERE user_points_season='[expr [lindex $curuser 2]+1]'"
set onelessrank [lindex [::mysql::fetch $::frogesport::mysql_conn] 0]
# Count how many users we currently have
set totalusers [::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(uid) FROM users" -list]
set rankmess " Rank: $rank av $totalusers."
}
set curclass ""
if {[info exists curuser] && $curuser != ""} {
set curclass " \[[lindex [lindex $::frogesport::classes [lindex $curuser 7]] 2]\]"
}
# Check if the user has a custom class
if {[lindex $curuser 9] != ""} {
set curclass " \[[lindex $curuser 9]\]"
}
set clanname ""
# Check if the user has a custom class
if {[lindex $curuser 12] != ""} {
set clanname "\[[lindex $curuser 12]\] "
}
# Tell everyone the time is up
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_text},${::frogesport::color_background}Vinnare: \003${::frogesport::color_nick}$nick\003${::frogesport::color_class}$curclass $clanname\003${::frogesport::color_text}Svar: \003${::frogesport::color_answer}$origarg \003${::frogesport::color_text}Tid: [string map {, . . ,} ${answertime}]s. KPM: [string map {, . . ,} ${answerkpm}] I rad: [lindex $::frogesport::currentcorrect 1]. Säsongspoäng: [expr [lindex $curuser 2]+1].$rankmess Total poäng: [expr [lindex $curuser 3]+1]."
if {[info exists updateclass] && $updateclass != ""} {
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick\003${::frogesport::color_text} har gått upp till level [lindex $newclass 0] och är nu rankad som [lindex $newclass 2]! [lindex $newclass 4]"
}
# If the user gained a rank, tell everyone
if {[info exists onelessrank] && $onelessrank > 1} {
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick \003${::frogesport::color_text}har stigit i ranking: $rank av $totalusers."
}
# If the user gained a prize, tell everyone
if {[set prize [lsearch -inline -all -index 1 $::frogesport::prizes [lindex $::frogesport::currentcorrect 1]]] != ""} {
# If there's multiple answers, choose one at random
set randprize [lindex [lindex $prize [expr int([llength $prize]*rand())]] 2]
putserv "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick \003${::frogesport::color_text}har svarat rätt \003${::frogesport::color_statsnumber}[lindex $::frogesport::currentcorrect 1]\003${::frogesport::color_text} gånger i rad och får $randprize som pris!"
}
# If we're configured to do so, save the answer.
if {[string is true $::frogesport::save_answers]} {
::mysql::exec $::frogesport::mysql_conn "INSERT INTO correctanswers (coan_qid, coan_uid, coan_channel, coan_answer, coan_time, coan_date)\
VALUES ('$::frogesport::cur_perm_id', '$uid', '[::mysql::escape $::frogesport::mysql_conn $::frogesport::running_chan]', \
'[::mysql::escape $::frogesport::mysql_conn $origarg]', '$answertime', UNIX_TIMESTAMP(NOW()))"
}
# Start the timer for the next question
variable nq_after_id [after $::frogesport::question_time ::frogesport::askquestion]
}
}
}
# Tell the users how far behind they were
proc closebehind { } {
# We are no longer collecting nicks
variable close_behind_collecting 0
# Check if there are any users who are close behind
if {[info exists ::frogesport::correct_close_nick]} {
set num_nicks [llength $::frogesport::correct_close_nick]
for {set i 0} {$i<$num_nicks} {incr i} {
lappend output "\003${::frogesport::color_nick},${::frogesport::color_background}[lindex $::frogesport::correct_close_nick $i] \003${::frogesport::color_text}var \003${::frogesport::color_statsnumber}[string map {, . . ,} [lindex $::frogesport::correct_close_time $i]]\003${::frogesport::color_text} sekunder efter"
}
putserv "PRIVMSG $::frogesport::running_chan :[join $output ", "]."
unset $::frogesport::correct_close_nick
unset $::frogesport::correct_close_time
}
}
proc stopquiz { nick host hand chan arg } {
# Only admins are allowed to use this procedure
if {![checkauth $nick]} {
return
}
# If the is_running variable doesn't exist or is 0, the bot isn't running
if {![info exists ::frogesport::is_running] || !$::frogesport::is_running} {
# Just in case, clear all pending after commands.
foreach i [split [after info]] {
after cancel $i
}
return
}
# We're not running anymore
variable is_running 0
variable cur_temp_id 0
# Unbind the answer checker to save CPU cycles
catch { unbind pubm * {*} ::frogesport::checkanswer }
# Clear all pending after commands.
foreach i [split [after info]] {
after cancel $i
}
putnow "PRIVMSG $::frogesport::running_chan :\003${::frogesport::color_text},${::frogesport::color_background}Slut! :("
}
# Clear all pending "after" commands, should never have to be used
proc clear { nick host hand chan arg } {
# Only admins are allowed to use this procedure
if {![checkauth $nick]} {
return
}
set running [split [after info]]
foreach i $running {
after cancel $i
}
variable is_running 0
# Unbind the answer checker to save CPU cycles
catch { unbind pubm * {*} ::frogesport::checkanswer }
}
# If the bot has stopped for some reason, it should be possible to just continue without having to restarting it
proc continue { nick host hand chan arg } {
# Only admins are allowed to use this procedure
if {![checkauth $nick]} {
return
}
# Check if the current id exists
if {![info exists ::frogesport::cur_temp_id] || !$::frogesport::cur_temp_id} {
putserv "PRIVMSG $chan :\003${::frogesport::color_text},${::frogesport::color_background}Det finns inget att fortsätta på!"
return
}
# Clear all pending after commands, in case the command was used when it wasn't needed. This effectively just cancels the current question and starts a new one
set running [split [after info]]
foreach i $running {
after cancel $i
}
# Fix some variables
variable is_running 1
variable answers ""
# If someone had a streak, they don't anymore
variable currentcorrect ""
# Binding for detecting answers (matches everything)
bind pubm * {*} ::frogesport::checkanswer
# Ask the next question
askquestion
}
proc ping { nick host hand chan arg } {
if {![info exist ::frogesport::lastping] || $::frogesport::lastping+$::frogesport::pinginterval < [clock clicks -milliseconds]} {
putnow "PRIVMSG $chan :$nick: Pong!"
variable lastping [clock clicks -milliseconds]
}
}
proc stats { nick host hand chan arg } {
set user [lindex [split $arg] 0]
# Check if we have an argument (not with eachother, not that type of argument), if not, use the users nick
if {$user == ""} {
set user $nick
}
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass, user_kpm_max FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $user]' LIMIT 1" -list] 0]
# Check if the user exists
if {$curuser == ""} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}$user har inte svarat rätt på någon fråga än."
return
}
# Count the number of users in the database
set totalusers [::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(uid) FROM users" -list]
# Count the number of users before the current user
set usersabove [::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(user_points_season) FROM users WHERE user_points_season>'[lindex $curuser 2]'" -list]
# Get info about the user above the current one
set aboveuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT `user_nick`,`user_points_season` FROM `users` WHERE `user_points_season`=(SELECT MIN(`user_points_season`) FROM `users`WHERE `user_points_season`>'[lindex $curuser 2]') LIMIT 1" -list] 0]
# Check if we're in the lead
if {$aboveuser != ""} {
set pointsfrom "\003${::frogesport::color_statsnumber}[expr [lindex $aboveuser 1]-[lindex $curuser 2]]\003${::frogesport::color_text} poäng ifrån \003${::frogesport::color_nick}[lindex $aboveuser 0]"
} else {
set pointsfrom "\003${::frogesport::color_nick}[lindex $curuser 1]\003${::frogesport::color_text} leder!"
}
# Check if the user has a custom class
if {[set curclass [lindex $curuser 9]] == ""} {
set curclass [lindex [lindex $::frogesport::classes [lsearch -index 0 $::frogesport::classes [lindex $curuser 7]]] 2]
}
# Check if we have a date for the current user
set lastpoint ""
if {[lindex $curuser 8]} {
set lastpoint "Senaste poäng: \003${::frogesport::color_statsnumber}[clock format [lindex $curuser 8] -format "%Y-%m-%d %H:%M:%S"]\003${::frogesport::color_text}. "
}
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Statistik för \003${::frogesport::color_nick}[lindex $curuser 1]\003${::frogesport::color_text}:\
Snabbaste tid: \003${::frogesport::color_statsnumber}[string map {, . . ,} [lindex $curuser 4]]\003${::frogesport::color_text} sekunder.\
Bästa streak: \003${::frogesport::color_statsnumber}[lindex $curuser 5]\003${::frogesport::color_text}.\
Högsta KPM: \003${::frogesport::color_statsnumber}[string map {, . . ,} [lindex $curuser 10]]\003${::frogesport::color_text}.\
Säsongspoäng: \003${::frogesport::color_statsnumber}[lindex $curuser 2]\003${::frogesport::color_text}.\
Total poäng: \003${::frogesport::color_statsnumber}[lindex $curuser 3]\003${::frogesport::color_text}.\
Klass: \003${::frogesport::color_class}$curclass\003${::frogesport::color_text}.\
Level: \003${::frogesport::color_statsnumber}[lindex $curuser 7]\003${::frogesport::color_text}.\
Mana: \003${::frogesport::color_statsnumber}[lindex $curuser 6]\003${::frogesport::color_text}.\
Rankad: \003${::frogesport::color_statsnumber}[expr [lindex $usersabove 0]+1]\003${::frogesport::color_text} av \003${::frogesport::color_statsnumber}[lindex $totalusers 0]\003${::frogesport::color_text}.\
$lastpoint$pointsfrom"
}
proc spell { nick host hand chan arg } {
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
# Check if the user exists
if {$curuser == ""} {
# Notify user and return
putserv "NOTICE $nick :Ditt nick finns inte i databasen."
return
}
# Check if the user has the required level to use spells, class 0 is God.
if {[lindex $curuser 7] < $::frogesport::spell_level && [lindex $curuser 7] != "0"} {
# Notify user and return
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du måste vara level \003${::frogesport::color_statsnumber}$::frogesport::spell_level\003${::frogesport::color_text} för att använda magier, du är bara på \003${::frogesport::color_statsnumber}[lindex $curuser 7]"
return
}
set spell [lindex [split $arg] 0]
# Set some notification variables
set helpvar "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Användning av !spell:\
!spell answer (få svaret på den nuvarande frågan, kostar $::frogesport::cost_answer mana),\
!spell steal <\003${::frogesport::color_nick}användare\003${::frogesport::color_text}> (stjäl $::frogesport::steal_points poäng, kostar $::frogesport::cost_steal mana),\
!spell give <\003${::frogesport::color_nick}nick\003${::frogesport::color_text}> (ge $::frogesport::give_points points, kostar $::frogesport::cost_give mana),\
!spell prevanswer <id> (få svaret på en tidigare fråga, kostar $::frogesport::cost_prevanswer mana),\
!spell setvoice \[\003${::frogesport::color_nick}användare\003${::frogesport::color_text}\] (ger dig eller en annan användare voice (+v), kostar $::frogesport::cost_setvoice mana)"
set lowmana "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du har inte tillräckligt med mana!"
# Get the user, if any
set user [lindex [split $arg] 1]
switch -glob $spell {
"sno" -
"steal" {
# Check if the user told us who to steal from
if {$user == ""} {
putserv $helpvar
} elseif {![string compare -nocase $user $nick]} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du kan inte stjäla poäng ifrån dig själv!"
} else {
# If the user has less than ::frogesport::cost_steal mana, he's not allowed to steal
if {[lindex $curuser 6] < $::frogesport::cost_steal} {
putserv $lowmana
} else {
# Get info about the user who's being robbed
set robbeduser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $user]' LIMIT 1" -list] 0]
# If the robbed user has less than ::frogesport::steal_points points, steal only them
if {[lindex $robbeduser 2] < $::frogesport::steal_points} {
set stealpoints [lindex $robbeduser 2]
} else {
set stealpoints $::frogesport::steal_points
}
# We don't need to check for class advancements since this only affects the season points and classes are based on total points
# Remove points from the robbed user and add them to the robber, and remove mana from the robber
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_points_season=user_points_season-$stealpoints WHERE uid='[lindex $robbeduser 0]' LIMIT 1"
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_points_season=user_points_season+$stealpoints, user_mana=user_mana-$::frogesport::cost_steal WHERE uid='[lindex $curuser 0]' LIMIT 1"
# Tell everyone and notify the stealer that the deed is done
putserv "PRIVMSG $chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick\003${::frogesport::color_text} stal $stealpoints poäng från \003${::frogesport::color_nick}$user"
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du stal \003${::frogesport::color_statsnumber}$stealpoints\003${::frogesport::color_text} poäng från \003${::frogesport::color_nick}$user\003${::frogesport::color_text},${::frogesport::color_background}, [expr [lindex $curuser 6]-$::frogesport::cost_steal] mana kvar."
}
}
}
"ge" -
"give" {
# Check if the user told us who to give to
if {$user == ""} {
putserv $helpvar
} elseif {![string compare -nocase $user $nick]} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du kan inte ge dig själv poäng!"
} else {
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
# If this user has less than ::frogesport::cost_steal mana, it's not allowed to steal
if {[lindex $curuser 6] < $::frogesport::cost_give} {
putserv $lowmana
} else {
# Get info about the user who's being given points
set givenuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $user]' LIMIT 1" -list] 0]
# If the giving user has less than ::frogesport::give_points points, give only that ammount
if {[lindex $curuser 2] < $::frogesport::give_points} {
set givepoints [lindex $curuser 2]
} else {
set givepoints $::frogesport::give_points
}
# We don't need to check for class advancements since this only affects the season points, and classes are based on total points
# Remove points from the giving user and add them to the lycky one, and remove mana from the giver
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_points_season=user_points_season+$givepoints WHERE uid='[lindex $givenuser 0]' LIMIT 1"
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_points_season=user_points_season-$givepoints, user_mana=user_mana-$::frogesport::cost_give WHERE uid='[lindex $curuser 0]' LIMIT 1"
# Tell everyone and notify the stealer that the deed is done
putserv "PRIVMSG $chan :\003${::frogesport::color_nick},${::frogesport::color_background}$nick\003${::frogesport::color_text} gav $givepoints poäng till \003${::frogesport::color_nick}$user"
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du gav \003${::frogesport::color_statsnumber}$givepoints\003${::frogesport::color_text} poäng till \003${::frogesport::color_nick}$user\003${::frogesport::color_text},${::frogesport::color_background}, [expr [lindex $curuser 6]-$::frogesport::cost_steal] mana kvar."
}
}
}
"svar" -
"answer" {
# Check if there is any answer to get, there isn't if someone answered correctly on the last question
if {![info exists ::frogesport::answers] || $::frogesport::answers == ""} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Det finns inget svar att hämta."
} elseif {[info exists ::frogesport::is_running] && $::frogesport::is_running} {
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
# If this user has less than ::frogesport::cost_answer mana, it's not allowed to get the answers
if {[lindex $curuser 6] < $::frogesport::cost_answer} {
putserv $lowmana
} else {
# Create a list of the correct answers
if {[llength [lindex $::frogesport::answers 1]]} {
set correctanswer "\003${::frogesport::color_text},${::frogesport::color_background}De rätta svaren på fråga \003${::frogesport::color_statsnumber}$::frogesport::cur_temp_id\003${::frogesport::color_text} är: \003${::frogesport::color_answer}"
} else {
set correctanswer "\003${::frogesport::color_text},${::frogesport::color_background}Det rätta svaret på fråga \003${::frogesport::color_statsnumber}$::frogesport::cur_temp_id\003${::frogesport::color_text} är: \003${::frogesport::color_answer}"
}
append correctanswer [join $::frogesport::answers "\003${::frogesport::color_text},${::frogesport::color_background}, \003${::frogesport::color_answer}"]
# Remove mana from the user
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_mana=user_mana-$::frogesport::cost_answer WHERE uid='[lindex $curuser 0]' LIMIT 1"
# Give the user the correct answer(s)
putserv "NOTICE $nick :$correctanswer"
}
}
}
"tidigaresvar" -
"prevanswer" {
# Check if the id is valid. ... This variable is currently named $user since it was previously only used for nicks, but oh well
if {![regexp "^\[0-9\]+\$" $user]} {
putserv $helpvar
return
}
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
if {[lindex $curuser 6] < $::frogesport::cost_prevanswer} {
# If this user has less than ::frogesport::cost_answer mana, it's not allowed to get the answers
putserv $lowmana
return
}
# Check that it's a valid ID
if {$user >= $::frogesport::cur_temp_id && !$::frogesport::answered} {
putserv "PRIVMSG $nick :\003${::frogesport::color_text},${::frogesport::color_background}Du får bara svaret på tidigare frågor!"
return
}
# Get the answers for the question
set numrows [::mysql::sel $::frogesport::mysql_conn "SELECT answers.answ_answer FROM questions LEFT JOIN answers ON questions.qid=answers.answ_question WHERE questions.ques_tempid='$user.$::frogesport::bot_id'"]
# Check if the tempid was valid
if {$numrows == "0"} {
putserv "PRIVMSG $nick :\003${::frogesport::color_text},${::frogesport::color_background}Tyvärr var det för länge sendan den här frågan ställdes."
return
}
while {[set row [::mysql::fetch $::frogesport::mysql_conn]] != ""} {
lappend answers [lindex $row 0]
}
# Create a list of the correct answers
if {[llength $answers] != "1"} {
set correctanswer "\003${::frogesport::color_text},${::frogesport::color_background}De rätta svaren på fråga \003${::frogesport::color_statsnumber}$user\003${::frogesport::color_text} var: \003${::frogesport::color_answer}"
} else {
set correctanswer "\003${::frogesport::color_text},${::frogesport::color_background}Det rätta svaret på fråga \003${::frogesport::color_statsnumber}$user\003${::frogesport::color_text} var: \003${::frogesport::color_answer}"
}
append correctanswer [join $answers "\003${::frogesport::color_text},${::frogesport::color_background}, \003${::frogesport::color_answer}"]
# Remove mana from the user
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_mana=user_mana-$::frogesport::cost_prevanswer WHERE uid='[lindex $curuser 0]' LIMIT 1"
# Give the user the correct answer(s)
putserv "NOTICE $nick :$correctanswer"
}
"voice" -
"setvoice" {
if {$user == ""} {
set user $nick
}
# Check if the user already is voiced
if {[isvoice $user $::frogesport::running_chan]} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}$user har redan voice (+v)"
} else {
# Get info about the current user
set curuser [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT uid, user_nick, user_points_season, user_points_total, user_time, user_inarow, user_mana, user_class, user_lastactive, user_customclass FROM users WHERE user_nick='[::mysql::escape $::frogesport::mysql_conn $nick]' LIMIT 1" -list] 0]
# If this user has less than ::frogesport::cost_setvoice mana, it's not allowed to set voice
if {[lindex $curuser 6] < $::frogesport::cost_setvoice} {
putserv $lowmana
} else {
::mysql::exec $::frogesport::mysql_conn "UPDATE users SET user_mana=user_mana-$::frogesport::cost_setvoice WHERE uid='[lindex $curuser 0]' LIMIT 1"
# Give the user voice
putserv "MODE $::frogesport::running_chan +v $user"
}
}
}
default {
putserv $helpvar
}
}
}
proc report { nick host hand chan arg } {
set tempid [lindex [split $arg] 0]
set comment [join [lrange [split $arg] 1 end]]
# Check that the id only contains numbers and that there is a comment
if {![regexp "^\[0-9\]+\$" $tempid] || $comment == ""} {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}!report <id> <kommentar> - Rapportera en fråga. Du måste ange både ID och en kommentar om varför du rapporterade den."
return
}
# Check if the temporary ID that the user supplied exists
set validid [lindex [::mysql::sel $::frogesport::mysql_conn "SELECT COUNT(ques_tempid) FROM questions WHERE ques_tempid='$tempid.$::frogesport::bot_id' LIMIT 1" -list] 0]
if {$validid} {
# The ID is valid, add the report and notify the user
::mysql::exec $::frogesport::mysql_conn "INSERT INTO reports (repo_qid, repo_comment, repo_user) VALUES((SELECT qid FROM questions WHERE ques_tempid='$tempid.$::frogesport::bot_id' LIMIT 1), '[::mysql::escape $::frogesport::mysql_conn $comment]', '[::mysql::escape $::frogesport::mysql_conn $nick]')"
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Fråga $tempid rapporterad, tack!"
} else {
putserv "NOTICE $nick :\003${::frogesport::color_text},${::frogesport::color_background}Fråga $tempid har inte ställts än, eller så var det för länge sedan."
}
}
proc recommendq { nick host hand chan arg } {
putserv "PRIVMSG $nick :\003${::frogesport::color_text},${::frogesport::color_background}Användning: suggest <källa på frågan>|<kategori>|<fråga>|<svar>\[|<svar>\]... Skriv \u00A7 i början av ett svar för att använda det för att skapa ledtråden. Exempel: recommend Wikipedia-länk|Grodan|I vilken kanal körs grodan?|#grodan|grodan"
}
proc msgrecommendq { nick host hand arg } {
set arg [string trim $arg "| "]
set helpvar "\003${::frogesport::color_text},${::frogesport::color_background}Användning: recommend <källa på frågan>|<kategori>|<fråga>|<svar>\[|<svar>\]... Skriv \u00A7 i början av ett svar för att använda det för att skapa ledtråden. Exempel: recommend Wikipedia-länk|Grodan|I vilken kanal körs grodan?|\u00A7#grodan|grodan"
if {[string equal -nocase "help" $arg] || [string match -nocase "hj?lp" $arg]} {
putserv "PRIVMSG $nick :$helpvar"
return
}