]> git.treefish.org Git - wirbrennen/flipacoin.git/commitdiff
Separated functional perl code and html using perl html template module.
authorAlexander Schmidt <alex@treefish.org>
Sun, 9 Mar 2014 16:38:06 +0000 (17:38 +0100)
committerAlexander Schmidt <alex@treefish.org>
Sun, 9 Mar 2014 16:38:06 +0000 (17:38 +0100)
.gitignore [new file with mode: 0644]
flipacoin.cgi
templates/.gitignore [new file with mode: 0644]
templates/_head.html [new file with mode: 0644]
templates/_header.html [new file with mode: 0644]
templates/alert.html [new file with mode: 0644]
templates/create.html [new file with mode: 0644]
templates/created.html [new file with mode: 0644]
templates/usecoin.html [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..186857b
--- /dev/null
@@ -0,0 +1 @@
+var
index 772b90e7c3d905b96355fffbc2d30e0b549d6c93..95bf6c1761af28a8e3c1d4cd2dc36d32801f68fe 100755 (executable)
@@ -2,6 +2,7 @@
 
 use DB_File;
 use CGI;
 
 use DB_File;
 use CGI;
+use HTML::Template;
 
 my $COINLIFETIME = 30;
 
 
 my $COINLIFETIME = 30;
 
@@ -12,24 +13,28 @@ my $requestedaction = $cgi->param('action');
 my $coinsindb=0;
 my $flippedcoinsindb=0;
 
 my $coinsindb=0;
 my $flippedcoinsindb=0;
 
-sub alert {
-    print "<b>ERROR:</b> @_[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 "<a href=/@_[0]>";
-    print "<font style=\"BACKGROUND-COLOR: yellow\" color=blue><b>";
-    print "http://$ENV{HTTP_HOST}/@_[0]";
-    print "</b></font>";
-    print "</a>";
+sub exitalert {
+    $template = HTML::Template->new(filename => 'alert.html');
+    $template->param(ALERTMSG => @_[0]);
+    printpage();
+    exit();
 }
 
 }
 
-sub printresult {
+sub translateresult {
     if ( @_[0] == 0 ) {
     if ( @_[0] == 0 ) {
-       print "Heads";
+       return "Heads";
     }
     else {
     }
     else {
-       print "Tails";
+       return "Tails";
     }
 }
 
     }
 }
 
@@ -55,34 +60,21 @@ foreach my $key ( keys %coins_db )
     }
 }
 
     }
 }
 
-print "Content-type: text/html\n\n";
-print "<html>";
-print "<head>";
-print "<title>flipacoin.org - The Internet's first coinflipping service.</title>";
-print "</head>";
-print "<body>";
-
 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
 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/) ) {
 }
 
 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
-    alert("You sent me an invalid action!");
-    exit;
+    exitalert("You sent me an invalid action!");
 }
 
 }
 
-print "<font size=1>During the last $COINLIFETIME days $flippedcoinsindb/$coinsindb coins were flipped/created. | Browse <a href='http://git.treefish.org/wirbrennen/flipacoin.git' target='_blank'>code</a> and send patches to <a href='mailto:patchme\@flipacoin.org'>patchme\@flipacoin.org</a>.</font>";
-print "<hr>";
-print "<h3>flipacoin.org - The Internet's first coinflipping service.</h3>";
-
 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
-    print "<a href=/?action=create><font color=blue>[Create a coin]</font></a>";
-    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;
 }
 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 = "";
     $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;
 
 
     $coins_db{$newcoinid} = time;
 
-    print "Created a new coin ";
-    printcoin($newcoinid);
-    print " for you.<br>";
-    print "<br>";
-    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} ) {
 }
 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;
 
     }
 
     my @coininfo = split(/,/, $coins_db{$requestedcoinid});
     my $coininfosize = $#coininfo + 1;
 
-    printcoin($requestedcoinid);
-    print "<br>";
-    print "<pre>";
-    print "<b>Creation time:</b> " . localtime($coininfo[0]);
-    print "<br>";
+    $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 "" ) {
 
     if ( $coininfosize == 1 && $requestedaction eq "" ) {
-       print "<b>Status:</b> Not yet flipped ";
-       print "<a href=/$requestedcoinid?action=flip>";
-       print "<font color=blue>[flip it]</font>";
-       print "</a>";
+       $template->param(NOTYETFLIPPED => 1);
     }
     elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
        $result = int(rand(2));
        $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
            "," . time .
            "," . $result;
     }
     elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
        $result = int(rand(2));
        $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
            "," . time .
            "," . $result;
-       print "<b>Status:</b> Just flipped";
-       print "<br>";
-       print "<b>Result:</b> ";
-       printresult($result);
+       $template->param(COINSTATUS => "Just flipped");
+       $template->param(COINRESULT => translateresult($result));
     }
     elsif ( $coininfosize == 3 ) {
     }
     elsif ( $coininfosize == 3 ) {
-       print "<b>Status:</b> Flipped @ " . localtime($coininfo[1]);
-       print "<br>";
-       print "<b>Result:</b> ";
-       printresult($coininfo[2]);
+       $template->param(COINSTATUS => "Flipped @ " . localtime($coininfo[1]));
+       $template->param(COINRESULT => translateresult($coininfo[2]));
     }
     }
