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 if($RULES['schweinchen']=='second' && $GAME['schweinchen-second'])
183 if($a == 19 || $a == 20 )
185 if($b == 19 || $b == 20 )
191 /* check for ten of hearts rule */
192 if($RULES["dullen"]=="secondwins")
193 if($a==1 && $b==1) /* both 10 of hearts */
194 return 0; /* second one wins.*/
198 /* no special cases here */
202 if(is_trump($a) && is_trump($b) && $a<=$b)
204 else if(is_trump($a) && is_trump($b) )
207 { /*$a is not a trump */
211 { /* both no trump */
214 $posA = pos_array($a,$CARDS["clubs"]);
215 $posB = pos_array($b,$CARDS["clubs"]);
223 $posA = pos_array($a,$CARDS["spades"]);
224 $posB = pos_array($b,$CARDS["spades"]);
232 $posA = pos_array($a,$CARDS["hearts"]);
233 $posB = pos_array($b,$CARDS["hearts"]);
241 $posA = pos_array($a,$CARDS["diamonds"]);
242 $posB = pos_array($b,$CARDS["diamonds"]);
249 /* not the same suit and no trump: a wins */
255 function get_winner($p,$mode)
257 /* get all 4 cards played in a trick, in the order they are played */
260 $c1pos = $tmp["pos"];
264 $c2pos = $tmp["pos"];
268 $c3pos = $tmp["pos"];
272 $c4pos = $tmp["pos"];
274 /* first card is better than all the rest */
275 if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
278 /* second card is better than first and better than the rest */
279 if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
282 /* third card is better than first card and better than last */
283 if( !compare_cards($c1,$c3,$mode) && compare_cards($c3,$c4,$mode) )
284 /* if second card is better than first, third card needs to be even better */
285 if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
287 /* second is worse than first, e.g. not following suite */
288 else if (compare_cards($c1,$c2,$mode) )
291 /* non of the above */
295 function count_nines($cards)
299 foreach($cards as $c)
301 if($c == "25" || $c == "26") $nines++;
302 else if($c == "33" || $c == "34") $nines++;
303 else if($c == "41" || $c == "42") $nines++;
304 else if($c == "47" || $c == "48") $nines++;
310 function check_wedding($cards)
313 if( in_array("3",$cards) && in_array("4",$cards) )
319 function count_trump($cards)
325 /* count each trump, including the foxes */
326 foreach($cards as $c)
330 /* normally foxes don't count as trump, so we substract them here
331 * in case someone has schweinchen, one or two of them should count as trump
332 * though, so we need to add one trump for those cases */
335 if( in_array("19",$cards))
337 if( in_array("20",$cards) )
340 /* handle case where player has schweinchen */
341 if( in_array("19",$cards) && in_array("20",$cards) )
342 switch($RULES["schweinchen"])
345 /* add two, in case the player has both foxes (schweinchen) */
350 case "secondaftercall":
351 /* add one, in case the player has both foxes (schweinchen) */
361 function create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
369 $r[ 0]=1; $r[12]=47; $r[24]=13; $r[36]=37;
370 $r[ 1]=2; $r[13]=23; $r[25]=14; $r[37]=38;
371 $r[ 2]=3; $r[14]=27; $r[26]=15; $r[38]=39;
372 $r[ 3]=4; $r[15]=16; $r[27]=28; $r[39]=40;
373 $r[ 4]=5; $r[16]=17; $r[28]=29; $r[40]=41;
374 $r[ 5]=18; $r[17]=6; $r[29]=30; $r[41]=42;
375 $r[ 6]=21; $r[18]=7; $r[30]=31; $r[42]=43;
376 $r[ 7]=22; $r[19]=8; $r[31]=32; $r[43]=44;
377 $r[ 8]=45; $r[20]=9; $r[32]=19; $r[44]=33;
378 $r[ 9]=46; $r[21]=10; $r[33]=20; $r[45]=24;
379 $r[10]=35; $r[22]=11; $r[34]=48; $r[46]=25;
380 $r[11]=36; $r[23]=12; $r[35]=34; $r[47]=26;
384 /* check if we can find a game were non of the player was involved and return
387 $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
388 $randomnumbers = DB_get_unused_randomnumbers($userstr);
389 $randomnumbers = explode(":",$randomnumbers);
391 if(sizeof($randomnumbers)==48)
392 return $randomnumbers;
394 /* need to create new numbers */
398 /* shuffle using a better random generator than the standard one */
399 for ($i = 0; $i <48; $i++)
401 $j = @mt_rand(0, $i);
411 function display_cards($me,$myturn)
416 function have_suit($cards,$c)
421 if(in_array($c,$CARDS["trump"]))
422 $suite = $CARDS["trump"];
423 else if(in_array($c,$CARDS["clubs"]))
424 $suite = $CARDS["clubs"];
425 else if(in_array($c,$CARDS["spades"]))
426 $suite = $CARDS["spades"];
427 else if(in_array($c,$CARDS["hearts"]))
428 $suite = $CARDS["hearts"];
429 else if(in_array($c,$CARDS["diamonds"]))
430 $suite = $CARDS["diamonds"];
432 foreach($cards as $card)
434 if(in_array($card,$suite))
441 function same_type($card,$c)
446 /* figure out what kind of card c is */
447 if(in_array($c,$CARDS["trump"]))
448 $suite = $CARDS["trump"];
449 else if(in_array($c,$CARDS["clubs"]))
450 $suite = $CARDS["clubs"];
451 else if(in_array($c,$CARDS["spades"]))
452 $suite = $CARDS["spades"];
453 else if(in_array($c,$CARDS["hearts"]))
454 $suite = $CARDS["hearts"];
455 else if(in_array($c,$CARDS["diamonds"]))
456 $suite = $CARDS["diamonds"];
458 /* card is the same suid return 1 */
459 if(in_array($card,$suite))
465 function set_gametype($gametype)
479 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
480 '17','18','19','20','21','22','23','24','25','26');
481 $CARDS["diamonds"] = array();
482 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
483 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
484 $CARDS["hearts"] = array('43','44','45','46','47','48');
485 $CARDS["foxes"] = array('19','20');
486 if($RULES["dullen"]=='none')
488 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
489 '17','18','19','20','21','22','23','24','25','26');
490 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
492 /* do we need to reorder for Schweinchen? need to search for it because of special case for dullen above*/
493 if($RULES['schweinchen']=='both'&& $GAME['schweinchen-who'])
495 /* find the fox and put them at the top of the stack */
496 foreach(array('19','20') as $fox)
499 $trump = $CARDS['trump'];
500 $key = array_keys($trump, $fox);
504 $foxa[]=$trump[$key[0]];
505 unset($trump[$key[0]]);
506 $trump = array_merge($foxa,$trump);
507 $CARDS['trump'] = $trump;
510 else if( ($RULES['schweinchen']=='second' || $RULES['schweinchen']=='secondaftercall')
511 && $GAME['schweinchen-who'])
513 /* find the fox and put them at the top of the stack */
514 $trump = $CARDS['trump'];
515 $key = array_keys($trump, '19');
519 $foxa[]=$trump[$key[0]];
520 unset($trump[$key[0]]);
521 $trump = array_merge($foxa,$trump);
522 $CARDS['trump'] = $trump;
526 $CARDS["trump"] = array('3','4','5','6','7','8','9','10');
527 $CARDS["clubs"] = array('27','28','29','30','31','32','11','12','33','34');
528 $CARDS["spades"] = array('35','36','37','38','39','40','13','14','41','42');
529 $CARDS["hearts"] = array('43','44', '1', '2','45','46','15','16','47','48');
530 $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
531 $CARDS["foxes"] = array();
534 $CARDS["trump"] = array('11','12','13','14','15','16','17','18');
535 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','33','34');
536 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','41','42');
537 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','47','48');
538 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
539 $CARDS["foxes"] = array();
542 $CARDS["trump"] = array();
543 $CARDS["clubs"] = array('27','28','29','30','31','32','3', '4','11','12','33','34');
544 $CARDS["spades"] = array('35','36','37','38','39','40','5', '6','13','14','41','42');
545 $CARDS["hearts"] = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
546 $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
547 $CARDS["foxes"] = array();
550 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
551 '17','18','27','28','29','30','31','32','33','34');
552 $CARDS["clubs"] = array();
553 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
554 $CARDS["hearts"] = array('43','44','45','46','47','48');
555 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
556 $CARDS["foxes"] = array();
557 if($RULES["dullen"]=='none')
559 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
560 '17','18','27','28','29','30','31','32','33','34');
561 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
565 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
566 '17','18','35','36','37','38','39','40','41','42');
567 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
568 $CARDS["spades"] = array();
569 $CARDS["hearts"] = array('43','44','45','46','47','48');
570 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
571 $CARDS["foxes"] = array();
572 if($RULES["dullen"]=='none')
574 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
575 '17','18','35','36','37','38','39','40','41','42');
576 $CARDS["hearts"] = array('43','44','1','2','45','46','47','48');
580 $CARDS["trump"] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
581 '17','18','43','44','45','46','47','48');
582 $CARDS["clubs"] = array('27','28','29','30','31','32','33','34');
583 $CARDS["spades"] = array('35','36','37','38','39','40','41','42');
584 $CARDS["hearts"] = array();
585 $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
586 $CARDS["foxes"] = array();
587 if($RULES["dullen"]=='none')
589 $CARDS["trump"] = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
590 '17','18','43','44','1','2','45','46','47','48');
596 function mysort($cards,$gametype)
598 usort ( $cards, "sort_comp" );
602 function sort_comp($a,$b)
607 $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
608 $CARDS["hearts"],$CARDS["spades"]);
610 return pos_array($a,$ALL)-pos_array($b,$ALL);
613 function can_call($what,$hash)
617 $gameid = DB_get_gameid_by_hash($hash);
618 $gametype = DB_get_gametype_by_gameid($gameid);
619 $oldcall = DB_get_call_by_hash($hash);
620 $pcall = DB_get_partner_call_by_hash($hash);
622 if( ($pcall!=NULL && $what >= $pcall) ||
623 ($oldcall!=NULL && $what >=$oldcall) )
628 $NRcards = count(DB_get_hand($hash));
633 $user = DB_get_hash_from_game_and_pos($gameid,$i);
634 $NRallcards += count(DB_get_hand($user));
637 /* in case of a wedding, everything will be delayed by an offset */
639 if($gametype=="wedding")
641 $offset = DB_get_sickness_by_gameid($gameid);
642 if ($offset <0) /* not resolved */
646 switch ($RULES["call"])
649 if( 4-($what/30) >= 12 - ($NRcards + $offset))
653 if( 27+4*($what/30) <= $NRallcards + $offset*4)
658 if($oldcall!=NULL && $pcall!=NULL)
659 $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
660 else if($oldcall!=NULL)
662 else if ($pcall!=NULL)
667 if( 12 <= ($NRcards + $offset))
671 else if ( 9 <= ($NRcards + $offset))
673 if( ($mincall>=0 && $mincall==120) )
676 else if ( 6 <= ($NRcards + $offset))
678 if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
681 else if ( 3 <= ($NRcards + $offset))
683 if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
686 else if ( 0 <= ($NRcards + $offset))
688 if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
697 function display_table ()
699 global $gameid, $GT, $debug,$INDEX,$defaulttimezone;
700 global $RULES,$GAME,$gametype;
702 $result = DB_query("SELECT User.fullname as name,".
703 " Hand.position as position, ".
705 " Hand.party as party, ".
706 " Hand.sickness as sickness, ".
707 " Hand.point_call, ".
708 " User.last_login, ".
712 "LEFT JOIN User ON User.id=Hand.user_id ".
713 "WHERE Hand.game_id='".$gameid."' ".
714 "ORDER BY position ASC");
716 echo "<div class=\"table\">\n".
717 " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
718 while($r = DB_fetch_array($result))
728 date_default_timezone_set($defaulttimezone);
729 $lastlogin = strtotime($r[6]);
730 date_default_timezone_set($timezone);
731 $timenow = strtotime(date("Y-m-d H:i:s"));
733 echo " <div class=\"table".($pos-1)."\">\n";
737 echo " <a href=\"".$INDEX."?action=game&me=".$hash."\">$name</a>\n";
739 /* add hints for poverty, wedding, solo, etc */
740 if( $gametype != "solo")
741 if( $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash )
742 echo " Schweinchen. <br />";
744 if($GT=="poverty" && $party=="re")
745 if($sickness=="poverty")
747 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
748 $cards = DB_get_all_hand($userhash);
749 $trumpNR = count_trump($cards);
751 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
753 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
756 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
760 if($sickness=="poverty")
762 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
763 $cards = DB_get_all_hand($userhash);
764 $trumpNR = count_trump($cards);
766 echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
768 echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
771 echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
773 if($sickness=="poverty")
775 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
776 $cards = DB_get_all_hand($userhash);
777 $trumpNR = count_trump($cards);
779 echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
781 echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
784 echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
786 if($GT=="wedding" && $party=="re")
787 if($sickness=="wedding")
788 echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
790 echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
792 if(ereg("solo",$GT) && $party=="re")
794 if(ereg("queen",$GT))
795 echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
796 else if(ereg("jack",$GT))
797 echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
798 else if(ereg("club",$GT))
799 echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
800 else if(ereg("spade",$GT))
801 echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
802 else if(ereg("heart",$GT))
803 echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
804 else if(ereg("trumpless",$GT))
805 echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
806 else if(ereg("trump",$GT))
807 echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
810 /* add point calls */
814 echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
816 echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
820 echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
823 echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
826 echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
829 echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
835 echo " <span title=\"".date("Y-m-d H:i:s",$timenow). "\">local time</span>\n";
836 echo " <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
840 echo "</div>\n"; /* end output table */
847 function display_user_menu()
851 /* get the id we are looking for */
852 if(isset($_SESSION['id']))
853 $myid = $_SESSION['id'];
857 $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
858 " LEFT JOIN Game On Hand.game_id=Game.id".
859 " WHERE Hand.user_id='$myid'".
860 " AND ( Game.player='$myid' OR ISNULL(Game.player) )".
861 " AND Game.status<>'gameover'".
862 " ORDER BY Game.session" );
865 while( $r = DB_fetch_array($result))
869 echo "<div class=\"usermenu\">\n";
870 echo "It's your turn in these games:<br />\n";
874 echo "<a href=\"".$INDEX."?action=game&me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
887 function generate_score_table($session)
889 /* returns an array with N entries
890 * $score[$i]["gameid"] = gameid
891 * $score[$i]["players"] = array (id=>total points)
892 * $score[$i]["points"] = points for this game
893 * $score[$i]["solo"] = 1 or 0
899 $gameids = DB_get_gameids_of_finished_games_by_session($session);
904 /* get player id, names... from the first game */
906 $result = DB_query("SELECT User.id, User.fullname from Hand".
907 " LEFT JOIN User On Hand.user_id=User.id".
908 " WHERE Hand.game_id=".$gameids[0]);
909 while( $r = DB_fetch_array($result))
912 /* get points and generate table */
913 foreach($gameids as $gameid)
915 $re_score = DB_get_score_by_gameid($gameid);
916 foreach($player as $id=>$points)
918 $party = DB_get_party_by_gameid_and_userid($gameid,$id);
920 if(DB_get_gametype_by_gameid($gameid)=="solo")
921 $player[$id] += 3*$re_score;
923 $player[$id] += $re_score;
924 else if ($party == "contra")
925 $player[$id] -= $re_score;
927 $score[$i]['gameid'] = $gameid ;
928 $score[$i]['players'] = $player;
929 $score[$i]['points'] = abs($re_score);
930 $score[$i]['solo'] = (DB_get_gametype_by_gameid($gameid)=="solo");
938 function generate_global_score_table()
943 $gameids = DB_get_gameids_of_finished_games_by_session(0);
948 /* get player id, names... from the User table */
950 $result = DB_query("SELECT User.id, User.fullname FROM User");
952 while( $r = DB_fetch_array($result))
953 $player[] = array( 'id' => $r[0], 'name'=> $r[1], 'points' => 0 ,'nr' => 0);
955 /* get points and generate table */
956 foreach($gameids as $gameid)
958 $re_score = DB_get_score_by_gameid($gameid);
959 /* TODO: this shouldn't loop over all players, just the 4 players that are in the game */
960 foreach($player as $key=>$pl)
962 $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
964 if(DB_get_gametype_by_gameid($gameid)=="solo")
965 $player[$key]['points'] += 3*$re_score;
967 $player[$key]['points'] += $re_score;
968 else if ($party == "contra")
969 $player[$key]['points'] -= $re_score;
971 $player[$key]['nr']+=1;
977 if($a['nr']==0 ) return 1;
978 if($b['nr']==0) return 1;
980 $a=$a['points']/$a['nr'];
981 $b=$b['points']/$b['nr'];
985 return ($a > $b) ? -1 : 1;
987 usort($player,"cmp");
989 foreach($player as $pl)
991 /* limit to players with at least 10 games */
993 $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3) );
999 function format_score_table_ascii($score)
1002 if(sizeof($score)==0)
1005 // if(sizeof($score)>5) $header.= " ... \n";
1008 foreach($score[0]['players'] as $id=>$points)
1010 $name = DB_get_name('userid',$id); /*TODO*/
1011 $output.= " ".substr($name,0,2)." |";
1014 $output.= "------+------+------+------+------+\n";
1016 $max = sizeof($score);
1019 if($i<$max-6) $output.=" ...\n";
1021 foreach($score as $game)
1024 if($i-1<$max-6) continue;
1025 foreach($game['players'] as $id=>$points)
1026 $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
1027 $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
1029 /* check for solo */
1040 function format_score_table_html($score,$userid)
1044 if(sizeof($score)==0)
1047 $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <thead><tr>\n";
1050 $output.= " <th> No </th>";
1051 foreach($score[0]['players'] as $id=>$points)
1053 $name = DB_get_name('userid',$id); /*TODO*/
1054 $output.= "<th> ".substr($name,0,2)." </th>";
1056 $output.="<th>P</th>\n </tr>\n</thead>\n<tbody>\n";
1059 foreach($score as $game)
1063 $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
1064 /* create link to old games only if you are logged in and its your game*/
1065 if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
1066 $output.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
1068 $output.=" <td>$i</td>";
1070 foreach($game['players'] as $id=>$points)
1071 $output.="<td>".$points."</td>";
1072 $output.="<td>".$game['points'];
1074 /* check for solo */
1077 $output.="</td></tr>\n";
1080 $output.="</tbody>\n</table></div>\n";
1085 function createCache($content, $cacheFile)
1087 $fp = fopen($cacheFile,"w");
1090 fwrite($fp,$content);
1094 echo "WARNING: couldn't create cache file";
1099 function getCache($cacheFile, $expireTime)
1101 if( file_exists($cacheFile) &&
1102 filemtime($cacheFile )>( time() - $expireTime ) )
1104 return file_get_contents($cacheFile);