.start file to skip too much old lines
[brisk.git] / web / briskin5 / statadm.php
1 <?php
2 /*
3  *  brisk - statadm.php
4  *
5  *  Copyright (C) 2009 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  * $Id$
24  *
25  */
26
27 /*
28   line example:
29 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
30
31 TODO:
32    - update the STAT file with differential
33    - recalculate points
34
35    
36 */
37
38 ini_set("max_execution_time",  "120");
39
40 require_once("../Obj/brisk.phh");
41 require_once("../Obj/auth.phh");
42 require_once("Obj/briskin5.phh");
43 require_once("Obj/placing.phh");
44
45 function main()
46 {
47   GLOBAL $pazz, $G_alarm_passwd;
48   $tri = array();
49   $mon = array();
50   $wee = array();
51   
52   echo "inizio<br>";
53   fflush();
54   if ($pazz != $G_alarm_passwd)
55     exit;
56
57   if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
58     echo "Open data file error";
59     exit;
60   }
61   echo "prima<br>";
62  
63   if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'r')) != FALSE) {
64     $skip = intval(fgets($fp_start));
65     if ($skip > 0)
66       fseek($fp, $skip, SEEK_SET);
67     fclose($fp_start);
68   }
69
70   $userdb = new LoginDB();
71
72   for ($i = 0 ; $i < $userdb->count() ; $i++) {
73     $login = $userdb->getlogin_byidx($i);
74     $tri[$i] = new Ptsgam($login);
75     $mon[$i] = new Ptsgam($login);
76     $wee[$i] = new Ptsgam($login);
77   }
78
79   // recalculate all the placings
80   $curtime = time();
81   // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
82   while (!feof($fp)) {
83     $p = 0;
84     $bf = fgets($fp, 4096);
85     $ar = csplitter($bf, '|');
86     // if not auth table, continue
87     if (count($ar) < 15)
88       continue;
89     
90     // echo $p++."<br>";
91     if ($ar[7] >= TABLES_AUTH_N)
92       continue;
93     // echo $p++." ".$ar[5]."<br>";
94     // if not FINISH_GAME line, continue
95     if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
96       continue;
97     // echo $p++."<br>";
98     // if to much old points, continue
99     if ($ar[0] < $curtime - TRI_LIMIT) {
100       if (($fp_start = @fopen(LEGAL_PATH."/points.start", 'w')) != FALSE) {
101         $curpos = ftell($fp);
102         fwrite($fp_start, sprintf("%d\n", $curpos));
103         fclose($fp_start);
104       }
105       
106       continue;
107     }
108     // echo $p++." ".BRISKIN5_PLAYERS_N."<br>";
109     
110     $found = FALSE;
111     $mult = 1;
112     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
113       for ($e = $i + 1 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
114         if ($ar[10+($i*2)] == $ar[10+($e*2)]) {
115           $mult = abs($ar[10+($i*2)]);
116           $found = TRUE;
117         }
118       }
119       if ($found)
120         break;
121     }
122
123     if ($mult == 0)
124        continue;
125     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
126       // echo $p." i) ".$i."<br>";
127       $username = $ar[9+($i*2)];
128       if (($item = $userdb->getitem_bylogin($username, &$id)) == FALSE) {
129         echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
130         continue;
131       }
132       
133       // echo $item->login." id)".$id."  ".$ar[10+($i*2)]." mult: ".$mult."<br>";
134       $tri[$id]->add($ar[10+($i*2)] / $mult);
135       if ($ar[0] >= $curtime - MON_LIMIT) 
136         $mon[$id]->add($ar[10+($i*2)] / $mult);
137       if ($ar[0] >= $curtime - WEE_LIMIT) 
138         $wee[$id]->add($ar[10+($i*2)] / $mult);
139     }
140     // $p++; echo $p++."<br>";
141   }
142   fclose($fp);
143   
144   usort($tri, ptsgam_cmp);
145   usort($mon, ptsgam_cmp);
146   usort($wee, ptsgam_cmp);
147   
148   echo "<br><br>TRI<br>\n";
149
150   if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
151     echo "Open tri_lo failed<br>";
152     exit;
153   }
154   if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
155     echo "Open tri_hi failed<br>";
156     exit;
157   }
158
159   for ($i = 0 ; $i < count($tri) ; $i++) {
160     if ($tri[$i]->gam == 0.0)
161       continue;
162     printf("%s: %s (%d) <br>\n",  $tri[$i]->username,  $tri[$i]->snormpts(), $tri[$i]->gam);
163     if ($tri[$i]->gam >= TRI_MAX_GAMES) 
164       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
165     else if ($tri[$i]->gam > TRI_MIN_GAMES) 
166       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
167   }
168   fclose($fphi);
169   fclose($fplo);
170
171   echo "<br><br>MON<br>\n";
172
173   if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
174     echo "Open tri_lo failed<br>";
175     exit;
176   }
177   if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
178     echo "Open tri_hi failed<br>";
179     exit;
180   }
181
182   for ($i = 0 ; $i < count($mon) ; $i++) {
183     if ($mon[$i]->gam == 0.0)
184       continue;
185     printf("%s: %s (%d) <br>\n",  $mon[$i]->username,  $mon[$i]->snormpts(), $mon[$i]->gam);
186     if ($mon[$i]->gam >= MON_MAX_GAMES) 
187       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
188     else if ($mon[$i]->gam > MON_MIN_GAMES) 
189       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
190   }
191   fclose($fphi);
192   fclose($fplo);
193
194   echo "<br><br>WEE<br>\n";
195   if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
196     echo "Open wee_lo failed<br>";
197     exit;
198   }
199   if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
200     echo "Open wee_hi failed<br>";
201     exit;
202   }
203
204   for ($i = 0 ; $i < count($wee) ; $i++) {
205     if ($wee[$i]->gam == 0.0) 
206       continue;
207     printf("%s: %s (%d) <br>\n",  $wee[$i]->username,  $wee[$i]->snormpts(), $wee[$i]->gam);
208     if ($wee[$i]->gam >= WEE_MAX_GAMES) 
209       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
210     else if ($wee[$i]->gam > WEE_MIN_GAMES) 
211       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
212   }
213   fclose($fphi);
214   fclose($fplo);
215
216
217   echo "FINITO\n";
218 }
219
220 main();
221 ?>