speed up get score table
authorArun Persaud <arun@nubati.net>
Sun, 19 Feb 2012 07:13:23 +0000 (23:13 -0800)
committerArun Persaud <apersaud@lbl.gov>
Sun, 19 Feb 2012 07:18:05 +0000 (23:18 -0800)
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

include/db.php
include/functions.php

index 2faac78493df339e11e68d0616e86924576bc6cb..cf39d85a72e4b7fdc670158a5a2d3221cd43f014 100644 (file)
@@ -772,6 +772,18 @@ function DB_get_party_by_gameid_and_userid($gameid,$userid)
     return NULL;
 }
 
     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));
 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;
 }
 
     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 */
 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 */
   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)
 }
 
 function DB_get_card_value_by_cardid($id)
index c8def33d50baa71979fbe070cc4ab6370b2b3b6c..a17ce0811d28c018bac0d4b578f6d1cae297fcaf 100644 (file)
@@ -1265,28 +1265,33 @@ function generate_score_table($session)
   $score = array();
   $i=0;
 
   $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;
 
   $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();
   $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;
 
   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)
     {
   /* 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)
        {
       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;
          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;
        }
          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");
       $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();
 
 {
   $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 '';
 
   $gameids = DB_get_gameids_of_finished_games_by_session(0);
 
   if($gameids == NULL)
     return '';
 
-  /* get player id, names... from the User table */
   $player = array();
   $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 */
   $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)
     {
   /* 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 */
 
       /* 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];
       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;
          if($party == 're')
            if($gametype=='solo')
              $player[$id]['points'] += 3*$re_score;