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="")
58 $message = str_replace("\n","<br />\n",$message);
59 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
60 "<a href=\"\\0\">\\0</a>", $message);
62 echo "<br />To: $To<br />";
64 echo $header."<br />";
65 echo "Subject: $Subject <br />$message<br />\n";
69 mail($To,$Subject,$message,$header);
71 mail($To,$Subject,$message);
77 /* returns 1 if all names passed as args are defined by a GET or POST statement,
82 $args = func_get_args();
84 foreach($args as $arg)
86 $ok = $ok * isset($_REQUEST[$arg]);
87 /*echo "$arg: ok = $ok <br />";
93 function myerror($message)
95 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
96 mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
100 function pos_array($c,$arr)
117 function is_trump($c)
121 if(in_array($c,$CARDS["trump"]))
127 function is_same_suite($c1,$c2)
131 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
132 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
133 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
134 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
135 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
140 function compare_cards($a,$b,$game)
142 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
148 /* first map all cards to the odd number,
149 * this insure that the first card wins the trick
150 * if they are the same card
152 if( $a/2 - (int)($a/2) != 0.5)
154 if( $b/2 - (int)($b/2) != 0.5)
157 /* some special cases */
162 if($RULES["schweinchen"]=="both" && $GAME["schweinchen"])
164 if($a == 19 || $a == 20 )
166 if($b == 19 || $b == 20 )
169 if($RULES["schweinchen"]=="second" && $GAME["schweinchen"]==3)
171 if($a == 19 || $a == 20 )
173 if($b == 19 || $b == 20 )
180 if($RULES["dullen"]=="secondwins")
181 if($a==1 && $b==1) /* both 10 of hearts */
182 return 0; /* second one wins.*/
186 if(is_trump($a) && is_trump($b) && $a<=$b)
188 else if(is_trump($a) && is_trump($b) )
191 { /*$a is not a trump */
195 { /* both no trump */
198 $posA = pos_array($a,$CARDS["clubs"]);
199 $posB = pos_array($b,$CARDS["clubs"]);
207 $posA = pos_array($a,$CARDS["spades"]);
208 $posB = pos_array($b,$CARDS["spades"]);
216 $posA = pos_array($a,$CARDS["hearts"]);
217 $posB = pos_array($b,$CARDS["hearts"]);
225 $posA = pos_array($a,$CARDS["diamonds"]);
226 $posB = pos_array($b,$CARDS["diamonds"]);
233 /* not the same suit and no trump: a wins */
239 function get_winner($p,$mode)
241 /* get all 4 cards played in a trick, in the order they are played */
244 $c1pos = $tmp["pos"];
248 $c2pos = $tmp["pos"];
252 $c3pos = $tmp["pos"];
256 $c4pos = $tmp["pos"];
258 /* first card is better than all the rest */
259 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
262 /* second card is better than first and better than the rest */
263 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
266 /* third card is better than first card and better than last */
267 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
268 /* if second card is better than first, third card needs to be even better */
269 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
271 /* second is worse than first, e.g. not following suite */
272 else if (compare_cards($c1,$c2,$mode) )
275 /* non of the above */
279 function count_nines($cards)
283 foreach($cards as $c)
285 if($c == "25" || $c == "26") $nines++;
286 else if($c == "33" || $c == "34") $nines++;
287 else if($c == "41" || $c == "42") $nines++;
288 else if($c == "47" || $c == "48") $nines++;
294 function check_wedding($cards)
297 if( in_array("3",$cards) && in_array("4",$cards) )
303 function count_trump($cards)
309 /* count each trump */
310 foreach($cards as $c)
314 switch($RULES["schweinchen"])
319 case "secondaftercall":
320 /* add one, in case the player has both foxes (schweinchen) */
321 if( in_array("19",$cards) && in_array("20",$cards) )
325 if( in_array("19",$cards))
327 if( in_array("20",$cards) )
335 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
343 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
344 $r[ 1]=2; $r[13]=48; $r[25]=14; $r[37]=38;
345 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
346 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
347 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
348 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
349 $r[ 6]=19; $r[18]=7; $r[30]=31; $r[42]=43;
350 $r[ 7]=20; $r[19]=8; $r[31]=32; $r[43]=44;
351 $r[ 8]=45; $r[20]=9; $r[32]=21; $r[44]=33;
352 $r[ 9]=46; $r[21]=10; $r[33]=22; $r[45]=34;
353 $r[10]=35; $r[22]=11; $r[34]=23; $r[46]=25;
354 $r[11]=36; $r[23]=12; $r[35]=24; $r[47]=26;
358 /* check if we can find a game were non of the player was involved and return
361 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
362 $randomnumbers = DB_get_unused_randomnumbers($userstr);
363 $randomnumbers = explode(":",$randomnumbers);
365 if(sizeof($randomnumbers)==48)
366 return $randomnumbers;
368 /* need to create new numbers */
372 /* shuffle using a better random generator than the standard one */
373 for ($i = 0; $i <48; $i++)
375 $j = @mt_rand(0, $i);
385 function display_cards($me,$myturn)
390 function return_timezone($offset)
395 $zone = "Europe/Berlin";
398 $zone = "America/Vancouver";
401 $zone = "Pacific/Auckland";
404 $zone = "Europe/London";
410 function have_suit($cards,$c)
415 if(in_array($c,$CARDS["trump"]))
416 $suite = $CARDS["trump"];
417 else if(in_array($c,$CARDS["clubs"]))
418 $suite = $CARDS["clubs"];
419 else if(in_array($c,$CARDS["spades"]))
420 $suite = $CARDS["spades"];
421 else if(in_array($c,$CARDS["hearts"]))
422 $suite = $CARDS["hearts"];
423 else if(in_array($c,$CARDS["diamonds"]))
424 $suite = $CARDS["diamonds"];
426 foreach($cards as $card)
428 if(in_array($card,$suite))
435 function same_type($card,$c)
440 /* figure out what kind of card c is */
441 if(in_array($c,$CARDS["trump"]))
442 $suite = $CARDS["trump"];
443 else if(in_array($c,$CARDS["clubs"]))
444 $suite = $CARDS["clubs"];
445 else if(in_array($c,$CARDS["spades"]))
446 $suite = $CARDS["spades"];
447 else if(in_array($c,$CARDS["hearts"]))
448 $suite = $CARDS["hearts"];
449 else if(in_array($c,$CARDS["diamonds"]))
450 $suite = $CARDS["diamonds"];
452 /* card is the same suid return 1 */
453 if(in_array($card,$suite))
459 function set_gametype($gametype)
472 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
473 '17','18','19','20','21','22','23','24','25','26');
474 $CARDS["diamonds"] = array();
475 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
476 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
477 $CARDS["hearts"] = array('43','44','45','46','47','48');
478 $CARDS["foxes"] = array('19','20');
479 if($RULES["dullen"]=='none')
481 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
482 '17','18','19','20','21','22','23','24','25','26');
483 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
487 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
488 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
489 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
490 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
491 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
492 $CARDS["foxes"] = array();
495 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
496 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
497 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
498 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
499 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
500 $CARDS["foxes"] = array();
503 $CARDS["trump"] = array();
504 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
505 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
506 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
507 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
508 $CARDS["foxes"] = array();
511 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
512 '17','18','27','28','29','30','31','32','33','34');
513 $CARDS["clubs"] = array();
514 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
515 $CARDS["hearts"] = array('43','44','45','46','47','48');
516 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
517 $CARDS["foxes"] = array();
518 if($RULES["dullen"]=='none')
520 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
521 '17','18','27','28','29','30','31','32','33','34');
522 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
526 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
527 '17','18','35','36','37','38','39','40','41','42');
528 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
529 $CARDS["spades"] = array();
530 $CARDS["hearts"] = array('43','44','45','46','47','48');
531 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
532 $CARDS["foxes"] = array();
533 if($RULES["dullen"]=='none')
535 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
536 '17','18','35','36','37','38','39','40','41','42');
537 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
541 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
542 '17','18','43','44','45','46','47','48');
543 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
544 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
545 $CARDS["hearts"] = array();
546 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
547 $CARDS["foxes"] = array();
548 if($RULES["dullen"]=='none')
550 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
551 '17','18','43','44','1','2','45','46','47','48');
557 function mysort($cards,$gametype)
559 usort ( $cards, "sort_comp" );
563 function sort_comp($a,$b)
568 $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
569 $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
571 return pos_array($a,$ALL)-pos_array($b,$ALL);
574 function can_call($what,$hash)
578 $gameid = DB_get_gameid_by_hash($hash);
579 $gametype = DB_get_gametype_by_gameid($gameid);
580 $oldcall = DB_get_call_by_hash($hash);
581 $pcall = DB_get_partner_call_by_hash($hash);
583 if( ($pcall!=NULL && $what >= $pcall) ||
584 ($oldcall!=NULL && $what >=$oldcall) )
589 $NRcards = count(DB_get_hand($hash));
594 $user = DB_get_hash_from_game_and_pos($gameid,$i);
595 $NRallcards += count(DB_get_hand($user));
598 /* in case of a wedding, everything will be delayed by an offset */
600 if($gametype=="wedding")
602 $offset = DB_get_sickness_by_gameid($gameid);
603 if ($offset <0) /* not resolved */
607 switch ($RULES["call"])
610 if( 4-($what/30) >= 12 - ($NRcards + $offset))
614 if( 27+4*($what/30) <= $NRallcards + $offset*4)
619 if($oldcall!=NULL && $pcall!=NULL)
620 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
621 else if($oldcall!=NULL)
623 else if ($pcall!=NULL)
628 if( 12 <= ($NRcards + $offset))
632 else if ( 9 <= ($NRcards + $offset))
634 if( ($mincall>=0 && $mincall==120) )
637 else if ( 6 <= ($NRcards + $offset))
639 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
642 else if ( 3 <= ($NRcards + $offset))
644 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
647 else if ( 0 <= ($NRcards + $offset))
649 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )