X-Git-Url: http://git.treefish.org/wirbrennen/flipacoin.git/blobdiff_plain/dc552cde191236983a1ab9a1e08311f279ccc6a2..refs/heads/master:/flipacoin.cgi diff --git a/flipacoin.cgi b/flipacoin.cgi index 0ada425..cba07b1 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,46 @@ my $requestedaction = $cgi->param('action'); my $coinsindb=0; my $flippedcoinsindb=0; -sub alert { - print "ERROR: @_[0]\n"; +my $template; + +sub setprotocolparam { + { + no warnings 'uninitialized'; + if( length $ENV{HTTPS} ) { + $template->param(PROTOCOL => "https"); + } + else { + $template->param(PROTOCOL => "http"); + } + } +} + +sub printpage { + $template->param(COINLIFETIME => $COINLIFETIME); + $template->param(FLIPPEDCOINSINDB => $flippedcoinsindb); + $template->param(COINSINDB => $coinsindb); + + my $rootloc = $ENV{SCRIPT_FILENAME}; + $rootloc =~ s/$ENV{DOCUMENT_ROOT}//g; + $rootloc =~ s/\/[^\/]*$//g; + $template->param( ROOTURL => $ENV{HTTP_HOST} . $rootloc ); + + 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 +78,21 @@ foreach my $key ( keys %coins_db ) } } -print "Content-type: text/html\n\n"; -print ""; -print ""; -print "flipacoin.org - The Internet's first coinflipping service."; -print ""; -print ""; - if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) { - alert("You sent me an invalid coin!"); - exit; + exitalert("You sent me an invalid coin!"); } if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) { - alert("You sent me an invalid action!"); - exit; + exitalert("You sent me an invalid action!"); } -print "During the last $COINLIFETIME days $flippedcoinsindb/$coinsindb coins were flipped/created. | "; -print "

flipacoin.org - The Internet's first coinflipping service.

"; - if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) { - print "
[Create a coin]"; - print " to make a decision."; + $template = HTML::Template->new(filename => 'create.html'); } elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) { my @chars = ("a".."z", "0".."9"); my $newcoinid; + $newcoinid .= $chars[rand @chars] for 1..13; while ( exists $coins_db{$newcoinid} ) { $newcoinid = ""; @@ -91,55 +101,43 @@ elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) { $coins_db{$newcoinid} = time; - print "Created a new coin "; - printcoin($newcoinid); - print " for you.
"; - print "
"; - print "Send the coin to the other party and tell them to flip it to make a decision."; + $template = HTML::Template->new(filename => 'created.html'); + $template->param(NEWCOINID => $newcoinid); + setprotocolparam(); } elsif ( length($requestedcoinid) > 0 ) { if ( ! exists $coins_db{$requestedcoinid} ) { - alert("The coinid $requestedcoinid does not exist!"); - exit; + exitalert("The coinid $requestedcoinid does not exist!"); } my @coininfo = split(/,/, $coins_db{$requestedcoinid}); my $coininfosize = $#coininfo + 1; - printcoin($requestedcoinid); - print "
"; - print "
";
-    print "Creation time: " . localtime($coininfo[0]);
-    print "
"; + $template = HTML::Template->new(filename => 'usecoin.html'); + + $template->param(REQUESTEDCOINID => $requestedcoinid); + $template->param(COINCREATIONTIME => localtime($coininfo[0]).""); + setprotocolparam(); if ( $coininfosize == 1 && $requestedaction eq "" ) { - print "Status: Not yet flipped "; - print ""; - print "[flip it]"; - print ""; + $template->param(STATUS_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(STATUS_JUSTFLIPPED => 1); + $template->param(COINRESULT => translateresult($result)); } elsif ( $coininfosize == 3 ) { - print "Status: Flipped @ " . localtime($coininfo[1]); - print "
"; - print "Result: "; - printresult($coininfo[2]); + $template->param(STATUS_FLIPPED => 1); + $template->param(COINFLIPPEDTIME => localtime($coininfo[1]).""); + $template->param(COINRESULT => translateresult($coininfo[2])); } - - print "
"; } -print ""; -print ""; +printpage(); untie(%coins_db);