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