removed legacy '*_file' functions and trailed spaces
[brisk.git] / web / briskin5 / stat-day.php
1 <?php
2 /*
3  *  brisk - stat-day.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   call example:
27 wget -O daily.txt dodo.birds.van/brisk/briskin5/stat-day.php?pazz=yourpasswd&from=1000&to=1380308086
28
29 now="$(date +%s)"
30
31 # from 100 days ago to 1 day after
32 to="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$(date +%s) + 86400" | bc))"
33 from="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$(date +%s) - 8640000" | bc))"
34 # to="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$now + 7200 " | bc))"
35 # from="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$now - 9200 " | bc))"
36
37 curl -d "pazz=$BRISK_PASS" "http://$BRISK_SITE/briskin5/stat-day.php?from=$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$(date +%s) - 8640000" | bc))&to=$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$(date +%s) + 86400" | bc))"
38
39 */
40
41 $G_base = "../";
42
43 // SYNC WITH bin5_tournaments table
44 $mlang_stat_day = array(
45                          'old rules: with draw'=> array( 'it' => 'Partite vecchie (con il pareggio)',
46                                                          'en' => 'Old matches (with draw)' ),
47
48                          'new rules: without draw' => array( 'it' => 'Partite nuove (senza pareggio)',
49                                                              'en' => 'New matches (without draw)'),
50
51                          'special match' => array( 'it' => 'Partite speciali',
52                                                    'en' => 'Special matches'),
53
54                          'info_total'=> array( 'it' => 'totali',
55                                                'en' => 'En totali')
56                          );
57
58
59 ini_set("max_execution_time",  "240");
60
61 require_once("../Obj/brisk.phh");
62 require_once("../Obj/user.phh");
63 require_once("../Obj/auth.phh");
64 require_once("../Obj/dbase_${G_dbasetype}.phh");
65 require_once("Obj/briskin5.phh");
66 require_once("Obj/placing.phh");
67
68 function main_pgsql($from, $to)
69 {
70     GLOBAL $G_lang, $G_dbpfx, $mlang_stat_day;
71
72     $ret = FALSE;
73     $fpexp = FALSE;
74
75     // log_crit("stat-day: BEGIN");
76     do {
77         if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
78             log_crit("stat-day: open explain failed");
79             break;
80         }
81         fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
82                 $from, $to);
83
84         if (($bdb = BriskDB::create()) == FALSE) {
85             log_crit("stat-day: database connection failed");
86             break;
87         }
88
89         //if ($bdb->transaction("BEGIN") == FALSE) {
90         //    log_crit("stat-day: begin failed");
91         //    break;
92         //}
93
94         // retrieve list of active tournaments
95         $trn_sql = sprintf("SELECT * FROM %sbin5_tournaments WHERE active = 1;", $G_dbpfx);
96         if (($trn_pg = pg_query($bdb->dbconn->db(), $trn_sql)) == FALSE) {
97             log_crit("stat-day: select from tournaments failed");
98             break;
99         }
100
101         $trn_n = pg_numrows($trn_pg);
102         printf("Number of tournaments: %d\n", $trn_n);
103
104         // loop on tournaments
105         for ($t = 0 ; $t < $trn_n ; $t++) {
106             // log_crit("stat-day: LOOP t");
107             $trn_obj = pg_fetch_object($trn_pg, $t);
108
109             $tmt_sql = sprintf("
110 SELECT m.code AS code, m.mazzo_next as minus_one_is_old
111     FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_tournaments as t
112     WHERE t.code = m.tcode AND m.code = g.mcode
113         AND t.code = %d AND g.tstamp >= '%s' AND g.tstamp < '%s'
114     GROUP BY m.code, minus_one_is_old
115     ORDER BY m.code, minus_one_is_old DESC;",
116                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $trn_obj->code, $from, $to);
117
118             // if deletable old matches exists then ...
119             if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) == FALSE) {
120                 log_crit("stat-day: select from matches failed");
121                 break;
122             }
123
124             //
125             // store matches before clean them
126             //
127             $tmt_n = pg_numrows($tmt_pg);
128             // get matches
129             if ($tmt_n == 0)
130                 continue;
131
132             if (!isset($mlang_stat_day[$trn_obj->name][$G_lang])) {
133                 log_crit("stat-day: tournament name not found in array");
134                 break;
135             }
136             printf("[Tournament [%s]], number of matches: %d\n", $mlang_stat_day[$trn_obj->name][$G_lang], $tmt_n);
137             fprintf($fpexp, "<h2 style=\"padding: 8px; background-color: pink;\">%s</h2>", $mlang_stat_day[$trn_obj->name][$G_lang]);
138
139             // loop on matches
140             for ($m = 0 ; $m < $tmt_n ; $m++) {
141                 // log_crit("stat-day: LOOP m");
142                 fprintf($fpexp, "<br>");
143                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
144
145                 // get users for the match m
146                 if (($users = $bdb->users_get($tmt_obj->code, TRUE, ($tmt_obj->minus_one_is_old > -1))) == FALSE) {
147                     log_crit(sprintf("stat_day: users_get failed %d", $tmt_obj->code));
148                     break;
149                 }
150
151                 $gam_sql = sprintf("
152 SELECT g.* FROM %sbin5_tournaments AS t, %sbin5_matches AS m, %sbin5_games AS g
153     WHERE t.code = m.tcode AND m.code = g.mcode AND m.code = %d
154     ORDER BY g.tstamp;",
155                                    $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
156                 if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE ) {
157                     log_crit("stat-day: gam_sql failed");
158                     break;
159                 }
160
161                 // loop on users of the match m
162                 for ($u = 0 ; $u < count($users) ; $u++) {
163                     // log_crit("stat-day: LOOP u");
164                     if ($u == 0) {
165                         fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $users[$u]['first'], $users[$u]['last'], $users[$u]['tidx']);
166                         fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
167                     }
168                     fprintf($fpexp, "<th>%s</th>", $users[$u]['login']);
169                     // note: we are looping on users, order on them not needed
170                     $pts_sql = sprintf("
171 SELECT p.pts AS pts
172     FROM %sbin5_games AS g, %sbin5_points AS p
173     WHERE g.code = p.gcode AND g.mcode = %d AND p.ucode = %d
174     ORDER BY g.tstamp",
175                                        $G_dbpfx, $G_dbpfx,
176                                        $tmt_obj->code, $users[$u]['code']);
177
178                     // points of the match for each user
179                     if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
180                         log_crit("stat-day: pts_sql failed");
181                         break;
182                     }
183                     if ($u == 0) {
184                         $num_games = pg_numrows($pts_pg[$u]);
185                     }
186                     else {
187                         if ($num_games != pg_numrows($pts_pg[$u])) {
188                             log_crit("stat-day: num_games != pg_numrows");
189                             break;
190                         }
191                     }
192                 }
193                 if ($u != BIN5_PLAYERS_N) {
194                     log_crit("stat-day: u != BIN5_PLAYERS_N");
195                     break;
196                 }
197
198                 if ($tmt_obj->minus_one_is_old != -1) {
199                     fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
200                 }
201                 // LISTA DELLE VARIE PARTITE
202                 $pts_obj = array();
203                 for ($g = 0 ; $g < $num_games ; $g++) {
204                     $gam_obj = pg_fetch_object($gam_pg, $g);
205                     fprintf($fpexp, "<tr>");
206                     $pt_min   = 1000;
207                     $pt_min_n = 0;
208                     $pt_max   = -1000;
209                     $pt_max_n = 0;
210                     for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
211                         $pts_obj[$u] = pg_fetch_object($pts_pg[$u], $g);
212
213                         if ($pt_min > $pts_obj[$u]->pts) {
214                             $pt_min = $pts_obj[$u]->pts;
215                             $pt_min_n = 1;
216                         }
217                         else if ($pt_min == $pts_obj[$u]->pts) {
218                             $pt_min_n++;
219                         }
220
221                         if ($pt_max < $pts_obj[$u]->pts) {
222                             $pt_max = $pts_obj[$u]->pts;
223                             $pt_max_n = 1;
224                         }
225                         else if ($pt_max == $pts_obj[$u]->pts) {
226                             $pt_max_n++;
227                         }
228                     }
229                     if ($pt_min_n > 1) {
230                         $pt_min =  1000;
231                     }
232                     if ($pt_max_n > 1) {
233                         $pt_max = -1000;
234                     }
235
236                     /* cases:
237                        pts = 0       -> white
238                        pts == pt_min -> red
239                        pts == pt_max -> green
240                        pts < 0       -> light red
241                        pts > 0       -> light green
242                      */
243                     for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
244                         $pts = $pts_obj[$u]->pts;
245
246                         if ($pts == 0)
247                             $cla_nam = 'bg_white';
248                         else if ($pts == $pt_min)
249                             $cla_nam = 'bg_red';
250                         else if ($pts == $pt_max)
251                             $cla_nam = 'bg_green';
252                         else if ($pts < 0)
253                             $cla_nam = 'bg_lired';
254                         else if ($pts > 0)
255                             $cla_nam = 'bg_ligre';
256
257                         fprintf($fpexp, "<%s class='%s'>%d</%s>",
258                                 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"),
259                                 $cla_nam,
260                                 pow(2,$gam_obj->mult) * $pts,
261                                 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"));
262                     }
263                     if ($tmt_obj->minus_one_is_old != -1) {
264                         // FIXME
265                         $rules_name = "Rules_old_rules";
266                         fprintf($fpexp, "<td>%s</td><td>%s</td>", $users[$gam_obj->mazzo]['login'],
267                                 xcape( ${rules_name}::game_description($gam_obj->act, 'plain', $gam_obj->mult,
268                                                         $gam_obj->asta_win,
269                                                         ($gam_obj->asta_win != -1 ?
270                                                          $users[$gam_obj->asta_win]['login'] : ""),
271                                                         $gam_obj->friend,
272                                                         ($gam_obj->friend != -1 ?
273                                                          $users[$gam_obj->friend]['login'] : ""),
274                                                         $gam_obj->pnt, $gam_obj->asta_pnt, $gam_obj->tourn_pts) )
275                                 );
276                     }
277                     fprintf($fpexp, "</tr>\n");
278                 }
279
280                 // LISTA DEI TOTALI
281                 fprintf($fpexp, "<tr>");
282                 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
283                     // NOTE: this part must be revisited when we move to multiple game rules
284                     //       probably removing the sum and adding another nested iteration on games.
285                     $tot_sql = sprintf("
286 SELECT sum(p.pts * (2^g.mult)) AS pts
287     FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
288     WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
289         AND ( (u.type & (CAST (X'00ff0000' as integer))) <> (CAST (X'00800000' as integer)) )
290         AND m.code = %d AND u.code = %d",
291                                        $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
292                                        $tmt_obj->code, $users[$u]['code']);
293                     if (($tot_pg  = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
294                         break;
295                     }
296                     $tot_obj = pg_fetch_object($tot_pg, 0);
297                     fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
298                 }
299                 if ($tmt_obj->minus_one_is_old != -1) {
300                     fprintf($fpexp, "<th colspan='2'>%s</th></tr>\n", $mlang_stat_day['info_total'][$G_lang]);
301                 }
302                 fprintf($fpexp, "</table>\n");
303             }
304             if ($m < $tmt_n) {
305                 log_crit("stat-day: m < tmt_n");
306                 break;
307             }
308         }
309         if ($t < $trn_n) {
310             log_crit(sprintf("stat-day: t < trn_n (%d, %d)", $t, $trn_n));
311             break;
312         }
313         // if ($bdb->transaction("COMMIT") == FALSE) {
314         //     break;
315         // }
316         $ret = (TRUE);
317     } while (0);
318
319     // if ($ret == FALSE) {
320     //    $bdb->transaction("ROLLBACK");
321     // }
322     if ($fpexp != FALSE) {
323         fclose($fpexp);
324     }
325
326     return ($ret);
327 }
328
329 function main()
330 {
331     GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
332
333     if ($pazz != $G_alarm_passwd) {
334         echo "Wrong password<br>";
335         mop_flush();
336         exit;
337     }
338
339     $fun_name = "main_${G_dbasetype}";
340
341     if ($ret = $fun_name($from, $to))
342         echo "Success.<br>\n";
343     else
344         echo "Failed.<br>\n";
345
346     echo "Fine.\n";
347     mop_flush();
348 }
349
350 main();
351 ?>