3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-18 01:49:53 +01:00
pbot/lib/PBot/Plugin/Spinach/Stats.pm

158 lines
4.6 KiB
Perl
Raw Normal View History

2021-07-11 00:00:22 +02:00
# File: Stats.pm
#
# Purpose: Records player stats.
# SPDX-FileCopyrightText: 2021-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-11 00:00:22 +02:00
# SPDX-License-Identifier: MIT
2021-07-14 04:45:56 +02:00
package PBot::Plugin::Spinach::Stats;
2021-07-14 04:45:56 +02:00
use PBot::Imports;
2019-07-11 03:40:53 +02:00
use DBI;
use Carp qw(shortmess);
sub new($class, %conf) {
2020-02-15 23:38:32 +01:00
my $self = bless {}, $class;
$self->initialize(%conf);
return $self;
}
sub initialize($self, %conf) {
2020-02-15 23:38:32 +01:00
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
$self->{filename} = $conf{filename} // 'stats.sqlite';
}
sub begin($self) {
2020-02-15 23:38:32 +01:00
$self->{pbot}->{logger}->log("Opening Spinach stats SQLite database: $self->{filename}\n");
2020-02-15 23:38:32 +01:00
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", {RaiseError => 1, PrintError => 0}) or die $DBI::errstr;
2020-02-15 23:38:32 +01:00
eval {
$self->{dbh}->do(<< 'SQL');
CREATE TABLE IF NOT EXISTS Stats (
id INTEGER PRIMARY KEY,
nick TEXT NOT NULL COLLATE NOCASE,
channel TEXT NOT NULL COLLATE NOCASE,
high_score INTEGER DEFAULT 0,
low_score INTEGER DEFAULT 0,
avg_score INTEGER DEFAULT 0,
times_first INTEGER DEFAULT 0,
times_second INTEGER DEFAULT 0,
times_third INTEGER DEFAULT 0,
good_lies INTEGER DEFAULT 0,
players_deceived INTEGER DEFAULT 0,
questions_played INTEGER DEFAULT 0,
games_played INTEGER DEFAULT 0,
good_guesses INTEGER DEFAULT 0,
bad_guesses INTEGER DEFAULT 0
)
SQL
2020-02-15 23:38:32 +01:00
};
2020-02-15 23:38:32 +01:00
$self->{pbot}->{logger}->log("Error creating database: $@\n") if $@;
}
sub end($self) {
2020-02-15 23:38:32 +01:00
if (exists $self->{dbh} and defined $self->{dbh}) {
$self->{pbot}->{logger}->log("Closing stats SQLite database\n");
$self->{dbh}->disconnect();
delete $self->{dbh};
}
}
sub add_player($self, $id, $nick, $channel) {
2020-02-15 23:38:32 +01:00
eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Stats (id, nick, channel) VALUES (?, ?, ?)');
$sth->execute($id, $nick, $channel);
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Spinach stats: failed to add new player ($id, $nick $channel): $@\n");
return 0;
}
2019-05-09 22:25:33 +02:00
2020-02-15 23:38:32 +01:00
return $id;
}
sub get_player_id($self, $nick, $channel, $dont_create_new = undef) {
2020-02-15 23:38:32 +01:00
my ($account_id) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($nick);
$account_id = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($account_id);
2019-05-09 22:25:33 +02:00
2020-02-15 23:38:32 +01:00
return undef if not $account_id;
2019-05-09 22:25:33 +02:00
2020-02-15 23:38:32 +01:00
my $id = eval {
my $sth = $self->{dbh}->prepare('SELECT id FROM Stats WHERE id = ? AND channel = ?');
$sth->execute($account_id, $channel);
my $row = $sth->fetchrow_hashref();
return $row->{id};
};
2020-02-15 23:38:32 +01:00
if ($@) {
$self->{pbot}->{logger}->log("Spinach stats: failed to get player id: $@\n");
return undef;
}
2020-02-15 23:38:32 +01:00
$id = $self->add_player($account_id, $nick, $channel) if not defined $id and not $dont_create_new;
return $id;
}
sub get_player_data($self, $id, @columns) {
2020-02-15 23:38:32 +01:00
return undef if not $id;
my $player_data = eval {
my $sql = 'SELECT ';
if (not @columns) { $sql .= '*'; }
else {
my $comma = '';
foreach my $column (@columns) {
$sql .= "$comma$column";
$comma = ', ';
}
}
$sql .= ' FROM Stats WHERE id = ?';
my $sth = $self->{dbh}->prepare($sql);
$sth->execute($id);
return $sth->fetchrow_hashref();
};
print STDERR $@ if $@;
return $player_data;
}
sub update_player_data($self, $id, $data) {
2020-02-15 23:38:32 +01:00
eval {
my $sql = 'UPDATE Stats SET ';
2020-02-15 23:38:32 +01:00
my $comma = '';
foreach my $key (keys %$data) {
$sql .= "$comma$key = ?";
$comma = ', ';
}
2020-02-15 23:38:32 +01:00
$sql .= ' WHERE id = ?';
2020-02-15 23:38:32 +01:00
my $sth = $self->{dbh}->prepare($sql);
2020-02-15 23:38:32 +01:00
my $param = 1;
foreach my $key (keys %$data) { $sth->bind_param($param++, $data->{$key}); }
2020-02-15 23:38:32 +01:00
$sth->bind_param($param, $id);
$sth->execute();
};
print STDERR $@ if $@;
}
sub get_all_players($self, $channel) {
2020-02-15 23:38:32 +01:00
my $players = eval {
my $sth = $self->{dbh}->prepare('SELECT * FROM Stats WHERE channel = ?');
$sth->execute($channel);
return $sth->fetchall_arrayref({});
};
$self->{pbot}->{logger}->log($@) if $@;
return $players;
}
1;