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 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
33 ini_set("max_execution_time", "240");
35 require_once("../Obj/brisk.phh");
36 require_once("../Obj/user.phh");
37 require_once("../Obj/auth.phh");
38 require_once("../Obj/dbase_${G_dbasetype}.phh");
39 require_once("Obj/briskin5.phh");
40 require_once("Obj/placing.phh");
42 function main_file($curtime)
44 GLOBAL $G_alarm_passwd;
49 if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
50 echo "Open data file error";
55 if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
56 $skip = intval(fgets($fp_start));
58 fseek($fp, $skip, SEEK_SET);
62 if (($bdb = BriskDB::create()) == FALSE) {
63 echo "database connection failed";
69 for ($i = 0 ; $i < $bdb->count() ; $i++) {
70 $login = $bdb->getlogin_byidx($i);
71 $tri[$i] = new Ptsgam($login);
72 $mon[$i] = new Ptsgam($login);
73 $wee[$i] = new Ptsgam($login);
76 // recalculate all the placings
77 // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
80 $bf = fgets($fp, 4096);
81 $ar = csplitter($bf, '|');
82 // if not auth table, continue
87 if ($ar[7] >= TABLES_AUTH_N)
89 // echo $p++." ".$ar[5]."<br>";
90 // if not FINISH_GAME line, continue
91 if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
94 // if to much old points, continue
95 if ($ar[0] < $curtime - TRI_LIMIT) {
96 if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'w')) != FALSE) {
98 fwrite($fp_start, sprintf("%d\n", $curpos));
104 // echo $p++." ".BIN5_PLAYERS_N."<br>";
108 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
109 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
110 if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
111 $mult = abs($ar[10+($i*2)]);
121 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
122 // echo $p." i) ".$i."<br>";
123 $username = $ar[9+($i*2)];
124 if (($item = $bdb->getitem_bylogin($username, &$id)) == FALSE) {
125 echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
129 // echo $item->login." id)".$id." ".$ar[10+($i*2)]." mult: ".$mult."<br>";
130 $tri[$id]->add($ar[10+($i*2)] / $mult);
131 if ($ar[0] >= $curtime - MON_LIMIT)
132 $mon[$id]->add($ar[10+($i*2)] / $mult);
133 if ($ar[0] >= $curtime - WEE_LIMIT)
134 $wee[$id]->add($ar[10+($i*2)] / $mult);
136 // $p++; echo $p++."<br>";
140 usort($tri, ptsgam_cmp);
141 usort($mon, ptsgam_cmp);
142 usort($wee, ptsgam_cmp);
144 echo "<br><br>TRI<br>\n";
146 if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
147 echo "Open tri_lo failed<br>";
150 if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
151 echo "Open tri_hi failed<br>";
155 for ($i = 0 ; $i < count($tri) ; $i++) {
156 if ($tri[$i]->gam == 0.0)
158 printf("%s: %s (%d) <br>\n", $tri[$i]->username, $tri[$i]->snormpts(), $tri[$i]->gam);
159 if ($tri[$i]->gam >= TRI_MAX_GAMES)
160 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
161 else if ($tri[$i]->gam > TRI_MIN_GAMES)
162 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
167 echo "<br><br>MON<br>\n";
169 if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
170 echo "Open tri_lo failed<br>";
173 if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
174 echo "Open tri_hi failed<br>";
178 for ($i = 0 ; $i < count($mon) ; $i++) {
179 if ($mon[$i]->gam == 0.0)
181 printf("%s: %s (%d) <br>\n", $mon[$i]->username, $mon[$i]->snormpts(), $mon[$i]->gam);
182 if ($mon[$i]->gam >= MON_MAX_GAMES)
183 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
184 else if ($mon[$i]->gam > MON_MIN_GAMES)
185 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
190 echo "<br><br>WEE<br>\n";
191 if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
192 echo "Open wee_lo failed<br>";
195 if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
196 echo "Open wee_hi failed<br>";
200 for ($i = 0 ; $i < count($wee) ; $i++) {
201 if ($wee[$i]->gam == 0.0)
203 printf("%s: %s (%d) <br>\n", $wee[$i]->username, $wee[$i]->snormpts(), $wee[$i]->gam);
204 if ($wee[$i]->gam >= WEE_MAX_GAMES)
205 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
206 else if ($wee[$i]->gam > WEE_MIN_GAMES)
207 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
214 function main_pgsql($from, $to)
218 if (($fpexp = @fopen(LEGAL_PATH."/explain.log", 'w')) == FALSE) {
219 echo "Open explain failed<br>";
222 fprintf($fpexp, "<h2>Minuta delle partite dal (%s) al (%s)</h2>",
225 if (($bdb = BriskDB::create()) == FALSE) {
226 echo "database connection failed";
230 if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
231 log_crit("statadm: begin failed");
235 $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);
237 // if deletable old matches exists then ...
238 if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
240 // store matches before clean them
242 $tmt_n = pg_numrows($tmt_pg);
244 for ($m = 0 ; $m < $tmt_n ; $m++) {
245 fprintf($fpexp, "<br>");
246 $tmt_obj = pg_fetch_object($tmt_pg, $m);
249 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);
251 if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE ) {
254 $usr_n = pg_numrows($usr_pg);
258 for ($u = 0 ; $u < $usr_n ; $u++) {
259 $usr_obj = pg_fetch_object($usr_pg, $u);
261 fprintf($fpexp, "<h3>Codice: %d (%s - %s), Tavolo: %s</h3>\n", $tmt_obj->code, $usr_obj->first, $usr_obj->last, $usr_obj->tidx);
262 fprintf($fpexp, "<table align='center' class='placing'><tr>\n");
264 fprintf($fpexp, "<th>%s</th>", $usr_obj->login);
266 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,
267 $tmt_obj->code, $usr_obj->code);
269 if (($pts_pg[$u] = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
273 $num_games = pg_numrows($pts_pg[$u]);
276 if ($num_games != pg_numrows($pts_pg[$u])) {
284 fprintf($fpexp, "</tr>\n");
286 // LISTA DELLE VARIE PARTITE
287 for ($g = 0 ; $g < $num_games ; $g++) {
288 fprintf($fpexp, "<tr>");
289 for ($u = 0 ; $u < 5 ; $u++) {
290 $pts_obj = pg_fetch_object($pts_pg[$u], $g);
291 fprintf($fpexp, "<td>%d</td>", $pts_obj->pts);
293 fprintf($fpexp, "</tr>\n");
297 fprintf($fpexp, "<tr>");
298 for ($u = 0 ; $u < 5 ; $u++) {
299 $usr_obj = pg_fetch_object($usr_pg, $u);
301 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,
302 $tmt_obj->code, $usr_obj->code);
303 if (($tot_pg = pg_query($bdb->dbconn->db(), $tot_sql)) == FALSE ) {
306 $tot_obj = pg_fetch_object($tot_pg, 0);
307 fprintf($fpexp, "<th>%d</th>", $tot_obj->pts);
309 fprintf($fpexp, "</tr>\n");
310 fprintf($fpexp, "</table>\n");
314 } // if (($tmt_pg = pg_query($bdb->dbco...
320 pg_query($bdb->dbconn->db(), "ROLLBACK");
330 GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz, $from, $to;
332 if ($pazz != $G_alarm_passwd) {
333 echo "Wrong password<br>";
338 $fun_name = "main_${G_dbasetype}";
340 if ($ret = $fun_name($from, $to))
341 echo "Success.<br>\n";
343 echo "Failed.<br>\n";