+ global $RULES;
+
+ $gameid = DB_get_gameid_by_hash($hash);
+ $gametype = DB_get_gametype_by_gameid($gameid);
+ $oldcall = DB_get_call_by_hash($hash);
+ $pcall = DB_get_partner_call_by_hash($hash);
+
+ if( ($pcall!=NULL && $what >= $pcall) ||
+ ($oldcall!=NULL && $what >=$oldcall) )
+ {
+ return 0;
+ }
+
+ $NRcards = count(DB_get_hand($hash));
+
+ $NRallcards = 0;
+ for ($i=1;$i<5;$i++)
+ {
+ $user = DB_get_hash_from_game_and_pos($gameid,$i);
+ $NRallcards += count(DB_get_hand($user));
+ };
+
+ /* in case of a wedding, everything will be delayed by an offset */
+ $offset = 0;
+ if($gametype=="wedding")
+ {
+ $offset = DB_get_sickness_by_gameid($gameid);
+ if ($offset <0) /* not resolved */
+ return 0;
+ };
+
+ switch ($RULES["call"])
+ {
+ case "1st-own-card":
+ if( 4-($what/30) >= 12 - ($NRcards + $offset))
+ return 1;
+ break;
+ case "5th-card":
+ if( 27+4*($what/30) <= $NRallcards + $offset*4)
+ return 1;
+ break;
+ case "9-cards":
+
+ if($oldcall!=NULL && $pcall!=NULL)
+ $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
+ else if($oldcall!=NULL)
+ $mincall = $oldcall;
+ else if ($pcall!=NULL)
+ $mincall = $pcall;
+ else
+ $mincall = -1;
+
+ if( 12 <= ($NRcards + $offset))
+ {
+ return 1;
+ }
+ else if ( 9 <= ($NRcards + $offset))
+ {
+ if( ($mincall>=0 && $mincall==120) )
+ return 1;
+ }
+ else if ( 6 <= ($NRcards + $offset))
+ {
+ if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
+ return 1;
+ }
+ else if ( 3 <= ($NRcards + $offset))
+ {
+ if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
+ return 1;
+ }
+ else if ( 0 <= ($NRcards + $offset))
+ {
+ if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
+ return 1;
+ };
+ break;
+ }
+
+ return 0;
+}
+
+function display_table ()
+{
+ global $gameid, $GT, $debug,$host;
+
+ $result = mysql_query("SELECT User.fullname as name,".
+ " Hand.position as position, ".
+ " User.id, ".
+ " Hand.party as party, ".
+ " Hand.sickness as sickness, ".
+ " Hand.point_call, ".
+ " User.last_login, ".
+ " Hand.hash, ".
+ " User.timezone ".
+ "FROM Hand ".
+ "LEFT JOIN User ON User.id=Hand.user_id ".
+ "WHERE Hand.game_id='".$gameid."' ".
+ "ORDER BY position ASC");
+
+ echo "<div class=\"table\">\n".
+ " <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
+ while($r = mysql_fetch_array($result,MYSQL_NUM))
+ {
+ $name = $r[0];
+ $pos = $r[1];
+ $user = $r[2];
+ $party = $r[3];
+ $sickness = $r[4];
+ $call = $r[5];
+ $hash = $r[7];
+ $timezone = $r[8];
+ date_default_timezone_set($timezone);
+ $timenow = strtotime(date("Y-m-d H:i:s"));
+ $lastlogin = strtotime($r[6]);
+
+
+ echo " <div class=\"table".($pos-1)."\">\n";
+ if(!$debug)
+ echo " $name \n";
+ else
+ echo " <a href=\"".$host."?me=".$hash."\">$name</a>\n";
+
+ /* add hints for poverty, wedding, solo, etc */
+ if($GT=="poverty" && $party=="re")
+ if($sickness=="poverty")
+ {
+ $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+ $cards = DB_get_all_hand($userhash);
+ $trumpNR = count_trump($cards);
+ if($trumpNR)
+ echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
+ else
+ echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
+ }
+ else
+ echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
+
+ if($GT=="dpoverty")
+ if($party=="re")
+ if($sickness=="poverty")
+ {
+ $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+ $cards = DB_get_all_hand($userhash);
+ $trumpNR = count_trump($cards);
+ if($trumpNR)
+ echo " <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
+ else
+ echo " <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
+ }
+ else
+ echo " <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
+ else
+ if($sickness=="poverty")
+ {
+ $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+ $cards = DB_get_all_hand($userhash);
+ $trumpNR = count_trump($cards);
+ if($trumpNR)
+ echo " <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
+ else
+ echo " <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
+ }
+ else
+ echo " <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
+
+ if($GT=="wedding" && $party=="re")
+ if($sickness=="wedding")
+ echo " <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
+ else
+ echo " <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
+
+ if(ereg("solo",$GT) && $party=="re")
+ {
+ if(ereg("queen",$GT))
+ echo " <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("jack",$GT))
+ echo " <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("club",$GT))
+ echo " <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("spade",$GT))
+ echo " <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("heart",$GT))
+ echo " <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("trumpless",$GT))
+ echo " <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ else if(ereg("trump",$GT))
+ echo " <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
+ }
+
+ /* add point calls */
+ if($call!=NULL)
+ {
+ if($party=="re")
+ echo " <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
+ else
+ echo " <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
+ switch($call)
+ {
+ case "0":
+ echo " <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
+ break;
+ case "30":
+ echo " <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
+ break;
+ case "60":
+ echo " <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
+ break;
+ case "90":
+ echo " <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
+ break;
+ }
+ }
+
+ echo " <br />\n";
+ echo " <span title=\"".date("Y-m-d H:i:s",$timenow). "\">local time</span>\n";
+ echo " <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
+ echo " </div>\n";
+
+ }
+ echo "</div>\n"; /* end output table */
+
+
+ return;
+}
+
+
+function display_user_menu()
+{
+ global $wiki,$myid,$host;
+ echo "<div class=\"usermenu\">\n".
+ "<a href=\"index.php\"> Go to my user page </a>";
+
+ $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
+ " LEFT JOIN Game On Hand.game_id=Game.id".
+ " WHERE Hand.user_id='$myid'".
+ " AND Game.player='$myid'".
+ " AND Game.status<>'gameover'".
+ " ORDER BY Game.session" );
+ if(mysql_num_rows($result))
+ echo "<hr />It's your turn in these games:<br />\n";
+
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ {
+ echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
+ }
+
+ echo "<hr /> <a href=\"".$host."?new\">Start a new game</a>\n";
+
+ echo "<hr /> <a href=\"".substr($host,0,-9)."stats.php\">Statistics</a>\n";
+
+ echo
+ "<hr />Report bugs in the <a href=\"". $wiki."\">wiki</a>\n";
+ echo "</div>\n";
+ return;
+}
+
+function generate_score_table($session)
+{
+
+ /* get all ids */
+ $gameids = DB_get_gameids_of_finished_games_by_session($session);
+
+ if($gameids == NULL)
+ return "";
+
+ $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n";
+
+
+ /* get player id, names... from the first game */
+ $player = array();
+ $result = mysql_query("SELECT User.id, User.fullname from Hand".
+ " LEFT JOIN User On Hand.user_id=User.id".
+ " WHERE Hand.game_id=".$gameids[0]);
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ {
+ $player[] = array( 'id' => $r[0], 'points' => 0 );
+ $output.= " <td> ".substr($r[1],0,2)." </td>\n";
+ }
+ $output.=" <td>P</td>\n </tr>\n";
+
+ /* get points and generate table */
+ foreach($gameids as $gameid)
+ {
+ $output.=" <tr>\n";
+
+ $re_score = DB_get_score_by_gameid($gameid);
+ foreach($player as $key=>$pl)
+ {
+ $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
+ if($party == "re")
+ if(DB_get_gametype_by_gameid($gameid)=="solo")
+ $player[$key]['points'] += 3*$re_score;
+ else
+ $player[$key]['points'] += $re_score;
+ else if ($party == "contra")
+ $player[$key]['points'] -= $re_score;
+
+ $output.=" <td>".$player[$key]['points']."</td>\n";
+ }
+ $output.=" <td>".abs($re_score);
+
+ /* check for solo */
+ if(DB_get_gametype_by_gameid($gameid)=="solo")
+ $output.= " S";
+ $output.="</td>\n </tr>\n";
+ }
+
+ $output.="</table></div>\n";
+
+ return $output;