source: scripts/karma.tcl @ 46

Revision 46, 9.3 KB checked in by djslash, 10 months ago (diff)

First commit for topic.tcl, it has some rough edges that needs to work out

  • Property svn:mime-type set to text/x-tcl
Line 
1# Karma v.06
2#
3
4# Script needs mc.main.tcl
5if {$moretools < "010108"} { die "Karma.tcl requires mc.main.tcl version > 010108" }
6
7# Where's the data file?
8set karma(datfile) "scripts/karma.dat"
9# To debug or not to debug
10set karma(debug) 1
11# counter for the anti-flood
12set karma(lasttime) [expr 60+[rand 91]]
13# maximum of karma's being set by a use in $karma(lasttime) 
14set karma(maxcount) 3 
15# Maximum of karma's displayed when matching
16set karma(maxmatch) 15
17
18bind pubm -|- "*\-\-*" karma_action
19bind pubm -|- "*\+\+*" karma_action
20bind pub - !karma karma_get
21bind pub - !karmamatch karma_match
22bind pub - !karmatop karma_top
23setudef flag karma
24
25proc karma_get { nick host hand chan args } {
26        # Check if karma is enabled on this chan
27        if {[lsearch [channel info $chan] -karma] != -1} { 
28                return 0 
29        }
30        global karma
31        # remove trailing whitespace from arguments
32        set args [string trim [join $args]]
33        # if there arent any args, set args to nickname
34        if {$args == ""} { 
35                set args $nick
36        }
37        # read the karmafile
38        set fs [open $karma(datfile) r]; set init 0;
39        while {![eof $fs]} { set karmalist($init) [gets $fs]; incr init; }
40        close $fs;
41        set hoev_karma 0
42        # search for the requested karma
43        foreach e [lsort [array names karmalist]] {
44                set blub [split $karmalist($e) #]
45                if {[string tolower $args]==[lindex $blub 0]} { set hoev_karma [lindex $blub 1]; }
46        }
47        # putquick "PRIVMSG $chan :args has a karma of: $hoev_karma"
48        putquick "PRIVMSG $chan :$args heeft een karma van: $hoev_karma"
49        return 0;
50}
51
52proc karma_match { nick host hand chan args } {
53        # Check if karma is enabled on this chan
54        if {[lsearch [channel info $chan] -karma] != -1} {
55                return 0
56        }
57        global karma
58        # remove trailing whitespace from arguments
59        set args [string trim [filt [join $args]]]
60        # read the karmafile
61        set fs [open $karma(datfile) r]; set init 0;
62        while {![eof $fs]} { set karmalist($init) [gets $fs]; incr init; }
63        close $fs;
64        set init 0
65        # search for the requested karma
66        foreach e [lsort [array names karmalist]] {
67                set blub [split $karmalist($e) #]
68                if {[string match -nocase $args [lindex $blub 0]]} { set match($init) $karmalist($e); incr init; }
69        }
70        if {[array size match] > $karma(maxmatch)} {
71                # putquick "PRIVMSG $chan :$args has too many results ([array size match]), improve your request"
72                putquick "PRIVMSG $chan :$args leverde teveel resultaten ([array size match]), verbeter je zoekopdracht"
73                return 0
74        } elseif {[array size match] == 0} {
75                # putquick "PRIVMSG $chan :$args didn't have any results, too bad!"
76                putquick "PRIVMSG $chan :$args leverde helemaal geen resultaten, jammer joh!"
77                return 0
78        } else {
79                set output ""
80                foreach m [array names match] {
81                        set bla [split $match($m) #]
82                        set output "$output ([lindex $bla 0]: [lindex $bla 1])"
83                }
84                putquick "PRIVMSG $chan :Karma's ([array size match]/[array size karmalist]):$output"
85        }
86}
87
88proc karma_top { nick host hand chan args } {
89        # Check if karma is enabled on this chan
90        if {[lsearch [channel info $chan] -karma] != -1} {
91                return 0
92        }
93        global karma
94        # read the karmafile
95        set fs [open $karma(datfile) r]; set init 0;
96        while {![eof $fs]} { set karmalist($init) [gets $fs]; incr init; }
97        close $fs;
98        # turn around the data, i.e. karma#111 -> 111 karma
99        foreach e [lsort [array names karmalist]] {
100                set blub [split $karmalist($e) #]
101                lappend karmastack "[lindex $blub 1] [list [lindex $blub 0]]"
102                # putloglev 8 * "[lindex $blub 1] [lindex $blub 0]"
103        }
104        # sort the list to get the highest number on top
105        set karmastack [lsort -decreasing -dictionary $karmastack]
106        set output "Karma top 5:"
107        for {set init 0} {$init<5} {incr init} {
108                set karmaoutput [lindex $karmastack $init]
109                set output "$output ([lindex $karmaoutput 1]: [lindex $karmaoutput 0])"
110        }
111        # now turn it around and get the bottom numbers
112        set output "$output // Karma bottom 5:"
113        set karmastack [lsort -command compare $karmastack]
114        for {set init 1} {$init<6} {incr init} {
115                set karmaoutput [lindex $karmastack $init]
116                set output "$output ([lindex $karmaoutput 1]: [lindex $karmaoutput 0])"
117        }
118        putquick "PRIVMSG $chan :$output"
119}
120
121proc karma_vs { nick host hand chan args } {
122        # Check if karma is enabled on this chan
123        if {[lsearch [channel info $chan] -karma] != -1} {
124                return 0
125        }
126        global karma
127        # read the karmafile
128        set fs [open $karma(datfile) r]; set init 0;
129        while {![eof $fs]} { set karmalist($init) [gets $fs]; incr init; }
130        close $fs;
131        set karma1 [lrange ] 
132}
133
134
135proc karma_action { nick host hand chan args } {
136        if {[lsearch [channel info $chan] -karma] != -1} { return 0 }
137        global karma karmalast karmacount;
138        set plusmin "0";
139        set args [string tolower [filter -- $args]];
140        if {$karma(debug)} { putloglev 8 * "\[karma_debug\] args = $args" }
141        set fs [open $karma(datfile) r];
142        while {![eof $fs]} { set line [split [gets $fs] "#"]; set karmalist([lindex $line 0]) [lindex $line 1]; }
143        close $fs;
144        if {[array size karmalist] == 0} { putloglev 6 * "\[karma\] ERROR: karmalist is empty, something went wrong!"; return 0 }
145
146        if {[llength [set inline [regexp -inline -- {([\w\-]+([\\]+ [^ ]+)*)(\+\+|\-\-)|(\w\-+)(\+\+|\-\-)|\\\[(.+)([^\\\#]+)*\\\](\+\+|\-\-)} $args]]] != 0} {
147 
148                if {[set karmaitem [lindex $inline 1]] == "" && [set karmaitem [lindex $inline 6]] == ""} {
149                        putloglev 6 * "\[karma\] ERROR: karmaitem not set! inline = $inline"; return 0
150                };
151                # I don't like the double join, but otherwise karmaitem would have a backslash (when spaces were escaped)
152                set karmaitem [join [join $karmaitem]]
153                # FIXME: This should be replaced by a better ignore system (user had Grolsch++ in his now playing script, which was annoying)
154                #if {([maskhost $nick!$host] == "*!Lup@*.rug.nl" || [maskhost $nick!$host] == "*!Lup@129.125.133.*") && $karmaitem == "grolsch"} { return 0 }
155
156                if {[array get karmalast $host%$karmaitem] != ""} {
157                        if {[expr [lindex $karmalast($host%$karmaitem) 0]+$karma(lasttime)] > [unixtime]} { return 0 }
158                } else { set karmalast($host%$karmaitem) [unixtime] }
159
160                if {[array get karmacount $host] != ""} {
161                        if {[lindex $karmacount($host) 0] > $karma(maxcount)} { return 0 } else { set karmacount($host) [join [expr $karmacount($host)+1] [unixtime]]}
162                } else { set karmacount($host) [join 1 [unixtime]]}
163
164                if {$karma(debug)} { putloglev 8 * "\[karma_debug\] karmacount($host) = $karmacount($host)" }
165
166                if {[string match "*\+\+" [lindex $inline 0]]} {
167                        set plusmin "+1"
168                                if {$karmaitem == [string tolower $nick]} {
169                                # You can put a penalty in the setting below, when someone karma's himself.
170                                set plusmin "0"
171                                # putquick "PRIVMSG $chan :$nick\: trying to put your ego up, ey?! Guess what, you just lost"
172                                putquick "PRIVMSG $chan :$nick\: Jij egoslet! Beetje je eigen karma ophogen, tss :p (U heeft zojuist verloren...)"
173                        }
174                } elseif {[string match "*\-\-" [lindex $inline 0]]} { 
175                        set plusmin "-1"
176                } else { 
177                        putloglev 6 * "\[karma\] ERROR: string match did not match \+\+ nor \-\-"; return 0
178                }
179                set karmaitem [join $karmaitem]
180
181                if {$karma(debug)} { putloglev 8 * "\[karma_debug\] plusmin = $plusmin !! karmaitem = $karmaitem" }
182
183                if {[llength [array get karmalist $karmaitem]] == 2 } {
184                        set karmalist($karmaitem) "[expr $karmalist($karmaitem) $plusmin]"
185                } else {
186                        set karmalist($karmaitem) "[expr 0 $plusmin]"
187                }
188                putloglev 6 * "\[karma\] $chan <$nick> set karmalist($karmaitem) $karmalist($karmaitem)"
189                # FIXME: This is added just for the karma of 'koffie', it might be more fun if there would be a system that has certain degrees or smth.
190                if {$karmalist($karmaitem) == "2000"} {
191                        # putquick "PRIVMSG $chan :WOW $nick, you just had the questionable honor of putting the karma of $karmalist($karmaitem) to 2000! Congratulations! Buy everyone a beer on this occasion, will you?"
192                        putquick "PRIVMSG $chan :WOW $nick, jij hebt de twijfelachtige eer om de karma van $karmalist($karmaitem) helemaal naar de 2000 te krijgen! GEFELICITEERD! Trakteer jij even iedereen bier?"
193                }
194                set fs [open $karma(datfile) w]
195                foreach item [lsort [array names karmalist]] {
196                        if {$karmalist($item)!=""} { puts $fs [join [list $item [lindex $karmalist($item) 0]] "#"] }
197                }
198                close $fs;
199        } else {
200                putloglev 6 * "\[karma\] We are triggerd, but filtered by regexp."
201        }
202}
203
204bind pub n !karmatest karma_test
205proc karma_test {nick host hand chan args} {
206        set args [string tolower [filter -tcl $args]];
207        putserv "privmsg $chan :$args"
208        set inline [regexp -inline -- {([\w\-]+([\\]+ [^ ]+)*)(\+\+|\-\-)|(\w\-+)(\+\+|\-\-)|\\\[(.+)([^\\\#]+)*\\\](\+\+|\-\-)} $args]
209        putserv "privmsg $chan :$inline"
210        putserv "privmsg $chan :[join [join [lindex $inline 1]]]"
211}
212
213
214proc clean_karmalast {} {
215        global karmalast
216        foreach {akey} [array names karmalast] {
217                if {[expr [lindex $karmalast($akey) 0]+120] < [unixtime]} { unset karmalast($akey) }
218        }
219        if {![string match *clean_karmalast* [utimers]]} { utimer 60 clean_karmalast }
220}
221
222proc clean_karmacount {} {
223        global karmacount
224        foreach akey [array names karmacount] {
225                if {[expr [lindex $karmacount($akey) 1]+120] < [unixtime]} { unset karmacount($akey) }
226        }
227        if {![string match *clean_karmacount* [utimers]]} { utimer 60 clean_karmacount }
228}
229
230if {![string match *clean_karmalast* [utimers]]} { utimer 60 clean_karmalast }
231if {![string match *clean_karmacount* [utimers]]} { utimer 60 clean_karmacount }
232
233# debugging stuff
234proc compare {a b} {
235        set a0 [lindex $a 0]
236        set b0 [lindex $b 0]
237        if {$a0 < $b0} {
238                return -1
239        } elseif {$a0 > $b0} {
240                return 1
241        }
242        return [string compare [lindex $a 1] [lindex $b 1]]
243}
244
Note: See TracBrowser for help on using the repository browser.