management of placements via postgresql
[brisk.git] / web / briskin5 / statadm.php
1 <?php
2 /*
3  *  brisk - statadm.php
4  *
5  *  Copyright (C) 2009-2011 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
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.
14  *
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.
22  *
23  */
24
25 /*
26   line example:
27 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
28
29 TODO:
30    - update the STAT file with differential
31    - recalculate points
32
33    
34 */
35
36 $G_base = "../";
37
38 ini_set("max_execution_time",  "240");
39
40 require_once("../Obj/brisk.phh");
41 require_once("../Obj/auth.phh");
42 require_once("../Obj/dbase_${G_dbasetype}.phh");
43 require_once("Obj/briskin5.phh");
44 require_once("Obj/placing.phh");
45
46 function main_file($curtime)
47 {
48   $tri = array();
49   $mon = array();
50   $wee = array();
51   
52   if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
53     echo "Open data file error";
54     exit;
55   }
56   echo "prima<br>";
57  
58   if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
59     $skip = intval(fgets($fp_start));
60     if ($skip > 0)
61       fseek($fp, $skip, SEEK_SET);
62     fclose($fp_start);
63   }
64
65   $bdb = new BriskDB();
66   $bdb->users_load();
67
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);
73   }
74
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|
77   while (!feof($fp)) {
78     $p = 0;
79     $bf = fgets($fp, 4096);
80     $ar = csplitter($bf, '|');
81     // if not auth table, continue
82     if (count($ar) < 15)
83       continue;
84     
85     // echo $p++."<br>";
86     if ($ar[7] >= TABLES_AUTH_N)
87       continue;
88     // echo $p++." ".$ar[5]."<br>";
89     // if not FINISH_GAME line, continue
90     if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
91       continue;
92     // echo $p++."<br>";
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) {
96         $curpos = ftell($fp);
97         fwrite($fp_start, sprintf("%d\n", $curpos));
98         fclose($fp_start);
99       }
100       
101       continue;
102     }
103     // echo $p++." ".BRISKIN5_PLAYERS_N."<br>";
104     
105     $found = FALSE;
106     $mult = 1;
107     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
108       for ($e = $i + 1 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
109         if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
110           $mult = abs($ar[10+($i*2)]);
111           $found = TRUE;
112         }
113       }
114       if ($found)
115         break;
116     }
117
118     if ($mult == 0)
119        continue;
120     for ($i = 0 ; $i < BRISKIN5_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>";
125         continue;
126       }
127       
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);
134     }
135     // $p++; echo $p++."<br>";
136   }
137   fclose($fp);
138   
139   usort($tri, ptsgam_cmp);
140   usort($mon, ptsgam_cmp);
141   usort($wee, ptsgam_cmp);
142   
143   echo "<br><br>TRI<br>\n";
144
145   if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
146     echo "Open tri_lo failed<br>";
147     exit;
148   }
149   if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
150     echo "Open tri_hi failed<br>";
151     exit;
152   }
153
154   for ($i = 0 ; $i < count($tri) ; $i++) {
155     if ($tri[$i]->gam == 0.0)
156       continue;
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));
162   }
163   fclose($fphi);
164   fclose($fplo);
165
166   echo "<br><br>MON<br>\n";
167
168   if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
169     echo "Open tri_lo failed<br>";
170     exit;
171   }
172   if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
173     echo "Open tri_hi failed<br>";
174     exit;
175   }
176
177   for ($i = 0 ; $i < count($mon) ; $i++) {
178     if ($mon[$i]->gam == 0.0)
179       continue;
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));
185   }
186   fclose($fphi);
187   fclose($fplo);
188
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>";
192     exit;
193   }
194   if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
195     echo "Open wee_hi failed<br>";
196     exit;
197   }
198
199   for ($i = 0 ; $i < count($wee) ; $i++) {
200     if ($wee[$i]->gam == 0.0) 
201       continue;
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));
207   }
208   fclose($fphi);
209   fclose($fplo);
210
211 }
212
213 function main_pgsql($curtime)
214 {
215     GLOBAL $G_dbpfx;
216
217     $bdb = new BriskDB();
218
219     $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT );
220     $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES );
221     $maxg = array( TRI_MAX_GAMES, MON_MAX_GAMES, WEE_MAX_GAMES );
222
223     do {
224         if (pg_query($bdb->dbconn->db(), "BEGIN") == FALSE) {
225             log_crit("statadm: begin failed");
226             break;
227         }
228
229         // Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
230         $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
231         if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
232             log_crit("statadm: truncate failed");
233             break;
234         }
235
236         for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
237             $old_score = array( 1000000000, 1000000000);
238             $old_gam   = array( -1, -1);
239             $rank      = array(  0,  0);
240             
241             $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
242                                 FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u 
243                                 WHERE p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND 
244                                       g.tstamp > to_timestamp(%d)
245                                 GROUP BY u.code, u.login
246                                 ORDER BY (float4(sum(p.pts)) * 100.0 ) /  float4(count(p.pts)) DESC, 
247                                          count(p.pts) DESC",
248                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $ming[$dtime]);
249
250             // log_crit("statadm: INFO: [$pla_sql]");
251
252             if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) {
253                 // no point found, abort
254                 log_crit("statadm: main placement select failed [$pla_sql]");
255                 break;
256             }
257             
258             for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
259                 $pla_obj = pg_fetch_object($pla_pg,$i);
260                 if ($pla_obj->games < $ming[$dtime])
261                     continue;
262
263                 if ($pla_obj->games < $maxg[$dtime])
264                     $subty = 0;
265                 else
266                     $subty = 1;
267                 
268                 $ty = ($dtime * 2) + $subty;
269                 
270                 if ($pla_obj->games != $old_gam[$subty] || $pla_obj->score != $old_score[$subty]) {
271                     $rank[$subty]++;
272                 }
273                 $new_sql = sprintf("INSERT INTO %sbin5_places (type, rank, ucode, login, pts, games, score)
274                                     VALUES (%d, %d, %d, '%s', %d, %d, %f);",
275                                    $G_dbpfx, $ty, $rank[$subty], $pla_obj->ucode, escsql($pla_obj->login), 
276                                    $pla_obj->points, $pla_obj->games, $pla_obj->score);
277                 if ( ! (($new_pg  = pg_query($bdb->dbconn->db(), $new_sql)) != FALSE && 
278                         pg_affected_rows($new_pg) == 1) ) {
279                     log_crit("statadm: new place insert failed: ".print_r($pla_obj, TRUE));
280                     break;                        
281                 }
282                 
283                 $old_gam[$subty]   = $pla_obj->games;
284                 $old_score[$subty] = $pla_obj->score;
285             } // for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
286             if ($i < pg_numrows($pla_pg)) {
287                 break;
288             }
289         } // for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
290         if ($dtime < count($limi)) {
291             break;
292         }
293
294         $mti_sql = sprintf("UPDATE %sbin5_places_mtime SET mtime = (to_timestamp(%d)) WHERE code = 0;",
295                            $G_dbpfx, $curtime);
296         if ( ! (($mti_pg  = pg_query($bdb->dbconn->db(), $mti_sql)) != FALSE && 
297                 pg_affected_rows($mti_pg) == 1) ) {
298             log_crit("statadm: new mtime insert failed.");
299             break;                        
300         }
301         
302         if (pg_query($bdb->dbconn->db(), "COMMIT") == FALSE) {
303             break;
304         }
305         return (TRUE);
306     } while (0);
307
308     pg_query($bdb->dbconn->db(), "ROLLBACK");
309
310     return (FALSE);
311 }
312
313 function main()
314 {
315     GLOBAL $G_dbasetype, $G_alarm_passwd, $pazz;
316     
317     echo "Inizio.<br>";
318     flush();
319     if ($pazz != $G_alarm_passwd) {
320         echo "Wrong password<br>";
321         flush();
322         exit;
323     }
324     
325     $fun_name = "main_${G_dbasetype}";
326     
327     $curtime = time();
328     if ($ret = $fun_name($curtime))
329         echo "Success.<br>\n";
330     else
331         echo "Failed.<br>\n";
332     
333     echo "Fine.\n";
334     flush();
335 }
336
337 main();
338 ?>