3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

C Jeopardy: Now says "Too slow!" if someone else also answers the correct answer within 15 seconds

This commit is contained in:
Pragmatic Software 2014-08-04 06:25:56 +00:00
parent 7d6e505d9f
commit 18d245cf10
3 changed files with 333 additions and 270 deletions

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 763, BUILD_REVISION => 764,
BUILD_DATE => "2014-08-03", BUILD_DATE => "2014-08-03",
}; };

File diff suppressed because it is too large Load Diff

View File

@ -4,13 +4,16 @@ use warnings;
use strict; use strict;
use Text::Levenshtein qw(fastdistance); use Text::Levenshtein qw(fastdistance);
use Time::HiRes qw(gettimeofday);
my $CJEOPARDY_DATA = 'cjeopardy.dat'; my $CJEOPARDY_DATA = 'cjeopardy.dat';
my $CJEOPARDY_HINT = 'cjeopardy.hint'; my $CJEOPARDY_HINT = 'cjeopardy.hint';
my $CJEOPARDY_LAST_ANSWER = 'cjeopardy.last_ans';
my $hint_only_mode = 0; my $hint_only_mode = 0;
my $channel = shift @ARGV; my $channel = shift @ARGV;
my $nick = shift @ARGV;
my $text = join(' ', @ARGV); my $text = join(' ', @ARGV);
sub encode { my $str = shift; $str =~ s/\\(.)/{sprintf "\\%03d", ord($1)}/ge; return $str; } sub encode { my $str = shift; $str =~ s/\\(.)/{sprintf "\\%03d", ord($1)}/ge; return $str; }
@ -35,9 +38,70 @@ if (not length $lctext) {
} }
my @data; my @data;
open my $fh, "<", "$CJEOPARDY_DATA-$channel" or print "There is no open C Jeopardy question. Use `cjeopardy` to get a question.\n" and exit;
@data = <$fh>; my $ret = open my $fh, "<", "$CJEOPARDY_LAST_ANSWER-$channel";
close $fh; if (defined $ret) {
my $last_nick = <$fh>;
my $last_answers = <$fh>;
my $last_timestamp = <$fh>;
close $fh;
chomp $last_nick;
if(scalar gettimeofday - $last_timestamp <= 15) {
$ret = open $fh, "<", "$CJEOPARDY_DATA-$channel";
if (defined $ret) {
@data = <$fh>;
close $fh;
}
my @current_answers = map { decode $_ } split /\|/, encode $data[1] if @data;
my @valid_answers = map { decode $_ } split /\|/, encode $last_answers;
foreach my $answer (@valid_answers) {
chomp $answer;
$answer =~ s/\\\|/|/g;
$answer =~ s/\s*{.*}\s*//;
my $skip_last;
if (@current_answers) {
foreach my $current_answer (@current_answers) {
chomp $current_answer;
$current_answer =~ s/\\\|/|/g;
$current_answer =~ s/\s*{.*}\s*//;
my $distance = fastdistance(lc $answer, lc $current_answer);
my $length = (length($answer) > length($current_answer)) ? length $answer : length $current_answer;
if ($distance / $length < 0.15) {
$skip_last = 1;
last;
}
}
}
last if $skip_last;
my $distance = fastdistance($lctext, lc $answer);
my $length = (length($lctext) > length($answer)) ? length $lctext : length $answer;
if ($distance / $length < 0.15) {
if ($last_nick eq $nick) {
print "Er, you already correctly answered that question.\n";
} else {
print "Too slow! $last_nick got the correct answer.\n";
}
exit;
}
}
}
}
if (not @data) {
open $fh, "<", "$CJEOPARDY_DATA-$channel" or print "There is no open C Jeopardy question. Use `cjeopardy` to get a question.\n" and exit;
@data = <$fh>;
close $fh;
}
my @valid_answers = map { decode $_ } split /\|/, encode $data[1]; my @valid_answers = map { decode $_ } split /\|/, encode $data[1];
@ -69,6 +133,11 @@ foreach my $answer (@valid_answers) {
unlink "$CJEOPARDY_DATA-$channel"; unlink "$CJEOPARDY_DATA-$channel";
unlink "$CJEOPARDY_HINT-$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') { if ($channel eq '#cjeopardy') {
my $question = `./cjeopardy.pl $channel`; my $question = `./cjeopardy.pl $channel`;