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