*
* This file is part of e-DoKo.
*
* e-DoKo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* e-DoKo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with e-DoKo. If not, see .
*
*/
/* make sure that we are not called from outside the scripts,
* use a variable defined in config.php to check this
*/
if(!isset($HOST))
exit;
function config_check()
{
global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
/* check if some variables are set in the config file, else set defaults */
if(!isset($EmailName))
$EmailName="[DoKo] ";
if(isset($EMAIL_REPLY))
{
ini_set("sendmail_from",$EMAIL_REPLY);
}
if(!isset($ADMIN_NAME))
{
output_header();
echo "
\n";
display_single_user($row1);
echo "\n
\n";
display_single_user($row0);
echo "
\n";
display_single_user($row2);
return;
}
function display_table_end ()
{
global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
global $RULES,$GAME,$gametype;
$result = DB_query("SELECT User.fullname as name,".
" Hand.position as position, ".
" User.id, ".
" Hand.party as party, ".
" Hand.sickness as sickness, ".
" Hand.point_call, ".
" User.last_login, ".
" Hand.hash, ".
" User.timezone, ".
" User.email ".
"FROM Hand ".
"LEFT JOIN User ON User.id=Hand.user_id ".
"WHERE Hand.game_id='".$gameid."' ".
"ORDER BY position ASC");
$row0 = DB_fetch_array($result);
$row1 = DB_fetch_array($result);
$row2 = DB_fetch_array($result);
$row3 = DB_fetch_array($result);
echo "
\n";
display_single_user($row3);
echo "
\n";
return;
}
function display_single_user($r)
{
global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
global $RULES,$GAME,$gametype;
$name = $r[0];
$pos = $r[1];
$user = $r[2];
$party = $r[3];
$sickness = $r[4];
$call = $r[5];
$hash = $r[7];
$timezone = $r[8];
$email = $r[9];
$wins = DB_get_number_of_tricks($gameid,$pos);
date_default_timezone_set($defaulttimezone);
$lastlogin = strtotime($r[6]);
date_default_timezone_set($timezone);
$timenow = strtotime(date("Y-m-d H:i:s"));
$gravatar = "$name\n";
if($debug)
echo "
";
if($vacation = check_vacation($user))
{
$start = $vacation[0];
$stop = substr($vacation[1],0,10);
$comment = $vacation[2];
$title = "begin: $start end: $stop $comment";
echo " $gravatar (on vacation until $stop) \n";
}
else
echo " $gravatar \n";
if($debug)
echo" \n";
/* add hints for poverty, wedding, solo, etc */
if( $gametype != "solo")
if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
echo " Schweinchen.
";
if($GT=="poverty" && $party=="re")
if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
{
$userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
$cards = DB_get_all_hand($userhash);
$trumpNR = count_trump($cards,'all');
if($trumpNR)
echo "
\n";
else
echo "
\n";
}
else
echo "
\n";
if($GT=="dpoverty")
if($party=="re")
if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
{
$userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
$cards = DB_get_all_hand($userhash);
$trumpNR = count_trump($cards,'all');
if($trumpNR)
echo "
\n";
else
echo "
\n";
}
else
echo "
\" title=\"poverty partner\" />\n";
else
if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
{
$userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
$cards = DB_get_all_hand($userhash);
$trumpNR = count_trump($cards,'all');
if($trumpNR)
echo "
\n";
else
echo "
\n";
}
else
echo "
\" title=\"poverty2 partner\" />\n";
if($GT=="wedding" && $party=="re")
if($sickness=="wedding")
echo "
\n";
else
echo "
\n";
if( (strpos($GT,"solo")!==false) && $party=="re")
{
if(strpos($GT,"queen")!==false)
echo "
\n";
else if(strpos($GT,"jack")!==false)
echo "
\n";
else if(strpos($GT,"club")!==false)
echo "
\n";
else if(strpos($GT,"spade")!==false)
echo "
\n";
else if(strpos($GT,"heart")!==false)
echo "
\n";
else if(strpos($GT,"trumpless")!==false)
echo "
\n";
else if(strpos($GT,"trump")!==false)
echo "
\n";
}
/* add point calls */
if($call!=NULL)
{
if($party=="re")
echo "
\n";
else
echo "
\n";
switch($call)
{
case "0":
echo "
\n";
break;
case "30":
echo "
\n";
break;
case "60":
echo "
\n";
break;
case "90":
echo "
\n";
break;
}
}
echo "
";
echo "
";
/* show how many tricks the person made */
switch($wins)
{
case 0:
echo "#tricks 0"; break;
case 1:
echo "#tricks 1"; break;
case 2:
case 3:
case 4:
echo "#tricks few"; break;
default:
echo "#tricks many"; break;
}
echo "\n";
echo "
\n";
}
function display_user_menu($id, $skiphash=NULL)
{
global $WIKI,$INDEX;
if($skiphash)
$result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
" LEFT JOIN Game On Hand.game_id=Game.id".
" WHERE Hand.user_id='$id'".
" AND Hand.hash!='$skiphash'".
" AND ( Game.player='$id' OR ISNULL(Game.player) )".
" AND ( Game.status='pre' OR Game.status='play' )".
" ORDER BY Game.session" );
else
$result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
" LEFT JOIN Game On Hand.game_id=Game.id".
" WHERE Hand.user_id='$id'".
" AND ( Game.player='$id' OR ISNULL(Game.player) )".
" AND ( Game.status='pre' OR Game.status='play' )".
" ORDER BY Game.session" );
$i=0;
while( $r = DB_fetch_array($result))
{
if($i==0)
{
echo "\n\n\n";
return;
}
function generate_score_table($session)
{
/* returns an array with N entries
* $score[$i]["gameid"] = gameid
* $score[$i]["players"] = array (id=>total points)
* $score[$i]["points"] = points for this game
* $score[$i]["solo"] = 1 or 0
*/
$score = array();
$i=0;
/* get all ids, scores and gametypes */
$gameids = DB_get_gameids_of_finished_games_by_session($session);
if($gameids == NULL)
return $score;
$player = array();
$player_party = array();
/* get player id from the first game */
$result = DB_query("SELECT user_id from Hand".
" WHERE Hand.game_id=".$gameids[0][0]);
while( $r = DB_fetch_array($result))
$player[$r[0]] = 0;
/* get party of players for each game in the session */
foreach($player as $id=>$points)
$player_party[$id]=DB_get_party_by_session_and_userid($session,$id);
/* get points and generate table */
foreach($gameids as $gameid)
{
$re_score = $gameid[1];
$gametype = $gameid[2];
foreach($player as $id=>$points)
{
$party = $player_party[$id][$i][0];
if($party == "re")
if($gametype=="solo")
$player[$id] += 3*$re_score;
else
$player[$id] += $re_score;
else if ($party == "contra")
$player[$id] -= $re_score;
}
$score[$i]['gameid'] = $gameid[0] ;
$score[$i]['players'] = $player;
$score[$i]['points'] = abs($re_score);
$score[$i]['solo'] = ($gametype=="solo");
$i++;
}
return $score;
}
function generate_global_score_table()
{
$return = array();
/* get all ids, scores and gametypes */
$gameids = DB_get_gameids_of_finished_games_by_session(0);
if($gameids == NULL)
return '';
$player = array();
/* get player id, names... from the User table */
$result = DB_query('SELECT User.id, User.fullname FROM User');
/* save information in an array */
while( $r = DB_fetch_array($result))
$player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
'response' => 0 , 'solo' => 0, 'soloavg' => 0);
/* get points and generate table */
foreach($gameids as $gameid)
{
$re_score = $gameid[1];
$gametype = $gameid[2];
/* get players involved in this game */
$result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid[0]));
while($r = DB_fetch_array($result))
{
$id = $r[0];
$party = DB_get_party_by_gameid_and_userid($gameid[0],$id);
if($party == 're')
if($gametype=='solo')
$player[$id]['points'] += 3*$re_score;
else
$player[$id]['points'] += $re_score;
else if ($party == 'contra')
$player[$id]['points'] -= $re_score;
if($party)
$player[$id]['nr']+=1;
}
}
/* add number of active games */
$result = DB_query_array_all("SELECT user_id, COUNT(*) as c " .
" FROM Hand".
" LEFT JOIN Game ON Game.id=game_id".
" WHERE Game.status IN ('pre','play')".
" GROUP BY user_id");
foreach($result as $res)
{
$player[$res[0]]['active'] = $res[1];
}
/* response time of users*/
$result = DB_query_array_all("SELECT user_id,".
"IFNULL(AVG(if(P1.sequence in (2,3,4),".
"-timestampdiff(MINUTE,mod_date,(select mod_date from Play P2 where P1.trick_id=P2.trick_id and P2.sequence=P1.sequence-1)),NULL )),1e9) as a ".
"FROM Play P1 ".
"LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
"LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
"GROUP BY user_id ");
foreach($result as $res)
{
$player[$res[0]]['response'] = $res[1];
}
/* most solos */
$result = DB_query_array_all("SELECT user_id as uid,".
" COUNT(*), ".
" COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
" FROM Game ".
" LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
" WHERE type='solo' AND Game.status='gameover' ".
" GROUP BY user_id ");
foreach($result as $res)
{
$player[$res[0]]['solo'] = $res[1];
$player[$res[0]]['soloavg'] = $res[2];
}
/* sort everything nicely */
function cmp($a,$b)
{
if($a['nr']==0) return 1;
if($b['nr']==0) return 1;
$a=$a['points']/$a['nr'];
$b=$b['points']/$b['nr'];
if ($a == $b)
return 0;
return ($a > $b) ? -1 : 1;
}
usort($player,'cmp');
foreach($player as $pl)
{
/* limit to players with at least 10 games */
if($pl['nr']>10)
$return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
$pl['response'],$pl['solo'],$pl['soloavg']);
}
return $return;
}
function format_score_table_ascii($score)
{
$output="";
if(sizeof($score)==0)
return "";
/* truncate table if we have too many games */
$max = sizeof($score);
if($max>6) $output.=" (table truncated to last 6 games)\n";
/* output header */
foreach($score[0]['players'] as $id=>$points)
{
$name = DB_get_name('userid',$id); /*TODO*/
$output.= " ".substr($name,0,2)." |";
}
$output.=" P |\n";
$output.= "------+------+------+------+------+\n";
/* output score for each game */
$i=0;
foreach($score as $game)
{
$i++;
if($i-1<$max-6) continue;
foreach($game['players'] as $id=>$points)
$output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
$output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
/* check for solo */
if($game['solo'])
$output.= " S|";
else
$output.= " |";
$output.="\n";
}
return $output;
}
function format_score_table_html($score,$userid)
{
global $INDEX;
if(sizeof($score)==0)
return "";
$output = "