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