Adding top hated top.

This commit is contained in:
sergiotarxz 2022-02-09 13:19:43 +01:00
parent 0267a679d5
commit cfd89464ce
2 changed files with 48 additions and 2 deletions

View File

@ -74,15 +74,39 @@ sub process_message_with_text {
if ( $text eq '/doctortop' ) {
show_top_ten($message);
}
if ( $text eq '/doctorhate' ) {
show_top_ten_hate($message);
}
}
sub show_top_ten_hate {
my $message = shift;
my $chat_id = $message->{chat}{id};
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
my $users = $user_dao->get_top_ten_hate_karma;
my $html = "<b>Top ten hated users:</b>\n\n";
my $i = 0;
for my $user (@$users) {
my $first_name = $user->first_name;
my $karma = $user->karma;
$i++;
$html .= "$i- <b>@{[xml_escape($first_name)]}</b> "
. "@{[xml_escape($karma)]}\n";
}
$logger->log_info("Showing top ten in $chat_id.");
$telegram->send_message(
chat_id => $chat_id,
text => $html
);
}
sub show_top_ten {
my $message = shift;
my $chat_id = $message->{chat}{id};
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
my $users = $user_dao->get_top_ten_karma;
my $html = "<b>Top ten users</b>\n\n";
my $html = "<b>Top ten users:</b>\n\n";
my $i = 0;
for my $user (@$users) {
my $first_name = $user->first_name;

View File

@ -242,6 +242,27 @@ EOF
}
}
sub get_top_ten_hate_karma {
my $self = shift;
my $db = $self->_db;
my $users = [];
my $users_db = $db->selectall_arrayref( <<'EOF', { Slice => {} } );
SELECT first_name, karma, username, last_karma_given_date, id as id_user
FROM users
WHERE first_name IS NOT NULL
ORDER BY karma ASC
LIMIT 10
EOF
for my $user_db (@$users_db) {
for my $key_field ( keys %$user_db ) {
$user_db->{$key_field} // delete $user_db->{$key_field};
}
push @$users, DoctorKarma::Model::User->new(%$user_db);
}
return $users;
}
sub get_top_ten_karma {
my $self = shift;
my $db = $self->_db;
@ -250,7 +271,8 @@ sub get_top_ten_karma {
my $users_db = $db->selectall_arrayref( <<'EOF', { Slice => {} } );
SELECT first_name, karma, username, last_karma_given_date, id as id_user
FROM users
ORDER BY karma desc
WHERE first_name IS NOT NULL
ORDER BY karma DESC
LIMIT 10
EOF
for my $user_db (@$users_db) {