Adding show top ten.

This commit is contained in:
sergiotarxz 2022-02-08 22:50:05 +01:00
parent 72f2460402
commit c6c922b9a7
2 changed files with 51 additions and 7 deletions

View File

@ -71,6 +71,31 @@ sub process_message_with_text {
if ( $text eq '/karma' ) {
show_karma($message);
}
if ( $text eq '/doctortop' ) {
show_top_ten($message);
}
}
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 $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_karma {
@ -79,10 +104,10 @@ sub show_karma {
my $user_id = $reply_to_message->{from}{id} // $message->{from}{id};
my $chat_id = $message->{chat}{id};
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
my $user = $user_dao->recover_id( id => $user_id );
my $user_dao = DoctorKarma::DAO::User->new( dbh => $db );
my $user = $user_dao->recover_id( id => $user_id );
my $first_name = $user->first_name;
my $karma = $user->karma;
my $karma = $user->karma;
my $html_message = <<"EOF";
<b>@{[xml_escape($first_name)]}</b> has @{[xml_escape($karma)]} points of karma.
@ -127,7 +152,7 @@ EOF
my $receptor_telegram_url = Mojo::URL->new('tg://user');
$receptor_telegram_url->query( id => $id_receptor );
my $first_name = $receiving_karma_user->first_name;
my $karma = $receiving_karma_user->karma;
my $karma = $receiving_karma_user->karma;
$telegram->send_message(
chat_id => $chat_id,
text =>
@ -167,8 +192,8 @@ EOF
$user_dao->add_1_karma( user => $receiving_karma_user );
my $id_receptor = $receiving_karma_user->id_user;
my $receptor_telegram_url = Mojo::URL->new('tg://user');
my $first_name = $receiving_karma_user->first_name;
my $karma = $receiving_karma_user->karma;
my $first_name = $receiving_karma_user->first_name;
my $karma = $receiving_karma_user->karma;
$telegram->send_message(
chat_id => $chat_id,
text =>

View File

@ -160,7 +160,6 @@ EOF
}
}
{
my $validator = validation_for(
params => {
@ -242,6 +241,26 @@ EOF
}
}
sub get_top_ten_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
ORDER BY karma desc
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;
}
{
my $validator = validation_for(
params => {