X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FCommandStubs.pl;h=5e2754f9864ef9a73f077cefae464d4ea92eb861;hb=34a4d11667b0a734343f82d2adbaac23f738ad18;hp=91dfc721ffeee61586670b7885d10a3c81456b2a;hpb=92c3870d1fd6aa5f063a57cfb690aea12ec77df7;p=infobot.git diff --git a/src/CommandStubs.pl b/src/CommandStubs.pl index 91dfc72..5e2754f 100644 --- a/src/CommandStubs.pl +++ b/src/CommandStubs.pl @@ -280,6 +280,11 @@ sub Modules { $itc =~ s/([^\w\s])/\\$1/g; my $z = join '|', split ' ', $itc; + if ($message =~ /^_stats(\s+(\S+))$/i) { + &textstats_main($2); + return; + } + if ($message =~ /^($z)stats(\s+(\S+))?$/i) { my $type = $1; my $arg = $3; @@ -292,7 +297,6 @@ sub Modules { &DEBUG("not using chan arg") if (!defined $c); my $sum = (&dbRawReturn("SELECT SUM(counter) FROM stats" ." WHERE ".$where ))[0]; - &DEBUG("type => $type, arg => $arg"); if (!defined $arg or $arg =~ /^\s*$/) { # this is way fucking ugly. @@ -319,9 +323,9 @@ sub Modules { } if (defined $sum) { - &pSReply("total count of '$type' on $c: $sum$topstr"); + &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr"); } else { - &pSReply("zero counter for '$type'."); + &pSReply("zero counter for \037$type\037."); } } else { my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats". @@ -348,11 +352,11 @@ sub Modules { my $xtra = ""; if ($total and $good) { my $pct = sprintf("%.01f", 100*(1+$total-$i)/$total); - $xtra = ", ranked $i/$total (percentile: $pct %)"; + $xtra = ", ranked $i\002/\002$total (percentile: \002$pct\002 %)"; } my $pct1 = sprintf("%.01f", 100*$x/$sum); - &pSReply("$arg has said $type $x times ($pct1 %)$xtra"); + &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra"); } return; @@ -845,6 +849,88 @@ sub do_verstats { return; } +sub textstats_main { + my($arg) = @_; + + # even more uglier with channel/time arguments. + my $c = $chan; +# my $c = $chan || "PRIVATE"; + my $where = "channel=".&dbQuote($c) if (defined $c); + &DEBUG("not using chan arg") if (!defined $c); + my $sum = (&dbRawReturn("SELECT SUM(counter) FROM stats" + ." WHERE ".$where ))[0]; + + if (!defined $arg or $arg =~ /^\s*$/) { + # this is way fucking ugly. + &DEBUG("_stats: !arg"); + + my %hash = &dbGetCol("stats", "nick,counter", + $where." ORDER BY counter DESC LIMIT 3", 1); + my $i; + my @top; + + # unfortunately we have to sort it again! + # todo: make dbGetCol return hash and array? too much effort. + my $tp = 0; + foreach $i (sort { $b <=> $a } keys %hash) { + foreach (keys %{ $hash{$i} }) { + my $p = sprintf("%.01f", 100*$i/$sum); + $tp += $p; + push(@top, "\002$_\002 -- $i ($p%)"); + } + } + + my $topstr = ""; + &DEBUG("tp => $tp"); + if (scalar @top) { + $topstr = ". Top ".scalar(@top).": ".join(', ', @top); + } + + if (defined $sum) { + &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr"); + } else { + &pSReply("zero counter for \037$type\037."); + } + } else { + my %hash = &dbGetCol("stats", "type,counter", + "$where AND nick=".&dbQuote($arg) ); + + foreach (keys %hash) { + &DEBUG("_stats: hash{$_} => $hash{$_}"); + # ranking. + my @array = &dbGet("stats", "nick", + $where." ORDER BY counter", 1); + my $good = 0; + my $i = 0; + for($i=0; $i $i, good => $good, total => $total"); + } + + return; + + if (!defined $x) { # !defined. + &pSReply("$arg has not said $type yet."); + return; + } + + my $xtra = ""; + if ($total and $good) { + my $pct = sprintf("%.01f", 100*(1+$total-$i)/$total); + $xtra = ", ranked $i\002/\002$total (percentile: \002$pct\002 %)"; + } + + my $pct1 = sprintf("%.01f", 100*$x/$sum); + &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra"); + } +} + sub nullski { my ($arg) = @_; return unless (defined $arg); foreach (`$arg`) { &msg($who,$_); } }