mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-27 12:34:18 +01:00
Spinach: stats are now nick-agnostic
This commit is contained in:
parent
d416b01359
commit
89b4b1da67
@ -67,7 +67,7 @@ sub initialize {
|
|||||||
$self->{metadata} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Spinach Metadata', filename => $self->{metadata_filename});
|
$self->{metadata} = PBot::HashObject->new(pbot => $self->{pbot}, name => 'Spinach Metadata', filename => $self->{metadata_filename});
|
||||||
$self->load_metadata;
|
$self->load_metadata;
|
||||||
|
|
||||||
$self->{stats} = PBot::Plugins::Spinach::Stats->new(filename => $self->{stats_filename});
|
$self->{stats} = PBot::Plugins::Spinach::Stats->new(pbot => $self->{pbot}, filename => $self->{stats_filename});
|
||||||
$self->{rankcmd} = PBot::Plugins::Spinach::Rank->new(pbot => $self->{pbot}, channel => $self->{channel}, filename => $self->{stats_filename});
|
$self->{rankcmd} = PBot::Plugins::Spinach::Rank->new(pbot => $self->{pbot}, channel => $self->{channel}, filename => $self->{stats_filename});
|
||||||
|
|
||||||
$self->create_states;
|
$self->create_states;
|
||||||
@ -1158,6 +1158,7 @@ sub create_states {
|
|||||||
$self->{pbot}->{logger}->log("Spinach: Creating game state machine\n");
|
$self->{pbot}->{logger}->log("Spinach: Creating game state machine\n");
|
||||||
|
|
||||||
$self->{previous_state} = '';
|
$self->{previous_state} = '';
|
||||||
|
$self->{previous_result} = '';
|
||||||
$self->{current_state} = 'nogame';
|
$self->{current_state} = 'nogame';
|
||||||
$self->{state_data} = { players => [], ticks => 0, newstate => 1 };
|
$self->{state_data} = { players => [], ticks => 0, newstate => 1 };
|
||||||
|
|
||||||
@ -2185,6 +2186,7 @@ sub showtruth {
|
|||||||
$player_data = $self->{stats}->get_player_data($player_id);
|
$player_data = $self->{stats}->get_player_data($player_id);
|
||||||
|
|
||||||
$player_data->{questions_played}++;
|
$player_data->{questions_played}++;
|
||||||
|
$player_data->{nick} = $player->{name}; # update nick in stats database once per question (nick changes, etc)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists $player->{deceived}) {
|
if (exists $player->{deceived}) {
|
||||||
@ -2369,7 +2371,10 @@ sub showfinalscore {
|
|||||||
|
|
||||||
sub nogame {
|
sub nogame {
|
||||||
my ($self, $state) = @_;
|
my ($self, $state) = @_;
|
||||||
$self->{stats}->end if $self->{stats_running};
|
if ($self->{stats_running}) {
|
||||||
|
$self->{stats}->end;
|
||||||
|
delete $self->{stats_running};
|
||||||
|
}
|
||||||
$state->{result} = 'nogame';
|
$state->{result} = 'nogame';
|
||||||
return $state;
|
return $state;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ sub initialize {
|
|||||||
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
||||||
$self->{channel} = $conf{channel} // Carp::croak("Missing channel reference to " . __FILE__);
|
$self->{channel} = $conf{channel} // Carp::croak("Missing channel reference to " . __FILE__);
|
||||||
$self->{filename} = $conf{filename} // 'stats.sqlite';
|
$self->{filename} = $conf{filename} // 'stats.sqlite';
|
||||||
$self->{stats} = PBot::Plugins::Spinach::Stats->new(filename => $self->{filename});
|
$self->{stats} = PBot::Plugins::Spinach::Stats->new(pbot => $self->{pbot}, filename => $self->{filename});
|
||||||
}
|
}
|
||||||
|
|
||||||
sub sort_generic {
|
sub sort_generic {
|
||||||
|
@ -12,8 +12,6 @@ use strict;
|
|||||||
use DBI;
|
use DBI;
|
||||||
use Carp qw(shortmess);
|
use Carp qw(shortmess);
|
||||||
|
|
||||||
my $debug = 0;
|
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my ($class, %conf) = @_;
|
my ($class, %conf) = @_;
|
||||||
my $self = bless {}, $class;
|
my $self = bless {}, $class;
|
||||||
@ -23,13 +21,14 @@ sub new {
|
|||||||
|
|
||||||
sub initialize {
|
sub initialize {
|
||||||
my ($self, %conf) = @_;
|
my ($self, %conf) = @_;
|
||||||
|
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
||||||
$self->{filename} = $conf{filename} // 'stats.sqlite';
|
$self->{filename} = $conf{filename} // 'stats.sqlite';
|
||||||
}
|
}
|
||||||
|
|
||||||
sub begin {
|
sub begin {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
print STDERR "Opening stats SQLite database: $self->{filename}\n" if $debug;
|
$self->{pbot}->{logger}->log("Opening Spinach stats SQLite database: $self->{filename}\n");
|
||||||
|
|
||||||
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0 }) or die $DBI::errstr;
|
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0 }) or die $DBI::errstr;
|
||||||
|
|
||||||
@ -55,56 +54,64 @@ CREATE TABLE IF NOT EXISTS Stats (
|
|||||||
SQL
|
SQL
|
||||||
};
|
};
|
||||||
|
|
||||||
print STDERR $@ if $@;
|
$self->{pbot}->{logger}->log("Error creating database: $@\n") if $@;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub end {
|
sub end {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
print STDERR "Closing stats SQLite database\n" if $debug;
|
|
||||||
|
|
||||||
if(exists $self->{dbh} and defined $self->{dbh}) {
|
if(exists $self->{dbh} and defined $self->{dbh}) {
|
||||||
|
$self->{pbot}->{logger}->log("Closing stats SQLite database\n");
|
||||||
$self->{dbh}->disconnect();
|
$self->{dbh}->disconnect();
|
||||||
delete $self->{dbh};
|
delete $self->{dbh};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub add_player {
|
sub add_player {
|
||||||
my ($self, $nick, $channel) = @_;
|
my ($self, $id, $nick, $channel) = @_;
|
||||||
|
|
||||||
my $id = eval {
|
eval {
|
||||||
my $sth = $self->{dbh}->prepare('INSERT INTO Stats (nick, channel) VALUES (?, ?)');
|
my $sth = $self->{dbh}->prepare('INSERT INTO Stats (id, nick, channel) VALUES (?, ?, ?)');
|
||||||
$sth->bind_param(1, $nick) ;
|
$sth->execute($id, $nick, $channel);
|
||||||
$sth->bind_param(2, $channel) ;
|
|
||||||
$sth->execute();
|
|
||||||
return $self->{dbh}->sqlite_last_insert_rowid();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
print STDERR $@ if $@;
|
if ($@) {
|
||||||
|
$self->{pbot}->{logger}->log("Spinach stats: failed to add new player ($id, $nick $channel): $@\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_player_id {
|
sub get_player_id {
|
||||||
my ($self, $nick, $channel, $dont_create_new) = @_;
|
my ($self, $nick, $channel, $dont_create_new) = @_;
|
||||||
|
|
||||||
|
my ($account_id) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($nick);
|
||||||
|
$account_id = $self->{pbot}->{messagehistory}->{database}->get_ancestor_id($account_id);
|
||||||
|
|
||||||
|
return undef if not $account_id;
|
||||||
|
|
||||||
my $id = eval {
|
my $id = eval {
|
||||||
my $sth = $self->{dbh}->prepare('SELECT id FROM Stats WHERE nick = ? AND channel = ?');
|
my $sth = $self->{dbh}->prepare('SELECT id FROM Stats WHERE id = ? AND channel = ?');
|
||||||
$sth->bind_param(1, $nick);
|
$sth->execute($account_id, $channel);
|
||||||
$sth->bind_param(2, $channel);
|
|
||||||
$sth->execute();
|
|
||||||
my $row = $sth->fetchrow_hashref();
|
my $row = $sth->fetchrow_hashref();
|
||||||
return $row->{id};
|
return $row->{id};
|
||||||
};
|
};
|
||||||
|
|
||||||
print STDERR $@ if $@;
|
if ($@) {
|
||||||
|
$self->{pbot}->{logger}->log("Spinach stats: failed to get player id: $@\n");
|
||||||
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
$id = $self->add_player($nick, $channel) if not defined $id and not $dont_create_new;
|
$id = $self->add_player($account_id, $nick, $channel) if not defined $id and not $dont_create_new;
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_player_data {
|
sub get_player_data {
|
||||||
my ($self, $id, @columns) = @_;
|
my ($self, $id, @columns) = @_;
|
||||||
|
|
||||||
|
return undef if not $id;
|
||||||
|
|
||||||
my $player_data = eval {
|
my $player_data = eval {
|
||||||
my $sql = 'SELECT ';
|
my $sql = 'SELECT ';
|
||||||
|
|
||||||
@ -120,8 +127,7 @@ sub get_player_data {
|
|||||||
|
|
||||||
$sql .= ' FROM Stats WHERE id = ?';
|
$sql .= ' FROM Stats WHERE id = ?';
|
||||||
my $sth = $self->{dbh}->prepare($sql);
|
my $sth = $self->{dbh}->prepare($sql);
|
||||||
$sth->bind_param(1, $id);
|
$sth->execute($id);
|
||||||
$sth->execute();
|
|
||||||
return $sth->fetchrow_hashref();
|
return $sth->fetchrow_hashref();
|
||||||
};
|
};
|
||||||
print STDERR $@ if $@;
|
print STDERR $@ if $@;
|
||||||
@ -160,11 +166,10 @@ sub get_all_players {
|
|||||||
|
|
||||||
my $players = eval {
|
my $players = eval {
|
||||||
my $sth = $self->{dbh}->prepare('SELECT * FROM Stats WHERE channel = ?');
|
my $sth = $self->{dbh}->prepare('SELECT * FROM Stats WHERE channel = ?');
|
||||||
$sth->bind_param(1, $channel);
|
$sth->execute($channel);
|
||||||
$sth->execute();
|
|
||||||
return $sth->fetchall_arrayref({});
|
return $sth->fetchall_arrayref({});
|
||||||
};
|
};
|
||||||
print STDERR $@ if $@;
|
$self->{pbot}->{logger}->log($@) if $@;
|
||||||
return $players;
|
return $players;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user