2 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 /* define possible status for showing cards */
91 define("CARDS_EMPTY", 0); /* show player's hand*/
92 define("CARDS_SHOW", 1); /* show player's hand*/
93 define("CARDS_MYTURN", 2); /* show radiobuttons for cards that can be played*/
94 define("CARDS_EXCHANGE", 3); /* do we need to return cards to our partner in poverty?*/
95 define("CARDS_GAMEOVER_ME", 4); /* show all cards from everyone*/
96 define("CARDS_GAMEOVER", 5); /* show all cards from everyone (looking at someone else's game)*/
98 /* cards_status: SHOW show our hand
99 * MYTURN show radiobutton for cards that can be played
100 * EXCHANGE do we need to return cards to our partner in poverty?
101 * GAMEOVER show all cards from everyone
104 function mymail($uid,$gameid=0,$type,$message)
106 global $EmailName,$WIKI;
108 /* do we send the email right away or save it in the database? */
111 /* add standard header and footer */
112 $subject = "$EmailName ";
114 $game = DB_format_gameid($gameid);
121 $subject.=sprintf(_("Game %s canceled"),$game);
123 case GAME_CANCELED_POVERTY:
124 $subject.=sprintf(_("Game %s canceled (poverty not resolved)"),$game);
126 case GAME_CANCELED_TIMEOUT:
127 $subject.=sprintf(_("Game %s canceled (timed out)"),$game);
130 $subject.=sprintf(_("A card has been played in game %s"),$game);
133 $subject.=sprintf(_("Ready, set, go... (game %s)"),$game);
136 $subject.=sprintf(_("Poverty (game %s)"),$game);
139 $subject.=sprintf(_("Double poverty (game %s)"),$game);
142 $subject.=sprintf(_("Game over (game %s)"),$game);
145 $subject.=_("Recovery");
148 $subject.=sprintf(_("Reminder: game %s it's your turn"),$game);
151 $subject.=sprintf(_("You are invited to a game of DoKo (game %s)"),$game);
154 $subject.=sprintf(_("Problem with email, contact admin (errorcode %d)"),$gameid);
157 /* standard goodbye */
158 $footer = "\nHave a nice day\n".
159 " your E-Doko service department\n\n".
161 "You can change your mail delivery mode in the preference menu.\n".
162 'web: http://doko.nubati.net '.
163 "help, bugs, etc.: $WIKI";
167 /* send email to more than one person */
169 $header = "Hello all\n\n";
171 foreach($uid as $user)
173 $all[] = DB_get_email('userid',$user);
175 $To = implode(",",$all);
179 /* standard greeting */
180 $name = DB_get_name('userid',$uid);
181 $header = "Hello $name\n\n";
183 $To = DB_get_email('userid',$uid);
185 /* check if user wants email right away or if we should save it in
186 * the database for later delivery
189 $PREF = DB_get_PREF($uid);
190 if( $PREF['digest'] != 'digest-off' )
195 sendmail($To,$subject,$header.$message.$footer);
198 /* store email in database */
199 DB_digest_insert_email($To,$message,$type,$gameid);
203 function sendmail($To,$Subject,$message)
205 /* this function sends the mail or outputs to the screen in case of debugging */
206 global $debug,$EMAIL_REPLY;
210 if(isset($EMAIL_REPLY))
211 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
215 /* display email on screen,
218 $message = str_replace("\n","<br />\n",$message);
219 $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
220 "<a href=\"$1\">$1</a>", $message);
222 echo "<br />To: $To<br />";
224 echo $header."<br />";
225 echo "Subject: $Subject <br />$message<br />\n";
229 mail($To,$Subject,$message,$header);
231 mail($To,$Subject,$message);
237 /* returns 1 if all names passed as args are defined by a GET or POST statement,
242 $args = func_get_args();
244 foreach($args as $arg)
246 $ok = $ok * isset($_REQUEST[$arg]);
247 /*echo "$arg: ok = $ok <br />";
253 function myerror($message)
255 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
256 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
260 function pos_array($c,$arr)
277 function is_trump($c)
281 if(in_array($c,$CARDS["trump"]))
287 function is_same_suite($c1,$c2)
291 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
292 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
293 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
294 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
295 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
300 function compare_cards($a,$b,$game)
302 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
308 /* first map all cards to the odd number,
309 * this insure that the first card wins the trick
310 * if they are the same card
312 if( $a/2 - (int)($a/2) != 0.5)
314 if( $b/2 - (int)($b/2) != 0.5)
317 /* check for schweinchen and ten of hearts*/
323 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
325 if($a == 19 || $a == 20 )
327 if($b == 19 || $b == 20 )
330 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
332 if($a == 19 || $a == 20 )
334 if($b == 19 || $b == 20 )
337 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
339 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
340 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
342 if($a == 19 || $a == 20 )
344 if($b == 19 || $b == 20 )
347 /* if not, do nothing and the foxes are just handeled as normal trump */
353 /* check for ten of hearts rule */
354 if($RULES["dullen"]=="secondwins")
355 if($a==1 && $b==1) /* both 10 of hearts */
356 return 0; /* second one wins.*/
360 /* no special cases here */
364 if(is_trump($a) && is_trump($b) && $a<=$b)
366 else if(is_trump($a) && is_trump($b) )
369 { /*$a is not a trump */
373 { /* both no trump */
376 $posA = pos_array($a,$CARDS["clubs"]);
377 $posB = pos_array($b,$CARDS["clubs"]);
385 $posA = pos_array($a,$CARDS["spades"]);
386 $posB = pos_array($b,$CARDS["spades"]);
394 $posA = pos_array($a,$CARDS["hearts"]);
395 $posB = pos_array($b,$CARDS["hearts"]);
403 $posA = pos_array($a,$CARDS["diamonds"]);
404 $posB = pos_array($b,$CARDS["diamonds"]);
411 /* not the same suit and no trump: a wins */
417 function get_winner($p,$mode)
419 /* get all 4 cards played in a trick, in the order they are played */
422 $c1pos = $tmp["pos"];
426 $c2pos = $tmp["pos"];
430 $c3pos = $tmp["pos"];
434 $c4pos = $tmp["pos"];
436 /* first card is better than all the rest */
437 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
440 /* second card is better than first and better than the rest */
441 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
444 /* third card is better than first card and better than last */
445 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
446 /* if second card is better than first, third card needs to be even better */
447 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
449 /* second is worse than first, e.g. not following suite */
450 else if (compare_cards($c1,$c2,$mode) )
453 /* non of the above */
457 function count_nines($cards)
461 foreach($cards as $c)
463 if($c == "25" || $c == "26") $nines++;
464 else if($c == "33" || $c == "34") $nines++;
465 else if($c == "41" || $c == "42") $nines++;
466 else if($c == "47" || $c == "48") $nines++;
472 function check_wedding($cards)
475 if( in_array("3",$cards) && in_array("4",$cards) )
481 function count_trump($cards,$status='pregame')
487 /* count each trump, including the foxes, since this is used to determine poverty status */
488 foreach($cards as $c)
492 /* In case we really want to know the amount of trump, we can use the status variable.
493 * This is needed for example to figure out what icon to display on the table in case of
494 * trump given back in poverty */
495 if($status=='all') return $trump;
497 /* normally foxes don't count as trump, so we substract them here
498 * in case someone has schweinchen, one or two of them should count as trump
499 * though, so we need to add one trump for those cases */
502 if( in_array("19",$cards))
504 if( in_array("20",$cards) )
507 /* handle case where player has schweinchen */
508 if( in_array("19",$cards) && in_array("20",$cards) )
509 switch($RULES["schweinchen"])
512 /* add two, in case the player has both foxes (schweinchen) */
517 case "secondaftercall":
518 /* add one, in case the player has both foxes (schweinchen) */
528 function check_low_trump($cards)
532 if($RULES['lowtrump']=='none')
535 /* check if we have low trump */
538 foreach($cards as $card)
540 /* card a trump, but not a diamond? */
545 /* handle case where player has schweinchen */
546 if( in_array("19",$cards) && in_array("20",$cards) )
547 switch($RULES["schweinchen"])
551 case "secondaftercall":
561 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
569 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
570 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
571 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
572 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
573 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=21;
574 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
575 $r[ 6]=41; $r[18]=7; $r[30]=31; $r[42]=43;
576 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=20;
577 $r[ 8]=45; $r[20]=9; $r[32]=33; $r[44]=19;
578 $r[ 9]=46; $r[21]=10; $r[33]=44; $r[45]=24;
579 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
580 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
584 /* check if we can find a game were non of the player was involved and return
587 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
588 $randomnumbers = DB_get_unused_randomnumbers($userstr);
589 $randomnumbers = explode(":",$randomnumbers);
591 if(sizeof($randomnumbers)==48)
592 return $randomnumbers;
594 /* need to create new numbers */
598 /* shuffle using a better random generator than the standard one */
599 for ($i = 0; $i <48; $i++)
601 $j = @mt_rand(0, $i);
611 function display_cards($me,$myturn)
616 function have_suit($cards,$c)
621 if(in_array($c,$CARDS["trump"]))
622 $suite = $CARDS["trump"];
623 else if(in_array($c,$CARDS["clubs"]))
624 $suite = $CARDS["clubs"];
625 else if(in_array($c,$CARDS["spades"]))
626 $suite = $CARDS["spades"];
627 else if(in_array($c,$CARDS["hearts"]))
628 $suite = $CARDS["hearts"];
629 else if(in_array($c,$CARDS["diamonds"]))
630 $suite = $CARDS["diamonds"];
632 foreach($cards as $card)
634 if(in_array($card,$suite))
641 function same_type($card,$c)
646 /* figure out what kind of card c is */
647 if(in_array($c,$CARDS["trump"]))
648 $suite = $CARDS["trump"];
649 else if(in_array($c,$CARDS["clubs"]))
650 $suite = $CARDS["clubs"];
651 else if(in_array($c,$CARDS["spades"]))
652 $suite = $CARDS["spades"];
653 else if(in_array($c,$CARDS["hearts"]))
654 $suite = $CARDS["hearts"];
655 else if(in_array($c,$CARDS["diamonds"]))
656 $suite = $CARDS["diamonds"];
658 /* card is the same suid return 1 */
659 if(in_array($card,$suite))
665 function set_gametype($gametype)
679 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
680 '17','18','19','20','21','22','23','24','25','26');
681 $CARDS["diamonds"] = array();
682 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
683 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
684 $CARDS["hearts"] = array('43','44','45','46','47','48');
685 $CARDS["foxes"] = array('19','20');
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','19','20','21','22','23','24','25','26');
690 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
692 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
693 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
695 /* find the fox and put them at the top of the stack */
696 foreach(array('19','20') as $fox)
699 $trump = $CARDS['trump'];
700 $key = array_keys($trump, $fox);
704 $foxa[]=$trump[$key[0]];
705 unset($trump[$key[0]]);
706 $trump = array_merge($foxa,$trump);
707 $CARDS['trump'] = $trump;
710 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
711 && $GAME['schweinchen-who'])
713 /* find the fox and put them at the top of the stack */
714 $trump = $CARDS['trump'];
715 $key = array_keys($trump, '19');
719 $foxa[]=$trump[$key[0]];
720 unset($trump[$key[0]]);
721 $trump = array_merge($foxa,$trump);
722 $CARDS['trump'] = $trump;
726 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
727 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
728 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
729 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
730 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
731 $CARDS["foxes"] = array();
734 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
735 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
736 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
737 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
738 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
739 $CARDS["foxes"] = array();
742 $CARDS["trump"] = array();
743 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
744 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
745 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
746 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
747 $CARDS["foxes"] = array();
750 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
751 '17','18','27','28','29','30','31','32','33','34');
752 $CARDS["clubs"] = array();
753 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
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','27','28','29','30','31','32','33','34');
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','35','36','37','38','39','40','41','42');
767 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
768 $CARDS["spades"] = array();
769 $CARDS["hearts"] = array('43','44','45','46','47','48');
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','35','36','37','38','39','40','41','42');
776 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
780 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
781 '17','18','43','44','45','46','47','48');
782 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
783 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
784 $CARDS["hearts"] = array();
785 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
786 $CARDS["foxes"] = array();
787 if($RULES["dullen"]=='none')
789 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
790 '17','18','43','44','1','2','45','46','47','48');
796 function mysort($cards,$gametype)
799 if(isset($PREF['sorting']))
800 if($PREF['sorting']=='high-low')
801 usort ( $cards, 'sort_comp_high_low' );
803 usort ( $cards, 'sort_comp_low_high' );
805 usort ( $cards, 'sort_comp_high_low' );
809 function sort_comp_high_low($a,$b)
814 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
815 $CARDS['hearts'],$CARDS['spades']);
817 return pos_array($a,$ALL)-pos_array($b,$ALL);
820 function sort_comp_low_high($a,$b)
825 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
826 $CARDS['hearts'],$CARDS['spades']);
828 return -pos_array($a,$ALL)+pos_array($b,$ALL);
831 function can_call($what,$hash)
833 /* figure out if a person can make a call:
834 $what in 0,30,60,90,120 = points of the call
835 $hash = the hash of the person who wants to make the call
838 0 can't make that call
840 2 can make the call, but this is the last chance to do so...
845 /* get some information
847 $gameid = DB_get_gameid_by_hash($hash);
848 $gametype = DB_get_gametype_by_gameid($gameid);
849 $oldcall = DB_get_call_by_hash($hash); /* did the person already made a call? */
850 $pcall = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
853 /* you're call must be better than the one you or your partner already made
855 if( ($pcall!=NULL && ($what >= $pcall))
856 || ($oldcall!=NULL && ($what >=$oldcall)) )
861 /* for some rules we need to know how many cards people have
863 $NRcards = count(DB_get_hand($hash));
868 $user = DB_get_hash_from_game_and_pos($gameid,$i);
869 $NRallcards += count(DB_get_hand($user));
872 /* in case of a wedding, everything will be delayed by an offset
875 if($gametype=="wedding")
877 $offset = DB_get_sickness_by_gameid($gameid);
878 if ($offset <0) /* not resolved */
882 /* now check if the call is allowed depending on the rule set
884 switch ($RULES["call"])
887 /* calls can be made before/while you play your card...
888 * first card = 120, second card = 90, etc.
890 if( 4-($what/30) == 12 - ($NRcards + $offset))
892 if( 4-($what/30) > 12 - ($NRcards + $offset))
896 /* you can make the first call anytime during the first trick
898 if( 27+4*($what/30) == $NRallcards + $offset*4)
900 if( 27+4*($what/30) < $NRallcards + $offset*4)
904 /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
905 * you can't skip a call though
908 /* figure out last call
910 if($oldcall!=NULL && $pcall!=NULL)
911 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
912 else if($oldcall!=NULL)
914 else if ($pcall!=NULL)
920 if( 12 == ($NRcards + $offset))
924 else if( 12 < ($NRcards + $offset))
928 else if ( 9 == ($NRcards + $offset))
930 if( ($mincall>=0 && $mincall<=120 && $what<=90 ) )
933 else if ( 9 < ($NRcards + $offset))
935 if( ($mincall>=0 && $mincall<=120 && $what<=90 ) )
938 else if ( 6 == ($NRcards + $offset))
940 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
943 else if ( 6 < ($NRcards + $offset))
945 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
948 else if ( 3 == ($NRcards + $offset))
950 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
953 else if ( 3 < ($NRcards + $offset))
955 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
958 else if ( 0 == ($NRcards + $offset))
960 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
963 else if ( 0 < ($NRcards + $offset))
965 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
974 function display_table_begin ()
976 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
977 global $RULES,$GAME,$gametype;
979 $result = DB_query("SELECT User.fullname as name,".
980 " Hand.position as position, ".
982 " Hand.party as party, ".
983 " Hand.sickness as sickness, ".
984 " Hand.point_call, ".
985 " User.last_login, ".
990 "LEFT JOIN User ON User.id=Hand.user_id ".
991 "WHERE Hand.game_id='".$gameid."' ".
992 "ORDER BY position ASC");
994 $row0 = DB_fetch_array($result);
995 $row1 = DB_fetch_array($result);
996 $row2 = DB_fetch_array($result);
997 $row3 = DB_fetch_array($result);
999 echo "<div class=\"table\">\n";
1000 display_single_user($row1);
1001 echo "\n<div class=\"middle\">\n";
1002 display_single_user($row0);
1003 echo " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
1004 display_single_user($row2);
1008 function display_table_end ()
1010 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
1011 global $RULES,$GAME,$gametype;
1013 $result = DB_query("SELECT User.fullname as name,".
1014 " Hand.position as position, ".
1016 " Hand.party as party, ".
1017 " Hand.sickness as sickness, ".
1018 " Hand.point_call, ".
1019 " User.last_login, ".
1024 "LEFT JOIN User ON User.id=Hand.user_id ".
1025 "WHERE Hand.game_id='".$gameid."' ".
1026 "ORDER BY position ASC");
1028 $row0 = DB_fetch_array($result);
1029 $row1 = DB_fetch_array($result);
1030 $row2 = DB_fetch_array($result);
1031 $row3 = DB_fetch_array($result);
1034 display_single_user($row3);
1040 function display_single_user($r)
1042 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
1043 global $RULES,$GAME,$gametype;
1054 $wins = DB_get_number_of_tricks($gameid,$pos);
1055 date_default_timezone_set($defaulttimezone);
1056 $lastlogin = strtotime($r[6]);
1057 date_default_timezone_set($timezone);
1058 $timenow = strtotime(date("Y-m-d H:i:s"));
1059 $gravatar = "$name<br />\n <img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".
1060 md5(strtolower(trim($email)))."?d=identicon\" alt=\"$name's gravatar\" />";
1062 echo " <div class=\"table".($pos-1)."\">\n";
1065 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
1066 if($vacation = check_vacation($user))
1068 $start = $vacation[0];
1069 $stop = substr($vacation[1],0,10);
1070 $comment = $vacation[2];
1072 $title = "begin: $start end: $stop $comment";
1073 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
1076 echo " $gravatar \n";
1080 /* add hints for poverty, wedding, solo, etc */
1081 if( $gametype != "solo")
1082 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
1083 echo " Schweinchen. <br />";
1085 if($GT=="poverty" && $party=="re")
1086 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1088 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1089 $cards = DB_get_all_hand($userhash);
1090 $trumpNR = count_trump($cards,'all');
1092 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" ".
1093 "alt=\"poverty - trump back\" title=\"poverty - trump back\" />\n";
1095 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" ".
1096 "alt=\"poverty - no trump back\" title=\"poverty - no trump back\" />\n";
1099 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" ".
1100 "alt=\"poverty partner\" title=\"poverty partner\" />\n";
1104 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1106 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1107 $cards = DB_get_all_hand($userhash);
1108 $trumpNR = count_trump($cards,'all');
1110 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" ".
1111 "alt=\"poverty < trump back\" title=\"poverty - trump back\" />\n";
1113 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" ".
1114 "alt=\"poverty <\" title=\"poverty - no trump back\" />\n";
1117 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" ".
1118 "alt=\"poverty >\" title=\"poverty partner\" />\n";
1120 if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
1122 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1123 $cards = DB_get_all_hand($userhash);
1124 $trumpNR = count_trump($cards,'all');
1126 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" ".
1127 "alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>\n";
1129 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" ".
1130 "alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />\n";
1133 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" ".
1134 "alt=\"poverty2 >\" title=\"poverty2 partner\" />\n";
1136 if($GT=="wedding" && $party=="re")
1137 if($sickness=="wedding")
1138 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />\n";
1140 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" ".
1141 "alt=\"wedding partner\" title=\"wedding partner\" />\n";
1143 if( (strpos($GT,"solo")!==false) && $party=="re")
1145 if(strpos($GT,"queen")!==false)
1146 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />\n";
1147 else if(strpos($GT,"jack")!==false)
1148 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />\n";
1149 else if(strpos($GT,"club")!==false)
1150 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />\n";
1151 else if(strpos($GT,"spade")!==false)
1152 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />\n";
1153 else if(strpos($GT,"heart")!==false)
1154 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />\n";
1155 else if(strpos($GT,"trumpless")!==false)
1156 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />\n";
1157 else if(strpos($GT,"trump")!==false)
1158 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />\n";
1161 /* add point calls */
1165 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />\n";
1167 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />\n";
1171 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />\n";
1174 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />\n";
1177 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />\n";
1180 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />\n";
1185 echo " <img src=\"pics/button/time-info.png\" class=\"button\" alt=\"time info\" ".
1186 "title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
1187 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\" />";
1189 echo " <br /><span class=\"numberoftricks\">";
1190 /* show how many tricks the person made */
1194 echo "#tricks 0"; break;
1196 echo "#tricks 1"; break;
1200 echo "#tricks few"; break;
1202 echo "#tricks many"; break;
1209 function display_user_menu($id, $skiphash=NULL)
1211 global $WIKI,$INDEX;
1214 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1215 " LEFT JOIN Game On Hand.game_id=Game.id".
1216 " WHERE Hand.user_id='$id'".
1217 " AND Hand.hash!='$skiphash'".
1218 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1219 " AND ( Game.status='pre' OR Game.status='play' )".
1220 " ORDER BY Game.session" );
1222 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1223 " LEFT JOIN Game On Hand.game_id=Game.id".
1224 " WHERE Hand.user_id='$id'".
1225 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1226 " AND ( Game.status='pre' OR Game.status='play' )".
1227 " ORDER BY Game.session" );
1230 while( $r = DB_fetch_array($result))
1234 echo "\n<div class=\"usermenu\">\n ";
1235 echo _('It\'s your turn in these games').":\n";
1243 echo " <a href=\"".$INDEX."?action=game&me=".$r[0].
1244 "\"> ".DB_format_gameid($r[1])." </a>\n";
1257 function generate_score_table($session)
1259 /* returns an array with N entries
1260 * $score[$i]["gameid"] = gameid
1261 * $score[$i]["players"] = array (id=>total points)
1262 * $score[$i]["points"] = points for this game
1263 * $score[$i]["solo"] = 1 or 0
1268 /* get all ids, scores and gametypes */
1269 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1271 if($gameids == NULL)
1275 $player_party = array();
1277 /* get player id from the first game */
1278 $result = DB_query("SELECT user_id from Hand".
1279 " WHERE Hand.game_id=".$gameids[0][0]);
1280 while( $r = DB_fetch_array($result))
1283 /* get party of players for each game in the session */
1284 foreach($player as $id=>$points)
1285 $player_party[$id]=DB_get_party_by_session_and_userid($session,$id);
1287 /* get points and generate table */
1288 foreach($gameids as $gameid)
1290 $re_score = $gameid[1];
1291 $gametype = $gameid[2];
1292 foreach($player as $id=>$points)
1294 $party = $player_party[$id][$i][0];
1296 if($gametype=="solo")
1297 $player[$id] += 3*$re_score;
1299 $player[$id] += $re_score;
1300 else if ($party == "contra")
1301 $player[$id] -= $re_score;
1303 $score[$i]['gameid'] = $gameid[0] ;
1304 $score[$i]['players'] = $player;
1305 $score[$i]['points'] = abs($re_score);
1306 $score[$i]['solo'] = ($gametype=="solo");
1314 function generate_global_score_table()
1318 /* get all ids, scores and gametypes */
1319 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1321 if($gameids == NULL)
1325 /* get player id, names... from the User table */
1326 $result = DB_query('SELECT User.id, User.fullname FROM User');
1328 /* save information in an array */
1329 while( $r = DB_fetch_array($result))
1330 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
1331 'response' => 0 , 'solo' => 0, 'soloavg' => 0);
1333 /* get points and generate table */
1334 foreach($gameids as $gameid)
1336 $re_score = $gameid[1];
1337 $gametype = $gameid[2];
1339 /* get players involved in this game */
1340 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid[0]));
1341 while($r = DB_fetch_array($result))
1344 $party = DB_get_party_by_gameid_and_userid($gameid[0],$id);
1346 if($gametype=='solo')
1347 $player[$id]['points'] += 3*$re_score;
1349 $player[$id]['points'] += $re_score;
1350 else if ($party == 'contra')
1351 $player[$id]['points'] -= $re_score;
1353 $player[$id]['nr']+=1;
1357 /* add number of active games */
1358 $result = DB_query_array_all("SELECT user_id, COUNT(*) as c " .
1360 " LEFT JOIN Game ON Game.id=game_id".
1361 " WHERE Game.status IN ('pre','play')".
1362 " GROUP BY user_id");
1364 foreach($result as $res)
1366 $player[$res[0]]['active'] = $res[1];
1369 /* response time of users*/
1370 $result = DB_query_array_all("SELECT user_id,".
1371 "IFNULL(AVG(if(P1.sequence in (2,3,4),".
1372 "-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 ".
1374 "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
1375 "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
1376 "GROUP BY user_id ");
1378 foreach($result as $res)
1380 $player[$res[0]]['response'] = $res[1];
1384 $result = DB_query_array_all("SELECT user_id as uid,".
1386 " COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
1388 " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
1389 " WHERE type='solo' AND Game.status='gameover' ".
1390 " GROUP BY user_id ");
1392 foreach($result as $res)
1394 $player[$res[0]]['solo'] = $res[1];
1395 $player[$res[0]]['soloavg'] = $res[2];
1399 /* sort everything nicely */
1403 if($a['nr']==0) return 1;
1404 if($b['nr']==0) return 1;
1406 $a=$a['points']/$a['nr'];
1407 $b=$b['points']/$b['nr'];
1411 return ($a > $b) ? -1 : 1;
1413 usort($player,'cmp');
1416 foreach($player as $pl)
1418 /* limit to players with at least 10 games */
1420 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
1421 $pl['response'],$pl['solo'],$pl['soloavg']);
1427 function format_score_table_ascii($score)
1430 if(sizeof($score)==0)
1433 /* truncate table if we have too many games */
1434 $max = sizeof($score);
1435 if($max>6) $output.=" (table truncated to last 6 games)\n";
1438 foreach($score[0]['players'] as $id=>$points)
1440 $name = DB_get_name('userid',$id); /*TODO*/
1441 $output.= " ".substr($name,0,2)." |";
1444 $output.= "------+------+------+------+------+\n";
1446 /* output score for each game */
1448 foreach($score as $game)
1451 if($i-1<$max-6) continue;
1453 foreach($game['players'] as $id=>$points)
1454 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1455 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1457 /* check for solo */
1468 function format_score_table_html($score,$userid)
1472 if(sizeof($score)==0)
1475 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1479 $header.= " <thead>\n <tr>\n";
1480 $header.= " <th> No </th>";
1481 foreach($score[0]['players'] as $id=>$points)
1483 $name = DB_get_name('userid',$id); /*TODO*/
1484 $header.= "<th> ".substr($name,0,2)." </th>";
1486 $header.="<th>P</th>\n </tr>\n </thead>\n";
1488 /* use the same as footer */
1490 $footer.= " <tfoot>\n <tr>\n";
1491 $footer.= " <td> No </td>";
1492 foreach($score[0]['players'] as $id=>$points)
1494 $name = DB_get_name('userid',$id); /*TODO*/
1495 $footer.= "<td> ".substr($name,0,2)." </td>";
1497 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1501 $body.= " <tbody>\n";
1503 foreach($score as $game)
1507 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1508 /* create link to old games only if you are logged in and its your game*/
1509 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1510 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1512 $body.=" <td>$i</td>";
1514 foreach($game['players'] as $id=>$points)
1515 $body.="<td>".$points."</td>";
1516 $body.="<td>".$game['points'];
1518 /* check for solo */
1521 $body.="</td></tr>\n";
1529 $output.=" </tbody>\n</table>\n</div>\n";
1534 function createCache($content, $cacheFile)
1536 $fp = fopen($cacheFile,"w");
1539 fwrite($fp,$content);
1543 echo "WARNING: couldn't create cache file";
1548 function getCache($cacheFile, $expireTime)
1550 if( file_exists($cacheFile) &&
1551 filemtime($cacheFile )>( time() - $expireTime ) )
1553 return file_get_contents($cacheFile);
1559 function check_vacation($userid)
1561 /* get start date */
1562 $result = DB_query_array("SELECT value FROM User_Prefs".
1563 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1565 $start = $result[0];
1570 $result = DB_query_array("SELECT value FROM User_Prefs".
1571 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1578 $result = DB_query_array("SELECT value FROM User_Prefs".
1579 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1581 $comment = $result[0];
1585 /* check if user is on vacation. TODO: use user's timezone */
1586 if( (time() - strtotime($start) >0) &&
1587 (strtotime($stop) - time() >0))
1588 return array ($start,$stop,$comment);
1593 function cancel_game($why,$gameid)
1595 $gameid = DB_quote_smart($gameid);
1597 /* update the game table */
1601 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1604 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1607 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1610 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1613 DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
1616 /* set each player to gameover */
1617 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1618 while($r = DB_fetch_array($result))
1621 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1627 function get_user_token($userid)
1632 $date = DB_get_user_creation_date($userid);
1633 $name = DB_get_name('userid',$userid);
1636 $token = md5("token".$name.$date);