More bugfixes..
[onis.git] / lib / Onis / Plugins / Core.pm
index b8e3571..fc8e3db 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use Carp (qw(confess));
+use Exporter;
 
 =head1 NAME
 
@@ -23,6 +24,9 @@ use Onis::Users (qw(get_realname get_link get_image ident_to_name));
 use Onis::Data::Core qw#get_all_nicks nick_to_ident ident_to_nick get_main_nick register_plugin#;
 use Onis::Data::Persistent;
 
+@Onis::Plugins::Core::EXPORT_OK = (qw(get_core_nick_counters get_sorted_nicklist));
+@Onis::Plugins::Core::ISA = ('Exporter');
+
 our $NickLinesCounter = Onis::Data::Persistent->new ('NickLinesCounter', 'nick',
        qw(
                lines00 lines01 lines02 lines03 lines04 lines05 lines06 lines07 lines08 lines09 lines10 lines11
@@ -44,7 +48,8 @@ our $NickCharsCounter = Onis::Data::Persistent->new ('NickCharsCounter', 'nick',
 
 our $QuoteCache = {}; # Saves per-nick information without any modification
 our $QuoteData = {};  # Is generated before output. Nicks are merged according to Data::Core.
-our $NickData = {}:  # Same as above, but for nicks rather than quotes.
+our $NickData = {};  # Same as above, but for nicks rather than quotes.
+our $SortedNicklist = [];
 
 our @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
 our $QuoteCacheSize = 10;
@@ -273,7 +278,7 @@ sub add
        {
                @counter = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0);
        }
-       $counter[$hour]++
+       $counter[$hour]++;
        $NickLinesCounter->put ($nick, @counter);
 
        @counter = $NickWordsCounter->get ($nick);
@@ -357,13 +362,13 @@ sub calculate
                        $NickData->{$main}{'words_total'} = $sum;
                }
 
-               @counter = $NickWordsCounter->get ($nick);
+               @counter = $NickCharsCounter->get ($nick);
                if (@counter)
                {
                        my $sum = 0;
                        for (my $i = 0; $i < 24; $i++)
                        {
-                               $NickData->{$main}{'words'}[$i] += $counter[$i];
+                               $NickData->{$main}{'chars'}[$i] += $counter[$i];
                                $sum += $counter[$i];
                        }
                        $NickData->{$main}{'chars_total'} = $sum;
@@ -375,8 +380,13 @@ sub calculate
                }
                if (defined ($QuoteCache->{$nick}))
                {
-                       my @new = sort (sub { $b->[0] <=> $a->[0] }, @{$QuoteCache->{$nick}}, @{$QuoteData->{$main}});
+                       my @new = ();
+                       push (@new, @{$QuoteData->{$main}}) if (@{$QuoteData->{$main}});
+                       push (@new, @{$QuoteCache->{$nick}}) if (@{$QuoteCache->{$nick}});
+
+                       @new = sort { $b->[0] <=> $a->[0] } (@new);
                        splice (@new, $QuoteCacheSize) if (scalar (@new) > $QuoteCacheSize);
+
                        $QuoteData->{$main} = \@new;
                }
        }
@@ -409,7 +419,7 @@ sub activetimes
 # $most_lines
        for (keys %$NickData)
        {
-               my $nick = shift;
+               my $nick = $_;
 
                for (my $i = 0; $i < 24; $i++)
                {
@@ -553,14 +563,19 @@ EOF
        print $fh "    <th>$trans</th>\n",
        "  </tr>\n";
 
-       for (sort
+       @$SortedNicklist = sort
        {
                $NickData->{$b}{"${sort_field}_total"} <=> $NickData->{$a}{"${sort_field}_total"}
-       } (@nicks))
+       } (@nicks);
+
+       @nicks = ();
+
+       for (@$SortedNicklist)
        {
                my $nick = $_;
                my $ident = nick_to_ident ($nick);
                my $name  = ident_to_name ($ident);
+               my $print = $name || $nick;
 
                $linescount++;
 
@@ -571,11 +586,15 @@ EOF
                {
                        my $quote = translate ('-- no quote available --');
 
-                       if (defined ($QuoteData->{$nick}))
+                       if (@{$QuoteData->{$nick}})
                        {
                                my $num = scalar (@{$QuoteData->{$nick}});
                                my $rand = int (rand ($num));
-                               $quote = html_escape ($QuoteData->{$nick}[$rand]);
+
+                               require Data::Dumper;
+                               print STDOUT Data::Dumper->Dump ([$rand, $QuoteData->{$nick}], ['rand', "QuoteData->{$nick}"]);
+                               
+                               $quote = html_escape ($QuoteData->{$nick}[$rand][1]);
                        }
 
                        my $link = '';
@@ -628,11 +647,11 @@ EOF
 
                        if ($link)
                        {
-                               print $fh qq#<a href="$link">$name</a></td>\n#
+                               print $fh qq#<a href="$link">$print</a></td>\n#
                        }
                        else
                        {
-                               print $fh qq#$name</td>\n#;
+                               print $fh qq#$print</td>\n#;
                        }
                
                        if ($DISPLAY_LINES ne 'NONE')
@@ -721,7 +740,7 @@ EOF
                                $total = $NickData->{$nick}{'chars_total'};
                        }
 
-                       my $title = $realname;
+                       my $title = $name ? get_realname ($name) : '';
                        if (!$title)
                        {
                                $title = "User: $name; " if ($name);
@@ -742,7 +761,7 @@ EOF
                                qq#  <tr>\n#;
                        }
                        
-                       print $fh qq#    <td title="$title">$name ($total)</td>\n#;
+                       print $fh qq#    <td title="$title">$print ($total)</td>\n#;
                        
                        if ($row_in_this_table == $ShortLines and $col_in_this_table == 5)
                        {
@@ -782,10 +801,9 @@ EOF
 sub bar
 {
        my $max_num = shift;
-
        my $source = shift;
 
-       confess unless (ref ($source eq 'ARRAY'));
+       confess () unless (ref ($source) eq 'ARRAY');
 
        # BAR_WIDTH is a least 10
        my $max_width = $BAR_WIDTH - 4;
@@ -821,6 +839,52 @@ sub bar
        return ($retval);
 }
 
+=head1 EXPORTED FUNCTIONS
+
+=over 4
+
+=item B<get_core_nick_counters> (I<$nick>)
+
+Returns a hash-ref that containes all the nick-counters available. It looks
+like this:
+
+    {
+        lines => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)],
+       words => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)],
+       chars => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)],
+       lines_total => 0,
+       words_total => 0,
+       chars_total => 0
+    }
+
+=cut
+
+sub get_core_nick_counters
+{
+       my $nick = shift;
+
+       if (!defined ($NickData->{$nick}))
+       {
+               return ({});
+       }
+
+       return ($NickData->{$nick});
+}
+
+=item B<get_sorted_nicklist> ()
+
+Returns an array-ref that containes all nicks, sorted by the field given in the
+config-file.
+
+=cut
+
+sub get_sorted_nicklist
+{
+       return ($SortedNicklist);
+}
+
+=back
+
 =head1 AUTHOR
 
 Florian octo Forster, E<lt>octo at verplant.orgE<gt>