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 $message = str_replace("\n","<br />\n",$message);
62 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
63 "<a href=\"\\0\">\\0</a>", $message);
65 echo "<br />To: $To<br />";
67 echo $header."<br />";
68 echo "Subject: $Subject <br />$message<br />\n";
72 mail($To,$Subject,$message,$header);
74 mail($To,$Subject,$message);
80 /* returns 1 if all names passed as args are defined by a GET or POST statement,
85 $args = func_get_args();
87 foreach($args as $arg)
89 $ok = $ok * isset($_REQUEST[$arg]);
90 /*echo "$arg: ok = $ok <br />";
96 function myerror($message)
98 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
99 mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
103 function pos_array($c,$arr)
120 function is_trump($c)
124 if(in_array($c,$CARDS["trump"]))
130 function is_same_suite($c1,$c2)
134 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
135 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
136 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
137 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
138 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
143 function compare_cards($a,$b,$game)
145 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
151 /* first map all cards to the odd number,
152 * this insure that the first card wins the trick
153 * if they are the same card
155 if( $a/2 - (int)($a/2) != 0.5)
157 if( $b/2 - (int)($b/2) != 0.5)
160 /* some special cases */
165 if($RULES["schweinchen"]=="both" && $GAME["schweinchen"])
167 if($a == 19 || $a == 20 )
169 if($b == 19 || $b == 20 )
172 if($RULES["schweinchen"]=="second" && $GAME["schweinchen"]==3)
174 if($a == 19 || $a == 20 )
176 if($b == 19 || $b == 20 )
183 if($RULES["dullen"]=="secondwins")
184 if($a==1 && $b==1) /* both 10 of hearts */
185 return 0; /* second one wins.*/
189 if(is_trump($a) && is_trump($b) && $a<=$b)
191 else if(is_trump($a) && is_trump($b) )
194 { /*$a is not a trump */
198 { /* both no trump */
201 $posA = pos_array($a,$CARDS["clubs"]);
202 $posB = pos_array($b,$CARDS["clubs"]);
210 $posA = pos_array($a,$CARDS["spades"]);
211 $posB = pos_array($b,$CARDS["spades"]);
219 $posA = pos_array($a,$CARDS["hearts"]);
220 $posB = pos_array($b,$CARDS["hearts"]);
228 $posA = pos_array($a,$CARDS["diamonds"]);
229 $posB = pos_array($b,$CARDS["diamonds"]);
236 /* not the same suit and no trump: a wins */
242 function get_winner($p,$mode)
244 /* get all 4 cards played in a trick, in the order they are played */
247 $c1pos = $tmp["pos"];
251 $c2pos = $tmp["pos"];
255 $c3pos = $tmp["pos"];
259 $c4pos = $tmp["pos"];
261 /* first card is better than all the rest */
262 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
265 /* second card is better than first and better than the rest */
266 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
269 /* third card is better than first card and better than last */
270 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
271 /* if second card is better than first, third card needs to be even better */
272 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
274 /* second is worse than first, e.g. not following suite */
275 else if (compare_cards($c1,$c2,$mode) )
278 /* non of the above */
282 function count_nines($cards)
286 foreach($cards as $c)
288 if($c == "25" || $c == "26") $nines++;
289 else if($c == "33" || $c == "34") $nines++;
290 else if($c == "41" || $c == "42") $nines++;
291 else if($c == "47" || $c == "48") $nines++;
297 function check_wedding($cards)
300 if( in_array("3",$cards) && in_array("4",$cards) )
306 function count_trump($cards)
312 /* count each trump */
313 foreach($cards as $c)
317 switch($RULES["schweinchen"])
322 case "secondaftercall":
323 /* add one, in case the player has both foxes (schweinchen) */
324 if( in_array("19",$cards) && in_array("20",$cards) )
328 if( in_array("19",$cards))
330 if( in_array("20",$cards) )
338 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
346 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
347 $r[ 1]=2; $r[13]=48; $r[25]=14; $r[37]=38;
348 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
349 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
350 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
351 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
352 $r[ 6]=19; $r[18]=7; $r[30]=31; $r[42]=43;
353 $r[ 7]=20; $r[19]=8; $r[31]=32; $r[43]=44;
354 $r[ 8]=45; $r[20]=9; $r[32]=21; $r[44]=33;
355 $r[ 9]=46; $r[21]=10; $r[33]=22; $r[45]=34;
356 $r[10]=35; $r[22]=11; $r[34]=23; $r[46]=25;
357 $r[11]=36; $r[23]=12; $r[35]=24; $r[47]=26;
361 /* check if we can find a game were non of the player was involved and return
364 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
365 $randomnumbers = DB_get_unused_randomnumbers($userstr);
366 $randomnumbers = explode(":",$randomnumbers);
368 if(sizeof($randomnumbers)==48)
369 return $randomnumbers;
371 /* need to create new numbers */
375 /* shuffle using a better random generator than the standard one */
376 for ($i = 0; $i <48; $i++)
378 $j = @mt_rand(0, $i);
388 function display_cards($me,$myturn)
393 function return_timezone($offset)
398 $zone = "Europe/Berlin";
401 $zone = "America/Vancouver";
404 $zone = "Pacific/Auckland";
407 $zone = "Europe/London";
413 function have_suit($cards,$c)
418 if(in_array($c,$CARDS["trump"]))
419 $suite = $CARDS["trump"];
420 else if(in_array($c,$CARDS["clubs"]))
421 $suite = $CARDS["clubs"];
422 else if(in_array($c,$CARDS["spades"]))
423 $suite = $CARDS["spades"];
424 else if(in_array($c,$CARDS["hearts"]))
425 $suite = $CARDS["hearts"];
426 else if(in_array($c,$CARDS["diamonds"]))
427 $suite = $CARDS["diamonds"];
429 foreach($cards as $card)
431 if(in_array($card,$suite))
438 function same_type($card,$c)
443 /* figure out what kind of card c is */
444 if(in_array($c,$CARDS["trump"]))
445 $suite = $CARDS["trump"];
446 else if(in_array($c,$CARDS["clubs"]))
447 $suite = $CARDS["clubs"];
448 else if(in_array($c,$CARDS["spades"]))
449 $suite = $CARDS["spades"];
450 else if(in_array($c,$CARDS["hearts"]))
451 $suite = $CARDS["hearts"];
452 else if(in_array($c,$CARDS["diamonds"]))
453 $suite = $CARDS["diamonds"];
455 /* card is the same suid return 1 */
456 if(in_array($card,$suite))
462 function set_gametype($gametype)
475 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
476 '17','18','19','20','21','22','23','24','25','26');
477 $CARDS["diamonds"] = array();
478 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
479 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
480 $CARDS["hearts"] = array('43','44','45','46','47','48');
481 $CARDS["foxes"] = array('19','20');
482 if($RULES["dullen"]=='none')
484 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
485 '17','18','19','20','21','22','23','24','25','26');
486 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
490 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
491 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
492 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
493 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
494 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
495 $CARDS["foxes"] = array();
498 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
499 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
500 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
501 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
502 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
503 $CARDS["foxes"] = array();
506 $CARDS["trump"] = array();
507 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
508 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
509 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
510 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
511 $CARDS["foxes"] = array();
514 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
515 '17','18','27','28','29','30','31','32','33','34');
516 $CARDS["clubs"] = array();
517 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
518 $CARDS["hearts"] = array('43','44','45','46','47','48');
519 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
520 $CARDS["foxes"] = array();
521 if($RULES["dullen"]=='none')
523 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
524 '17','18','27','28','29','30','31','32','33','34');
525 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
529 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
530 '17','18','35','36','37','38','39','40','41','42');
531 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
532 $CARDS["spades"] = array();
533 $CARDS["hearts"] = array('43','44','45','46','47','48');
534 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
535 $CARDS["foxes"] = array();
536 if($RULES["dullen"]=='none')
538 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
539 '17','18','35','36','37','38','39','40','41','42');
540 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
544 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
545 '17','18','43','44','45','46','47','48');
546 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
547 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
548 $CARDS["hearts"] = array();
549 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
550 $CARDS["foxes"] = array();
551 if($RULES["dullen"]=='none')
553 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
554 '17','18','43','44','1','2','45','46','47','48');
560 function mysort($cards,$gametype)
562 usort ( $cards, "sort_comp" );
566 function sort_comp($a,$b)
571 $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
572 $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
574 return pos_array($a,$ALL)-pos_array($b,$ALL);
577 function can_call($what,$hash)
581 $gameid = DB_get_gameid_by_hash($hash);
582 $gametype = DB_get_gametype_by_gameid($gameid);
583 $oldcall = DB_get_call_by_hash($hash);
584 $pcall = DB_get_partner_call_by_hash($hash);
586 if( ($pcall!=NULL && $what >= $pcall) ||
587 ($oldcall!=NULL && $what >=$oldcall) )
592 $NRcards = count(DB_get_hand($hash));
597 $user = DB_get_hash_from_game_and_pos($gameid,$i);
598 $NRallcards += count(DB_get_hand($user));
601 /* in case of a wedding, everything will be delayed by an offset */
603 if($gametype=="wedding")
605 $offset = DB_get_sickness_by_gameid($gameid);
606 if ($offset <0) /* not resolved */
610 switch ($RULES["call"])
613 if( 4-($what/30) >= 12 - ($NRcards + $offset))
617 if( 27+4*($what/30) <= $NRallcards + $offset*4)
622 if($oldcall!=NULL && $pcall!=NULL)
623 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
624 else if($oldcall!=NULL)
626 else if ($pcall!=NULL)
631 if( 12 <= ($NRcards + $offset))
635 else if ( 9 <= ($NRcards + $offset))
637 if( ($mincall>=0 && $mincall==120) )
640 else if ( 6 <= ($NRcards + $offset))
642 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
645 else if ( 3 <= ($NRcards + $offset))
647 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
650 else if ( 0 <= ($NRcards + $offset))
652 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
661 function display_table ()
663 global $gameid, $GT, $debug,$host;
665 $result = mysql_query("SELECT User.fullname as name,".
666 " Hand.position as position, ".
668 " Hand.party as party, ".
669 " Hand.sickness as sickness, ".
670 " Hand.point_call, ".
671 " User.last_login, ".
674 "LEFT JOIN User ON User.id=Hand.user_id ".
675 "WHERE Hand.game_id='".$gameid."' ".
676 "ORDER BY position ASC");
678 echo "<div class=\"table\">\n".
679 " <img src=\"pics/table.png\" alt=\"table\" />\n";
680 while($r = mysql_fetch_array($result,MYSQL_NUM))
688 $lastlogin = strtotime($r[6]);
691 $offset = DB_get_user_timezone($user);
692 $zone = return_timezone($offset);
693 date_default_timezone_set($zone);
695 echo " <div class=\"table".($pos-1)."\">\n";
699 echo " <a href=\"".$host."?me=".$hash."\">$name</a>\n";
701 /* add hints for poverty, wedding, solo, etc */
702 if($GT=="poverty" && $party=="re")
703 if($sickness=="poverty")
705 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
706 $cards = DB_get_all_hand($userhash);
707 $trumpNR = count_trump($cards);
709 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
711 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
714 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
718 if($sickness=="poverty")
720 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
721 $cards = DB_get_all_hand($userhash);
722 $trumpNR = count_trump($cards);
724 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
726 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
729 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
731 if($sickness=="poverty")
733 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
734 $cards = DB_get_all_hand($userhash);
735 $trumpNR = count_trump($cards);
737 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
739 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
742 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
744 if($GT=="wedding" && $party=="re")
745 if($sickness=="wedding")
746 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
748 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
750 if(ereg("solo",$GT) && $party=="re")
752 if(ereg("queen",$GT))
753 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
754 else if(ereg("jack",$GT))
755 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
756 else if(ereg("club",$GT))
757 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
758 else if(ereg("spade",$GT))
759 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
760 else if(ereg("heart",$GT))
761 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
762 else if(ereg("trumpless",$GT))
763 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
764 else if(ereg("trump",$GT))
765 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
768 /* add point calls */
772 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
774 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
778 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
781 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
784 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
787 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
793 echo " <span title=\"".date("Y-m-d H:i:s")."\">local time</span>\n";
794 echo " <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
798 echo "</div>\n"; /* end output table */
805 function display_user_menu()
807 global $wiki,$myid,$host;
808 echo "<div class=\"usermenu\">\n".
809 "<a href=\"index.php\"> go to my user page </a>";
811 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
812 " LEFT JOIN Game On Hand.game_id=Game.id".
813 " WHERE Hand.user_id='$myid'".
814 " AND Game.player='$myid'".
815 " AND Game.status<>'gameover'" );
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=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
824 echo "<hr /> <a href=\"".$host."?new\">start a new game</a>\n";
826 echo "<hr /> <a href=\"".substr($host,0,-9)."stats.php\">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";