11 my $requestedcoinid = $cgi->param('coinid');
12 my $requestedaction = $cgi->param('action');
14 my $flippedcoinsindb=0;
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;
26 $template = HTML::Template->new(filename => 'alert.html');
27 $template->param(ALERTMSG => @_[0]);
41 if (-e "var/coins.db") {
42 tie (%coins_db, DB_File, "var/coins.db") ||
43 die ("Cannot open var/coins.db");
46 tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
47 die ("Cannot create or open var/coins.db");
50 foreach my $key ( keys %coins_db )
52 @value = split(/,/, $coins_db{$key});
53 if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
54 delete $coins_db{$key};
58 if ( ($#value + 1) == 3 ) {
63 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
64 exitalert("You sent me an invalid coin!");
67 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
68 exitalert("You sent me an invalid action!");
71 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
72 $template = HTML::Template->new(filename => 'create.html');
74 elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
75 my @chars = ("a".."z", "0".."9");
78 $newcoinid .= $chars[rand @chars] for 1..13;
79 while ( exists $coins_db{$newcoinid} ) {
81 $newcoinid .= $chars[rand @chars] for 1..13;
84 $coins_db{$newcoinid} = time;
86 $template = HTML::Template->new(filename => 'created.html');
87 $template->param(NEWCOINID => $newcoinid);
88 $template->param(HTTP_HOST => $ENV{HTTP_HOST});
90 elsif ( length($requestedcoinid) > 0 ) {
91 if ( ! exists $coins_db{$requestedcoinid} ) {
92 exitalert("The coinid $requestedcoinid does not exist!");
95 my @coininfo = split(/,/, $coins_db{$requestedcoinid});
96 my $coininfosize = $#coininfo + 1;
98 $template = HTML::Template->new(filename => 'usecoin.html');
100 $template->param(REQUESTEDCOINID => $requestedcoinid);
101 $template->param(HTTP_HOST => $ENV{HTTP_HOST});
102 $template->param(COINCREATIONTIME => localtime($coininfo[0])."");
104 if ( $coininfosize == 1 && $requestedaction eq "" ) {
105 $template->param(NOTYETFLIPPED => 1);
107 elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
108 $result = int(rand(2));
109 $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
112 $template->param(COINSTATUS => "Just flipped");
113 $template->param(COINRESULT => translateresult($result));
115 elsif ( $coininfosize == 3 ) {
116 $template->param(COINSTATUS => "Flipped @ " . localtime($coininfo[1]));
117 $template->param(COINRESULT => translateresult($coininfo[2]));