From 1f8445d3bc78116a6fbd90f32ed6e5cd79146067 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 28 Jul 2014 07:45:05 +0000 Subject: [PATCH] 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 --- PBot/VERSION.pm | 4 ++-- modules/cjeopardy.pl | 22 ++++++++++++++++++++-- modules/cjeopardy_answer.pl | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 5c8ac13d..2c0ebbf3 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -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; diff --git a/modules/cjeopardy.pl b/modules/cjeopardy.pl index 77deac84..1cd44cf7 100755 --- a/modules/cjeopardy.pl +++ b/modules/cjeopardy.pl @@ -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; diff --git a/modules/cjeopardy_answer.pl b/modules/cjeopardy_answer.pl index 1047fd41..8d3e25f4 100755 --- a/modules/cjeopardy_answer.pl +++ b/modules/cjeopardy_answer.pl @@ -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;