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