summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--include/db.php60
-rw-r--r--include/functions.php35
2 files changed, 44 insertions, 51 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)
diff --git a/include/functions.php b/include/functions.php
index c8def33..a17ce08 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -1265,28 +1265,33 @@ function generate_score_table($session)
$score = array();
$i=0;
- /* get all ids */
+ /* get all ids, scores and gametypes */
$gameids = DB_get_gameids_of_finished_games_by_session($session);
if($gameids == NULL)
return $score;
- /* get player id, names... from the first game */
$player = array();
- $result = DB_query("SELECT User.id, User.fullname from Hand".
- " LEFT JOIN User On Hand.user_id=User.id".
- " WHERE Hand.game_id=".$gameids[0]);
+ $player_party = array();
+
+ /* get player id from the first game */
+ $result = DB_query("SELECT user_id from Hand".
+ " WHERE Hand.game_id=".$gameids[0][0]);
while( $r = DB_fetch_array($result))
$player[$r[0]] = 0;
+ /* get party of players for each game in the session */
+ foreach($player as $id=>$points)
+ $player_party[$id]=DB_get_party_by_session_and_userid($session,$id);
+
/* get points and generate table */
foreach($gameids as $gameid)
{
- $re_score = DB_get_score_by_gameid($gameid);
- $gametype = DB_get_gametype_by_gameid($gameid);
+ $re_score = $gameid[1];
+ $gametype = $gameid[2];
foreach($player as $id=>$points)
{
- $party = DB_get_party_by_gameid_and_userid($gameid,$id);
+ $party = $player_party[$id][$i][0];
if($party == "re")
if($gametype=="solo")
$player[$id] += 3*$re_score;
@@ -1295,7 +1300,7 @@ function generate_score_table($session)
else if ($party == "contra")
$player[$id] -= $re_score;
}
- $score[$i]['gameid'] = $gameid ;
+ $score[$i]['gameid'] = $gameid[0] ;
$score[$i]['players'] = $player;
$score[$i]['points'] = abs($re_score);
$score[$i]['solo'] = ($gametype=="solo");
@@ -1310,14 +1315,14 @@ function generate_global_score_table()
{
$return = array();
- /* get all ids */
+ /* get all ids, scores and gametypes */
$gameids = DB_get_gameids_of_finished_games_by_session(0);
if($gameids == NULL)
return '';
- /* get player id, names... from the User table */
$player = array();
+ /* get player id, names... from the User table */
$result = DB_query('SELECT User.id, User.fullname FROM User');
/* save information in an array */
@@ -1328,15 +1333,15 @@ function generate_global_score_table()
/* get points and generate table */
foreach($gameids as $gameid)
{
- $re_score = DB_get_score_by_gameid($gameid);
- $gametype = DB_get_gametype_by_gameid($gameid);
+ $re_score = $gameid[1];
+ $gametype = $gameid[2];
/* get players involved in this game */
- $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid));
+ $result = DB_query('SELECT user_id FROM Hand WHERE game_id='.DB_quote_smart($gameid[0]));
while($r = DB_fetch_array($result))
{
$id = $r[0];
- $party = DB_get_party_by_gameid_and_userid($gameid,$id);
+ $party = DB_get_party_by_gameid_and_userid($gameid[0],$id);
if($party == 're')
if($gametype=='solo')
$player[$id]['points'] += 3*$re_score;