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 /* define possible status for email subsjects */
77 define("GAME_CANCELED", 0);
78 define("GAME_CANCELED_POVERTY", 1);
79 define("GAME_CANCELED_TIMEOUT", 2);
80 define("GAME_YOUR_TURN", 3);
81 define("GAME_READY", 4);
82 define("GAME_POVERTY", 5);
83 define("GAME_DPOVERTY", 6);
84 define("GAME_OVER", 7);
85 define("GAME_RECOVERY", 8);
86 define("GAME_REMINDER", 9);
87 define("GAME_NEW", 10);
89 function mymail($uid,$gameid=0,$type,$message)
91 global $EmailName,$WIKI;
93 /* do we send the email right away or save it in the database? */
96 /* add standard header and footer */
97 $subject = "$EmailName ";
99 $game = DB_format_gameid($gameid);
106 $subject.="Game $game canceled";
108 case GAME_CANCELED_POVERTY:
109 $subject.="Game $game canceled (poverty not resolved)";
111 case GAME_CANCELED_TIMEOUT:
112 $subject.="Game $game canceled (timed out)";
115 $subject.="A card has been played in game $game";
118 $subject.="Ready, set, go... (game $game)";
121 $subject.="Poverty (game $game)";
124 $subject.="Double poverty (game $game)";
127 $subject.="Game over (game $game)";
130 $subject.="Recovery";
133 $subject.="Reminder: game $game it's your turn";
136 $subject.="You are invited to a game of DoKo (game $game)";
139 $subject.="Problem with email, contact admin (errorcode $gameid)";
142 /* standard goodbye */
143 $footer = "\nHave a nice day\n".
144 " your E-Doko service department\n\n".
146 "You can change your mail delivery mode in the preference menu.\n".
147 'web: http://doko.nubati.net '.
148 "help, bugs, etc.: $WIKI";
152 /* send email to more than one person */
154 $header = "Hello all\n\n";
156 foreach($uid as $user)
158 $all[] = DB_get_email('userid',$user);
160 $To = implode(",",$all);
164 /* standard greeting */
165 $name = DB_get_name('userid',$uid);
166 $header = "Hello $name\n\n";
168 $To = DB_get_email('userid',$uid);
170 /* check if user wants email right away or if we should save it in
171 * the database for later delivery
174 $PREF = DB_get_PREF($uid);
175 if( $PREF['digest'] != 'digest-off' )
180 sendmail($To,$subject,$header.$message.$footer);
183 /* store email in database */
184 DB_digest_insert_email($To,$message);
188 function sendmail($To,$Subject,$message)
190 /* this function sends the mail or outputs to the screen in case of debugging */
191 global $debug,$EMAIL_REPLY;
195 if(isset($EMAIL_REPLY))
196 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
200 /* display email on screen,
203 $message = str_replace("\n","<br />\n",$message);
204 $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
205 "<a href=\"$1\">$1</a>", $message);
207 echo "<br />To: $To<br />";
209 echo $header."<br />";
210 echo "Subject: $Subject <br />$message<br />\n";
214 mail($To,$Subject,$message,$header);
216 mail($To,$Subject,$message);
222 /* returns 1 if all names passed as args are defined by a GET or POST statement,
227 $args = func_get_args();
229 foreach($args as $arg)
231 $ok = $ok * isset($_REQUEST[$arg]);
232 /*echo "$arg: ok = $ok <br />";
238 function myerror($message)
240 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
241 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
245 function pos_array($c,$arr)
262 function is_trump($c)
266 if(in_array($c,$CARDS["trump"]))
272 function is_same_suite($c1,$c2)
276 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
277 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
278 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
279 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
280 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
285 function compare_cards($a,$b,$game)
287 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
293 /* first map all cards to the odd number,
294 * this insure that the first card wins the trick
295 * if they are the same card
297 if( $a/2 - (int)($a/2) != 0.5)
299 if( $b/2 - (int)($b/2) != 0.5)
302 /* check for schweinchen and ten of hearts*/
308 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
310 if($a == 19 || $a == 20 )
312 if($b == 19 || $b == 20 )
315 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
317 if($a == 19 || $a == 20 )
319 if($b == 19 || $b == 20 )
322 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
324 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
325 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
327 if($a == 19 || $a == 20 )
329 if($b == 19 || $b == 20 )
332 /* if not, do nothing and the foxes are just handeled as normal trump */
338 /* check for ten of hearts rule */
339 if($RULES["dullen"]=="secondwins")
340 if($a==1 && $b==1) /* both 10 of hearts */
341 return 0; /* second one wins.*/
345 /* no special cases here */
349 if(is_trump($a) && is_trump($b) && $a<=$b)
351 else if(is_trump($a) && is_trump($b) )
354 { /*$a is not a trump */
358 { /* both no trump */
361 $posA = pos_array($a,$CARDS["clubs"]);
362 $posB = pos_array($b,$CARDS["clubs"]);
370 $posA = pos_array($a,$CARDS["spades"]);
371 $posB = pos_array($b,$CARDS["spades"]);
379 $posA = pos_array($a,$CARDS["hearts"]);
380 $posB = pos_array($b,$CARDS["hearts"]);
388 $posA = pos_array($a,$CARDS["diamonds"]);
389 $posB = pos_array($b,$CARDS["diamonds"]);
396 /* not the same suit and no trump: a wins */
402 function get_winner($p,$mode)
404 /* get all 4 cards played in a trick, in the order they are played */
407 $c1pos = $tmp["pos"];
411 $c2pos = $tmp["pos"];
415 $c3pos = $tmp["pos"];
419 $c4pos = $tmp["pos"];
421 /* first card is better than all the rest */
422 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
425 /* second card is better than first and better than the rest */
426 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
429 /* third card is better than first card and better than last */
430 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
431 /* if second card is better than first, third card needs to be even better */
432 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
434 /* second is worse than first, e.g. not following suite */
435 else if (compare_cards($c1,$c2,$mode) )
438 /* non of the above */
442 function count_nines($cards)
446 foreach($cards as $c)
448 if($c == "25" || $c == "26") $nines++;
449 else if($c == "33" || $c == "34") $nines++;
450 else if($c == "41" || $c == "42") $nines++;
451 else if($c == "47" || $c == "48") $nines++;
457 function check_wedding($cards)
460 if( in_array("3",$cards) && in_array("4",$cards) )
466 function count_trump($cards,$status='pregame')
472 /* count each trump, including the foxes, since this is used to determine poverty status */
473 foreach($cards as $c)
477 /* In case we really want to know the amount of trump, we can use the status variable.
478 * This is needed for example to figure out what icon to display on the table in case of
479 * trump given back in poverty */
480 if($status=='all') return $trump;
482 /* normally foxes don't count as trump, so we substract them here
483 * in case someone has schweinchen, one or two of them should count as trump
484 * though, so we need to add one trump for those cases */
487 if( in_array("19",$cards))
489 if( in_array("20",$cards) )
492 /* handle case where player has schweinchen */
493 if( in_array("19",$cards) && in_array("20",$cards) )
494 switch($RULES["schweinchen"])
497 /* add two, in case the player has both foxes (schweinchen) */
502 case "secondaftercall":
503 /* add one, in case the player has both foxes (schweinchen) */
513 function check_low_trump($cards)
517 if($RULES['lowtrump']=='none')
520 /* check if we have low trump */
523 foreach($cards as $card)
525 /* card a trump, but not a diamond? */
530 /* handle case where player has schweinchen */
531 if( in_array("19",$cards) && in_array("20",$cards) )
532 switch($RULES["schweinchen"])
536 case "secondaftercall":
546 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
554 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
555 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
556 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
557 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
558 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=21;
559 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
560 $r[ 6]=41; $r[18]=7; $r[30]=31; $r[42]=43;
561 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=20;
562 $r[ 8]=45; $r[20]=9; $r[32]=33; $r[44]=19;
563 $r[ 9]=46; $r[21]=10; $r[33]=44; $r[45]=24;
564 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
565 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
569 /* check if we can find a game were non of the player was involved and return
572 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
573 $randomnumbers = DB_get_unused_randomnumbers($userstr);
574 $randomnumbers = explode(":",$randomnumbers);
576 if(sizeof($randomnumbers)==48)
577 return $randomnumbers;
579 /* need to create new numbers */
583 /* shuffle using a better random generator than the standard one */
584 for ($i = 0; $i <48; $i++)
586 $j = @mt_rand(0, $i);
596 function display_cards($me,$myturn)
601 function have_suit($cards,$c)
606 if(in_array($c,$CARDS["trump"]))
607 $suite = $CARDS["trump"];
608 else if(in_array($c,$CARDS["clubs"]))
609 $suite = $CARDS["clubs"];
610 else if(in_array($c,$CARDS["spades"]))
611 $suite = $CARDS["spades"];
612 else if(in_array($c,$CARDS["hearts"]))
613 $suite = $CARDS["hearts"];
614 else if(in_array($c,$CARDS["diamonds"]))
615 $suite = $CARDS["diamonds"];
617 foreach($cards as $card)
619 if(in_array($card,$suite))
626 function same_type($card,$c)
631 /* figure out what kind of card c is */
632 if(in_array($c,$CARDS["trump"]))
633 $suite = $CARDS["trump"];
634 else if(in_array($c,$CARDS["clubs"]))
635 $suite = $CARDS["clubs"];
636 else if(in_array($c,$CARDS["spades"]))
637 $suite = $CARDS["spades"];
638 else if(in_array($c,$CARDS["hearts"]))
639 $suite = $CARDS["hearts"];
640 else if(in_array($c,$CARDS["diamonds"]))
641 $suite = $CARDS["diamonds"];
643 /* card is the same suid return 1 */
644 if(in_array($card,$suite))
650 function set_gametype($gametype)
664 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
665 '17','18','19','20','21','22','23','24','25','26');
666 $CARDS["diamonds"] = array();
667 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
668 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
669 $CARDS["hearts"] = array('43','44','45','46','47','48');
670 $CARDS["foxes"] = array('19','20');
671 if($RULES["dullen"]=='none')
673 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
674 '17','18','19','20','21','22','23','24','25','26');
675 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
677 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
678 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
680 /* find the fox and put them at the top of the stack */
681 foreach(array('19','20') as $fox)
684 $trump = $CARDS['trump'];
685 $key = array_keys($trump, $fox);
689 $foxa[]=$trump[$key[0]];
690 unset($trump[$key[0]]);
691 $trump = array_merge($foxa,$trump);
692 $CARDS['trump'] = $trump;
695 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
696 && $GAME['schweinchen-who'])
698 /* find the fox and put them at the top of the stack */
699 $trump = $CARDS['trump'];
700 $key = array_keys($trump, '19');
704 $foxa[]=$trump[$key[0]];
705 unset($trump[$key[0]]);
706 $trump = array_merge($foxa,$trump);
707 $CARDS['trump'] = $trump;
711 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
712 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
713 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
714 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
715 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
716 $CARDS["foxes"] = array();
719 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
720 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
721 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
722 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
723 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
724 $CARDS["foxes"] = array();
727 $CARDS["trump"] = array();
728 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
729 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
730 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
731 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
732 $CARDS["foxes"] = array();
735 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
736 '17','18','27','28','29','30','31','32','33','34');
737 $CARDS["clubs"] = array();
738 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
739 $CARDS["hearts"] = array('43','44','45','46','47','48');
740 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
741 $CARDS["foxes"] = array();
742 if($RULES["dullen"]=='none')
744 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
745 '17','18','27','28','29','30','31','32','33','34');
746 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
750 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
751 '17','18','35','36','37','38','39','40','41','42');
752 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
753 $CARDS["spades"] = array();
754 $CARDS["hearts"] = array('43','44','45','46','47','48');
755 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
756 $CARDS["foxes"] = array();
757 if($RULES["dullen"]=='none')
759 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
760 '17','18','35','36','37','38','39','40','41','42');
761 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
765 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
766 '17','18','43','44','45','46','47','48');
767 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
768 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
769 $CARDS["hearts"] = array();
770 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
771 $CARDS["foxes"] = array();
772 if($RULES["dullen"]=='none')
774 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
775 '17','18','43','44','1','2','45','46','47','48');
781 function mysort($cards,$gametype)
784 if(isset($PREF['sorting']))
785 if($PREF['sorting']=='high-low')
786 usort ( $cards, 'sort_comp_high_low' );
788 usort ( $cards, 'sort_comp_low_high' );
790 usort ( $cards, 'sort_comp_high_low' );
794 function sort_comp_high_low($a,$b)
799 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
800 $CARDS['hearts'],$CARDS['spades']);
802 return pos_array($a,$ALL)-pos_array($b,$ALL);
805 function sort_comp_low_high($a,$b)
810 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
811 $CARDS['hearts'],$CARDS['spades']);
813 return -pos_array($a,$ALL)+pos_array($b,$ALL);
816 function can_call($what,$hash)
818 /* figure out if a person can make a call:
819 $what in 0,30,60,90,120 = points of the call
820 $hash = the hash of the person who wants to make the call
823 0 can't make that call
825 2 can make the call, but this is the last chance to do so...
830 /* get some information
832 $gameid = DB_get_gameid_by_hash($hash);
833 $gametype = DB_get_gametype_by_gameid($gameid);
834 $oldcall = DB_get_call_by_hash($hash); /* did the person already made a call? */
835 $pcall = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
838 /* you're call must be better than the one you or your partner already made
840 if( ($pcall!=NULL && ($what >= $pcall))
841 || ($oldcall!=NULL && ($what >=$oldcall)) )
846 /* for some rules we need to know how many cards people have
848 $NRcards = count(DB_get_hand($hash));
853 $user = DB_get_hash_from_game_and_pos($gameid,$i);
854 $NRallcards += count(DB_get_hand($user));
857 /* in case of a wedding, everything will be delayed by an offset
860 if($gametype=="wedding")
862 $offset = DB_get_sickness_by_gameid($gameid);
863 if ($offset <0) /* not resolved */
867 /* now check if the call is allowed depending on the rule set
869 switch ($RULES["call"])
872 /* calls can be made before/while you play your card...
873 * first card = 120, second card = 90, etc.
875 if( 4-($what/30) == 12 - ($NRcards + $offset))
877 if( 4-($what/30) > 12 - ($NRcards + $offset))
881 /* you can make the first call anytime during the first trick
883 if( 27+4*($what/30) == $NRallcards + $offset*4)
885 if( 27+4*($what/30) < $NRallcards + $offset*4)
889 /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
890 * you can't skip a call though
893 /* figure out last call
895 if($oldcall!=NULL && $pcall!=NULL)
896 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
897 else if($oldcall!=NULL)
899 else if ($pcall!=NULL)
905 if( 12 == ($NRcards + $offset))
909 else if( 12 < ($NRcards + $offset))
913 else if ( 9 == ($NRcards + $offset))
915 if( ($mincall>=0 && $mincall<=120 && $what<=90 ) )
918 else if ( 9 < ($NRcards + $offset))
920 if( ($mincall>=0 && $mincall<=120 && $what<=90 ) )
923 else if ( 6 == ($NRcards + $offset))
925 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
928 else if ( 6 < ($NRcards + $offset))
930 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
933 else if ( 3 == ($NRcards + $offset))
935 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
938 else if ( 3 < ($NRcards + $offset))
940 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
943 else if ( 0 == ($NRcards + $offset))
945 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
948 else if ( 0 < ($NRcards + $offset))
950 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
959 function display_table ()
961 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
962 global $RULES,$GAME,$gametype;
964 $result = DB_query("SELECT User.fullname as name,".
965 " Hand.position as position, ".
967 " Hand.party as party, ".
968 " Hand.sickness as sickness, ".
969 " Hand.point_call, ".
970 " User.last_login, ".
975 "LEFT JOIN User ON User.id=Hand.user_id ".
976 "WHERE Hand.game_id='".$gameid."' ".
977 "ORDER BY position ASC");
979 echo "<div class=\"table\">\n".
980 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
981 while($r = DB_fetch_array($result))
992 $wins = DB_get_number_of_tricks($gameid,$pos);
993 date_default_timezone_set($defaulttimezone);
994 $lastlogin = strtotime($r[6]);
995 date_default_timezone_set($timezone);
996 $timenow = strtotime(date("Y-m-d H:i:s"));
997 $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" alt=\"$name's gravatar\" />";
1000 echo " <div class=\"table".($pos-1)."\">\n";
1003 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
1004 if($vacation = check_vacation($user))
1006 $start = $vacation[0];
1007 $stop = substr($vacation[1],0,10);
1008 $comment = $vacation[2];
1010 $title = "begin: $start end: $stop $comment";
1011 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
1014 echo " $gravatar \n";
1018 /* add hints for poverty, wedding, solo, etc */
1019 if( $gametype != "solo")
1020 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
1021 echo " Schweinchen. <br />";
1023 if($GT=="poverty" && $party=="re")
1024 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1026 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1027 $cards = DB_get_all_hand($userhash);
1028 $trumpNR = count_trump($cards,'all');
1030 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty - trump back\" title=\"poverty - trump back\" />";
1032 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty - no trump back\" title=\"poverty - no trump back\" />";
1035 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty partner\" title=\"poverty partner\" />";
1039 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1041 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1042 $cards = DB_get_all_hand($userhash);
1043 $trumpNR = count_trump($cards,'all');
1045 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
1047 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
1050 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
1052 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1054 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1055 $cards = DB_get_all_hand($userhash);
1056 $trumpNR = count_trump($cards,'all');
1058 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
1060 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
1063 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
1065 if($GT=="wedding" && $party=="re")
1066 if($sickness=="wedding")
1067 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
1069 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
1071 if( (strpos($GT,"solo")!==false) && $party=="re")
1073 if(strpos($GT,"queen")!==false)
1074 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
1075 else if(strpos($GT,"jack")!==false)
1076 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
1077 else if(strpos($GT,"club")!==false)
1078 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
1079 else if(strpos($GT,"spade")!==false)
1080 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
1081 else if(strpos($GT,"heart")!==false)
1082 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
1083 else if(strpos($GT,"trumpless")!==false)
1084 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
1085 else if(strpos($GT,"trump")!==false)
1086 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
1089 /* add point calls */
1093 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
1095 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
1099 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
1102 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
1105 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
1108 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
1114 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
1115 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
1116 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
1119 /* show how many tricks the person made */
1123 echo "#tricks 0"; break;
1125 echo "#tricks 1"; break;
1129 echo "#tricks few"; break;
1131 echo "#tricks many"; break;
1137 echo "</div>\n"; /* end output table */
1144 function display_user_menu($id)
1146 global $WIKI,$INDEX;
1148 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1149 " LEFT JOIN Game On Hand.game_id=Game.id".
1150 " WHERE Hand.user_id='$id'".
1151 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1152 " AND ( Game.status='pre' OR Game.status='play' )".
1153 " ORDER BY Game.session" );
1156 while( $r = DB_fetch_array($result))
1160 echo "<div class=\"usermenu\">\n";
1161 echo _('It\'s your turn in these games').":<br />\n";
1165 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
1166 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1179 function generate_score_table($session)
1181 /* returns an array with N entries
1182 * $score[$i]["gameid"] = gameid
1183 * $score[$i]["players"] = array (id=>total points)
1184 * $score[$i]["points"] = points for this game
1185 * $score[$i]["solo"] = 1 or 0
1191 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1193 if($gameids == NULL)
1196 /* get player id, names... from the first game */
1198 $result = DB_query("SELECT User.id, User.fullname from Hand".
1199 " LEFT JOIN User On Hand.user_id=User.id".
1200 " WHERE Hand.game_id=".$gameids[0]);
1201 while( $r = DB_fetch_array($result))
1204 /* get points and generate table */
1205 foreach($gameids as $gameid)
1207 $re_score = DB_get_score_by_gameid($gameid);
1208 $gametype = DB_get_gametype_by_gameid($gameid);
1209 foreach($player as $id=>$points)
1211 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1213 if($gametype=="solo")
1214 $player[$id] += 3*$re_score;
1216 $player[$id] += $re_score;
1217 else if ($party == "contra")
1218 $player[$id] -= $re_score;
1220 $score[$i]['gameid'] = $gameid ;
1221 $score[$i]['players'] = $player;
1222 $score[$i]['points'] = abs($re_score);
1223 $score[$i]['solo'] = ($gametype=="solo");
1231 function generate_global_score_table()
1236 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1238 if($gameids == NULL)
1241 /* get player id, names... from the User table */
1243 $result = DB_query('SELECT User.id, User.fullname FROM User');
1245 /* save information in an array */
1246 while( $r = DB_fetch_array($result))
1247 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
1248 'response' => 0 , 'solo' => 0, 'soloavg' => 0);
1250 /* get points and generate table */
1251 foreach($gameids as $gameid)
1253 $re_score = DB_get_score_by_gameid($gameid);
1254 $gametype = DB_get_gametype_by_gameid($gameid);
1256 /* get players involved in this game */
1257 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1258 while($r = DB_fetch_array($result))
1261 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1263 if($gametype=='solo')
1264 $player[$id]['points'] += 3*$re_score;
1266 $player[$id]['points'] += $re_score;
1267 else if ($party == 'contra')
1268 $player[$id]['points'] -= $re_score;
1270 $player[$id]['nr']+=1;
1274 /* add number of active games */
1275 $result = DB_query_array_all("SELECT user_id, COUNT(*) as c " .
1277 " LEFT JOIN Game ON Game.id=game_id".
1278 " WHERE Game.status IN ('pre','play')".
1279 " GROUP BY user_id");
1281 foreach($result as $res)
1283 $player[$res[0]]['active'] = $res[1];
1286 /* response time of users*/
1287 $result = DB_query_array_all("SELECT user_id,".
1288 "IFNULL(AVG(if(P1.sequence in (2,3,4),".
1289 "-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 ".
1291 "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
1292 "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
1293 "GROUP BY user_id ");
1295 foreach($result as $res)
1297 $player[$res[0]]['response'] = $res[1];
1301 $result = DB_query_array_all("SELECT user_id as uid,".
1303 " COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
1305 " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
1306 " WHERE type='solo' AND Game.status='gameover' ".
1307 " GROUP BY user_id ");
1309 foreach($result as $res)
1311 $player[$res[0]]['solo'] = $res[1];
1312 $player[$res[0]]['soloavg'] = $res[2];
1316 /* sort everything nicely */
1320 if($a['nr']==0) return 1;
1321 if($b['nr']==0) return 1;
1323 $a=$a['points']/$a['nr'];
1324 $b=$b['points']/$b['nr'];
1328 return ($a > $b) ? -1 : 1;
1330 usort($player,'cmp');
1333 foreach($player as $pl)
1335 /* limit to players with at least 10 games */
1337 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
1338 $pl['response'],$pl['solo'],$pl['soloavg']);
1344 function format_score_table_ascii($score)
1347 if(sizeof($score)==0)
1350 /* truncate table if we have too many games */
1351 $max = sizeof($score);
1352 if($max>6) $output.=" (table truncated to last 6 games)\n";
1355 foreach($score[0]['players'] as $id=>$points)
1357 $name = DB_get_name('userid',$id); /*TODO*/
1358 $output.= " ".substr($name,0,2)." |";
1361 $output.= "------+------+------+------+------+\n";
1363 /* output score for each game */
1365 foreach($score as $game)
1368 if($i-1<$max-6) continue;
1370 foreach($game['players'] as $id=>$points)
1371 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1372 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1374 /* check for solo */
1385 function format_score_table_html($score,$userid)
1389 if(sizeof($score)==0)
1392 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1396 $header.= " <thead>\n <tr>\n";
1397 $header.= " <th> No </th>";
1398 foreach($score[0]['players'] as $id=>$points)
1400 $name = DB_get_name('userid',$id); /*TODO*/
1401 $header.= "<th> ".substr($name,0,2)." </th>";
1403 $header.="<th>P</th>\n </tr>\n </thead>\n";
1405 /* use the same as footer */
1407 $footer.= " <tfoot>\n <tr>\n";
1408 $footer.= " <td> No </td>";
1409 foreach($score[0]['players'] as $id=>$points)
1411 $name = DB_get_name('userid',$id); /*TODO*/
1412 $footer.= "<td> ".substr($name,0,2)." </td>";
1414 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1418 $body.= " <tbody>\n";
1420 foreach($score as $game)
1424 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1425 /* create link to old games only if you are logged in and its your game*/
1426 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1427 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1429 $body.=" <td>$i</td>";
1431 foreach($game['players'] as $id=>$points)
1432 $body.="<td>".$points."</td>";
1433 $body.="<td>".$game['points'];
1435 /* check for solo */
1438 $body.="</td></tr>\n";
1446 $output.=" </tbody>\n</table>\n</div>\n";
1451 function createCache($content, $cacheFile)
1453 $fp = fopen($cacheFile,"w");
1456 fwrite($fp,$content);
1460 echo "WARNING: couldn't create cache file";
1465 function getCache($cacheFile, $expireTime)
1467 if( file_exists($cacheFile) &&
1468 filemtime($cacheFile )>( time() - $expireTime ) )
1470 return file_get_contents($cacheFile);
1476 function check_vacation($userid)
1478 /* get start date */
1479 $result = DB_query_array("SELECT value FROM User_Prefs".
1480 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1482 $start = $result[0];
1487 $result = DB_query_array("SELECT value FROM User_Prefs".
1488 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1495 $result = DB_query_array("SELECT value FROM User_Prefs".
1496 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1498 $comment = $result[0];
1502 /* check if user is on vacation. TODO: use user's timezone */
1503 if( (time() - strtotime($start) >0) &&
1504 (strtotime($stop) - time() >0))
1505 return array ($start,$stop,$comment);
1510 function cancel_game($why,$gameid)
1512 $gameid = DB_quote_smart($gameid);
1514 /* update the game table */
1518 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1521 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1524 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1527 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1530 DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
1533 /* set each player to gameover */
1534 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1535 while($r = DB_fetch_array($result))
1538 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1544 function get_user_token($userid)
1549 $date = DB_get_user_creation_date($userid);
1550 $name = DB_get_name('userid',$userid);
1553 $token = md5("token".$name.$date);