source point file fixed
[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 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   if ($pazz != $G_alarm_passwd)
51     exit;
52
53   if (($fp = @fopen(LEGAL_PATH."/points.log", 'r')) == FALSE) {
54     echo "Open data file error";
55     exit;
56   }
57   
58   $userdb = new LoginDB();
59
60   for ($i = 0 ; $i < $userdb->count() ; $i++) {
61     $login = $userdb->getlogin_byidx($i);
62     $tri[$i] = new Ptsgam($login);
63     $mon[$i] = new Ptsgam($login);
64     $wee[$i] = new Ptsgam($login);
65   }
66
67   // recalculate all the placings
68   $curtime = time();
69   // 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
70   while (!feof($fp)) {
71     $p = 0;
72     $bf = fgets($fp, 4096);
73     $ar = csplitter($bf, '|');
74     // if not auth table, continue
75     if (count($ar) < 15)
76       continue;
77     
78     // echo $p++."<br>";
79     if ($ar[7] >= TABLES_AUTH_N)
80       continue;
81     // echo $p++." ".$ar[5]."<br>";
82     // if not FINISH_GAME line, continue
83     if ($ar[5] != "STAT:BRISKIN5:FINISH_GAME")
84       continue;
85     // echo $p++."<br>";
86     // if to much old points, continue
87     if ($ar[0] < $curtime - TRI_LIMIT)
88       continue;
89     // echo $p++." ".BRISKIN5_PLAYERS_N."<br>";
90     
91     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
92       // echo $p." i) ".$i."<br>";
93       $username = $ar[9+($i*2)];
94       if (($item = $userdb->getitem_bylogin($username, &$id)) == FALSE) {
95         echo "WARNING: the user [".$username."] NOT EXISTS!<br>";
96         continue;
97       }
98       
99       // echo $item->login." id)".$id."  ".$ar[10+($i*2)]."<br>";
100       $tri[$id]->add($ar[10+($i*2)]);
101       if ($ar[0] >= $curtime - MON_LIMIT) 
102         $mon[$id]->add($ar[10+($i*2)]);
103       if ($ar[0] >= $curtime - WEE_LIMIT) 
104         $wee[$id]->add($ar[10+($i*2)]);
105     }
106     // $p++; echo $p++."<br>";
107   }
108   fclose($fp);
109   
110   usort($tri, ptsgam_cmp);
111   usort($mon, ptsgam_cmp);
112   usort($wee, ptsgam_cmp);
113   
114   echo "<br><br>TRI<br>";
115
116   if (($fplo = @fopen(LEGAL_PATH."/class_tri_lo.log", 'w')) == FALSE) {
117     echo "Open tri_lo failed<br>";
118     exit;
119   }
120   if (($fphi = @fopen(LEGAL_PATH."/class_tri_hi.log", 'w')) == FALSE) {
121     echo "Open tri_hi failed<br>";
122     exit;
123   }
124
125   for ($i = 0 ; $i < count($tri) ; $i++) {
126     if ($tri[$i]->gam == 0.0)
127       continue;
128     printf("%s: %s (%d) <br>",  $tri[$i]->username,  $tri[$i]->snormpts(), $tri[$i]->gam);
129     if ($tri[$i]->gam < TRI_FEW_GAMES) 
130       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
131     else
132       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($tri[$i]->username), $tri[$i]->pts, $tri[$i]->gam));
133   }
134   fclose($fphi);
135   fclose($fplo);
136
137   echo "<br><br>MON<br>";
138
139   if (($fplo = @fopen(LEGAL_PATH."/class_mon_lo.log", 'w')) == FALSE) {
140     echo "Open tri_lo failed<br>";
141     exit;
142   }
143   if (($fphi = @fopen(LEGAL_PATH."/class_mon_hi.log", 'w')) == FALSE) {
144     echo "Open tri_hi failed<br>";
145     exit;
146   }
147
148   for ($i = 0 ; $i < count($mon) ; $i++) {
149     if ($mon[$i]->gam == 0.0)
150       continue;
151     printf("%s: %s (%d) <br>",  $mon[$i]->username,  $mon[$i]->snormpts(), $mon[$i]->gam);
152     if ($mon[$i]->gam < MON_FEW_GAMES) 
153       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
154     else
155       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($mon[$i]->username), $mon[$i]->pts, $mon[$i]->gam));
156   }
157   fclose($fphi);
158   fclose($fplo);
159
160   echo "<br><br>WEE<br>";
161   if (($fplo = @fopen(LEGAL_PATH."/class_wee_lo.log", 'w')) == FALSE) {
162     echo "Open wee_lo failed<br>";
163     exit;
164   }
165   if (($fphi = @fopen(LEGAL_PATH."/class_wee_hi.log", 'w')) == FALSE) {
166     echo "Open wee_hi failed<br>";
167     exit;
168   }
169
170   for ($i = 0 ; $i < count($wee) ; $i++) {
171     if ($wee[$i]->gam == 0.0) 
172       continue;
173     printf("%s: %s (%d) <br>",  $wee[$i]->username,  $wee[$i]->snormpts(), $wee[$i]->gam);
174     if ($wee[$i]->gam < WEE_FEW_GAMES) 
175       fwrite($fplo, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
176     else
177       fwrite($fphi, sprintf("%s|%d|%d|\n", xcapelt($wee[$i]->username), $wee[$i]->pts, $wee[$i]->gam));
178   }
179   fclose($fphi);
180   fclose($fplo);
181
182
183   echo "FINITO";
184 }
185
186 main();
187 ?>