2 /* make sure that we are not called from outside the scripts,
3 * use a variable defined in config.php to check this
8 function config_check()
10 global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
12 /* check if some variables are set in the config file, else set defaults */
13 if(!isset($EmailName))
15 if(isset($EMAIL_REPLY))
17 ini_set("sendmail_from",$EMAIL_REPLY);
19 if(!isset($ADMIN_NAME))
22 echo "<h1>Setup not completed</h1>";
23 echo "You need to set \$ADMIN_NAME in config.php.";
27 if(!isset($ADMIN_EMAIL))
30 echo "<h1>Setup not completed</h1>";
31 echo "You need to set \$ADMIN_EMAIL in config.php. ".
32 "If something goes wrong an email will be send to this address.";
39 echo "<h1>Setup not completed</h1>";
40 echo "You need to set \$DB_work in config.php. ".
41 "If this is set to 1, the game will be suspended and one can work safely on the database.".
42 "The default should be 0 for the game to work.";
49 echo "Working on the database...please check back later.";
57 function mymail($uid,$subject,$message)
61 /* do we send the email right away or save it in the database? */
64 /* add standard header and footer */
65 $subject = "$EmailName".$subject;
67 /* standard goodbye */
68 $footer = "\nHave a nice day\n".
69 " your E-Doko service department\n\n".
71 "You can change your mail delivery mode in the preference menu.\n".
72 'web: http://doko.nubati.net '.
73 'help: http://wiki.nubati.net/EmailDoko '.
74 'bugs: http://wiki.nubati.net/EmailDokoIssues';
78 /* send email to more than one person */
80 $header = "Hello all\n\n";
82 foreach($uid as $user)
84 $all[] = DB_get_email('userid',$user);
86 $To = implode(",",$all);
90 /* standard greeting */
91 $name = DB_get_name('userid',$uid);
92 $header = "Hello $name\n\n";
94 $To = DB_get_email('userid',$uid);
96 /* check if user wants email right away or if we should save it in
97 * the database for later delivery
100 $PREF = DB_get_PREF($uid);
101 if( $PREF['digest'] != 'digest-off' )
106 sendmail($To,$subject,$header.$message.$footer);
109 /* store email in database */
110 DB_digest_insert_email($To,$message);
114 function sendmail($To,$Subject,$message)
116 /* this function sends the mail or outputs to the screen in case of debugging */
117 global $debug,$EMAIL_REPLY;
121 if(isset($EMAIL_REPLY))
122 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
126 /* display email on screen,
129 $message = str_replace("\n","<br />\n",$message);
130 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
131 "<a href=\"\\0\">\\0</a>", $message);
133 echo "<br />To: $To<br />";
135 echo $header."<br />";
136 echo "Subject: $Subject <br />$message<br />\n";
140 mail($To,$Subject,$message,$header);
142 mail($To,$Subject,$message);
148 /* returns 1 if all names passed as args are defined by a GET or POST statement,
153 $args = func_get_args();
155 foreach($args as $arg)
157 $ok = $ok * isset($_REQUEST[$arg]);
158 /*echo "$arg: ok = $ok <br />";
164 function myerror($message)
166 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
167 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
171 function pos_array($c,$arr)
188 function is_trump($c)
192 if(in_array($c,$CARDS["trump"]))
198 function is_same_suite($c1,$c2)
202 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
203 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
204 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
205 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
206 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
211 function compare_cards($a,$b,$game)
213 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
219 /* first map all cards to the odd number,
220 * this insure that the first card wins the trick
221 * if they are the same card
223 if( $a/2 - (int)($a/2) != 0.5)
225 if( $b/2 - (int)($b/2) != 0.5)
228 /* check for schweinchen and ten of hearts*/
234 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
236 if($a == 19 || $a == 20 )
238 if($b == 19 || $b == 20 )
241 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
243 if($a == 19 || $a == 20 )
245 if($b == 19 || $b == 20 )
248 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
250 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
251 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
253 if($a == 19 || $a == 20 )
255 if($b == 19 || $b == 20 )
258 /* if not, do nothing and the foxes are just handeled as normal trump */
264 /* check for ten of hearts rule */
265 if($RULES["dullen"]=="secondwins")
266 if($a==1 && $b==1) /* both 10 of hearts */
267 return 0; /* second one wins.*/
271 /* no special cases here */
275 if(is_trump($a) && is_trump($b) && $a<=$b)
277 else if(is_trump($a) && is_trump($b) )
280 { /*$a is not a trump */
284 { /* both no trump */
287 $posA = pos_array($a,$CARDS["clubs"]);
288 $posB = pos_array($b,$CARDS["clubs"]);
296 $posA = pos_array($a,$CARDS["spades"]);
297 $posB = pos_array($b,$CARDS["spades"]);
305 $posA = pos_array($a,$CARDS["hearts"]);
306 $posB = pos_array($b,$CARDS["hearts"]);
314 $posA = pos_array($a,$CARDS["diamonds"]);
315 $posB = pos_array($b,$CARDS["diamonds"]);
322 /* not the same suit and no trump: a wins */
328 function get_winner($p,$mode)
330 /* get all 4 cards played in a trick, in the order they are played */
333 $c1pos = $tmp["pos"];
337 $c2pos = $tmp["pos"];
341 $c3pos = $tmp["pos"];
345 $c4pos = $tmp["pos"];
347 /* first card is better than all the rest */
348 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
351 /* second card is better than first and better than the rest */
352 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
355 /* third card is better than first card and better than last */
356 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
357 /* if second card is better than first, third card needs to be even better */
358 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
360 /* second is worse than first, e.g. not following suite */
361 else if (compare_cards($c1,$c2,$mode) )
364 /* non of the above */
368 function count_nines($cards)
372 foreach($cards as $c)
374 if($c == "25" || $c == "26") $nines++;
375 else if($c == "33" || $c == "34") $nines++;
376 else if($c == "41" || $c == "42") $nines++;
377 else if($c == "47" || $c == "48") $nines++;
383 function check_wedding($cards)
386 if( in_array("3",$cards) && in_array("4",$cards) )
392 function count_trump($cards,$status='pregame')
398 /* count each trump, including the foxes, since this is used to determine poverty status */
399 foreach($cards as $c)
403 /* In case we really want to know the amount of trump, we can use the status variable.
404 * This is needed for example to figure out what icon to display on the table in case of
405 * trump given back in poverty */
406 if($status=='all') return $trump;
408 /* normally foxes don't count as trump, so we substract them here
409 * in case someone has schweinchen, one or two of them should count as trump
410 * though, so we need to add one trump for those cases */
413 if( in_array("19",$cards))
415 if( in_array("20",$cards) )
418 /* handle case where player has schweinchen */
419 if( in_array("19",$cards) && in_array("20",$cards) )
420 switch($RULES["schweinchen"])
423 /* add two, in case the player has both foxes (schweinchen) */
428 case "secondaftercall":
429 /* add one, in case the player has both foxes (schweinchen) */
439 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
447 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
448 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
449 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
450 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
451 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
452 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
453 $r[ 6]=21; $r[18]=7; $r[30]=31; $r[42]=43;
454 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=44;
455 $r[ 8]=45; $r[20]=9; $r[32]=19; $r[44]=33;
456 $r[ 9]=46; $r[21]=10; $r[33]=20; $r[45]=24;
457 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
458 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
462 /* check if we can find a game were non of the player was involved and return
465 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
466 $randomnumbers = DB_get_unused_randomnumbers($userstr);
467 $randomnumbers = explode(":",$randomnumbers);
469 if(sizeof($randomnumbers)==48)
470 return $randomnumbers;
472 /* need to create new numbers */
476 /* shuffle using a better random generator than the standard one */
477 for ($i = 0; $i <48; $i++)
479 $j = @mt_rand(0, $i);
489 function display_cards($me,$myturn)
494 function have_suit($cards,$c)
499 if(in_array($c,$CARDS["trump"]))
500 $suite = $CARDS["trump"];
501 else if(in_array($c,$CARDS["clubs"]))
502 $suite = $CARDS["clubs"];
503 else if(in_array($c,$CARDS["spades"]))
504 $suite = $CARDS["spades"];
505 else if(in_array($c,$CARDS["hearts"]))
506 $suite = $CARDS["hearts"];
507 else if(in_array($c,$CARDS["diamonds"]))
508 $suite = $CARDS["diamonds"];
510 foreach($cards as $card)
512 if(in_array($card,$suite))
519 function same_type($card,$c)
524 /* figure out what kind of card c is */
525 if(in_array($c,$CARDS["trump"]))
526 $suite = $CARDS["trump"];
527 else if(in_array($c,$CARDS["clubs"]))
528 $suite = $CARDS["clubs"];
529 else if(in_array($c,$CARDS["spades"]))
530 $suite = $CARDS["spades"];
531 else if(in_array($c,$CARDS["hearts"]))
532 $suite = $CARDS["hearts"];
533 else if(in_array($c,$CARDS["diamonds"]))
534 $suite = $CARDS["diamonds"];
536 /* card is the same suid return 1 */
537 if(in_array($card,$suite))
543 function set_gametype($gametype)
557 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
558 '17','18','19','20','21','22','23','24','25','26');
559 $CARDS["diamonds"] = array();
560 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
561 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
562 $CARDS["hearts"] = array('43','44','45','46','47','48');
563 $CARDS["foxes"] = array('19','20');
564 if($RULES["dullen"]=='none')
566 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
567 '17','18','19','20','21','22','23','24','25','26');
568 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
570 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
571 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
573 /* find the fox and put them at the top of the stack */
574 foreach(array('19','20') as $fox)
577 $trump = $CARDS['trump'];
578 $key = array_keys($trump, $fox);
582 $foxa[]=$trump[$key[0]];
583 unset($trump[$key[0]]);
584 $trump = array_merge($foxa,$trump);
585 $CARDS['trump'] = $trump;
588 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
589 && $GAME['schweinchen-who'])
591 /* find the fox and put them at the top of the stack */
592 $trump = $CARDS['trump'];
593 $key = array_keys($trump, '19');
597 $foxa[]=$trump[$key[0]];
598 unset($trump[$key[0]]);
599 $trump = array_merge($foxa,$trump);
600 $CARDS['trump'] = $trump;
604 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
605 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
606 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
607 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
608 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
609 $CARDS["foxes"] = array();
612 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
613 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
614 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
615 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
616 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
617 $CARDS["foxes"] = array();
620 $CARDS["trump"] = array();
621 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
622 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
623 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
624 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
625 $CARDS["foxes"] = array();
628 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
629 '17','18','27','28','29','30','31','32','33','34');
630 $CARDS["clubs"] = array();
631 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
632 $CARDS["hearts"] = array('43','44','45','46','47','48');
633 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
634 $CARDS["foxes"] = array();
635 if($RULES["dullen"]=='none')
637 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
638 '17','18','27','28','29','30','31','32','33','34');
639 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
643 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
644 '17','18','35','36','37','38','39','40','41','42');
645 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
646 $CARDS["spades"] = array();
647 $CARDS["hearts"] = array('43','44','45','46','47','48');
648 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
649 $CARDS["foxes"] = array();
650 if($RULES["dullen"]=='none')
652 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
653 '17','18','35','36','37','38','39','40','41','42');
654 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
658 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
659 '17','18','43','44','45','46','47','48');
660 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
661 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
662 $CARDS["hearts"] = array();
663 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
664 $CARDS["foxes"] = array();
665 if($RULES["dullen"]=='none')
667 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
668 '17','18','43','44','1','2','45','46','47','48');
674 function mysort($cards,$gametype)
677 if(isset($PREF['sorting']))
678 if($PREF['sorting']=='high-low')
679 usort ( $cards, 'sort_comp_high_low' );
681 usort ( $cards, 'sort_comp_low_high' );
683 usort ( $cards, 'sort_comp_high_low' );
687 function sort_comp_high_low($a,$b)
692 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
693 $CARDS['hearts'],$CARDS['spades']);
695 return pos_array($a,$ALL)-pos_array($b,$ALL);
698 function sort_comp_low_high($a,$b)
703 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
704 $CARDS['hearts'],$CARDS['spades']);
706 return -pos_array($a,$ALL)+pos_array($b,$ALL);
709 function can_call($what,$hash)
711 /* figure out if a person can make a call:
712 $what in 0,30,60,90,120 = points of the call
713 $hash = the hash of the person who wants to make the call
716 0 can't make that call
718 2 can make the call, but this is the last chance to do so...
723 /* get some information
725 $gameid = DB_get_gameid_by_hash($hash);
726 $gametype = DB_get_gametype_by_gameid($gameid);
727 $oldcall = DB_get_call_by_hash($hash); /* did the person already made a call? */
728 $pcall = DB_get_partner_call_by_hash($hash); /* did the partner already made a call */
731 /* you're call must be better than the one you or your partner already made
733 if( ($pcall!=NULL && ($what >= $pcall))
734 || ($oldcall!=NULL && ($what >=$oldcall)) )
739 /* for some rules we need to know how many cards people have
741 $NRcards = count(DB_get_hand($hash));
746 $user = DB_get_hash_from_game_and_pos($gameid,$i);
747 $NRallcards += count(DB_get_hand($user));
750 /* in case of a wedding, everything will be delayed by an offset
753 if($gametype=="wedding")
755 $offset = DB_get_sickness_by_gameid($gameid);
756 if ($offset <0) /* not resolved */
760 /* now check if the call is allowed depending on the rule set
762 switch ($RULES["call"])
765 /* calls can be made before/while you play your card...
766 * first card = 120, second card = 90, etc.
768 if( 4-($what/30) == 12 - ($NRcards + $offset))
770 if( 4-($what/30) > 12 - ($NRcards + $offset))
774 /* you can make the first call anytime during the first trick
776 if( 27+4*($what/30) == $NRallcards + $offset*4)
778 if( 27+4*($what/30) < $NRallcards + $offset*4)
782 /* you can call 120 with 12 cards, 90 with 9 or more cards, 60 with 6 or more, etc.
783 * you can't skip a call though
786 /* figure out last call
788 if($oldcall!=NULL && $pcall!=NULL)
789 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
790 else if($oldcall!=NULL)
792 else if ($pcall!=NULL)
798 if( 12 == ($NRcards + $offset))
802 else if( 12 < ($NRcards + $offset))
806 else if ( 9 == ($NRcards + $offset))
808 if( ($mincall>=0 && $mincall==120) )
811 else if ( 9 < ($NRcards + $offset))
813 if( ($mincall>=0 && $mincall==120) )
816 else if ( 6 == ($NRcards + $offset))
818 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
821 else if ( 6 < ($NRcards + $offset))
823 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
826 else if ( 3 == ($NRcards + $offset))
828 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
831 else if ( 3 < ($NRcards + $offset))
833 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
836 else if ( 0 == ($NRcards + $offset))
838 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
841 else if ( 0 < ($NRcards + $offset))
843 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
852 function display_table ()
854 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
855 global $RULES,$GAME,$gametype;
857 $result = DB_query("SELECT User.fullname as name,".
858 " Hand.position as position, ".
860 " Hand.party as party, ".
861 " Hand.sickness as sickness, ".
862 " Hand.point_call, ".
863 " User.last_login, ".
868 "LEFT JOIN User ON User.id=Hand.user_id ".
869 "WHERE Hand.game_id='".$gameid."' ".
870 "ORDER BY position ASC");
872 echo "<div class=\"table\">\n".
873 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
874 while($r = DB_fetch_array($result))
885 $wins = DB_get_number_of_tricks($gameid,$pos);
886 date_default_timezone_set($defaulttimezone);
887 $lastlogin = strtotime($r[6]);
888 date_default_timezone_set($timezone);
889 $timenow = strtotime(date("Y-m-d H:i:s"));
890 $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" />";
893 echo " <div class=\"table".($pos-1)."\">\n";
896 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
897 if($vacation = check_vacation($user))
899 $start = $vacation[0];
900 $stop = substr($vacation[1],0,10);
901 $comment = $vacation[2];
903 $title = "begin: $start end: $stop $comment";
904 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
907 echo " $gravatar \n";
911 /* add hints for poverty, wedding, solo, etc */
912 if( $gametype != "solo")
913 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
914 echo " Schweinchen. <br />";
916 if($GT=="poverty" && $party=="re")
917 if($sickness=="poverty")
919 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
920 $cards = DB_get_all_hand($userhash);
921 $trumpNR = count_trump($cards,'all');
923 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
925 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
928 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
932 if($sickness=="poverty")
934 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
935 $cards = DB_get_all_hand($userhash);
936 $trumpNR = count_trump($cards,'all');
938 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
940 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
943 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
945 if($sickness=="poverty")
947 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
948 $cards = DB_get_all_hand($userhash);
949 $trumpNR = count_trump($cards,'all');
951 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
953 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
956 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
958 if($GT=="wedding" && $party=="re")
959 if($sickness=="wedding")
960 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
962 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
964 if(ereg("solo",$GT) && $party=="re")
966 if(ereg("queen",$GT))
967 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
968 else if(ereg("jack",$GT))
969 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
970 else if(ereg("club",$GT))
971 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
972 else if(ereg("spade",$GT))
973 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
974 else if(ereg("heart",$GT))
975 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
976 else if(ereg("trumpless",$GT))
977 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
978 else if(ereg("trump",$GT))
979 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
982 /* add point calls */
986 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
988 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
992 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
995 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
998 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
1001 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
1007 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
1008 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
1009 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
1012 /* show how many tricks the person made */
1016 echo "#tricks 0"; break;
1018 echo "#tricks 1"; break;
1022 echo "#tricks few"; break;
1024 echo "#tricks many"; break;
1030 echo "</div>\n"; /* end output table */
1037 function display_user_menu($id)
1039 global $WIKI,$INDEX;
1041 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
1042 " LEFT JOIN Game On Hand.game_id=Game.id".
1043 " WHERE Hand.user_id='$id'".
1044 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
1045 " AND ( Game.status='pre' OR Game.status='play' )".
1046 " ORDER BY Game.session" );
1049 while( $r = DB_fetch_array($result))
1053 echo "<div class=\"usermenu\">\n";
1054 echo "It's your turn in these games:<br />\n";
1058 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
1059 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1072 function generate_score_table($session)
1074 /* returns an array with N entries
1075 * $score[$i]["gameid"] = gameid
1076 * $score[$i]["players"] = array (id=>total points)
1077 * $score[$i]["points"] = points for this game
1078 * $score[$i]["solo"] = 1 or 0
1084 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1086 if($gameids == NULL)
1089 /* get player id, names... from the first game */
1091 $result = DB_query("SELECT User.id, User.fullname from Hand".
1092 " LEFT JOIN User On Hand.user_id=User.id".
1093 " WHERE Hand.game_id=".$gameids[0]);
1094 while( $r = DB_fetch_array($result))
1097 /* get points and generate table */
1098 foreach($gameids as $gameid)
1100 $re_score = DB_get_score_by_gameid($gameid);
1101 $gametype = DB_get_gametype_by_gameid($gameid);
1102 foreach($player as $id=>$points)
1104 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1106 if($gametype=="solo")
1107 $player[$id] += 3*$re_score;
1109 $player[$id] += $re_score;
1110 else if ($party == "contra")
1111 $player[$id] -= $re_score;
1113 $score[$i]['gameid'] = $gameid ;
1114 $score[$i]['players'] = $player;
1115 $score[$i]['points'] = abs($re_score);
1116 $score[$i]['solo'] = ($gametype=="solo");
1124 function generate_global_score_table()
1129 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1131 if($gameids == NULL)
1134 /* get player id, names... from the User table */
1136 $result = DB_query('SELECT User.id, User.fullname FROM User');
1138 /* save information in an array */
1139 while( $r = DB_fetch_array($result))
1140 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 ,'nr' => 0);
1142 /* get points and generate table */
1143 foreach($gameids as $gameid)
1145 $re_score = DB_get_score_by_gameid($gameid);
1146 $gametype = DB_get_gametype_by_gameid($gameid);
1148 /* get players involved in this game */
1149 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1150 while($r = DB_fetch_array($result))
1153 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1155 if($gametype=='solo')
1156 $player[$id]['points'] += 3*$re_score;
1158 $player[$id]['points'] += $re_score;
1159 else if ($party == 'contra')
1160 $player[$id]['points'] -= $re_score;
1162 $player[$id]['nr']+=1;
1168 if($a['nr']==0 ) return 1;
1169 if($b['nr']==0) return 1;
1171 $a=$a['points']/$a['nr'];
1172 $b=$b['points']/$b['nr'];
1176 return ($a > $b) ? -1 : 1;
1178 usort($player,'cmp');
1180 foreach($player as $pl)
1182 /* limit to players with at least 10 games */
1184 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr']);
1190 function format_score_table_ascii($score)
1193 if(sizeof($score)==0)
1196 /* truncate table if we have too many games */
1197 $max = sizeof($score);
1198 if($max>6) $output.=" (table truncated to last 6 games)\n";
1201 foreach($score[0]['players'] as $id=>$points)
1203 $name = DB_get_name('userid',$id); /*TODO*/
1204 $output.= " ".substr($name,0,2)." |";
1207 $output.= "------+------+------+------+------+\n";
1209 /* output score for each game */
1211 foreach($score as $game)
1214 if($i-1<$max-6) continue;
1216 foreach($game['players'] as $id=>$points)
1217 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1218 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1220 /* check for solo */
1231 function format_score_table_html($score,$userid)
1235 if(sizeof($score)==0)
1238 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1242 $header.= " <thead>\n <tr>\n";
1243 $header.= " <th> No </th>";
1244 foreach($score[0]['players'] as $id=>$points)
1246 $name = DB_get_name('userid',$id); /*TODO*/
1247 $header.= "<th> ".substr($name,0,2)." </th>";
1249 $header.="<th>P</th>\n </tr>\n </thead>\n";
1251 /* use the same as footer */
1253 $footer.= " <tfoot>\n <tr>\n";
1254 $footer.= " <td> No </td>";
1255 foreach($score[0]['players'] as $id=>$points)
1257 $name = DB_get_name('userid',$id); /*TODO*/
1258 $footer.= "<td> ".substr($name,0,2)." </td>";
1260 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1264 $body.= " <tbody>\n";
1266 foreach($score as $game)
1270 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1271 /* create link to old games only if you are logged in and its your game*/
1272 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1273 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1275 $body.=" <td>$i</td>";
1277 foreach($game['players'] as $id=>$points)
1278 $body.="<td>".$points."</td>";
1279 $body.="<td>".$game['points'];
1281 /* check for solo */
1284 $body.="</td></tr>\n";
1292 $output.=" </tbody>\n</table>\n</div>\n";
1297 function createCache($content, $cacheFile)
1299 $fp = fopen($cacheFile,"w");
1302 fwrite($fp,$content);
1306 echo "WARNING: couldn't create cache file";
1311 function getCache($cacheFile, $expireTime)
1313 if( file_exists($cacheFile) &&
1314 filemtime($cacheFile )>( time() - $expireTime ) )
1316 return file_get_contents($cacheFile);
1322 function check_vacation($userid)
1324 /* get start date */
1325 $result = DB_query_array("SELECT value FROM User_Prefs".
1326 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1328 $start = $result[0];
1333 $result = DB_query_array("SELECT value FROM User_Prefs".
1334 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1341 $result = DB_query_array("SELECT value FROM User_Prefs".
1342 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1344 $comment = $result[0];
1348 /* check if user is on vacation. TODO: use user's timezone */
1349 if( (time() - strtotime($start) >0) &&
1350 (strtotime($stop) - time() >0))
1351 return array ($start,$stop,$comment);
1356 function cancel_game($why,$gameid)
1358 $gameid = DB_quote_smart($gameid);
1360 /* update the game table */
1364 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1367 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1370 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1373 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1376 /* set each player to gameover */
1377 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1378 while($r = DB_fetch_array($result))
1381 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));
1387 function get_user_token($userid)
1392 $date = DB_get_user_creation_date($userid);
1393 $name = DB_get_name('userid',$userid);
1396 $token = md5("token".$name.$date);