-sub textstats_main {
- my($arg) = @_;
-
- # even more uglier with channel/time arguments.
- my $c = $chan;
-# my $c = $chan || 'PRIVATE';
- &DEBUG('not using chan arg') if (!defined $c);
-
- # example of converting from RawReturn to sqlSelect.
- my $where_href = (defined $c) ? { channel => $c } : '';
- my $sum = &sqlSelect('stats', 'SUM(counter)', $where_href);
-
- if (!defined $arg or $arg =~ /^\s*$/) {
- # this is way ugly.
- &DEBUG('_stats: !arg');
-
- my %hash = &sqlSelectColHash('stats', 'nick,counter',
- $where_href,
- ' ORDER BY counter DESC LIMIT 3', 1
- );
- my $i;
- my @top;
-
- # unfortunately we have to sort it again!
- 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 = '';
- if (scalar @top) {
- $topstr = '. Top '.scalar(@top).': '.join(', ', @top);
- }
-
- if (defined $sum) {
- &performStrictReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
- } else {
- &performStrictReply("zero counter for \037$type\037.");
- }
-
- return;
- }
-
- # TODO: add nick to where_href
- my %hash = &sqlSelectColHash('stats', 'type,counter',
- $where_href, ' AND nick='.&sqlQuote($arg)
- );
-
- # this is totally messed up... needs to be fixed... and cleaned up.
- my $total;
- my $good;
- my $ii;
- my $x;
-
- foreach (keys %hash) {
- &DEBUG("_stats: hash{$_} => $hash{$_}");
- # ranking.
- # TODO: convert $where to hash
- my $where = '';
- my @array = &sqlSelect('stats', 'nick', undef, $where.' ORDER BY counter', 1);
- $good = 0;
- $ii = 0;
- for(my $i=0; $i<scalar @array; $i++) {
- next unless ($array[0] =~ /^\Q$who\E$/);
- $good++;
- last;
- }
- $ii++;
-
- $total = scalar(@array);
- &DEBUG(" i => $i, good => $good, total => $total");
- $x .= ' '.$total.'blah blah';
- }
-
-# return;
-
- if (!defined $x) { # !defined.
- &performStrictReply("$arg has not said $type yet.");
- return;
- }
-
- my $xtra = '';
- if ($total and $good) {
- my $pct = sprintf("%.01f", 100*(1+$total-$ii)/$total);
- $xtra = ", ranked $ii\002/\002$total (percentile: \002$pct\002 %)";
- }
-
- my $pct1 = sprintf("%.01f", 100*$x/$sum);
- &performStrictReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
-}
-