NEW FEATURE: new settings page, user can now change timezone
[e-DoKo.git] / include / functions.php
index 28e5937c5a1259f5c9841fcf16f8aa1ef2967789..382b1488f20512864a68e6091c3e5f3b913e2d2d 100644 (file)
@@ -665,23 +665,23 @@ function display_table ()
 {
   global $gameid, $GT, $debug,$INDEX,$defaulttimezone;
 
-  $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");
+  $result = DB_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))
+  while($r = DB_fetch_array($result))
     {
       $name  = $r[0];
       $pos   = $r[1];
@@ -808,27 +808,29 @@ function display_table ()
 
 function display_user_menu()
 {
-  global $WIKI,$myid,$INDEX,$STATS;
+  global $WIKI,$myid,$INDEX;
   echo "<div class=\"usermenu\">\n".
     "<a href=\"".$INDEX."\"> 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))
+  $result = DB_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(DB_num_rows($result))
       echo "<hr />It's your turn in these games:<br />\n";
 
-  while( $r = mysql_fetch_array($result,MYSQL_NUM))
+  while( $r = DB_fetch_array($result))
     {
       echo "<a href=\"".$INDEX."?action=game&me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
     }
 
   echo "<hr /> <a href=\"".$INDEX."?action=new\">Start a new game</a>\n";
 
-  echo "<hr /> <a href=\"".$STATS."\">Statistics</a>\n";
+  echo "<hr /> <a href=\"".$INDEX."?action=prefs\">Change settings</a>\n";
+
+  echo "<hr /> <a href=\"".$INDEX."?action=stats\">Statistics</a>\n";
 
   echo
     "<hr />Report bugs in the <a href=\"".$WIKI."\">wiki</a>\n";
@@ -838,7 +840,6 @@ function display_user_menu()
 
 function generate_score_table($session)
 {
-
   /* get all ids */
   $gameids = DB_get_gameids_of_finished_games_by_session($session);
 
@@ -850,10 +851,10 @@ function generate_score_table($session)
 
   /* 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))
+  $result = DB_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 = DB_fetch_array($result))
     {
       $player[] = array( 'id' => $r[0], 'points' => 0 );
       $output.= "  <td> ".substr($r[1],0,2)." </td>\n";
@@ -892,4 +893,65 @@ function generate_score_table($session)
   return $output;
 }
 
+function generate_global_score_table()
+{
+  /* get all ids */
+  $gameids = DB_get_gameids_of_finished_games_by_session(0);
+
+  if($gameids == NULL)
+    return "";
+
+  /* get player id, names... from the User table */
+  $player = array();
+  $result = DB_query("SELECT User.id, User.fullname FROM User");
+
+  while( $r = DB_fetch_array($result))
+    $player[] = array( 'id' => $r[0], 'name'=> $r[1], 'points' => 0 ,'nr' => 0);
+
+  /* get points and generate table */
+  foreach($gameids as $gameid)
+    {
+      $re_score = DB_get_score_by_gameid($gameid);
+      /* TODO: this shouldn't loop over all players, just the 4 players that are in the game */
+      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;
+         if($party)
+           $player[$key]['nr']+=1;
+       }
+    }
+  
+  echo "<table>\n <tr>\n";
+  function cmp($a,$b)
+  {
+    if($a['nr']==0 ) return 1;
+    if($b['nr']==0) return 1;
+
+    $a=$a['points']/$a['nr'];
+    $b=$b['points']/$b['nr'];
+
+    if ($a == $b) 
+      return 0;
+    return ($a > $b) ? -1 : 1;
+  }
+  usort($player,"cmp");
+  foreach($player as $pl)
+    {
+      if($pl['nr']>10)
+       echo "  <tr><td>",$pl['name'],"</td><td>",round($pl['points']/$pl['nr'],3),"</td></tr>\n";
+    }
+  echo "</table>\n";
+    
+  return;
+}
+
+
+
 ?>