#!/usr/bin/perl
# ================================================================================
# $Rev: 143 $ $URL: svn://localhost/PlayChess/trunk/cgi/tour-vars.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 OTree;
use PC;
use PCLeagueUtil;
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;
# local $| = 1; # disable output buffering
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 $tourname = $cgi->param( 'tourname' );
local $pDepth = $cgi->param( 'd' );
local $var = $cgi->param( 'var' );
# ignore leading and trailing blanks
$name =~ s/^\s*(.*?)\s*$/$1/;
die( "Tounament name is missing" ) unless($tourname);
local( $league, $tour ) = split( /-/, $tourname );
die( "Tounament name is not valid" ) unless($tour && $league);
# print $ENV{'HTTP_HOST'}, ": tour=$tour league=$league
\n" if( Util::isLocal || Util::isTest );
# Openings of a tournament
# ~~~~~~~~~~~~~~~~~~~~~~~~
{
# Page requires membership
# ~~~~~~~~~~~~~~~~~~~~~~~~
$sobj->assertMember();
# Following actions require database access
#------------------------------------------
$dbh = dbiConnect() or exit(1);
local $ST = time();
my( $stmt, $rows, @results );
my $starttime = time;
my $depth = $pDepth || 10;
my $plies = 2*$depth;
my $movecond = ($var) ? "and m.moves like '$var%'" : '';
$stmt = "
select
m.gid, m.moves,
IF(g.result='w',1,0),
IF(g.result='d',1,0),
IF(g.result='b',1,0),
IF(g.result='', 1,0),
g.name, g.lastmove
from
tbl_tournament t,
tbl_game g,
tbl_move_archive m
where 1
and t.name=?
and t.league=?
and t.tid=g.tid
and g.gid=m.gid
and g.valid=1
$movecond
# and g.result is not null
order by 2,3
limit 500
";
# $rows = MyDbi::getRows( $stmt, $league );
$rows = MyDbi::getRows( $stmt, $tour, $league );
# print "rows = $rows
\n" if( Util::isLocal || Util::isTest );
# No database access beyond this point
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$dbh->disconnect();
$db_time = "DB-Time: ", time()-$ST, " seconds
\n";
#exit(0);
unless( $var )
{
# Create HTML for opening tree
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# $tree = OTree::createTree( $rows, 'game.cgi?game', $plies );
$tree = OTree::createTree( $rows, $plies, "/games/%s", "/tournaments/$tourname/openings?var=%s" );
# Beginning to create output
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
my $tpl = Template->new( "tour-otree.tpl" );
$tpl->addMakro( "NAME", $name );
$tpl->addMakro( "TOURNAME", $tourname );
$tpl->addMakro( "LEAGUE", $league );
$tpl->addMakro( "TOUR", $tour );
$tpl->addMakro( "TREE", $tree );
$tpl->addMakro( "FOUND", $#{$rows}+1 );
print $tpl->Expand($sobj);
exit(0);
}
else
{
# Create HTML for game list
# ~~~~~~~~~~~~~~~~~~~~~~~~~
my $gamelist_ref;
($gamelist_ref, $var) = OTree::createList( $rows, $var );
# Beginning to create output
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
my $tpl = Template->new( "tour-otree.tpl" );
$tpl->addMakro( "NAME", $name );
$tpl->addMakro( "TOURNAME", $tourname );
$tpl->addMakro( "LEAGUE", $league );
$tpl->addMakro( "TOUR", $tour );
$tpl->addArray( 'GAMELIST', $gamelist_ref );
$tpl->addMakro( "FOUND", $#{$rows}+1 );
$tpl->addMakro( "VARIANT", $var );
print $tpl->Expand($sobj);
}
}