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