#!/usr/bin/perl
# ============================================================================
# $Rev: 184 $ $URL: svn://localhost/PlayChess/trunk/cgi/player-pgn.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# $Date: 2006-05-20 12:29:26 +0200 (Sa, 20 Mai 2006) $
# $Author: Thomas $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# PGN download of games of a certain player
# ============================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use PCLeagueUtil;
use PCGame;
use PCPlayer;
use PC;
use PCSession;
use Template;
use Util;
use ChessConfig;
use PGN;
use PgnList;
use MyDbi;
use Time::Local;
##--------------------------------------------------------------------------
## LOCAL CONFIGURATION
##--------------------------------------------------------------------------
local $config = getConfig();
local $starttime = time();
local $debug = 0 || Util::isDebug;
local $maxGames = 100;
local $nGames;
##--------------------------------------------------------------------------
## External CGI parameters
##--------------------------------------------------------------------------
$cgi = CGI->new();
$sobj = PCSession->new( $cgi );
$sname = $sobj->getValue( 'name' );
$name = $cgi->param('name');
$action = $cgi->param('action');
$from = $cgi->param('from') || '2000-01-01';
$until = $cgi->param('until') || Util::getDate( time() );
# Page requires membership
$sobj->assertMember();
cgiError( "Player name is missing" ) unless( $name );
if( $action =~ /download/ )
{
# check parameters
ShowPage( 'date_format' ) unless( !$from || $from =~ /2\d\d\d-\d\d-\d\d/ );
ShowPage( 'date_format' ) unless( !$until || $until =~ /2\d\d\d-\d\d-\d\d/ );
# Connect to database
$dbh = MyDbi::dbiConnect() or die( "DB-Handle is undefined" );
# determine minTime and maxTime from $from/$until
my ($yf, $mf, $df) = split( /-/, $from );
my ($yu, $mu, $du) = split( /-/, $until );
my $minTime = timegm( 0, 0, 0, $df, $mf-1, $yf-1900 );
my $maxTime = timegm( 0, 0, 0, $du, $mu-1, $yu-1900 );
# fetch pid of that player
my $pstmt = "select pid from tbl_player where name=?";
my $pid = MyDbi::getValue($pstmt, $name);
# debug
print "pid($name) = $pid
\n" if( $debug );
# finished games of that player
$stmt = "
select g.name
from tbl_player_game pg, tbl_game g
where pg.pid=?
and pg.gid=g.gid
and g.valid=1
and g.lastmove > $minTime
and g.lastmove < $maxTime
order by g.lastmove
limit $maxGames
";
my $gamelistRef = MyDbi::getColumn( $stmt, $pid );
$nGames = $#{$gamelistRef} + 1;
# error: too many games
ShowPage( 'too_many' ) if( $nGames > $maxGames );
# debug
print "list($name) = $gamelistRef nGames=$nGames @{$gamelistRef}
\n" if( $debug );
# create PGN page
my $pgnobj = PgnList->new( $gamelistRef, $sname );
$pgnobj->createPgn();
my $tpl = $pgnobj->getPgnPage();
# database is no longer needed
$dbh->disconnect();
# $tobj->addMakros( $tpl );
print $tpl->Expand( $sobj );
exit(0);
}
else
{
ShowPage( '' );
}
#------------------------------------
sub ShowPage
#------------------------------------
{
my( $errstr ) = @_;
my $file = "player-pgn.tpl";
my $tpl = Template->new( $file );
$tpl->addMakro( "NAME", $name );
$tpl->addMakro( "from", $from );
$tpl->addMakro( "until", $until );
$tpl->addMakro( "nGames", $nGames );
$tpl->addMakro( "maxGames", $maxGames );
$tpl->addMakro( "ERR", $errstr );
$tpl->addMakro( "noPobj", 1 );
$tpl->addMakro( $errstr, 1 );
my $txt = $tpl->Expand($sobj);
print $txt;
exit(0);
}