#!/usr/bin/perl 

# makerank.pl
# NDC Code Release 1
#
# Updates the rank field in the stats database.
#
#   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;

### Var def ###

$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 fe_skill > 0 ORDER by fe_skill DESC");
$query->execute;

$rank = 1;

while ($row_hash = $query->fetchrow_hashref) {
	$name = $row_hash->{nick};
	$nick{$name} = $name;
	$rank{$name} = $rank++;
}

$query->finish;

foreach $name (%nick) {

  $sql_query_string = "UPDATE PlayerStats SET fe_rank=? WHERE nick=?";
  $sql_query=$dbh->prepare($sql_query_string);
  $sql_query->execute($rank{$name},$nick{$name});
  $sql_query->finish;
}

$dbh->disconnect;

