17c6e8ce9fb5c11a3a59be8bdefbcdd2f7e54fed
[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.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</h3>\n", $tmt_obj->code, $users[$u]['first'], $users[$u]['last'], $users[$u]['tidx']);
171                         fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
172                     }
173                     fprintf($fpexp, "<th>%s</th>", $users[$u]['login']);
174                     // note: we are looping on users, order on them not needed
175                     $pts_sql = sprintf("
176 SELECT p.pts AS pts
177     FROM %sbin5_games AS g, %sbin5_points AS p
178     WHERE g.code = p.gcode AND g.mcode = %d AND p.ucode = %d
179     ORDER BY g.tstamp",
180                                        $G_dbpfx, $G_dbpfx,
181                                        $tmt_obj->code, $users[$u]['code']);
182
183                     // points of the match for each user
184                     if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
185                         log_crit("stat-day: pts_sql failed");
186                         break;
187                     }
188                     if ($u == 0) {
189                         $num_games = pg_numrows($pts_pg[$u]);
190                     }
191                     else {
192                         if ($num_games != pg_numrows($pts_pg[$u])) {
193                             log_crit("stat-day: num_games != pg_numrows");
194                             break;
195                         }
196                     }
197                 }
198                 if ($u != BIN5_PLAYERS_N) {
199                     log_crit("stat-day: u != BIN5_PLAYERS_N");
200                     break;
201                 }
202
203                 if ($tmt_obj->minus_one_is_old != -1) {
204                     fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
205                 }
206                 // LISTA DELLE VARIE PARTITE
207                 $pts_obj = array();
208                 for ($g = 0 ; $g < $num_games ; $g++) {
209                     $gam_obj = pg_fetch_object($gam_pg, $g);
210                     fprintf($fpexp, "<tr>");
211                     $pt_min   = 1000;
212                     $pt_min_n = 0;
213                     $pt_max   = -1000;
214                     $pt_max_n = 0;
215                     for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
216                         $pts_obj[$u] = pg_fetch_object($pts_pg[$u], $g);
217
218                         if ($pt_min > $pts_obj[$u]->pts) {
219                             $pt_min = $pts_obj[$u]->pts;
220                             $pt_min_n = 1;
221                         }
222                         else if ($pt_min == $pts_obj[$u]->pts) {
223                             $pt_min_n++;
224                         }
225
226                         if ($pt_max < $pts_obj[$u]->pts) {
227                             $pt_max = $pts_obj[$u]->pts;
228                             $pt_max_n = 1;
229                         }
230                         else if ($pt_max == $pts_obj[$u]->pts) {
231                             $pt_max_n++;
232                         }
233                     }
234                     if ($pt_min_n > 1) {
235                         $pt_min =  1000;
236                     }
237                     if ($pt_max_n > 1) {
238                         $pt_max = -1000;
239                     }
240
241                     /* cases:
242                        pts = 0       -> white
243                        pts == pt_min -> red
244                        pts == pt_max -> green
245                        pts < 0       -> light red
246                        pts > 0       -> light green
247                      */
248                     for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
249                         $pts = $pts_obj[$u]->pts;
250
251                         if ($pts == 0)
252                             $cla_nam = 'bg_white';
253                         else if ($pts == $pt_min)
254                             $cla_nam = 'bg_red';
255                         else if ($pts == $pt_max)
256                             $cla_nam = 'bg_green';
257                         else if ($pts < 0)
258                             $cla_nam = 'bg_lired';
259                         else if ($pts > 0)
260                             $cla_nam = 'bg_ligre';
261
262                         fprintf($fpexp, "<%s class='%s'>%d</%s>",
263                                 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"),
264                                 $cla_nam,
265                                 pow(2,$gam_obj->mult) * $pts,
266                                 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"));
267                     }
268                     if ($tmt_obj->minus_one_is_old != -1) {
269                         $rules_name = rules_id2name($trn_obj->code);
270                         fprintf($fpexp, "<td>%s</td><td>%s</td>", $users[$gam_obj->mazzo]['login'],
271                                 xcape( $rules_name::game_description($gam_obj->act, 'plain', $gam_obj->mult,
272                                                         $gam_obj->asta_win,
273                                                         ($gam_obj->asta_win != -1 ?
274                                                          $users[$gam_obj->asta_win]['login'] : ""),
275                                                         $gam_obj->friend,
276                                                         ($gam_obj->friend != -1 ?
277                                                          $users[$gam_obj->friend]['login'] : ""),
278                                                         $gam_obj->pnt, $gam_obj->asta_pnt, $gam_obj->tourn_pts) )
279                                 );
280                     }
281                     fprintf($fpexp, "</tr>\n");
282                 }
283
284                 // LISTA DEI TOTALI
285                 fprintf($fpexp, "<tr>");
286                 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
287                     // NOTE: this part must be revisited when we move to multiple game rules
288                     //       probably removing the sum and adding another nested iteration on games.
289                     $tot_sql = sprintf("
290 SELECT sum(p.pts * (2^g.mult)) AS pts
291     FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
292     WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
293         AND ( (u.type & (CAST (X'00ff0000' as integer))) <> (CAST (X'00800000' as integer)) )
294         AND m.code = %d AND u.code = %d",
295                                        $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
296                                        $tmt_obj->code, $users[$u]['code']);
297                     if (($tot_pg  = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
298                         break;
299                     }
300                     $tot_obj = pg_fetch_object($tot_pg, 0);
301                     fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
302                 }
303                 if ($tmt_obj->minus_one_is_old != -1) {
304                     fprintf($fpexp, "<th colspan='2'>%s</th></tr>\n", $mlang_stat_day['info_total'][$G_lang]);
305                 }
306                 fprintf($fpexp, "</table>\n");
307             }
308             if ($m < $tmt_n) {
309                 log_crit("stat-day: m < tmt_n");
310                 break;
311             }
312         }
313         if ($t < $trn_n) {
314             log_crit(sprintf("stat-day: t < trn_n (%d, %d)", $t, $trn_n));
315             break;
316         }
317         // if ($bdb->transaction("COMMIT") == FALSE) {
318         //     break;
319         // }
320         $ret = (TRUE);
321     } while (0);
322
323     // if ($ret == FALSE) {
324     //    $bdb->transaction("ROLLBACK");
325     // }
326     if ($fpexp != FALSE) {
327         fclose($fpexp);
328     }
329
330     return ($ret);
331 }
332
333 function main()
334 {
335     GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
336
337     if ($pazz != $G_alarm_passwd) {
338         echo "Wrong password<br>";
339         mop_flush();
340         exit;
341     }
342
343     $fun_name = "main_${G_dbasetype}";
344
345     if ($ret = $fun_name($from, $to))
346         echo "Success.<br>\n";
347     else
348         echo "Failed.<br>\n";
349
350     echo "Fine.\n";
351     mop_flush();
352 }
353
354 main();
355 ?>