8 use POSIX qw
/strftime/;
12 $ENV{HOME
} = "/home/pasky/WWW/asr";
17 $this->{startup
} = Time
::HiRes
::time();
19 $this->{cgi
} = new CGI
;
21 winportion
=> 3, # how big portion of score delta are win points
22 winfloor
=> 2, # minimal number of win points
23 dailydrop
=> 0.5, # daily global point drop
30 return $_[0]->{dbi
} if exists $_[0]->{dbi
};
32 open MYCNF
, "$ENV{HOME}/.my.cnf";
34 my $contents = <MYCNF
>;
36 my ($user, $database, $password);
37 $user = $1 if $contents =~ /user = (.*)/;
38 $database = $1 if $contents =~ /database = (.*)/;
39 $password = $1 if $contents =~ /password = (.*)/;
40 $_[0]->{adminpw
} = $1 if $contents =~ /adminpw = (.*)/;
42 if (!$user || !$database || !$password || !$_[0]->{adminpw
}) {
43 &die_clean_fatal
("Sorry, the .my.cnf file appears to be corrupt");
46 $_[0]->{dbi
} = DBI
->connect("dbi:mysql:database=$database", $user, $password);
47 $_[0]->{password
} = $password;
50 $_[0]->die_fatal_db("Sorry, I can't seem to connect to the database.");
57 $_[0]->header("Argh") unless $_[0]->{header
};
58 print $_[0]->h2("Fatal error: $_[1]");
59 print $_[0]->h3("$_[2]");
65 $_[0]->die_fatal($_[1], "Database says: ".DBI
->errstr);
68 sub die_fatal_permissions
{
69 $_[0]->die_fatal($_[1], "You don't have permission to do that!");
72 sub die_fatal_badinput
{
73 $_[0]->die_fatal($_[1], "Your input incomplete/invalid.");
78 &die_clean_fatal
("Header output twice?!") if $this->{header
};
82 print $cgi->start_html(-title
=> "ASR Ladder", -style
=> "style.css");
83 print $cgi->h1("ASR Ladder BETA");
85 #print qq<<p class=nav><a href="?mode=index$period">Summary</a> | <a href="?mode=stats$period">Stats</a> | <a href="?mode=brawl$period">Brawl</a> | <a href="?mode=votes">Votes</a></p>>;
87 $_[1] and print $cgi->h2($_[1]);
93 print $_[0]{cgi
}->h2("Failed");
94 print $_[0]{cgi
}->p($_[1]);
98 print $_[0]{cgi
}->h2("Success");
99 print $_[0]{cgi
}->p($_[1]);
104 my $cgi = $this->{cgi
};
105 my $time = Time
::HiRes
::time() - $this->{startup
};
106 #print $cgi->p("Request took $time secs, made $this->{dbreqs} queries.");
107 print $cgi->end_html;
113 return $this->{dbi
}->do(@_);
119 return $this->{dbi
}->selectall_hashref(@_);
125 return $this->{dbi
}->selectrow_hashref(@_);
130 my $row = $this->{dbi
}->selectrow_arrayref(@_);
132 return undef unless $row;
138 return $this->db_selectone("SELECT LAST_INSERT_ID();");
143 my %vals = $this->{cgi
}->Vars();
144 delete $vals{$_} foreach (@_);
146 return '?'.(join '&', map { "$_=$vals{$_}" } keys %vals).'&';
153 return qq|<input type
="hidden" name
="$_[1]" value
="$_[2]"/>|;
158 return $this->{cgi
}->textarea(@_);
163 return $this->{cgi
}->textfield(@_);
167 return qq|<input type
="text" name
="$_[1]" value
="$_[2]" size
="$_[3]" maxlength
="$_[4]"/>|;
172 return $this->{cgi
}->password_field(@_);
177 return $this->{cgi
}->param(@_);
182 return $this->{cgi
}->h1(@_);
187 return $this->{cgi
}->h2(@_);
192 return $this->{cgi
}->h3(@_);
197 return $this->{cgi
}->h4(@_);
202 return $this->{cgi
}->p(@_);
207 return $this->{cgi
}->submit(@_);
212 return $this->{cgi
}->end_form(@_);
217 return $this->{cgi
}->escapeHTML(@_);
222 if (shift eq 'get') {
223 print $this->{cgi
}->start_form(-method
=> 'GET', -action
=> '?');
225 print $this->{cgi
}->start_form(-method
=> 'POST', -action
=> 'index.pl');
227 while (my $name = shift) {
229 print $this->hidden($name, $val);
236 print "<li>$_</li>" foreach(@_);
242 my ($id, $nick) = @_;
243 return "<a href=\"player.pl?pid=$id\" class=\"playerlink\">$nick</a>";
246 sub recompute_score
{
248 my ($winner, $loser) = @_;
250 # Fine-tunable scoring constants
251 my $gameval = 1; # points per completely even game
253 my $delta = 0.0 + $loser - $winner;
254 my $extra = $delta / $this->{scoring
}->{winportion
};
255 ($extra > $this->{scoring
}->{winfloor
}) or $extra = $this->{scoring
}->{winfloor
};
256 return $winner + $extra;