5 * Copyright (C) 2009-2011 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/auth.phh");
37 require_once("../Obj/dbase_${G_dbasetype}.phh");
38 require_once("Obj/briskin5.phh");
39 require_once("Obj/placing.phh");
41 function main_file($curtime)
43 GLOBAL $G_alarm_passwd;
48 if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
49 echo "Open data file error";
54 if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
55 $skip = intval(fgets($fp_start));
57 fseek($fp, $skip, SEEK_SET);
61 if (($bdb = BriskDB::create()) == FALSE) {
62 echo "database connection failed";
68 for ($i = 0 ; $i < $bdb->count() ; $i++) {
69 $login = $bdb->getlogin_byidx($i);
70 $tri[$i] = new Ptsgam($login);
71 $mon[$i] = new Ptsgam($login);
72 $wee[$i] = new Ptsgam($login);
75 // recalculate all the placings
76 // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
79 $bf = fgets($fp, 4096);
80 $ar = csplitter($bf, '|');
81 // if not auth table, continue
86 if ($ar[7] >= TABLES_AUTH_N)
88 // echo $p++." ".$ar[5]."<br>";
89 // if not FINISH_GAME line, continue
90 if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
93 // if to much old points, continue
94 if ($ar[0] < $curtime - TRI_LIMIT) {
95 if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'w')) != FALSE) {
97 fwrite($fp_start, sprintf("%d\n", $curpos));
103 // echo $p++." ".BIN5_PLAYERS_N."<br>";
107 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
108 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
109 if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
110 $mult = abs($ar[10+($i*2)]);
120 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
121 // echo $p." i) ".$i."<br>";
122 $username = $ar[9+($i*2)];
123 if (($item = $bdb->getitem_bylogin($username, &$id)) == FALSE) {
124 echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
128 // echo $item->login." id)".$id." ".$ar[10+($i*2)]." mult: ".$mult."<br>";
129 $tri[$id]->add($ar[10+($i*2)] / $mult);
130 if ($ar[0] >= $curtime - MON_LIMIT)
131 $mon[$id]->add($ar[10+($i*2)] / $mult);
132 if ($ar[0] >= $curtime - WEE_LIMIT)
133 $wee[$id]->add($ar[10+($i*2)] / $mult);
135 // $p++; echo $p++."<br>";
139 usort($tri, ptsgam_cmp);
140 usort($mon, ptsgam_cmp);
141 usort($wee, ptsgam_cmp);
143 echo "<br><br>TRI<br>\n";
145 if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
146 echo "Open tri_lo failed<br>";
149 if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
150 echo "Open tri_hi failed<br>";
154 for ($i = 0 ; $i < count($tri) ; $i++) {
155 if ($tri[$i]->gam == 0.0)
157 printf("%s: %s (%d) <br>\n", $tri[$i]->username, $tri[$i]->snormpts(), $tri[$i]->gam);
158 if ($tri[$i]->gam >= TRI_MAX_GAMES)
159 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
160 else if ($tri[$i]->gam > TRI_MIN_GAMES)
161 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
166 echo "<br><br>MON<br>\n";
168 if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
169 echo "Open tri_lo failed<br>";
172 if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
173 echo "Open tri_hi failed<br>";
177 for ($i = 0 ; $i < count($mon) ; $i++) {
178 if ($mon[$i]->gam == 0.0)
180 printf("%s: %s (%d) <br>\n", $mon[$i]->username, $mon[$i]->snormpts(), $mon[$i]->gam);
181 if ($mon[$i]->gam >= MON_MAX_GAMES)
182 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
183 else if ($mon[$i]->gam > MON_MIN_GAMES)
184 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
189 echo "<br><br>WEE<br>\n";
190 if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
191 echo "Open wee_lo failed<br>";
194 if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
195 echo "Open wee_hi failed<br>";
199 for ($i = 0 ; $i < count($wee) ; $i++) {
200 if ($wee[$i]->gam == 0.0)
202 printf("%s: %s (%d) <br>\n", $wee[$i]->username, $wee[$i]->snormpts(), $wee[$i]->gam);
203 if ($wee[$i]->gam >= WEE_MAX_GAMES)
204 fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
205 else if ($wee[$i]->gam > WEE_MIN_GAMES)
206 fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
213 function main_pgsql($curtime)
217 if (($bdb = BriskDB::create()) == FALSE) {
218 echo "database connection failed";
222 $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
223 $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
224 $maxg = array( TRI_MAX_GAMES, MON_MAX_GAMES, WEE_MAX_GAMES );
227 if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
228 log_crit("statadm: begin failed");
232 $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp
233 FROM %sbin5_matches as m, %sbin5_games as g
234 WHERE g.mcode = m.code GROUP BY m.code, m.ttok",
235 $G_dbpfx, $G_dbpfx, $G_dbpfx);
236 // error_log($mtc_sql, 0);
237 if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) {
238 log_crit("statadm: temporary matches table creation [$mtc_sql] failed");
242 $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE tstamp < to_timestamp(%d)",
243 $G_dbpfx, $curtime - TRI_LIMIT);
244 // error_log($tmt_sql, 0);
246 // if deletable old matches exists then ...
247 if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
249 // store matches before clean them
251 $fname = sprintf("%s/pts_archive%s.log", LEGAL_PATH, date("Ymd", $curtime));
252 if (($fp = @fopen($fname, 'a')) == FALSE) {
253 log_crit("statadm: log file [$fname] open failed");
257 $tmt_n = pg_numrows($tmt_pg);
259 for ($m = 0 ; $m < $tmt_n ; $m++) {
260 $tmt_obj = pg_fetch_object($tmt_pg, $m);
262 $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d",
263 $G_dbpfx, $tmt_obj->code);
265 // error_log($mtc_sql, 0);
266 if (($mtc_pg = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
267 log_crit("statadm: matches row select failed");
270 $mtc_obj = pg_fetch_object($mtc_pg, 0);
272 if (fwrite($fp, sprintf("M|%d|%s|%d|\n", $mtc_obj->code, xcapelt($mtc_obj->ttok), $mtc_obj->tidx)) == FALSE) {
273 log_crit("statadm: log file [$fname] write match failed");
277 // get games associated to each match
278 $gam_sql = sprintf("SELECT code, mcode, EXTRACT(epoch FROM tstamp) AS tstamp FROM %sbin5_games
279 WHERE mcode = %d ORDER BY tstamp",
280 $G_dbpfx, $mtc_obj->code);
281 if (($gam_pg = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE) {
282 log_crit("statadm: games row select failed");
286 $gam_n = pg_numrows($gam_pg);
287 for ($g = 0 ; $g < $gam_n ; $g++) {
288 $gam_obj = pg_fetch_object($gam_pg, $g);
290 if (fwrite($fp, sprintf("G|%d|%d|%d|\n", $gam_obj->mcode, $gam_obj->code, $gam_obj->tstamp)) == FALSE) {
291 log_crit("statadm: log file [$fname] write game failed");
294 $pts_sql = sprintf("SELECT * FROM %sbin5_points WHERE gcode = %d", $G_dbpfx, $gam_obj->code);
295 if (($pts_pg = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
296 log_crit("statadm: points row select [$pts_sql] failed");
299 $pts_n = pg_numrows($pts_pg);
300 for ($p = 0 ; $p < $pts_n ; $p++) {
301 $pts_obj = pg_fetch_object($pts_pg, $p);
303 if (fwrite($fp, sprintf("P|%d|%d|%d|\n", $pts_obj->gcode, $pts_obj->ucode, $pts_obj->pts)) == FALSE) {
304 log_crit("statadm: log file [$fname] write pts failed");
314 // delete match and all it's childs (games and points)
315 $del_sql = sprintf("DELETE FROM %sbin5_matches WHERE code = %d",
316 $G_dbpfx, $tmt_obj->code);
317 if (($del_pg = pg_query($bdb->dbconn->db(),$del_sql)) == FALSE || pg_affected_rows($del_pg) != 1) {
318 log_crit("statadm: matches row deletion failed");
325 } // if (($tmt_pg = pg_query($bdb->dbco...
327 // GEN: Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
328 $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
329 if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
330 log_crit("statadm: truncate failed");
334 for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
335 $old_score = array( 1000000000, 1000000000);
336 $old_gam = array( -1, -1);
337 $rank = array( 0, 0);
339 $pla_sql = sprintf("SELECT (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) as score, sum(p.pts) as points, count(p.pts) as games, u.code as ucode, u.login as login
340 FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u
341 WHERE p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND
342 g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d)
343 GROUP BY u.code, u.login
344 ORDER BY (float4(sum(p.pts)) * 100.0 ) / float4(count(p.pts)) DESC,
346 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $curtime);
348 // log_crit("statadm: INFO: [$pla_sql]");
350 if (($pla_pg = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE) {
351 // no point found, abort
352 log_crit("statadm: main placement select failed [$pla_sql]");
356 for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
357 $pla_obj = pg_fetch_object($pla_pg,$i);
358 if ($pla_obj->games < $ming[$dtime])
361 if ($pla_obj->games < $maxg[$dtime])
366 $ty = ($dtime * 2) + $subty;
368 if ($pla_obj->games != $old_gam[$subty] || $pla_obj->score != $old_score[$subty]) {
371 $new_sql = sprintf("INSERT INTO %sbin5_places (type, rank, ucode, login, pts, games, score)
372 VALUES (%d, %d, %d, '%s', %d, %d, %f);",
373 $G_dbpfx, $ty, $rank[$subty], $pla_obj->ucode, escsql($pla_obj->login),
374 $pla_obj->points, $pla_obj->games, $pla_obj->score);
375 if ( ! (($new_pg = pg_query($bdb->dbconn->db(), $new_sql)) != FALSE &&
376 pg_affected_rows($new_pg) == 1) ) {
377 log_crit("statadm: new place insert failed: ".print_r($pla_obj, TRUE));
381 $old_gam[$subty] = $pla_obj->games;
382 $old_score[$subty] = $pla_obj->score;
383 } // for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
384 if ($i < pg_numrows($pla_pg)) {
387 } // for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
388 if ($dtime < count($limi)) {
392 $mti_sql = sprintf("UPDATE %sbin5_places_mtime SET mtime = (to_timestamp(%d)) WHERE code = 0;",
394 if ( ! (($mti_pg = pg_query($bdb->dbconn->db(), $mti_sql)) != FALSE &&
395 pg_affected_rows($mti_pg) == 1) ) {
396 log_crit("statadm: new mtime insert failed.");
400 if (pg_query($bdb->dbconn->db(), "COMMIT") == FALSE) {
406 pg_query($bdb->dbconn->db(), "ROLLBACK");
413 GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz;
417 if ($pazz != $G_alarm_passwd) {
418 echo "Wrong password<br>";
423 $fun_name = "main_${G_dbasetype}";
427 $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
428 if ($ret = $fun_name($curtime))
429 echo "Success.<br>\n";
431 echo "Failed.<br>\n";