X-Git-Url: http://git.treefish.org/wirbrennen/flipacoin.git/blobdiff_plain/0e6a8202464aa57c8ab060c8e939283c7c024b1b..3b3d3fe4ea7feca2525038b4f44cb1f0b6b7b37c:/flipacoin.cgi diff --git a/flipacoin.cgi b/flipacoin.cgi index 772b90e..95bf6c1 100755 --- a/flipacoin.cgi +++ b/flipacoin.cgi @@ -2,6 +2,7 @@ use DB_File; use CGI; +use HTML::Template; my $COINLIFETIME = 30; @@ -12,24 +13,28 @@ my $requestedaction = $cgi->param('action'); my $coinsindb=0; my $flippedcoinsindb=0; -sub alert { - print "ERROR: @_[0]\n"; +my $template; + +sub printpage { + $template->param(COINLIFETIME => $COINLIFETIME); + $template->param(FLIPPEDCOINSINDB => $flippedcoinsindb); + $template->param(COINSINDB => $coinsindb); + print "Content-type: text/html\n\n", $template->output; } -sub printcoin { - print ""; - print ""; - print "http://$ENV{HTTP_HOST}/@_[0]"; - print ""; - print ""; +sub exitalert { + $template = HTML::Template->new(filename => 'alert.html'); + $template->param(ALERTMSG => @_[0]); + printpage(); + exit(); } -sub printresult { +sub translateresult { if ( @_[0] == 0 ) { - print "Heads"; + return "Heads"; } else { - print "Tails"; + return "Tails"; } } @@ -55,34 +60,21 @@ foreach my $key ( keys %coins_db ) } } -print "Content-type: text/html\n\n"; -print ""; -print "
"; -print ""; - print "Creation time: " . localtime($coininfo[0]); - print ""; } -print ""; -print ""; +printpage(); untie(%coins_db);
"; + $template = HTML::Template->new(filename => 'usecoin.html'); + + $template->param(REQUESTEDCOINID => $requestedcoinid); + $template->param(HTTP_HOST => $ENV{HTTP_HOST}); + $template->param(COINCREATIONTIME => localtime($coininfo[0]).""); if ( $coininfosize == 1 && $requestedaction eq "" ) { - print "Status: Not yet flipped "; - print ""; - print "[flip it]"; - print ""; + $template->param(NOTYETFLIPPED => 1); } elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) { $result = int(rand(2)); $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} . "," . time . "," . $result; - print "Status: Just flipped"; - print "
"; - print "Result: "; - printresult($result); + $template->param(COINSTATUS => "Just flipped"); + $template->param(COINRESULT => translateresult($result)); } elsif ( $coininfosize == 3 ) { - print "Status: Flipped @ " . localtime($coininfo[1]); - print "
"; - print "Result: "; - printresult($coininfo[2]); + $template->param(COINSTATUS => "Flipped @ " . localtime($coininfo[1])); + $template->param(COINRESULT => translateresult($coininfo[2])); } - - print "