Advertisement
TheRealKingS

karma.tcl

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