Added Longterm-stats to userdetails
[onis.git] / lib / Onis / Plugins / Userdetails.pm
index 75f91e2..1d12dc8 100644 (file)
@@ -6,8 +6,15 @@ use warnings;
 use Onis::Config (qw(get_config));
 use Onis::Html (qw(html_escape get_filehandle));
 use Onis::Language (qw(translate));
-use Onis::Data::Core (qw(get_main_nick register_plugin));
-use Onis::Users (qw(ident_to_name get_link get_image));
+use Onis::Data::Core (qw(get_main_nick register_plugin nick_to_name get_most_recent_time));
+use Onis::Users (qw(get_link get_image));
+
+use Onis::Plugins::Core (qw(get_core_nick_counters get_sorted_nicklist));
+use Onis::Plugins::Weekdays (qw(get_weekdays));
+use Onis::Plugins::Longterm (qw(get_longterm));
+use Onis::Plugins::Conversations (qw(get_conversations));
+use Onis::Plugins::Bignumbers (qw(get_bignumbers));
+use Onis::Plugins::Interestingnumbers (qw(get_interestingnumbers));
 
 our $DISPLAY_IMAGES = 0;
 our $DEFAULT_IMAGE = '';
@@ -122,6 +129,14 @@ if (get_config ('bar_width'))
        $BAR_WIDTH = $tmp if ($tmp >= 10);
 }
 
+our $LongtermDays = 7;
+if (get_config ('userdetails_longterm_days'))
+{
+       my $tmp = get_config ('userdetails_longterm_days');
+       $tmp =~ s/\D//g;
+       $LongtermDays = $tmp if ($tmp);
+}
+
 my $VERSION = '$Id: Userdetails.pm,v 1.5 2005/03/14 18:40:25 octo Exp $';
 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
 
