copyright updated and mop user status added
[brisk.git] / web / briskin5 / Obj / placing.phh
1 <?php
2 /*
3  *  brisk - placing.phh
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 define(TOP_NUM, 10);
27 define(TRI_LIMIT, (90 * 24 * 60 * 60));
28 define(TRI_MIN_GAMES, 70);
29 define(TRI_MAX_GAMES, 140);
30
31 define(MON_LIMIT, (30 * 24 * 60 * 60));
32 define(MON_MIN_GAMES, 35);
33 define(MON_MAX_GAMES, 70);
34
35 define(WEE_LIMIT, (7 * 24 * 60 * 60));
36 define(WEE_MIN_GAMES, 10);
37 define(WEE_MAX_GAMES, 35);
38
39
40 class Ptsgam {
41   var $username;
42   var $pts;
43   var $gam;
44   
45   function Ptsgam($username = "", $pts = 0, $gam = 0)
46   {
47     $this->username = $username;
48     $this->pts = $pts;
49     $this->gam = $gam;
50   }
51
52   function &myclone()
53   {
54     $ret = new Ptsgam($this->username, $this->pts, $this->gam);
55
56     return ($ret);
57   }
58
59   function add($pts)
60   {
61     $this->pts += $pts;
62     $this->gam++;
63   }
64
65   function snormpts() 
66   {
67     $ret = sprintf ("%.3f", $this->normpts() * 100.0);
68     if (strchr($ret, ".")) {
69       $ret =  rtrim(rtrim($ret, "0"), ".");
70     }
71     return ($ret);
72   }
73
74   function normpts()
75   {
76     if ($this->gam == 0)
77       return (0);
78     else
79       return ($this->pts / $this->gam);
80   }
81 }
82
83 function ptsgam_cmp($a, $b)
84 {
85   $norma = $a->normpts();
86   $normb = $b->normpts();
87     
88   if ($norma == $normb) {
89     if ($a->gam == $b->gam)
90       return (0);
91     else 
92       return ($a->gam < $b->gam ? 1 : -1);
93   }
94   else
95     return (($norma < $normb) ? 1 : -1);
96 }
97
98 function placings_show(&$user) 
99 {
100   $mtime = placing_time();
101   $tm = placing_date($mtime);
102   $ret = sprintf("<div style='padding: auto;'><h2><b>CLASSIFICHE</b></h2>(aggiornate alle ore %s del %s)<table class='placings'>", $tm[0], $tm[1]);
103
104   $tmwee = placing_date($mtime - WEE_LIMIT + (3600));
105   $ret .= sprintf("<tr><td style='background-color: #f0f0ff;'><br><b>Settimanale</b><br>dal %s al %s<br>(non meno di %d partite)<br><br>%s<br></td>", $tmwee[1], $tm[1], WEE_MAX_GAMES, placing_show($user, "wee_hi") );
106   $ret .= sprintf("<td style='background-color: #f0f0ff;'><br><b>Settimanale</b><br>dal %s al %s<br>(meno di %d partite, più di %d)<br><br>%s<br></td></tr>\n", $tmwee[1], $tm[1], WEE_MAX_GAMES, WEE_MIN_GAMES, placing_show($user, "wee_lo") );
107
108   $tmmon = placing_date($mtime - MON_LIMIT + (3600));
109   $ret .= sprintf("<tr><td style='background-color: #fffff0;'><br><b>Mensile</b><br>dal %s al %s<br>(non meno di %d partite)<br><br>%s<br></td>", $tmmon[1], $tm[1], MON_MAX_GAMES, placing_show($user, "mon_hi") );
110   $ret .= sprintf("<td style='background-color: #fffff0;'><br><b>Mensile</b><br>dal %s al %s<br>(meno di %d partite, più di %d)<br><br>%s<br></td></tr>\n", $tmmon[1], $tm[1], MON_MAX_GAMES, MON_MIN_GAMES, placing_show($user, "mon_lo") );
111
112   $tmtri = placing_date($mtime - TRI_LIMIT + (3600));
113   $ret .= sprintf("<tr><td style='background-color: #fff0f0;'><br><b>Trimestrale</b><br>dal %s al %s<br>(non meno di %d partite)<br><br>%s<br></td>", $tmtri[1], $tm[1], TRI_MAX_GAMES, placing_show($user, "tri_hi")); 
114   $ret .= sprintf("<td style='background-color: #fff0f0;'><br><b>Trimestrale</b><br>dal %s al %s<br>(meno di %d partite, più di %d)<br><br>%s<br></td></tr>", $tmtri[1], $tm[1], TRI_MAX_GAMES, TRI_MIN_GAMES, placing_show($user, "tri_lo")); 
115
116
117   $ret .= sprintf("</table></div>");
118   return ($ret);
119 }
120
121 function placing_time()
122 {
123   if (($fp = @fopen(LEGAL_PATH."/class_wee_lo.log", 'r')) == FALSE) {
124     return (FALSE);
125   }
126   $st = fstat($fp);
127   fclose($fp);
128
129   return ( $st['mtime'] );
130 }
131
132 function placing_date($mtime)
133 {
134   return array( date('G:i', $mtime), date('j/n/y', $mtime) );
135 }
136   
137
138 function placing_show(&$user, $suff) 
139 {
140   $tail = FALSE;
141   
142   if (($fp = @fopen(LEGAL_PATH."/class_".$suff.".log", 'r')) == FALSE) {
143     return (FALSE);
144   }
145   
146   // MLANG
147   $ret = sprintf("<table class='placing'><tr><th>Pos.</th><th>Utente</th><th>Score</th><th>(Punti/Partite)</th>");
148
149   $old_normpts = 1000000000;
150   $old_gam = -1;
151   for ($i = 0 ; !feof($fp) ; $i++) {
152     $bf = fgets($fp, 4096);
153     $ar = csplitter($bf, '|');
154     
155     $pg = new Ptsgam($ar[0], $ar[1], $ar[2]);
156
157     if ($pg->username == "")
158       continue;
159
160     if ($pg->normpts() == $old_normpts && $pg->gam == $old_gam)
161        $i--;
162
163     if ($i < TOP_NUM) {
164       $ret .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1, 
165                       ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
166     }
167     if ($user != FALSE) {
168       if (strcasecmp($pg->username, $user->name) == 0 && $i >= TOP_NUM) {
169         $tail = sprintf("<tr><td colspan=4 style='text-align: center'> . . . . . . . . . . </td></tr>");
170         $tail .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1,
171                          ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
172       }
173     }
174     $old_normpts = $pg->normpts();
175     $old_gam = $pg->gam;
176   }
177
178   if ($tail != FALSE) {
179     $ret .= $tail;
180   }
181   $ret .= "</table>"; 
182   
183   fclose($fp);
184   
185   return ($ret);
186 }