#!/usr/bin/perl
# ================================================================================
# $Rev: 143 $ $URL: svn://localhost/PlayChess/trunk/cgi/playerstats-wins.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Author: $Author: Thomas $
# Modified: $Date: 2005-08-15 14:06:50 +0200 (Mo, 15 Aug 2005) $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright at playchess.de - all rights reserved
# ================================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use PCPlayer;
use PC;
use PCLeagueUtil;
use Annotate;
use Member;
use Template;
use Object;
use Util;
use ChessConfig;
use PCSession;
use MyDbi;
##--------------------------------------------------------------------------
## LOCAL CONFIGURATION
##--------------------------------------------------------------------------
local $config = getConfig();
$URL = $ENV{DOCUMENT_URI}; # url of your domain
$ROOT = $ENV{DOCUMENT_ROOT}; # path to document root
$SERVER = $ENV{SERVER_NAME}; # server name
$HOST = $ENV{HTTP_HOST};
$err = ''; # error message
$syserr = 0;
$min = 0;
$max = 0;
local ($starttime,$endtime) = (time(),0);
$cgi = CGI->new();
$sobj = PCSession->new($cgi);
$sname = $sobj->getValue( 'name' );
#$spasswd = $sobj->getValue( 'passwd' );
local $action = $cgi->param( 'action' ) || $ENV{'action'};
local $league = $cgi->param( 'league' ) || 'HCL';
local $name = $cgi->param( 'name' ) || $sname;
local $min = $cgi->param( 'min' );
local $admin = $cgi->param( 'admin' );
# ignore leading and trailing blanks
$name =~ s/^\s*(.*?)\s*$/$1/;
# Following actions require database access
#------------------------------------------
$dbh = dbiConnect() or exit(1);
# Win, draw and loss
#--------------------
#elsif( $action eq 's1' )
{
my( $stmt, $rows, @results );
my $starttime = time;
my $pid = getValue('select pid from tbl_player where name=?', $name );
my( $stmt, $sth, $total_games );
# Page requires membership
# ~~~~~~~~~~~~~~~~~~~~~~~~
$sobj->assertMember();
$stmt = "
select
IF(pg.color='w','White','Black'),
sum(g.result=pg.color),
sum(g.result='d'),
sum(g.result!='d' && g.result!=pg.color),
count(g.result),
max(pg.newelo),
max(IF(g.result=pg.color,pg.oppelo,0)),
min(IF(g.result!='d' && g.result!=pg.color,pg.oppelo,9999))
from
tbl_player_game pg,
tbl_game g
where pg.pid=?
and pg.gid=g.gid
and g.name like ?
and g.result is not null
and g.result != ''
and g.valid = '1'
group by 1
order by 1 desc
";
$sth = $dbh->prepare( $stmt );
my $tpl = Template->new( "playerstats-wl.tpl" );
foreach $league ('HCL', 'ACL', 'CCL')
{
$rows = getRows( $sth, $pid, "$league-%" );
#@Row = @{$row};
#print( __FILE__,__LINE__, join(' | ',$name,$rows,$#{$rows}), "
\n" );
# Fill results into template
#---------------------------
my( $r, @results, @ratings );
my( $tw, $td, $tl, $tg, $tpoints, $tpercent, $maxelo);
for $r ( 0 .. $#{$rows} )
{
my( $c, $w, $d, $l, $g, $max, $maxwin, $minloss) = @{$rows->[$r]};
my $points = (2*$w+$d)/2.0;
my $percent = ($g) ? int( (100*(2*$w+$d))/(2.0*$g) + 0.5 ) : 0;
push @results, join("|", $c, $w, $d, $l, $g, $points, $percent);
$minloss = '-' if( $minloss >= 9999 );
push @ratings, join("|", $c, $maxwin, $minloss);
#print( __FILE__,__LINE__, $r, join( "|", @{$rows->[$r]} ), "
\n" );
# totals
$tw += $w; $td += $d; $tl += $l; $tg += $g;
$maxelo = $max if( $max > $maxelo );
}
if( $tg )
{
$tpoints = (2*$tw+$td)/2.0;
$tpercent = ($tg) ? int( (100*(2*$tw+$td))/(2.0*$tg) + 0.5 ) : 0;
push @results, join("|", 'Total', $tw, $td, $tl, $tg, $tpoints, $tpercent, 1);
}
$total_games += $tg;
$tpl->addMakro( "TOTAL_GAMES_$league", $tg );
$tpl->addMakro( "PCR_MAX_$league", $maxelo );
$tpl->addArray( "RESULTS_$league", \@results );
$tpl->addArray( "RATINGS_$league", \@ratings );
}
# Display Page
#-------------
$tpl->addMakro( "NAME", $name );
$tpl->addMakro( "TOTAL_GAMES", $total_games );
print $tpl->Expand($sobj);
AUS( __FILE__,__LINE__, "TOTAL TIME:", time()-$starttime, $starttime, time );
}