add match check on ttype to generate ranking just on certified and guaranteed users
[brisk.git] / web / briskin5 / statadm.php
1 <?php
2 /*
3  *  brisk - statadm.php
4  *
5  *  Copyright (C) 2009-2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 /*
26   line example:
27 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
28
29 */
30
31 foreach (array("HTTP_HOST", "DOCUMENT_ROOT") as $i) {
32     if (isset($_SERVER[$i])) {
33         $$i = $_SERVER[$i];
34         }
35     }
36
37 foreach (array("pazz") as $i) {
38     if (isset($_POST[$i])) {
39         $$i = $_POST[$i];
40         }
41     }
42
43 foreach (array("from", "to") as $i) {
44     if (isset($_GET[$i])) {
45         $$i = $_GET[$i];
46         }
47     }
48
49 $G_base = "../";
50
51 ini_set("max_execution_time",  "240");
52
53 require_once("../Obj/brisk.phh");
54 require_once("../Obj/user.phh");
55 require_once("../Obj/auth.phh");
56 require_once("../Obj/dbase_${G_dbasetype}.phh");
57 require_once("Obj/briskin5.phh");
58 require_once("Obj/placing.phh");
59
60 function main_pgsql($curtime)
61 {
62     GLOBAL $G_dbpfx;
63
64     if (($bdb = BriskDB::create()) == FALSE) {
65         echo "database connection failed";
66         exit;
67     }
68
69     $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
70     $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
71     $maxg = array( TRI_MAX_GAMES, MON_MAX_GAMES, WEE_MAX_GAMES );
72
73     do {
74         if ($bdb->transaction("BEGIN") == FALSE) {
75             log_crit("statadm: begin failed");
76             break;
77         }
78
79         $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp
80                             FROM %sbin5_matches as m, %sbin5_games as g
81                             WHERE (m.tcode = %d OR m.tcode = %d) AND m.code = g.mcode GROUP BY m.code, m.ttok",
82                            $G_dbpfx, $G_dbpfx, $G_dbpfx, BIN5_TOURNAMENT_OLDRULES, BIN5_TOURNAMENT_NO_DRAW);
83         if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) {
84             log_crit("statadm: temporary matches table creation [$mtc_sql] failed");
85             break;
86         }
87
88         $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE  tstamp < to_timestamp(%d)",
89                            $G_dbpfx, $curtime - TRI_LIMIT);
90
91         // if deletable old matches exists then ...
92         if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
93             //
94             // store matches before clean them
95             //
96             $fname = sprintf("%s/pts_archive%s.log", LEGAL_PATH, date("Ymd", $curtime));
97             if (($fp = @fopen($fname, 'a')) == FALSE) {
98                 log_crit("statadm: log file [$fname] open failed");
99                 break;
100             }
101
102             $tmt_n = pg_numrows($tmt_pg);
103             // get matches
104             for ($m = 0 ; $m < $tmt_n ; $m++) {
105                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
106
107                 $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d",
108                                    $G_dbpfx, $tmt_obj->code);
109
110                 if (($mtc_pg  = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
111                     log_crit("statadm: matches row select failed");
112                     break;
113                 }
114                 $mtc_obj = pg_fetch_object($mtc_pg, 0);
115
116                 if (fwrite($fp, sprintf("M|%d|%s|%d|%d|\n", $mtc_obj->code, xcapelt($mtc_obj->ttok),
117                                         $mtc_obj->tidx, $mtc_obj->tcode)) == FALSE) {
118                     log_crit("statadm: log file [$fname] write match failed");
119                     break;
120                 }
121
122                 // get games associated to each match
123                 $gam_sql = sprintf("SELECT code, mcode, EXTRACT(epoch FROM tstamp) AS tstamp FROM %sbin5_games
124                                     WHERE mcode = %d ORDER BY tstamp",
125                                    $G_dbpfx, $mtc_obj->code);
126                 if (($gam_pg  = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE) {
127                     log_crit("statadm: games row select failed");
128                     break;
129                 }
130
131                 $gam_n = pg_numrows($gam_pg);
132                 for ($g = 0 ; $g < $gam_n ; $g++) {
133                     $gam_obj = pg_fetch_object($gam_pg, $g);
134
135                     if (fwrite($fp, sprintf("G|%d|%d|%d|\n", $gam_obj->mcode, $gam_obj->code, $gam_obj->tstamp)) == FALSE) {
136                         log_crit("statadm: log file [$fname] write game failed");
137                         break;
138                     }
139                     $pts_sql = sprintf("SELECT * FROM %sbin5_points WHERE gcode = %d", $G_dbpfx, $gam_obj->code);
140                     if (($pts_pg  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
141                         log_crit("statadm: points row select [$pts_sql] failed");
142                         break;
143                     }
144                     $pts_n = pg_numrows($pts_pg);
145                     for ($p = 0 ; $p < $pts_n ; $p++) {
146                         $pts_obj = pg_fetch_object($pts_pg, $p);
147
148                         if (fwrite($fp, sprintf("P|%d|%d|%d|\n", $pts_obj->gcode, $pts_obj->ucode, $pts_obj->pts)) == FALSE) {
149                             log_crit("statadm: log file [$fname] write pts failed");
150                             break;
151                         }
152                     }
153                     if ($p < $pts_n)
154                         break;
155                 }
156                 if ($g < $gam_n)
157                     break;
158
159                 // delete match and all it's childs (games and points)
160                 $del_sql = sprintf("DELETE FROM %sbin5_matches WHERE code = %d",
161                                    $G_dbpfx, $tmt_obj->code);
162                 if (($del_pg = pg_query($bdb->dbconn->db(),$del_sql)) == FALSE || pg_affected_rows($del_pg) != 1) {
163                     log_crit("statadm: matches row deletion failed");
164                     break;
165                 }
166
167             }
168             if ($m < $tmt_n)
169                 break;
170         } // if (($tmt_pg = pg_query($bdb->dbco...
171
172         // GEN: Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
173         $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
174         if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
175             log_crit("statadm: truncate failed");
176             break;
177         }
178
179         for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
180             $old_score = array( 1000000000, 1000000000);
181             $old_gam   = array( -1, -1);
182             $rank      = array(  0,  0);
183
184             // TAG: POINTS_MANAGEMENT
185             $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
186                                 FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u
187                                 WHERE m.ttype <= 2 AND (m.tcode = %d OR m.tcode = %d) AND m.code = g.mcode AND
188                                       ( (u.type & (CAST (X'ff0000' as integer))) <> (CAST (X'800000' as integer)) ) AND
189                                       g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d) AND
190                                       p.ucode = u.code AND p.gcode = g.code AND
191                                       p.pts != 0
192                                 GROUP BY u.code, u.login
193                                 ORDER BY (float4(sum(p.pts)) * 100.0 ) /  float4(count(p.pts)) DESC,
194                                          count(p.pts) DESC",
195                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
196                                BIN5_TOURNAMENT_OLDRULES, BIN5_TOURNAMENT_NO_DRAW,
197                                $curtime - $limi[$dtime], $curtime);
198
199             // log_crit("statadm: INFO: [$pla_sql]");
200
201             if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE) {
202                 // no point found, abort
203                 log_crit("statadm: main placement select failed [$pla_sql]");
204                 break;
205             }
206
207             for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
208                 $pla_obj = pg_fetch_object($pla_pg,$i);
209                 if ($pla_obj->games < $ming[$dtime])
210                     continue;
211
212                 if ($pla_obj->games < $maxg[$dtime])
213                     $subty = 0;
214                 else
215                     $subty = 1;
216
217                 $ty = ($dtime * 2) + $subty;
218
219                 if ($pla_obj->games != $old_gam[$subty] || $pla_obj->score != $old_score[$subty]) {
220                     $rank[$subty]++;
221                 }
222                 $new_sql = sprintf("INSERT INTO %sbin5_places (type, rank, ucode, login, pts, games, score)
223                                     VALUES (%d, %d, %d, '%s', %d, %d, %f);",
224                                    $G_dbpfx, $ty, $rank[$subty], $pla_obj->ucode, escsql($pla_obj->login),
225                                    $pla_obj->points, $pla_obj->games, $pla_obj->score);
226                 if ( ! (($new_pg  = pg_query($bdb->dbconn->db(), $new_sql)) != FALSE &&
227                         pg_affected_rows($new_pg) == 1) ) {
228                     log_crit("statadm: new place insert failed: ".print_r($pla_obj, TRUE));
229                     break;
230                 }
231
232                 $old_gam[$subty]   = $pla_obj->games;
233                 $old_score[$subty] = $pla_obj->score;
234             } // for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
235             if ($i < pg_numrows($pla_pg)) {
236                 break;
237             }
238         } // for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
239         if ($dtime < count($limi)) {
240             break;
241         }
242
243         $mti_sql = sprintf("UPDATE %sbin5_places_mtime SET mtime = (to_timestamp(%d)) WHERE code = 0;",
244                            $G_dbpfx, $curtime);
245         if ( ! (($mti_pg  = pg_query($bdb->dbconn->db(), $mti_sql)) != FALSE &&
246                 pg_affected_rows($mti_pg) == 1) ) {
247             log_crit("statadm: new mtime insert failed.");
248             break;
249         }
250
251         if ($bdb->transaction("COMMIT") == FALSE) {
252             break;
253         }
254         return (TRUE);
255     } while (0);
256
257     $bdb->transaction("ROLLBACK");
258
259     return (FALSE);
260 }
261
262 function main()
263 {
264     GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz;
265
266     echo "Inizio.<br>";
267     mop_flush();
268     if ($pazz != $G_alarm_passwd) {
269         echo "Wrong password<br>";
270         mop_flush();
271         exit;
272     }
273
274     $fun_name = "main_${G_dbasetype}";
275
276     $ctime = time();
277
278     if (BIN5_PLAYERS_N != 5)
279         $curtime = $ctime;
280     else
281         $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
282     if ($ret = $fun_name($curtime))
283         echo "Success.<br>\n";
284     else
285         echo "Failed.<br>\n";
286
287     echo "Fine.\n";
288     mop_flush();
289 }
290
291 main();
292 ?>