removed legacy '*_file' functions and trailed spaces
[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 $G_base = "../";
32
33 ini_set("max_execution_time",  "240");
34
35 require_once("../Obj/brisk.phh");
36 require_once("../Obj/user.phh");
37 require_once("../Obj/auth.phh");
38 require_once("../Obj/dbase_${G_dbasetype}.phh");
39 require_once("Obj/briskin5.phh");
40 require_once("Obj/placing.phh");
41
42 function main_pgsql($curtime)
43 {
44     GLOBAL $G_dbpfx;
45
46     if (($bdb = BriskDB::create()) == FALSE) {
47         echo "database connection failed";
48         exit;
49     }
50
51     $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
52     $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
53     $maxg = array( TRI_MAX_GAMES, MON_MAX_GAMES, WEE_MAX_GAMES );
54
55     do {
56         if ($bdb->transaction("BEGIN") == FALSE) {
57             log_crit("statadm: begin failed");
58             break;
59         }
60
61         $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp
62                             FROM %sbin5_matches as m, %sbin5_games as g
63                             WHERE (m.tcode = %d OR m.tcode = %d) AND m.code = g.mcode GROUP BY m.code, m.ttok",
64                            $G_dbpfx, $G_dbpfx, $G_dbpfx, BIN5_TOURNAMENT_OLDRULES, BIN5_TOURNAMENT_NO_DRAW);
65         if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) {
66             log_crit("statadm: temporary matches table creation [$mtc_sql] failed");
67             break;
68         }
69
70         $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE  tstamp < to_timestamp(%d)",
71                            $G_dbpfx, $curtime - TRI_LIMIT);
72
73         // if deletable old matches exists then ...
74         if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
75             //
76             // store matches before clean them
77             //
78             $fname = sprintf("%s/pts_archive%s.log", LEGAL_PATH, date("Ymd", $curtime));
79             if (($fp = @fopen($fname, 'a')) == FALSE) {
80                 log_crit("statadm: log file [$fname] open failed");
81                 break;
82             }
83
84             $tmt_n = pg_numrows($tmt_pg);
85             // get matches
86             for ($m = 0 ; $m < $tmt_n ; $m++) {
87                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
88
89                 $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d",
90                                    $G_dbpfx, $tmt_obj->code);
91
92                 if (($mtc_pg  = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
93                     log_crit("statadm: matches row select failed");
94                     break;
95                 }
96                 $mtc_obj = pg_fetch_object($mtc_pg, 0);
97
98                 if (fwrite($fp, sprintf("M|%d|%s|%d|%d|\n", $mtc_obj->code, xcapelt($mtc_obj->ttok),
99                                         $mtc_obj->tidx, $mtc_obj->tcode)) == FALSE) {
100                     log_crit("statadm: log file [$fname] write match failed");
101                     break;
102                 }
103
104                 // get games associated to each match
105                 $gam_sql = sprintf("SELECT code, mcode, EXTRACT(epoch FROM tstamp) AS tstamp FROM %sbin5_games
106                                     WHERE mcode = %d ORDER BY tstamp",
107                                    $G_dbpfx, $mtc_obj->code);
108                 if (($gam_pg  = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE) {
109                     log_crit("statadm: games row select failed");
110                     break;
111                 }
112
113                 $gam_n = pg_numrows($gam_pg);
114                 for ($g = 0 ; $g < $gam_n ; $g++) {
115                     $gam_obj = pg_fetch_object($gam_pg, $g);
116
117                     if (fwrite($fp, sprintf("G|%d|%d|%d|\n", $gam_obj->mcode, $gam_obj->code, $gam_obj->tstamp)) == FALSE) {
118                         log_crit("statadm: log file [$fname] write game failed");
119                         break;
120                     }
121                     $pts_sql = sprintf("SELECT * FROM %sbin5_points WHERE gcode = %d", $G_dbpfx, $gam_obj->code);
122                     if (($pts_pg  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
123                         log_crit("statadm: points row select [$pts_sql] failed");
124                         break;
125                     }
126                     $pts_n = pg_numrows($pts_pg);
127                     for ($p = 0 ; $p < $pts_n ; $p++) {
128                         $pts_obj = pg_fetch_object($pts_pg, $p);
129
130                         if (fwrite($fp, sprintf("P|%d|%d|%d|\n", $pts_obj->gcode, $pts_obj->ucode, $pts_obj->pts)) == FALSE) {
131                             log_crit("statadm: log file [$fname] write pts failed");
132                             break;
133                         }
134                     }
135                     if ($p < $pts_n)
136                         break;
137                 }
138                 if ($g < $gam_n)
139                     break;
140
141                 // delete match and all it's childs (games and points)
142                 $del_sql = sprintf("DELETE FROM %sbin5_matches WHERE code = %d",
143                                    $G_dbpfx, $tmt_obj->code);
144                 if (($del_pg = pg_query($bdb->dbconn->db(),$del_sql)) == FALSE || pg_affected_rows($del_pg) != 1) {
145                     log_crit("statadm: matches row deletion failed");
146                     break;
147                 }
148
149             }
150             if ($m < $tmt_n)
151                 break;
152         } // if (($tmt_pg = pg_query($bdb->dbco...
153
154         // GEN: Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
155         $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
156         if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
157             log_crit("statadm: truncate failed");
158             break;
159         }
160
161         for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
162             $old_score = array( 1000000000, 1000000000);
163             $old_gam   = array( -1, -1);
164             $rank      = array(  0,  0);
165
166             // TAG: POINTS_MANAGEMENT
167             $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
168                                 FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u
169                                 WHERE (m.tcode = %d OR m.tcode = %d) AND m.code = g.mcode AND
170                                       ( (u.type & (CAST (X'ff0000' as integer))) <> (CAST (X'800000' as integer)) ) AND
171                                       g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d) AND
172                                       p.ucode = u.code AND p.gcode = g.code AND
173                                       p.pts != 0
174                                 GROUP BY u.code, u.login
175                                 ORDER BY (float4(sum(p.pts)) * 100.0 ) /  float4(count(p.pts)) DESC,
176                                          count(p.pts) DESC",
177                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
178                                BIN5_TOURNAMENT_OLDRULES, BIN5_TOURNAMENT_NO_DRAW,
179                                $curtime - $limi[$dtime], $curtime);
180
181             // log_crit("statadm: INFO: [$pla_sql]");
182
183             if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE) {
184                 // no point found, abort
185                 log_crit("statadm: main placement select failed [$pla_sql]");
186                 break;
187             }
188
189             for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
190                 $pla_obj = pg_fetch_object($pla_pg,$i);
191                 if ($pla_obj->games < $ming[$dtime])
192                     continue;
193
194                 if ($pla_obj->games < $maxg[$dtime])
195                     $subty = 0;
196                 else
197                     $subty = 1;
198
199                 $ty = ($dtime * 2) + $subty;
200
201                 if ($pla_obj->games != $old_gam[$subty] || $pla_obj->score != $old_score[$subty]) {
202                     $rank[$subty]++;
203                 }
204                 $new_sql = sprintf("INSERT INTO %sbin5_places (type, rank, ucode, login, pts, games, score)
205                                     VALUES (%d, %d, %d, '%s', %d, %d, %f);",
206                                    $G_dbpfx, $ty, $rank[$subty], $pla_obj->ucode, escsql($pla_obj->login),
207                                    $pla_obj->points, $pla_obj->games, $pla_obj->score);
208                 if ( ! (($new_pg  = pg_query($bdb->dbconn->db(), $new_sql)) != FALSE &&
209                         pg_affected_rows($new_pg) == 1) ) {
210                     log_crit("statadm: new place insert failed: ".print_r($pla_obj, TRUE));
211                     break;
212                 }
213
214                 $old_gam[$subty]   = $pla_obj->games;
215                 $old_score[$subty] = $pla_obj->score;
216             } // for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
217             if ($i < pg_numrows($pla_pg)) {
218                 break;
219             }
220         } // for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
221         if ($dtime < count($limi)) {
222             break;
223         }
224
225         $mti_sql = sprintf("UPDATE %sbin5_places_mtime SET mtime = (to_timestamp(%d)) WHERE code = 0;",
226                            $G_dbpfx, $curtime);
227         if ( ! (($mti_pg  = pg_query($bdb->dbconn->db(), $mti_sql)) != FALSE &&
228                 pg_affected_rows($mti_pg) == 1) ) {
229             log_crit("statadm: new mtime insert failed.");
230             break;
231         }
232
233         if ($bdb->transaction("COMMIT") == FALSE) {
234             break;
235         }
236         return (TRUE);
237     } while (0);
238
239     $bdb->transaction("ROLLBACK");
240
241     return (FALSE);
242 }
243
244 function main()
245 {
246     GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz;
247
248     echo "Inizio.<br>";
249     mop_flush();
250     if ($pazz != $G_alarm_passwd) {
251         echo "Wrong password<br>";
252         mop_flush();
253         exit;
254     }
255
256     $fun_name = "main_${G_dbasetype}";
257
258     $ctime = time();
259
260     if (BIN5_PLAYERS_N != 5)
261         $curtime = $ctime;
262     else
263         $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
264     if ($ret = $fun_name($curtime))
265         echo "Success.<br>\n";
266     else
267         echo "Failed.<br>\n";
268
269     echo "Fine.\n";
270     mop_flush();
271 }
272
273 main();
274 ?>