Initial checkin
[ndccode.git] / stats / foreverstats2.pl
1 #!/usr/bin/perl 
2
3 # foreverstats2.pl
4 # NDC Code Release 1
5 #
6 # Connects to the database holding the statistic data and builds
7 # a web page from it.
8 # This is old code. The HTML is inlined in it. Hard to read,
9 # almost impossible to maintain.
10 # CGI-Parameters:
11 #       start:          First entry displayed
12 #       limit:          Number of entries. Defaults to 20.
13 #       sort:           Sort mode.
14 #
15 #   Copyright (C) 2001 Andreas Ulbrich, Ralf Ertzinger (ndccode@ndc.sh)
16 #
17 #   This program is free software; you can redistribute it and/or modify
18 #   it under the terms of the GNU General Public License as published by
19 #   the Free Software Foundation; either version 2 of the License, or
20 #   (at your option) any later version.
21 #
22 #   This program is distributed in the hope that it will be useful,
23 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
24 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 #   GNU General Public License for more details.
26 #
27 #   You should have received a copy of the GNU General Public License
28 #   along with this program; if not, write to the Free Software
29 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
31 #use strict;
32 require 5.002;
33 use DBI;
34 use CGI;
35
36 my $CGIquery;
37
38 ### Var def ###
39 $CGIquery = new CGI;
40 $sort = ($CGIquery->param("sort")?$CGIquery->param("sort"):'0');
41 $limit = ($CGIquery->param("limit")?$CGIquery->param("limit"):'20');
42 $start = ($CGIquery->param("start")?$CGIquery->param("start"):'0');
43 $back = ($CGIquery->param("back")?$CGIquery->param("back"):'0');
44
45 # Rebuild database, if necessary
46 open (FIRST, "< first.run") || die "Can't open first.run\n";
47 while (<FIRST>) {
48         if ($_ == 1) {
49                 close (FIRST);
50                 open (FIRST, "> first.run") || die "Can't open first.run\n";
51                 print FIRST "0\n";
52                 close (FIRST);
53                 {$update = `databasectrl.sh`}
54         }
55 }
56 close (FIRST);
57
58 $database = "stats";
59 $data_source = "DBI:mysql:$database";
60 $username = "";
61 $password = "";
62 $dbh = DBI->connect( $data_source, $username, $password) or die "Can't connect to $data_source\n"; #: $dbh->errstr\n";
63
64 $query = $dbh->prepare("SELECT nick FROM PlayerStats WHERE fe_skill > 0");
65 $query->execute;
66 $rows = $query->rows;
67 $query->finish;
68
69
70 $query = $dbh->prepare("SELECT nick FROM PlayerStats");
71 $query->execute;
72 $rowsall = $query->rows;
73 $query->finish;
74
75
76
77 $query = $dbh->prepare("SELECT nick ,fe_skill, fe_kills, fe_victims, fe_suicides, fe_mapsplayed, fe_mapswon, fe_time, fe_fph, date_format(LastSeen, '%d.%m.%Y') as date_europe FROM PlayerStats WHERE fe_skill > 0 ORDER by fe_skill DESC LIMIT $start,$limit");
78 $query->execute;
79
80 while ($row_hash = $query->fetchrow_hashref) {
81         $name = $row_hash->{nick};
82         $nick{$name} = $name;
83         $skill{$name} = $row_hash->{fe_skill};
84         $kills{$name} = $row_hash->{fe_kills};
85         $victims{$name} = $row_hash->{fe_victims};
86         $suicides{$name} = $row_hash->{fe_suicides};
87         $maps{$name} = $row_hash->{fe_mapsplayed};
88         $wonmaps{$name} = $row_hash->{fe_mapswon};
89         $time{$name} = $row_hash->{fe_time};
90         $fph{$name} = $row_hash->{fe_fph};
91         $lastseen{$name} = $row_hash->{date_europe};
92         unless(defined($lastseen{$name})) { $lastseen{$name} = '--.--.--'; }
93 }
94 $query->finish;
95 $dbh->disconnect;
96
97
98
99 print "Content-Type: text/html\n\n\n";
100
101
102 ###### Sortieren #####
103
104 # By default, sort by rank
105
106 if ($sort eq "0") {
107   @sortkey = sort { $skill{$b} <=> $skill{$a} } (keys %nick);
108   $sortname = "Skill";
109   $darstellung = "\= Kills \/ \( 1 \+ Victims \+ Suicides \) \* \( 1 \+ \( Maps-won \/ Maps-played \)";
110 }
111
112
113
114 if ($sort eq "1") {
115   @sortkey = sort { $nick{$a} cmp $nick{$b} } (keys %nick);
116   $sortname = "Nick";
117   $darstellung = "";
118 }
119
120 if ($sort eq "2") {
121   @sortkey = sort { $maps{$b} <=> $maps{$a} } (keys %maps);
122   $sortname = "gespielten Maps";
123   $darstellung = "";
124 }
125
126 if ($sort eq "3") {
127   @sortkey = sort { $kills{$b} <=> $kills{$a} } (keys %kills);
128   $sortname = "Kills";
129   $darstellung = "";
130 }
131
132 if ($sort eq "4") {
133   @sortkey = sort { $victims{$b} <=> $victims{$a} } (keys %victims);
134   $sortname = "Victims";
135   $darstellung = "";
136 }
137
138 if ($sort eq "5") {
139   @sortkey = sort { $suicides{$b} <=> $suicides{$a} } (keys %suicides);
140   $sortname = "Suicides";
141   $darstellung = "";
142 }
143
144 if ($sort eq "6") {
145   @sortkey = sort { $fph{$b} <=> $fph{$a} } (keys %fph);
146   $sortname = "FPH";
147   $darstellung = "\= Kills \/ \(Sec \/ 3600 \)";
148 }
149
150 if ($sort eq "7") {
151   @sortkey = sort { $time{$b} <=> $time{$a} } (keys %nick);
152   $sortname = "Zeit";
153   $darstellung = "";
154 }
155
156 if ($sort eq "8") {
157   @sortkey = sort { $wonmaps{$b} <=> $wonmaps{$a} } (keys %nick);
158   $sortname = "gewonnenen Maps";
159   $darstellung = "";
160 }
161 ## Ausgabe ##
162 ### Header ###
163 print '<HTML><HEAD><TITLE>Cardassia - Hall of Fame</TITLE>';
164 print '<META NAME="Andreas Ulbrich \+ Ralf Ertzinger">';
165 print '</HEAD><BODY BGCOLOR="#FFFFFF">';
166 print "\n<CENTER><img SRC=\"http\:\/\/www.ndc.sh\/graphics\/halloffame_logo.jpg\" WIDTH=\"500\" HEIGHT=\"90\" BORDER=\"0\"ALT=\"\[SERVER Hall of Fame\]\"><\/CENTER>\n";
167
168 print "<CENTER><b>Erfasste Spieler gesamt: $rowsall <BR>Anzahl der Spieler mit mehr als 10 Maps: $rows<\/B><BR>\n";
169 if ($back == 0) {
170         print "<b><a href =\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=0\&limit\=$rows\&back\=1\">$rows Spieler anzeigen<\/A><\/B><\/CENTER><\/BR>\n";
171 }
172 else {
173         print "<b><a href =\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=0\&limit\=20\&back\=0\">Die ersten 20 anzeigen<\/A><\/B><\/CENTER><\/BR>\n";
174 }
175
176
177 ##### Sortiert nach sortkey ########
178
179 print "<P>\&nbsp\;<P><UL><b>Sortiert nach $sortname $darstellung<\/b>\n";
180 print "<table BORDER=\"0\">\n";
181 print "<tr>\n";
182 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b>Rang<\/b><\/td>
183 \n";
184 print "<td WIDTH=\"120\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=1\&limit=$limit\&back=$back\">Name<\/a><\/b><\/td>\n";
185 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=0\&limit=$limit\&back=$back\">Skill<\/a><\/b><\/td>\n";
186 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=6\&limit=$limit\&back=$back\">FPH<\/a><\/b><\/td>\n";
187 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=2\&limit=$limit\&back=$back\">Maps gespielt<\/a><\/b><\/td>\n";
188 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=8\&limit=$limit\&back=$back\">Maps gewonnen<\/a><\/b><\/td>\n";
189 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=3\&limit=$limit\&back=$back\">Kills<\/a><\/b><\/td>\n";
190 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=4\&limit=$limit\&back=$back\">Victims<\/a><\/b><\/td>\n";
191 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=5\&limit=$limit\&back=$back\">Suicides<\/a><\/b><\/td>\n";
192 print "<td WIDTH=\"50\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b><a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort\=7\&limit=$limit\&back=$back\">Zeit (Min)<\/a><\/b><\/td>\n";
193 print "<td WIDTH=\"100\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b>Zuletzt gespielt<\/b><\/td>\n";
194 print "<\/tr>\n";
195
196 $count = $start;
197 foreach $name (@sortkey)
198 {
199         
200        $count ++;
201        print "<tr>\n";
202        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"+0\">$count<\/font><\/td>\n";
203        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#CCCCCC\"><font size =\"+0\">";
204        print "<a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/playerstats.pl\?nick\=";
205        print make_compatible($nick{$name});
206        print "\">";
207        print "<font color\=\"#000000\">";
208        print make_html($nick{$name});
209        print "<\/font>";
210        print "<\/a><\/font><\/td>\n";
211        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$skill{$name}<\/font><\/td>\n";
212        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$fph{$name}<\/font><\/td>\n";
213
214        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$maps{$name}<\/font><\/td>\n";
215        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$wonmaps{$name}<\/font><\/td>\n";
216        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$kills{$name}<\/font><\/td>\n";
217        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$victims{$name}<\/font><\/td>\n";
218        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$suicides{$name}<\/font><\/td>\n";
219        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$time{$name}<\/font><\/td>\n";
220        print "<td ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\"><font size =\"-1\">$lastseen{$name}<\/font><\/td>\n";
221        print "<\/tr>\n";
222 }
223
224 print "<\/TABLE><\/UL><P>\n";
225
226 if ($back == 0) {
227   print "<CENTER><TABLE BORDER=\"0\">\n";
228   print "<tr>\n";
229   print "<td WIDTH=\"200\" ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\">";
230   if ($start != 0) {
231     $start_prev = $start - $limit;
232     if ($start_prev < 0) {$start_prev = 0;}
233     print "<a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort=$sort&start=$start_prev&limit=$limit&back=0\">";
234     print "Zur&uuml;ck";
235     print "<\/a>\n";
236   } else {
237     print "Zur&uuml;ck";
238   }
239   print "<\/td>\n";
240   print "<td WIDTH=\"200\" ALIGN=\"CENTER\" BGCOLOR=\"\#EEEEEE\">";
241   if ($start + $limit < $rows) {
242     $start_next = $start + $limit;
243     print "<a href=\"http\:\/\/www.ndc.sh\/cgi-bin\/quake\/foreverstats2.pl\?sort=$sort&start=$start_next&limit=$limit&back=0\">";
244     print "Weiter";
245     print "<\/a>\n";
246   } else {
247     print "Weiter";
248   }
249   print "<\/td>\n";
250   print "<\/tr>\n";
251   print "<\/TABLE><\/CENTER>\n";
252 }
253
254 print "<P><FONT SIZE=\"-2\"><DIV ALIGN=right>Skripting by NDC Code<BR><A href=\"http://www.ndc.sh\" target=\"_top\">www.ndc.sh</A></DIV></FONT><BR>\n";
255 print '</BODY></HTML>';
256
257
258 ############### Subroutines ###########
259
260 sub make_html {
261
262   $input = shift;
263 #  $input =~ s/&/und/g;
264   $input =~ s/</&lt\;/g;
265   $input =~ s/>/&gt\;/g;
266   $input =~ s/"/&quot\;/g;
267   $input =~ s/\x1c/&middot;/g;
268 #  $input =~ s/\|/pipe/g;
269   return $input;
270 }
271
272 sub make_compatible {
273
274   $input = shift;
275   $input =~ s/%/%25/g;
276   $input =~ s/\+/%2B/g;
277   $input =~ s/ /+/g;
278   $input =~ s/,/%2C/g;
279   $input =~ s/</%3C/g;
280   $input =~ s/>/%3E/g;
281   $input =~ s/#/%23/g;
282   $input =~ s/\[/%5B/g;
283   $input =~ s/\]/%5D/g;
284   $input =~ s/\//%2F/g;
285   $input =~ s/\|/%7C/g;
286   $input =~ s/\?/%3F/g;
287   $input =~ s/\x1c/%1C/g;
288
289   return $input;
290 }