#!/usr/bin/perl
#----------------------------------------------------------------------------
# $Header: e:/p/w/playchess/rcs/playerstats-game.cgi 1.4 2003/01/15 16:53:06Z ts init $
# $Log: playerstats-game.cgi $
# Revision 1.4 2003/01/15 16:53:06Z ts # CGI-scripts moved to cgi directory -> relative path
# Revision 1.3 2002/11/10 21:19:07Z ts # Renamed all tables: tbl_+oldname
# Revision 1.2 2001/11/26 16:55:32Z ts # Don't know
#----------------------------------------------------------------------------
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 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;
#$| = 1; # disable output buffering
$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 $league = $cgi->param( 'league' ) || 'HCL';
local $name = $cgi->param( 'name' ) || $sname;
local $min = $cgi->param( 'min' );
#local $ppp = $cgi->param( 'ppp' ) || 20;
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);
# Games and Rating
#-----------------
{
my $limit = 20;
$limit = 40 if($sobj->getValue('isMember')); # Standard + Trial
$limit = 80 if($sobj->getValue('isPremiumMember')); # Premium members
# game history only for members
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# $min = 0 unless($sobj->getValue('isMember'));
my( $pstmt, $cstmt, $stmt, $rows, @table );
# Default values
#---------------
$min = 0 unless $min;
$limit = 10 unless $limit;
# fetch pid of that player
$pstmt = "select pid from tbl_player where name=?";
my $pid = MyDbi::getValue($pstmt, $name);
#print "pid = $pid
\n";
#print "limit = $limit
\n";
# count total finished games of that player
$cstmt = "
select count(*)
from tbl_player_game pg, tbl_game g
where pg.pid=?
and g.name like '$league%'
and g.result<>''
and g.valid = 1
and g.gid=pg.gid
";
my $total_games = MyDbi::getValue($cstmt, $pid);
#print "total_games = $total_games
\n";
#print "league = $league
\n";
$stmt = "
select
p2.name,
pg1.color, pg1.oppelo, pg1.newelo,
g.result, g.lastmove, g.name,
a.changed
from
(
tbl_player_game pg1,
tbl_game g,
tbl_player_game pg2,
tbl_player p2
)
left outer join tbl_annotation a on a.gid=g.gid and a.apid=pg1.pid
where pg1.pid = ?
and pg1.gid = pg2.gid
and pg1.gid = g.gid
and p2.pid = pg2.pid
and pg1.pid <> pg2.pid
and g.result <> ''
and g.valid = 1
and g.name like '$league%'
order by g.lastmove desc
limit $min, $limit
";
# and g.tid = t.tid
# and t.league = ?
$rows = MyDbi::getRows( $stmt, $pid );
#print "rows = $rows
\n";
print "stmt = '$stmt'
\n" if( Util::isDebug );
# finished games
my ( $r, $annotated, $pts, %Rating, $line, $created, $changed );
for $r ( 0 .. $#{$rows} )
{
( $oppname, $mycolor, $oppelo, $myelo, $result, $lastmove, $gamename, $changed ) = @{ $rows->[$r] };
($league,$tour,$bd) = split( /-/, $gamename );
# date of annotation (if exists)
#-------------------------------
# ($created,$changed) = Annotate->When( "$league-$tour-$bd-$name" );
$annotated = ($changed) ? Util::getDate($changed, "dd.mm.yy") : "";
$pts = ($result eq $mycolor) ? 1 : ($result eq 'd' ? 0.5 : 0);
# requiring ter and oppter hides errors in old system!!!
#-------------------------------------------------------
if( $myelo && $oppelo )
{
# $Rating->{$lastmove} = $myelo;
$Rating{1000-$r} = $myelo;
$line = join( "|", $gamename, Util::getDate($lastmove,"dd.mm.yy"), $pts,
$mycolor, 0+$myelo, $oppname, 0+$oppelo, $annotated, ($min+$r+1), ($r%2) );
push @results, $line;
#AUS(__FILE__,__LINE__, $r, $line );
}
}
my $finished = scalar(@results);
my $RatingGraph;
$RatingGraph = PCPlayer::HashGraphic( "Ratings in $league", "number", 100, %Rating )
if $finished>2;
my $tpl = Template->new( "playerstats-fg.tpl" );
$tpl->addBatch( $min, $limit, $finished, $total_games );
$tpl->addMakro( "SESSION_INFO", $sobj->Info() );
$tpl->addArray( "RESULTS", \@results );
$tpl->addMakro( "RATING_GRAPH", $RatingGraph );
$tpl->addMakro( "NGAMES", 0+$finished );
$tpl->addMakro( "TEST", "NGAMES" );
$tpl->addMakro( "NAME", $name );
$tpl->addMakro( "LEAGUE", $league );
$tpl->addMakro( "FIRST", $min+1 );
$tpl->addMakro( "NEWMIN", $min+$limit );
$tpl->addMakro( "LIMIT", $limit );
# Display Page
#-------------
print $tpl->Expand($sobj);
AUS( __FILE__,__LINE__, "TOTAL TIME:", time()-$starttime, $starttime, time );
}