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 // SYNC WITH bin5_tournaments table
44 $mlang_stat_day = array(
45 'old rules: with draw'=> array( 'it' => 'Partite vecchie (con il pareggio)',
46 'en' => 'Old matches (with draw)' ),
48 'new rules: without draw' => array( 'it' => 'Partite nuove (senza pareggio)',
49 'en' => 'New matches (without draw)'),
51 'special match' => array( 'it' => 'Partite speciali',
52 'en' => 'Special matches'),
54 'info_total'=> array( 'it' => 'totali',
59 ini_set("max_execution_time", "240");
61 require_once("../Obj/brisk.phh");
62 require_once("../Obj/user.phh");
63 require_once("../Obj/auth.phh");
64 require_once("../Obj/dbase_${G_dbasetype}.phh");
65 require_once("Obj/briskin5.phh");
66 require_once("Obj/placing.phh");
68 function main_pgsql($from, $to)
70 GLOBAL $G_lang, $G_dbpfx, $mlang_stat_day;
75 // log_crit("stat-day: BEGIN");
77 if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
78 log_crit("stat-day: open explain failed");
81 fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
84 if (($bdb = BriskDB::create()) == FALSE) {
85 log_crit("stat-day: database connection failed");
89 //if ($bdb->transaction("BEGIN") == FALSE) {
90 // log_crit("stat-day: begin failed");
94 // retrieve list of active tournaments
95 $trn_sql = sprintf("SELECT * FROM %sbin5_tournaments WHERE active = 1;", $G_dbpfx);
96 if (($trn_pg = pg_query($bdb->dbconn->db(), $trn_sql)) == FALSE) {
97 log_crit("stat-day: select from tournaments failed");
101 $trn_n = pg_numrows($trn_pg);
102 printf("Number of tournaments: %d\n", $trn_n);
104 // loop on tournaments
105 for ($t = 0 ; $t < $trn_n ; $t++) {
106 // log_crit("stat-day: LOOP t");
107 $trn_obj = pg_fetch_object($trn_pg, $t);
110 SELECT m.code AS code, m.mazzo_next as minus_one_is_old
111 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_tournaments as t
112 WHERE t.code = m.tcode AND m.code = g.mcode
113 AND t.code = %d AND g.tstamp >= '%s' AND g.tstamp < '%s'
114 GROUP BY m.code, minus_one_is_old
115 ORDER BY m.code, minus_one_is_old DESC;",
116 $G_dbpfx, $G_dbpfx, $G_dbpfx, $trn_obj->code, $from, $to);
118 // if deletable old matches exists then ...
119 if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) == FALSE) {
120 log_crit("stat-day: select from matches failed");
125 // store matches before clean them
127 $tmt_n = pg_numrows($tmt_pg);
132 if (!isset($mlang_stat_day[$trn_obj->name][$G_lang])) {
133 log_crit("stat-day: tournament name not found in array");
136 printf("[Tournament [%s]], number of matches: %d\n", $mlang_stat_day[$trn_obj->name][$G_lang], $tmt_n);
137 fprintf($fpexp, "<h2 style=\"padding: 8px; background-color: pink;\">%s</h2>", $mlang_stat_day[$trn_obj->name][$G_lang]);
140 for ($m = 0 ; $m < $tmt_n ; $m++) {
141 // log_crit("stat-day: LOOP m");
142 fprintf($fpexp, "<br>");
143 $tmt_obj = pg_fetch_object($tmt_pg, $m);
145 // get users for the match m
146 if (($users = $bdb->users_get($tmt_obj->code, TRUE, ($tmt_obj->minus_one_is_old > -1))) == FALSE) {
147 log_crit(sprintf("stat_day: users_get failed %d", $tmt_obj->code));
152 SELECT g.* FROM %sbin5_tournaments AS t, %sbin5_matches AS m, %sbin5_games AS g
153 WHERE t.code = m.tcode AND m.code = g.mcode AND m.code = %d
155 $G_dbpfx, $G_dbpfx, $G_dbpfx, $tmt_obj->code);
156 if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE ) {
157 log_crit("stat-day: gam_sql failed");
161 // loop on users of the match m
162 for ($u = 0 ; $u < count($users) ; $u++) {
163 // log_crit("stat-day: LOOP u");
165 fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $users[$u]['first'], $users[$u]['last'], $users[$u]['tidx']);
166 fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
168 fprintf($fpexp, "<th>%s</th>", $users[$u]['login']);
169 // note: we are looping on users, order on them not needed
172 FROM %sbin5_games AS g, %sbin5_points AS p
173 WHERE g.code = p.gcode AND g.mcode = %d AND p.ucode = %d
176 $tmt_obj->code, $users[$u]['code']);
178 // points of the match for each user
179 if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
180 log_crit("stat-day: pts_sql failed");
184 $num_games = pg_numrows($pts_pg[$u]);
187 if ($num_games != pg_numrows($pts_pg[$u])) {
188 log_crit("stat-day: num_games != pg_numrows");
193 if ($u != BIN5_PLAYERS_N) {
194 log_crit("stat-day: u != BIN5_PLAYERS_N");
198 if ($tmt_obj->minus_one_is_old != -1) {
199 fprintf($fpexp, "<th>mazzo</th><th>descrizione</th></tr>\n");
201 // LISTA DELLE VARIE PARTITE
203 for ($g = 0 ; $g < $num_games ; $g++) {
204 $gam_obj = pg_fetch_object($gam_pg, $g);
205 fprintf($fpexp, "<tr>");
210 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
211 $pts_obj[$u] = pg_fetch_object($pts_pg[$u], $g);
213 if ($pt_min > $pts_obj[$u]->pts) {
214 $pt_min = $pts_obj[$u]->pts;
217 else if ($pt_min == $pts_obj[$u]->pts) {
221 if ($pt_max < $pts_obj[$u]->pts) {
222 $pt_max = $pts_obj[$u]->pts;
225 else if ($pt_max == $pts_obj[$u]->pts) {
239 pts == pt_max -> green
241 pts > 0 -> light green
243 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
244 $pts = $pts_obj[$u]->pts;
247 $cla_nam = 'bg_white';
248 else if ($pts == $pt_min)
250 else if ($pts == $pt_max)
251 $cla_nam = 'bg_green';
253 $cla_nam = 'bg_lired';
255 $cla_nam = 'bg_ligre';
257 fprintf($fpexp, "<%s class='%s'>%d</%s>",
258 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"),
260 pow(2,$gam_obj->mult) * $pts,
261 ($tmt_obj->minus_one_is_old == -1 ? "td" : "th"));
263 if ($tmt_obj->minus_one_is_old != -1) {
265 $rules_name = "Rules_old_rules";
266 fprintf($fpexp, "<td>%s</td><td>%s</td>", $users[$gam_obj->mazzo]['login'],
267 xcape( ${rules_name}::game_description($gam_obj->act, 'plain', $gam_obj->mult,
269 ($gam_obj->asta_win != -1 ?
270 $users[$gam_obj->asta_win]['login'] : ""),
272 ($gam_obj->friend != -1 ?
273 $users[$gam_obj->friend]['login'] : ""),
274 $gam_obj->pnt, $gam_obj->asta_pnt, $gam_obj->tourn_pts) )
277 fprintf($fpexp, "</tr>\n");
281 fprintf($fpexp, "<tr>");
282 for ($u = 0 ; $u < BIN5_PLAYERS_N ; $u++) {
283 // NOTE: this part must be revisited when we move to multiple game rules
284 // probably removing the sum and adding another nested iteration on games.
286 SELECT sum(p.pts * (2^g.mult)) AS pts
287 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
288 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
289 AND ( (u.type & (CAST (X'00ff0000' as integer))) <> (CAST (X'00800000' as integer)) )
290 AND m.code = %d AND u.code = %d",
291 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx,
292 $tmt_obj->code, $users[$u]['code']);
293 if (($tot_pg = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
296 $tot_obj = pg_fetch_object($tot_pg, 0);
297 fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
299 if ($tmt_obj->minus_one_is_old != -1) {
300 fprintf($fpexp, "<th colspan='2'>%s</th></tr>\n", $mlang_stat_day['info_total'][$G_lang]);
302 fprintf($fpexp, "</table>\n");
305 log_crit("stat-day: m < tmt_n");
310 log_crit(sprintf("stat-day: t < trn_n (%d, %d)", $t, $trn_n));
313 // if ($bdb->transaction("COMMIT") == FALSE) {
319 // if ($ret == FALSE) {
320 // $bdb->transaction("ROLLBACK");
322 if ($fpexp != FALSE) {
331 GLOBAL $G_lang, $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
333 if ($pazz != $G_alarm_passwd) {
334 echo "Wrong password<br>";
339 $fun_name = "main_${G_dbasetype}";
341 if ($ret = $fun_name($from, $to))
342 echo "Success.<br>\n";
344 echo "Failed.<br>\n";