#!/usr/bin/perl 

# dailystats_wap3.pl
# NDC Code Release 1
#
# Connects to the server holding the stats, and builds a web page
# of yesterdays players
# CGI-Parameters:
#       start:          First entry displayed
#       limit:          Number of entries. Defaults to 10.
#
#   Copyright (C) 2001 Andreas Ulbrich, Ralf Ertzinger (ndccode@ndc.sh)
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#use strict;
require 5.002;
use DBI;
use CGI;

my $CGIquery;

### Var def ###
$CGIquery = new CGI;
$start = ($CGIquery->param("start")?$CGIquery->param("start"):0);
$limit = ($CGIquery->param("limit")?$CGIquery->param("limit"):10);

# Rebuild database, if necessary

$database = "stats";
$data_source = "DBI:mysql:$database";
$username = "";
$password = "";
$dbh = DBI->connect( $data_source, $username, $password) or die "Can't connect to $data_source\n"; #: $dbh->errstr\n";

$query = $dbh->prepare("SELECT nick FROM PlayerStats WHERE mapsplayed > 0");
$query->execute;
$rows = $query->rows;
$query->finish;

$query = $dbh->prepare("SELECT nick ,skill FROM PlayerStats WHERE mapsplayed > 0 ORDER by skill DESC LIMIT $start,$limit");
$query->execute;

while ($row_hash = $query->fetchrow_hashref) {
	$name = $row_hash->{nick};
	$nick{$name} = $name;
	$skill{$name} = $row_hash->{skill};
}
$query->finish;
$dbh->disconnect;

# Sort
@sortkey = sort { $skill{$b} <=> $skill{$a} } (keys %nick);

print "Content-Type: text/vnd.wap.wml\n\n";

## Ausgabe ##
### Header ###
print '<?xml version="1.0"?>' . "\n"; 
print '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">' . "\n\n"; 

print "<wml>\n";
print "<card id=\"card1\" title=\"Gestern\">\n";

if (($start + 10) < $rows) {
  $start_next=$start + 10;
  print "<do type=\"accept\" label=\"Weiter\">\n";
  print "<go href=\"\/cgi-bin\/quake\/dailystats_wap.pl\?start=$start_next\"\/>\n";
  print "<\/do>\n";
}

print "<p>\n";

print "<small>\n";
print "<table columns=\"3\">\n";
print "<tr><td>P.<\/td><td>Name<\/td><td>Skill<\/td><\/tr>\n";

$count = $start;
foreach $name (@sortkey)
{
       $count ++;
       print "<tr>\n";
       print "<td>$count<\/td>\n";
       print "<td>";
       print make_html($nick{$name});
       print "<\/td>\n";
       print "<td>$skill{$name}<\/td>\n";
       print "<\/tr>\n";
}

print "<\/table></small>\n";
print "<\/p>\n";
print "</card>\n";
print '</wml>';


############### Subroutines ###########

sub make_html {

  $input = shift;
#  $input =~ s/&/und/g;
  $input =~ s/</&lt\;/g;
  $input =~ s/>/&gt\;/g;
  $input =~ s/"/&quot\;/g;
#  $input =~ s/\|/pipe/g;
  return $input;
}

sub make_compatible {

  $input = shift;
  $input =~ s/%/%25/g;
  $input =~ s/\+/%2B/g;
  $input =~ s/ /+/g;
  $input =~ s/,/%2C/g;
  $input =~ s/</%3C/g;
  $input =~ s/>/%3E/g;
  $input =~ s/#/%23/g;
  $input =~ s/\[/%5B/g;
  $input =~ s/\]/%5D/g;
  $input =~ s/\//%2F/g;
  $input =~ s/\|/%7C/g;
  $input =~ s/\?/%3F/g;

  return $input;
}