-
-    print "</pre>";
 }
 
 }
 
-print "</body>";
-print "</html>";
+printpage();
 
 untie(%coins_db);
 
 
 untie(%coins_db);
 
diff --git a/templates/.gitignore b/templates/.gitignore
new file mode 100644 (file)
index 0000000..b25c15b
--- /dev/null
@@ -0,0 +1 @@
+*~
diff --git a/templates/_head.html b/templates/_head.html
new file mode 100644 (file)
index 0000000..e9a7693
--- /dev/null
@@ -0,0 +1,12 @@
+<font size=1>
+  During the last <!-- TMPL_VAR NAME=COINLIFETIME --> days 
+  <!-- TMPL_VAR NAME=FLIPPEDCOINSINDB -->/<!-- TMPL_VAR NAME=COINSINDB --> coins were flipped/created. 
+  | Browse 
+  <a href='http://git.treefish.org/wirbrennen/flipacoin.git' target='_blank'>
+    code</a> 
+  and send patches to 
+  <a href='mailto:patchme@flipacoin.org'>
+    patchme@flipacoin.org</a>.
+</font>
+<hr>
+<h3>flipacoin.org - The Internet's first coinflipping service.</h3>
diff --git a/templates/_header.html b/templates/_header.html
new file mode 100644 (file)
index 0000000..32406f3
--- /dev/null
@@ -0,0 +1,3 @@
+<header>
+  <title>flipacoin.org</title>
+</header>
diff --git a/templates/alert.html b/templates/alert.html
new file mode 100644 (file)
index 0000000..190c234
--- /dev/null
@@ -0,0 +1,8 @@
+<html>
+  <!-- TMPL_INCLUDE NAME=_header.html -->
+  <body>
+    <!-- TMPL_INCLUDE NAME=_head.html -->
+    
+    <b>ERROR:</b> <!-- TMPL_VAR NAME=ALERTMSG -->
+  </body>
+</html>
diff --git a/templates/create.html b/templates/create.html
new file mode 100644 (file)
index 0000000..88f7d24
--- /dev/null
@@ -0,0 +1,9 @@
+<html>
+  <!-- TMPL_INCLUDE NAME=_header.html -->
+  <body>
+    <!-- TMPL_INCLUDE NAME=_head.html -->
+
+    <a href=/?action=create><font color=blue>[Create a coin]</font></a>
+    to make a decision.
+  </body>
+</html>
diff --git a/templates/created.html b/templates/created.html
new file mode 100644 (file)
index 0000000..d5c4fc7
--- /dev/null
@@ -0,0 +1,15 @@
+<html>
+  <!-- TMPL_INCLUDE NAME=_header.html -->
+  <body>
+    <!-- TMPL_INCLUDE NAME=_head.html -->
+
+    Created a new coin
+
+    <a href=/<!-- TMPL_VAR NAME=NEWCOINID -->>
+      <font style="BACKGROUND-COLOR: yellow" color=blue>
+       <b>http://<!-- TMPL_VAR NAME=HTTP_HOST -->/<!-- TMPL_VAR NAME=NEWCOINID --></b></font></a>
+    
+    for you.<br><br>
+    Send the coin to the other party and tell them to flip it to make a decision.
+  </body>
+</html>
diff --git a/templates/usecoin.html b/templates/usecoin.html
new file mode 100644 (file)
index 0000000..ae6194b
--- /dev/null
@@ -0,0 +1,26 @@
+<html>
+  <!-- TMPL_INCLUDE NAME=_header.html -->
+  <body>
+    <!-- TMPL_INCLUDE NAME=_head.html -->
+
+    <a href=/<!-- TMPL_VAR NAME=REQUESTEDCOINID -->>
+      <font style="BACKGROUND-COLOR: yellow" color=blue>
+       <b>http://<!-- TMPL_VAR NAME=HTTP_HOST -->/<!-- TMPL_VAR NAME=REQUESTEDCOINID --></b></font></a>
+    
+    <br>
+     
+    <!-- TMPL_IF NAME=NOTYETFLIPPED -->
+      <pre>
+<b>Creation time:</b> <!-- TMPL_VAR NAME=COINCREATIONTIME -->
+<b>Status:</b> Not yet flipped <a href=/<!-- TMPL_VAR NAME=REQUESTEDCOINID -->?action=flip><font color=blue>[flip it]</font></a>
+      </pre>
+    <!-- TMPL_ELSE -->
+      <pre>
+<b>Creation time:</b> <!-- TMPL_VAR NAME=COINCREATIONTIME -->
+<b>Status:</b> <!-- TMPL_VAR NAME=COINSTATUS -->
+<b>Result:</b> <!-- TMPL_VAR NAME=COINRESULT -->
+      </pre>
+    <!-- /TMPL_IF -->
+
+  </body>
+</html>