5 * Copyright (C) 2009-2012 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
27 wget -O daily.txt dodo.birds.van/brisk/briskin5/stat-day.php?pazz=yourpasswd&from=1000&to=1380308086
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))"
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))"
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')
50 ini_set("max_execution_time", "240");
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");
59 function main_file($curtime)
61 GLOBAL $G_lang, $G_alarm_passwd;
66 if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
67 echo "Open data file error";
72 if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
73 $skip = intval(fgets($fp_start));
75 fseek($fp, $skip, SEEK_SET);
79 if (($bdb = BriskDB::create()) == FALSE) {
80 echo "database connection failed";
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);
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|
97 $bf = fgets($fp, 4096);
98 $ar = csplitter($bf, '|');
99 // if not auth table, continue
104 if ($ar[7] >= TABLES_AUTH_N)
106 // echo $p++." ".$ar[5]."<br>";
107 // if not FINISH_GAME line, continue
108 if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
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));
121 // echo $p++." ".BIN5_PLAYERS_N."<br>";
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)]);
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>";
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);
153 // $p++; echo $p++."<br>";
157 usort($tri, ptsgam_cmp);
158 usort($mon, ptsgam_cmp);
159 usort($wee, ptsgam_cmp);
161 echo "<br><br>TRI<br>\n";
163 if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
164 echo "Open tri_lo failed<br>";
167 if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
168 echo "Open tri_hi failed<br>";
172 for ($i = 0 ; $i < count($tri) ; $i++) {
173 if ($tri[$i]->gam == 0.0)
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));
184 echo "<br><br>MON<br>\n";
186 if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
187 echo "Open tri_lo failed<br>";
190 if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
191 echo "Open tri_hi failed<br>";
195 for ($i = 0 ; $i < count($mon) ; $i++) {
196 if ($mon[$i]->gam == 0.0)
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));
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>";
212 if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
213 echo "Open wee_hi failed<br>";
217 for ($i = 0 ; $i < count($wee) ; $i++) {
218 if ($wee[$i]->gam == 0.0)
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));
231 function main_pgsql($from, $to)
233 GLOBAL $G_lang, $G_dbpfx, $mlang_stat_day;
239 if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
240 log_crit("stat-day: open explain failed");
243 fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
246 if (($bdb = BriskDB::create()) == FALSE) {
247 log_crit("stat-day: database connection failed");
251 if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
252 log_crit("stat-day: begin failed");
256 // retrieve list of active tournaments
257 $trn_sql = sprintf("SELECT * FROM %sbin5_tournaments WHERE active = 1;", $G_dbpfx);
258 if (($trn_pg = pg_query($bdb->dbconn->db(), $trn_sql)) == FALSE) {
259 log_crit("stat-day: select from tournaments failed");
263 $trn_n = pg_numrows($trn_pg);
264 printf("Number of tournaments: %d\n", $trn_n);
266 for ($t = 0 ; $t < $trn_n ; $t++) {
267 $trn_obj = pg_fetch_object($trn_pg, $t);
269 $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 t.code = %d AND g.tstamp >= '%s' AND g.tstamp < '%s' GROUP BY m.code;",
270 $G_dbpfx, $G_dbpfx, $G_dbpfx, $trn_obj->code, $from, $to);
272 // if deletable old matches exists then ...
273 if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) == FALSE) {
274 log_crit("stat-day: select from matches failed");
279 // store matches before clean them
281 $tmt_n = pg_numrows($tmt_pg);
286 if (!isset($mlang_stat_day[$trn_obj->name][$G_lang])) {
287 log_crit("stat-day: tournament name not found in array");
290 printf("[Tournament [%s]], number of matches: %d\n", $mlang_stat_day[$trn_obj->name][$G_lang], $tmt_n);
291 fprintf($fpexp, "<h3>%s</h3>", $mlang_stat_day[$trn_obj->name][$G_lang]);
293 for ($m = 0 ; $m < $tmt_n ; $m++) {
294 fprintf($fpexp, "<br>");
295 $tmt_obj = pg_fetch_object($tmt_pg, $m);
298 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);
300 if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE ) {
303 $usr_n = pg_numrows($usr_pg);
304 if ($usr_n != BIN5_PLAYERS_N) {
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 ) {
315 for ($u = 0 ; $u < $usr_n ; $u++) {
316 $usr_obj[$u] = pg_fetch_object($usr_pg, $u);
318 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);
319 fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
321 fprintf($fpexp, "<th>%s</th>", $usr_obj[$u]->login);
322 $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,
323 $tmt_obj->code, $usr_obj[$u]->code);
325 // points of the match for each user
326 if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
330 $num_games = pg_numrows($pts_pg[$u]);
333 if ($num_games != pg_numrows($pts_pg[$u])) {
338 if ($u != BIN5_PLAYERS_N) {
341 fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
343 // LISTA DELLE VARIE PARTITE
344 for ($g = 0 ; $g < $num_games ; $g++) {
345 $gam_obj = pg_fetch_object($gam_pg, $g);
346 fprintf($fpexp, "<tr>");
347 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
348 $pts_obj = pg_fetch_object($pts_pg[$u], $g);
349 fprintf($fpexp, "<th>%d</th>", $pts_obj->pts);
351 fprintf($fpexp, "<td>%s</td><td>%s</td>", $usr_obj[$gam_obj->mazzo]->login,
352 xcape( game_description($gam_obj->act, 'plain', $gam_obj->mult,
353 $gam_obj->asta_win, $usr_obj[$gam_obj->asta_win]->login,
354 $gam_obj->friend, $usr_obj[$gam_obj->friend]->login,
355 $gam_obj->pnt, $gam_obj->asta_pnt) )
357 fprintf($fpexp, "</tr>\n");
361 fprintf($fpexp, "<tr>");
362 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
364 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,
365 $tmt_obj->code, $usr_obj[$u]->code);
366 if (($tot_pg = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
369 $tot_obj = pg_fetch_object($tot_pg, 0);
370 fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
372 fprintf($fpexp, "</tr>\n");
373 fprintf($fpexp, "</table>\n");
384 pg_query($bdb->dbconn->db(), "ROLLBACK");
386 if ($fpexp != FALSE) {
397 GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
399 if ($pazz != $G_alarm_passwd) {
400 echo "Wrong password<br>";
405 $fun_name = "main_${G_dbasetype}";
407 if ($ret = $fun_name($from, $to))
408 echo "Success.<br>\n";
410 echo "Failed.<br>\n";