Skip to content
Extraits de code Groupes Projets
Valider e939c8fe rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

new command !stats

parent 269f694e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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 {
......
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;
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;
......@@ -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;
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter