some more comments
[e-DoKo.git] / include / db.php
index 6e1af93c31faa2c64fd5a000467eade4bc5d8143..b8b184dd7673d73e8aaca176248b16fbfe89c347 100644 (file)
@@ -18,8 +18,10 @@ function DB_open()
       mysql_select_db($DB_database) or die('Could not select database');
     }
   else
-    return -1;
-
+    {
+      echo mysql_errno() . ": " . mysql_error(). "\n";
+      return -1;
+    }
   return 0;
 }
 
@@ -58,6 +60,22 @@ function DB_test()
 /* use Mysql in the background */
 function DB_query($query)
 {
+  /* debug/optimize the database
+  $time = microtime();
+  $return = mysql_query($query);
+  $time = $time - microtime();
+
+  if($time > 0.05) // this way we can find only the long ones
+  {
+    $logfile=fopen('/tmp/DBlog.log','a+');
+    fwrite($logfile,"EXPLAIN $query ;\n");
+    fwrite($logfile,"time of above query: $time\n");
+    fclose($logfile);
+  };
+
+  return $return;
+  */
+
   return mysql_query($query);
 }
 
@@ -251,34 +269,6 @@ function DB_get_gameid_by_hash($hash)
     return 0;
 }
 
-function DB_cancel_game($hash)
-{
-  $gameid = DB_get_gameid_by_hash($hash);
-
-  if(!$gameid)
-    return;
-
-  /* get the IDs of all players */
-  $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
-  while($r = DB_fetch_array($result))
-    {
-      $id = $r[0];
-
-      $tmp = DB_query_array("SELECT id  FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
-      DB_query("DELETE FROM Play WHERE hand_card_id=".DB_quote_smart($tmp[0]));
-
-      DB_query("DELETE FROM Hand_Card WHERE hand_id=".DB_quote_smart($id));
-      DB_query("DELETE FROM Hand WHERE id=".DB_quote_smart($id));
-    }
-
-  /* delete game */
-  DB_query("DELETE FROM User_Game_Prefs WHERE game_id=".DB_quote_smart($gameid));
-  DB_query("DELETE FROM Trick WHERE game_id=".DB_quote_smart($gameid));
-  DB_query("DELETE FROM Game WHERE id=".DB_quote_smart($gameid));
-
-  return;
-}
-
 function DB_get_hand($me)
 {
   $cards = array();
@@ -707,63 +697,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)
+  /* 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) )
     {
-      /* 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)
-    {
-      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';
-
-  /* Sorting */
-  $r = DB_query_array("SELECT value FROM User_Prefs".
-                     " WHERE user_id='$myid' AND pref_key='sorting'" );
-  if($r)
-    $PREF['sorting'] = $r[0];
-  else
-    $PREF['sorting']='high-low';
-
-  /* Open for new games */
-  $r = DB_query_array("SELECT value FROM User_Prefs".
-                     " WHERE user_id='$myid' AND pref_key='open for games'" );
-  if($r)
-    $PREF['open_for_games'] = $r[0];
-  else
-    $PREF['open_for_games']='yes';
-
-
   return $PREF;
 }
 
@@ -852,6 +844,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)
@@ -914,13 +909,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)
@@ -948,7 +946,7 @@ function DB_is_session_active($session)
 {
   $r = DB_query_array("SELECT COUNT(*) FROM Game ".
                      "  WHERE session=$session ".
-                     "  AND status<>'gameover' ");
+                     "  AND status IN ('pre','play') ");
   if($r)
     return $r[0];
   else
@@ -1040,7 +1038,7 @@ function DB_get_userid($type,$var1="",$var2="")
       /* test if a recovery password has been set */
       if(!$r)
        {
-         echo "testing alternative password";
+         /* testing alternative password */
          $result = DB_query("SELECT User.id FROM User".
                             " LEFT JOIN Recovery ON User.id=Recovery.user_id".
                             " WHERE email=".DB_quote_smart($var1).
@@ -1151,4 +1149,10 @@ function DB_played_by_others($gameid)
       $gameids[]=$r[0];
   return $gameids;
 }
+
+function DB_get_number_of_tricks($gameid,$position)
+{
+  $r = DB_query_array("SELECT COUNT(winner) FROM Trick Where game_id='$gameid' and winner='$position'");
+  return $r[0];
+}
 ?>
\ No newline at end of file