71cbca17bdf5dab758b8c4442d8967d8302c17bb
[brisk.git] / web / briskin5 / Obj / placing.phh
1 <?php
2 /*
3  *  brisk - placing.phh
4  *
5  *  Copyright (C) 2009-2012 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 } // class Ptsgam {
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 /* types of placing based on delta time */
99 define(TY_DTIME_TRI, 0);
100 define(TY_DTIME_MON, 1);
101 define(TY_DTIME_WEE, 2);
102
103 /* subtypes of placing based on number of played games */
104 define(SUBTY_FREQ_LO, 0);
105 define(SUBTY_FREQ_HI, 1);
106
107
108 function placings_show($user) 
109 {
110   $mtime = placing_time();
111   $tm = placing_date($mtime);
112   $ret = sprintf("<div style='padding: auto;'><h2><b>CLASSIFICHE</b></h2>(aggiornate alle ore %s del %s)<table class='placings'>", $tm[0], $tm[1]);
113
114   $tmwee = placing_date($mtime - WEE_LIMIT + (3600));
115   $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, TY_DTIME_WEE, SUBTY_FREQ_HI) );
116   $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, TY_DTIME_WEE, SUBTY_FREQ_LO) );
117
118   $tmmon = placing_date($mtime - MON_LIMIT + (3600));
119   $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, TY_DTIME_MON, SUBTY_FREQ_HI) );
120   $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, TY_DTIME_MON, SUBTY_FREQ_LO) );
121
122   $tmtri = placing_date($mtime - TRI_LIMIT + (3600));
123   $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, TY_DTIME_TRI, SUBTY_FREQ_HI)); 
124   $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, TY_DTIME_TRI, SUBTY_FREQ_LO)); 
125
126
127   $ret .= sprintf("</table></div>");
128   return ($ret);
129 }
130
131 function placing_time_file()
132 {
133   if (($fp = @fopen(LEGAL_PATH."/class_wee_lo.log", 'r')) == FALSE) {
134     return (FALSE);
135   }
136   $st = fstat($fp);
137   fclose($fp);
138
139   return ( $st['mtime'] );
140 }
141
142 function placing_time_pgsql()
143 {    
144     GLOBAL $G_dbpfx;
145     
146     if (($bdb = BriskDB::create()) == FALSE) {
147         log_crit("placing: database connection failed");
148         return (FALSE);
149     }
150     
151     $mti_sql = sprintf("SELECT CAST(EXTRACT(EPOCH FROM mtime) AS INTEGER) as mtime 
152                         FROM %sbin5_places_mtime WHERE code = 0;", $G_dbpfx);
153
154     if (($mti_pg  = pg_query($bdb->dbconn->db(), $mti_sql)) == FALSE || pg_numrows($mti_pg) == 0) {
155         // no point found, abort
156         log_crit("placing: get placing mtime failed [$mti_sql]");
157         return (FALSE);
158     }
159     
160     $mti_pg = pg_fetch_object($mti_pg, 0);
161
162     return ($mti_pg->mtime);
163 }
164
165 function placing_time()
166 {
167     GLOBAL $G_dbasetype;
168     
169     $fun_name = "placing_time_${G_dbasetype}";
170     
171     return ($fun_name());
172 }
173
174 function placing_date($mtime)
175 {
176   return array( date('G:i', $mtime), date('j/n/y', $mtime) );
177 }
178   
179
180 function placing_show_file($user, $ty, $subty) 
181 {
182   $tail = FALSE;
183
184   $suff = "";
185   switch($ty) {
186   case TY_DTIME_TRI:
187       $suff = "tri_";
188       break;
189   case TY_DTIME_MON:
190       $suff = "mon_";
191       break;
192   case TY_DTIME_WEE:
193       $suff = "wee_";
194       break;
195   }
196   
197   switch($subty) {
198   case TY_FREQ_LO:
199       $suff .= "lo";
200       break;
201   case TY_FREQ_HI:
202       $suff .= "hi";
203       break;
204   }
205   
206   if (($fp = @fopen(LEGAL_PATH."/class_".$suff.".log", 'r')) == FALSE) {
207     return (FALSE);
208   }
209   
210   // MLANG
211   $ret = sprintf("<table class='placing'><tr><th>Pos.</th><th>Utente</th><th>Score</th><th>(Punti/Partite)</th>");
212
213   $old_normpts = 1000000000;
214   $old_gam = -1;
215   for ($i = 0 ; !feof($fp) ; $i++) {
216     $bf = fgets($fp, 4096);
217     $ar = csplitter($bf, '|');
218     
219     $pg = new Ptsgam($ar[0], $ar[1], $ar[2]);
220
221     if ($pg->username == "")
222       continue;
223
224     if ($pg->normpts() == $old_normpts && $pg->gam == $old_gam)
225        $i--;
226
227     if ($i < TOP_NUM) {
228       $ret .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1, 
229                       ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
230     }
231     if ($user != FALSE) {
232       if (strcasecmp($pg->username, $user->name) == 0 && $i >= TOP_NUM) {
233         $tail = sprintf("<tr><td colspan=4 style='text-align: center'> . . . . . . . . . . </td></tr>");
234         $tail .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1,
235                          ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
236       }
237     }
238     $old_normpts = $pg->normpts();
239     $old_gam = $pg->gam;
240   }
241
242   if ($tail != FALSE) {
243     $ret .= $tail;
244   }
245   $ret .= "</table>"; 
246   
247   fclose($fp);
248   
249   return ($ret);
250 }
251 //
252 //
253     
254 function placing_show_pgsql($user, $ty, $subty) 
255 {
256     GLOBAL $G_dbpfx;
257     
258     if (($bdb = BriskDB::create()) == FALSE) {
259         log_crit("placing: database connection failed");
260         return ("");
261     }
262       
263     if ($user != FALSE) {
264         $pla_sql = sprintf("SELECT * from %sbin5_places where type = %d AND (rank <= %d OR ucode = '%s');",
265                            $G_dbpfx, ($ty * 2) + $subty, TOP_NUM, escsql($user->code));
266     }
267     else {
268         $pla_sql = sprintf("SELECT * from %sbin5_places where type = %d AND rank <= %d;",
269                            $G_dbpfx, ($ty * 2) + $subty, TOP_NUM);
270     }
271     
272     if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) {
273         // no point found, abort
274         log_crit("placing: get placing list failed [$pla_sql]");
275         return ("");
276     }
277
278     // MLANG
279     $ret = sprintf("<table class='placing'><tr><th>Pos.</th><th>Utente</th><th>Score</th><th>(Punti/Partite)</th>");
280
281     for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
282         $pla_obj = pg_fetch_object($pla_pg,$i);
283         
284         $ein = "";
285         $eou = "";
286         if ($user != FALSE) {
287             if ($user->code == $pla_obj->ucode) {
288                 $ein = "<b>";
289                 $eou = "</b>";
290             }
291         }
292         
293         /* when the user is far from the top-ten we place a ... separator before it */
294         if ($pla_obj->rank > TOP_NUM) {
295             $ret .= sprintf("<tr><td colspan=4 style='text-align: center'> . . . . . . . . . . </td></tr>");
296         }        
297         $ret .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s%12.3f%s</td><td>%s(%d/%d)%s</td></tr>", $pla_obj->rank, 
298                         $ein, xcape($pla_obj->login), $eou,
299                         $ein, $pla_obj->score, $eou,
300                         $ein, $pla_obj->pts, $pla_obj->games, $eou);
301     }
302     $ret .= "</table>"; 
303   
304     return ($ret);
305 }
306
307 function placing_show($user, $ty, $subty) 
308 {
309     GLOBAL $G_dbasetype;
310
311     $fun_name = "placing_show_${G_dbasetype}";
312     
313     return ($fun_name($user, $ty, $subty));
314 }
315
316 ?>