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