#!/usr/bin/perl -Tw use strict; $|++; ## config my $DATA_DB = "/home/merlyn/Web/profanity_quiz"; my $COUNT = 3; ## end config use CGI qw(:all); use CGI::Carp qw(fatalsToBrowser); ## set up the cache use File::Cache; my $cache = File::Cache->new({namespace => 'profanityquiz', username => 'nobody', filemode => 0666, expires_in => 3600, # one hour }); unless ($cache->get(" _purge_ ")) { # cleanup? $cache->purge; $cache->set(" _purge ", 1, 3600 * 4); # purge every four hours } ## connect to the database dbmopen my %DATA, $DATA_DB, 0666 or die "Cannot open data: $!"; ## session info my @unused_keys; my @this_keys; my $winner; my $answered; my $correct; ## end session info print header, start_html("Guess the profanity"), h1("Guess the profanity"); ## first, pull up existing session data: my $session = param('session'); if (defined $session and $session =~ /\A[0-9a-f]{32}\z/ and my $data = $cache->get($session)) { @unused_keys = @{$data->{unused_keys}}; @this_keys = @{$data->{this_keys}}; $winner = $data->{winner}; $answered = $data->{answered}; $correct = $data->{correct}; } else { undef $session; # no good, so ignore } ## now handle form response if within a valid session: if ($session) { if (defined(my $answer = param('answer'))) { if (my ($guess_answered, $guess_guessed) = $answer =~ /(\d+)-(\d+)/) { if (0 <= $guess_guessed and $guess_guessed <= $#this_keys) { print h2("Scoring"); if ($guess_answered == $answered) { $answered += 1; print "You guessed ", a({-href => "http://$this_keys[$guess_guessed]"}, $DATA{$this_keys[$guess_guessed]} =~ /(.*)/), ", "; if ($guess_guessed == $winner) { $correct += 1; print "which is correct!"; } else { print "which is wrong. The correct answer is ", a({-href => "http://$this_keys[$winner]"}, $DATA{$this_keys[$winner]} =~ /(.*)/), "."; } @this_keys = (); } else { print "You've already answered this! Stop trying to cheat!"; } print p("Your total score so far is $correct out of $answered."); } } } } else { # start a new session: require MD5; param('session', $session = MD5->hexhash(MD5->hexhash(time.{}.rand().$$))); @unused_keys = keys %DATA; $answered = $correct = 0; @this_keys = (); } unless (@this_keys) { # pick a new question push @this_keys, splice @unused_keys, rand @unused_keys, 1 for 1..$COUNT; $winner = int rand @this_keys; } print h2("Show us how smart you are..."); ## save session data for next hit: { my $data = {}; @{$data->{unused_keys}} = @unused_keys; @{$data->{this_keys}} = @this_keys; $data->{winner} = $winner; $data->{answered} = $answered; $data->{correct} = $correct; $cache->set($session, $data); } my @this_values = @DATA{@this_keys}; # cache %DATA we need print "Which one of these movies had this profanity information at ", a({-href => 'http://www.screenit.com/'}, "screenit.com"), "?"; ## pull up the profanity paragraph, boxed for easy reading: print table({-border => 1, -cellspacing => 0, -cellpadding => 5}, Tr(td($this_values[$winner] =~ /^.*\n([\s\S]+)/))); ## show the choices, with links back to us including session tag: my $url = url; print ol(map { my ($title) = $this_values[$_] =~ /(.*)/; li(a({-href => "$url?session=$session&answer=$answered-$_"}, $title)); } 0..$#this_values); ## (for debugging, because I was lazy... :-) ## print "\n(Hint: the answer is ", $this_values[$winner] =~ /(.*)/, ")\n"; print h2("Disclaimer"); print "All decisions of our judges are final. ", "Even if two movies have the same answer. "; print "And ", a({-href => 'http://www.screenit.com/'}, "screenit.com"), " had nothing to do with this program. It's all ", a({-href => "/merlyn/"}, "my"), " fault. "; print end_html;