Add scorestreaks to C Jeopardy

This commit is contained in:
Pragmatic Software 2015-01-28 03:04:28 -08:00
parent b647034595
commit e656c4f2cc
3 changed files with 78 additions and 20 deletions

View File

@ -8,6 +8,8 @@ use strict;
use DBI;
use Carp qw(shortmess);
my $debug = 0;
sub new {
my ($class, %conf) = @_;
my $self = bless {}, $class;
@ -23,7 +25,7 @@ sub initialize {
sub begin {
my $self = shift;
print STDERR "Opening scores SQLite database: $self->{filename}\n";
print STDERR "Opening scores SQLite database: $self->{filename}\n" if $debug;
$self->{dbh} = DBI->connect("dbi:SQLite:dbname=$self->{filename}", "", "", { RaiseError => 1, PrintError => 0 }) or die $DBI::errstr;
@ -58,7 +60,7 @@ SQL
sub end {
my $self = shift;
print STDERR "Closing scores SQLite database\n";
print STDERR "Closing scores SQLite database\n" if $debug;
if(exists $self->{dbh} and defined $self->{dbh}) {
$self->{dbh}->disconnect();
@ -121,6 +123,7 @@ sub get_player_data {
$sth->execute();
return $sth->fetchrow_hashref();
};
print STDERR $@ if $@;
return $player_data;
}
@ -150,4 +153,17 @@ sub update_player_data {
};
}
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;
}
1;

View File

@ -158,26 +158,19 @@ foreach my $answer (@valid_answers) {
print "\n";
}
unlink "$CJEOPARDY_DATA-$channel";
unlink "$CJEOPARDY_HINT-$channel";
my $streakers = $scores->get_all_correct_streaks($channel);
open $fh, ">", "$CJEOPARDY_LAST_ANSWER-$channel" or die "Couldn't open $CJEOPARDY_LAST_ANSWER-$channel: $!";
my $time = scalar gettimeofday;
print $fh "$nick\n$data[1]$time\n";
close $fh;
foreach my $streaker (@$streakers) {
next if $streaker->{nick} eq $nick;
if ($channel eq '#cjeopardy') {
my $question = `./cjeopardy/cjeopardy.pl $channel`;
if ($hint_only_mode) {
my $hint = `./cjeopardy/cjeopardy_hint.pl $channel`;
$hint =~ s/^Hint: //;
print "Next hint: $hint\n";
} else {
print "$color{green}Next question$color{reset}: $question\n";
if ($streaker->{correct_streak} >= 3) {
print "$color{orange}$nick$color{red} ended $color{orange}$streaker->{nick}$color{red}'s $color{orange}$streaker->{correct_streak}$color{red} correct answer streak!$color{reset}\n";
}
$streaker->{correct_streak} = 0;
$scores->update_player_data($streaker->{id}, $streaker);
}
$player_data->{correct_answers}++;
$player_data->{lifetime_correct_answers}++;
$player_data->{correct_streak}++;
@ -192,8 +185,44 @@ foreach my $answer (@valid_answers) {
$player_data->{lifetime_highest_correct_streak} = $player_data->{highest_correct_streak};
}
my %streaks = (
3 => "$color{orange}$nick$color{green} is on a $color{orange}3$color{green} correct answer streak!",
5 => "$color{orange}$nick$color{green} is hot with a $color{orange}5$color{green} correct answer streak!",
8 => "$color{orange}$nick$color{green} is on fire with an $color{orange}8$color{green} correct answer streak!",
12 => "$color{orange}$nick$color{green} is on fire with a $color{orange}12$color{green} correct answer streak!",
16 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}16$color{green} correct answer streak!",
20 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}20$color{green} correct answer streak!",
25 => "$color{orange}$nick$color{green} is DOMINATING with a $color{orange}25$color{green} correct answer streak!",
30 => "$color{orange}$nick$color{green} IS UNTOUCHABLE WITH A $color{orange}30$color{green} CORRECT ANSWER STREAK! ARE THEY PLAYING WITH THEMSELVES?!"
);
if (exists $streaks{$player_data->{correct_streak}}) {
print "$streaks{$player_data->{correct_streak}}$color{reset}\n";
}
$scores->update_player_data($player_id, $player_data);
$scores->end;
unlink "$CJEOPARDY_DATA-$channel";
unlink "$CJEOPARDY_HINT-$channel";
open $fh, ">", "$CJEOPARDY_LAST_ANSWER-$channel" or die "Couldn't open $CJEOPARDY_LAST_ANSWER-$channel: $!";
my $time = scalar gettimeofday;
print $fh "$nick\n$data[1]$time\n";
close $fh;
if ($channel eq '#cjeopardy') {
my $question = `./cjeopardy.pl $channel`;
if ($hint_only_mode) {
my $hint = `./cjeopardy_hint.pl $channel`;
$hint =~ s/^Hint: //;
print "Next hint: $hint\n";
} else {
print "$color{green}Next question$color{reset}: $question\n";
}
}
exit;
}
}
@ -224,5 +253,20 @@ if ($player_data->{highest_wrong_streak} > $player_data->{lifetime_highest_wrong
$player_data->{lifetime_highest_wrong_streak} = $player_data->{highest_wrong_streak};
}
my %streaks = (
3 => "$color{red}Try a little bit harder, $color{orange}$nick$color{red}.",
5 => "$color{red}Guessing, are we, $color{orange}$nick$color{red}?",
8 => "$color{red}Please use better guesses, $color{orange}$nick!",
12 => "$color{red}Are you even trying, $color{orange}$nick$color{red}?!",
16 => "$color{red}Have you been checked for mental retardation yet, $color{orange}$nick$color{red}?",
20 => "$color{red}Are you sure you weren't dropped on your head, $color{orange}$nick$color{red}?",
25 => "$color{red}Maybe you should go look it up, $color{orange}$nick$color{red}.",
30 => "$color{red}FOR FUCK'S SAKE, GO LOOK IT UP ALREADY, $color{orange}$nick$color{red}!"
);
if (exists $streaks{$player_data->{wrong_streak}}) {
print "$streaks{$player_data->{wrong_streak}}$color{reset}\n";
}
$scores->update_player_data($player_id, $player_data);
$scores->end;

View File

@ -18,8 +18,6 @@ my $timeout = 30;
my $nick = shift @ARGV;
my $channel = shift @ARGV;
print STDERR "nick: $nick, channel: $channel\n";
sub encode { my $str = shift; $str =~ s/\\(.)/{sprintf "\\%03d", ord($1)}/ge; return $str; }
sub decode { my $str = shift; $str =~ s/\\(\d{3})/{"\\" . chr($1)}/ge; return $str }