3 function config_check()
5 global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
7 /* check if some variables are set in the config file, else set defaults */
10 if(isset($EMAIL_REPLY))
12 ini_set("sendmail_from",$EMAIL_REPLY);
14 if(!isset($ADMIN_NAME))
17 echo "<h1>Setup not completed</h1>";
18 echo "You need to set \$ADMIN_NAME in config.php.";
22 if(!isset($ADMIN_EMAIL))
25 echo "<h1>Setup not completed</h1>";
26 echo "You need to set \$ADMIN_EMAIL in config.php. ".
27 "If something goes wrong an email will be send to this address.";
34 echo "<h1>Setup not completed</h1>";
35 echo "You need to set \$DB_work in config.php. ".
36 "If this is set to 1, the game will be suspended and one can work safely on the database.".
37 "The default should be 0 for the game to work.";
44 echo "Working on the database...please check back later.";
52 function mymail($To,$Subject,$message,$header="")
54 global $debug,$EMAIL_REPLY;
56 if(isset($EMAIL_REPLY))
57 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
61 /* display email on screen,
64 $message = str_replace("\n","<br />\n",$message);
65 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
66 "<a href=\"\\0\">\\0</a>", $message);
68 echo "<br />To: $To<br />";
70 echo $header."<br />";
71 echo "Subject: $Subject <br />$message<br />\n";
75 mail($To,$Subject,$message,$header);
77 mail($To,$Subject,$message);
83 /* returns 1 if all names passed as args are defined by a GET or POST statement,
88 $args = func_get_args();
90 foreach($args as $arg)
92 $ok = $ok * isset($_REQUEST[$arg]);
93 /*echo "$arg: ok = $ok <br />";
99 function myerror($message)
101 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
102 mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
106 function pos_array($c,$arr)
123 function is_trump($c)
127 if(in_array($c,$CARDS["trump"]))
133 function is_same_suite($c1,$c2)
137 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
138 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
139 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
140 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
141 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
146 function compare_cards($a,$b,$game)
148 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
154 /* first map all cards to the odd number,
155 * this insure that the first card wins the trick
156 * if they are the same card
158 if( $a/2 - (int)($a/2) != 0.5)
160 if( $b/2 - (int)($b/2) != 0.5)
163 /* check for schweinchen and ten of hearts*/
169 if($RULES["schweinchen"]=="both" && $GAME["schweinchen"])
171 if($a == 19 || $a == 20 )
173 if($b == 19 || $b == 20 )
176 if($RULES["schweinchen"]=="second" && $GAME["schweinchen"]==3)
178 if($a == 19 || $a == 20 )
180 if($b == 19 || $b == 20 )
186 /* check for ten of hearts rule */
187 if($RULES["dullen"]=="secondwins")
188 if($a==1 && $b==1) /* both 10 of hearts */
189 return 0; /* second one wins.*/
193 /* no special cases here */
197 if(is_trump($a) && is_trump($b) && $a<=$b)
199 else if(is_trump($a) && is_trump($b) )
202 { /*$a is not a trump */
206 { /* both no trump */
209 $posA = pos_array($a,$CARDS["clubs"]);
210 $posB = pos_array($b,$CARDS["clubs"]);
218 $posA = pos_array($a,$CARDS["spades"]);
219 $posB = pos_array($b,$CARDS["spades"]);
227 $posA = pos_array($a,$CARDS["hearts"]);
228 $posB = pos_array($b,$CARDS["hearts"]);
236 $posA = pos_array($a,$CARDS["diamonds"]);
237 $posB = pos_array($b,$CARDS["diamonds"]);
244 /* not the same suit and no trump: a wins */
250 function get_winner($p,$mode)
252 /* get all 4 cards played in a trick, in the order they are played */
255 $c1pos = $tmp["pos"];
259 $c2pos = $tmp["pos"];
263 $c3pos = $tmp["pos"];
267 $c4pos = $tmp["pos"];
269 /* first card is better than all the rest */
270 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
273 /* second card is better than first and better than the rest */
274 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
277 /* third card is better than first card and better than last */
278 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
279 /* if second card is better than first, third card needs to be even better */
280 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
282 /* second is worse than first, e.g. not following suite */
283 else if (compare_cards($c1,$c2,$mode) )
286 /* non of the above */
290 function count_nines($cards)
294 foreach($cards as $c)
296 if($c == "25" || $c == "26") $nines++;
297 else if($c == "33" || $c == "34") $nines++;
298 else if($c == "41" || $c == "42") $nines++;
299 else if($c == "47" || $c == "48") $nines++;
305 function check_wedding($cards)
308 if( in_array("3",$cards) && in_array("4",$cards) )
314 function count_trump($cards)
320 /* count each trump, including the foxes */
321 foreach($cards as $c)
325 /* normally foxes don't count as trump, so we substract them here
326 * in case someone has schweinchen, one or two of them should count as trump
327 * though, so we need to add one trump for those cases */
330 if( in_array("19",$cards))
332 if( in_array("20",$cards) )
335 /* handle case where player has schweinchen */
336 if( in_array("19",$cards) && in_array("20",$cards) )
337 switch($RULES["schweinchen"])
340 /* add two, in case the player has both foxes (schweinchen) */
345 case "secondaftercall":
346 /* add one, in case the player has both foxes (schweinchen) */
356 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
364 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
365 $r[ 1]=2; $r[13]=48; $r[25]=14; $r[37]=38;
366 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
367 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
368 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
369 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
370 $r[ 6]=19; $r[18]=7; $r[30]=31; $r[42]=43;
371 $r[ 7]=20; $r[19]=8; $r[31]=32; $r[43]=44;
372 $r[ 8]=45; $r[20]=9; $r[32]=21; $r[44]=33;
373 $r[ 9]=46; $r[21]=10; $r[33]=22; $r[45]=34;
374 $r[10]=35; $r[22]=11; $r[34]=23; $r[46]=25;
375 $r[11]=36; $r[23]=12; $r[35]=24; $r[47]=26;
379 /* check if we can find a game were non of the player was involved and return
382 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
383 $randomnumbers = DB_get_unused_randomnumbers($userstr);
384 $randomnumbers = explode(":",$randomnumbers);
386 if(sizeof($randomnumbers)==48)
387 return $randomnumbers;
389 /* need to create new numbers */
393 /* shuffle using a better random generator than the standard one */
394 for ($i = 0; $i <48; $i++)
396 $j = @mt_rand(0, $i);
406 function display_cards($me,$myturn)
411 function have_suit($cards,$c)
416 if(in_array($c,$CARDS["trump"]))
417 $suite = $CARDS["trump"];
418 else if(in_array($c,$CARDS["clubs"]))
419 $suite = $CARDS["clubs"];
420 else if(in_array($c,$CARDS["spades"]))
421 $suite = $CARDS["spades"];
422 else if(in_array($c,$CARDS["hearts"]))
423 $suite = $CARDS["hearts"];
424 else if(in_array($c,$CARDS["diamonds"]))
425 $suite = $CARDS["diamonds"];
427 foreach($cards as $card)
429 if(in_array($card,$suite))
436 function same_type($card,$c)
441 /* figure out what kind of card c is */
442 if(in_array($c,$CARDS["trump"]))
443 $suite = $CARDS["trump"];
444 else if(in_array($c,$CARDS["clubs"]))
445 $suite = $CARDS["clubs"];
446 else if(in_array($c,$CARDS["spades"]))
447 $suite = $CARDS["spades"];
448 else if(in_array($c,$CARDS["hearts"]))
449 $suite = $CARDS["hearts"];
450 else if(in_array($c,$CARDS["diamonds"]))
451 $suite = $CARDS["diamonds"];
453 /* card is the same suid return 1 */
454 if(in_array($card,$suite))
460 function set_gametype($gametype)
473 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
474 '17','18','19','20','21','22','23','24','25','26');
475 $CARDS["diamonds"] = array();
476 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
477 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
478 $CARDS["hearts"] = array('43','44','45','46','47','48');
479 $CARDS["foxes"] = array('19','20');
480 if($RULES["dullen"]=='none')
482 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
483 '17','18','19','20','21','22','23','24','25','26');
484 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
488 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
489 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
490 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
491 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
492 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
493 $CARDS["foxes"] = array();
496 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
497 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
498 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
499 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
500 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
501 $CARDS["foxes"] = array();
504 $CARDS["trump"] = array();
505 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
506 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
507 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
508 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
509 $CARDS["foxes"] = array();
512 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
513 '17','18','27','28','29','30','31','32','33','34');
514 $CARDS["clubs"] = array();
515 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
516 $CARDS["hearts"] = array('43','44','45','46','47','48');
517 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
518 $CARDS["foxes"] = array();
519 if($RULES["dullen"]=='none')
521 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
522 '17','18','27','28','29','30','31','32','33','34');
523 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
527 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
528 '17','18','35','36','37','38','39','40','41','42');
529 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
530 $CARDS["spades"] = array();
531 $CARDS["hearts"] = array('43','44','45','46','47','48');
532 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
533 $CARDS["foxes"] = array();
534 if($RULES["dullen"]=='none')
536 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
537 '17','18','35','36','37','38','39','40','41','42');
538 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
542 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
543 '17','18','43','44','45','46','47','48');
544 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
545 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
546 $CARDS["hearts"] = array();
547 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
548 $CARDS["foxes"] = array();
549 if($RULES["dullen"]=='none')
551 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
552 '17','18','43','44','1','2','45','46','47','48');
558 function mysort($cards,$gametype)
560 usort ( $cards, "sort_comp" );
564 function sort_comp($a,$b)
569 $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
570 $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
572 return pos_array($a,$ALL)-pos_array($b,$ALL);
575 function can_call($what,$hash)
579 $gameid = DB_get_gameid_by_hash($hash);
580 $gametype = DB_get_gametype_by_gameid($gameid);
581 $oldcall = DB_get_call_by_hash($hash);
582 $pcall = DB_get_partner_call_by_hash($hash);
584 if( ($pcall!=NULL && $what >= $pcall) ||
585 ($oldcall!=NULL && $what >=$oldcall) )
590 $NRcards = count(DB_get_hand($hash));
595 $user = DB_get_hash_from_game_and_pos($gameid,$i);
596 $NRallcards += count(DB_get_hand($user));
599 /* in case of a wedding, everything will be delayed by an offset */
601 if($gametype=="wedding")
603 $offset = DB_get_sickness_by_gameid($gameid);
604 if ($offset <0) /* not resolved */
608 switch ($RULES["call"])
611 if( 4-($what/30) >= 12 - ($NRcards + $offset))
615 if( 27+4*($what/30) <= $NRallcards + $offset*4)
620 if($oldcall!=NULL && $pcall!=NULL)
621 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
622 else if($oldcall!=NULL)
624 else if ($pcall!=NULL)
629 if( 12 <= ($NRcards + $offset))
633 else if ( 9 <= ($NRcards + $offset))
635 if( ($mincall>=0 && $mincall==120) )
638 else if ( 6 <= ($NRcards + $offset))
640 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
643 else if ( 3 <= ($NRcards + $offset))
645 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
648 else if ( 0 <= ($NRcards + $offset))
650 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
659 function display_table ()
661 global $gameid, $GT, $debug,$INDEX,$defaulttimezone;
663 $result = mysql_query("SELECT User.fullname as name,".
664 " Hand.position as position, ".
666 " Hand.party as party, ".
667 " Hand.sickness as sickness, ".
668 " Hand.point_call, ".
669 " User.last_login, ".
673 "LEFT JOIN User ON User.id=Hand.user_id ".
674 "WHERE Hand.game_id='".$gameid."' ".
675 "ORDER BY position ASC");
677 echo "<div class=\"table\">\n".
678 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
679 while($r = mysql_fetch_array($result,MYSQL_NUM))
689 date_default_timezone_set($defaulttimezone);
690 $lastlogin = strtotime($r[6]);
691 date_default_timezone_set($timezone);
692 $timenow = strtotime(date("Y-m-d H:i:s"));
694 echo " <div class=\"table".($pos-1)."\">\n";
698 echo " <a href=\"".$INDEX."?me=".$hash."\">$name</a>\n";
700 /* add hints for poverty, wedding, solo, etc */
701 if($GT=="poverty" && $party=="re")
702 if($sickness=="poverty")
704 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
705 $cards = DB_get_all_hand($userhash);
706 $trumpNR = count_trump($cards);
708 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
710 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
713 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
717 if($sickness=="poverty")
719 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
720 $cards = DB_get_all_hand($userhash);
721 $trumpNR = count_trump($cards);
723 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
725 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
728 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
730 if($sickness=="poverty")
732 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
733 $cards = DB_get_all_hand($userhash);
734 $trumpNR = count_trump($cards);
736 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
738 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
741 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
743 if($GT=="wedding" && $party=="re")
744 if($sickness=="wedding")
745 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
747 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
749 if(ereg("solo",$GT) && $party=="re")
751 if(ereg("queen",$GT))
752 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
753 else if(ereg("jack",$GT))
754 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
755 else if(ereg("club",$GT))
756 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
757 else if(ereg("spade",$GT))
758 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
759 else if(ereg("heart",$GT))
760 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
761 else if(ereg("trumpless",$GT))
762 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
763 else if(ereg("trump",$GT))
764 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
767 /* add point calls */
771 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
773 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
777 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
780 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
783 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
786 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
792 echo " <span title=\"".date("Y-m-d H:i:s",$timenow). "\">local time</span>\n";
793 echo " <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
797 echo "</div>\n"; /* end output table */
804 function display_user_menu()
806 global $WIKI,$myid,$INDEX,$STATS;
807 echo "<div class=\"usermenu\">\n".
808 "<a href=\"".$INDEX."\"> Go to my user page </a>";
810 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
811 " LEFT JOIN Game On Hand.game_id=Game.id".
812 " WHERE Hand.user_id='$myid'".
813 " AND Game.player='$myid'".
814 " AND Game.status<>'gameover'".
815 " ORDER BY Game.session" );
816 if(mysql_num_rows($result))
817 echo "<hr />It's your turn in these games:<br />\n";
819 while( $r = mysql_fetch_array($result,MYSQL_NUM))
821 echo "<a href=\"".$INDEX."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
824 echo "<hr /> <a href=\"".$INDEX."?new\">Start a new game</a>\n";
826 echo "<hr /> <a href=\"".$STATS."\">Statistics</a>\n";
829 "<hr />Report bugs in the <a href=\"".$WIKI."\">wiki</a>\n";
834 function generate_score_table($session)
838 $gameids = DB_get_gameids_of_finished_games_by_session($session);
843 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n";
846 /* get player id, names... from the first game */
848 $result = mysql_query("SELECT User.id, User.fullname from Hand".
849 " LEFT JOIN User On Hand.user_id=User.id".
850 " WHERE Hand.game_id=".$gameids[0]);
851 while( $r = mysql_fetch_array($result,MYSQL_NUM))
853 $player[] = array( 'id' => $r[0], 'points' => 0 );
854 $output.= " <td> ".substr($r[1],0,2)." </td>\n";
856 $output.=" <td>P</td>\n </tr>\n";
858 /* get points and generate table */
859 foreach($gameids as $gameid)
863 $re_score = DB_get_score_by_gameid($gameid);
864 foreach($player as $key=>$pl)
866 $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
868 if(DB_get_gametype_by_gameid($gameid)=="solo")
869 $player[$key]['points'] += 3*$re_score;
871 $player[$key]['points'] += $re_score;
872 else if ($party == "contra")
873 $player[$key]['points'] -= $re_score;
875 $output.=" <td>".$player[$key]['points']."</td>\n";
877 $output.=" <td>".abs($re_score);
880 if(DB_get_gametype_by_gameid($gameid)=="solo")
882 $output.="</td>\n </tr>\n";
885 $output.="</table></div>\n";