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)
78 global $EmailName,$WIKI;
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, bugs, etc.: $WIKI";
96 /* send email to more than one person */
98 $header = "Hello all\n\n";
100 foreach($uid as $user)
102 $all[] = DB_get_email('userid',$user);
104 $To = implode(",",$all);
108 /* standard greeting */
109 $name = DB_get_name('userid',$uid);
110 $header = "Hello $name\n\n";
112 $To = DB_get_email('userid',$uid);
114 /* check if user wants email right away or if we should save it in
115 * the database for later delivery
118 $PREF = DB_get_PREF($uid);
119 if( $PREF['digest'] != 'digest-off' )
124 sendmail($To,$subject,$header.$message.$footer);
127 /* store email in database */
128 DB_digest_insert_email($To,$message);
132 function sendmail($To,$Subject,$message)
134 /* this function sends the mail or outputs to the screen in case of debugging */
135 global $debug,$EMAIL_REPLY;
139 if(isset($EMAIL_REPLY))
140 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
144 /* display email on screen,
147 $message = str_replace("\n","<br />\n",$message);
148 $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
149 "<a href=\"$1\">$1</a>", $message);
151 echo "<br />To: $To<br />";
153 echo $header."<br />";
154 echo "Subject: $Subject <br />$message<br />\n";
158 mail($To,$Subject,$message,$header);
160 mail($To,$Subject,$message);
166 /* returns 1 if all names passed as args are defined by a GET or POST statement,
171 $args = func_get_args();
173 foreach($args as $arg)
175 $ok = $ok * isset($_REQUEST[$arg]);
176 /*echo "$arg: ok = $ok <br />";
182 function myerror($message)
184 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
185 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
189 function pos_array($c,$arr)
206 function is_trump($c)
210 if(in_array($c,$CARDS["trump"]))
216 function is_same_suite($c1,$c2)
220 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
221 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
222 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
223 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
224 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
229 function compare_cards($a,$b,$game)
231 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
237 /* first map all cards to the odd number,
238 * this insure that the first card wins the trick
239 * if they are the same card
241 if( $a/2 - (int)($a/2) != 0.5)
243 if( $b/2 - (int)($b/2) != 0.5)
246 /* check for schweinchen and ten of hearts*/
252 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
254 if($a == 19 || $a == 20 )
256 if($b == 19 || $b == 20 )
259 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
261 if($a == 19 || $a == 20 )
263 if($b == 19 || $b == 20 )
266 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
268 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
269 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
271 if($a == 19 || $a == 20 )
273 if($b == 19 || $b == 20 )
276 /* if not, do nothing and the foxes are just handeled as normal trump */
282 /* check for ten of hearts rule */
283 if($RULES["dullen"]=="secondwins")
284 if($a==1 && $b==1) /* both 10 of hearts */
285 return 0; /* second one wins.*/
289 /* no special cases here */
293 if(is_trump($a) && is_trump($b) && $a<=$b)
295 else if(is_trump($a) && is_trump($b) )
298 { /*$a is not a trump */
302 { /* both no trump */
305 $posA = pos_array($a,$CARDS["clubs"]);
306 $posB = pos_array($b,$CARDS["clubs"]);
314 $posA = pos_array($a,$CARDS["spades"]);
315 $posB = pos_array($b,$CARDS["spades"]);
323 $posA = pos_array($a,$CARDS["hearts"]);
324 $posB = pos_array($b,$CARDS["hearts"]);
332 $posA = pos_array($a,$CARDS["diamonds"]);
333 $posB = pos_array($b,$CARDS["diamonds"]);
340 /* not the same suit and no trump: a wins */
346 function get_winner($p,$mode)
348 /* get all 4 cards played in a trick, in the order they are played */
351 $c1pos = $tmp["pos"];
355 $c2pos = $tmp["pos"];
359 $c3pos = $tmp["pos"];
363 $c4pos = $tmp["pos"];
365 /* first card is better than all the rest */
366 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
369 /* second card is better than first and better than the rest */
370 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
373 /* third card is better than first card and better than last */
374 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
375 /* if second card is better than first, third card needs to be even better */
376 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
378 /* second is worse than first, e.g. not following suite */
379 else if (compare_cards($c1,$c2,$mode) )
382 /* non of the above */
386 function count_nines($cards)
390 foreach($cards as $c)
392 if($c == "25" || $c == "26") $nines++;
393 else if($c == "33" || $c == "34") $nines++;
394 else if($c == "41" || $c == "42") $nines++;
395 else if($c == "47" || $c == "48") $nines++;
401 function check_wedding($cards)
404 if( in_array("3",$cards) && in_array("4",$cards) )
410 function count_trump($cards,$status='pregame')
416 /* count each trump, including the foxes, since this is used to determine poverty status */
417 foreach($cards as $c)
421 /* In case we really want to know the amount of trump, we can use the status variable.
422 * This is needed for example to figure out what icon to display on the table in case of
423 * trump given back in poverty */
424 if($status=='all') return $trump;
426 /* normally foxes don't count as trump, so we substract them here
427 * in case someone has schweinchen, one or two of them should count as trump
428 * though, so we need to add one trump for those cases */
431 if( in_array("19",$cards))
433 if( in_array("20",$cards) )
436 /* handle case where player has schweinchen */
437 if( in_array("19",$cards) && in_array("20",$cards) )
438 switch($RULES["schweinchen"])
441 /* add two, in case the player has both foxes (schweinchen) */
446 case "secondaftercall":
447 /* add one, in case the player has both foxes (schweinchen) */
457 function check_low_trump($cards)
461 if($RULES['lowtrump']=='none')
464 /* check if we have low trump */
467 foreach($cards as $card)
469 /* card a trump, but not a diamond? */
474 /* handle case where player has schweinchen */
475 if( in_array("19",$cards) && in_array("20",$cards) )
476 switch($RULES["schweinchen"])
480 case "secondaftercall":
490 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
498 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
499 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
500 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
501 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
502 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=21;
503 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
504 $r[ 6]=41; $r[18]=7; $r[30]=31; $r[42]=43;
505 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=20;
506 $r[ 8]=45; $r[20]=9; $r[32]=33; $r[44]=19;
507 $r[ 9]=46; $r[21]=10; $r[33]=44; $r[45]=24;
508 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
509 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
513 /* check if we can find a game were non of the player was involved and return
516 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
517 $randomnumbers = DB_get_unused_randomnumbers($userstr);
518 $randomnumbers = explode(":",$randomnumbers);
520 if(sizeof($randomnumbers)==48)
521 return $randomnumbers;
523 /* need to create new numbers */
527 /* shuffle using a better random generator than the standard one */
528 for ($i = 0; $i <48; $i++)
530 $j = @mt_rand(0, $i);
540 function display_cards($me,$myturn)
545 function have_suit($cards,$c)
550 if(in_array($c,$CARDS["trump"]))
551 $suite = $CARDS["trump"];
552 else if(in_array($c,$CARDS["clubs"]))
553 $suite = $CARDS["clubs"];
554 else if(in_array($c,$CARDS["spades"]))
555 $suite = $CARDS["spades"];
556 else if(in_array($c,$CARDS["hearts"]))
557 $suite = $CARDS["hearts"];
558 else if(in_array($c,$CARDS["diamonds"]))
559 $suite = $CARDS["diamonds"];
561 foreach($cards as $card)
563 if(in_array($card,$suite))
570 function same_type($card,$c)
575 /* figure out what kind of card c is */
576 if(in_array($c,$CARDS["trump"]))
577 $suite = $CARDS["trump"];
578 else if(in_array($c,$CARDS["clubs"]))
579 $suite = $CARDS["clubs"];
580 else if(in_array($c,$CARDS["spades"]))
581 $suite = $CARDS["spades"];
582 else if(in_array($c,$CARDS["hearts"]))
583 $suite = $CARDS["hearts"];
584 else if(in_array($c,$CARDS["diamonds"]))
585 $suite = $CARDS["diamonds"];
587 /* card is the same suid return 1 */
588 if(in_array($card,$suite))
594 function set_gametype($gametype)
608 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
609 '17','18','19','20','21','22','23','24','25','26');
610 $CARDS["diamonds"] = array();
611 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
612 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
613 $CARDS["hearts"] = array('43','44','45','46','47','48');
614 $CARDS["foxes"] = array('19','20');
615 if($RULES["dullen"]=='none')
617 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
618 '17','18','19','20','21','22','23','24','25','26');
619 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
621 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
622 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
624 /* find the fox and put them at the top of the stack */
625 foreach(array('19','20') as $fox)
628 $trump = $CARDS['trump'];
629 $key = array_keys($trump, $fox);
633 $foxa[]=$trump[$key[0]];
634 unset($trump[$key[0]]);
635 $trump = array_merge($foxa,$trump);
636 $CARDS['trump'] = $trump;
639 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
640 && $GAME['schweinchen-who'])
642 /* find the fox and put them at the top of the stack */
643 $trump = $CARDS['trump'];
644 $key = array_keys($trump, '19');
648 $foxa[]=$trump[$key[0]];
649 unset($trump[$key[0]]);
650 $trump = array_merge($foxa,$trump);
651 $CARDS['trump'] = $trump;
655 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
656 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
657 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
658 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
659 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
660 $CARDS["foxes"] = array();
663 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
664 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
665 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
666 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
667 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
668 $CARDS["foxes"] = array();
671 $CARDS["trump"] = array();
672 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
673 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
674 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
675 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
676 $CARDS["foxes"] = array();
679 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
680 '17','18','27','28','29','30','31','32','33','34');
681 $CARDS["clubs"] = array();
682 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
683 $CARDS["hearts"] = array('43','44','45','46','47','48');
684 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
685 $CARDS["foxes"] = array();
686 if($RULES["dullen"]=='none')
688 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
689 '17','18','27','28','29','30','31','32','33','34');
690 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
694 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
695 '17','18','35','36','37','38','39','40','41','42');
696 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
697 $CARDS["spades"] = array();
698 $CARDS["hearts"] = array('43','44','45','46','47','48');
699 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
700 $CARDS["foxes"] = array();
701 if($RULES["dullen"]=='none')
703 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
704 '17','18','35','36','37','38','39','40','41','42');
705 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
709 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
710 '17','18','43','44','45','46','47','48');
711 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
712 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
713 $CARDS["hearts"] = array();
714 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
715 $CARDS["foxes"] = array();
716 if($RULES["dullen"]=='none')
718 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
719 '17','18','43','44','1','2','45','46','47','48');
725 function mysort($cards,$gametype)
728 if(isset($PREF['sorting']))
729 if($PREF['sorting']=='high-low')
730 usort ( $cards, 'sort_comp_high_low' );
732 usort ( $cards, 'sort_comp_low_high' );
734 usort ( $cards, 'sort_comp_high_low' );
738 function sort_comp_high_low($a,$b)
743 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
744 $CARDS['hearts'],$CARDS['spades']);
746 return pos_array($a,$ALL)-pos_array($b,$ALL);
749 function sort_comp_low_high($a,$b)
754 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
755 $CARDS['hearts'],$CARDS['spades']);
757 return -pos_array($a,$ALL)+pos_array($b,$ALL);
760 function can_call($what,$hash)
762 /* figure out if a person can make a call:
763 $what in 0,30,60,90,120 = points of the call
764 $hash = the hash of the person who wants to make the call
767 0 can't make that call
769 2 can make the call, but this is the last chance to do so...
774 /* get some information
776 $gameid = DB_get_gameid_by_hash($hash);
777 $gametype = DB_get_gametype_by_gameid($gameid);
778 $oldcall = DB_get_call_by_hash($hash); /* did the person already made a call? */
779 $pcall = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
782 /* you're call must be better than the one you or your partner already made
784 if( ($pcall!=NULL && ($what >= $pcall))
785 || ($oldcall!=NULL && ($what >=$oldcall)) )
790 /* for some rules we need to know how many cards people have
792 $NRcards = count(DB_get_hand($hash));
797 $user = DB_get_hash_from_game_and_pos($gameid,$i);
798 $NRallcards += count(DB_get_hand($user));
801 /* in case of a wedding, everything will be delayed by an offset
804 if($gametype=="wedding")
806 $offset = DB_get_sickness_by_gameid($gameid);
807 if ($offset <0) /* not resolved */
811 /* now check if the call is allowed depending on the rule set
813 switch ($RULES["call"])
816 /* calls can be made before/while you play your card...
817 * first card = 120, second card = 90, etc.
819 if( 4-($what/30) == 12 - ($NRcards + $offset))
821 if( 4-($what/30) > 12 - ($NRcards + $offset))
825 /* you can make the first call anytime during the first trick
827 if( 27+4*($what/30) == $NRallcards + $offset*4)
829 if( 27+4*($what/30) < $NRallcards + $offset*4)
833 /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
834 * you can't skip a call though
837 /* figure out last call
839 if($oldcall!=NULL && $pcall!=NULL)
840 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
841 else if($oldcall!=NULL)
843 else if ($pcall!=NULL)
849 if( 12 == ($NRcards + $offset))
853 else if( 12 < ($NRcards + $offset))
857 else if ( 9 == ($NRcards + $offset))
859 if( ($mincall>=0 && $mincall==120) )
862 else if ( 9 < ($NRcards + $offset))
864 if( ($mincall>=0 && $mincall==120) )
867 else if ( 6 == ($NRcards + $offset))
869 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
872 else if ( 6 < ($NRcards + $offset))
874 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
877 else if ( 3 == ($NRcards + $offset))
879 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
882 else if ( 3 < ($NRcards + $offset))
884 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
887 else if ( 0 == ($NRcards + $offset))
889 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
892 else if ( 0 < ($NRcards + $offset))
894 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
903 function display_table ()
905 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
906 global $RULES,$GAME,$gametype;
908 $result = DB_query("SELECT User.fullname as name,".
909 " Hand.position as position, ".
911 " Hand.party as party, ".
912 " Hand.sickness as sickness, ".
913 " Hand.point_call, ".
914 " User.last_login, ".
919 "LEFT JOIN User ON User.id=Hand.user_id ".
920 "WHERE Hand.game_id='".$gameid."' ".
921 "ORDER BY position ASC");
923 echo "<div class=\"table\">\n".
924 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
925 while($r = DB_fetch_array($result))
936 $wins = DB_get_number_of_tricks($gameid,$pos);
937 date_default_timezone_set($defaulttimezone);
938 $lastlogin = strtotime($r[6]);
939 date_default_timezone_set($timezone);
940 $timenow = strtotime(date("Y-m-d H:i:s"));
941 $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" alt=\"$name's gravatar\" />";
944 echo " <div class=\"table".($pos-1)."\">\n";
947 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
948 if($vacation = check_vacation($user))
950 $start = $vacation[0];
951 $stop = substr($vacation[1],0,10);
952 $comment = $vacation[2];
954 $title = "begin: $start end: $stop $comment";
955 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
958 echo " $gravatar \n";
962 /* add hints for poverty, wedding, solo, etc */
963 if( $gametype != "solo")
964 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
965 echo " Schweinchen. <br />";
967 if($GT=="poverty" && $party=="re")
968 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
970 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
971 $cards = DB_get_all_hand($userhash);
972 $trumpNR = count_trump($cards,'all');
974 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty - trump back\" title=\"poverty - trump back\" />";
976 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty - no trump back\" title=\"poverty - no trump back\" />";
979 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty partner\" title=\"poverty partner\" />";
983 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
985 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
986 $cards = DB_get_all_hand($userhash);
987 $trumpNR = count_trump($cards,'all');
989 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
991 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
994 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
996 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
998 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
999 $cards = DB_get_all_hand($userhash);
1000 $trumpNR = count_trump($cards,'all');
1002 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
1004 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
1007 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
1009 if($GT=="wedding" && $party=="re")
1010 if($sickness=="wedding")
1011 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
1013 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
1015 if( (strpos($GT,"solo")!==false) && $party=="re")
1017 if(strpos($GT,"queen")!==false)
1018 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
1019 else if(strpos($GT,"jack")!==false)
1020 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
1021 else if(strpos($GT,"club")!==false)
1022 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
1023 else if(strpos($GT,"spade")!==false)
1024 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
1025 else if(strpos($GT,"heart")!==false)
1026 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
1027 else if(strpos($GT,"trumpless")!==false)
1028 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
1029 else if(strpos($GT,"trump")!==false)
1030 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
1033 /* add point calls */
1037 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
1039 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
1043 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
1046 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
1049 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
1052 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
1058 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
1059 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
1060 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
1063 /* show how many tricks the person made */
1067 echo "#tricks 0"; break;
1069 echo "#tricks 1"; break;
1073 echo "#tricks few"; break;
1075 echo "#tricks many"; break;
1081 echo "</div>\n"; /* end output table */
1088 function display_user_menu($id)
1090 global $WIKI,$INDEX;
1092 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1093 " LEFT JOIN Game On Hand.game_id=Game.id".
1094 " WHERE Hand.user_id='$id'".
1095 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1096 " AND ( Game.status='pre' OR Game.status='play' )".
1097 " ORDER BY Game.session" );
1100 while( $r = DB_fetch_array($result))
1104 echo "<div class=\"usermenu\">\n";
1105 echo _('It\'s your turn in these games').":<br />\n";
1109 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
1110 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1123 function generate_score_table($session)
1125 /* returns an array with N entries
1126 * $score[$i]["gameid"] = gameid
1127 * $score[$i]["players"] = array (id=>total points)
1128 * $score[$i]["points"] = points for this game
1129 * $score[$i]["solo"] = 1 or 0
1135 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1137 if($gameids == NULL)
1140 /* get player id, names... from the first game */
1142 $result = DB_query("SELECT User.id, User.fullname from Hand".
1143 " LEFT JOIN User On Hand.user_id=User.id".
1144 " WHERE Hand.game_id=".$gameids[0]);
1145 while( $r = DB_fetch_array($result))
1148 /* get points and generate table */
1149 foreach($gameids as $gameid)
1151 $re_score = DB_get_score_by_gameid($gameid);
1152 $gametype = DB_get_gametype_by_gameid($gameid);
1153 foreach($player as $id=>$points)
1155 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1157 if($gametype=="solo")
1158 $player[$id] += 3*$re_score;
1160 $player[$id] += $re_score;
1161 else if ($party == "contra")
1162 $player[$id] -= $re_score;
1164 $score[$i]['gameid'] = $gameid ;
1165 $score[$i]['players'] = $player;
1166 $score[$i]['points'] = abs($re_score);
1167 $score[$i]['solo'] = ($gametype=="solo");
1175 function generate_global_score_table()
1180 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1182 if($gameids == NULL)
1185 /* get player id, names... from the User table */
1187 $result = DB_query('SELECT User.id, User.fullname FROM User');
1189 /* save information in an array */
1190 while( $r = DB_fetch_array($result))
1191 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
1192 'response' => 0 , 'solo' => 0, 'soloavg' => 0);
1194 /* get points and generate table */
1195 foreach($gameids as $gameid)
1197 $re_score = DB_get_score_by_gameid($gameid);
1198 $gametype = DB_get_gametype_by_gameid($gameid);
1200 /* get players involved in this game */
1201 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1202 while($r = DB_fetch_array($result))
1205 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1207 if($gametype=='solo')
1208 $player[$id]['points'] += 3*$re_score;
1210 $player[$id]['points'] += $re_score;
1211 else if ($party == 'contra')
1212 $player[$id]['points'] -= $re_score;
1214 $player[$id]['nr']+=1;
1218 /* add number of active games */
1219 $result = DB_query_array_all("SELECT user_id, COUNT(*) as c " .
1221 " LEFT JOIN Game ON Game.id=game_id".
1222 " WHERE Game.status IN ('pre','play')".
1223 " GROUP BY user_id");
1225 foreach($result as $res)
1227 $player[$res[0]]['active'] = $res[1];
1230 /* response time of users*/
1231 $result = DB_query_array_all("SELECT user_id,".
1232 "IFNULL(AVG(if(P1.sequence in (2,3,4),".
1233 "-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 ".
1235 "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
1236 "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
1237 "GROUP BY user_id ");
1239 foreach($result as $res)
1241 $player[$res[0]]['response'] = $res[1];
1245 $result = DB_query_array_all("SELECT user_id as uid,".
1247 " COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
1249 " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
1250 " WHERE type='solo' AND Game.status='gameover' ".
1251 " GROUP BY user_id ");
1253 foreach($result as $res)
1255 $player[$res[0]]['solo'] = $res[1];
1256 $player[$res[0]]['soloavg'] = $res[2];
1260 /* sort everything nicely */
1264 if($a['nr']==0) return 1;
1265 if($b['nr']==0) return 1;
1267 $a=$a['points']/$a['nr'];
1268 $b=$b['points']/$b['nr'];
1272 return ($a > $b) ? -1 : 1;
1274 usort($player,'cmp');
1277 foreach($player as $pl)
1279 /* limit to players with at least 10 games */
1281 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
1282 $pl['response'],$pl['solo'],$pl['soloavg']);
1288 function format_score_table_ascii($score)
1291 if(sizeof($score)==0)
1294 /* truncate table if we have too many games */
1295 $max = sizeof($score);
1296 if($max>6) $output.=" (table truncated to last 6 games)\n";
1299 foreach($score[0]['players'] as $id=>$points)
1301 $name = DB_get_name('userid',$id); /*TODO*/
1302 $output.= " ".substr($name,0,2)." |";
1305 $output.= "------+------+------+------+------+\n";
1307 /* output score for each game */
1309 foreach($score as $game)
1312 if($i-1<$max-6) continue;
1314 foreach($game['players'] as $id=>$points)
1315 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1316 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1318 /* check for solo */
1329 function format_score_table_html($score,$userid)
1333 if(sizeof($score)==0)
1336 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1340 $header.= " <thead>\n <tr>\n";
1341 $header.= " <th> No </th>";
1342 foreach($score[0]['players'] as $id=>$points)
1344 $name = DB_get_name('userid',$id); /*TODO*/
1345 $header.= "<th> ".substr($name,0,2)." </th>";
1347 $header.="<th>P</th>\n </tr>\n </thead>\n";
1349 /* use the same as footer */
1351 $footer.= " <tfoot>\n <tr>\n";
1352 $footer.= " <td> No </td>";
1353 foreach($score[0]['players'] as $id=>$points)
1355 $name = DB_get_name('userid',$id); /*TODO*/
1356 $footer.= "<td> ".substr($name,0,2)." </td>";
1358 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1362 $body.= " <tbody>\n";
1364 foreach($score as $game)
1368 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1369 /* create link to old games only if you are logged in and its your game*/
1370 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1371 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1373 $body.=" <td>$i</td>";
1375 foreach($game['players'] as $id=>$points)
1376 $body.="<td>".$points."</td>";
1377 $body.="<td>".$game['points'];
1379 /* check for solo */
1382 $body.="</td></tr>\n";
1390 $output.=" </tbody>\n</table>\n</div>\n";
1395 function createCache($content, $cacheFile)
1397 $fp = fopen($cacheFile,"w");
1400 fwrite($fp,$content);
1404 echo "WARNING: couldn't create cache file";
1409 function getCache($cacheFile, $expireTime)
1411 if( file_exists($cacheFile) &&
1412 filemtime($cacheFile )>( time() - $expireTime ) )
1414 return file_get_contents($cacheFile);
1420 function check_vacation($userid)
1422 /* get start date */
1423 $result = DB_query_array("SELECT value FROM User_Prefs".
1424 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1426 $start = $result[0];
1431 $result = DB_query_array("SELECT value FROM User_Prefs".
1432 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1439 $result = DB_query_array("SELECT value FROM User_Prefs".
1440 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1442 $comment = $result[0];
1446 /* check if user is on vacation. TODO: use user's timezone */
1447 if( (time() - strtotime($start) >0) &&
1448 (strtotime($stop) - time() >0))
1449 return array ($start,$stop,$comment);
1454 function cancel_game($why,$gameid)
1456 $gameid = DB_quote_smart($gameid);
1458 /* update the game table */
1462 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1465 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1468 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1471 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1474 DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
1477 /* set each player to gameover */
1478 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1479 while($r = DB_fetch_array($result))
1482 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1488 function get_user_token($userid)
1493 $date = DB_get_user_creation_date($userid);
1494 $name = DB_get_name('userid',$userid);
1497 $token = md5("token".$name.$date);