Fixes this bug:
[onis.git] / lib / Onis / Plugins / Core.pm
index ec6dc79..627fc5e 100644 (file)
@@ -17,14 +17,14 @@ complicated plugin so far.
 
 =cut
 
-use Onis::Config qw/get_config/;
-use Onis::Html qw/html_escape get_filehandle/;
-use Onis::Language qw/translate/;
-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));
+use Onis::Config (qw(get_config));
+use Onis::Html (qw(html_escape get_filehandle));
+use Onis::Language (qw(translate));
+use Onis::Users (qw(get_realname get_link get_image chatter_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 nick_is_in_main_table));
 @Onis::Plugins::Core::ISA = ('Exporter');
 
 our $NickLinesCounter = Onis::Data::Persistent->new ('NickLinesCounter', 'nick',
@@ -53,20 +53,19 @@ our $QuoteData = {};  # Is generated before output. Nicks are merged according t
 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 $NicksInMainTable = {};
+
+our @HorizontalImages = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#;
 our $QuoteCacheSize = 10;
 our $QuoteMin = 30;
 our $QuoteMax = 80;
-our $WORD_LENGTH = 5;
-our $SORT_BY = 'LINES';
-our $DISPLAY_LINES = 'BOTH';
-our $DISPLAY_WORDS = 'NONE';
-our $DISPLAY_CHARS = 'NONE';
-our $DISPLAY_TIMES = 0;
-our $DISPLAY_IMAGES = 0;
-our $DEFAULT_IMAGE = '';
-our $BAR_HEIGHT = 130;
-our $BAR_WIDTH  = 100;
+our $SortBy = 'LINES';
+our $DisplayLines = 'BOTH';
+our $DisplayWords = 'NONE';
+our $DisplayChars = 'NONE';
+our $DisplayTimes = 0;
+our $DisplayImages = 0;
+our $DefaultImage = '';
 our $LongLines  = 50;
 our $ShortLines = 10;
 
@@ -112,20 +111,6 @@ if (get_config ('quote_max'))
        $QuoteMax = $tmp if ($tmp);
 }
 
-=item B<min_word_length>: I<5>
-
-Sets how many word-characters in a row are considered to be a word. Or, in more
-normal terms: Sets the minimum length for words..
-
-=cut
-
-if (get_config ('min_word_length'))
-{
-       my $tmp = get_config ('min_word_length');
-       $tmp =~ s/\D//g;
-       $WORD_LENGTH = $tmp if ($tmp);
-}
-
 =item B<display_lines>: I<BOTH>
 
 Choses wether to display B<lines> as I<BAR>, I<NUMBER>, I<BOTH> or not at all
