game_description() manage all actions (pass, abandon, finish)
[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 "$now + 86400" | bc))"
33 from="$(date +"%Y-%m-%d+%H:%M:%S" -d @$(echo "$now - 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=$from&to=$to"
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
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_file($curtime)
60 {
61     GLOBAL $G_lang, $G_alarm_passwd;
62   $tri = array();
63   $mon = array();
64   $wee = array();
65
66   if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
67     echo "Open data file error";
68     exit;
69   }
70   echo "prima<br>";
71
72   if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
73     $skip = intval(fgets($fp_start));
74     if ($skip > 0)
75       fseek($fp, $skip, SEEK_SET);
76     fclose($fp_start);
77   }
78
79   if (($bdb = BriskDB::create()) == FALSE) {
80     echo "database connection failed";
81     exit;
82   }
83
84   $bdb->users_load();
85
86   for ($i = 0 ; $i < $bdb->count() ; $i++) {
87     $login = $bdb->getlogin_byidx($i);
88     $tri[$i] = new Ptsgam($login);
89     $mon[$i] = new Ptsgam($login);
90     $wee[$i] = new Ptsgam($login);
91   }
92
93   // recalculate all the placings
94   // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
95   while (!feof($fp)) {
96     $p = 0;
97     $bf = fgets($fp, 4096);
98     $ar = csplitter($bf, '|');
99     // if not auth table, continue
100     if (count($ar) < 15)
101       continue;
102
103     // echo $p++."<br>";
104     if ($ar[7] >= TABLES_AUTH_N)
105       continue;
106     // echo $p++." ".$ar[5]."<br>";
107     // if not FINISH_GAME line, continue
108     if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
109       continue;
110     // echo $p++."<br>";
111     // if to much old points, continue
112     if ($ar[0] < $curtime - TRI_LIMIT) {
113       if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'w')) != FALSE) {
114         $curpos = ftell($fp);
115         fwrite($fp_start, sprintf("%d\n", $curpos));
116         fclose($fp_start);
117       }
118
119       continue;
120     }
121     // echo $p++." ".BIN5_PLAYERS_N."<br>";
122
123     $found = FALSE;
124     $mult = 1;
125     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
126       for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
127         if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
128           $mult = abs($ar[10+($i*2)]);
129           $found = TRUE;
130         }
131       }
132       if ($found)
133         break;
134     }
135
136     if ($mult == 0)
137        continue;
138     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
139       // echo $p." i) ".$i."<br>";
140       $username = $ar[9+($i*2)];
141       if (($item = $bdb->getitem_bylogin($username, &$id)) == FALSE) {
142         echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
143         continue;
144       }
145
146       // echo $item->login." id)".$id."  ".$ar[10+($i*2)]." mult: ".$mult."<br>";
147       $tri[$id]->add($ar[10+($i*2)] / $mult);
148       if ($ar[0] >= $curtime - MON_LIMIT)
149         $mon[$id]->add($ar[10+($i*2)] / $mult);
150       if ($ar[0] >= $curtime - WEE_LIMIT)
151         $wee[$id]->add($ar[10+($i*2)] / $mult);
152     }
153     // $p++; echo $p++."<br>";
154   }
155   fclose($fp);
156
157   usort($tri, ptsgam_cmp);
158   usort($mon, ptsgam_cmp);
159   usort($wee, ptsgam_cmp);
160
161   echo "<br><br>TRI<br>\n";
162
163   if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
164     echo "Open tri_lo failed<br>";
165     exit;
166   }
167   if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
168     echo "Open tri_hi failed<br>";
169     exit;
170   }
171
172   for ($i = 0 ; $i < count($tri) ; $i++) {
173     if ($tri[$i]->gam == 0.0)
174       continue;
175     printf("%s: %s (%d) <br>\n",  $tri[$i]->username,  $tri[$i]->snormpts(), $tri[$i]->gam);
176     if ($tri[$i]->gam >= TRI_MAX_GAMES)
177       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
178     else if ($tri[$i]->gam > TRI_MIN_GAMES)
179       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
180   }
181   fclose($fphi);
182   fclose($fplo);
183
184   echo "<br><br>MON<br>\n";
185
186   if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
187     echo "Open tri_lo failed<br>";
188     exit;
189   }
190   if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
191     echo "Open tri_hi failed<br>";
192     exit;
193   }
194
195   for ($i = 0 ; $i < count($mon) ; $i++) {
196     if ($mon[$i]->gam == 0.0)
197       continue;
198     printf("%s: %s (%d) <br>\n",  $mon[$i]->username,  $mon[$i]->snormpts(), $mon[$i]->gam);
199     if ($mon[$i]->gam >= MON_MAX_GAMES)
200       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
201     else if ($mon[$i]->gam > MON_MIN_GAMES)
202       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
203   }
204   fclose($fphi);
205   fclose($fplo);
206
207   echo "<br><br>WEE<br>\n";
208   if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
209     echo "Open wee_lo failed<br>";
210     exit;
211   }
212   if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
213     echo "Open wee_hi failed<br>";
214     exit;
215   }
216
217   for ($i = 0 ; $i < count($wee) ; $i++) {
218     if ($wee[$i]->gam == 0.0)
219       continue;
220     printf("%s: %s (%d) <br>\n",  $wee[$i]->username,  $wee[$i]->snormpts(), $wee[$i]->gam);
221     if ($wee[$i]->gam >= WEE_MAX_GAMES)
222       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
223     else if ($wee[$i]->gam > WEE_MIN_GAMES)
224       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
225   }
226   fclose($fphi);
227   fclose($fplo);
228
229 }
230
231 function main_pgsql($from, $to)
232 {
233     GLOBAL $G_dbpfx;
234
235   if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
236     echo "Open explain failed<br>";
237     exit;
238   }
239     fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
240         $from, $to);
241
242     if (($bdb = BriskDB::create()) == FALSE) {
243         echo "database connection failed";
244         exit;
245     }
246     do {
247         if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
248             log_crit("statadm: begin failed");
249             break;
250         }
251         
252         $tmt_sql = sprintf("select m.code as code from %sbin5_matches as m, %sbin5_games as g WHERE m.code = g.mcode AND g.tstamp >= '%s' AND g.tstamp < '%s' GROUP BY m.code;", $G_dbpfx, $G_dbpfx, $from, $to);
253
254         // if deletable old matches exists then ...
255         if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
256             //
257             // store matches before clean them
258             //
259             $tmt_n = pg_numrows($tmt_pg);
260             // get matches
261             for ($m = 0 ; $m < $tmt_n ; $m++) {
262                 fprintf($fpexp, "<br>");
263                 $tmt_obj = pg_fetch_object($tmt_pg, $m);
264                 
265                 $usr_sql = sprintf("
266 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 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d GROUP BY u.code, u.login, m.tidx;", $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
267                 
268                if (($usr_pg  = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE ) {
269                     break;
270                 }
271                 $usr_n = pg_numrows($usr_pg);
272                 if ($usr_n != 5) {
273                     break;
274                 }
275                 for ($u = 0 ; $u < $usr_n ; $u++) {
276                     $usr_obj = pg_fetch_object($usr_pg, $u);
277                     if ($u == 0) {
278                         fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $usr_obj->first, $usr_obj->last, $usr_obj->tidx);
279                         fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
280                         }
281                     fprintf($fpexp, "<th>%s</th>", $usr_obj->login);
282                     $pts_sql = sprintf("
283 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,
284                                    $tmt_obj->code, $usr_obj->code);
285
286                     if (($pts_pg[$u]  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
287                         break;
288                     }
289                     if ($u == 0) {
290                         $num_games = pg_numrows($pts_pg[$u]);
291                     }
292                     else {
293                         if ($num_games != pg_numrows($pts_pg[$u])) {
294                             break;
295                         }
296                     }
297                 }
298                 if ($u != 5) {
299                     break;
300                 } 
301                 fprintf($fpexp, "</tr>\n");
302
303                 // LISTA DELLE VARIE PARTITE
304                 for ($g = 0 ; $g < $num_games ; $g++) {
305                     fprintf($fpexp, "<tr>");
306                     for ($u = 0 ; $u < 5 ; $u++) {
307                         $pts_obj = pg_fetch_object($pts_pg[$u], $g);
308                         fprintf($fpexp, "<td>%d</td>", $pts_obj->pts);
309                     }
310                     fprintf($fpexp, "</tr>\n");
311                 }
312
313                 // LISTA DEI TOTALI
314                 fprintf($fpexp, "<tr>");
315                 for ($u = 0 ; $u < 5 ; $u++) {
316                     $usr_obj = pg_fetch_object($usr_pg, $u);
317                     $tot_sql = sprintf("
318 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,
319                                    $tmt_obj->code, $usr_obj->code);
320                     if (($tot_pg  = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
321                         break;
322                     }
323                     $tot_obj = pg_fetch_object($tot_pg, 0);
324                     fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
325                 }
326                 fprintf($fpexp, "</tr>\n");
327                 fprintf($fpexp, "</table>\n");
328             }
329             if ($m < $tmt_n)
330                 break;
331         } // if (($tmt_pg = pg_query($bdb->dbco...
332
333         fclose($fpexp);
334         return (TRUE);
335     } while (0);
336
337     pg_query($bdb->dbconn->db(), "ROLLBACK");
338     fclose($fpexp);
339
340     return (FALSE);
341 }
342
343 // echo "QUIr\n";
344 // exit(123);
345 function main()
346 {
347     GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
348     
349     if ($pazz != $G_alarm_passwd) {
350         echo "Wrong password<br>";
351         mop_flush();
352         exit;
353     }
354     
355     $fun_name = "main_${G_dbasetype}";
356     
357     if ($ret = $fun_name($from, $to))
358         echo "Success.<br>\n";
359     else
360         echo "Failed.<br>\n";
361     
362     echo "Fine.\n";
363     mop_flush();
364 }
365
366 main();
367 ?>