@@ -129,17 +144,7 @@ return (1);
 
 sub output
 {
-       my @names = sort
-       {
-               $DATA->{'byname'}{$b}{$SORT_BY} <=> $DATA->{'byname'}{$a}{$SORT_BY}
-       } grep
-       {
-               defined ($DATA->{'byname'}{$_}{'words'})
-       } (keys (%{$DATA->{'byname'}}));
-
-       return (undef) unless (@names);
-       
-       my $max = $PLUGIN_MAX;
+       my $nicks_ref = get_sorted_nicklist ();
        
        my $fh = get_filehandle ();
 
@@ -148,67 +153,79 @@ sub output
 
        my $max_time = 0;
        my $max_conv = 0;
+       my $max_weekdays = 0;
+       my $max_longterm = 0;
+
+       my @nicks = @$nicks_ref;
+       my $nick_data = {};
 
-       for (@names)
+       splice (@nicks, $PLUGIN_MAX) if (scalar (@nicks) > $PLUGIN_MAX);
+
+       for (@nicks)
        {
-               my $name = $_;
+               my $nick = $_;
+
+               $nick_data->{$nick} = get_core_nick_counters ($nick);
+               $nick_data->{$nick}{'weekdays'} = get_weekdays ($nick);
+               $nick_data->{$nick}{'longterm'} = get_longterm ($nick);
+               $nick_data->{$nick}{'conversations'} = get_conversations ($nick);
+               $nick_data->{$nick}{'bignumbers'} = get_bignumbers ($nick);
+               $nick_data->{$nick}{'interestingnumbers'} = get_interestingnumbers ($nick);
                
-               if (defined ($DATA->{'byname'}{$name}{'chars_time'}))
+               for (my $i = 0; $i < 24; $i++)
                {
-                       for (0..23)
+                       $num = $nick_data->{$nick}{'chars'}[$i];
+                       $max_time = $num if ($max_time < $num);
+               }
+
+               for (keys %{$nick_data->{$nick}{'conversations'}})
+               {
+                       my $other = $_;
+                       my $ptr = $nick_data->{$nick}{'conversations'}{$other}{'nicks'}{$nick};
+                       $num = $ptr->[0] + $ptr->[1] + $ptr->[2] + $ptr->[3];
+                       $max_conv = $num if ($max_conv < $num);
+               }
+
+               for (keys %{$nick_data->{$nick}{'weekdays'}})
+               {
+                       my $ptr = $nick_data->{$nick}{'weekdays'}{$_};
+                       for (my $i = 0; $i < 4; $i++)
                        {
-                               next unless (defined ($DATA->{'byname'}{$name}{'chars_time'}{$_}));
-                               if ($DATA->{'byname'}{$name}{'chars_time'}{$_} > $max_time)
-                               {
-                                       $max_time = $DATA->{'byname'}{$name}{'chars_time'}{$_};
-                               }
+                               $max_weekdays = $ptr->[$i] if ($max_weekdays < $ptr->[$i]);
                        }
                }
-               if (defined ($DATA->{'byname'}{$name}{'conversations'}))
+
+               if (@{$nick_data->{$nick}{'longterm'}})
                {
-                       my @others = keys (%{$DATA->{'byname'}{$name}{'conversations'}});
-                       for (@others)
-                       {
-                               my $o = $_;
-                               my $num = 0;
+                       my $num = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       $LongtermDays = $num if ($LongtermDays > $num);
 
-                               for (0..3)
-                               {
-                                       $num += $DATA->{'byname'}{$name}{'conversations'}{$o}[$_];
-                               }
+                       for (my $i = $num - $LongtermDays; $i < $num; $i++)
+                       {
+                               my $ptr = $nick_data->{$nick}{'longterm'}[$i];
 
-                               if ($num > $max_conv)
+                               for (my $j = 0; $j < 4; $j++)
                                {
-                                       $max_conv = $num;
+                                       $max_longterm = $ptr->[$j] if ($max_longterm < $ptr->[$j]);
                                }
                        }
                }
        }
 
-       my $time_factor = 0;
-       my $conv_factor = 0;
-
-       if ($max_time)
-       {
-               $time_factor = $BAR_HEIGHT / $max_time;
-       }
-
-       if ($max_conv)
-       {
-               $conv_factor = $BAR_WIDTH / $max_conv;
-       }
-       
        print $fh qq#<table class="plugin userdetails">\n#,
        qq#  <tr>\n#,
-       qq#    <th colspan="#, $DISPLAY_IMAGES ? 3 : 2, qq#">$trans</th>\n#,
+       qq#    <th colspan="#, $DISPLAY_IMAGES ? 4 : 3, qq#">$trans</th>\n#,
        qq#  </tr>\n#;
 
-       for (@names)
+       for (@nicks)
        {
-               my $name = $_;
+               my $nick = $_;
+               my $name = nick_to_name ($nick);
+               my $print = $name ? $name : $nick;
+               my $ptr = $nick_data->{$nick};
 
                print $fh qq#  <tr>\n#,
-               qq#    <th colspan="#, $DISPLAY_IMAGES ? 3 : 2, qq#" class="nick">$name</th>\n#,
+               qq#    <th colspan="#, $DISPLAY_IMAGES ? 4 : 3, qq#" class="nick">$print</th>\n#,
                qq#  </tr>\n#,
                qq#  <tr>\n#;
 
@@ -229,7 +246,7 @@ sub output
                                {
                                        print $fh qq#<a href="$link">#;
                                }
-                               print $fh qq#<img src="$image" alt="$name" />#;
+                               print $fh qq#<img src="$image" alt="$print" />#;
                                if ($link)
                                {
                                        print $fh "</a>";
@@ -244,186 +261,116 @@ sub output
 
                print $fh qq#    <td class="counters">\n#;
 
-               $num = $DATA->{'byname'}{$name}{'lines'};
-               $trans = translate ('Has written %u lines');
-               printf $fh ("      $trans<br />\n", $num);
+               {
+                       my $lines;
+                       my $words;
+                       my $chars;
 
-               $num = $DATA->{'byname'}{$name}{'words'};
-               $trans = translate ('Has written %u words');
-               printf $fh ("      $trans<br />\n", $num);
+                       $lines = $ptr->{'lines_total'};
+                       $trans = translate ('Has written %u lines');
+                       printf $fh ("      $trans<br />\n", $lines);
 
-               $num = $DATA->{'byname'}{$name}{'chars'};
-               $trans = translate ('Has written %u chars');
-               printf $fh ("      $trans<br />\n", $num);
+                       $words = $ptr->{'words_total'};
+                       $trans = translate ('Has written %u words');
+                       printf $fh ("      $trans<br />\n", $words);
 
-               if ($DATA->{'byname'}{$name}{'lines'})
-               {
-                       $num = $DATA->{'byname'}{$name}{'words'} / $DATA->{'byname'}{$name}{'lines'};
+                       $chars = $ptr->{'chars_total'};
+                       $trans = translate ('Has written %u chars');
+                       printf $fh ("      $trans<br />\n", $chars);
+
+                       $lines ||= 1;
+
+                       $num = $words / $lines;
                        $trans = translate ('Has written %.1f words per line');
                        printf $fh ("      $trans<br />\n", $num);
 
-                       $num = $DATA->{'byname'}{$name}{'chars'} / $DATA->{'byname'}{$name}{'lines'};
+                       $num = $chars / $lines;
                        $trans = translate ('Has written %.1f characters per line');
                        printf $fh ("      $trans<br />\n", $num);
                }
 
                print $fh qq#    </td>\n    <td class="numbers">\n#;
 
-               if (defined ($DATA->{'byname'}{$name}{'op_given'}))
+               if (%{$ptr->{'interestingnumbers'}})
                {
-                       $num = $DATA->{'byname'}{$name}{'op_given'};
                        $trans = translate ('Has given %u ops');
-
-                       printf $fh ("      $trans<br />\n", $num);
-               }
+                       printf $fh ("      $trans<br />\n", $ptr->{'interestingnumbers'}{'op_given'});
                
-               if (defined ($DATA->{'byname'}{$name}{'op_taken'}))
-               {
-                       $num = $DATA->{'byname'}{$name}{'op_taken'};
                        $trans = translate ('Has taken %u ops');
+                       printf $fh ("      $trans<br />\n", $ptr->{'interestingnumbers'}{'op_taken'});
 
-                       printf $fh ("      $trans<br />\n", $num);
-               }
-               
-               if (defined ($DATA->{'byname'}{$name}{'kick_given'}))
-               {
-                       $num = $DATA->{'byname'}{$name}{'kick_given'};
                        $trans = translate ('Has kicked out %u people');
-
-                       printf $fh ("      $trans<br />\n", $num);
-               }
+                       printf $fh ("      $trans<br />\n", $ptr->{'interestingnumbers'}{'kick_given'});
                
-               if (defined ($DATA->{'byname'}{$name}{'kick_received'}))
-               {
-                       $num = $DATA->{'byname'}{$name}{'kick_received'};
                        $trans = translate ('Has been kicked out %u times');
+                       printf $fh ("      $trans<br />\n", $ptr->{'interestingnumbers'}{'kick_received'});
 
-                       printf $fh ("      $trans<br />\n", $num);
+                       $trans = translate ('Has performed %u actions');
+                       printf $fh ("      $trans<br />\n", $ptr->{'interestingnumbers'}{'actions'});
                }
-               
-               if (defined ($DATA->{'byname'}{$name}{'questions'}))
+
+               if (%{$ptr->{'bignumbers'}})
                {
-                       $num = 100 * $DATA->{'byname'}{$name}{'questions'} / $DATA->{'byname'}{$name}{'lines'};
+                       $num = 100 * $ptr->{'bignumbers'}{'questions'} / $ptr->{'lines_total'};
                        $trans = translate ("Question ratio: %.1f%%");
-
                        printf $fh ("      $trans<br />\n", $num);
-               }
-
-               if (defined ($DATA->{'byname'}{$name}{'topics'}))
-               {
-                       $num = $DATA->{'byname'}{$name}{'topics'};
-                       $trans = translate ('Has set %u topics');
 
+                       $num = 100 * $ptr->{'bignumbers'}{'uppercase'} / $ptr->{'lines_total'};
+                       $trans = translate ("Uppercase ratio: %.1f%%");
                        printf $fh ("      $trans<br />\n", $num);
-               }
-
-               if (defined ($DATA->{'byname'}{$name}{'actions'}))
-               {
-                       $num = $DATA->{'byname'}{$name}{'actions'};
-                       $trans = translate ('Has performed %u actions');
 
+                       $num = 100 * $ptr->{'bignumbers'}{'smiley_happy'} / $ptr->{'lines_total'};
+                       $trans = translate ("Happy smiley ratio: %.1f%%");
                        printf $fh ("      $trans<br />\n", $num);
-               }
-
-               # actions # TODO
-               # exclamation ratio # TODO
-               # # of nicks
-               #
-               # chats with
-               # lines per day
-
-               print $fh qq#    </td>\n  </tr>\n  <tr>\n    <td class="houractivity">\n#;
-               
-               if (defined ($DATA->{'byname'}{$name}{'chars_time'}))
-               {
-                       print $fh qq#      <table class="hours_of_day">\n        <tr>\n#;
-                       
-                       for (0..11)
-                       {
-                               my $hour = 2 * $_;
-                               my $num = 0;
-
-                               my $img = $V_IMAGES[int ($hour / 6)];
-                               my $height;
-
-                               if (defined ($DATA->{'byname'}{$name}{'chars_time'}{$hour}))
-                               {
-                                       $num = $DATA->{'byname'}{$name}{'chars_time'}{$hour};
-                               }
-                               if (defined ($DATA->{'byname'}{$name}{'chars_time'}{1 + $hour}))
-                               {
-                                       $num = $DATA->{'byname'}{$name}{'chars_time'}{1 + $hour};
-                               }
-
-                               $height = int (0.5 + ($time_factor * $num));
-                               if (!$height)
-                               {
-                                       $height = 1;
-                               }
 
-                               print $fh qq#          <td><img src="$img" alt="$num chars" #,
-                               qq#style="height: ${height}px;" /></td>\n#;
-                       }
-
-                       print $fh <<EOF;
-        </tr>
-       <tr class="hour_row">
-         <td colspan="3">0-5</td>
-         <td colspan="3">6-11</td>
-         <td colspan="3">12-17</td>
-         <td colspan="3">18-23</td>
-       </tr>
-      </table>
-EOF
-               }
-               else
-               {
-                       print $fh '&nbsp;';
+                       $num = 100 * $ptr->{'bignumbers'}{'smiley_sad'} / $ptr->{'lines_total'};
+                       $trans = translate ("Sad smiley ratio: %.1f%%");
+                       printf $fh ("      $trans<br />\n", $num);
                }
 
-               print $fh qq#    </td>\n    <td class="convpartners">\n#;
+               print $fh qq#    </td>\n    <td>\n#;
                
-               if (defined ($DATA->{'byname'}{$name}{'conversations'}))
+               if (%{$ptr->{'conversations'}})
                {
                        my $i;
-                       my $data = $DATA->{'byname'}{$name}{'conversations'};
-                       my @names = sort
+                       my @others = sort
                        {
-                               ($data->{$b}[0] + $data->{$b}[1] + $data->{$b}[2] + $data->{$b}[3])
+                               ($ptr->{'conversations'}{$b}{'nicks'}{$nick}[0]
+                                       + $ptr->{'conversations'}{$b}{'nicks'}{$nick}[1]
+                                       + $ptr->{'conversations'}{$b}{'nicks'}{$nick}[2]
+                                       + $ptr->{'conversations'}{$b}{'nicks'}{$nick}[3])
                                <=>
-                               ($data->{$a}[0] + $data->{$a}[1] + $data->{$a}[2] + $data->{$a}[3])
+                               ($ptr->{'conversations'}{$a}{'nicks'}{$nick}[0]
+                                       + $ptr->{'conversations'}{$a}{'nicks'}{$nick}[1]
+                                       + $ptr->{'conversations'}{$a}{'nicks'}{$nick}[2]
+                                       + $ptr->{'conversations'}{$a}{'nicks'}{$nick}[3])
                        }
-                       keys (%$data);
+                       (keys %{$ptr->{'conversations'}});
 
                        $trans = translate ('Talks to');
 
                        print $fh <<EOF;
-      <table>
+      <table class="conversations">
         <tr>
          <td colspan="2">$trans:</td>
        </tr>
 EOF
 
-                       $i = 0;
-                       for (@names)
+                       for (my $i = 0; $i < $PLUGIN_MAX and $i < scalar (@others); $i++)
                        {
-                               my $this_name = $_;
+                               my $other = $others[$i];
+                               my $other_name = nick_to_name ($other) || $other;
                                my $total = 0;
 
                                print $fh "        <tr>\n",
-                               qq#          <td class="nick">$this_name</td>\n#,
-                               qq#          <td class="bar">#;
+                               qq#          <td class="nick right">$other_name</td>\n#,
+                               qq#          <td class="bar horizontal right">#;
 
-                               for (0..3)
+                               for (my $k = 0; $k < 4; $k++)
                                {
-                                       my $k = $_;
-                                       
                                        my $img = $H_IMAGES[$k];
-                                       my $width = int (0.5 + ($conv_factor * $data->{$this_name}[$_]));
-                                       if (!$width)
-                                       {
-                                               $width = 1;
-                                       }
+                                       my $num = $ptr->{'conversations'}{$other}{'nicks'}{$nick}[$k];
+                                       my $width = sprintf ("%.2f", 95 * $num / $max_conv);
                                        
                                        print $fh qq#<img src="$img" alt="" #;
                                        if ($k == 0)
@@ -434,31 +381,154 @@ EOF
                                        {
                                                print $fh qq#class="last" #;
                                        }
-                                       print $fh qq#style="width: ${width}px;" />#;
+                                       print $fh qq#style="width: $width\%;" />#;
                                }
                                
                                print $fh "</td>\n        </tr>\n";
+                       }
+
+                       print $fh "      </table>\n";
+               }
+               else
+               {
+                       print $fh '&nbsp;';
+               }
+               print $fh qq#    </td>\n  </tr>\n#,
+               qq#  <tr>\n    <td>\n#;
+               
+               if (defined ($ptr->{'chars'}))
+               {
+                       print $fh qq#      <table class="hours">\n        <tr class="bars">\n#;
+                       
+                       for (my $i = 0; $i < 24; $i++)
+                       {
+                               $num = 0;
+
+                               my $img = $V_IMAGES[int ($i / 6)];
+                               my $height;
 
-                               $i++;
+                               $num  = $ptr->{'chars'}[$i];
 
-                               if ($i >= $PLUGIN_MAX)
+                               $height = sprintf ("%.2f", 95 * $num / $max_time);
+
+                               print $fh qq#          <td class="bar vertical"><img src="$img" alt="$num chars" #,
+                               qq#class="first last" style="height: $height\%;" /></td>\n#;
+                       }
+
+                       print $fh <<EOF;
+        </tr>
+       <tr class="numeration">
+         <td colspan="6" class="numeration">0-5</td>
+         <td colspan="6" class="numeration">6-11</td>
+         <td colspan="6" class="numeration">12-17</td>
+         <td colspan="6" class="numeration">18-23</td>
+       </tr>
+      </table>
+EOF
+               }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
+
+               print $fh qq#    </td>\n    <td>\n#;
+
+               #weekly
+               if (%{$nick_data->{$nick}{'weekdays'}})
+               {
+                       my $data = $nick_data->{$nick}{'weekdays'};
+                       my @days = (qw(mon tue wed thu fri sat sun));
+
+                       print $fh qq#      <table class="weekdays">\n#,
+                       qq#        <tr class="bars">\n#;
+
+                       for (@days)
+                       {
+                               my $day = $_;
+                               for (my $i = 0; $i < 4; $i++)
                                {
-                                       last;
+                                       my $num = $nick_data->{$nick}{'weekdays'}{$day}[$i];
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_weekdays);
+                                       my $img = $V_IMAGES[$i];
+
+                                       print $fh qq#          <td class="bar vertical">#,
+                                       qq#<img src="$img" alt="" class="first last" style="height: $height\%;" />#,
+                                       qq#</td>\n#;
                                }
                        }
 
-                       print $fh "      </table>\n";
+                       print $fh qq#        </tr>\n#,
+                       qq#        <tr class="numeration">\n#;
+
+                       for (@days)
+                       {
+                               my $day = $_;
+                               my $trans = translate ($day);
+                               
+                               print $fh qq#          <td colspan="4" class="numeration $day">$trans</td>\n#;
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#      </table>\n#;
                }
                else
                {
-                       print $fh '&nbsp;';
+                       print $fh "      &nbsp;\n";
                }
 
-               $max--;
-               if ($max <= 0)
+               print $fh qq#    </td>\n    <td>\n#;
+
+               #longterm
+               if (@{$nick_data->{$nick}{'longterm'}})
                {
-                       last;
+                       my $num_fields = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       my $now_epoch = get_most_recent_time ();
+                       my $now_day = int ($now_epoch / 86400);
+                       my $last_day;
+
+                       my @weekdays = (qw(sun mon tue wed thu fri sat));
+
+                       $LongtermDays = $num_fields if ($LongtermDays > $num_fields);
+                       $last_day = 1 + $now_day - $LongtermDays;
+                       
+                       print $fh qq#      <table class="longterm">\n#,
+                       qq#        <tr class="bars">\n#;
+
+                       for (my $i = $num_fields - $LongtermDays; $i < $num_fields; $i++)
+                       {
+                               for (my $j = 0; $j < 4; $j++)
+                               {
+                                       my $num = $nick_data->{$nick}{'longterm'}[$i][$j];
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_longterm);
+                                       my $img = $V_IMAGES[$j];
+                                       
+                                       print $fh qq#          <td class="bar vertical">#,
+                                       qq#<img src="$img" alt="" class="first last" style="height: $height\%;" />#,
+                                       qq#</td>\n#;
+                               }
+                       }
+                       
+                       print $fh qq#        </tr>\n#,
+                       qq#        <tr class="numeration">\n#;
+
+                       for (my $i = 0; $i < $LongtermDays; $i++)
+                       {
+                               my $epoch = ($last_day + $i) * 86400;
+                               my ($day, $wd) = (localtime ($epoch))[3,6];
+                               $wd = $weekdays[$wd];
+
+                               print $fh qq#          <td colspan="4" class="numeration $wd">$day.</td>\n#;
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#      </table>\n#;
                }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
+
+               print $fh qq#    </td>\n  </tr>\n#;
        }
 
        print $fh "</table>\n\n";