+}
+
+function main_pgsql($curtime)
+{
+ GLOBAL $G_dbpfx;
+
+ $bdb = new BriskDB();
+
+ $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
+ $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
+ $maxg = array( TRI_MAX_GAMES, MON_MAX_GAMES, WEE_MAX_GAMES );
+
+ do {
+ if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
+ log_crit("statadm: begin failed");
+ break;
+ }
+
+ // Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
+ $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
+ if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
+ log_crit("statadm: truncate failed");
+ break;
+ }
+
+ for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
+ $old_score = array( 1000000000, 1000000000);
+ $old_gam = array( -1, -1);
+ $rank = array( 0, 0);
+
+ $pla_sql = sprintf("SELECT (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) as score, sum(p.pts) as points, count(p.pts) as games, u.code as ucode, u.login as login
+ FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u
+ WHERE p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND
+ g.tstamp > to_timestamp(%d)
+ GROUP BY u.code, u.login
+ ORDER BY (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) DESC,
+ count(p.pts) DESC",
+ $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $ming[$dtime]);
+
+ // log_crit("statadm: INFO: [$pla_sql]");
+
+ if (($pla_pg = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) {
+ // no point found, abort
+ log_crit("statadm: main placement select failed [$pla_sql]");
+ break;
+ }
+
+ for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
+ $pla_obj = pg_fetch_object($pla_pg,$i);
+ if ($pla_obj->games < $ming[$dtime])
+ continue;