diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm index f001e1f8515a21941fb0341da6e8325f5f1dbba6..d72f78bcfefd48247dbaff44ae58409b6dac9714 100644 --- a/lib/commands/parser.pm +++ b/lib/commands/parser.pm @@ -8,6 +8,7 @@ use commands::fav; use commands::later; use commands::tag; use commands::get; +use commands::stats; use utils::id; my $lastID; @@ -35,6 +36,7 @@ sub setConf { $commands::fav::irc = $ircNew; $commands::get::irc = $ircNew; + $commands::stats::irc = $ircNew; $lastID = $lastIDnew; $irc = $ircNew; @@ -97,6 +99,15 @@ sub exec { $lastID->{$chan} = $id; } } + if ($msg =~ /^ *!stats(?: (\S+))? *$/) { + my $index = $1; + try { + my $id = utils::id::get($chan, $index); + commands::stats::exec($chan, $id) + } catch { + $irc->yield(privmsg => $chan => $insultes[rand @insultes]); + } + } elsif ($msg =~ /^ *!help/) { $irc->yield(privmsg => $nick => '!fav [<id>] : enregistre la vidéo dans les favoris'); $irc->yield(privmsg => $nick => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo'); @@ -110,7 +121,7 @@ sub exec { $irc->yield(privmsg => $nick => "Toutes les commandes fonctionnent en query."); $irc->yield(privmsg => $nick => 'Niveau vie privée, potentiellement toute commande (excepté !help) entraine un enregistrement dans la base de données avec au minimum la date et l\'heure et le nick de la personne ayant exécuté la commande.'); } - elsif ($msg =~/^ *!(fav|lat|tag)/) { + elsif ($msg =~/^ *!(fav|lat|tag|stats)/) { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); } else { diff --git a/lib/commands/stats.pm b/lib/commands/stats.pm new file mode 100644 index 0000000000000000000000000000000000000000..f048098b9ef76cef41477378da2989cbca415123 --- /dev/null +++ b/lib/commands/stats.pm @@ -0,0 +1,30 @@ +package commands::stats; + +use strict; +use warnings; + +use lib "$FindBin::Bin/lib/"; +use utils::print; +use utils::db; +use utils::db::stats; + +our $irc; + +my @insultes = ("Ahahahah ! 23 à 0 !", "C'est la piquette, Jack !", "Tu sais pas jouer, Jack !", "T'es mauvais, Jack !"); + +sub exec { + my ($chan, $id) = @_; + + my $stats = utils::db::stats->new($id); + if ($stats) { + foreach (utils::print::stats($stats)) { + $irc->yield(privmsg => $chan => $_) if ($_); + } + } + else { + $irc->yield(privmsg => $chan => $insultes[rand @insultes]); + } +} + + +1; diff --git a/lib/utils/db/stats.pm b/lib/utils/db/stats.pm new file mode 100644 index 0000000000000000000000000000000000000000..e4b66814055b5154578ed5aa6bfd3cf0e509e76f --- /dev/null +++ b/lib/utils/db/stats.pm @@ -0,0 +1,83 @@ +package utils::db::stats; + +use Moose; +use FindBin; +use List::Util qw( reduce ); + +use lib "$FindBin::Bin/lib/"; +use utils::db; + + +has 'sender' => (is => 'ro', isa => 'Str'); +has 'chan' => (is => 'ro', isa => 'Str'); +has 'date' => (is => 'ro', isa => 'Str'); +has 'count' => (is => 'ro', isa => 'Int'); +has 'senders' => (is => 'ro', isa => 'HashRef[Str]'); +has 'channels' => (is => 'ro', isa => 'HashRef[Str]'); + + +around 'BUILDARGS' => sub { + my ($orig, $class, $id) = @_; + + my $count = 0; + my $sender; + my $chan; + my $date; + my $senders = {}; + my $channels = {}; + + my $dbh = utils::db::main_session; + my $sth = $dbh->prepare(' + select + date, chan, sender_irc + from playbot_chan + where content = ? + order by date + '); + + $sth->execute($id); + while (my $row = $sth->fetch) { + # first entry + if (not $sender) { + $sender = $row->[2]; + $chan = $row->[1]; + $date = $row->[0]; + } + + $senders->{$row->[2]}++; + $channels->{$row->[1]}++; + $count++; + } + + $dbh->commit; + + return $class->$orig( + count => $count, + sender => $sender, + chan => $chan, + date => $date, + senders => $senders, + channels => $channels + ); +}; + +sub max_sender { + my $self = shift; + my $max = reduce { + $self->senders->{$a} > $self->senders->{$b} ? $a : $b + } keys %{$self->senders}; + + return $max; +} + +sub max_channel { + my $self = shift; + my $max = reduce { + $self->channels->{$a} > $self->channels->{$b} ? $a : $b + } keys %{$self->channels}; + + return $max; +} + + +1; diff --git a/lib/utils/print.pm b/lib/utils/print.pm index 2b17bf7e49b6dd0bb4ade429d89fdf7a2a463c63..cdf151e313cc854b733f3cd25622020030e22636 100644 --- a/lib/utils/print.pm +++ b/lib/utils/print.pm @@ -49,4 +49,42 @@ sub print { return $msg; } +sub stats { + # a utils::db::stats object; + my $stats = shift; + my $line1; + my $line2; + my $line3; + + $line1 .= 'Posté la 1re fois par '.$stats->sender; + $line1 .= ' le '.$stats->date; + $line1 .= ' sur '.$stats->chan; + + if ($stats->count > 1) { + my $senders_count = keys %{$stats->senders}; + my $channels_count = keys %{$stats->channels}; + + $line2 .= 'Posté '.$stats->count.' fois'; + $line2 .= ' par '.$senders_count.' personne'; + $line2 .= 's' if ($senders_count > 1); + $line2 .= ' sur '.$channels_count.' channel'; + $line2 .= 's' if ($channels_count > 1); + + my $max_sender_count = $stats->senders->{$stats->max_sender}; + if ($max_sender_count > 1) { + $line3 .= $stats->max_sender. " l'a posté "; + $line3 .= $max_sender_count. ' fois'; + } + + my $max_channel_count = $stats->channels->{$stats->max_channel}; + if ($max_channel_count != $stats->count and $max_channel_count > 1) { + $line3 .= ' et ' if ($max_sender_count > 1); + $line3 .= 'il a été posté '.$max_channel_count; + $line3 .= ' fois sur '.$stats->max_channel; + } + } + + return ($line1, $line2, $line3); +} + 1;