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