summaryrefslogtreecommitdiffstats
path: root/include/db.php
diff options
context:
space:
mode:
authorArun Persaud <arun@nubati.net>2012-02-18 23:13:23 -0800
committerArun Persaud <apersaud@lbl.gov>2012-02-18 23:18:05 -0800
commit72842383a6162e43f8b9630bfcca13d10eca7bff (patch)
tree89aa18a5bf6f36109d8244ca926484ebdbb04c03 /include/db.php
parentcc143e38299d3dd3981efd81972c0e0df4df1462 (diff)
downloade-DoKo-72842383a6162e43f8b9630bfcca13d10eca7bff.tar.gz
e-DoKo-72842383a6162e43f8b9630bfcca13d10eca7bff.tar.bz2
e-DoKo-72842383a6162e43f8b9630bfcca13d10eca7bff.zip
speed up get score table
instead of looping over all games and have 3 database querries for each gameid, we now only do 5 querries: one for the gameids, scores and gametype and then one for each player to get his party
Diffstat (limited to 'include/db.php')
-rw-r--r--include/db.php60
1 files changed, 24 insertions, 36 deletions
diff --git a/include/db.php b/include/db.php
index 2faac78..cf39d85 100644
--- a/include/db.php
+++ b/include/db.php
@@ -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));
@@ -1051,48 +1063,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");
-
- $i=0;
- while($r = DB_fetch_array($queryresult) )
- {
- $ids[$i] = $r[0];
- $i++;
- }
-
- return $ids;
+ $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");
+
+ return $queryresult;
}
function DB_get_card_value_by_cardid($id)