From: Alexander Schmidt Date: Sun, 9 Mar 2014 16:38:06 +0000 (+0100) Subject: Separated functional perl code and html using perl html template module. X-Git-Url: https://git.treefish.org/wirbrennen/flipacoin.git/commitdiff_plain/3b3d3fe4ea7feca2525038b4f44cb1f0b6b7b37c Separated functional perl code and html using perl html template module. --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..186857b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +var 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 "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. | Browse code and send patches to patchme\@flipacoin.org."; -print "
"; -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 +83,42 @@ 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); + $template->param(HTTP_HOST => $ENV{HTTP_HOST}); } 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(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 "
"; } -print ""; -print ""; +printpage(); untie(%coins_db); diff --git a/templates/.gitignore b/templates/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/templates/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/templates/_head.html b/templates/_head.html new file mode 100644 index 0000000..e9a7693 --- /dev/null +++ b/templates/_head.html @@ -0,0 +1,12 @@ + + During the last days + / coins were flipped/created. + | Browse + + code + and send patches to + + patchme@flipacoin.org. + +
+

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

diff --git a/templates/_header.html b/templates/_header.html new file mode 100644 index 0000000..32406f3 --- /dev/null +++ b/templates/_header.html @@ -0,0 +1,3 @@ +
+ flipacoin.org +
diff --git a/templates/alert.html b/templates/alert.html new file mode 100644 index 0000000..190c234 --- /dev/null +++ b/templates/alert.html @@ -0,0 +1,8 @@ + + + + + + ERROR: + + diff --git a/templates/create.html b/templates/create.html new file mode 100644 index 0000000..88f7d24 --- /dev/null +++ b/templates/create.html @@ -0,0 +1,9 @@ + + + + + + [Create a coin] + to make a decision. + + diff --git a/templates/created.html b/templates/created.html new file mode 100644 index 0000000..d5c4fc7 --- /dev/null +++ b/templates/created.html @@ -0,0 +1,15 @@ + + + + + + Created a new coin + + > + + http:/// + + for you.

+ Send the coin to the other party and tell them to flip it to make a decision. + + diff --git a/templates/usecoin.html b/templates/usecoin.html new file mode 100644 index 0000000..ae6194b --- /dev/null +++ b/templates/usecoin.html @@ -0,0 +1,26 @@ + + + + + + > + + http:/// + +
+ + +
+Creation time: 
+Status: Not yet flipped ?action=flip>[flip it]
+      
+ +
+Creation time: 
+Status: 
+Result: 
+      
+ + + +