#!/usr/bin/perl 

# playerstats.pl
# NDC Code Release 1
# CGI-Parameters:
#	nick:	Player name
#
# Displays a web page showing the stats for one player, displaying
# a picture, mail address and web page.
#
#   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;
$picturepath = 'http://www.ndc.sh/graphics/playerdb/';
$nick = $CGIquery->param("nick");

# Define SQL-Variables

$sql_database = "stats";
$sql_datasource = "DBI:mysql:$sql_database";
$sql_username = "";
$sql_password = "";
$sql_db_handle = DBI->connect($sql_datasource, $sql_username, $sql_password) or die "Could not connect to database.\n";


print "Content-Type: text/html\n\n\n";

# Retrieve player data from SQL

$sql_query_string = "SELECT nick, email, picture, homepage, kills, victims, suicides, mapsplayed, mapswon, FPH, skill, time, skill_top, fe_skill, fe_kills, fe_victims, fe_suicides, fe_mapsplayed, fe_mapswon, fe_fph, fe_time, fe_rank, date_format(LastSeen, '%d.%m.%Y') as date_europe FROM PlayerStats WHERE nick=?";
$sql_query = $sql_db_handle->prepare($sql_query_string);
$sql_query->execute($nick);

$sql_row = $sql_query->fetchrow_hashref;

$pl_name = $sql_row->{nick};
$pl_email = $sql_row->{email};
$pl_picture = $picturepath . $sql_row->{picture};
$pl_homepage = $sql_row->{homepage};
$pl_kills = $sql_row->{kills};
$pl_victims = $sql_row->{victims};
$pl_suicides = $sql_row->{suicides};
$pl_fph = $sql_row->{FPH};
$pl_skill = $sql_row->{skill};
$pl_time = $sql_row->{time};
$pl_maps_played = $sql_row->{mapsplayed};
$pl_maps_won = $sql_row->{mapswon};
$pl_fe_kills = $sql_row->{fe_kills};
$pl_fe_victims = $sql_row->{fe_victims};
$pl_fe_suicides = $sql_row->{fe_suicides};
$pl_fe_maps_played= $sql_row->{fe_mapsplayed};
$pl_fe_maps_won = $sql_row->{fe_mapswon};
$pl_fe_skill = $sql_row->{fe_skill};
$pl_fe_fph = $sql_row->{fe_fph};
$pl_fe_time = $sql_row->{fe_time};
$pl_skill_top = $sql_row->{skill_top};
$pl_fe_rank = $sql_row->{fe_rank};
$pl_lastseen = $sql_row->{date_europe};

$sql_query->finish;

$sql_db_handle->disconnect;

## Ausgabe ##
### Header ###
print '<HTML><HEAD><TITLE>NDC Spielerdaten</TITLE>';
print '<META NAME="Andreas Ulbrich">';
print '</HEAD><BODY BGCOLOR="#FFFFFF">';
print "\n<CENTER><img SRC=\"http:\/\/www.ndc.sh\/graphics\/aktuell_logo.jpg\" WIDTH=\"300\" HEIGHT=\"90\" BORDER=\"0\"ALT=\"\[SERVER Status\]\"><\/CENTER>\n";
print "\&nbsp\;<P>\n";
if (defined($eingabe{showquery})) {
  print "<pre>\n";
  print "SQL-Query:\n";
  print "$sql_query_string\n";
  print "</pre>";
}
print "<UL><table BORDER=\"0\">\n";
print "<tr>\n";
print "<td WIDTH=\"270\" ALIGN=\"CENTER\" BGCOLOR=\"\#FFFFCC\"><b>Spielerdaten<\/b><\/td>\n";
print "<\/TABLE><\/UL>\n";

