C Jeopardy: Add time-limit to `cjeopardy` command. The current question will be shown if within this time-limit, otherwise a new question is shown

This commit is contained in:
Pragmatic Software 2014-07-28 07:45:05 +00:00
parent 4b2abf316f
commit 1f8445d3bc
3 changed files with 23 additions and 5 deletions

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 750,
BUILD_DATE => "2014-07-27",
BUILD_REVISION => 751,
BUILD_DATE => "2014-07-28",
};
1;

View File

@ -4,11 +4,14 @@ use warnings;
use strict;
use Time::HiRes qw/gettimeofday/;
use Time::Duration qw/duration/;
my $CJEOPARDY_FILE = 'cjeopardy.txt';
my $CJEOPARDY_DATA = 'cjeopardy.dat';
my $CJEOPARDY_SQL = 'cjeopardy.sqlite3';
my $TIMELIMIT = 300;
my $channel = shift @ARGV;
my $text = join(' ', @ARGV);
@ -17,8 +20,22 @@ if ($channel !~ /^#/) {
exit;
}
my $ret = open my $fh, "<", "$CJEOPARDY_DATA-$channel";
if (defined $ret) {
my $last_question = <$fh>;
my $last_answer = <$fh>;
my $last_timestamp = <$fh>;
if (scalar gettimeofday - $last_timestamp <= $TIMELIMIT) {
my $duration = duration($TIMELIMIT - scalar gettimeofday - $last_timestamp);
print "The current question is: $last_question You will be able to request a new question in $duration.\n";
close $fh;
exit;
}
}
my @questions;
open my $fh, "<", $CJEOPARDY_FILE or die "Could not open $CJEOPARDY_FILE: $!";
open $fh, "<", $CJEOPARDY_FILE or die "Could not open $CJEOPARDY_FILE: $!";
while (my $question = <$fh>) {
my ($question_only) = split /\|/, $question, 2;
next if length $text and $question_only !~ /\Q$text\E/i;
@ -41,6 +58,7 @@ $q =~ s/^\[.*?\]\s+//;
print "$q\n";
open $fh, ">", "$CJEOPARDY_DATA-$channel" or die "Could not open $CJEOPARDY_DATA-$channel: $!";
print $fh "$q\n";
print $fh "$a\n";
print $fh gettimeofday, "\n";
print $fh scalar gettimeofday, "\n";
close $fh;

View File

@ -35,7 +35,7 @@ open my $fh, "<", "$CJEOPARDY_DATA-$channel" or print "There is no open C Jeopar
@data = <$fh>;
close $fh;
my @valid_answers = map { lc decode $_ } split /\|/, encode $data[0];
my @valid_answers = map { lc decode $_ } split /\|/, encode $data[1];
foreach my $answer (@valid_answers) {
chomp $answer;