#!/usr/bin/perl
# ================================================================================
# $Rev: 184 $ $URL: svn://localhost/PlayChess/trunk/cgi/prv-publist.cgi $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Author: $Author: Thomas $
# Modified: $Date: 2006-05-20 12:29:26 +0200 (Sa, 20 Mai 2006) $
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Pending anonymous challenges of all players
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright at playchess.de - all rights reserved
# ================================================================================
use lib "../cgi-bin";
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use PCLeagueUtil;
use PCPlayer;
use PC;
use PCGame;
use Object;
use PCSession;
use Template;
use Util;
use ChessConfig;
use MyDbi;
##--------------------------------------------------------------------------
## LOCAL CONFIGURATION
##--------------------------------------------------------------------------
local ($starttime,$endtime) = (time(),0);
$cgi = CGI->new();
$action = $cgi->param( 'action' );
$sobj = PCSession->new( $cgi );
$spid = $sobj->getValue( 'pid' );
$sname = $sobj->getValue( 'name' );
$ppp = $cgi->param('ppp') || 25;
$min = $cgi->param('min');
$max = $cgi->param('max');
$sort = $cgi->param('sort') || 'age';
# Default action: list of challenges
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# if( !$action )
{
# Page requires login
# ~~~~~~~~~~~~~~~~~~~
# $sobj->assertLogged();
$sname =~ s/^\s*(.*?)\s*$/$1/; # ignore leading and trailing blanks
$sname =~ s/\s/_/g; # replace inner blanks with '_'
$min = 0 unless $min;
$ppp = 10 if( $ppp<=10 || $ppp>500);
my $stmt;
# Connect to database
#--------------------
$dbh = dbiConnect() or die( "Cannot access database" );
# Pending challenges
# ~~~~~~~~~~~~~~~~~~
my $hclelo = 0+$sobj->getValue('hclelo');
my $aclelo = 0+$sobj->getValue('aclelo');
my $cclelo = 0+$sobj->getValue('cclelo');
my $compelo = $aclelo or $cclelo;
my $rcond = "";
# print "he=$hclelo ae=$aclelo
\n";
$rcond = "
and
(
(c.usecomp='no' and (c.pcr_min = 0 or c.pcr_min <= ?) and (c.pcr_max = 0 or c.pcr_max >= ?))
or
(c.usecomp='yes' and (c.pcr_min = 0 or c.pcr_min <= ?) and (c.pcr_max = 0 or c.pcr_max >= ?))
)
" if( $hclelo && $aclelo );
$stmt = "
select count(*)
from tbl_challenge c
where c.state='pending' and c.gpid = 0
$rcond
";
#$tp = MyDbi::getValue( $stmt );
$tp = MyDbi::getValue( $stmt, $hclelo, $hclelo, $compelo, $compelo ) if( $compelo );
$tp = MyDbi::getValue( $stmt ) unless( $compelo );
#print "tp=$tp ($stmt)
\n";
$order = 'order by 6 desc, 3' if $sort eq 'pcr';
$order = 'order by 3, 6 desc' if $sort eq 'name';
$order = 'order by 7 desc, 3' if $sort eq 'age';
$order = 'order by 9, 8, 10, 3' if $sort eq 'clock';
$stmt = "
select c.cid, ph.pid, ph.name, ph.country, ph.flag, r.rating,
c.ctime, c.clockmin, c.clockinc, c.clockmax,
c.color, c.publish, c.usecomp, c.pcr_min, c.pcr_max
from tbl_challenge c, tbl_player ph, tbl_rating r
where 1
and c.state='pending'
and c.gpid = 0
$rcond
and ph.pid=c.hpid
and r.pid=c.hpid
and (c.usecomp='no' and r.league='HCL' or c.usecomp='yes' and r.league<>'HCL')
$order
limit $min,$ppp
";
# order by c.ctime desc
$rows = MyDbi::getRows( $stmt, $hclelo, $hclelo, $compelo, $compelo ) if( $compelo );
$rows = MyDbi::getRows( $stmt ) unless( $compelo );
# $rows = MyDbi::getRows( $stmt );
# print "rows=$rows", "
\n";
my @challenge;
for $r ( 0 .. $#{$rows} )
{
my ($cid, $hpid, $hname, $hcountry, $hflag, $pcr, $when,
$cmin, $cinc, $cmax, $color, $publish, $usecomp, $pcr_min, $pcr_max) = @{ $rows->[$r] };
my $opponent = ($hname eq $sname) ? $gname : $hname;
my $mycolor = ($color eq 'r') ? 'r' : other($color);
# color of player: table contains color of challenger - not this player
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#$color = ($mycolor eq 'w') ? 'White' : ($mycolor eq 'b' ? 'Black' : 'Random');
#$color = ($mycolor eq 'w') ? 'White' : ($mycolor eq 'b' ? 'Black' : 'Random');
if( $color eq 'Random' || $role eq 'Host' ) { $mycolor = 'R'; }
else { $mycolor = ($color eq 'White') ? 'B' : 'W'; }
$publish = 'G,N' if $publish eq 'all';
$publish = 'G' if $publish eq 'privnotes';
$publish = '-' if $publish =~ /priv|none/;
#my $line = join( '|', $cid, Util::getDate($when,'dd.mm.yy'), $cmin, $cinc, $cmax,
my $age = int( (time()-$when)/86400 + 0.5 );
my $line = join( '|', $cid, $age, $cmin, $cinc, $cmax,
$mycolor, $publish, $hname, $pcr, $usecomp, $min+$r+1, $hpid==$spid,
$hcountry, $hflag, $pcr_min, $pcr_max );
push @challenge, $line;
#print "line=$line
\n";
}
# database is no longer needed
#-----------------------------
$dbh->disconnect();
my $tpl = Template->new( 'prv-publist.tpl' );
$tpl->addMakro( "TP", $tp );
$tpl->addMakro( "COLS", ($sobj->isLogged) ? 11 : 10 );
$tpl->addMakro( "NAME", $sname );
$tpl->addMakro( "SORT", $sort );
$tpl->addMakro( "SELECTION", $compelo );
$tpl->addMakro( "NCHALLENGE", scalar @challenge );
$tpl->addArray( "CHALLENGE", \@challenge );
$tpl->addBatch( $min, $ppp, scalar @challenge, $tp );
print $tpl->Expand( $sobj );
}