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)
713 $gameid = DB_get_gameid_by_hash($hash);
714 $gametype = DB_get_gametype_by_gameid($gameid);
715 $oldcall = DB_get_call_by_hash($hash);
716 $pcall = DB_get_partner_call_by_hash($hash);
718 if( ($pcall!=NULL && $what >= $pcall) ||
719 ($oldcall!=NULL && $what >=$oldcall) )
724 $NRcards = count(DB_get_hand($hash));
729 $user = DB_get_hash_from_game_and_pos($gameid,$i);
730 $NRallcards += count(DB_get_hand($user));
733 /* in case of a wedding, everything will be delayed by an offset */
735 if($gametype=="wedding")
737 $offset = DB_get_sickness_by_gameid($gameid);
738 if ($offset <0) /* not resolved */
742 switch ($RULES["call"])
745 if( 4-($what/30) >= 12 - ($NRcards + $offset))
749 if( 27+4*($what/30) <= $NRallcards + $offset*4)
754 if($oldcall!=NULL && $pcall!=NULL)
755 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
756 else if($oldcall!=NULL)
758 else if ($pcall!=NULL)
763 if( 12 <= ($NRcards + $offset))
767 else if ( 9 <= ($NRcards + $offset))
769 if( ($mincall>=0 && $mincall==120) )
772 else if ( 6 <= ($NRcards + $offset))
774 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
777 else if ( 3 <= ($NRcards + $offset))
779 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
782 else if ( 0 <= ($NRcards + $offset))
784 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
793 function display_table ()
795 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
796 global $RULES,$GAME,$gametype;
798 $result = DB_query("SELECT User.fullname as name,".
799 " Hand.position as position, ".
801 " Hand.party as party, ".
802 " Hand.sickness as sickness, ".
803 " Hand.point_call, ".
804 " User.last_login, ".
809 "LEFT JOIN User ON User.id=Hand.user_id ".
810 "WHERE Hand.game_id='".$gameid."' ".
811 "ORDER BY position ASC");
813 echo "<div class=\"table\">\n".
814 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
815 while($r = DB_fetch_array($result))
826 $wins = DB_get_number_of_tricks($gameid,$pos);
827 date_default_timezone_set($defaulttimezone);
828 $lastlogin = strtotime($r[6]);
829 date_default_timezone_set($timezone);
830 $timenow = strtotime(date("Y-m-d H:i:s"));
831 $gravatar = "$name<br /><img class=\"gravatar\" title=\"$name\" src=\"http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=identicon\" />";
834 echo " <div class=\"table".($pos-1)."\">\n";
837 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
838 if($vacation = check_vacation($user))
840 $start = $vacation[0];
841 $stop = substr($vacation[1],0,10);
842 $comment = $vacation[2];
844 $title = "begin: $start end: $stop $comment";
845 echo " <span class=\"vacation\" title=\"$title\">$gravatar (on vacation until $stop)</span> \n";
848 echo " $gravatar \n";
852 /* add hints for poverty, wedding, solo, etc */
853 if( $gametype != "solo")
854 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
855 echo " Schweinchen. <br />";
857 if($GT=="poverty" && $party=="re")
858 if($sickness=="poverty")
860 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
861 $cards = DB_get_all_hand($userhash);
862 $trumpNR = count_trump($cards,'all');
864 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
866 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
869 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
873 if($sickness=="poverty")
875 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
876 $cards = DB_get_all_hand($userhash);
877 $trumpNR = count_trump($cards,'all');
879 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
881 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
884 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
886 if($sickness=="poverty")
888 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
889 $cards = DB_get_all_hand($userhash);
890 $trumpNR = count_trump($cards,'all');
892 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
894 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
897 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
899 if($GT=="wedding" && $party=="re")
900 if($sickness=="wedding")
901 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
903 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
905 if(ereg("solo",$GT) && $party=="re")
907 if(ereg("queen",$GT))
908 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
909 else if(ereg("jack",$GT))
910 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
911 else if(ereg("club",$GT))
912 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
913 else if(ereg("spade",$GT))
914 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
915 else if(ereg("heart",$GT))
916 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
917 else if(ereg("trumpless",$GT))
918 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
919 else if(ereg("trump",$GT))
920 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
923 /* add point calls */
927 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
929 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
933 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
936 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
939 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
942 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
948 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
949 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
950 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
953 /* show how many tricks the person made */
957 echo "#tricks 0"; break;
959 echo "#tricks 1"; break;
963 echo "#tricks few"; break;
965 echo "#tricks many"; break;
971 echo "</div>\n"; /* end output table */
978 function display_user_menu($id)
982 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
983 " LEFT JOIN Game On Hand.game_id=Game.id".
984 " WHERE Hand.user_id='$id'".
985 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
986 " AND ( Game.status='pre' OR Game.status='play' )".
987 " ORDER BY Game.session" );
990 while( $r = DB_fetch_array($result))
994 echo "<div class=\"usermenu\">\n";
995 echo "It's your turn in these games:<br />\n";
999 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
1000 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1013 function generate_score_table($session)
1015 /* returns an array with N entries
1016 * $score[$i]["gameid"] = gameid
1017 * $score[$i]["players"] = array (id=>total points)
1018 * $score[$i]["points"] = points for this game
1019 * $score[$i]["solo"] = 1 or 0
1025 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1027 if($gameids == NULL)
1030 /* get player id, names... from the first game */
1032 $result = DB_query("SELECT User.id, User.fullname from Hand".
1033 " LEFT JOIN User On Hand.user_id=User.id".
1034 " WHERE Hand.game_id=".$gameids[0]);
1035 while( $r = DB_fetch_array($result))
1038 /* get points and generate table */
1039 foreach($gameids as $gameid)
1041 $re_score = DB_get_score_by_gameid($gameid);
1042 $gametype = DB_get_gametype_by_gameid($gameid);
1043 foreach($player as $id=>$points)
1045 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1047 if($gametype=="solo")
1048 $player[$id] += 3*$re_score;
1050 $player[$id] += $re_score;
1051 else if ($party == "contra")
1052 $player[$id] -= $re_score;
1054 $score[$i]['gameid'] = $gameid ;
1055 $score[$i]['players'] = $player;
1056 $score[$i]['points'] = abs($re_score);
1057 $score[$i]['solo'] = ($gametype=="solo");
1065 function generate_global_score_table()
1070 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1072 if($gameids == NULL)
1075 /* get player id, names... from the User table */
1077 $result = DB_query('SELECT User.id, User.fullname FROM User');
1079 /* save information in an array */
1080 while( $r = DB_fetch_array($result))
1081 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 ,'nr' => 0);
1083 /* get points and generate table */
1084 foreach($gameids as $gameid)
1086 $re_score = DB_get_score_by_gameid($gameid);
1087 $gametype = DB_get_gametype_by_gameid($gameid);
1089 /* get players involved in this game */
1090 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1091 while($r = DB_fetch_array($result))
1094 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1096 if($gametype=='solo')
1097 $player[$id]['points'] += 3*$re_score;
1099 $player[$id]['points'] += $re_score;
1100 else if ($party == 'contra')
1101 $player[$id]['points'] -= $re_score;
1103 $player[$id]['nr']+=1;
1109 if($a['nr']==0 ) return 1;
1110 if($b['nr']==0) return 1;
1112 $a=$a['points']/$a['nr'];
1113 $b=$b['points']/$b['nr'];
1117 return ($a > $b) ? -1 : 1;
1119 usort($player,'cmp');
1121 foreach($player as $pl)
1123 /* limit to players with at least 10 games */
1125 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr']);
1131 function format_score_table_ascii($score)
1134 if(sizeof($score)==0)
1137 /* truncate table if we have too many games */
1138 $max = sizeof($score);
1139 if($max>6) $output.=" (table truncated to last 6 games)\n";
1142 foreach($score[0]['players'] as $id=>$points)
1144 $name = DB_get_name('userid',$id); /*TODO*/
1145 $output.= " ".substr($name,0,2)." |";
1148 $output.= "------+------+------+------+------+\n";
1150 /* output score for each game */
1152 foreach($score as $game)
1155 if($i-1<$max-6) continue;
1157 foreach($game['players'] as $id=>$points)
1158 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1159 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1161 /* check for solo */
1172 function format_score_table_html($score,$userid)
1176 if(sizeof($score)==0)
1179 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1183 $header.= " <thead>\n <tr>\n";
1184 $header.= " <th> No </th>";
1185 foreach($score[0]['players'] as $id=>$points)
1187 $name = DB_get_name('userid',$id); /*TODO*/
1188 $header.= "<th> ".substr($name,0,2)." </th>";
1190 $header.="<th>P</th>\n </tr>\n </thead>\n";
1192 /* use the same as footer */
1194 $footer.= " <tfoot>\n <tr>\n";
1195 $footer.= " <td> No </td>";
1196 foreach($score[0]['players'] as $id=>$points)
1198 $name = DB_get_name('userid',$id); /*TODO*/
1199 $footer.= "<td> ".substr($name,0,2)." </td>";
1201 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1205 $body.= " <tbody>\n";
1207 foreach($score as $game)
1211 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1212 /* create link to old games only if you are logged in and its your game*/
1213 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1214 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1216 $body.=" <td>$i</td>";
1218 foreach($game['players'] as $id=>$points)
1219 $body.="<td>".$points."</td>";
1220 $body.="<td>".$game['points'];
1222 /* check for solo */
1225 $body.="</td></tr>\n";
1233 $output.=" </tbody>\n</table>\n</div>\n";
1238 function createCache($content, $cacheFile)
1240 $fp = fopen($cacheFile,"w");
1243 fwrite($fp,$content);
1247 echo "WARNING: couldn't create cache file";
1252 function getCache($cacheFile, $expireTime)
1254 if( file_exists($cacheFile) &&
1255 filemtime($cacheFile )>( time() - $expireTime ) )
1257 return file_get_contents($cacheFile);
1263 function check_vacation($userid)
1265 /* get start date */
1266 $result = DB_query_array("SELECT value FROM User_Prefs".
1267 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1269 $start = $result[0];
1274 $result = DB_query_array("SELECT value FROM User_Prefs".
1275 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1282 $result = DB_query_array("SELECT value FROM User_Prefs".
1283 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1285 $comment = $result[0];
1289 /* check if user is on vacation. TODO: use user's timezone */
1290 if( (time() - strtotime($start) >0) &&
1291 (strtotime($stop) - time() >0))
1292 return array ($start,$stop,$comment);
1297 function cancel_game($why,$gameid)
1299 $gameid = DB_quote_smart($gameid);
1301 /* update the game table */
1305 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1308 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1311 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1314 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1317 /* set each player to gameover */
1318 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1319 while($r = DB_fetch_array($result))
1322 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));