if (defined($pl_name)) {
  print "<UL><table BORDER=\"0\">\n";
  print "<tr>\n";
  print "<td ROWSPAN=\"10\" VALIGN=\"TOP\"><img src \=\"$pl_picture\" WIDTH=\"186\" HEIGHT=\"300\" BORDER=\"0\"><\/td>";
  print "<td WIDTH=\"120\" BGCOLOR=\"\#CCCCCC\">Nickname:<\/td>";
  print "<td WIDTH=\"200\" BGCOLOR=\"\#EEEEEE\"><b>" . make_html($pl_name) . "<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">email:<\/td>";
  if ($pl_email ne "") {
    print "<td BGCOLOR=\"#EEEEEE\"><b><a href=\"mailto:$pl_email\">$pl_email<\/a><\/b><\/td>\n";
  } else {
    print "<td BGCOLOR=\"#EEEEEE\"><b>-<\/b><\/td>\n";
  }
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Homepage:<\/td>";
  if ($pl_homepage ne "") {
    print "<td BGCOLOR=\"#EEEEEE\"><b><a href=\"$pl_homepage\" TARGET=\"xxyyzz\">$pl_homepage<\/a><\/b><\/td>\n";
  } else {
    print "<td BGCOLOR=\"#EEEEEE\"><b>-<\/b><\/td>\n";
  }
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Kills:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_fe_kills<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Victims:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_fe_victims<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Suicides:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_fe_suicides<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Skill:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_fe_skill<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">H&ouml;chster Skill:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_skill_top<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Rang:<\/td>";
  if ($pl_fe_rank != 0) {
    print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_fe_rank<\/b><\/td>\n";
  } else {
    print "<td BGCOLOR=\"#EEEEEE\"><b>-<\/b><\/td>\n";
  }
  print "<\/tr>\n";

  print "<tr>\n";
  print "<td BGCOLOR=\"#CCCCCC\">Zuletzt gespielt:<\/td>";
  print "<td BGCOLOR=\"#EEEEEE\"><b>$pl_lastseen<\/b><\/td>\n";
  print "<\/tr>\n";

  print "<\/TABLE><\/UL>\n";

  print "<P>&nbsp;<P>\n";
  print "<UL><b>Gestern<\/b><br>\n";
  print "<TABLE border=\"0\">\n";
  print "<tr>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"100\">Map<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Skill<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">FPH<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Maps gespielt<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Maps gewonnen<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Kills<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Victims<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Suicides<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Zeit (Min)<\/td>\n";
  print "<\/tr>\n";

  if ($pl_maps_played > 0) {

    print "<tr>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#CCCCCC\">Total<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_skill<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fph<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_maps_played<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_maps_won<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_kills<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_victims<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_suicides<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_time<\/td>\n";
    print "<\/tr>\n";

  } else {

    print "<tr>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#CCCCCC\">Total<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">-<\/td>\n";
    print "<\/tr>\n";

  }

  print "<\/TABLE><\/UL>\n";


  print "<P>&nbsp;<P>\n";
  print "<UL><b>Gesamt<\/b><br>\n";
  print "<TABLE border=\"0\">\n";
  print "<tr>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"100\">Map<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Skill<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">FPH<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Maps gespielt<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Maps gewonnen<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Kills<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Victims<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Suicides<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#FFFFCC\" WIDTH=\"50\">Zeit (Min)<\/td>\n";
  print "<\/tr>\n";


  print "<tr>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#CCCCCC\">Total<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_skill<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_fph<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_maps_played<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_maps_won<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_kills<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_victims<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_suicides<\/td>\n";
  print "<td ALIGN=\"CENTER\" BGCOLOR=\"#EEEEEE\">$pl_fe_time<\/td>\n";
  print "<\/tr>\n";

  print "<\/TABLE><\/UL>\n";

  print "<P>&nbsp;<P>\n";
  print "<UL>Suche bei <a href=\"http\:\/\/www.theclq.com\/asp\/find.asp\?name=";
  print make_compatible($pl_name);
  print "\" TARGET=\"_blank\">The CLQ<\/a><\/ul>\n";
  

} else {

  print "<ul>Es wurde kein Spieler dieses Namens in der Datenbank gefunden.</ul>\n";

}
  
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";

print '</BODY></HTML>';


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

sub make_html {

  $input = shift;
#  $input =~ s/&/und/g;
  $input =~ s/</&lt\;/g;
  $input =~ s/>/&gt\;/g;
  $input =~ s/"/&quot\;/g;
  $input =~ s/\x1c/&middot;/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;
  $input =~ s/\x1c/%1C/g;

  return $input;
}

