#!/usr/bin/perl
#

use 5.006;
use DBI;
use CGI;
use lib qw(/mnt/sda/tmm/handlers/genc);
use TClubMD5;

my $server = "127.0.0.1";
my $user = $primary_user;
my $passwd = $primary_passwd;
my $dbase = $primary_dbase;
my $port = $primary_port;

my $query = new CGI;

print "Content-Type: text/html; charset=UTF-8\n\n";

my $who = $query->param('who');
my $token = $query->param('token');
my $ru_en = $query->param('l');
my $language = $query->param('lang');
my $par = $query->param('par');
my $db = $query->param('db');
my $curr_sport = $query->param('cat');

my $session = TClubMD5::md5_hex($who.$salt);

die("active_players.pl: token is $token, session is $session!\n") if ($token ne $session);
die("psssooff!\n") if (in_list($who,$pssoff_guys_string));

my $vic = '';
my $mtch = '';

if ($language eq 'russian') {
$vic = 'побед в';
$mtch = 'матчах';
} else {
$vic = 'victories in';
$mtch = 'matches';
};

$ru_en = 'second_floor' if ($db eq 'three');

($dbase,$server,$port,$user,$passwd) = TClubMD5::get_connect_req($ru_en);

$dbase = "clone" if ($db eq 'three');
my $three = '';
$three = '_three' if ($db eq 'three');

my $and = '';
$and = " and (e_masterst ~ '$opz' or e_masterst ~ '$opf' or e_masterst ~ '$tpz' or e_masterst ~ '$tpf' or e_masterst ~ '$hpz') " 
	if ($db eq 'three');

$dbconn=DBI->connect("dbi:Pg:dbname=$dbase;port=$port;host=$server",$user, $passwd);

$comm = "select a_nkname, num_of_unique_played_matches_current_month".$three."(proj_code,$curr_sport) as numm, num_of_unique_victories_current_month".$three."(proj_code,$curr_sport) as numv, proj_code, oid  from members where d_profi != 'new' and num_of_unique_played_matches_current_month".$three."(proj_code,$curr_sport) > 0 and p_vremya_z is not null $and order by numv desc, numm asc limit 10";
$comm = "select a_nkname, num_of_unique_played_matches_in_month".$three."(proj_code,$par,$curr_sport) as numm, num_of_unique_victories_in_month".$three."(proj_code,$par,$curr_sport) as numv, proj_code, oid  from members where d_profi != 'new' and num_of_unique_played_matches_in_month".$three."(proj_code,$par,$curr_sport) > 0 and p_vremya_z is not null $and order by numv desc, numm asc limit 15"
	if ($par);
#print STDERR $comm ."\n";
&getTable;

my $xml = '<div style="margin:3px;">';

$xml .= 'no matches' if ($ntuples < 1);
for (my $i=0; $i<$ntuples;$i++) {


my $who = ${${$listresult}[$i]}[0];
my $nm = ${${$listresult}[$i]}[1];
my $nv = ${${$listresult}[$i]}[2];
my $pr = ${${$listresult}[$i]}[3];
my $o = ${${$listresult}[$i]}[4];
$who = $pr if(in_list($pr,$changing_guys_string));

	print qq{
	<form id="myForm_show_profile_$o" action="$scriptname?mode=show_profile&member=$o" method="get">
	</form>	
	};
	
$vic = 'победы в' if($language eq 'russian' && $nv > 1 && $nv < 5);
$vic = 'победа в' if($language eq 'russian' && $nv == 1);
$mtch = 'матче' if($language eq 'russian' && $nv == 1);
if ($nv > 0) {
	$xml .= '<div style="float:left;padding:2px;color:#2233cc;" onclick="js_show_profile('.$o.');" onmouseover="this.style.color=\'#bbb\';" onmouseout="this.style.color=\'#2233cc\';">'.$who.'</div>';
	$xml .= '<div align=right style="padding:2px;">' . $nv . ' '.$vic.' ' . $nm . ' '.$mtch.'</div>';
	$xml .= '<div style="clear:left;"></div>';
}
} #for

$xml .= '</div>';
print $xml;

$dbconn->disconnect;
$query->delete_all;
exit;

sub getTable {

	$result=$dbconn->prepare($comm);
    	$result->execute;
	&dBaseError($result, $comm."  (".$result->rows()." rows found)") if ($result->rows() ==
	-2);
	
	$listresult = $result->fetchall_arrayref;
	$ntuples = $result->rows();

};

sub dBaseError {

    my ($check, $message) = @_;
    print "<H4><FONT COLOR=BLACK><P>$message<BR>Error: ".$check->errstr."</FONT></H4>";
    die("Action failed on command:$message  Error_was:$DBI::errstr");
};

sub in_list {
my $guy = shift;
my $str = shift;
my $comma = shift;

if (defined($comma)) {
return 1 if ($str eq $guy || $str =~ /^$guy\,/ || $str =~ /\,$guy\,/ || $str =~ /\,$guy$/);
return 0;
} else {
return 1 if ($str eq $guy || $str =~ /^$guy\#/ || $str =~ /\#$guy\#/ || $str =~ /\#$guy$/);
return 0;
}
}
