refactorization to manage old rules with a separated class
[brisk.git] / web / briskin5 / statadm.php
index 15fd27c..ee06e08 100644 (file)
@@ -2,7 +2,7 @@
 /*
  *  brisk - statadm.php
  *
- *  Copyright (C) 2009-2011 Matteo Nastasi
+ *  Copyright (C) 2009-2012 Matteo Nastasi
  *                          mailto: nastasi@alternativeoutput.it 
  *                                  matteo.nastasi@milug.org
  *                          web: http://www.alternativeoutput.it
@@ -33,6 +33,7 @@ $G_base = "../";
 ini_set("max_execution_time",  "240");
 
 require_once("../Obj/brisk.phh");
+require_once("../Obj/user.phh");
 require_once("../Obj/auth.phh");
 require_once("../Obj/dbase_${G_dbasetype}.phh");
 require_once("Obj/briskin5.phh");
@@ -58,8 +59,11 @@ function main_file($curtime)
     fclose($fp_start);
   }
 
-  // FIXME: now create can return FALSE
-  $bdb = BriskDB::create();
+  if (($bdb = BriskDB::create()) == FALSE) {
+    echo "database connection failed";
+    exit;
+  }
+      
   $bdb->users_load();
 
   for ($i = 0 ; $i < $bdb->count() ; $i++) {
@@ -97,12 +101,12 @@ function main_file($curtime)
       
       continue;
     }
-    // echo $p++." ".BRISKIN5_PLAYERS_N."<br>";
+    // echo $p++." ".BIN5_PLAYERS_N."<br>";
     
     $found = FALSE;
     $mult = 1;
-    for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
-      for ($e = $i + 1 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
+    for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
+      for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
         if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
           $mult = abs($ar[10+($i*2)]);
           $found = TRUE;
@@ -114,7 +118,7 @@ function main_file($curtime)
 
     if ($mult == 0)
        continue;
-    for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
+    for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
       // echo $p." i) ".$i."<br>";
       $username = $ar[9+($i*2)];
       if (($item = $bdb->getitem_bylogin($username, &$id)) == FALSE) {
@@ -211,8 +215,10 @@ function main_pgsql($curtime)
 {
     GLOBAL $G_dbpfx;
 
-    // FIXME: now create can return FALSE
-    $bdb = BriskDB::create();
+    if (($bdb = BriskDB::create()) == FALSE) {
+        echo "database connection failed";
+        exit;
+    }
 
     $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
     $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
@@ -226,9 +232,8 @@ function main_pgsql($curtime)
         
         $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp 
                             FROM %sbin5_matches as m, %sbin5_games as g 
-                            WHERE g.mcode = m.code GROUP BY m.code, m.ttok",
-                           $G_dbpfx, $G_dbpfx, $G_dbpfx);
-        // error_log($mtc_sql, 0);
+                            WHERE m.tcode = %d AND m.code = g.mcode GROUP BY m.code, m.ttok",
+                           $G_dbpfx, $G_dbpfx, $G_dbpfx, BIN5_TOURNAMENT_NORMAL);
         if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) {
             log_crit("statadm: temporary matches table creation [$mtc_sql] failed");
             break;
@@ -236,7 +241,6 @@ function main_pgsql($curtime)
         
         $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE  tstamp < to_timestamp(%d)",
                            $G_dbpfx, $curtime - TRI_LIMIT);
-        // error_log($tmt_sql, 0);
 
         // if deletable old matches exists then ...
         if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
@@ -257,7 +261,6 @@ function main_pgsql($curtime)
                 $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d",
                                    $G_dbpfx, $tmt_obj->code);
                 
-                error_log($mtc_sql, 0);
                 if (($mtc_pg  = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
                     log_crit("statadm: matches row select failed");
                     break;
@@ -330,15 +333,20 @@ function main_pgsql($curtime)
             $old_score = array( 1000000000, 1000000000);
             $old_gam   = array( -1, -1);
             $rank      = array(  0,  0);
-            
+
+            // TAG: POINTS_MANAGEMENT
             $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) AND g.tstamp <= to_timestamp(%d)
+                                WHERE m.tcode = %d AND m.code = g.mcode AND
+                                      ( (u.type & (CAST (X'ff0000' as integer))) <> (CAST (X'800000' as integer)) ) AND
+                                      g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d) AND
+                                      p.ucode = u.code AND p.gcode = g.code AND
+                                      p.pts != 0
                                 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], $curtime);
+                               $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, BIN5_TOURNAMENT_NORMAL,
+                               $curtime - $limi[$dtime], $curtime);
 
             // log_crit("statadm: INFO: [$pla_sql]");
 
@@ -408,10 +416,10 @@ function main()
     GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz;
     
     echo "Inizio.<br>";
-    flush();
+    mop_flush();
     if ($pazz != $G_alarm_passwd) {
         echo "Wrong password<br>";
-        flush();
+        mop_flush();
         exit;
     }
     
@@ -419,14 +427,17 @@ function main()
     
     $ctime = time();
 
-    $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
+    if (BIN5_PLAYERS_N != 5)
+        $curtime = $ctime;
+    else
+        $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
     if ($ret = $fun_name($curtime))
         echo "Success.<br>\n";
     else
         echo "Failed.<br>\n";
     
     echo "Fine.\n";
-    flush();
+    mop_flush();
 }
 
 main();