@@ -140,7 +125,7 @@ if (get_config ('display_lines'))
 
        if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH'))
        {
-               $DISPLAY_LINES = $tmp;
+               $DisplayLines = $tmp;
        }
        else
        {
@@ -163,7 +148,7 @@ if (get_config ('display_words'))
 
        if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH'))
        {
-               $DISPLAY_WORDS = $tmp;
+               $DisplayWords = $tmp;
        }
        else
        {
@@ -186,7 +171,7 @@ if (get_config ('display_chars'))
 
        if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH'))
        {
-               $DISPLAY_CHARS = $tmp;
+               $DisplayChars = $tmp;
        }
        else
        {
@@ -209,11 +194,11 @@ if (get_config ('display_times'))
 
        if ($tmp =~ m/true|on|yes/i)
        {
-               $DISPLAY_TIMES = 1;
+               $DisplayTimes = 1;
        }
        elsif ($tmp =~ m/false|off|no/i)
        {
-               $DISPLAY_TIMES = 0;
+               $DisplayTimes = 0;
        }
        else
        {
@@ -234,11 +219,11 @@ if (get_config ('display_images'))
 
        if ($tmp =~ m/true|on|yes/i)
        {
-               $DISPLAY_IMAGES = 1;
+               $DisplayImages = 1;
        }
        elsif ($tmp =~ m/false|off|no/i)
        {
-               $DISPLAY_IMAGES = 0;
+               $DisplayImages = 0;
        }
        else
        {
@@ -256,7 +241,7 @@ to take care of (absolute) paths yourself.
 
 if (get_config ('default_image'))
 {
-       $DEFAULT_IMAGE = get_config ('default_image');
+       $DefaultImage = get_config ('default_image');
 }
 
 =item B<sort_by>: I<LINES>
@@ -274,7 +259,7 @@ if (get_config ('sort_by'))
 
        if (($tmp eq 'LINES') or ($tmp eq 'WORDS') or ($tmp eq 'CHARS'))
        {
-               $SORT_BY = $tmp;
+               $SortBy = $tmp;
        }
        else
        {
@@ -308,36 +293,10 @@ if (get_config ('horizontal_images'))
                        next;
                }
 
-               $H_IMAGES[$i] = $tmp[$i];
+               $HorizontalImages[$i] = $tmp[$i];
        }
 }
 
-=item B<bar_height>: I<130>
-
-Sets the height (in pixels) of the highest vertical graph.
-
-=cut
-
-if (get_config ('bar_height'))
-{
-       my $tmp = get_config ('bar_height');
-       $tmp =~ s/\D//g;
-       $BAR_HEIGHT = $tmp if ($tmp >= 10);
-}
-
-=item B<bar_width>: I<100>
-
-Sets the width (in pixels) of the widest horizontal graph.
-
-=cut
-
-if (get_config ('bar_width'))
-{
-       my $tmp = get_config ('bar_width');
-       $tmp =~ s/\D//g;
-       $BAR_WIDTH = $tmp if ($tmp >= 10);
-}
-
 =item B<longlines>: I<50>
 
 Sets the number of rows of the main ranking table.
@@ -512,7 +471,7 @@ sub calculate
 
                if (!defined ($QuoteData->{$main}))
                {
-                       die;
+                       next;
                }
                elsif (scalar (@{$QuoteData->{$main}}) < $QuoteCacheSize)
                {
@@ -551,7 +510,6 @@ sub activetimes
 {
        my $max = 0;            # the most lines that were written in one hour..
        my $total = 0;          # the total amount of lines we wrote..
-       my $factor = 0;         # used to find a bar's height
 
        my @data = 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);
 
@@ -587,18 +545,16 @@ sub activetimes
                $max = 1;
        }
 
-       $factor = (($BAR_HEIGHT - 1) / $max);
-
        my $header = translate ('When do we actually talk here?');
        print $fh "<h2>$header</h2>\n",
-       qq#<table class="hours_of_day">\n#,
-       qq#  <tr>\n#;
+       qq#<table class="hours">\n#,
+       qq#  <tr class="bars">\n#;
 
 # this for circles through the four colors. Each color represents six hours.
 # (4 * 6 hours = 24 hours)
        for (my $i = 0; $i <= 3; $i++)
        {
-               for (my $j = 0; $j <= 5; $j++)
+               for (my $j = 0; $j < 6; $j++)
                {
                        my $hour = (($i * 6) + $j);
                        if (!defined ($data[$hour]))
@@ -606,19 +562,22 @@ sub activetimes
                                $data[$hour] = 0;
                        }
 
-                       my $percent = 100 * ($data[$hour] / $total);
-                       my $height = int ($data[$hour] * $factor) + 1;
-                       my $img_url = $img_urls[$i];
+                       my $height  = sprintf ("%.2f", 95 * $data[$hour] / $max);
+                       my $img = $img_urls[$i];
                        
-                       print $fh '    <td>', sprintf ("%2.1f", $percent),
-                       qq#%<br /><img src="$img_url" style="height: $height#,
-                       qq#px;" alt="" /></td>\n#;
+                       print $fh qq#    <td class="bar vertical"><img src="$img" class="first last" style="height: $height\%;" alt="" /></td>\n#;
                }
        }
+       print $fh qq#  </tr>\n  <tr class="counter">\n#;
+       for (my $i = 0; $i < 24; $i++)
+       {
+               my $percent = sprintf ("%.1f", 100 * $data[$i] / $total);
+               print $fh qq#    <td class="counter">$percent\%</td>\n#;
+       }
 
        print $fh "  </tr>\n",
-       qq#  <tr class="hour_row">\n#;
-       print $fh map { "    <td>$_</td>\n" } (0 .. 23);
+       qq#  <tr class="numeration">\n#;
+       print $fh map { qq#    <td class="numeration">$_</td>\n# } (0 .. 23);
        print $fh "  </tr>\n",
        "</table>\n\n";
 }
@@ -639,7 +598,7 @@ sub ranking
 
        my $fh = get_filehandle () or die;
 
-       my $sort_field = lc ($SORT_BY);
+       my $sort_field = lc ($SortBy);
 
        my $trans;
 
@@ -652,19 +611,19 @@ sub ranking
        
        ($tmp) = sort { $NickData->{$b}{'chars_total'} <=> $NickData->{$a}{'chars_total'} } (@nicks);
        $max_chars = $NickData->{$tmp}{'chars_total'} || 0;
-       
+
        $trans = translate ('Most active nicks');
        
        print $fh "<h2>$trans</h2>\n";
-       if ($SORT_BY eq 'LINES')
+       if ($SortBy eq 'LINES')
        {
                $trans = translate ('Nicks sorted by numbers of lines written');
        }
-       elsif ($SORT_BY eq 'WORDS')
+       elsif ($SortBy eq 'WORDS')
        {
                $trans = translate ('Nicks sorted by numbers of words written');
        }
-       else # ($SORT_BY eq 'CHARS')
+       else # ($SortBy eq 'CHARS')
        {
                $trans = translate ('Nicks sorted by numbers of characters written');
        }
@@ -676,7 +635,7 @@ sub ranking
   <tr>
     <td class="invis">&nbsp;</td>
 EOF
-       if ($DISPLAY_IMAGES)
+       if ($DisplayImages)
        {
                $trans = translate ('Image');
                print $fh "    <th>$trans</th>\n";
@@ -686,22 +645,25 @@ EOF
                $trans = translate ('Nick');
                print $fh "    <th>$trans</th>\n";
        }
-       if ($DISPLAY_LINES ne 'NONE')
+       if ($DisplayLines ne 'NONE')
        {
+               my $span = $DisplayLines eq 'BOTH' ? ' colspan="2"' : '';
                $trans = translate ('Number of Lines');
-               print $fh "    <th>$trans</th>\n";
+               print $fh "    <th$span>$trans</th>\n";
        }
-       if ($DISPLAY_WORDS ne 'NONE')
+       if ($DisplayWords ne 'NONE')
        {
+               my $span = $DisplayWords eq 'BOTH' ? ' colspan="2"' : '';
                $trans = translate ('Number of Words');
-               print $fh "    <th>$trans</th>\n";
+               print $fh "    <th$span>$trans</th>\n";
        }
-       if ($DISPLAY_CHARS ne 'NONE')
+       if ($DisplayChars ne 'NONE')
        {
+               my $span = $DisplayChars eq 'BOTH' ? ' colspan="2"' : '';
                $trans = translate ('Number of Characters');
-               print $fh "    <th>$trans</th>\n";
+               print $fh "    <th$span>$trans</th>\n";
        }
-       if ($DISPLAY_TIMES)
+       if ($DisplayTimes)
        {
                $trans = translate ('When?');
                print $fh "    <th>$trans</th>\n";
@@ -722,7 +684,7 @@ EOF
        {
                my $nick = $_;
                my $ident = nick_to_ident ($nick);
-               my $name  = ident_to_name ($ident);
+               my $name  = chatter_to_name ("$nick!$ident");
                my $print = $name || $nick;
 
                $linescount++;
@@ -732,6 +694,8 @@ EOF
                # our table..
                if ($linescount <= $LongLines)
                {
+                       $NicksInMainTable->{$nick} = $linescount;
+                       
                        my $quote = translate ('-- no quote available --');
 
                        if (@{$QuoteData->{$nick}})
@@ -755,11 +719,11 @@ EOF
                        print $fh "  <tr>\n",
                        qq#    <td class="numeration"># . $linescount . "</td>\n";
 
-                       if ($DISPLAY_IMAGES)
+                       if ($DisplayImages)
                        {
-                               if ($DEFAULT_IMAGE and !$image)
+                               if ($DefaultImage and !$image)
                                {
-                                       $image = $DEFAULT_IMAGE;
+                                       $image = $DefaultImage;
                                }
                                
                                print $fh qq#    <td class="image">#;
@@ -799,58 +763,52 @@ EOF
                                print $fh qq#$print</td>\n#;
                        }
                
-                       if ($DISPLAY_LINES ne 'NONE')
+                       if ($DisplayLines ne 'NONE')
                        {
-                               print $fh qq#    <td class="bar">#;
-                               if (($DISPLAY_LINES eq 'BOTH') or ($DISPLAY_LINES eq 'BAR'))
+                               if (($DisplayLines eq 'BOTH') or ($DisplayLines eq 'NUMBER'))
                                {
-                                       my $code = bar ($max_lines, $NickData->{$nick}{'lines'});
-                                       print $fh $code;
+                                       my $num = $NickData->{$nick}{'lines_total'};
+                                       print $fh qq(    <td class="counter">$num</td>\n);
                                }
-                               print $fh '&nbsp;' if ($DISPLAY_LINES eq 'BOTH');
-                               if (($DISPLAY_LINES eq 'BOTH') or ($DISPLAY_LINES eq 'NUMBER'))
+                               if (($DisplayLines eq 'BOTH') or ($DisplayLines eq 'BAR'))
                                {
-                                       print $fh $NickData->{$nick}{'lines_total'};
+                                       my $code = bar ($max_lines, $NickData->{$nick}{'lines'});
+                                       print $fh qq(    <td class="bar horizontal">$code</td>\n);
                                }
-                               print $fh "</td>\n";
                        }
 
-                       if ($DISPLAY_WORDS ne 'NONE')
+                       if ($DisplayWords ne 'NONE')
                        {
-                               print $fh qq#    <td class="bar">#;
-                               if (($DISPLAY_WORDS eq 'BOTH') or ($DISPLAY_WORDS eq 'BAR'))
+                               if (($DisplayWords eq 'BOTH') or ($DisplayWords eq 'NUMBER'))
                                {
-                                       my $code = bar ($max_words, $NickData->{$nick}{'words'});
-                                       print $fh $code;
+                                       my $num = $NickData->{$nick}{'words_total'};
+                                       print $fh qq(    <td class="counter">$num</td>\n);
                                }
-                               print $fh '&nbsp;' if ($DISPLAY_WORDS eq 'BOTH');
-                               if (($DISPLAY_WORDS eq 'BOTH') or ($DISPLAY_WORDS eq 'NUMBER'))
+                               if (($DisplayWords eq 'BOTH') or ($DisplayWords eq 'BAR'))
                                {
-                                       print $fh $NickData->{$nick}{'words_total'};
+                                       my $code = bar ($max_words, $NickData->{$nick}{'words'});
+                                       print $fh qq(    <td class="bar horizontal">$code</td>\n);
                                }
-                               print $fh "</td>\n";
                        }
 
-                       if ($DISPLAY_CHARS ne 'NONE')
+                       if ($DisplayChars ne 'NONE')
                        {
-                               print $fh qq#    <td class="bar">#;
-                               if (($DISPLAY_CHARS eq 'BOTH') or ($DISPLAY_CHARS eq 'BAR'))
+                               if (($DisplayChars eq 'BOTH') or ($DisplayChars eq 'NUMBER'))
                                {
-                                       my $code = bar ($max_chars, $NickData->{$nick}{'chars'});
-                                       print $fh $code;
+                                       my $num = $NickData->{$nick}{'chars_total'};
+                                       print $fh qq(    <td class="counter">$num</td>\n);
                                }
-                               print $fh '&nbsp;' if ($DISPLAY_CHARS eq 'BOTH');
-                               if (($DISPLAY_CHARS eq 'BOTH') or ($DISPLAY_CHARS eq 'NUMBER'))
+                               if (($DisplayChars eq 'BOTH') or ($DisplayChars eq 'BAR'))
                                {
-                                       print $fh $NickData->{$nick}{'chars_total'};
+                                       my $code = bar ($max_chars, $NickData->{$nick}{'chars'});
+                                       print $fh qq(    <td class="bar horizontal">$code</td>\n);
                                }
-                               print $fh "</td>\n";
                        }
 
-                       if ($DISPLAY_TIMES)
+                       if ($DisplayTimes)
                        {
                                my $code = bar ($NickData->{$nick}{'chars_total'}, $NickData->{$nick}{'chars'});
-                               print $fh qq#    <td class="bar">$code</td>\n#;
+                               print $fh qq#    <td class="bar horizontal">$code</td>\n#;
                        }
 
                        print $fh qq#    <td class="quote">$quote</td>\n#,
@@ -872,15 +830,15 @@ EOF
                        my $col_in_this_table = ($linescount - $LongLines - 1) % 6;
 
                        my $total = 0;
-                       if ($SORT_BY eq 'LINES')
+                       if ($SortBy eq 'LINES')
                        {
                                $total = $NickData->{$nick}{'lines_total'};
                        }
-                       elsif ($SORT_BY eq 'WORDS')
+                       elsif ($SortBy eq 'WORDS')
                        {
                                $total = $NickData->{$nick}{'words_total'};
                        }
-                       else # ($SORT_BY eq 'CHARS')
+                       else # ($SortBy eq 'CHARS')
                        {
                                $total = $NickData->{$nick}{'chars_total'};
                        }
@@ -950,22 +908,18 @@ sub bar
 
        confess () unless (ref ($source) eq 'ARRAY');
 
-       # BAR_WIDTH is a least 10
-       my $max_width = $BAR_WIDTH - 4;
-       my $factor = 1;
        my $retval = '';
 
        my $i;
        my $j;
 
-       if (!$max_num) { return ($retval); }
-       $factor = $max_width / $max_num;
+       $max_num ||= 1;
 
        for ($i = 0; $i < 4; $i++)
        {
                my $sum = 0;
-               my $width = 1;
-               my $img = $H_IMAGES[$i];
+               my $img = $HorizontalImages[$i];
+               my $width;
 
                for ($j = 0; $j < 6; $j++)
                {
@@ -973,9 +927,9 @@ sub bar
                        $sum += $source->[$hour];
                }
 
-               $width += int (0.5 + ($sum * $factor));
+               $width = sprintf ("%.2f", 95 * $sum / $max_num);
                
-               $retval .= qq#<img src="$img" style="width: # . $width . q#px"#;
+               $retval .= qq#<img src="$img" style="width: $width%;"#;
                if ($i == 0) { $retval .= qq# class="first"#; }
                elsif ($i == 3) { $retval .= qq# class="last"#; }
                $retval .= qq( alt="$sum" />);
@@ -1028,10 +982,24 @@ sub get_sorted_nicklist
        return ($SortedNicklist);
 }
 
+=item B<nick_is_in_main_table> (I<$nick>)
+
+Returns the position of the nick in the main table or zero if it is not in the
+main table.
+
+=cut
+
+sub nick_is_in_main_table
+{
+       my $nick = shift;
+
+       return (defined ($NicksInMainTable->{$nick}) ? $NicksInMainTable->{$nick} : 0);
+}
+
 =back
 
 =head1 AUTHOR
 
-Florian octo Forster, E<lt>octo at verplant.orgE<gt>
+Florian octo Forster E<lt>octo at verplant.orgE<gt>
 
 =cut