From 236b4c1d066cbdc1e00dc5b0ecec7c0346c43644 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 29 Jul 2014 20:07:48 +0000 Subject: [PATCH] C Jeopardy: Add time-limit to hints --- PBot/VERSION.pm | 2 +- modules/cjeopardy_hint.pl | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 48caf16f..82a0378b 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 756, + BUILD_REVISION => 757, BUILD_DATE => "2014-07-29", }; diff --git a/modules/cjeopardy_hint.pl b/modules/cjeopardy_hint.pl index 6f87b593..a9c34ad2 100755 --- a/modules/cjeopardy_hint.pl +++ b/modules/cjeopardy_hint.pl @@ -3,12 +3,14 @@ use warnings; use strict; -use Text::Levenshtein qw(fastdistance); +use Time::HiRes qw/gettimeofday/; +use Time::Duration qw/duration/; my $CJEOPARDY_DATA = 'cjeopardy.dat'; my $CJEOPARDY_HINT = 'cjeopardy.hint'; my @hints = (0.90, 0.75, 0.50, 0.25, 0.10); +my $timeout = 60; my $channel = shift @ARGV; @@ -37,17 +39,26 @@ foreach my $answer (@valid_answers) { } } -my $hint_counter; +my ($hint_counter, $last_timeout); my $ret = open $fh, "<", "$CJEOPARDY_HINT-$channel"; if (defined $ret) { $hint_counter = <$fh>; + $last_timeout = <$fh>; close $fh; } +my $duration = scalar gettimeofday - $last_timeout; +if ($duration < $timeout) { + $duration = duration($timeout - $duration); + print "Please wait $duration before requesting another hint.\n"; + exit; +} + $hint_counter++; open $fh, ">", "$CJEOPARDY_HINT-$channel" or die "Couldn't open $CJEOPARDY_HINT-$channel: $!"; print $fh "$hint_counter\n"; +print $fh scalar gettimeofday, "\n"; close $fh; my $hidden_character_count = int length ($hint) * $hints[$hint_counter > $#hints ? $#hints : $hint_counter];