2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
4 * This file is part of e-DoKo.
6 * e-DoKo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * e-DoKo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with e-DoKo. If not, see <http://www.gnu.org/licenses/>.
21 /* make sure that we are not called from outside the scripts,
22 * use a variable defined in config.php to check this
27 function config_check()
29 global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
31 /* check if some variables are set in the config file, else set defaults */
32 if(!isset($EmailName))
34 if(isset($EMAIL_REPLY))
36 ini_set("sendmail_from",$EMAIL_REPLY);
38 if(!isset($ADMIN_NAME))
41 echo "<h1>Setup not completed</h1>";
42 echo "You need to set \$ADMIN_NAME in config.php.";
46 if(!isset($ADMIN_EMAIL))
49 echo "<h1>Setup not completed</h1>";
50 echo "You need to set \$ADMIN_EMAIL in config.php. ".
51 "If something goes wrong an email will be send to this address.";
58 echo "<h1>Setup not completed</h1>";
59 echo "You need to set \$DB_work in config.php. ".
60 "If this is set to 1, the game will be suspended and one can work safely on the database.".
61 "The default should be 0 for the game to work.";
68 echo "Working on the database...please check back later.";
76 function mymail($uid,$subject,$message)
80 /* do we send the email right away or save it in the database? */
83 /* add standard header and footer */
84 $subject = "$EmailName".$subject;
86 /* standard goodbye */
87 $footer = "\nHave a nice day\n".
88 " your E-Doko service department\n\n".
90 "You can change your mail delivery mode in the preference menu.\n".
91 'web: http://doko.nubati.net '.
92 'help: http://wiki.nubati.net/EmailDoko '.
93 'bugs: http://wiki.nubati.net/EmailDokoIssues';
97 /* send email to more than one person */
99 $header = "Hello all\n\n";
101 foreach($uid as $user)
103 $all[] = DB_get_email('userid',$user);
105 $To = implode(",",$all);
109 /* standard greeting */
110 $name = DB_get_name('userid',$uid);
111 $header = "Hello $name\n\n";
113 $To = DB_get_email('userid',$uid);
115 /* check if user wants email right away or if we should save it in
116 * the database for later delivery
119 $PREF = DB_get_PREF($uid);
120 if( $PREF['digest'] != 'digest-off' )
125 sendmail($To,$subject,$header.$message.$footer);
128 /* store email in database */
129 DB_digest_insert_email($To,$message);
133 function sendmail($To,$Subject,$message)
135 /* this function sends the mail or outputs to the screen in case of debugging */
136 global $debug,$EMAIL_REPLY;
140 if(isset($EMAIL_REPLY))
141 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
145 /* display email on screen,
148 $message = str_replace("\n","<br />\n",$message);
149 $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
150 "<a href=\"$1\">$1</a>", $message);
152 echo "<br />To: $To<br />";
154 echo $header."<br />";
155 echo "Subject: $Subject <br />$message<br />\n";
159 mail($To,$Subject,$message,$header);
161 mail($To,$Subject,$message);
167 /* returns 1 if all names passed as args are defined by a GET or POST statement,
172 $args = func_get_args();
174 foreach($args as $arg)
176 $ok = $ok * isset($_REQUEST[$arg]);
177 /*echo "$arg: ok = $ok <br />";
183 function myerror($message)
185 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
186 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
190 function pos_array($c,$arr)
207 function is_trump($c)
211 if(in_array($c,$CARDS["trump"]))
217 function is_same_suite($c1,$c2)
221 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
222 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
223 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
224 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
225 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
230 function compare_cards($a,$b,$game)
232 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
238 /* first map all cards to the odd number,
239 * this insure that the first card wins the trick
240 * if they are the same card
242 if( $a/2 - (int)($a/2) != 0.5)
244 if( $b/2 - (int)($b/2) != 0.5)
247 /* check for schweinchen and ten of hearts*/
253 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
255 if($a == 19 || $a == 20 )
257 if($b == 19 || $b == 20 )
260 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
262 if($a == 19 || $a == 20 )
264 if($b == 19 || $b == 20 )
267 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
269 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
270 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
272 if($a == 19 || $a == 20 )
274 if($b == 19 || $b == 20 )
277 /* if not, do nothing and the foxes are just handeled as normal trump */
283 /* check for ten of hearts rule */
284 if($RULES["dullen"]=="secondwins")
285 if($a==1 && $b==1) /* both 10 of hearts */
286 return 0; /* second one wins.*/
290 /* no special cases here */
294 if(is_trump($a) && is_trump($b) && $a<=$b)
296 else if(is_trump($a) && is_trump($b) )
299 { /*$a is not a trump */
303 { /* both no trump */
306 $posA = pos_array($a,$CARDS["clubs"]);
307 $posB = pos_array($b,$CARDS["clubs"]);
315 $posA = pos_array($a,$CARDS["spades"]);
316 $posB = pos_array($b,$CARDS["spades"]);
324 $posA = pos_array($a,$CARDS["hearts"]);
325 $posB = pos_array($b,$CARDS["hearts"]);
333 $posA = pos_array($a,$CARDS["diamonds"]);
334 $posB = pos_array($b,$CARDS["diamonds"]);
341 /* not the same suit and no trump: a wins */
347 function get_winner($p,$mode)
349 /* get all 4 cards played in a trick, in the order they are played */
352 $c1pos = $tmp["pos"];
356 $c2pos = $tmp["pos"];
360 $c3pos = $tmp["pos"];
364 $c4pos = $tmp["pos"];
366 /* first card is better than all the rest */
367 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
370 /* second card is better than first and better than the rest */
371 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
374 /* third card is better than first card and better than last */
375 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
376 /* if second card is better than first, third card needs to be even better */
377 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
379 /* second is worse than first, e.g. not following suite */
380 else if (compare_cards($c1,$c2,$mode) )
383 /* non of the above */
387 function count_nines($cards)
391 foreach($cards as $c)
393 if($c == "25" || $c == "26") $nines++;
394 else if($c == "33" || $c == "34") $nines++;
395 else if($c == "41" || $c == "42") $nines++;
396 else if($c == "47" || $c == "48") $nines++;
402 function check_wedding($cards)
405 if( in_array("3",$cards) && in_array("4",$cards) )
411 function count_trump($cards,$status='pregame')
417 /* count each trump, including the foxes, since this is used to determine poverty status */
418 foreach($cards as $c)
422 /* In case we really want to know the amount of trump, we can use the status variable.
423 * This is needed for example to figure out what icon to display on the table in case of
424 * trump given back in poverty */
425 if($status=='all') return $trump;
427 /* normally foxes don't count as trump, so we substract them here
428 * in case someone has schweinchen, one or two of them should count as trump
429 * though, so we need to add one trump for those cases */
432 if( in_array("19",$cards))
434 if( in_array("20",$cards) )
437 /* handle case where player has schweinchen */
438 if( in_array("19",$cards) && in_array("20",$cards) )
439 switch($RULES["schweinchen"])
442 /* add two, in case the player has both foxes (schweinchen) */
447 case "secondaftercall":
448 /* add one, in case the player has both foxes (schweinchen) */
458 function check_low_trump($cards)
462 if($RULES['lowtrump']=='none')
465 /* check if we have low trump */
468 foreach($cards as $card)
470 /* card a trump, but not a diamond? */
475 /* handle case where player has schweinchen */
476 if( in_array("19",$cards) && in_array("20",$cards) )
477 switch($RULES["schweinchen"])
481 case "secondaftercall":
491 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
499 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
500 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
501 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
502 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
503 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=21;
504 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
505 $r[ 6]=41; $r[18]=7; $r[30]=31; $r[42]=43;
506 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=20;
507 $r[ 8]=45; $r[20]=9; $r[32]=33; $r[44]=19;
508 $r[ 9]=46; $r[21]=10; $r[33]=44; $r[45]=24;
509 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
510 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
514 /* check if we can find a game were non of the player was involved and return
517 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
518 $randomnumbers = DB_get_unused_randomnumbers($userstr);
519 $randomnumbers = explode(":",$randomnumbers);
521 if(sizeof($randomnumbers)==48)
522 return $randomnumbers;
524 /* need to create new numbers */
528 /* shuffle using a better random generator than the standard one */
529 for ($i = 0; $i <48; $i++)
531 $j = @mt_rand(0, $i);
541 function display_cards($me,$myturn)
546 function have_suit($cards,$c)
551 if(in_array($c,$CARDS["trump"]))
552 $suite = $CARDS["trump"];
553 else if(in_array($c,$CARDS["clubs"]))
554 $suite = $CARDS["clubs"];
555 else if(in_array($c,$CARDS["spades"]))
556 $suite = $CARDS["spades"];
557 else if(in_array($c,$CARDS["hearts"]))
558 $suite = $CARDS["hearts"];
559 else if(in_array($c,$CARDS["diamonds"]))
560 $suite = $CARDS["diamonds"];
562 foreach($cards as $card)
564 if(in_array($card,$suite))
571 function same_type($card,$c)
576 /* figure out what kind of card c is */
577 if(in_array($c,$CARDS["trump"]))
578 $suite = $CARDS["trump"];
579 else if(in_array($c,$CARDS["clubs"]))
580 $suite = $CARDS["clubs"];
581 else if(in_array($c,$CARDS["spades"]))
582 $suite = $CARDS["spades"];
583 else if(in_array($c,$CARDS["hearts"]))
584 $suite = $CARDS["hearts"];
585 else if(in_array($c,$CARDS["diamonds"]))
586 $suite = $CARDS["diamonds"];
588 /* card is the same suid return 1 */
589 if(in_array($card,$suite))
595 function set_gametype($gametype)
609 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
610 '17','18','19','20','21','22','23','24','25','26');
611 $CARDS["diamonds"] = array();
612 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
613 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
614 $CARDS["hearts"] = array('43','44','45','46','47','48');
615 $CARDS["foxes"] = array('19','20');
616 if($RULES["dullen"]=='none')
618 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
619 '17','18','19','20','21','22','23','24','25','26');
620 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
622 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
623 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
625 /* find the fox and put them at the top of the stack */
626 foreach(array('19','20') as $fox)
629 $trump = $CARDS['trump'];
630 $key = array_keys($trump, $fox);
634 $foxa[]=$trump[$key[0]];
635 unset($trump[$key[0]]);
636 $trump = array_merge($foxa,$trump);
637 $CARDS['trump'] = $trump;
640 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
641 && $GAME['schweinchen-who'])
643 /* find the fox and put them at the top of the stack */
644 $trump = $CARDS['trump'];
645 $key = array_keys($trump, '19');
649 $foxa[]=$trump[$key[0]];
650 unset($trump[$key[0]]);
651 $trump = array_merge($foxa,$trump);
652 $CARDS['trump'] = $trump;
656 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
657 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
658 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
659 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
660 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
661 $CARDS["foxes"] = array();
664 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
665 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
666 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
667 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
668 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
669 $CARDS["foxes"] = array();
672 $CARDS["trump"] = array();
673 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
674 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
675 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
676 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
677 $CARDS["foxes"] = array();
680 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
681 '17','18','27','28','29','30','31','32','33','34');
682 $CARDS["clubs"] = array();
683 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
684 $CARDS["hearts"] = array('43','44','45','46','47','48');
685 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
686 $CARDS["foxes"] = array();
687 if($RULES["dullen"]=='none')
689 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
690 '17','18','27','28','29','30','31','32','33','34');
691 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
695 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
696 '17','18','35','36','37','38','39','40','41','42');
697 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
698 $CARDS["spades"] = array();
699 $CARDS["hearts"] = array('43','44','45','46','47','48');
700 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
701 $CARDS["foxes"] = array();
702 if($RULES["dullen"]=='none')
704 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
705 '17','18','35','36','37','38','39','40','41','42');
706 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
710 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
711 '17','18','43','44','45','46','47','48');
712 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
713 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
714 $CARDS["hearts"] = array();
715 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
716 $CARDS["foxes"] = array();
717 if($RULES["dullen"]=='none')
719 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
720 '17','18','43','44','1','2','45','46','47','48');
726 function mysort($cards,$gametype)
729 if(isset($PREF['sorting']))
730 if($PREF['sorting']=='high-low')
731 usort ( $cards, 'sort_comp_high_low' );
733 usort ( $cards, 'sort_comp_low_high' );
735 usort ( $cards, 'sort_comp_high_low' );
739 function sort_comp_high_low($a,$b)
744 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
745 $CARDS['hearts'],$CARDS['spades']);
747 return pos_array($a,$ALL)-pos_array($b,$ALL);
750 function sort_comp_low_high($a,$b)
755 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
756 $CARDS['hearts'],$CARDS['spades']);
758 return -pos_array($a,$ALL)+pos_array($b,$ALL);
761 function can_call($what,$hash)
763 /* figure out if a person can make a call:
764 $what in 0,30,60,90,120 = points of the call
765 $hash = the hash of the person who wants to make the call
768 0 can't make that call
770 2 can make the call, but this is the last chance to do so...
775 /* get some information
777 $gameid = DB_get_gameid_by_hash($hash);
778 $gametype = DB_get_gametype_by_gameid($gameid);
779 $oldcall = DB_get_call_by_hash($hash); /* did the person already made a call? */
780 $pcall = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
783 /* you're call must be better than the one you or your partner already made
785 if( ($pcall!=NULL && ($what >= $pcall))
786 || ($oldcall!=NULL && ($what >=$oldcall)) )
791 /* for some rules we need to know how many cards people have
793 $NRcards = count(DB_get_hand($hash));
798 $user = DB_get_hash_from_game_and_pos($gameid,$i);
799 $NRallcards += count(DB_get_hand($user));
802 /* in case of a wedding, everything will be delayed by an offset
805 if($gametype=="wedding")
807 $offset = DB_get_sickness_by_gameid($gameid);
808 if ($offset <0) /* not resolved */
812 /* now check if the call is allowed depending on the rule set
814 switch ($RULES["call"])
817 /* calls can be made before/while you play your card...
818 * first card = 120, second card = 90, etc.
820 if( 4-($what/30) == 12 - ($NRcards + $offset))
822 if( 4-($what/30) > 12 - ($NRcards + $offset))
826 /* you can make the first call anytime during the first trick
828 if( 27+4*($what/30) == $NRallcards + $offset*4)
830 if( 27+4*($what/30) < $NRallcards + $offset*4)
834 /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
835 * you can't skip a call though
838 /* figure out last call
840 if($oldcall!=NULL && $pcall!=NULL)
841 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
842 else if($oldcall!=NULL)
844 else if ($pcall!=NULL)
850 if( 12 == ($NRcards + $offset))
854 else if( 12 < ($NRcards + $offset))
858 else if ( 9 == ($NRcards + $offset))
860 if( ($mincall>=0 && $mincall==120) )
863 else if ( 9 < ($NRcards + $offset))
865 if( ($mincall>=0 && $mincall==120) )
868 else if ( 6 == ($NRcards + $offset))
870 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
873 else if ( 6 < ($NRcards + $offset))
875 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
878 else if ( 3 == ($NRcards + $offset))
880 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
883 else if ( 3 < ($NRcards + $offset))
885 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
888 else if ( 0 == ($NRcards + $offset))
890 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
893 else if ( 0 < ($NRcards + $offset))
895 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
904 function display_table ()
906 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
907 global $RULES,$GAME,$gametype;
909 $result = DB_query("SELECT User.fullname as name,".
910 " Hand.position as position, ".
912 " Hand.party as party, ".
913 " Hand.sickness as sickness, ".
914 " Hand.point_call, ".
915 " User.last_login, ".
920 "LEFT JOIN User ON User.id=Hand.user_id ".
921 "WHERE Hand.game_id='".$gameid."' ".
922 "ORDER BY position ASC");
924 echo "<div class=\"table\">\n".
925 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
926 while($r = DB_fetch_array($result))
937 $wins = DB_get_number_of_tricks($gameid,$pos);
938 date_default_timezone_set($defaulttimezone);
939 $lastlogin = strtotime($r[6]);
940 date_default_timezone_set($timezone);
941 $timenow = strtotime(date("Y-m-d H:i:s"));
942 $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" />";
945 echo " <div class=\"table".($pos-1)."\">\n";
948 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
949 if($vacation = check_vacation($user))
951 $start = $vacation[0];
952 $stop = substr($vacation[1],0,10);
953 $comment = $vacation[2];
955 $title = "begin: $start end: $stop $comment";
956 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
959 echo " $gravatar \n";
963 /* add hints for poverty, wedding, solo, etc */
964 if( $gametype != "solo")
965 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
966 echo " Schweinchen. <br />";
968 if($GT=="poverty" && $party=="re")
969 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
971 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
972 $cards = DB_get_all_hand($userhash);
973 $trumpNR = count_trump($cards,'all');
975 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
977 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
980 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
984 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
986 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
987 $cards = DB_get_all_hand($userhash);
988 $trumpNR = count_trump($cards,'all');
990 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
992 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
995 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
997 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
999 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1000 $cards = DB_get_all_hand($userhash);
1001 $trumpNR = count_trump($cards,'all');
1003 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
1005 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
1008 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
1010 if($GT=="wedding" && $party=="re")
1011 if($sickness=="wedding")
1012 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
1014 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
1016 if( (strpos($GT,"solo")!==false) && $party=="re")
1018 if(strpos($GT,"queen")!==false)
1019 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
1020 else if(strpos($GT,"jack")!==false)
1021 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
1022 else if(strpos($GT,"club")!==false)
1023 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
1024 else if(strpos($GT,"spade")!==false)
1025 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
1026 else if(strpos($GT,"heart")!==false)
1027 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
1028 else if(strpos($GT,"trumpless")!==false)
1029 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
1030 else if(strpos($GT,"trump")!==false)
1031 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
1034 /* add point calls */
1038 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
1040 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
1044 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
1047 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
1050 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
1053 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
1059 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
1060 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
1061 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
1064 /* show how many tricks the person made */
1068 echo "#tricks 0"; break;
1070 echo "#tricks 1"; break;
1074 echo "#tricks few"; break;
1076 echo "#tricks many"; break;
1082 echo "</div>\n"; /* end output table */
1089 function display_user_menu($id)
1091 global $WIKI,$INDEX;
1093 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1094 " LEFT JOIN Game On Hand.game_id=Game.id".
1095 " WHERE Hand.user_id='$id'".
1096 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1097 " AND ( Game.status='pre' OR Game.status='play' )".
1098 " ORDER BY Game.session" );
1101 while( $r = DB_fetch_array($result))
1105 echo "<div class=\"usermenu\">\n";
1106 echo "It's your turn in these games:<br />\n";
1110 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
1111 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1124 function generate_score_table($session)
1126 /* returns an array with N entries
1127 * $score[$i]["gameid"] = gameid
1128 * $score[$i]["players"] = array (id=>total points)
1129 * $score[$i]["points"] = points for this game
1130 * $score[$i]["solo"] = 1 or 0
1136 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1138 if($gameids == NULL)
1141 /* get player id, names... from the first game */
1143 $result = DB_query("SELECT User.id, User.fullname from Hand".
1144 " LEFT JOIN User On Hand.user_id=User.id".
1145 " WHERE Hand.game_id=".$gameids[0]);
1146 while( $r = DB_fetch_array($result))
1149 /* get points and generate table */
1150 foreach($gameids as $gameid)
1152 $re_score = DB_get_score_by_gameid($gameid);
1153 $gametype = DB_get_gametype_by_gameid($gameid);
1154 foreach($player as $id=>$points)
1156 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1158 if($gametype=="solo")
1159 $player[$id] += 3*$re_score;
1161 $player[$id] += $re_score;
1162 else if ($party == "contra")
1163 $player[$id] -= $re_score;
1165 $score[$i]['gameid'] = $gameid ;
1166 $score[$i]['players'] = $player;
1167 $score[$i]['points'] = abs($re_score);
1168 $score[$i]['solo'] = ($gametype=="solo");
1176 function generate_global_score_table()
1181 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1183 if($gameids == NULL)
1186 /* get player id, names... from the User table */
1188 $result = DB_query('SELECT User.id, User.fullname FROM User');
1190 /* save information in an array */
1191 while( $r = DB_fetch_array($result))
1192 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
1193 'response' => 0 , 'solo' => 0, 'soloavg' => 0);
1195 /* get points and generate table */
1196 foreach($gameids as $gameid)
1198 $re_score = DB_get_score_by_gameid($gameid);
1199 $gametype = DB_get_gametype_by_gameid($gameid);
1201 /* get players involved in this game */
1202 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1203 while($r = DB_fetch_array($result))
1206 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1208 if($gametype=='solo')
1209 $player[$id]['points'] += 3*$re_score;
1211 $player[$id]['points'] += $re_score;
1212 else if ($party == 'contra')
1213 $player[$id]['points'] -= $re_score;
1215 $player[$id]['nr']+=1;
1219 /* add number of active games */
1220 $result = DB_query_array_all("SELECT user_id, COUNT(*) as c " .
1222 " LEFT JOIN Game ON Game.id=game_id".
1223 " WHERE Game.status IN ('pre','play')".
1224 " GROUP BY user_id");
1226 foreach($result as $res)
1228 $player[$res[0]]['active'] = $res[1];
1231 /* response time of users*/
1232 $result = DB_query_array_all("SELECT user_id,".
1233 "IFNULL(AVG(if(P1.sequence in (2,3,4),".
1234 "-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 ".
1236 "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
1237 "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
1238 "GROUP BY user_id ");
1240 foreach($result as $res)
1242 $player[$res[0]]['response'] = $res[1];
1246 $result = DB_query_array_all("SELECT user_id as uid,".
1248 " COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
1250 " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
1251 " WHERE type='solo' AND Game.status='gameover' ".
1252 " GROUP BY user_id ");
1254 foreach($result as $res)
1256 $player[$res[0]]['solo'] = $res[1];
1257 $player[$res[0]]['soloavg'] = $res[2];
1261 /* sort everything nicely */
1265 if($a['nr']==0) return 1;
1266 if($b['nr']==0) return 1;
1268 $a=$a['points']/$a['nr'];
1269 $b=$b['points']/$b['nr'];
1273 return ($a > $b) ? -1 : 1;
1275 usort($player,'cmp');
1278 foreach($player as $pl)
1280 /* limit to players with at least 10 games */
1282 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
1283 $pl['response'],$pl['solo'],$pl['soloavg']);
1289 function format_score_table_ascii($score)
1292 if(sizeof($score)==0)
1295 /* truncate table if we have too many games */
1296 $max = sizeof($score);
1297 if($max>6) $output.=" (table truncated to last 6 games)\n";
1300 foreach($score[0]['players'] as $id=>$points)
1302 $name = DB_get_name('userid',$id); /*TODO*/
1303 $output.= " ".substr($name,0,2)." |";
1306 $output.= "------+------+------+------+------+\n";
1308 /* output score for each game */
1310 foreach($score as $game)
1313 if($i-1<$max-6) continue;
1315 foreach($game['players'] as $id=>$points)
1316 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1317 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1319 /* check for solo */
1330 function format_score_table_html($score,$userid)
1334 if(sizeof($score)==0)
1337 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1341 $header.= " <thead>\n <tr>\n";
1342 $header.= " <th> No </th>";
1343 foreach($score[0]['players'] as $id=>$points)
1345 $name = DB_get_name('userid',$id); /*TODO*/
1346 $header.= "<th> ".substr($name,0,2)." </th>";
1348 $header.="<th>P</th>\n </tr>\n </thead>\n";
1350 /* use the same as footer */
1352 $footer.= " <tfoot>\n <tr>\n";
1353 $footer.= " <td> No </td>";
1354 foreach($score[0]['players'] as $id=>$points)
1356 $name = DB_get_name('userid',$id); /*TODO*/
1357 $footer.= "<td> ".substr($name,0,2)." </td>";
1359 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1363 $body.= " <tbody>\n";
1365 foreach($score as $game)
1369 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1370 /* create link to old games only if you are logged in and its your game*/
1371 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1372 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1374 $body.=" <td>$i</td>";
1376 foreach($game['players'] as $id=>$points)
1377 $body.="<td>".$points."</td>";
1378 $body.="<td>".$game['points'];
1380 /* check for solo */
1383 $body.="</td></tr>\n";
1391 $output.=" </tbody>\n</table>\n</div>\n";
1396 function createCache($content, $cacheFile)
1398 $fp = fopen($cacheFile,"w");
1401 fwrite($fp,$content);
1405 echo "WARNING: couldn't create cache file";
1410 function getCache($cacheFile, $expireTime)
1412 if( file_exists($cacheFile) &&
1413 filemtime($cacheFile )>( time() - $expireTime ) )
1415 return file_get_contents($cacheFile);
1421 function check_vacation($userid)
1423 /* get start date */
1424 $result = DB_query_array("SELECT value FROM User_Prefs".
1425 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1427 $start = $result[0];
1432 $result = DB_query_array("SELECT value FROM User_Prefs".
1433 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1440 $result = DB_query_array("SELECT value FROM User_Prefs".
1441 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1443 $comment = $result[0];
1447 /* check if user is on vacation. TODO: use user's timezone */
1448 if( (time() - strtotime($start) >0) &&
1449 (strtotime($stop) - time() >0))
1450 return array ($start,$stop,$comment);
1455 function cancel_game($why,$gameid)
1457 $gameid = DB_quote_smart($gameid);
1459 /* update the game table */
1463 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1466 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1469 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1472 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1475 DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
1478 /* set each player to gameover */
1479 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1480 while($r = DB_fetch_array($result))
1483 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1489 function get_user_token($userid)
1494 $date = DB_get_user_creation_date($userid);
1495 $name = DB_get_name('userid',$userid);
1498 $token = md5("token".$name.$date);