]> git.treefish.org Git - wirbrennen/flipacoin.git/blob - flipacoin.cgi
Passing state to usecoin.html.
[wirbrennen/flipacoin.git] / flipacoin.cgi
1 #!/usr/bin/perl
2
3 use DB_File;
4 use CGI;
5 use HTML::Template;
6
7 my $COINLIFETIME = 30;
8
9 my %coins_db;
10 my $cgi = CGI->new();
11 my $requestedcoinid = $cgi->param('coinid');
12 my $requestedaction = $cgi->param('action');
13 my $coinsindb=0;
14 my $flippedcoinsindb=0;
15
16 my $template;
17
18 sub printpage {
19     $template->param(COINLIFETIME => $COINLIFETIME);
20     $template->param(FLIPPEDCOINSINDB => $flippedcoinsindb);
21     $template->param(COINSINDB => $coinsindb);
22     print "Content-type: text/html\n\n", $template->output;
23 }
24
25 sub exitalert {
26     $template = HTML::Template->new(filename => 'alert.html');
27     $template->param(ALERTMSG => @_[0]);
28     printpage();
29     exit();
30 }
31
32 sub translateresult {
33     if ( @_[0] == 0 ) {
34         return "Heads";
35     }
36     else {
37         return "Tails";
38     }
39 }
40
41 if (-e "var/coins.db") {
42     tie (%coins_db, DB_File, "var/coins.db") ||
43         die ("Cannot open var/coins.db");
44 }
45 else {
46     tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
47         die ("Cannot create or open var/coins.db");
48 }
49
50 foreach my $key ( keys %coins_db )
51 {
52     @value = split(/,/, $coins_db{$key});
53     if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
54         delete $coins_db{$key};
55     }
56
57     $coinsindb++;
58     if ( ($#value + 1) == 3 ) {
59         $flippedcoinsindb++;
60     }
61 }
62
63 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
64     exitalert("You sent me an invalid coin!");
65 }
66
67 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
68     exitalert("You sent me an invalid action!");
69 }
70
71 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
72     $template = HTML::Template->new(filename => 'create.html');
73 }
74 elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
75     my @chars = ("a".."z", "0".."9");
76     my $newcoinid;
77
78     $newcoinid .= $chars[rand @chars] for 1..13;
79     while ( exists $coins_db{$newcoinid} ) {
80         $newcoinid = "";
81         $newcoinid .= $chars[rand @chars] for 1..13;
82     }
83
84     $coins_db{$newcoinid} = time;
85
86     $template = HTML::Template->new(filename => 'created.html');
87     $template->param(NEWCOINID => $newcoinid);
88     $template->param(HTTP_HOST => $ENV{HTTP_HOST});
89 }
90 elsif ( length($requestedcoinid) > 0 ) {
91     if ( ! exists $coins_db{$requestedcoinid} ) {
92         exitalert("The coinid $requestedcoinid does not exist!");
93     }
94
95     my @coininfo = split(/,/, $coins_db{$requestedcoinid});
96     my $coininfosize = $#coininfo + 1;
97
98     $template = HTML::Template->new(filename => 'usecoin.html');
99
100     $template->param(REQUESTEDCOINID => $requestedcoinid);
101     $template->param(HTTP_HOST => $ENV{HTTP_HOST});
102     $template->param(COINCREATIONTIME => localtime($coininfo[0])."");
103
104     if ( $coininfosize == 1 && $requestedaction eq "" ) {
105         $template->param(STATUS_NOTYETFLIPPED => 1);
106     }
107     elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
108         $result = int(rand(2));
109         $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
110             "," . time .
111             "," . $result;
112         $template->param(STATUS_JUSTFLIPPED => 1);
113         $template->param(COINRESULT => translateresult($result));
114     }
115     elsif ( $coininfosize == 3 ) {
116         $template->param(STATUS_FLIPPED => 1);
117         $template->param(COINFLIPPEDTIME => localtime($coininfo[1])."");
118         $template->param(COINRESULT => translateresult($coininfo[2]));
119     }
120 }
121
122 printpage();
123
124 untie(%coins_db);
125
126 exit;