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 /* check if user wants email right away or if we should save it in
62 * the database for later delivery
66 /* send to database (not yet implemented)*/
70 /* send email right away */
72 /* add standard header and footer */
73 $subject = "$EmailName".$subject;
75 /* standard goodbye */
76 $footer = "\nHave a nice day\n".
77 " your E-Doko service department\n\n".
79 "You can change your mail delivery mode in the preference menu.\n".
80 'web: http://doko.nubati.net '.
81 'help: http://wiki.nubati.net/EmailDoko '.
82 'bugs: http://wiki.nubati.net/EmailDokoIssues';
86 /* send email to more than one person */
88 $header = "Hello all\n\n";
90 foreach($uid as $user)
92 $all[] = DB_get_email('userid',$user);
94 $To = implode(",",$all);
98 /* standard greeting */
99 $name = DB_get_name('userid',$uid);
100 $header = "Hello $name\n\n";
102 $To = DB_get_email('userid',$uid);
105 sendmail($To,$subject,$header.$message.$footer);
109 function sendmail($To,$Subject,$message)
111 /* this function sends the mail or outputs to the screen in case of debugging */
112 global $debug,$EMAIL_REPLY;
116 if(isset($EMAIL_REPLY))
117 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
121 /* display email on screen,
124 $message = str_replace("\n","<br />\n",$message);
125 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
126 "<a href=\"\\0\">\\0</a>", $message);
128 echo "<br />To: $To<br />";
130 echo $header."<br />";
131 echo "Subject: $Subject <br />$message<br />\n";
135 mail($To,$Subject,$message,$header);
137 mail($To,$Subject,$message);
143 /* returns 1 if all names passed as args are defined by a GET or POST statement,
148 $args = func_get_args();
150 foreach($args as $arg)
152 $ok = $ok * isset($_REQUEST[$arg]);
153 /*echo "$arg: ok = $ok <br />";
159 function myerror($message)
161 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
162 sendmail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
166 function pos_array($c,$arr)
183 function is_trump($c)
187 if(in_array($c,$CARDS["trump"]))
193 function is_same_suite($c1,$c2)
197 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
198 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
199 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
200 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
201 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
206 function compare_cards($a,$b,$game)
208 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
214 /* first map all cards to the odd number,
215 * this insure that the first card wins the trick
216 * if they are the same card
218 if( $a/2 - (int)($a/2) != 0.5)
220 if( $b/2 - (int)($b/2) != 0.5)
223 /* check for schweinchen and ten of hearts*/
229 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
231 if($a == 19 || $a == 20 )
233 if($b == 19 || $b == 20 )
236 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
238 if($a == 19 || $a == 20 )
240 if($b == 19 || $b == 20 )
243 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
245 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
246 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
248 if($a == 19 || $a == 20 )
250 if($b == 19 || $b == 20 )
253 /* if not, do nothing and the foxes are just handeled as normal trump */
259 /* check for ten of hearts rule */
260 if($RULES["dullen"]=="secondwins")
261 if($a==1 && $b==1) /* both 10 of hearts */
262 return 0; /* second one wins.*/
266 /* no special cases here */
270 if(is_trump($a) && is_trump($b) && $a<=$b)
272 else if(is_trump($a) && is_trump($b) )
275 { /*$a is not a trump */
279 { /* both no trump */
282 $posA = pos_array($a,$CARDS["clubs"]);
283 $posB = pos_array($b,$CARDS["clubs"]);
291 $posA = pos_array($a,$CARDS["spades"]);
292 $posB = pos_array($b,$CARDS["spades"]);
300 $posA = pos_array($a,$CARDS["hearts"]);
301 $posB = pos_array($b,$CARDS["hearts"]);
309 $posA = pos_array($a,$CARDS["diamonds"]);
310 $posB = pos_array($b,$CARDS["diamonds"]);
317 /* not the same suit and no trump: a wins */
323 function get_winner($p,$mode)
325 /* get all 4 cards played in a trick, in the order they are played */
328 $c1pos = $tmp["pos"];
332 $c2pos = $tmp["pos"];
336 $c3pos = $tmp["pos"];
340 $c4pos = $tmp["pos"];
342 /* first card is better than all the rest */
343 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
346 /* second card is better than first and better than the rest */
347 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
350 /* third card is better than first card and better than last */
351 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
352 /* if second card is better than first, third card needs to be even better */
353 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
355 /* second is worse than first, e.g. not following suite */
356 else if (compare_cards($c1,$c2,$mode) )
359 /* non of the above */
363 function count_nines($cards)
367 foreach($cards as $c)
369 if($c == "25" || $c == "26") $nines++;
370 else if($c == "33" || $c == "34") $nines++;
371 else if($c == "41" || $c == "42") $nines++;
372 else if($c == "47" || $c == "48") $nines++;
378 function check_wedding($cards)
381 if( in_array("3",$cards) && in_array("4",$cards) )
387 function count_trump($cards,$status='pregame')
393 /* count each trump, including the foxes, since this is used to determine poverty status */
394 foreach($cards as $c)
398 /* In case we really want to know the amount of trump, we can use the status variable.
399 * This is needed for example to figure out what icon to display on the table in case of
400 * trump given back in poverty */
401 if($status=='all') return $trump;
403 /* normally foxes don't count as trump, so we substract them here
404 * in case someone has schweinchen, one or two of them should count as trump
405 * though, so we need to add one trump for those cases */
408 if( in_array("19",$cards))
410 if( in_array("20",$cards) )
413 /* handle case where player has schweinchen */
414 if( in_array("19",$cards) && in_array("20",$cards) )
415 switch($RULES["schweinchen"])
418 /* add two, in case the player has both foxes (schweinchen) */
423 case "secondaftercall":
424 /* add one, in case the player has both foxes (schweinchen) */
434 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
442 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
443 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
444 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
445 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
446 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
447 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
448 $r[ 6]=21; $r[18]=7; $r[30]=31; $r[42]=43;
449 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=44;
450 $r[ 8]=45; $r[20]=9; $r[32]=19; $r[44]=33;
451 $r[ 9]=46; $r[21]=10; $r[33]=20; $r[45]=24;
452 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
453 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
457 /* check if we can find a game were non of the player was involved and return
460 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
461 $randomnumbers = DB_get_unused_randomnumbers($userstr);
462 $randomnumbers = explode(":",$randomnumbers);
464 if(sizeof($randomnumbers)==48)
465 return $randomnumbers;
467 /* need to create new numbers */
471 /* shuffle using a better random generator than the standard one */
472 for ($i = 0; $i <48; $i++)
474 $j = @mt_rand(0, $i);
484 function display_cards($me,$myturn)
489 function have_suit($cards,$c)
494 if(in_array($c,$CARDS["trump"]))
495 $suite = $CARDS["trump"];
496 else if(in_array($c,$CARDS["clubs"]))
497 $suite = $CARDS["clubs"];
498 else if(in_array($c,$CARDS["spades"]))
499 $suite = $CARDS["spades"];
500 else if(in_array($c,$CARDS["hearts"]))
501 $suite = $CARDS["hearts"];
502 else if(in_array($c,$CARDS["diamonds"]))
503 $suite = $CARDS["diamonds"];
505 foreach($cards as $card)
507 if(in_array($card,$suite))
514 function same_type($card,$c)
519 /* figure out what kind of card c is */
520 if(in_array($c,$CARDS["trump"]))
521 $suite = $CARDS["trump"];
522 else if(in_array($c,$CARDS["clubs"]))
523 $suite = $CARDS["clubs"];
524 else if(in_array($c,$CARDS["spades"]))
525 $suite = $CARDS["spades"];
526 else if(in_array($c,$CARDS["hearts"]))
527 $suite = $CARDS["hearts"];
528 else if(in_array($c,$CARDS["diamonds"]))
529 $suite = $CARDS["diamonds"];
531 /* card is the same suid return 1 */
532 if(in_array($card,$suite))
538 function set_gametype($gametype)
552 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
553 '17','18','19','20','21','22','23','24','25','26');
554 $CARDS["diamonds"] = array();
555 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
556 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
557 $CARDS["hearts"] = array('43','44','45','46','47','48');
558 $CARDS["foxes"] = array('19','20');
559 if($RULES["dullen"]=='none')
561 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
562 '17','18','19','20','21','22','23','24','25','26');
563 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
565 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
566 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
568 /* find the fox and put them at the top of the stack */
569 foreach(array('19','20') as $fox)
572 $trump = $CARDS['trump'];
573 $key = array_keys($trump, $fox);
577 $foxa[]=$trump[$key[0]];
578 unset($trump[$key[0]]);
579 $trump = array_merge($foxa,$trump);
580 $CARDS['trump'] = $trump;
583 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
584 && $GAME['schweinchen-who'])
586 /* find the fox and put them at the top of the stack */
587 $trump = $CARDS['trump'];
588 $key = array_keys($trump, '19');
592 $foxa[]=$trump[$key[0]];
593 unset($trump[$key[0]]);
594 $trump = array_merge($foxa,$trump);
595 $CARDS['trump'] = $trump;
599 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
600 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
601 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
602 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
603 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
604 $CARDS["foxes"] = array();
607 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
608 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
609 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
610 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
611 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
612 $CARDS["foxes"] = array();
615 $CARDS["trump"] = array();
616 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
617 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
618 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
619 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
620 $CARDS["foxes"] = array();
623 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
624 '17','18','27','28','29','30','31','32','33','34');
625 $CARDS["clubs"] = array();
626 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
627 $CARDS["hearts"] = array('43','44','45','46','47','48');
628 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
629 $CARDS["foxes"] = array();
630 if($RULES["dullen"]=='none')
632 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
633 '17','18','27','28','29','30','31','32','33','34');
634 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
638 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
639 '17','18','35','36','37','38','39','40','41','42');
640 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
641 $CARDS["spades"] = array();
642 $CARDS["hearts"] = array('43','44','45','46','47','48');
643 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
644 $CARDS["foxes"] = array();
645 if($RULES["dullen"]=='none')
647 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
648 '17','18','35','36','37','38','39','40','41','42');
649 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
653 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
654 '17','18','43','44','45','46','47','48');
655 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
656 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
657 $CARDS["hearts"] = array();
658 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
659 $CARDS["foxes"] = array();
660 if($RULES["dullen"]=='none')
662 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
663 '17','18','43','44','1','2','45','46','47','48');
669 function mysort($cards,$gametype)
672 if(isset($PREF['sorting']))
673 if($PREF['sorting']=='high-low')
674 usort ( $cards, 'sort_comp_high_low' );
676 usort ( $cards, 'sort_comp_low_high' );
678 usort ( $cards, 'sort_comp_high_low' );
682 function sort_comp_high_low($a,$b)
687 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
688 $CARDS['hearts'],$CARDS['spades']);
690 return pos_array($a,$ALL)-pos_array($b,$ALL);
693 function sort_comp_low_high($a,$b)
698 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
699 $CARDS['hearts'],$CARDS['spades']);
701 return -pos_array($a,$ALL)+pos_array($b,$ALL);
704 function can_call($what,$hash)
708 $gameid = DB_get_gameid_by_hash($hash);
709 $gametype = DB_get_gametype_by_gameid($gameid);
710 $oldcall = DB_get_call_by_hash($hash);
711 $pcall = DB_get_partner_call_by_hash($hash);
713 if( ($pcall!=NULL && $what >= $pcall) ||
714 ($oldcall!=NULL && $what >=$oldcall) )
719 $NRcards = count(DB_get_hand($hash));
724 $user = DB_get_hash_from_game_and_pos($gameid,$i);
725 $NRallcards += count(DB_get_hand($user));
728 /* in case of a wedding, everything will be delayed by an offset */
730 if($gametype=="wedding")
732 $offset = DB_get_sickness_by_gameid($gameid);
733 if ($offset <0) /* not resolved */
737 switch ($RULES["call"])
740 if( 4-($what/30) >= 12 - ($NRcards + $offset))
744 if( 27+4*($what/30) <= $NRallcards + $offset*4)
749 if($oldcall!=NULL && $pcall!=NULL)
750 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
751 else if($oldcall!=NULL)
753 else if ($pcall!=NULL)
758 if( 12 <= ($NRcards + $offset))
762 else if ( 9 <= ($NRcards + $offset))
764 if( ($mincall>=0 && $mincall==120) )
767 else if ( 6 <= ($NRcards + $offset))
769 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
772 else if ( 3 <= ($NRcards + $offset))
774 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
777 else if ( 0 <= ($NRcards + $offset))
779 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
788 function display_table ()
790 global $gameid, $GT, $debug,$INDEX,$defaulttimezone,$session;
791 global $RULES,$GAME,$gametype;
793 $result = DB_query("SELECT User.fullname as name,".
794 " Hand.position as position, ".
796 " Hand.party as party, ".
797 " Hand.sickness as sickness, ".
798 " Hand.point_call, ".
799 " User.last_login, ".
803 "LEFT JOIN User ON User.id=Hand.user_id ".
804 "WHERE Hand.game_id='".$gameid."' ".
805 "ORDER BY position ASC");
807 echo "<div class=\"table\">\n".
808 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
809 while($r = DB_fetch_array($result))
819 $wins = DB_get_number_of_tricks($gameid,$pos);
820 date_default_timezone_set($defaulttimezone);
821 $lastlogin = strtotime($r[6]);
822 date_default_timezone_set($timezone);
823 $timenow = strtotime(date("Y-m-d H:i:s"));
825 echo " <div class=\"table".($pos-1)."\">\n";
828 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">";
829 if($vacation = check_vacation($user))
831 $start = $vacation[0];
832 $stop = substr($vacation[1],0,10);
833 $comment = $vacation[2];
835 $title = "begin: $start end: $stop $comment";
836 echo " <span class=\"vacation\" title=\"$title\">$name (on vacation until $stop)</span> \n";
843 /* add hints for poverty, wedding, solo, etc */
844 if( $gametype != "solo")
845 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
846 echo " Schweinchen. <br />";
848 if($GT=="poverty" && $party=="re")
849 if($sickness=="poverty")
851 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
852 $cards = DB_get_all_hand($userhash);
853 $trumpNR = count_trump($cards,'all');
855 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
857 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
860 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
864 if($sickness=="poverty")
866 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
867 $cards = DB_get_all_hand($userhash);
868 $trumpNR = count_trump($cards,'all');
870 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" title=\"poverty - trump back\" />";
872 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" title=\"poverty - no trump back\" />";
875 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
877 if($sickness=="poverty")
879 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
880 $cards = DB_get_all_hand($userhash);
881 $trumpNR = count_trump($cards,'all');
883 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" title=\"poverty2 - trump back\"/>";
885 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" title=\"poverty2 - no trump back\" />";
888 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" title=\"poverty2 partner\" />";
890 if($GT=="wedding" && $party=="re")
891 if($sickness=="wedding")
892 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" title=\"wedding\" />";
894 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
896 if(ereg("solo",$GT) && $party=="re")
898 if(ereg("queen",$GT))
899 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
900 else if(ereg("jack",$GT))
901 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
902 else if(ereg("club",$GT))
903 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
904 else if(ereg("spade",$GT))
905 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
906 else if(ereg("heart",$GT))
907 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
908 else if(ereg("trumpless",$GT))
909 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
910 else if(ereg("trump",$GT))
911 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
914 /* add point calls */
918 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" title=\"Re\" />";
920 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" title=\"Contra\" />";
924 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" title=\"Call 0\" />";
927 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" title=\"Call 30\" />";
930 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" title=\"Call 60\" />";
933 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" title=\"Call 90\" />";
939 echo " <span title=\"local time: ".date("Y-m-d H:i:s",$timenow). " ".
940 "last login: ".date("Y-m-d H:i:s",$lastlogin)."\">".
941 "<img src=\"pics/button/time-info.png\" class=\"tinybutton\" alt=\"time info\" />".
944 /* show how many tricks the person made */
948 echo "#tricks 0"; break;
950 echo "#tricks 1"; break;
954 echo "#tricks few"; break;
956 echo "#tricks many"; break;
962 echo "</div>\n"; /* end output table */
969 function display_user_menu($id)
973 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
974 " LEFT JOIN Game On Hand.game_id=Game.id".
975 " WHERE Hand.user_id='$id'".
976 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
977 " AND ( Game.status='pre' OR Game.status='play' )".
978 " ORDER BY Game.session" );
981 while( $r = DB_fetch_array($result))
985 echo "<div class=\"usermenu\">\n";
986 echo "It's your turn in these games:<br />\n";
990 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
991 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
1004 function generate_score_table($session)
1006 /* returns an array with N entries
1007 * $score[$i]["gameid"] = gameid
1008 * $score[$i]["players"] = array (id=>total points)
1009 * $score[$i]["points"] = points for this game
1010 * $score[$i]["solo"] = 1 or 0
1016 $gameids = DB_get_gameids_of_finished_games_by_session($session);
1018 if($gameids == NULL)
1021 /* get player id, names... from the first game */
1023 $result = DB_query("SELECT User.id, User.fullname from Hand".
1024 " LEFT JOIN User On Hand.user_id=User.id".
1025 " WHERE Hand.game_id=".$gameids[0]);
1026 while( $r = DB_fetch_array($result))
1029 /* get points and generate table */
1030 foreach($gameids as $gameid)
1032 $re_score = DB_get_score_by_gameid($gameid);
1033 $gametype = DB_get_gametype_by_gameid($gameid);
1034 foreach($player as $id=>$points)
1036 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1038 if($gametype=="solo")
1039 $player[$id] += 3*$re_score;
1041 $player[$id] += $re_score;
1042 else if ($party == "contra")
1043 $player[$id] -= $re_score;
1045 $score[$i]['gameid'] = $gameid ;
1046 $score[$i]['players'] = $player;
1047 $score[$i]['points'] = abs($re_score);
1048 $score[$i]['solo'] = ($gametype=="solo");
1056 function generate_global_score_table()
1061 $gameids = DB_get_gameids_of_finished_games_by_session(0);
1063 if($gameids == NULL)
1066 /* get player id, names... from the User table */
1068 $result = DB_query('SELECT User.id, User.fullname FROM User');
1070 /* save information in an array */
1071 while( $r = DB_fetch_array($result))
1072 $player[$r[0]] = array('name'=> $r[1], 'points' => 0 ,'nr' => 0);
1074 /* get points and generate table */
1075 foreach($gameids as $gameid)
1077 $re_score = DB_get_score_by_gameid($gameid);
1078 $gametype = DB_get_gametype_by_gameid($gameid);
1080 /* get players involved in this game */
1081 $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
1082 while($r = DB_fetch_array($result))
1085 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
1087 if($gametype=='solo')
1088 $player[$id]['points'] += 3*$re_score;
1090 $player[$id]['points'] += $re_score;
1091 else if ($party == 'contra')
1092 $player[$id]['points'] -= $re_score;
1094 $player[$id]['nr']+=1;
1100 if($a['nr']==0 ) return 1;
1101 if($b['nr']==0) return 1;
1103 $a=$a['points']/$a['nr'];
1104 $b=$b['points']/$b['nr'];
1108 return ($a > $b) ? -1 : 1;
1110 usort($player,'cmp');
1112 foreach($player as $pl)
1114 /* limit to players with at least 10 games */
1116 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr']);
1122 function format_score_table_ascii($score)
1125 if(sizeof($score)==0)
1128 /* truncate table if we have too many games */
1129 $max = sizeof($score);
1130 if($max>6) $output.=" (table truncated to last 6 games)\n";
1133 foreach($score[0]['players'] as $id=>$points)
1135 $name = DB_get_name('userid',$id); /*TODO*/
1136 $output.= " ".substr($name,0,2)." |";
1139 $output.= "------+------+------+------+------+\n";
1141 /* output score for each game */
1143 foreach($score as $game)
1146 if($i-1<$max-6) continue;
1148 foreach($game['players'] as $id=>$points)
1149 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1150 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1152 /* check for solo */
1163 function format_score_table_html($score,$userid)
1167 if(sizeof($score)==0)
1170 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
1174 $header.= " <thead>\n <tr>\n";
1175 $header.= " <th> No </th>";
1176 foreach($score[0]['players'] as $id=>$points)
1178 $name = DB_get_name('userid',$id); /*TODO*/
1179 $header.= "<th> ".substr($name,0,2)." </th>";
1181 $header.="<th>P</th>\n </tr>\n </thead>\n";
1183 /* use the same as footer */
1185 $footer.= " <tfoot>\n <tr>\n";
1186 $footer.= " <td> No </td>";
1187 foreach($score[0]['players'] as $id=>$points)
1189 $name = DB_get_name('userid',$id); /*TODO*/
1190 $footer.= "<td> ".substr($name,0,2)." </td>";
1192 $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
1196 $body.= " <tbody>\n";
1198 foreach($score as $game)
1202 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1203 /* create link to old games only if you are logged in and its your game*/
1204 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1205 $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1207 $body.=" <td>$i</td>";
1209 foreach($game['players'] as $id=>$points)
1210 $body.="<td>".$points."</td>";
1211 $body.="<td>".$game['points'];
1213 /* check for solo */
1216 $body.="</td></tr>\n";
1224 $output.=" </tbody>\n</table>\n</div>\n";
1229 function createCache($content, $cacheFile)
1231 $fp = fopen($cacheFile,"w");
1234 fwrite($fp,$content);
1238 echo "WARNING: couldn't create cache file";
1243 function getCache($cacheFile, $expireTime)
1245 if( file_exists($cacheFile) &&
1246 filemtime($cacheFile )>( time() - $expireTime ) )
1248 return file_get_contents($cacheFile);
1254 function check_vacation($userid)
1256 /* get start date */
1257 $result = DB_query_array("SELECT value FROM User_Prefs".
1258 " WHERE user_id='$userid' AND pref_key='vacation start'" );
1260 $start = $result[0];
1265 $result = DB_query_array("SELECT value FROM User_Prefs".
1266 " WHERE user_id='$userid' AND pref_key='vacation stop'" );
1273 $result = DB_query_array("SELECT value FROM User_Prefs".
1274 " WHERE user_id='$userid' AND pref_key='vacation comment'" );
1276 $comment = $result[0];
1280 /* check if user is on vacation. TODO: use user's timezone */
1281 if( (time() - strtotime($start) >0) &&
1282 (strtotime($stop) - time() >0))
1283 return array ($start,$stop,$comment);
1288 function cancel_game($why,$gameid)
1290 $gameid = DB_quote_smart($gameid);
1292 /* update the game table */
1296 DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
1299 DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
1302 DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
1305 DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
1308 /* set each player to gameover */
1309 $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
1310 while($r = DB_fetch_array($result))
1313 DB_query("UPDATE Hand SET status='gameover' WHERE id=".DB_quote_smart($id));