*
* 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 '
'.
_("Working on some aspect of e-DoKo... This will probably take max. $DB_work minutes. It could be over in a few seconds too though ;)").
'
';
output_footer();
exit();
}
return;
}
/* define possible status for email subsjects */
define("GAME_CANCELED", 0);
define("GAME_CANCELED_POVERTY", 1);
define("GAME_CANCELED_TIMEOUT", 2);
define("GAME_YOUR_TURN", 3);
define("GAME_READY", 4);
define("GAME_POVERTY", 5);
define("GAME_DPOVERTY", 6);
define("GAME_OVER", 7);
define("GAME_RECOVERY", 8);
define("GAME_REMINDER", 9);
define("GAME_NEW", 10);
/* define possible status for showing cards */
define("CARDS_EMPTY", 0); /* show player's hand*/
define("CARDS_SHOW", 1); /* show player's hand*/
define("CARDS_MYTURN", 2); /* show radiobuttons for cards that can be played*/
define("CARDS_EXCHANGE", 3); /* do we need to return cards to our partner in poverty?*/
define("CARDS_GAMEOVER_ME", 4); /* show all cards from everyone*/
define("CARDS_GAMEOVER", 5); /* show all cards from everyone (looking at someone else's game)*/
/* cards_status: SHOW show our hand
* MYTURN show radiobutton for cards that can be played
* EXCHANGE do we need to return cards to our partner in poverty?
* GAMEOVER show all cards from everyone
*/
function mymail($uid,$gameid=0,$type,$message)
{
global $EmailName,$WIKI,$PREF;
/* uid can be either a single uid or an array, convert everything to
* an array, so that we can loop over it */
if(!is_array($uid))
{
$to_uid=$uid;
$uid = array();
$uid[]=$to_uid;
}
foreach($uid as $user)
{
/* do we send the email right away or save it in the database? */
$send_now = 1;
$name = DB_get_name('userid',$user);
$To = DB_get_email('userid',$user);
/* check if user wants email right away or if we should save it in
* the database for later delivery
*/
$uidPREF = DB_get_PREF($user);
if( $uidPREF['digest'] != 'digest-off' )
$send_now = 0;
/* use local language */
set_language($uidPREF['language']);
$header = sprintf(_('Hello %s'),$name);
$header .= "\n\n";
/* add standard header and footer */
$subject = "$EmailName ";
if($gameid)
$game = DB_format_gameid($gameid);
else
$game = '';
switch($type)
{
case GAME_CANCELED:
$subject.=sprintf(_('Game %s canceled'),$game);
break;
case GAME_CANCELED_POVERTY:
$subject.=sprintf(_('Game %s canceled (poverty not resolved)'),$game);
break;
case GAME_CANCELED_TIMEOUT:
$subject.=sprintf(_('Game %s canceled (timed out)'),$game);
break;
case GAME_YOUR_TURN:
$subject.=sprintf(_('A card has been played in game %s'),$game);
break;
case GAME_READY:
$subject.=sprintf(_('Ready, set, go... (game %s)'),$game);
break;
case GAME_POVERTY:
$subject.=sprintf(_('Poverty (game %s)'),$game);
break;
case GAME_DPOVERTY:
$subject.=sprintf(_('Double poverty (game %s)'),$game);
break;
case GAME_OVER:
$subject.=sprintf(_('Game over (game %s)'),$game);
break;
case GAME_RECOVERY:
$subject.=_('Recovery');
break;
case GAME_REMINDER:
$subject.=sprintf(_("Reminder: game %s it's your turn"),$game);
break;
case GAME_NEW:
$subject.=sprintf(_('You are invited to a game of DoKo (game %s)'),$game);
break;
default:
$subject.=sprintf(_('Problem with email, contact admin (errorcode %d)'),$gameid);
}
/* standard goodbye */
$footer = "\n"._('Have a nice day')."\n "._('your E-Doko service department').
"\n\n".
"-- \n".
_('You can change your mail delivery mode in the preference menu.').
"\n".
_('web').': http://doko.nubati.net '.
_('help, bugs, etc.').": $WIKI";
if($send_now)
sendmail($To,$subject,$header.$message.$footer);
else
{
/* store email in database */
DB_digest_insert_email($To,$message,$type,$gameid);
}
}
/* reset language to original user*/
set_language($PREF['language']);
return;
}
function sendmail($To,$Subject,$message)
{
/* this function sends the mail or outputs to the screen in case of debugging */
global $debug,$EMAIL_REPLY;
$header = "";
if(isset($EMAIL_REPLY))
$header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
$header .= "Content-Type: text/plain; charset = \"UTF-8\";\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n";
$header .= "\r\n";
if($debug)
{
/* display email on screen,
* change txt -> html
*/
$message = str_replace("\n","\n";
display_single_user($row1);
echo "\n
\n";
display_single_user($row0,1); /* mark starting player in case re/contra is not set yet */
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=".DB_quote_smart($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,$start=0)
{
/* start=1, mark starting player, default=0, so the player on the left is not marked */
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";
/* mark starting player */
if($start && ! ($party=="re" || $party=="contra"))
echo '
'._('Starting Player')." \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=".DB_quote_smart($id).
" AND Hand.hash!=".DB_quote_smart($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=".DB_quote_smart($id).
" AND ( Game.player=".DB_quote_smart($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=".DB_quote_smart($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 = "