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 $template->param(HTTP_HOST => $ENV{HTTP_HOST});
23 print "Content-type: text/html\n\n", $template->output;
27 $template = HTML::Template->new(filename => 'alert.html');
28 $template->param(ALERTMSG => @_[0]);
42 if (-e "var/coins.db") {
43 tie (%coins_db, DB_File, "var/coins.db") ||
44 die ("Cannot open var/coins.db");
47 tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
48 die ("Cannot create or open var/coins.db");
51 foreach my $key ( keys %coins_db )
53 @value = split(/,/, $coins_db{$key});
54 if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
55 delete $coins_db{$key};
59 if ( ($#value + 1) == 3 ) {
64 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
65 exitalert("You sent me an invalid coin!");
68 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
69 exitalert("You sent me an invalid action!");
72 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
73 $template = HTML::Template->new(filename => 'create.html');
75 elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
76 my @chars = ("a".."z", "0".."9");
79 $newcoinid .= $chars[rand @chars] for 1..13;
80 while ( exists $coins_db{$newcoinid} ) {
82 $newcoinid .= $chars[rand @chars] for 1..13;
85 $coins_db{$newcoinid} = time;
87 $template = HTML::Template->new(filename => 'created.html');
88 $template->param(NEWCOINID => $newcoinid);
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(COINCREATIONTIME => localtime($coininfo[0])."");
103 if ( $coininfosize == 1 && $requestedaction eq "" ) {
104 $template->param(STATUS_NOTYETFLIPPED => 1);
106 elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
107 $result = int(rand(2));
108 $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
111 $template->param(STATUS_JUSTFLIPPED => 1);
112 $template->param(COINRESULT => translateresult($result));
114 elsif ( $coininfosize == 3 ) {
115 $template->param(STATUS_FLIPPED => 1);
116 $template->param(COINFLIPPEDTIME => localtime($coininfo[1])."");
117 $template->param(COINRESULT => translateresult($coininfo[2]));