2015-01-28 09:40:40 +01:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
package Scorekeeper;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Carp qw(shortmess);
|
|
|
|
|
2015-01-28 12:04:28 +01:00
|
|
|
my $debug = 0;
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
sub new {
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{filename} = $conf{filename} // 'scores.sqlite';
|
|
|
|
}
|
|
|
|
|
|
|
|
sub begin {
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-01-28 12:04:28 +01:00
|
|
|
print STDERR "Opening scores SQLite database: $self->{filename}\n" if $debug;
|
2015-01-28 09:40:40 +01:00
|
|
|
|
|
|
|
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0 }) or die $DBI::errstr;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$self->{dbh}->do(<< 'SQL');
|
|
|
|
CREATE TABLE IF NOT EXISTS Scores (
|
2015-05-21 11:25:08 +02:00
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
nick TEXT NOT NULL COLLATE NOCASE,
|
|
|
|
channel TEXT NOT NULL COLLATE NOCASE,
|
|
|
|
correct_answers INTEGER DEFAULT 0,
|
|
|
|
wrong_answers INTEGER DEFAULT 0,
|
|
|
|
lifetime_correct_answers INTEGER DEFAULT 0,
|
|
|
|
lifetime_wrong_answers INTEGER DEFAULT 0,
|
|
|
|
correct_streak INTEGER DEFAULT 0,
|
|
|
|
wrong_streak INTEGER DEFAULT 0,
|
|
|
|
lifetime_highest_correct_streak INTEGER DEFAULT 0,
|
|
|
|
lifetime_highest_wrong_streak INTEGER DEFAULT 0,
|
|
|
|
highest_correct_streak INTEGER DEFAULT 0,
|
|
|
|
highest_wrong_streak INTEGER DEFAULT 0,
|
|
|
|
hints INTEGER DEFAULT 0,
|
|
|
|
lifetime_hints INTEGER DEFAULT 0,
|
|
|
|
last_wrong_timestamp NUMERIC DEFAULT 0,
|
|
|
|
last_correct_timestamp NUMERIC DEFAULT 0,
|
|
|
|
quickest_correct NUMERIC DEFAULT 0,
|
|
|
|
correct_streak_timestamp NUMERIC DEFAULT 0,
|
|
|
|
highest_quick_correct_streak INTEGER DEFAULT 0,
|
|
|
|
quickest_correct_streak NUMERIC DEFAULT 0,
|
2015-05-21 12:26:38 +02:00
|
|
|
lifetime_highest_quick_correct_streak INTEGER DEFAULT 0,
|
2015-05-21 11:25:08 +02:00
|
|
|
lifetime_quickest_correct_streak NUMERIC DEFAULT 0
|
2015-01-28 09:40:40 +01:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
};
|
|
|
|
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end {
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-01-28 12:04:28 +01:00
|
|
|
print STDERR "Closing scores SQLite database\n" if $debug;
|
2015-01-28 09:40:40 +01:00
|
|
|
|
|
|
|
if(exists $self->{dbh} and defined $self->{dbh}) {
|
|
|
|
$self->{dbh}->disconnect();
|
|
|
|
delete $self->{dbh};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_player {
|
|
|
|
my ($self, $nick, $channel) = @_;
|
|
|
|
|
|
|
|
my $id = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('INSERT INTO Scores (nick, channel) VALUES (?, ?)');
|
|
|
|
$sth->bind_param(1, $nick) ;
|
|
|
|
$sth->bind_param(2, $channel) ;
|
|
|
|
$sth->execute();
|
|
|
|
return $self->{dbh}->sqlite_last_insert_rowid();
|
|
|
|
};
|
|
|
|
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_player_id {
|
2015-01-28 22:11:04 +01:00
|
|
|
my ($self, $nick, $channel, $dont_create_new) = @_;
|
2015-01-28 09:40:40 +01:00
|
|
|
|
|
|
|
my $id = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT id FROM Scores WHERE nick = ? AND channel = ?');
|
|
|
|
$sth->bind_param(1, $nick);
|
|
|
|
$sth->bind_param(2, $channel);
|
|
|
|
$sth->execute();
|
|
|
|
my $row = $sth->fetchrow_hashref();
|
|
|
|
return $row->{id};
|
|
|
|
};
|
|
|
|
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
|
2015-01-28 22:11:04 +01:00
|
|
|
$id = $self->add_player($nick, $channel) if not defined $id and not $dont_create_new;
|
2015-01-28 09:40:40 +01:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_player_data {
|
|
|
|
my ($self, $id, @columns) = @_;
|
|
|
|
|
|
|
|
my $player_data = eval {
|
|
|
|
my $sql = 'SELECT ';
|
|
|
|
|
|
|
|
if(not @columns) {
|
|
|
|
$sql .= '*';
|
|
|
|
} else {
|
|
|
|
my $comma = '';
|
|
|
|
foreach my $column (@columns) {
|
|
|
|
$sql .= "$comma$column";
|
|
|
|
$comma = ', ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' FROM Scores WHERE id = ?';
|
|
|
|
my $sth = $self->{dbh}->prepare($sql);
|
|
|
|
$sth->bind_param(1, $id);
|
|
|
|
$sth->execute();
|
|
|
|
return $sth->fetchrow_hashref();
|
|
|
|
};
|
2015-01-28 12:04:28 +01:00
|
|
|
print STDERR $@ if $@;
|
2015-01-28 09:40:40 +01:00
|
|
|
return $player_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub update_player_data {
|
|
|
|
my ($self, $id, $data) = @_;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $sql = 'UPDATE Scores SET ';
|
|
|
|
|
|
|
|
my $comma = '';
|
|
|
|
foreach my $key (keys %$data) {
|
|
|
|
$sql .= "$comma$key = ?";
|
|
|
|
$comma = ', ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= ' WHERE id = ?';
|
|
|
|
|
|
|
|
my $sth = $self->{dbh}->prepare($sql);
|
|
|
|
|
|
|
|
my $param = 1;
|
|
|
|
foreach my $key (keys %$data) {
|
|
|
|
$sth->bind_param($param++, $data->{$key});
|
|
|
|
}
|
|
|
|
|
|
|
|
$sth->bind_param($param, $id);
|
|
|
|
$sth->execute();
|
|
|
|
};
|
2015-01-28 22:11:04 +01:00
|
|
|
print STDERR $@ if $@;
|
2015-01-28 09:40:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 12:04:28 +01:00
|
|
|
sub get_all_correct_streaks {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
|
|
|
|
my $streakers = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Scores WHERE channel = ? AND correct_streak > 0');
|
|
|
|
$sth->bind_param(1, $channel);
|
|
|
|
$sth->execute();
|
|
|
|
return $sth->fetchall_arrayref({});
|
|
|
|
};
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
return $streakers;
|
|
|
|
}
|
|
|
|
|
2015-02-12 05:58:16 +01:00
|
|
|
sub get_all_players {
|
|
|
|
my ($self, $channel) = @_;
|
|
|
|
|
|
|
|
my $players = eval {
|
|
|
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Scores WHERE channel = ?');
|
|
|
|
$sth->bind_param(1, $channel);
|
|
|
|
$sth->execute();
|
|
|
|
return $sth->fetchall_arrayref({});
|
|
|
|
};
|
|
|
|
print STDERR $@ if $@;
|
|
|
|
return $players;
|
|
|
|
}
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
1;
|