schema updated and stat-day.php fixed
[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 to="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "($now / 86400) * 86400 + 7200 " | bc))"
32 from="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "($now / 86400) * 86400 + 7200 - 86400" | bc))"
33 # to="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$now + 7200 " | bc))"
34 # from="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$now - 9200 " | bc))"
35
36 curl -d 'pazz=simbaalarm' "http://www.alternativeoutput.it/brisk/briskin5/stat-day.php?from=$from&to=$to"
37
38 */
39
40 $G_base = "../";
41
42 $mlang_stat_day = array( 'normal match'=> array( 'it' => 'Partite normali',
43                                                  'en' => 'Normal matches' ),
44                          'special match' => array( 'it' => 'Partite speciali',
45                                                    'en' => 'Special matches')
46                          );
47
48
49 ini_set("max_execution_time",  "240");
50
51 require_once("../Obj/brisk.phh");
52 require_once("../Obj/user.phh");
53 require_once("../Obj/auth.phh");
54 require_once("../Obj/dbase_${G_dbasetype}.phh");
55 require_once("Obj/briskin5.phh");
56 require_once("Obj/placing.phh");
57
58 function main_file($curtime)
59 {
60     GLOBAL $G_lang, $G_alarm_passwd;
61   $tri = array();
62   $mon = array();
63   $wee = array();
64
65   if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
66     echo "Open data file error";
67     exit;
68   }
69   echo "prima<br>";
70
71   if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
72     $skip = intval(fgets($fp_start));
73     if ($skip > 0)
74       fseek($fp, $skip, SEEK_SET);
75     fclose($fp_start);
76   }
77
78   if (($bdb = BriskDB::create()) == FALSE) {
79     echo "database connection failed";
80     exit;
81   }
82
83   $bdb->users_load();
84
85   for ($i = 0 ; $i < $bdb->count() ; $i++) {
86     $login = $bdb->getlogin_byidx($i);
87     $tri[$i] = new Ptsgam($login);
88     $mon[$i] = new Ptsgam($login);
89     $wee[$i] = new Ptsgam($login);
90   }
91
92   // recalculate all the placings
93   // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
94   while (!feof($fp)) {
95     $p = 0;
96     $bf = fgets($fp, 4096);
97     $ar = csplitter($bf, '|');
98     // if not auth table, continue
99     if (count($ar) < 15)
100       continue;
101
102     // echo $p++."<br>";
103     if ($ar[7] >= TABLES_AUTH_N)
104       continue;
105     // echo $p++." ".$ar[5]."<br>";
106     // if not FINISH_GAME line, continue
107     if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
108       continue;
109     // echo $p++."<br>";
110     // if to much old points, continue
111     if ($ar[0] < $curtime - TRI_LIMIT) {
112       if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'w')) != FALSE) {
113         $curpos = ftell($fp);
114         fwrite($fp_start, sprintf("%d\n", $curpos));
115         fclose($fp_start);
116       }
117
118       continue;
119     }
120     // echo $p++." ".BIN5_PLAYERS_N."<br>";
121
122     $found = FALSE;
123     $mult = 1;
124     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
125       for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
126         if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
127           $mult = abs($ar[10+($i*2)]);
128           $found = TRUE;
129         }
130       }
131       if ($found)
132         break;
133     }
134
135     if ($mult == 0)
136        continue;
137     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
138       // echo $p." i) ".$i."<br>";
139       $username = $ar[9+($i*2)];
140       if (($item = $bdb->getitem_bylogin($username, &$id)) == FALSE) {
141         echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
142         continue;
143       }
144
145       // echo $item->login." id)".$id."  ".$ar[10+($i*2)]." mult: ".$mult."<br>";
146       $tri[$id]->add($ar[10+($i*2)] / $mult);
147       if ($ar[0] >= $curtime - MON_LIMIT)
148         $mon[$id]->add($ar[10+($i*2)] / $mult);
149       if ($ar[0] >= $curtime - WEE_LIMIT)
150         $wee[$id]->add($ar[10+($i*2)] / $mult);
151     }
152     // $p++; echo $p++."<br>";
153   }
154   fclose($fp);
155
156   usort($tri, ptsgam_cmp);
157   usort($mon, ptsgam_cmp);
158   usort($wee, ptsgam_cmp);
159
160   echo "<br><br>TRI<br>\n";
161
162   if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
163     echo "Open tri_lo failed<br>";
164     exit;
165   }
166   if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
167     echo "Open tri_hi failed<br>";
168     exit;
169   }
170
171   for ($i = 0 ; $i < count($tri) ; $i++) {
172     if ($tri[$i]->gam == 0.0)
173       continue;
174     printf("%s: %s (%d) <br>\n",  $tri[$i]->username,  $tri[$i]->snormpts(), $tri[$i]->gam);
175     if ($tri[$i]->gam >= TRI_MAX_GAMES)
176       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
177     else if ($tri[$i]->gam > TRI_MIN_GAMES)
178       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
179   }
180   fclose($fphi);
181   fclose($fplo);
182
183   echo "<br><br>MON<br>\n";
184
185   if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
186     echo "Open tri_lo failed<br>";
187     exit;
188   }
189   if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
190     echo "Open tri_hi failed<br>";
191     exit;
192   }
193
194   for ($i = 0 ; $i < count($mon) ; $i++) {
195     if ($mon[$i]->gam == 0.0)
196       continue;
197     printf("%s: %s (%d) <br>\n",  $mon[$i]->username,  $mon[$i]->snormpts(), $mon[$i]->gam);
198     if ($mon[$i]->gam >= MON_MAX_GAMES)
199       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
200     else if ($mon[$i]->gam > MON_MIN_GAMES)
201       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
202   }
203   fclose($fphi);
204   fclose($fplo);
205
206   echo "<br><br>WEE<br>\n";
207   if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
208     echo "Open wee_lo failed<br>";
209     exit;
210   }
211   if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
212     echo "Open wee_hi failed<br>";
213     exit;
214   }
215
216   for ($i = 0 ; $i < count($wee) ; $i++) {
217     if ($wee[$i]->gam == 0.0)
218       continue;
219     printf("%s: %s (%d) <br>\n",  $wee[$i]->username,  $wee[$i]->snormpts(), $wee[$i]->gam);
220     if ($wee[$i]->gam >= WEE_MAX_GAMES)
221       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
222     else if ($wee[$i]->gam > WEE_MIN_GAMES)
223       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
224   }
225   fclose($fphi);
226   fclose($fplo);
227
228 }
229
230 function main_pgsql($from, $to)
231 {
232     GLOBAL $G_lang, $G_dbpfx, $mlang_stat_day;
233
234     $ret = FALSE;
235     $fpexp = FALSE;
236
237     do {
238         if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
239             log_crit("stat-day: open explain failed");
240             break;
241         }
242         fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
243                 $from, $to);
244
245         if (($bdb = BriskDB::create()) == FALSE) {
246             log_crit("stat-day: database connection failed");
247             break;
248         }
249
250         if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
251             log_crit("stat-day: begin failed");
252             break;
253         }
254
255         // retrieve list of active tournaments
256         $trn_sql = sprintf("SELECT * FROM %sbin5_tournaments WHERE active = 1;", $G_dbpfx);
257         if (($trn_pg = pg_query($bdb->dbconn->db(), $trn_sql)) == FALSE) {
258             log_crit("stat-day: select from tournaments failed");
259             break;
260         }
261
262         $trn_n = pg_numrows($trn_pg);
263
264         for ($t = 0 ; $t < $trn_n ; $t++) {
265             $trn_obj = pg_fetch_object($trn_pg, $t);
266
267             $tmt_sql = sprintf("SELECT m.code AS code FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_tournaments as t WHERE t.code = m.tcode AND m.code = g.mcode AND g.tstamp >= '%s' AND g.tstamp < '%s' GROUP BY m.code;",
268                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $from, $to);
269
270             // if deletable old matches exists then ...
271             if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) == FALSE) {
272                 log_crit("stat-day: select from matches failed");
273                 break;
274             }
275
276             //
277             // store matches before clean them
278             //
279             $tmt_n = pg_numrows($tmt_pg);
280             // get matches
281             if ($tmt_n == 0)
282                 continue;
283
284             if (!isset($mlang_stat_day[$trn_obj->name][$G_lang])) {
285                 log_crit("stat-day: tournament name not found in array");
286                 break;
287             }
288             fprintf($fpexp, "<h3>%s</h3>", $mlang_stat_day[$trn_obj->name][$G_lang]);
289
290             for ($m = 0 ; $m < $tmt_n ; $m++) {
291                 fprintf($fpexp, "<br>");
292                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
293
294                 $usr_sql = sprintf("
295 SELECT u.code AS code, u.login AS login, min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u, %sbin5_table_orders AS o WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND m.code = o.mcode AND u.code = o.ucode GROUP BY u.code, u.login, m.tidx, o.pos ORDER BY o.pos;", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
296
297                 if (($usr_pg  = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE ) {
298                     break;
299                 }
300                 $usr_n = pg_numrows($usr_pg);
301                 if ($usr_n != BIN5_PLAYERS_N) {
302                     break;
303                 }
304
305                 $gam_sql = sprintf("SELECT g.* FROM %sbin5_games AS g, %sbin5_matches AS m WHERE g.mcode = m.code AND m.code = %d ORDER BY g.tstamp;",
306                                    $G_dbpfx, $G_dbpfx, $tmt_obj->code);
307                 if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE ) {
308                     break;
309                 }
310
311                 $usr_obj = array();
312                 for ($u = 0 ; $u < $usr_n ; $u++) {
313                     $usr_obj[$u] = pg_fetch_object($usr_pg, $u);
314                     if ($u == 0) {
315                         fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $usr_obj[$u]->first, $usr_obj[$u]->last, $usr_obj[$u]->tidx);
316                         fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
317                     }
318                     fprintf($fpexp, "<th>%s</th>", $usr_obj[$u]->login);
319                     $pts_sql = sprintf("SELECT p.pts as pts from %sbin5_matches as m, %sbin5_games as g, %sbin5_points as p, %susers as u WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND u.code = %d ORDER BY g.code", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
320                                        $tmt_obj->code, $usr_obj[$u]->code);
321
322                     // points of the match for each user
323                     if (($pts_pg[$u]  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
324                         break;
325                     }
326                     if ($u == 0) {
327                         $num_games = pg_numrows($pts_pg[$u]);
328                     }
329                     else {
330                         if ($num_games != pg_numrows($pts_pg[$u])) {
331                             break;
332                         }
333                     }
334                 }
335                 if ($u != BIN5_PLAYERS_N) {
336                     break;
337                 }
338                 fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
339
340                 // LISTA DELLE VARIE PARTITE
341                 for ($g = 0 ; $g < $num_games ; $g++) {
342                     $gam_obj = pg_fetch_object($gam_pg, $g);
343                     fprintf($fpexp, "<tr>");
344                     for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
345                         $pts_obj = pg_fetch_object($pts_pg[$u], $g);
346                         fprintf($fpexp, "<th>%d</th>", $pts_obj->pts);
347                     }
348                     fprintf($fpexp, "<td>%s</td><td>%s</td>", $usr_obj[$gam_obj->mazzo]->login,
349                             xcape( game_description($gam_obj->act, 'plain',
350                                                     $gam_obj->asta_win, $usr_obj[$gam_obj->asta_win]->login,
351                                                     $gam_obj->friend, $usr_obj[$gam_obj->friend]->login,
352                                                     $gam_obj->pnt, $gam_obj->asta_pnt) )
353                             );
354                     fprintf($fpexp, "</tr>\n");
355                 }
356
357                 // LISTA DEI TOTALI
358                 fprintf($fpexp, "<tr>");
359                 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
360                     $tot_sql = sprintf("
361 SELECT SUM(p.pts) AS pts FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d AND u.code = %d", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
362                                        $tmt_obj->code, $usr_obj[$u]->code);
363                     if (($tot_pg  = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
364                         break;
365                     }
366                     $tot_obj = pg_fetch_object($tot_pg, 0);
367                     fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
368                 }
369                 fprintf($fpexp, "</tr>\n");
370                 fprintf($fpexp, "</table>\n");
371             }
372             if ($m < $tmt_n)
373                 break;
374         }
375         if ($t < $trn_n)
376             break;
377         $ret = (TRUE);
378     } while (0);
379
380     if ($ret == FALSE) {
381         pg_query($bdb->dbconn->db(), "ROLLBACK");
382     }
383     if ($fpexp != FALSE) {
384         fclose($fpexp);
385     }
386
387     return ($ret);
388 }
389
390 // echo "QUIr\n";
391 // exit(123);
392 function main()
393 {
394     GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
395
396     if ($pazz != $G_alarm_passwd) {
397         echo "Wrong password<br>";
398         mop_flush();
399         exit;
400     }
401
402     $fun_name = "main_${G_dbasetype}";
403
404     if ($ret = $fun_name($from, $to))
405         echo "Success.<br>\n";
406     else
407         echo "Failed.<br>\n";
408
409     echo "Fine.\n";
410     mop_flush();
411 }
412
413 main();
414 ?>