2014-07-29 19:30:12 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-07-29 19:30:12 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2014-07-29 22:07:48 +02:00
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
use Time::Duration qw/duration/;
|
2015-01-30 06:55:46 +01:00
|
|
|
use Fcntl qw(:flock);
|
2014-07-29 19:30:12 +02:00
|
|
|
|
2018-03-12 16:52:43 +01:00
|
|
|
use lib ".";
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
use Scorekeeper;
|
2015-05-23 11:27:53 +02:00
|
|
|
use QStatskeeper;
|
2015-01-28 09:40:40 +01:00
|
|
|
use IRCColors;
|
|
|
|
|
|
|
|
my $CJEOPARDY_DATA = 'data/cjeopardy.dat';
|
|
|
|
my $CJEOPARDY_HINT = 'data/cjeopardy.hint';
|
2014-07-29 19:30:12 +02:00
|
|
|
|
|
|
|
my @hints = (0.90, 0.75, 0.50, 0.25, 0.10);
|
2015-05-22 12:57:30 +02:00
|
|
|
my $timeout = 20;
|
2014-07-29 19:30:12 +02:00
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
my $nick = shift @ARGV;
|
2014-07-29 19:30:12 +02:00
|
|
|
my $channel = shift @ARGV;
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
|
|
|
if ($channel !~ /^#/) {
|
2014-08-31 22:21:09 +02:00
|
|
|
print "Sorry, C Jeopardy must be played in a channel. Feel free to join #cjeopardy.\n";
|
2014-07-29 19:30:12 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-01-30 06:55:46 +01:00
|
|
|
open my $semaphore, ">", "$CJEOPARDY_DATA-$channel.lock" or die "Couldn't create semaphore lock: $!";
|
|
|
|
flock $semaphore, LOCK_EX;
|
|
|
|
|
2014-07-29 19:30:12 +02:00
|
|
|
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>;
|
|
|
|
close $fh;
|
|
|
|
|
2014-08-04 00:20:54 +02:00
|
|
|
my @valid_answers = map { decode $_ } split /\|/, encode $data[1];
|
2014-07-29 19:30:12 +02:00
|
|
|
|
|
|
|
my ($hint, $length) = ('', 0);
|
|
|
|
foreach my $answer (@valid_answers) {
|
|
|
|
chomp $answer;
|
|
|
|
$answer =~ s/\\\|/|/g;
|
2014-08-04 00:20:54 +02:00
|
|
|
|
|
|
|
my $supplemental_text;
|
|
|
|
if ($answer =~ s/\s*{(.*)}\s*$//) {
|
|
|
|
$supplemental_text = $1;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:30:12 +02:00
|
|
|
if (length $answer > $length) {
|
|
|
|
$length = length $answer;
|
|
|
|
$hint = $answer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 22:07:48 +02:00
|
|
|
my ($hint_counter, $last_timeout);
|
2014-07-29 19:30:12 +02:00
|
|
|
my $ret = open $fh, "<", "$CJEOPARDY_HINT-$channel";
|
|
|
|
if (defined $ret) {
|
|
|
|
$hint_counter = <$fh>;
|
2014-07-29 22:07:48 +02:00
|
|
|
$last_timeout = <$fh>;
|
2014-07-29 19:30:12 +02:00
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2014-08-04 00:20:54 +02:00
|
|
|
$last_timeout = 0 if not defined $last_timeout;
|
|
|
|
|
2014-07-29 22:07:48 +02:00
|
|
|
my $duration = scalar gettimeofday - $last_timeout;
|
|
|
|
if ($duration < $timeout) {
|
|
|
|
$duration = duration($timeout - $duration);
|
2014-08-04 00:20:54 +02:00
|
|
|
unless ($duration eq 'just now') {
|
2015-01-31 03:40:19 +01:00
|
|
|
print "$color{red}Please wait $color{orange}$duration$color{red} before requesting another hint.$color{reset}\n";
|
2014-08-04 00:20:54 +02:00
|
|
|
exit;
|
|
|
|
}
|
2014-07-29 22:07:48 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 19:30:12 +02:00
|
|
|
$hint_counter++;
|
|
|
|
|
|
|
|
open $fh, ">", "$CJEOPARDY_HINT-$channel" or die "Couldn't open $CJEOPARDY_HINT-$channel: $!";
|
|
|
|
print $fh "$hint_counter\n";
|
2014-07-29 22:07:48 +02:00
|
|
|
print $fh scalar gettimeofday, "\n";
|
2014-07-29 19:30:12 +02:00
|
|
|
close $fh;
|
|
|
|
|
|
|
|
my $hidden_character_count = int length ($hint) * $hints[$hint_counter > $#hints ? $#hints : $hint_counter];
|
|
|
|
my $spaces = () = $hint =~ / /g;
|
2014-08-04 00:20:54 +02:00
|
|
|
my $dashes = () = $hint =~ /-/g;
|
|
|
|
my $underscores = () = $hint =~ /_/g;
|
2015-12-23 02:20:00 +01:00
|
|
|
my $dquotes = () = $hint =~ /"/g;
|
|
|
|
|
2014-07-29 19:30:12 +02:00
|
|
|
|
|
|
|
my @indices;
|
2015-12-23 02:20:00 +01:00
|
|
|
while (@indices <= $hidden_character_count - $spaces - $dashes - $underscores - $dquotes) {
|
2014-07-29 19:30:12 +02:00
|
|
|
my $index = int rand($length);
|
2014-08-04 00:20:54 +02:00
|
|
|
my $char = substr($hint, $index, 1);
|
|
|
|
next if $char eq ' ';
|
|
|
|
next if $char eq '-';
|
|
|
|
next if $char eq '_';
|
2015-12-23 02:20:00 +01:00
|
|
|
next if $char eq '"';
|
2014-07-29 19:30:12 +02:00
|
|
|
next if grep { $index eq $_ } @indices;
|
|
|
|
push @indices, $index;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $index (@indices) {
|
2014-08-31 22:21:09 +02:00
|
|
|
substr $hint, $index, 1, '.';
|
2014-07-29 19:30:12 +02:00
|
|
|
}
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
print "$color{lightgreen}Hint$color{reset}: $hint\n";
|
|
|
|
|
2015-05-22 12:57:30 +02:00
|
|
|
exit if $nick eq 'candide'; # hint_only_mode
|
|
|
|
|
2015-01-28 09:40:40 +01:00
|
|
|
my $scores = Scorekeeper->new;
|
|
|
|
$scores->begin;
|
|
|
|
my $id = $scores->get_player_id($nick, $channel);
|
|
|
|
my $player_data = $scores->get_player_data($id, 'hints', 'lifetime_hints');
|
|
|
|
$player_data->{hints}++;
|
|
|
|
$player_data->{lifetime_hints}++;
|
|
|
|
$scores->update_player_data($id, $player_data);
|
|
|
|
$scores->end;
|
2015-05-23 11:27:53 +02:00
|
|
|
|
2016-12-07 02:58:01 +01:00
|
|
|
print "(You can use !hint again to reveal more characters.)\n" if $player_data->{hints} <= 1;
|
|
|
|
|
2015-05-23 11:27:53 +02:00
|
|
|
($id) = $data[0] =~ m/^(\d+)/;
|
|
|
|
my $qstats = QStatskeeper->new;
|
|
|
|
$qstats->begin;
|
|
|
|
my $qdata = $qstats->get_question_data($id);
|
|
|
|
$qdata->{last_touched} = gettimeofday;
|
|
|
|
$qdata->{hints}++;
|
|
|
|
$qstats->update_question_data($id, $qdata);
|
|
|
|
$qstats->end;
|