NEW FEATURE: better debugging and optimization code for db-queries.
[e-DoKo.git] / include / db.php
index 1e18f95f007a538d4136b6b9055e0ac3f99994c3..b84a56670c329c3dfb6ef1c16bcaab19f02ada7c 100644 (file)
@@ -58,6 +58,21 @@ function DB_test()
 /* use Mysql in the background */
 function DB_query($query)
 {
+  /* debug/optimize the database
+  $logfile=fopen('/tmp/DBlog.log','a+');
+  fwrite($logfile,"EXPLAIN $query ;\n");
+
+  $time = microtime();
+  $return = mysql_query($query);
+  $time = $time - microtime();
+
+  fwrite($logfile,"time of above query: $time\n");
+
+  fclose($logfile);
+
+  return $return;
+  */
+
   return mysql_query($query);
 }
 
@@ -439,6 +454,24 @@ function DB_get_all_names()
   $names  = array();
 
   $result = DB_query("SELECT fullname FROM User");
+
+  while($r = DB_fetch_array($result))
+    $names[] = $r[0];
+
+  return $names;
+}
+
+function DB_get_all_user_names_open_for_games()
+{
+  $names  = array();
+
+  DB_query("DROP   TEMPORARY TABLE IF EXISTS Usertmp;");
+  DB_query("CREATE TEMPORARY TABLE Usertmp SELECT id,fullname FROM User;");
+  DB_query("DELETE FROM Usertmp WHERE id IN (SELECT user_id FROM User_Prefs WHERE pref_key='open for games' and value='no')");
+
+  $result = DB_query("SELECT fullname FROM Usertmp");
+  DB_query("DROP   TEMPORARY TABLE IF EXISTS Usertmp;");
+
   while($r = DB_fetch_array($result))
     $names[] = $r[0];
 
@@ -689,46 +722,65 @@ function DB_set_party_by_hash($hash,$party)
 
 function DB_get_PREF($myid)
 {
-  /* Cardset */
-  $r = DB_query_array("SELECT value from User_Prefs".
-                     " WHERE user_id='$myid' AND pref_key='cardset'" );
-  if($r)
-    {
-      /* licence only valid until then */
-      if($r[0]=="altenburg" && (time()-strtotime( "2009-12-31 23:59:59")<0) )
-       $PREF["cardset"]="altenburg";
-      else
-       $PREF["cardset"]="english";
-    }
-  else
-    $PREF["cardset"]="english";
-
-  /* Email */
-  $r = DB_query_array("SELECT value FROM User_Prefs".
-                     " WHERE user_id='$myid' AND pref_key='email'" );
-  if($r)
+  /* set defaults */
+  $PREF['cardset']             = 'english';
+  $PREF['email']               = 'emailnonaddict';
+  $PREF['autosetup']           = 'no';
+  $PREF['sorting']             = 'high-low';
+  $PREF['open_for_games']      = 'yes';
+  $PREF['vacation_start']      =  NULL;
+  $PREF['vacation_stop']       =  NULL;
+  $PREF['vacation_comment']    =  '';
+
+  /* get all preferences */
+  $r = DB_query('SELECT pref_key, value FROM User_Prefs'.
+                     " WHERE user_id='$myid' " );
+  while($pref = DB_fetch_array($r) )
     {
-      if($r[0]=="emailaddict")
-       $PREF["email"]="emailaddict";
-      else
-       $PREF["email"]="emailnonaddict";
-    }
-  else
-    $PREF["email"]="emailnonaddict";
-
-  /* Autosetup */
-  $r = DB_query_array("SELECT value FROM User_Prefs".
-                     " WHERE user_id='$myid' AND pref_key='autosetup'" );
-  if($r)
-    {
-      if($r[0]=='yes')
-       $PREF['autosetup']='yes';
-      else
-       $PREF['autosetup']='no';
+      switch($pref[0])
+       {
+       case 'cardset':
+         /* licence only valid until then */
+         if($pref[1]=="altenburg" && (time()-strtotime( "2009-12-31 23:59:59")<0) )
+           $PREF["cardset"]="altenburg";
+         break;
+
+       case 'email':
+         if($pref[1]=="emailaddict")
+           $PREF["email"]="emailaddict";
+         break;
+
+       case 'autosetup':
+         if($pref[1]=='yes')
+           $PREF['autosetup']='yes';
+         break;
+
+       case 'sorting':
+         if($pref[1])
+           $PREF['sorting'] = $pref[1];
+         break;
+
+       case 'open for games':
+         if($pref[1])
+           $PREF['open_for_games'] = $pref[1];
+         break;
+
+       case 'vacation start':
+         if($pref[1])
+           $PREF['vacation_start'] = $pref[1];
+         break;
+
+       case 'vacation stop':
+         if($pref[1])
+           $PREF['vacation_stop'] = $pref[1];
+         break;
+
+       case 'vacation comment':
+         if($pref[1])
+           $PREF['vacation_comment'] = $pref[1];
+         break;
+       }
     }
-  else
-    $PREF['autosetup']='no';
-
   return $PREF;
 }
 
@@ -778,13 +830,18 @@ function DB_get_email_pref_by_uid($uid)
 
 function DB_get_unused_randomnumbers($userstr)
 {
-  $r = DB_query_array(" SELECT randomnumbers FROM Game".
-                     "   WHERE randomnumbers NOT IN".
-                     "           (SELECT randomnumbers FROM Game".
-                     "                LEFT JOIN Hand ON Game.id=Hand.game_id".
-                     "                WHERE user_id IN  (". $userstr .")".
-                     "                GROUP BY randomnumbers".
-                     "           )");
+  /* optimized version of this query using temporary tables (perhaps we should use a procedure here?).
+     First we create a copy of the Game table using just the gameid and the cards.
+     Then in a second round we delete all the gameids of games where our players are in.
+     At the end we return only the first entry in the temporary table.
+  */
+  DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
+  DB_query("CREATE TEMPORARY TABLE gametmp SELECT id,randomnumbers FROM Game;");
+  DB_query("DELETE FROM gametmp WHERE randomnumbers IN (SELECT randomnumbers FROM Hand LEFT JOIN Game ON Game.id=game_id WHERE user_id IN (".$userstr."));");
+
+  $r = DB_query_array("SELECT randomnumbers FROM gametmp LIMIT 1;");
+  DB_query("DROP   TEMPORARY TABLE IF EXISTS gametmp;");
+
   if($r)
     return $r[0];
   else
@@ -812,6 +869,9 @@ function DB_set_recovery_password($user,$newpw)
 
 function DB_get_card_name($card)
 {
+  if($card==0)
+    return 'backside';
+
   $r = DB_query_array("SELECT strength,suite FROM Card WHERE id='$card'");
 
   if($r)
@@ -874,13 +934,16 @@ function DB_get_partner_hash_by_hash($hash)
 
 function DB_format_gameid($gameid)
 {
-  $session = DB_get_session_by_gameid($gameid);
+  /* get session and create date */
+  $r = DB_query_array("SELECT session, create_date FROM Game WHERE id='$gameid' ");
+  $session = $r[0];
+  $date    = $r[1];
 
   /* get number of game */
-  $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid')))<=0) ".
+  $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, '$date'))<=0) ".
                      " FROM Game".
                      " WHERE session='$session' ");
-  return $session.".".$r[0];
+  return $session.'.'.$r[0];
 }
 
 function DB_get_reminder($user,$gameid)
@@ -1102,5 +1165,13 @@ function DB_get_exchanged_cards($hash)
   return $cards;
 }
 
-
+function DB_played_by_others($gameid)
+{
+  $gameids = array();
+  $result = DB_query("SELECT id FROM Game WHERE randomnumbers=(SELECT randomnumbers FROM Game WHERE id=$gameid) AND status='gameover'");
+  while($r = DB_fetch_array($result))
+    if($r[0]!=$gameid)
+      $gameids[]=$r[0];
+  return $gameids;
+}
 ?>
\ No newline at end of file