<?php
-/* make sure that we are not called from outside the scripts,
+/* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
+ *
+ * This file is part of e-DoKo.
+ *
+ * e-DoKo is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * e-DoKo is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with e-DoKo. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/* make sure that we are not called from outside the scripts,
* use a variable defined in config.php to check this
*/
if(!isset($HOST))
function DB_open()
{
+ $version_needed = 2;
+
global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
$DB = @mysql_connect($DB_host,$DB_user, $DB_password);
if ( $DB )
{
- mysql_select_db($DB_database) or die('Could not select database');
+ mysql_select_db($DB_database) or die('Error: Could not select database');
}
else
- return -1;
+ {
+ echo mysql_errno() . ": " . mysql_error(). "\n";
+ return -1;
+ };
+
+ $version = DB_get_version();
+ if ($version != $version_needed)
+ return -2;
return 0;
}
/* 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);
}
{
$result = DB_query($query);
$return = DB_fetch_array($result);
-
+
return $return;
}
function DB_query_array_all($query)
{
$result = array();
-
+
$queryresult = DB_query($query);
while($row = DB_fetch_array($queryresult))
$result[] = $row;
return $result;
}
+function DB_get_version()
+{
+ $version = DB_query_array('SELECT version FROM Version');
+ return $version[0];
+}
+
function DB_get_passwd_by_name($name)
{
$r = DB_query_array("SELECT password FROM User WHERE fullname=".DB_quote_smart($name)."");
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();
$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];
return $names;
}
+function DB_get_emails_of_last_logins($N)
+{
+ $emails = array();
+
+ $result = DB_query("SELECT email FROM User ORDER BY last_login DESC LIMIT $N");
+ while($r = DB_fetch_array($result))
+ $emails[] = $r[0];
+
+ return $emails;
+}
+
function DB_get_names_of_new_logins($N)
{
$names = array();
return;
}
+function DB_get_game_timestamp($gameid)
+{
+ $r = DB_query_array("SELECT mod_date FROM Game WHERE id=".DB_quote_smart($gameid));
+
+ if($r)
+ return $r[0];
+ else
+ return NULL;
+}
function DB_update_user_timestamp($userid)
{
else
return NULL;
}
+
+function DB_get_user_creation_date($userid)
+{
+ $r = DB_query_array("SELECT create_date FROM User WHERE id=".DB_quote_smart($userid));
+
+ if($r)
+ return $r[0];
+ else
+ return NULL;
+}
+
function DB_get_user_timezone($userid)
{
$r = DB_query_array("SELECT timezone FROM User WHERE id=".DB_quote_smart($userid));
return $r;
}
-function DB_get_ruleset($dullen,$schweinchen,$call)
+function DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump)
{
$r = array();
$result = DB_query("SELECT id FROM Rulesets WHERE".
" dullen=".DB_quote_smart($dullen)." AND ".
- " call=".DB_quote_smart($call)." AND ".
- " schweinchen=".DB_quote_smart($schweinchen));
+ " Rulesets.call=".DB_quote_smart($call)." AND ".
+ " schweinchen=".DB_quote_smart($schweinchen)." AND ".
+ " lowtrump=".DB_quote_smart($lowtrump));
if($result)
$r = DB_fetch_array($result);
$result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
DB_quote_smart($dullen).",".
DB_quote_smart($schweinchen).",".
+ DB_quote_smart($lowtrump).",".
DB_quote_smart($call).
", NULL)");
if($result)
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['digest'] = 'digest-off';
+ $PREF['autosetup'] = 'no';
+ $PREF['sorting'] = 'high-low';
+ $PREF['open_for_games'] = 'yes';
+ $PREF['vacation_start'] = NULL;
+ $PREF['vacation_stop'] = NULL;
+ $PREF['vacation_comment'] = '';
+ $PREF['language'] = 'en';
+
+ /* 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( '2012-12-31 23:59:59')<0) )
+ $PREF['cardset']='altenburg';
+ break;
+
+ case 'email':
+ if($pref[1]=='emailaddict')
+ $PREF['email']='emailaddict';
+ break;
+
+ case 'digest':
+ if($pref[1])
+ $PREF['digest'] = $pref[1];
+ 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;
+
+ case 'language':
+ if($pref[1])
+ $PREF['language'] = $pref[1];
+ break;
+ }
}
- else
- $PREF['autosetup']='no';
-
+ $_SESSION['language'] = $PREF['language'];
return $PREF;
}
$r = DB_query_array("SELECT * FROM Rulesets".
" LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
" WHERE Game.id='$gameid'" );
-
- $RULES["dullen"] = $r[2];
- $RULES["schweinchen"] = $r[3];
- $RULES["call"] = $r[4];
-
+
+ $RULES['dullen'] = $r[2];
+ $RULES['schweinchen'] = $r[3];
+ $RULES['lowtrump'] = $r[4];
+ $RULES['call'] = $r[5];
+
return $RULES;
}
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
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)
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 COUNT(*),create_date FROM Game".
- " WHERE session='$session' ".
- " AND TIMEDIFF(create_date, (SELECT create_date FROM Game WHERE id='$gameid'))<=0 ".
- " GROUP by session");
- return $session.".".$r[0];
+ $r = DB_query_array("SELECT SUM(TIME_TO_SEC(TIMEDIFF(create_date, '$date'))<=0) ".
+ " FROM Game".
+ " WHERE session='$session' ");
+ return $session.'.'.$r[0];
}
function DB_get_reminder($user,$gameid)
{
$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
*/
$r = NULL;
-
+
switch($type)
{
case 'name':
/* 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).
"AND Hand.position=".DB_quote_smart($var1)."");
break;
}
-
+
$r = DB_fetch_array($result);
if($r)
return "";
}
+function DB_add_exchanged_card($card,$old_hand_id,$new_hand_id)
+{
+ DB_query("INSERT INTO Card_Exchange VALUES (NULL,$new_hand_id,$old_hand_id,$card)");
+ return;
+}
+
+function DB_get_exchanged_cards($hash)
+{
+ $cards = array();
+
+ $handid = DB_get_handid('hash',$hash);
+
+ $result = DB_query("SELECT card_id FROM Card_Exchange WHERE orig_hand_id=".DB_quote_smart($handid));
+ while($r = DB_fetch_array($result))
+ $cards[]=$r[0];
+
+ 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;
+}
+
+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];
+}
+
+function DB_digest_insert_email($To,$message)
+{
+ DB_query("INSERT INTO digest_email VALUES (NULL,".DB_quote_smart($To).",NULL,".DB_quote_smart($message).")");
+ return;
+}
+
+function DB_get_digest_users()
+{
+ $users = array();
+
+ $result = DB_query("SELECT user_id FROM User_Prefs WHERE pref_key='digest' and value <> 'digest-off'");
+ while($r = DB_fetch_array($result))
+ $users[]=$r[0];
+
+ return $users;
+}
+
+function DB_get_digest_message_by_email($email)
+{
+ $messages = array();
+
+ $result = DB_query("SELECT id,content FROM digest_email Where email='$email'");
+ while($r = DB_fetch_array($result))
+ $messages[]=$r;
+
+ return $messages;
+}
+
+function DB_digest_delete_message($id)
+{
+ DB_query("Delete from digest_email where id='$id'");
+}
+
?>
\ No newline at end of file