remove Altenburger Spielkarten, their copyright notice, etc. since the licence expired
[e-DoKo.git] / include / db.php
index 2faac78493df339e11e68d0616e86924576bc6cb..f683beba29402686e4e93a057139cd63787b0bd3 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-/* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arun Persaud <arun@nubati.net>
+/* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Arun Persaud <arun@nubati.net>
  *
  *   This file is part of e-DoKo.
  *
@@ -30,7 +30,7 @@ if(!isset($HOST))
 
 function DB_open()
 {
-  $version_needed = 3;
+  $version_needed = 5;
 
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
@@ -772,6 +772,18 @@ function DB_get_party_by_gameid_and_userid($gameid,$userid)
     return NULL;
 }
 
+function DB_get_party_by_session_and_userid($session,$userid)
+{
+  /* used in score table by index. gameids are sorted by date, so we need to sort here too! */
+  $r = DB_query_array_all("SELECT party FROM Hand".
+                         " LEFT JOIN Game ON Game.id = Hand.game_id".
+                         " WHERE Game.session=".DB_quote_smart($session).
+                         "  AND user_id=".DB_quote_smart($userid)."".
+                         "  AND Game.status='gameover' ".
+                         " ORDER BY Game.create_date ASC");
+  return $r;
+}
+
 function DB_set_party_by_hash($hash,$party)
 {
   DB_query("UPDATE Hand SET party=".DB_quote_smart($party)." WHERE hash=".DB_quote_smart($hash));
@@ -800,9 +812,6 @@ function DB_get_PREF($myid)
       switch($pref[0])
        {
        case 'cardset':
-         /* licence only valid until then */
-         if($pref[1]=='altenburg' && (time()-strtotime( '2012-12-31 23:59:59')<0) )
-           $PREF['cardset']='altenburg';
          break;
 
        case 'email':
@@ -851,7 +860,7 @@ function DB_get_PREF($myid)
          break;
        }
     }
-  $_SESSION['language'] =  $PREF['language'];
+
   return $PREF;
 }
 
@@ -939,6 +948,12 @@ function DB_set_recovery_password($user,$newpw)
   return;
 }
 
+function DB_delete_recovery_passwords($userid)
+{
+  DB_query("DELETE FROM Recovery WHERE user_id=".DB_quote_smart($userid));
+  return;
+}
+
 function DB_get_card_name($card)
 {
   if($card==0)
@@ -1051,48 +1066,24 @@ function DB_is_session_active($session)
     return -1;
 }
 
-function DB_get_score_by_gameid($gameid)
-{
-  /* returns the points of a game from the point of the re parth (<0 if they lost) */
-  $queryresult = DB_query("SELECT COUNT(*),party FROM Score ".
-                         "  WHERE game_id=$gameid ".
-                         "  GROUP BY party ");
-  $re     = 0;
-  $contra = 0;
-
-  while($r = DB_fetch_array($queryresult) )
-    {
-      if($r[1] == "re")
-       $re += $r[0];
-      else if ($r[1] == "contra")
-       $contra += $r[0];
-    };
-
-  return ($re - $contra);
-}
-
 function DB_get_gameids_of_finished_games_by_session($session)
 {
   $ids = array ();
 
   if($session==0) /* return all games */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           " WHERE status='gameover' ".
-                           " ORDER BY create_date ASC");
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 " WHERE status='gameover' ".
+                                 " GROUP BY Game.id");
   else   /* return games in a session */
-    $queryresult = DB_query("SELECT id FROM Game ".
-                           "  WHERE session=$session ".
-                           "   AND status='gameover' ".
-                           " ORDER BY create_date ASC");
+    $queryresult = DB_query_array_all("SELECT Game.id,SUM(IF(STRCMP(Score.party,'re'),-1,1)),Game.type FROM Game ".
+                                 " LEFT JOIN Score on game_id=Game.id".
+                                 "  WHERE session=$session ".
+                                 "   AND status='gameover' ".
+                                 " GROUP BY Game.id".
+                                 " ORDER BY Game.create_date ASC");
 
-  $i=0;
-  while($r = DB_fetch_array($queryresult) )
-    {
-      $ids[$i] = $r[0];
-      $i++;
-    }
-
-  return $ids;
+  return $queryresult;
 }
 
 function DB_get_card_value_by_cardid($id)
@@ -1254,9 +1245,12 @@ function DB_get_number_of_tricks($gameid,$position)
   return $r[0];
 }
 
-function DB_digest_insert_email($To,$message)
+function DB_digest_insert_email($To,$message,$type,$gameid)
 {
-  DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,".DB_quote_smart($message).")");
+  if($type == GAME_YOUR_TURN)
+    DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,'your_turn',$gameid,".DB_quote_smart($message).")");
+  else
+    DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,'misc',NULL,".DB_quote_smart($message).")");
   return;
 }
 
@@ -1275,7 +1269,7 @@ function DB_get_digest_message_by_email($email)
 {
   $messages = array();
 
-  $result = DB_query("SELECT id,content FROM digest_email Where email='$email'");
+  $result = DB_query("SELECT id,content,type,game_id FROM digest_email Where email='$email'");
   while($r = DB_fetch_array($result))
     $messages[]=$r;