minimum games number to be added to placements reduced to 0 in development environment
[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 if (BIN5_PLAYERS_N != 5)
29     define('TRI_MIN_GAMES', 0);
30 else
31     define('TRI_MIN_GAMES', 70);
32 define('TRI_MAX_GAMES', 140);
33
34 define('MON_LIMIT', (30 * 24 * 60 * 60));
35 if (BIN5_PLAYERS_N != 5)
36     define('MON_MIN_GAMES', 0);
37 else
38     define('MON_MIN_GAMES', 35);
39 define('MON_MAX_GAMES', 70);
40
41 define('WEE_LIMIT', (7 * 24 * 60 * 60));
42 if (BIN5_PLAYERS_N != 5)
43     define('WEE_MIN_GAMES', 0);
44 else
45     define('WEE_MIN_GAMES', 10);
46 define('WEE_MAX_GAMES', 35);
47
48
49 class Ptsgam {
50   var $username;
51   var $pts;
52   var $gam;
53   
54   function Ptsgam($username = "", $pts = 0, $gam = 0)
55   {
56     $this->username = $username;
57     $this->pts = $pts;
58     $this->gam = $gam;
59   }
60
61   function myclone()
62   {
63     $ret = new Ptsgam($this->username, $this->pts, $this->gam);
64
65     return ($ret);
66   }
67
68   function add($pts)
69   {
70     $this->pts += $pts;
71     $this->gam++;
72   }
73
74   function snormpts() 
75   {
76     $ret = sprintf ("%.3f", $this->normpts() * 100.0);
77     if (strchr($ret, ".")) {
78       $ret =  rtrim(rtrim($ret, "0"), ".");
79     }
80     return ($ret);
81   }
82
83   function normpts()
84   {
85     if ($this->gam == 0)
86       return (0);
87     else
88       return ($this->pts / $this->gam);
89   }
90 } // class Ptsgam {
91
92 function ptsgam_cmp($a, $b)
93 {
94   $norma = $a->normpts();
95   $normb = $b->normpts();
96     
97   if ($norma == $normb) {
98     if ($a->gam == $b->gam)
99       return (0);
100     else 
101       return ($a->gam < $b->gam ? 1 : -1);
102   }
103   else
104     return (($norma < $normb) ? 1 : -1);
105 }
106
107 /* types of placing based on delta time */
108 define('TY_DTIME_TRI', 0);
109 define('TY_DTIME_MON', 1);
110 define('TY_DTIME_WEE', 2);
111
112 /* subtypes of placing based on number of played games */
113 define('SUBTY_FREQ_LO', 0);
114 define('SUBTY_FREQ_HI', 1);
115
116
117 function placings_show($user) 
118 {
119   $mtime = placing_time();
120   $tm = placing_date($mtime);
121   $ret = sprintf("<div style='padding: auto;'><h2><b>CLASSIFICHE</b></h2>(aggiornate alle ore %s del %s)<table class='placings'>", $tm[0], $tm[1]);
122
123   $tmwee = placing_date($mtime - WEE_LIMIT + (3600));
124   $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) );
125   $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) );
126
127   $tmmon = placing_date($mtime - MON_LIMIT + (3600));
128   $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) );
129   $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) );
130
131   $tmtri = placing_date($mtime - TRI_LIMIT + (3600));
132   $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)); 
133   $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)); 
134
135
136   $ret .= sprintf("</table></div>");
137   return ($ret);
138 }
139
140 function placing_time_file()
141 {
142   if (($fp = @fopen(LEGAL_PATH."/class_wee_lo.log", 'r')) == FALSE) {
143     return (FALSE);
144   }
145   $st = fstat($fp);
146   fclose($fp);
147
148   return ( $st['mtime'] );
149 }
150
151 function placing_time_pgsql()
152 {    
153     GLOBAL $G_dbpfx;
154     
155     if (($bdb = BriskDB::create()) == FALSE) {
156         log_crit("placing: database connection failed");
157         return (FALSE);
158     }
159     
160     $mti_sql = sprintf("SELECT CAST(EXTRACT(EPOCH FROM mtime) AS INTEGER) as mtime 
161                         FROM %sbin5_places_mtime WHERE code = 0;", $G_dbpfx);
162
163     if (($mti_pg  = pg_query($bdb->dbconn->db(), $mti_sql)) == FALSE || pg_numrows($mti_pg) == 0) {
164         // no point found, abort
165         log_crit("placing: get placing mtime failed [$mti_sql]");
166         return (FALSE);
167     }
168     
169     $mti_pg = pg_fetch_object($mti_pg, 0);
170
171     return ($mti_pg->mtime);
172 }
173
174 function placing_time()
175 {
176     GLOBAL $G_dbasetype;
177     
178     $fun_name = "placing_time_${G_dbasetype}";
179     
180     return ($fun_name());
181 }
182
183 function placing_date($mtime)
184 {
185   return array( date('G:i', $mtime), date('j/n/y', $mtime) );
186 }
187   
188
189 function placing_show_file($user, $ty, $subty) 
190 {
191   $tail = FALSE;
192
193   $suff = "";
194   switch($ty) {
195   case TY_DTIME_TRI:
196       $suff = "tri_";
197       break;
198   case TY_DTIME_MON:
199       $suff = "mon_";
200       break;
201   case TY_DTIME_WEE:
202       $suff = "wee_";
203       break;
204   }
205   
206   switch($subty) {
207   case TY_FREQ_LO:
208       $suff .= "lo";
209       break;
210   case TY_FREQ_HI:
211       $suff .= "hi";
212       break;
213   }
214   
215   if (($fp = @fopen(LEGAL_PATH."/class_".$suff.".log", 'r')) == FALSE) {
216     return (FALSE);
217   }
218   
219   // MLANG
220   $ret = sprintf("<table class='placing'><tr><th>Pos.</th><th>Utente</th><th>Score</th><th>(Punti/Partite)</th>");
221
222   $old_normpts = 1000000000;
223   $old_gam = -1;
224   for ($i = 0 ; !feof($fp) ; $i++) {
225     $bf = fgets($fp, 4096);
226     $ar = csplitter($bf, '|');
227     
228     $pg = new Ptsgam($ar[0], $ar[1], $ar[2]);
229
230     if ($pg->username == "")
231       continue;
232
233     if ($pg->normpts() == $old_normpts && $pg->gam == $old_gam)
234        $i--;
235
236     if ($i < TOP_NUM) {
237       $ret .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1, 
238                       ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
239     }
240     if ($user != FALSE) {
241       if (strcasecmp($pg->username, $user->name) == 0 && $i >= TOP_NUM) {
242         $tail = sprintf("<tr><td colspan=4 style='text-align: center'> . . . . . . . . . . </td></tr>");
243         $tail .= sprintf("<tr><td>%d</td><td>%s%s%s</td><td>%s</td><td>(%d/%d)</td></tr>", $i+1,
244                          ($pg->username == $user->name ? "<b>" : ""), xcape($pg->username), ($pg->username == $user->name ? "</b>" : ""), $pg->snormpts(), $pg->pts, $pg->gam);
245       }
246     }
247     $old_normpts = $pg->normpts();
248     $old_gam = $pg->gam;
249   }
250
251   if ($tail != FALSE) {
252     $ret .= $tail;
253   }
254   $ret .= "</table>"; 
255   
256   fclose($fp);
257   
258   return ($ret);
259 }
260 //
261 //
262     
263 function placing_show_pgsql($user, $ty, $subty) 
264 {
265     GLOBAL $G_dbpfx;
266     
267     if (($bdb = BriskDB::create()) == FALSE) {
268         log_crit("placing: database connection failed");
269         return ("");
270     }
271       
272     if ($user != FALSE) {
273         $pla_sql = sprintf("SELECT * from %sbin5_places where type = %d AND (rank <= %d OR ucode = '%s');",
274                            $G_dbpfx, ($ty * 2) + $subty, TOP_NUM, escsql($user->code));
275     }
276     else {
277         $pla_sql = sprintf("SELECT * from %sbin5_places where type = %d AND rank <= %d;",
278                            $G_dbpfx, ($ty * 2) + $subty, TOP_NUM);
279     }
280     
281     if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) {
282         // no point found, abort
283         log_crit("placing: get placing list failed [$pla_sql]");
284         return ("");
285     }
286
287     // MLANG
288     $ret = sprintf("<table class='placing'><tr><th>Pos.</th><th>Utente</th><th>Score</th><th>(Punti/Partite)</th>");
289
290     for ($i = 0 ; $i < pg_numrows($pla_pg) ; $i++) {
291         $pla_obj = pg_fetch_object($pla_pg,$i);
292         
293         $ein = "";
294         $eou = "";
295         if ($user != FALSE) {
296             if ($user->code == $pla_obj->ucode) {
297                 $ein = "<b>";
298                 $eou = "</b>";
299             }
300         }
301         
302         /* when the user is far from the top-ten we place a ... separator before it */
303         if ($pla_obj->rank > TOP_NUM) {
304             $ret .= sprintf("<tr><td colspan=4 style='text-align: center'> . . . . . . . . . . </td></tr>");
305         }        
306         $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, 
307                         $ein, xcape($pla_obj->login), $eou,
308                         $ein, $pla_obj->score, $eou,
309                         $ein, $pla_obj->pts, $pla_obj->games, $eou);
310     }
311     $ret .= "</table>"; 
312   
313     return ($ret);
314 }
315
316 function placing_show($user, $ty, $subty) 
317 {
318     GLOBAL $G_dbasetype;
319
320     $fun_name = "placing_show_${G_dbasetype}";
321     
322     return ($fun_name($user, $ty, $subty));
323 }
324
325 ?>