From a6dc7c74c4b119cdeb8f76e3901d1a70f9d4fb1a Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Thu, 31 Oct 2013 18:00:11 +0100 Subject: [PATCH] users_get, match_continue, match_order_get functions to retrieve info about users matches and users order to manage continue command in dbase_psql.php file --- web/Obj/dbase_pgsql.phh | 170 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/web/Obj/dbase_pgsql.phh b/web/Obj/dbase_pgsql.phh index abe9e73..6c127b5 100644 --- a/web/Obj/dbase_pgsql.phh +++ b/web/Obj/dbase_pgsql.phh @@ -289,6 +289,176 @@ class BriskDB return ($this->dbconn); } + // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position + function users_get($match_code, $with_minmaxtidx, $is_newmatch) + { + GLOBAL $G_dbpfx; + + if ($is_newmatch) { // is new + $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s + FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, + %susers AS u, %sbin5_table_orders AS o + WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode + AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d + GROUP BY u.code, u.login%s, o.pos + ORDER BY o.pos;", + ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""), + $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code, + ($with_minmaxtidx ? ", m.tidx" : "")); + } + else { // is old + $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s + FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u + WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d + GROUP BY u.code, u.login%s;", + ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""), + $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code, + ($with_minmaxtidx ? ", m.tidx" : "")); + } + + if (($usr_pg = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) { + log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql)); + return (FALSE); + } + $usr_n = pg_numrows($usr_pg); + if ($usr_n != BIN5_PLAYERS_N) { + log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n)); + return (FALSE); + } + $users = array(); + + if ($with_minmaxtidx) + $fields = array('code', 'login', 'first', 'last', 'tidx'); + else + $fields = array('code', 'login'); + + for ($u = 0 ; $u < $usr_n ; $u++) { + $usr_obj = pg_fetch_object($usr_pg, $u); + fprintf(STDERR, "GOGORO [%s]\n", print_r($usr_obj, TRUE)); + $users[$u] = array(); + foreach($fields as $field) { + $users[$u][$field] = $usr_obj->$field; + } + } + return ($users); + } + + // out: tab->{points,points_n,old_reason}, in: tab->ttok + function match_continue($match_code, $table, $tidx) + { + GLOBAL $G_dbpfx; + + if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) { + log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__)); + return (FALSE); + } + + $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code); + if (($num_pg = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) { + log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__)); + return (FALSE); + } + $num_obj = pg_fetch_object($num_pg, 0); + $table->points_n = $num_obj->points_n; + + $tot_sql = sprintf("SELECT sum(p.pts) AS pts + FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u, + %sbin5_table_orders AS o + WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code + AND p.ucode = o.ucode AND g.mcode = o.mcode + GROUP BY p.ucode, o.pos + ORDER BY o.pos;", + $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code); + if (($tot_pg = pg_query($this->dbconn->db(), $tot_sql)) == FALSE + || pg_numrows($tot_pg) != BIN5_PLAYERS_N) { + log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__)); + return(FALSE); + } + + $u = 0; + foreach ($users as $user) { + $pts_sql = sprintf("SELECT p.pts AS pts + FROM %sbin5_points as p, %sbin5_games as g + WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d + ORDER BY g.code ASC + LIMIT %d OFFSET %d;", + $G_dbpfx, $G_dbpfx, $match_code, $user['code'], + MAX_POINTS, + ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS)); + + // points of the match for each user + if (($pts_pg = $this->query($pts_sql)) == FALSE) { + log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__)); + return (FALSE); + } + $pts_n = pg_numrows($pts_pg); + if ($pts_n > $table->points_n) { + // inconsistent scenario number of points great than number of games + log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__)); + return (FALSE); + } + for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) { + $pts_obj = pg_fetch_object($pts_pg, $i); + $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts; + } + $tot_obj = pg_fetch_object($tot_pg, $u); + $table->total[$u] = $tot_obj->pts; + + $u++; + } + + $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY code DESC LIMIT 1;", $G_dbpfx, $match_code); + if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) { + log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__)); + return (FALSE); + } + $gam_obj = pg_fetch_object($gam_pg, 0); + + $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult, + $gam_obj->asta_win, $users[$gam_obj->asta_win]['login'], + $gam_obj->friend, $users[$gam_obj->friend]['login'], + $gam_obj->pnt, $gam_obj->asta_pnt); + + return (TRUE); + } + + function match_order_get(&$match_data, $match_code, $exp_num) + { + GLOBAL $G_dbpfx; + + $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;", + $G_dbpfx, $match_code); + + if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) { + log_crit(sprintf("%s: fails for id or users number", __FUNCTION__)); + return (FALSE); + } + + $ucodes = array(); + for ($i = 0 ; $i < $exp_num ; $i++) { + $ord_obj = pg_fetch_object($ord_pg, $i); + $ucodes[$i] = $ord_obj->ucode; + } + + if ($match_data !== NULL) { + $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;", + $G_dbpfx, $match_code); + + if (($mtdt_pg = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) { + log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code)); + return (FALSE); + } + + $mtdt_obj = pg_fetch_object($mtdt_pg, 0); + + foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) { + $match_data[$match_name] = $mtdt_obj->$match_name; + } + } + + return ($ucodes); + } + // ttok text UNIQUE, // tidx function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts) -- 2.17.1