#!/usr/bin/perl
# ================================================================================
# $Rev: 296 $ $URL: svn://localhost/PlayChess/trunk/cgi/movelist.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Author: $Author: epts $
# Modified: $Date: 2007-02-04 21:26:23 +0100 (So, 04 Feb 2007) $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright at playchess.de - all rights reserved
# ================================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use PCGame;
use PCSession;
use Template;
use Util;
use MyDbi;
local $cgi = CGI->new();
local $sobj = PCSession->new( $cgi );
local $game = $sobj->getCgiParam( 'game' );
local $gid = $sobj->getCgiParam( 'gid' );
local $wp = $sobj->getCgiParam( 'wp' );
local $bp = $sobj->getCgiParam( 'bp' );
local $config = getConfig();
local @movelist;
local $debug = (0 || Util::isDebug);
if( $debug )
{
print "
game = $game
\n";
print "gid = $gid
\n";
print "wp = $wp
\n";
print "bp = $bp
\n";
print "query = " , $ENV{QUERY_STRING} . "
\n";
# exit(0);
}
die( "Game id is missing" ) unless( $gid || $game );
# Connect to database
#--------------------
$dbh = dbiConnect() or die( "DB-Handle is undefined" );
# Move list
#----------
if( 0 )
{
my( $game, $publish, $result );
my $stmt;
$stmt = "select name, publish, result from tbl_game where gid=?";
( $game, $publish, $result ) = @{ MyDbi::getRow( $stmt, $gid ) };
# print join( ', ', $game, $publish, $result, $tbl_move ), "
\n";
$stmt = " select m.ply, m.color, m.san, n.note
from tbl_move m
left outer join tbl_movenote n on n.gid=m.gid and n.ply=m.ply
where m.gid = ?
order by m.ply ";
my $rows = MyDbi::getRows($stmt,$gid);
my $m = 0;
my ($moveno, $wmove, $wnote, $wod, $wcm, $hasWNote, $hasNote);
my( $ply, $color, $san, $note );
$wmove = '...';
for $r ( 0 .. $#{$rows} )
{
( $ply, $color, $san, $note ) = @{ $rows->[$r] };
($isDrawOffer, $isCondMove) = (undef,undef);
$hasNote = ($note != '');
print "$ply: $color $san $note
\n";
$moveno = int( ($ply+1)/2 );
# extract draw offers from note text
if( $note =~ /^\(offering draw\) / )
{
$isDrawOffer = 1;
$note =~ s/\(offering draw\) //;
}
# extract conditional moves from note text
if( $note =~ /^\(CM\) / )
{
$isCondMove = 1;
$note =~ s/\(CM\)//;
}
# filter out pipe-symbols (used in smilies)
$note =~ s/\|/)/g;
if( $color eq 'w' )
{
$wmove = $san;
$wnote = $note;
$wod = $isDrawOffer;
$wcm = $isCondMove;
$hasWNote = $hasNote;
}
else
{
$line = "$moveno|$wmove|$san|$wnote|$note|$wod|$isDrawOffer|$wcm|$isCondMove|$hasWNote|$hasNote";
push @movelist, $line;
print "$line
\n";
$wmove = '';
}
}
push @movelist, "$moveno|$wmove||$wnote||$wod||$wcm||$hasWNote" if $wmove;
# Thematic games
# ~~~~~~~~~~~~~~
if( $game =~ /T\d+/ )
{
$stmt = "select v.name, v.variant
from tbl_game g, tbl_tournament t, tbl_thema v
where g.gid=?
and g.tid=t.tid
and t.vid=v.vid
";
($varname,$variant) = @{ MyDbi::getRow( $stmt, $gid ) };
@var = split( /\s+/, $variant );
my $moveno = 1;
foreach $v (0 .. $#var)
{
$var[$v] = ($moveno++) . '.' . $var[$v] if( $v%2==0 );
}
$variant = join( ' ', @var );
}
}
# load game depending on CGI parameter given
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $gobj;
if( $gid ) { $gobj = PCGame->loadById( $gid ); }
elsif( $game ) { $gobj = PCGame->loadByName( $game ); }
$gid = $gobj->Id;
$game = $gobj->Name;
# players
my $wp = $gobj->Player('w')->Name;
my $bp = $gobj->Player('b')->Name;
# game status
my $publish = $gobj->Publish;
my $result = $gobj->Result;
my $variant = $gobj->PrescribedMoves;
my $varname = $gobj->Thema;
# move notes
my $stmt = "select ply, note from tbl_movenote where gid=?";
my $rows = MyDbi::getRows($stmt,$gid);
my %NOTE;
for $r ( 0 .. $#{$rows} )
{
my( $ply, $note ) = @{ $rows->[$r] };
$NOTE{ $ply } = $note;
}
# moves
my $moves = $gobj->readHistory;
my @Moves = split( /\s+/, $moves );
my $ply = 0;
my( $m, $note, $moveno, $whitemove, $whitenote, @movelist, @line );
print "moves='$moves'
\n" if($debug);
print "moves='", join('|',@Moves),"'
\n" if($debug);
foreach $m (@Moves)
{
my( $isDrawOffer, $isCondMove );
$moveno = 1 + int($ply/2);
$ply++;
$note = $NOTE{$ply};
# filter out pipe-symbols (used in smilies)
$note =~ s/\|/|/g;
print "$ply $moveno $m '$note'
\n" if($debug);
$hasNote = ($note != '');
# extract draw offers from note text
if( $note =~ /^\(offering draw\) / )
{
$isDrawOffer = 'D';
$note =~ s/\(offering draw\) //;
}
if( $note =~ /^\(OD\) / )
{
$isDrawOffer = 'D';
$note =~ s/\(OD\) //;
}
# extract conditional moves from note text
if( $note =~ /^\(CM\)/ )
{
$isCondMove = 'C';
$note =~ s/\(CM\)//;
}
if( $note =~ /^\(conditional move\)/ )
{
$isCondMove = 'C';
$note =~ s/\(conditional move\)//;
}
if( $note =~ /^\(\*\)/ )
{
$isCondMove = 'C';
$note =~ s/\(\*\)//;
}
# white move
if( $ply % 2 == 1 )
{
@line = ($moveno);
}
push @line, $hasNote, $m, $note, $isDrawOffer, $isCondMove, $ply;
# black move or end of sequence
if( $ply % 2 == 0 || $ply >= @Moves )
{
push @movelist, join('|',@line);
print "Line = ", join('|',@line), "
\n" if($debug);
}
}
# ---------- Advertisement -----------------------
# _RandomBook( 100, $tpl );
# visitor and game state determine the templated used for display
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $isPlayer = ($sobj->getValue('name') eq $wp ||
$sobj->getValue('name') eq $bp ||
$sobj->isAdmin );
my $isHidden = !$isPlayer && ($publish eq 'priv' || $publish eq 'privnotes');
# End of data retrieval. Now generating output
#---------------------------------------------
$dbh->disconnect();
# Template
#---------
local $tpl = Template->new( "movelist.tpl" );
$tpl->addMakro( "GAME", $game );
$tpl->addMakro( "RESULT", $result );
$tpl->addMakro( "WIN_W", 1 ) if( $result eq 'w' );
$tpl->addMakro( "DRAW", 1 ) if( $result eq 'd' );
$tpl->addMakro( "WIN_B", 1 ) if( $result eq 'b' );
$tpl->addMakro( "VARNAME", $varname );
$tpl->addMakro( "VARIANT", $variant );
$tpl->addMakro( "PUBLISH", $publish );
$tpl->addMakro( "IS_PLAYER", $isPlayer );
$tpl->addMakro( "IS_HIDDEN", $isHidden );
$tpl->addMakro( "WP", $wp );
$tpl->addMakro( "BP", $bp );
$tpl->addArray( "MOVELIST", \@movelist );
print $tpl->Expand($sobj);
exit(0);
#--------------------------------------------------------------------
sub _RandomBook
#--------------------------------------------------------------------
{
my( $percent, $tpl ) = @_;
my( $t ) = "";
my $Host = $ENV{HTTP_HOST};
my $isLocal = ($Host =~ /^(localhost|127\.0\.0)/);
if( 0 && $isLocal )
{
$tpl->addMakro( "SHOWBOOK", 0 );
return;
}
srand( time() ^ $$ );
if( 0 && rand(100) > 100-$percent )
{
my $book = ChessBook->findRandomBook();
#my $book = ChessBook->lookup('0486232085');
my $author = $book->{'author'};
my $isbn = $book->{'isbn'};
my $title = $book->{'title'};
my $year = $book->{'year'};
my $short = $book->{'short'};
my $desc = $book->{'desc'};
my $review = $book->{'review'};
my $target = $book->{'target'};
my $img = $book->{'img'};
my $imgw = $book->{'imgwidth'};
my $format = $book->{'format'};
my $avail = $book->{'avail'};
my $imgdir = "";
my $DefImg = 'amazon_book_globe.gif';
my $maxlength = 400;
$imgdir = '/img/books/' unless ($img =~ /\//);
$author =~ s/,.+//;
my $text;
#$text = $short;
$text = $desc."
" if $desc;
$text .= $review;
$text =~ s//
/gi;
if( length($text) > $maxlength )
{
$text = substr($text, 0, $maxlength) . " ...";
}
$tpl->addMakro( "SHOWBOOK", 1 );
$tpl->addMakro( "ISBN", $isbn );
$tpl->addMakro( "TARGET", $target );
$tpl->addMakro( "AUTHOR", $author );
$tpl->addMakro( "TITLE", $title );
$tpl->addMakro( "SHORT", $text );
$tpl->addMakro( "IMG", $img || $DefImg );
$tpl->addMakro( "IMGDIR", $imgdir );
$tpl->addMakro( "IMGW", $imgw );
$tpl->addMakro( "YEAR", $year );
$tpl->addMakro( uc($avail), 1 );
$tpl->addMakro( "QUER", 1 ) if $format eq 'hor';
#AUS(__FILE__,__LINE__, join( '|', $img, $imgdir, $DefImg) );
}
}