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))"
41 foreach (array("HTTP_HOST", "DOCUMENT_ROOT") as $i) {
42 if (isset($_SERVER[$i])) {
47 foreach (array("pazz") as $i) {
48 if (isset($_POST[$i])) {
53 foreach (array("from", "to") as $i) {
54 if (isset($_GET[$i])) {
61 // SYNC WITH bin5_tournaments table
62 $mlang_stat_day = array(
63 'info_total'=> array( 'it' => 'totali',
68 ini_set("max_execution_time", "240");
70 require_once("../Obj/brisk.phh");
71 require_once("../Obj/user.phh");
72 require_once("../Obj/auth.phh");
73 require_once("../Obj/dbase_${G_dbasetype}.phh");
74 require_once("Obj/briskin5.phh");
75 require_once("Obj/placing.phh");
77 function main_pgsql($from, $to)
79 GLOBAL $G_lang, $G_dbpfx, $mlang_stat_day;
84 // log_crit("stat-day: BEGIN");
86 if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
87 log_crit("stat-day: open explain failed");
90 fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
93 if (($bdb = BriskDB::create()) == FALSE) {
94 log_crit("stat-day: database connection failed");
98 //if ($bdb->transaction("BEGIN") == FALSE) {
99 // log_crit("stat-day: begin failed");
103 // retrieve list of active tournaments
104 $trn_sql = sprintf("SELECT * FROM %sbin5_tournaments WHERE active = 1;", $G_dbpfx);
105 if (($trn_pg = pg_query($bdb->dbconn->db(), $trn_sql)) == FALSE) {
106 log_crit("stat-day: select from tournaments failed");
110 $trn_n = pg_numrows($trn_pg);
111 printf("Number of tournaments: %d\n", $trn_n);
113 // loop on tournaments
114 for ($t = 0 ; $t < $trn_n ; $t++) {
115 // log_crit("stat-day: LOOP t");
116 $trn_obj = pg_fetch_object($trn_pg, $t);
119 SELECT m.code AS code, m.ttype AS ttype, m.mazzo_next AS minus_one_is_old
120 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_tournaments AS t
121 WHERE t.code = m.tcode AND m.code = g.mcode
122 AND t.code = %d AND g.tstamp >= '%s' AND g.tstamp < '%s'
123 GROUP BY m.code, minus_one_is_old
124 ORDER BY m.code, minus_one_is_old DESC;",
125 $G_dbpfx, $G_dbpfx, $G_dbpfx, $trn_obj->code, $from, $to);
127 // if deletable old matches exists then ...
128 if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) == FALSE) {
129 log_crit("stat-day: select from matches failed");
134 // store matches before clean them
136 $tmt_n = pg_numrows($tmt_pg);
141 printf("[Tournament [%s]], number of matches: %d\n", rules_id2descr($trn_obj->code, $G_lang), $tmt_n);
142 fprintf($fpexp, "<h2 style=\"padding: 8px; background-color: pink;\">%s</h2>", xcape(ucfirst(rules_id2descr($trn_obj->code, $G_lang))));
145 for ($m = 0 ; $m < $tmt_n ; $m++) {
146 // log_crit("stat-day: LOOP m");
147 fprintf($fpexp, "<br>");
148 $tmt_obj = pg_fetch_object($tmt_pg, $m);
150 // get users for the match m
151 if (($users = $bdb->users_get($tmt_obj->code, TRUE, ($tmt_obj->minus_one_is_old > -1))) == FALSE) {
152 log_crit(sprintf("stat_day: users_get failed %d", $tmt_obj->code));
157 SELECT g.* FROM %sbin5_tournaments AS t, %sbin5_matches AS m, %sbin5_games AS g
158 WHERE t.code = m.tcode AND m.code = g.mcode AND m.code = %d
160 $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
161 if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE ) {
162 log_crit("stat-day: gam_sql failed");
166 // loop on users of the match m
167 for ($u = 0 ; $u < count($users) ; $u++) {
168 // log_crit("stat-day: LOOP u");
170 fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s%s</h3>\n",
171 $tmt_obj->code, $users[$u]['first'], $users[$u]['last'], $users[$u]['tidx'],
172 ($tmt_obj->ttype > 2 ? " (tavolo per apprendisti)" : ""));
173 fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
175 fprintf($fpexp, "<th>%s</th>", $users[$u]['login']);
176 // note: we are looping on users, order on them not needed
179 FROM %sbin5_games AS g, %sbin5_points AS p
180 WHERE g.code = p.gcode AND g.mcode = %d AND p.ucode = %d
183 $tmt_obj->code, $users[$u]['code']);
185 // points of the match for each user
186 if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
187 log_crit("stat-day: pts_sql failed");
191 $num_games = pg_numrows($pts_pg[$u]);
194 if ($num_games != pg_numrows($pts_pg[$u])) {
195 log_crit("stat-day: num_games != pg_numrows");
200 if ($u != BIN5_PLAYERS_N) {
201 log_crit("stat-day: u != BIN5_PLAYERS_N");
205 if ($tmt_obj->minus_one_is_old != -1) {
206 fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
208 // LISTA DELLE VARIE PARTITE
210 for ($g = 0 ; $g < $num_games ; $g++) {
211 $gam_obj = pg_fetch_object($gam_pg, $g);
212 fprintf($fpexp, "<tr>");
217 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
218 $pts_obj[$u] = pg_fetch_object($pts_pg[$u], $g);
220 if ($pt_min > $pts_obj[$u]->pts) {
221 $pt_min = $pts_obj[$u]->pts;
224 else if ($pt_min == $pts_obj[$u]->pts) {
228 if ($pt_max < $pts_obj[$u]->pts) {
229 $pt_max = $pts_obj[$u]->pts;
232 else if ($pt_max == $pts_obj[$u]->pts) {
246 pts == pt_max -> green
248 pts > 0 -> light green
250 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
251 $pts = $pts_obj[$u]->pts;
254 $cla_nam = 'bg_white';
255 else if ($pts == $pt_min)
257 else if ($pts == $pt_max)
258 $cla_nam = 'bg_green';
260 $cla_nam = 'bg_lired';
262 $cla_nam = 'bg_ligre';
264 fprintf($fpexp, "<%s class='%s'>%d</%s>",
265 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"),
267 pow(2,$gam_obj->mult) * $pts,
268 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"));
270 if ($tmt_obj->minus_one_is_old != -1) {
271 $rules_name = rules_id2name($trn_obj->code);
272 fprintf($fpexp, "<td>%s</td><td>%s</td>", $users[$gam_obj->mazzo]['login'],
273 xcape( $rules_name::game_description($gam_obj->act, 'plain', $gam_obj->mult,
275 ($gam_obj->asta_win != -1 ?
276 $users[$gam_obj->asta_win]['login'] : ""),
278 ($gam_obj->friend != -1 ?
279 $users[$gam_obj->friend]['login'] : ""),
280 $gam_obj->pnt, $gam_obj->asta_pnt, $gam_obj->tourn_pts) )
283 fprintf($fpexp, "</tr>\n");
287 fprintf($fpexp, "<tr>");
288 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
289 // NOTE: this part must be revisited when we move to multiple game rules
290 // probably removing the sum and adding another nested iteration on games.
292 SELECT sum(p.pts * (2^g.mult)) AS pts
293 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
294 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
295 AND ( (u.type & (CAST (X'00ff0000' as integer))) <> (CAST (X'00800000' as integer)) )
296 AND m.code = %d AND u.code = %d",
297 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
298 $tmt_obj->code, $users[$u]['code']);
299 if (($tot_pg = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
302 $tot_obj = pg_fetch_object($tot_pg, 0);
303 fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
305 if ($tmt_obj->minus_one_is_old != -1) {
306 fprintf($fpexp, "<th colspan='2'>%s</th></tr>\n", $mlang_stat_day['info_total'][$G_lang]);
308 fprintf($fpexp, "</table>\n");
311 log_crit("stat-day: m < tmt_n");
316 log_crit(sprintf("stat-day: t < trn_n (%d, %d)", $t, $trn_n));
319 // if ($bdb->transaction("COMMIT") == FALSE) {
325 // if ($ret == FALSE) {
326 // $bdb->transaction("ROLLBACK");
328 if ($fpexp != FALSE) {
337 GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
339 if ($pazz != $G_alarm_passwd) {
340 echo "Wrong password<br>";
345 $fun_name = "main_${G_dbasetype}";
347 if ($ret = $fun_name($from, $to))
348 echo "Success.<br>\n";
350 echo "Failed.<br>\n";