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($To,$Subject,$message,$header="")
59 global $debug,$EMAIL_REPLY;
61 if(isset($EMAIL_REPLY))
62 $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
66 /* display email on screen,
69 $message = str_replace("\n","<br />\n",$message);
70 $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
71 "<a href=\"\\0\">\\0</a>", $message);
73 echo "<br />To: $To<br />";
75 echo $header."<br />";
76 echo "Subject: $Subject <br />$message<br />\n";
80 mail($To,$Subject,$message,$header);
82 mail($To,$Subject,$message);
88 /* returns 1 if all names passed as args are defined by a GET or POST statement,
93 $args = func_get_args();
95 foreach($args as $arg)
97 $ok = $ok * isset($_REQUEST[$arg]);
98 /*echo "$arg: ok = $ok <br />";
104 function myerror($message)
106 echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
107 mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
111 function pos_array($c,$arr)
128 function is_trump($c)
132 if(in_array($c,$CARDS["trump"]))
138 function is_same_suite($c1,$c2)
142 if(in_array($c1,$CARDS["trump"] ) && in_array($c2,$CARDS["trump"] ) ) return 1;
143 if(in_array($c1,$CARDS["clubs"] ) && in_array($c2,$CARDS["clubs"] ) ) return 1;
144 if(in_array($c1,$CARDS["hearts"] ) && in_array($c2,$CARDS["hearts"] ) ) return 1;
145 if(in_array($c1,$CARDS["spades"] ) && in_array($c2,$CARDS["spades"] ) ) return 1;
146 if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
151 function compare_cards($a,$b,$game)
153 /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
159 /* first map all cards to the odd number,
160 * this insure that the first card wins the trick
161 * if they are the same card
163 if( $a/2 - (int)($a/2) != 0.5)
165 if( $b/2 - (int)($b/2) != 0.5)
168 /* check for schweinchen and ten of hearts*/
174 if($RULES['schweinchen']=='both' && $GAME['schweinchen-who'])
176 if($a == 19 || $a == 20 )
178 if($b == 19 || $b == 20 )
181 else if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
183 if($a == 19 || $a == 20 )
185 if($b == 19 || $b == 20 )
188 else if($RULES['schweinchen']=='secondaftercall' && $GAME['schweinchen-who'] && $GAME['schweinchen-second'] )
190 /* check if a call was made either by the player or his partner. If so activate Schweinchen rule. */
191 if(DB_get_call_by_hash($GAME['schweinchen-who']) || DB_get_partner_call_by_hash($GAME['schweinchen-who']) )
193 if($a == 19 || $a == 20 )
195 if($b == 19 || $b == 20 )
198 /* if not, do nothing and the foxes are just handeled as normal trump */
204 /* check for ten of hearts rule */
205 if($RULES["dullen"]=="secondwins")
206 if($a==1 && $b==1) /* both 10 of hearts */
207 return 0; /* second one wins.*/
211 /* no special cases here */
215 if(is_trump($a) && is_trump($b) && $a<=$b)
217 else if(is_trump($a) && is_trump($b) )
220 { /*$a is not a trump */
224 { /* both no trump */
227 $posA = pos_array($a,$CARDS["clubs"]);
228 $posB = pos_array($b,$CARDS["clubs"]);
236 $posA = pos_array($a,$CARDS["spades"]);
237 $posB = pos_array($b,$CARDS["spades"]);
245 $posA = pos_array($a,$CARDS["hearts"]);
246 $posB = pos_array($b,$CARDS["hearts"]);
254 $posA = pos_array($a,$CARDS["diamonds"]);
255 $posB = pos_array($b,$CARDS["diamonds"]);
262 /* not the same suit and no trump: a wins */
268 function get_winner($p,$mode)
270 /* get all 4 cards played in a trick, in the order they are played */
273 $c1pos = $tmp["pos"];
277 $c2pos = $tmp["pos"];
281 $c3pos = $tmp["pos"];
285 $c4pos = $tmp["pos"];
287 /* first card is better than all the rest */
288 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
291 /* second card is better than first and better than the rest */
292 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
295 /* third card is better than first card and better than last */
296 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
297 /* if second card is better than first, third card needs to be even better */
298 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
300 /* second is worse than first, e.g. not following suite */
301 else if (compare_cards($c1,$c2,$mode) )
304 /* non of the above */
308 function count_nines($cards)
312 foreach($cards as $c)
314 if($c == "25" || $c == "26") $nines++;
315 else if($c == "33" || $c == "34") $nines++;
316 else if($c == "41" || $c == "42") $nines++;
317 else if($c == "47" || $c == "48") $nines++;
323 function check_wedding($cards)
326 if( in_array("3",$cards) && in_array("4",$cards) )
332 function count_trump($cards)
338 /* count each trump, including the foxes */
339 foreach($cards as $c)
343 /* normally foxes don't count as trump, so we substract them here
344 * in case someone has schweinchen, one or two of them should count as trump
345 * though, so we need to add one trump for those cases */
348 if( in_array("19",$cards))
350 if( in_array("20",$cards) )
353 /* handle case where player has schweinchen */
354 if( in_array("19",$cards) && in_array("20",$cards) )
355 switch($RULES["schweinchen"])
358 /* add two, in case the player has both foxes (schweinchen) */
363 case "secondaftercall":
364 /* add one, in case the player has both foxes (schweinchen) */
374 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
382 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
383 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
384 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
385 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
386 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
387 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
388 $r[ 6]=21; $r[18]=7; $r[30]=31; $r[42]=43;
389 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=44;
390 $r[ 8]=45; $r[20]=9; $r[32]=19; $r[44]=33;
391 $r[ 9]=46; $r[21]=10; $r[33]=20; $r[45]=24;
392 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
393 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
397 /* check if we can find a game were non of the player was involved and return
400 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
401 $randomnumbers = DB_get_unused_randomnumbers($userstr);
402 $randomnumbers = explode(":",$randomnumbers);
404 if(sizeof($randomnumbers)==48)
405 return $randomnumbers;
407 /* need to create new numbers */
411 /* shuffle using a better random generator than the standard one */
412 for ($i = 0; $i <48; $i++)
414 $j = @mt_rand(0, $i);
424 function display_cards($me,$myturn)
429 function have_suit($cards,$c)
434 if(in_array($c,$CARDS["trump"]))
435 $suite = $CARDS["trump"];
436 else if(in_array($c,$CARDS["clubs"]))
437 $suite = $CARDS["clubs"];
438 else if(in_array($c,$CARDS["spades"]))
439 $suite = $CARDS["spades"];
440 else if(in_array($c,$CARDS["hearts"]))
441 $suite = $CARDS["hearts"];
442 else if(in_array($c,$CARDS["diamonds"]))
443 $suite = $CARDS["diamonds"];
445 foreach($cards as $card)
447 if(in_array($card,$suite))
454 function same_type($card,$c)
459 /* figure out what kind of card c is */
460 if(in_array($c,$CARDS["trump"]))
461 $suite = $CARDS["trump"];
462 else if(in_array($c,$CARDS["clubs"]))
463 $suite = $CARDS["clubs"];
464 else if(in_array($c,$CARDS["spades"]))
465 $suite = $CARDS["spades"];
466 else if(in_array($c,$CARDS["hearts"]))
467 $suite = $CARDS["hearts"];
468 else if(in_array($c,$CARDS["diamonds"]))
469 $suite = $CARDS["diamonds"];
471 /* card is the same suid return 1 */
472 if(in_array($card,$suite))
478 function set_gametype($gametype)
492 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
493 '17','18','19','20','21','22','23','24','25','26');
494 $CARDS["diamonds"] = array();
495 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
496 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
497 $CARDS["hearts"] = array('43','44','45','46','47','48');
498 $CARDS["foxes"] = array('19','20');
499 if($RULES["dullen"]=='none')
501 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
502 '17','18','19','20','21','22','23','24','25','26');
503 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
505 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
506 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
508 /* find the fox and put them at the top of the stack */
509 foreach(array('19','20') as $fox)
512 $trump = $CARDS['trump'];
513 $key = array_keys($trump, $fox);
517 $foxa[]=$trump[$key[0]];
518 unset($trump[$key[0]]);
519 $trump = array_merge($foxa,$trump);
520 $CARDS['trump'] = $trump;
523 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
524 && $GAME['schweinchen-who'])
526 /* find the fox and put them at the top of the stack */
527 $trump = $CARDS['trump'];
528 $key = array_keys($trump, '19');
532 $foxa[]=$trump[$key[0]];
533 unset($trump[$key[0]]);
534 $trump = array_merge($foxa,$trump);
535 $CARDS['trump'] = $trump;
539 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
540 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
541 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
542 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
543 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
544 $CARDS["foxes"] = array();
547 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
548 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
549 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
550 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
551 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
552 $CARDS["foxes"] = array();
555 $CARDS["trump"] = array();
556 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
557 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
558 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
559 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
560 $CARDS["foxes"] = array();
563 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
564 '17','18','27','28','29','30','31','32','33','34');
565 $CARDS["clubs"] = array();
566 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
567 $CARDS["hearts"] = array('43','44','45','46','47','48');
568 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
569 $CARDS["foxes"] = array();
570 if($RULES["dullen"]=='none')
572 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
573 '17','18','27','28','29','30','31','32','33','34');
574 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
578 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
579 '17','18','35','36','37','38','39','40','41','42');
580 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
581 $CARDS["spades"] = array();
582 $CARDS["hearts"] = array('43','44','45','46','47','48');
583 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
584 $CARDS["foxes"] = array();
585 if($RULES["dullen"]=='none')
587 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
588 '17','18','35','36','37','38','39','40','41','42');
589 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
593 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
594 '17','18','43','44','45','46','47','48');
595 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
596 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
597 $CARDS["hearts"] = array();
598 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
599 $CARDS["foxes"] = array();
600 if($RULES["dullen"]=='none')
602 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
603 '17','18','43','44','1','2','45','46','47','48');
609 function mysort($cards,$gametype)
612 if(isset($PREF['sorting']))
613 if($PREF['sorting']=='high-low')
614 usort ( $cards, 'sort_comp_high_low' );
616 usort ( $cards, 'sort_comp_low_high' );
618 usort ( $cards, 'sort_comp_high_low' );
622 function sort_comp_high_low($a,$b)
627 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
628 $CARDS['hearts'],$CARDS['spades']);
630 return pos_array($a,$ALL)-pos_array($b,$ALL);
633 function sort_comp_low_high($a,$b)
638 $ALL = array_merge($CARDS['trump'],$CARDS['diamonds'],$CARDS['clubs'],
639 $CARDS['hearts'],$CARDS['spades']);
641 return -pos_array($a,$ALL)+pos_array($b,$ALL);
644 function can_call($what,$hash)
648 $gameid = DB_get_gameid_by_hash($hash);
649 $gametype = DB_get_gametype_by_gameid($gameid);
650 $oldcall = DB_get_call_by_hash($hash);
651 $pcall = DB_get_partner_call_by_hash($hash);
653 if( ($pcall!=NULL && $what >= $pcall) ||
654 ($oldcall!=NULL && $what >=$oldcall) )
659 $NRcards = count(DB_get_hand($hash));
664 $user = DB_get_hash_from_game_and_pos($gameid,$i);
665 $NRallcards += count(DB_get_hand($user));
668 /* in case of a wedding, everything will be delayed by an offset */
670 if($gametype=="wedding")
672 $offset = DB_get_sickness_by_gameid($gameid);
673 if ($offset <0) /* not resolved */
677 switch ($RULES["call"])
680 if( 4-($what/30) >= 12 - ($NRcards + $offset))
684 if( 27+4*($what/30) <= $NRallcards + $offset*4)
689 if($oldcall!=NULL && $pcall!=NULL)
690 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
691 else if($oldcall!=NULL)
693 else if ($pcall!=NULL)
698 if( 12 <= ($NRcards + $offset))
702 else if ( 9 <= ($NRcards + $offset))
704 if( ($mincall>=0 && $mincall==120) )
707 else if ( 6 <= ($NRcards + $offset))
709 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
712 else if ( 3 <= ($NRcards + $offset))
714 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
717 else if ( 0 <= ($NRcards + $offset))
719 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
728 function display_table ()
730 global $gameid, $GT, $debug,$INDEX,$defaulttimezone;
731 global $RULES,$GAME,$gametype;
733 $result = DB_query("SELECT User.fullname as name,".
734 " Hand.position as position, ".
736 " Hand.party as party, ".
737 " Hand.sickness as sickness, ".
738 " Hand.point_call, ".
739 " User.last_login, ".
743 "LEFT JOIN User ON User.id=Hand.user_id ".
744 "WHERE Hand.game_id='".$gameid."' ".
745 "ORDER BY position ASC");
747 echo "<div class=\"table\">\n".
748 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
749 while($r = DB_fetch_array($result))
759 date_default_timezone_set($defaulttimezone);
760 $lastlogin = strtotime($r[6]);
761 date_default_timezone_set($timezone);
762 $timenow = strtotime(date("Y-m-d H:i:s"));
764 echo " <div class=\"table".($pos-1)."\">\n";
768 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">$name</a>\n";
770 /* add hints for poverty, wedding, solo, etc */
771 if( $gametype != "solo")
772 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
773 echo " Schweinchen. <br />";
775 if($GT=="poverty" && $party=="re")
776 if($sickness=="poverty")
778 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
779 $cards = DB_get_all_hand($userhash);
780 $trumpNR = count_trump($cards);
782 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
784 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
787 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
791 if($sickness=="poverty")
793 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
794 $cards = DB_get_all_hand($userhash);
795 $trumpNR = count_trump($cards);
797 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
799 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
802 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
804 if($sickness=="poverty")
806 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
807 $cards = DB_get_all_hand($userhash);
808 $trumpNR = count_trump($cards);
810 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
812 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
815 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
817 if($GT=="wedding" && $party=="re")
818 if($sickness=="wedding")
819 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
821 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
823 if(ereg("solo",$GT) && $party=="re")
825 if(ereg("queen",$GT))
826 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
827 else if(ereg("jack",$GT))
828 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
829 else if(ereg("club",$GT))
830 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
831 else if(ereg("spade",$GT))
832 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
833 else if(ereg("heart",$GT))
834 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
835 else if(ereg("trumpless",$GT))
836 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
837 else if(ereg("trump",$GT))
838 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
841 /* add point calls */
845 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
847 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
851 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
854 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
857 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
860 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
866 echo " <span title=\"".date("Y-m-d H:i:s",$timenow). "\">local time</span>\n";
867 echo " <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
871 echo "</div>\n"; /* end output table */
878 function display_user_menu($id)
882 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
883 " LEFT JOIN Game On Hand.game_id=Game.id".
884 " WHERE Hand.user_id='$id'".
885 " AND ( Game.player='$id' OR ISNULL(Game.player) )".
886 " AND Game.status<>'gameover'".
887 " ORDER BY Game.session" );
890 while( $r = DB_fetch_array($result))
894 echo "<div class=\"usermenu\">\n";
895 echo "It's your turn in these games:<br />\n";
899 echo "<a href=\"".$INDEX."?action=game&me=".$r[0].
900 "\">game ".DB_format_gameid($r[1])." </a><br />\n";
913 function generate_score_table($session)
915 /* returns an array with N entries
916 * $score[$i]["gameid"] = gameid
917 * $score[$i]["players"] = array (id=>total points)
918 * $score[$i]["points"] = points for this game
919 * $score[$i]["solo"] = 1 or 0
925 $gameids = DB_get_gameids_of_finished_games_by_session($session);
930 /* get player id, names... from the first game */
932 $result = DB_query("SELECT User.id, User.fullname from Hand".
933 " LEFT JOIN User On Hand.user_id=User.id".
934 " WHERE Hand.game_id=".$gameids[0]);
935 while( $r = DB_fetch_array($result))
938 /* get points and generate table */
939 foreach($gameids as $gameid)
941 $re_score = DB_get_score_by_gameid($gameid);
942 foreach($player as $id=>$points)
944 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
946 if(DB_get_gametype_by_gameid($gameid)=="solo")
947 $player[$id] += 3*$re_score;
949 $player[$id] += $re_score;
950 else if ($party == "contra")
951 $player[$id] -= $re_score;
953 $score[$i]['gameid'] = $gameid ;
954 $score[$i]['players'] = $player;
955 $score[$i]['points'] = abs($re_score);
956 $score[$i]['solo'] = (DB_get_gametype_by_gameid($gameid)=="solo");
964 function generate_global_score_table()
969 $gameids = DB_get_gameids_of_finished_games_by_session(0);
974 /* get player id, names... from the User table */
976 $result = DB_query("SELECT User.id, User.fullname FROM User");
978 while( $r = DB_fetch_array($result))
979 $player[] = array( 'id' => $r[0], 'name'=> $r[1], 'points' => 0 ,'nr' => 0);
981 /* get points and generate table */
982 foreach($gameids as $gameid)
984 $re_score = DB_get_score_by_gameid($gameid);
985 /* TODO: this shouldn't loop over all players, just the 4 players that are in the game */
986 foreach($player as $key=>$pl)
988 $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
990 if(DB_get_gametype_by_gameid($gameid)=="solo")
991 $player[$key]['points'] += 3*$re_score;
993 $player[$key]['points'] += $re_score;
994 else if ($party == "contra")
995 $player[$key]['points'] -= $re_score;
997 $player[$key]['nr']+=1;
1003 if($a['nr']==0 ) return 1;
1004 if($b['nr']==0) return 1;
1006 $a=$a['points']/$a['nr'];
1007 $b=$b['points']/$b['nr'];
1011 return ($a > $b) ? -1 : 1;
1013 usort($player,"cmp");
1015 foreach($player as $pl)
1017 /* limit to players with at least 10 games */
1019 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3) );
1025 function format_score_table_ascii($score)
1028 if(sizeof($score)==0)
1031 /* truncate table if we have too many games */
1032 $max = sizeof($score);
1033 if($max>6) $output.=" (table truncated to last 6 games)\n";
1036 foreach($score[0]['players'] as $id=>$points)
1038 $name = DB_get_name('userid',$id); /*TODO*/
1039 $output.= " ".substr($name,0,2)." |";
1042 $output.= "------+------+------+------+------+\n";
1044 /* output score for each game */
1046 foreach($score as $game)
1049 if($i-1<$max-6) continue;
1051 foreach($game['players'] as $id=>$points)
1052 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1053 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1055 /* check for solo */
1066 function format_score_table_html($score,$userid)
1070 if(sizeof($score)==0)
1073 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <thead>\n <tr>\n";
1076 $output.= " <th> No </th>";
1077 foreach($score[0]['players'] as $id=>$points)
1079 $name = DB_get_name('userid',$id); /*TODO*/
1080 $output.= "<th> ".substr($name,0,2)." </th>";
1082 $output.="<th>P</th>\n </tr>\n </thead>\n <tbody>\n";
1085 foreach($score as $game)
1089 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1090 /* create link to old games only if you are logged in and its your game*/
1091 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1092 $output.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1094 $output.=" <td>$i</td>";
1096 foreach($game['players'] as $id=>$points)
1097 $output.="<td>".$points."</td>";
1098 $output.="<td>".$game['points'];
1100 /* check for solo */
1103 $output.="</td></tr>\n";
1106 $output.=" </tbody>\n</table>\n</div>\n";
1111 function createCache($content, $cacheFile)
1113 $fp = fopen($cacheFile,"w");
1116 fwrite($fp,$content);
1120 echo "WARNING: couldn't create cache file";
1125 function getCache($cacheFile, $expireTime)
1127 if( file_exists($cacheFile) &&
1128 filemtime($cacheFile )>( time() - $expireTime ) )
1130 return file_get_contents($cacheFile);