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