refactorization to manage old rules with a separated class
[brisk.git] / web / briskin5 / Obj / briskin5.phh
1 <?php
2 /*
3  *  brisk - briskin5.phh
4  *
5  *  Copyright (C) 2006-2013 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 define('BIN5_PLAYERS_N', 3);
25 define('BIN5_CARD_HAND', 3); // normal value 8
26 define('BIN5_MAX_PLAYERS', BIN5_PLAYERS_N);
27 // define(BIN5_SHM_MIN', (50000 * BIN5_MAX_PLAYERS));
28 define('BIN5_SHM_MIN', 32768);
29 define('BIN5_SHM_MAX', (BIN5_SHM_MIN + 1048576));
30 define('BIN5_SHM_DLT', 32768);
31 define('BIN5_PROXY_PATH', PROXY_PATH."/bin5");
32
33 define('BIN5_RULES_OLDSCHEMA', -1);
34 define('BIN5_RULES_FINISH',     0);
35 define('BIN5_RULES_ABANDON',    1);
36 define('BIN5_RULES_ALLPASS',    2);
37
38 define('BIN5_TOURNAMENT_OLDRULES', 1);
39 define('BIN5_TOURNAMENT_NO_DRAW',  2);
40
41 $mlang_bin5_bin5 = array(
42                          // br, hr, b, /b, win, fri
43                          'info_last' => array( 'it' => '%3$sultima mano%4$s',
44                                                'en' => '%3$slast hand%4$s'),
45                          'info_curr' => array( 'it' => '%3$smano corrente%4$s',
46                                                'en' => '%3$scurrent hand%4$s'),
47                          // br, hr, b, /b, win, fri
48                          'info_part' => array( 'it' => 'Ha chiamato %3$s%5$s%4$s (punti torneo %7$d), il socio era %3$s%6$s%4$s,%1$s',
49                                                'en' => 'The declarer was %3$s%5$s%4$s (tournment points %7$d), the partner was %3$s%6$s%4$s,%1$s'),
50                          // br, hr, b, /b
51                          'info_capp' => array( 'it' => 'hanno fatto %3$scappotto%4$s EBBRAVI!.%1$s',
52                                                'en' => 'and they made %3Dscapot%4$s WELL DONE!.%1$s'),
53
54                          // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
55                          'info_alea' => array( 'it' => 'dovevano fare %3$salmeno %5$d%4$s punti e ne hanno fatti %3$s%6$d%4$s: hanno %7$s.%1$s',
56                                                'en' => 'they had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: they have %7$s.%1$s'),
57                          // br, hr, b, /b, old_pnt, winornot
58                          'info_more' => array( 'it' => 'dovevano fare %3$spiù di 60%4$s punti e ne hanno fatti %3$s%5$d%4$s: hanno %6$s.%1$s',
59                                                'en' => 'they had to do %3$sover 60%4$s points and they had made %3$s%5$d%4$s: they have %3$s%6$s%4$s.%1$s'),
60                          'info_win'  => array( 'it' => 'vinto',
61                                                'en' => 'win'),
62                          'info_peer' => array( 'it' => 'pareggiato',
63                                                'en' => 'drew'),
64                          'info_lost' => array( 'it' => 'perso',
65                                                'en' => 'lost'),
66
67                          // br, hr, b, /b, win_name
68                          'info_alon' => array( 'it' => '%3$s%5$s%4$s si è chiamato in mano (punti torneo %6$d),%1$s',
69                                                'en' => '%3$s%5$s%4$s play alone against each other (tournment points %6$d),%1$s'),
70                          // br, hr, b, /b, old_asta_pnt, old_pnt, winornot
71                          'info_aleaa' => array( 'it' => 'doveva fare %3$salmeno %5$d%4$s punti e ne ha fatti %3$s%6$d%4$s: ha %3$s%7$s%4$s.%1$s',
72                                                'en' => 'he/she had to do %3$sat least %5$d%4$s points and they had made %3$s%6$d%4$s: he/she had %3$s%7$s%4$s.%1$s'),
73                          // br, hr, b, /b, old_pnt, winornot
74                          'info_morea' => array( 'it' => 'doveva fare %3$spiù di 60%4$s punti e ne ha fatti %3$s%5$d%4$s: ha %3$s%6$s%4$s.%1$s',
75                                                 'en' => 'he/she had to do %3$smore than 60%4$s points and they had made %3$s%5$d%4$s: he/she had %3$s%6$s%4$s.%1$s'),
76                          // br, hr, b, /b
77                          'info_acap' => array( 'it' => 'ha fatto %3$scappotto%4$s EBBRAVO!.%1$s',
78                                                'en' => 'and he/she made %3$scapot%4$s WELL DONE!.%1$s'),
79
80                          // br, hr, b, /b
81                          'info_omul' => array( 'it' => '%1$sLa partita valeva %3$s%5$s%4$s.%1$s',
82                                                'en' => '%1$sEN: The game was worth %3$s%5$s%4$s.%1$s' ),
83
84                          // br, hr, b, /b
85                          'info_alpa' => array( 'it' => '%1$sHanno passato %3$stutti%4$s.%1$s',
86                                                'en' => '%1$sEN: Hanno passato %3$stutti%4$s.%1$s' ),
87
88                          // br, hr, b, /b, aband-name
89                          'info_aban' => array( 'it' => ' Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.',
90                                                'en' => ' EN: Ha lasciato %3$s%5$s%4$s perché aveva al massimo %3$s2 punti%4$s.'),
91
92                          'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
93                                                'en' => '<b>%s</b> shuffled the cards, '),
94                          'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
95                                                'en' => ' it\'s <b>your</b> turn.'),
96                          'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
97                                                'en' => 'it\'s the <b>%s</b>\'s turn.'),
98                          'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
99                                                'en' => ' The game worth <b>%s</b>.' ),
100                          'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
101                                                'en' => 'It\'s <b>your</b> shuffled the cards, '),
102                          'info_match'=> array( 'it' => 'Il codice della partita è <b>%d</b>.',
103                                                'en' => 'Match code is <b>%d</b>.'),
104                          'btn_bkgame'=> array( 'it' => 'torna alla partita',
105                                                'en' => 'back to the game'),
106                          'call_wptn' => array( 'it' => '<br>con %d punti',
107                                                'en' => '<br>with %d points'),
108                          'call_ycall'=> array( 'it' => 'Chiami%s:',
109                                                'en' => 'Call%s:'),
110                          'call_call' => array( 'it' => 'Chiama %s%s:',
111                                                'en' => 'The declarer is %s%s:')
112                           );
113
114 // MLANG
115 $table_wellarr = Array( 'it' => Array ( 'Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non pu&ograve; risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.'),
116                         'en' => Array ( 'EN Benvenuto al tavolo. Se almeno tre giocatori non sbloccano l\'uscita cliccando il lucchetto, chi esce non pu&ograve; risedersi a un qualunque tavolo per '.floor(BAN_TIME/60).' minuti.') );
117
118 function multoval($mult)
119 {
120     GLOBAL $G_lang;
121
122     if ($mult == 2)
123         return ($G_lang == 'en' ? 'double' : 'doppio');
124     if ($mult == 3)
125         return ($G_lang == 'en' ? 'triple' : 'triplo');
126     else if ($mult == 4)
127         return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
128     else
129         return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
130 }
131
132 abstract class Rules {
133     var $table;
134
135     abstract function engine(&$bri, $curtime, $action, $user);
136
137     function __construct($table)
138     {
139         $this->table = $table;
140     }
141 }
142
143 class Rules_old_rules extends Rules {
144     function __construct($table)
145     {
146         parent::__construct($table);
147     }
148
149     static function asta2mult($asta_pnt)
150     {
151         if ($asta_pnt > 110)
152             return (6);
153         else if ($asta_pnt > 100)
154             return (5);
155         else if ($asta_pnt > 90)
156             return (4);
157         else if ($asta_pnt > 80)
158             return (3);
159         else if ($asta_pnt > 70)
160             return (2);
161         else
162             return (1);
163     }
164     static function s_multer($mult, $pnt)
165     {
166         return (pow(2, $mult) * static::asta2mult($pnt));
167     }
168
169     static function s_point_calc($pnt_done, $mult, $pnt_req, $is_allpoints)
170     {
171         return ($pnt_done * static::s_multer($mult, $pnt_req) * ($is_allpoints ? 2 : 1));
172     }
173
174     function multer($is_new)
175     {
176         if ($is_new) {
177             return (static::s_multer($this->table->mult, $this->table->asta_pnt));
178         }
179         else {
180             return (static::s_multer($this->table->old_mult, $this->table->old_asta_pnt));
181         }
182     }
183
184     static function game_result($asta_pnt, $pnt)
185     {
186         $sixty = (BIN5_PLAYERS_N == 3 ? 30 : 60);
187         if ($asta_pnt == 61) {
188             if ($pnt > $sixty)
189                 return (1);
190             else if ($pnt == $sixty)
191                 return (0);
192             else
193                 return (-1);
194         }
195         else {
196             if ($pnt >= $asta_pnt)
197                 return (1);
198             else
199                 return (-1);
200         }
201     }
202
203     function engine(&$bri, $curtime, $action, $user)
204     {
205         GLOBAL $G_all_points, $G_dbasetype;
206
207         $table = $this->table;
208         $pnts_sav = array();
209
210         if ($action == BIN5_RULES_ALLPASS) { // return TRUE if all correct
211             $table->old_act = $action;
212             $table->old_asta_win = -1;
213             $table->old_pnt = 0;
214             $table->mult_inc(1);
215             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
216                 $pnts_sav[$i] = 0;
217             }
218
219             $game_delta = 1;
220             // $table->game_next(1);
221             $table->game_init(&$bri->user);
222         }
223         else if ($action == BIN5_RULES_ABANDON) { // return TRUE if all correct
224             if (!($user->handpt <= 2)) {
225                 return (FALSE);
226             }
227             $table->old_act = $action;
228             log_wr(sprintf("GIOCO FINITO !!!"));
229             $table->old_asta_win = $user->table_pos;
230             $table->old_pnt = 0;
231             $table->mult_inc(1);
232
233             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
234                 $pnts_sav[$i] = 0;
235             }
236
237             // Non si cambia mazzo se si abbandona la partita
238             $game_delta = 0;
239             // $table->game_next(0);
240             $table->game_init(&$bri->user);
241         }
242         else if ($action == BIN5_RULES_FINISH) { // return TRUE if all correct
243             $table->old_act = $action;
244             do {
245                 $pro = 0;
246
247                 if ($table->asta_pnt == 60)
248                     $table->asta_pnt = 61;
249
250                 $table->old_reason = "";
251
252                 // count points for the temporary 2 teams
253                 for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
254                     $ctt = $table->card[$i]->value % 10;
255                     $own = $table->card[$i]->owner;
256                     if ($own == $table->asta_win || $own == $table->friend)
257                         $pro += $G_all_points[$ctt];
258                 }
259
260                 log_wr(sprintf("PRO: [%d]", $pro));
261
262                 // PATTA case !
263                 if (static::game_result($table->asta_pnt, $pro) == 0) {
264                     $table->points[$table->points_n % MAX_POINTS] = array();
265                     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
266                         $table->points[$table->points_n % MAX_POINTS][$i] = 0;
267                         $pnts_sav[$i] = 0;
268                     }
269                     $table->points_n++;
270                     $table->old_pnt = $pro;
271                     $table->old_asta_win = $table->asta_win;
272                     $table->mult_inc(1);
273
274                     break;
275                     }
276
277                 if (static::game_result($table->asta_pnt, $pro) == 1)
278                     $sig = 1;
279                 else
280                     $sig = -1;
281
282                 // TAG: POINTS_MANAGEMENT
283                 $table->points[$table->points_n % MAX_POINTS] = array();
284                 for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
285                     if ($i == $table->asta_win)
286                         $pnt = ($i == $table->friend ? 4 : 2);
287                     else if ($i == $table->friend)
288                         $pnt = 1;
289                     else
290                         $pnt = -1;
291
292                     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pnt));
293
294                     $pnt_sav = static::s_point_calc($pnt * $sig,           0, $table->asta_pnt, ($pro == 120));
295                     $pnt_tab = static::s_point_calc($pnt * $sig, $table->mult, $table->asta_pnt, ($pro == 120));
296
297                     $table->points[$table->points_n % MAX_POINTS][$i] = $pnt_tab;
298                     $table->total[$i] += $pnt_tab;
299                     $pnts_sav[$i] = $pnt_sav;
300                 }
301                 $table->points_n++;
302                 $table->old_pnt = $pro;
303                 $table->old_asta_win = $table->asta_win;
304                 $table->mult_set(0);
305             } while (0);
306             $game_delta = 1;
307         }
308         else {
309             return (FALSE);
310         }
311         $table->game_next($game_delta);
312
313         $plist = "$table->table_token|$user->table_orig|$table->player_n";
314         $ucodes = array();
315         $codes = "";
316         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
317             $user_cur = &$bri->user[$table->player[$i]];
318
319             /* pro db */
320             $ucodes[$i] = $user_cur->code_get();
321
322             /* pro log */
323             $plist .= '|'.xcapelt($user_cur->name).'|'.$pnts_sav[$i];
324             $codes .= '|'.xcapelt($user_cur->code_get());
325         }
326         $plist .= $codes;
327         log_legal($curtime, $user->ip, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
328
329         $table->old_asta_pnt = $table->asta_pnt;
330         // $table->old_mazzo is managed by ->game_next();
331         // $table->old_mult, $table->old_pnt, $table->old_reason and $table->old_asta_win are specific
332
333         $table->old_friend = $table->friend;
334         $table->old_tourn_pts = $table->tourn_pts;
335
336         $table->old_reason = static::game_description($action, 'html', $table->old_mult,
337                                              $table->old_asta_win,
338                                              ($table->old_asta_win != -1 ?
339                                               $bri->user[$table->player[$table->old_asta_win]]->name : ""),
340                                              $table->old_friend,
341                                              ($table->old_friend != -1 ?
342                                               $bri->user[$table->player[$table->old_friend]]->name : ""),
343                                              $table->old_pnt, $table->old_asta_pnt, $table->old_tourn_pts);
344
345
346         if ($user->table_orig < TABLES_AUTH_N) {
347             require_once("../Obj/dbase_".$G_dbasetype.".phh");
348
349             if (($bdb = BriskDB::create()) != FALSE) {
350                 $bdb->bin5_points_save($curtime, $table, $user->table_orig, $action, $ucodes, $pnts_sav);
351                 unset($bdb);
352             }
353             else {
354                 log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED");
355             }
356             log_points($user->ip, $curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist);
357         }
358
359         $table->game_init(&$bri->user);
360
361         return (TRUE);
362     }
363
364     static function game_description($act, $form, $old_mult, $win = -1, $win_name = "?1?", $fri = -1, $fri_name = "?2?",
365                               $old_pnt = -1, $old_asta_pnt = -1, $old_tourn_pts = -1)
366     {
367         GLOBAL $G_lang, $mlang_bin5_bin5;
368
369         if ($form == 'html') {
370             $tg_br = "<br>";
371             $tg_hr = "<hr>";
372             $tg_bo = "<b>";
373             $tg_bc = "</b>";
374             $win_name = xcape($win_name);
375             $fri_name = xcape($fri_name);
376         }
377         else {
378             $tg_br = " ";
379             $tg_hr = " ";
380             $tg_bo = "";
381             $tg_bc = "";
382         }
383
384         if ($act == BIN5_RULES_OLDSCHEMA) {
385             return ("");
386         }
387         else if ($act == BIN5_RULES_ALLPASS) {
388             return (sprintf($mlang_bin5_bin5['info_alpa'][$G_lang],
389                             $tg_br, $tg_hr, $tg_bo, $tg_bc));
390         }
391         else if ($act == BIN5_RULES_ABANDON) {
392             return (sprintf($mlang_bin5_bin5['info_aban'][$G_lang],
393                             $tg_br, $tg_hr, $tg_bo, $tg_bc,
394                             $win_name));
395         }
396         else {
397             $wol = static::game_result($old_asta_pnt, $old_pnt);
398
399             $noty = "";
400
401             if ($win != $fri) { // not alone case
402                 /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'pi&ugrave; di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
403
404                 $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang],
405                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
406                                  $win_name,
407                                  $fri_name,
408                                  $old_tourn_pts);
409                 if ($old_pnt == 120) {
410                     $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang],
411                                      $tg_br, $tg_hr, $tg_bo, $tg_bc );
412                 }
413                 else {
414                     if ($old_asta_pnt > 61) {
415                         $noty .= sprintf($mlang_bin5_bin5['info_alea'][$G_lang],
416                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
417                                          $old_asta_pnt, $old_pnt,
418                                          ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
419                                           ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
420                                            $mlang_bin5_bin5['info_lost'][$G_lang])));
421                     }
422                     else {
423                         $noty .= sprintf($mlang_bin5_bin5['info_more'][$G_lang],
424                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
425                                          $old_pnt,
426                                          ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
427                                           ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
428                                            $mlang_bin5_bin5['info_lost'][$G_lang])));
429                     } // else of if ($old_asta_pnt > 61) {
430                 } // else of if ($old_pnt == 120) {
431             } // if ($win != $fri) { // not alone case
432             else {
433                 $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
434                                  $tg_br, $tg_hr, $tg_bo, $tg_bc,
435                                  $win_name,
436                                  $old_tourn_pts);
437                 if ($old_pnt == 120) {
438                     $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang],
439                                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
440                 }
441                 else {
442                     if ($old_asta_pnt > 61) {
443                         $noty .= sprintf($mlang_bin5_bin5['info_aleaa'][$G_lang],
444                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
445                                          $old_asta_pnt, $old_pnt,
446                                          ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
447                                           ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
448                                            $mlang_bin5_bin5['info_lost'][$G_lang])));
449                     }
450                     else {
451                         $noty .= sprintf($mlang_bin5_bin5['info_morea'][$G_lang],
452                                          $tg_br, $tg_hr, $tg_bo, $tg_bc,
453                                          $old_pnt,
454                                          ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] :
455                                           ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] :
456                                            $mlang_bin5_bin5['info_lost'][$G_lang])));
457                     }
458                 }
459             }
460         }
461
462         $old_multer = static::s_multer($old_mult, $old_asta_pnt);
463         if ($old_multer > 1) {
464             $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang],
465                              $tg_br, $tg_hr, $tg_bo, $tg_bc,
466                              multoval($old_multer));
467         }
468         $noty .= sprintf('%2$s', $tg_br, $tg_hr);
469
470         return ($noty);
471     }
472 } // class Rules_old_rules
473
474 class Card {
475     var $value; /* 0 - 39 card value */
476     var $stat;  /* 'bunch', 'hand', 'table', 'take' */
477     var $owner; /* (table position 0-4) */
478     // var $pos;   /* Pos in hand. */
479     var $x;     /* When played the X position on the table of the owner. */
480     var $y;     /* When played the Y position on the table of the owner. */
481
482     function Card($value, $stat, $owner)
483     {
484         $this->value = $value;
485         $this->stat  = $stat; // Card stat
486         $this->owner = $owner;
487     }
488
489     function assign($stat,$owner)
490     {
491         $this->stat  = $stat; // Card stat
492         $this->owner = $owner;
493     }
494
495     function setpos($pos)
496     {
497         $this->pos   = $pos;
498     }
499
500     function play($x,$y)
501     {
502         $this->stat = 'table'; // Card stat
503         $this->x = $x;
504         $this->y = $y;
505     }
506
507     function take($newown)
508     {
509         $this->stat = 'take'; // Card stat
510         $this->owner = $newown;
511     }
512 } // end class Card
513
514 class Bin5_table extends Table {
515     var $card;       // il mazzo di carte
516     var $mazzo;      // chi e' di mazzo
517     var $gstart;
518     var $turn;
519
520     var $asta_pla;
521     var $asta_pla_n;
522     var $asta_card;
523     var $asta_pnt;
524
525     var $mult;
526     var $points;    // points array
527     var $points_n;  // number of row of points
528     var $total;
529
530     var $asta_win;          // the caller idx position at table
531     var $briscola;
532     var $tourn_pts;         // points in the caller hand
533     var $friend;            // the callee idx position at table
534
535     var $match_id;          // the id of the match on the database (-1 == just not saved)
536
537     var $old_act;           // last action that trigs the end of the game
538     var $old_mazzo;
539     var $old_reason;
540     var $old_asta_pnt;
541     var $old_mult;
542     var $old_pnt;           // points made by caller and callee
543     var $old_asta_win;      // the old caller idx position at table
544     var $old_friend;        // the old callee idx position at table
545
546     var $old_tourn_pts;     // the old tournment computed points in the hand of caller
547     var $rules;
548
549     function Bin5_table()
550     {
551     }
552
553
554     /* CREATE() NOT USED
555        function create($idx)
556        {
557        if (($thiz =& new Bin5_table()) == FALSE)
558        return (FALSE);
559
560        $thiz->create($idx);
561
562        $thiz->card      =   FALSE;
563        $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
564        $thiz->asta_pla_n=  -1;
565        $thiz->asta_card =  -1;
566        $thiz->asta_pnt  =  -1;
567        $thiz->mult      =   0;
568
569        $thiz->points    =   array( );
570        $thiz->points_n  =   0;
571        $thiz->total     =   array( 0, 0, 0, 0, 0);
572        $thiz->asta_win  =  -1;
573        $thiz->briscola  =  -1;
574        $thiz->tourn_pts =  -1;
575        $thiz->friend    =  -1;
576        $thiz->turn      =   0;
577
578        $thiz->match_id     = -1;
579
580        $thiz->old_act      = -1;
581        $thiz->old_mazzo    = -1;
582        $thiz->old_reason   = "";
583        $thiz->old_asta_pnt = -1;
584        $thiz->old_mult     = -1;
585        $thiz->old_pnt      = -1;
586        $thiz->old_asta_win = -1;
587        $thiz->old_friend   = -1;
588
589        return ($thiz);
590        }
591     */
592
593     /* CLONE() NOT USED
594        function myclone(&$from)
595        {
596        if (($thiz =& new Bin5_table()) == FALSE)
597        return (FALSE);
598
599        parent::copy($from);
600
601        $thiz->card         = $from->card;
602        $thiz->mazzo        = $from->mazzo; // REVIEW
603        $thiz->gstart       = $from->gstart;
604        $thiz->turn         = $from->turn;
605
606        $thiz->asta_pla     = $from->asta_pla;
607        $thiz->asta_pla_n   = $from->asta_pla_n;
608        $thiz->asta_card    = $from->asta_card;
609        $thiz->asta_pnt     = $from->asta_pnt;
610
611        $thiz->mult         = $from->mult;
612        $thiz->points       = $from->points;
613        $thiz->points_n     = $from->points_n;
614        $thiz->total        = $from->total;
615
616        $thiz->asta_win     = $from->asta_win;
617        $thiz->briscola     = $from->briscola;
618        $thiz->tourn_pts    = $from->tourn_pts;
619        $thiz->friend       = $from->friend;
620
621        $thiz->match_id     = $from->match_id;
622
623        $thiz->old_act      = $from->old_act;
624        $thiz->old_mazzo    = $from->old_mazzo;
625        $thiz->old_reason   = $from->old_reason;
626        $thiz->old_asta_pnt = $from->old_asta_pnt;
627        $thiz->old_mult     = $from->mult;
628        $thiz->old_pnt      = $from->old_pnt;
629        $thiz->old_asta_win = $from->old_asta_win;
630        $thiz->old_friend   = $from->old_friend;
631
632        return ($thiz);
633        }
634     */
635
636     function parentcopy(&$from)
637     {
638         parent::copy($from);
639     }
640
641     function spawn(&$from)
642     {
643         if (($thiz =& new Bin5_table()) == FALSE)
644             return (FALSE);
645
646         $thiz->parentcopy($from);
647
648         log_main("PLAYER_N - spawn.".$thiz->player_n);
649
650         $thiz->card = array();
651         $thiz->bunch_create();
652         $thiz->mazzo    = rand(0,PLAYERS_N-1);
653         $thiz->points = array();
654         $thiz->total  = array();
655         $thiz->points_n = 0;
656         $thiz->mult     = 0;
657
658         $thiz->match_id     = -1;
659
660         $thiz->old_asta_win = -1;
661         $thiz->old_reason = "";
662
663         // FIXME
664         $rules_name = "Rules_old_rules";
665         $thiz->rules = new $rules_name($thiz);
666         // players are rearranged in an dedicated array
667         $thiz->player = array();
668         for ($i = 0 ; $i < $from->player_n ; $i++)
669             $thiz->player[$i] = $i;
670
671         log_main("TABLE_OLD_WIN - spawn:".$thiz->old_asta_win);
672
673         return ($thiz);
674     }
675
676
677     //   function bunch_create_old() function AND
678     //   {
679     //     $ret = array();
680     //
681     //     for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
682     //       // for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
683     //       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
684     //     }
685     //
686     //     $oret = &$ret;
687     //     return ($oret);
688     //   }
689
690     function bunch_create()
691     {
692         $ret = array();
693
694         for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
695             $this->card[$i] = new Card($i, 'bunch', 'no_owner');
696         }
697     }
698
699     function bunch_make()
700     {
701         log_main("bunch_make start");
702         $ct = array(0,0,0,0,0);
703
704         mt_srand(make_seed());
705
706         for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--)
707             $rest[$i] = $i;
708
709         for ($i = (BIN5_CARD_HAND * BIN5_PLAYERS_N) - 1 ; $i >= 0 ; $i--) {
710             $rn = rand(0, $i);
711
712             if ($rn == 0)
713                 log_main("RND ZERO");
714
715             $id = $rest[$rn];
716
717             $owner = $i % BIN5_PLAYERS_N;
718             $this->card[$id]->assign('hand', $owner);
719
720             $rest[$rn] = $rest[$i];
721             // $pubbpos[$rn2] = $pubbpos[$i];
722         }
723         log_main("bunch_make end");
724     }
725
726     function init($userarr)
727     {
728         /* MOVED INTO SPAWN
729            $this->mazzo    =  rand(0,PLAYERS_N-1);
730            $this->points_n =  0;
731            $this->mult     =  0;
732            $this->old_asta_win  = -1;
733            $this->old_reason = "";
734         */
735         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
736             $this->total[$i] = 0;
737             $user_cur = $userarr[$this->player[$i]];
738             $user_cur->stat_set('table');
739             $user_cur->exitislock = TRUE;
740         }
741
742         log_main("table::init: ci siamo");
743     }
744
745     function game_init($userarr)
746     {
747         log_rd2("GSTART 4");
748
749         $this->gstart = ($this->mazzo+1) % BIN5_PLAYERS_N;
750         $this->bunch_make();
751
752         $this->asta_pla_n = BIN5_PLAYERS_N;
753         $this->asta_card = -1;
754         $this->asta_pnt  = 60;
755         $this->asta_win  = -1;
756         $this->briscola  = -1;
757         $this->tourn_pts = -1;
758         $this->friend    = -1;
759         $this->turn      =  0;
760
761         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
762             $this->asta_pla[$i] = TRUE;
763             $user_cur = $userarr[$this->player[$i]];
764             $user_cur->subst = 'asta';
765             $user_cur->asta_card = -2;
766             $user_cur->asta_pnt  = -1;
767             $user_cur->handpt = $this->hand_points($i);
768         }
769         log_rd2("GEND 4");
770     }
771
772     function game_next($delta)
773     {
774         $this->old_mazzo = $this->mazzo;
775         $this->mazzo  = ($this->mazzo + $delta) % BIN5_PLAYERS_N;
776     }
777
778     function mult_inc($val)
779     {
780         $this->old_mult = $this->mult;
781         $this->mult += $val;
782     }
783
784     function mult_set($val)
785     {
786         $this->old_mult = $this->mult;
787         $this->mult = $val;
788     }
789
790     function hand_points($idx)
791     {
792         GLOBAL $G_all_points;
793
794         $tot = 0;
795
796         for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
797             // for ($i = 0 ; $i < 40 ; $i++) {
798             if ($this->card[$i]->owner != $idx)
799                 continue;
800
801             $ctt = $this->card[$i]->value % 10;
802             $tot += $G_all_points[$ctt];
803         }
804
805         return ($tot);
806     }
807
808
809     function exitlock_show($userarr, $table_pos)
810     {
811         $ct = $this->exitlock_calc($userarr, $table_pos);
812
813         $ret = sprintf('exitlock_show(%d, %s);', $ct,
814                        ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
815         return ($ret);
816     }
817
818     function exitlock_calc(&$userarr, $table_pos)
819     {
820         $ct = 0;
821
822         for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {
823             if ($userarr[$this->player[$i]]->exitislock == FALSE)
824                 $ct++;
825         }
826
827         return ($ct);
828     }
829
830     function match_continue(&$bri, $user, $match_id_s)
831     {
832         $ret = FALSE;
833         $curtime = time();
834
835         do {
836             /* - verify if match_id and user are both valid to accept
837                the match_continue request - */
838             $match_id = (int)$match_id_s;
839             if ($match_id <= 0) {
840                 $msg = "questa partita non esiste";
841                 break;
842             }
843
844             if ($user->continue_get() == BIN5_USER_CONTINUE_ALREADY) {
845                 $msg = "Hai già richiesto di continuare una partita.";
846                 break;
847             }
848
849             if ($user->continue_get() == $match_id) {
850                 $msg = "Hai già richiesto di continuare questa partita.";
851                 break;
852             }
853             // retrieves users list for this match
854             $match_data = array();
855             if (($bdb = BriskDB::create()) != FALSE) {
856                 // match_order_get return FALSE for old matches
857                 $ucodes = $bdb->match_order_get($match_data, $match_id, BIN5_PLAYERS_N);
858                 unset($bdb);
859             }
860             if ($ucodes == FALSE) {
861                 $msg = "questa partita non è stata memorizzata correttamente";
862                 break;
863             }
864
865             // if current user code must be in the users list
866             if (array_search($user->code, $ucodes) === FALSE) {
867                 $msg = sprintf("Questo utente non compare nella partita che si vuole continuare [%d].", $user->code);
868                 break;
869             }
870
871             /* - the user is in the list, add the match_id to his profile
872                and check if he is the N'th to require continue - */
873
874             // set the match_id for the current user
875             $user->continue_set($match_id);
876
877             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
878                 $user_cur = &$bri->user[$this->player[$i]];
879                 if ($user_cur->continue_get() != $match_id) {
880                     break;
881                 }
882             }
883             $ret = TRUE;
884             // not all players set the continue match than we exit
885             if ($i < BIN5_PLAYERS_N) {
886                 $msg = sprintf("<b>L'utente <i>%s</i> vorrebbe continuare la partita n° %d.</b>",
887                                xcape($user->name), $match_id);
888                 break;
889             }
890
891             /* - all users decide to continue the same match, update all infos and rearrange users
892                to the right positions on the table - */
893
894             /*  reset users table order */
895             for ($i = 0 ; $i < BIN5_PLAYERS_N - 1 ; $i++) {
896                 if ($bri->user[$this->player[$i]]->code == $ucodes[$i]) {
897                     continue;
898                 }
899                 for ($e = $i + 1 ; $e < BIN5_PLAYERS_N ; $e++) {
900                     if ($bri->user[$this->player[$e]]->code == $ucodes[$i]) {
901                         $swap = $this->player[$i];
902                         $this->player[$i] = $this->player[$e];
903                         $this->player[$e] = $swap;
904                         $bri->user[$this->player[$i]]->table_pos = $i;
905                         $bri->user[$this->player[$e]]->table_pos = $e;
906                     }
907                 }
908             }
909             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
910                 fprintf(STDERR, "USERZ: [%s]\n", $bri->user[$this->player[$i]]->name);
911             }
912
913             // update database info to be aligned with current table (ttok and table_idx
914             if (($bdb = BriskDB::create()) != FALSE) {
915                 if ($bdb->match_continue($match_id, $this, $user->table_orig) == FALSE) {
916                     unset($bdb);
917                     $msg = "aggiornamento dei dati della partita fallito";
918                     break;
919                 }
920                 unset($bdb);
921             }
922
923             /* bunch and multiplier status set */
924             $this->mazzo = $match_data['mazzo_next'];
925             $this->mult  = $match_data['mult_next'];
926             $this->match_id = $match_id;
927             $this->game_init(&$bri->user);
928
929             /* reload of the page with the new layout */
930             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
931                 $user_cur = &$bri->user[$this->player[$i]];
932                 $user_cur->continue_set(BIN5_USER_CONTINUE_ALREADY);
933
934                 $user_cur->trans_step = $user_cur->step + 1;
935                 $user_cur->comm[$user_cur->step % COMM_N] = sprintf('gst.st_loc++; gst.st=%d; xstm.stop(); window.onunload = null ; window.onbeforeunload = null ; document.location.assign("index.php");|', $user_cur->step+1);
936                 $user_cur->step_inc();
937
938                 // a void command force xynt-streamer to flush all data to client
939                 $user_cur->trans_step = $user_cur->step + 1;
940                 $user_cur->comm[$user_cur->step % COMM_N] = "";
941                 $user_cur->step_inc();
942
943                 $user_cur->comm[$user_cur->step % COMM_N] = show_table(&$bri, &$user_cur, $user_cur->step+1, TRUE, FALSE);
944                 $user_cur->step_inc();
945             }
946             return (TRUE);
947         } while (FALSE);
948
949         $dt = date("H:i ", $curtime);
950         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
951             if ($ret == FALSE && $this->player[$i] != $user->idx)
952                 continue;
953             $user_cur = &$bri->user[$this->player[$i]];
954             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
955             $user_cur->comm[$user_cur->step % COMM_N] .= nickserv_msg($dt, $msg);
956             $user_cur->step_inc();
957         }
958     }
959
960 } // end class Bin5_table
961
962
963
964
965 define('BIN5_USER_FLAG_RING_ENDAUCT', 0x01);
966 define('BIN5_USER_CONTINUE_INIT', -1);
967 define('BIN5_USER_CONTINUE_ALREADY', -2);
968 class Bin5_user extends User {
969     var $asta_card;  //
970     var $asta_pnt;   //
971     var $handpt;     // Total card points at the beginning of the current hand.
972     var $exitislock; // Player can exit from the table ?
973     var $privflags;  // Flags for briskin5 only
974
975     var $continue;   // Id of the match that the user would continue
976
977     const BASE = "../";
978
979     function User() {
980     }
981
982     /* CREATE NOT USED
983        function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
984        if (($thiz =& new User()) == FALSE)
985        return (FALSE);
986
987        $thiz->asta_card = -2;
988        $thiz->asta_pnt  = -1;
989        $thiz->handpt = -1;
990        $thiz->exitislock = TRUE;
991        $thiz->privflags = 0;
992        $thiz->continue = BIN5_USER_CONTINUE_INIT;
993
994        return ($thiz);
995        }
996     */
997
998     function parentcopy(&$from)
999     {
1000         parent::copy($from);
1001     }
1002
1003     function copy(&$from)
1004     {
1005         $this->parentcopy($from);
1006
1007         $this->asta_card  = $from->asta_card;
1008         $this->asta_pnt   = $from->asta_pnt;
1009         $this->handpt     = $from->handpt;
1010         $this->exitislock = $from->exitislock;
1011         $this->privflags  = $from->privflags;
1012         $this->continue   = $from->continue;
1013     }
1014
1015     /* CLONE NOT USED
1016        function myclone(&$from)
1017        {
1018        if (($thiz =& new User()) == FALSE)
1019        return (FALSE);
1020
1021        $thiz->copy($from);
1022
1023        return ($thiz);
1024        }
1025     */
1026
1027     static function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
1028     {
1029         if (($thiz = new Bin5_user()) == FALSE)
1030             return (FALSE);
1031
1032         if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
1033             $CO_bin5_pref_ring_endauct = "";
1034         }
1035
1036         $thiz->parentcopy($from);
1037
1038         /* NOTE: at this moment idx and table_pos fields have the same value
1039            but diffentent functions, we keep them separated for a while */
1040         $thiz->room       = $bri;
1041         $thiz->idx        = $table_pos; // it is the position in the mini-room,
1042                                         // not related to table pos (see below)
1043         $thiz->asta_card  = -2;
1044         $thiz->asta_pnt   = -1;
1045         $thiz->handpt     = -1;
1046         $thiz->exitislock = TRUE;
1047         $thiz->continue   = BIN5_USER_CONTINUE_INIT;
1048
1049         log_wr("Bin5 constructor");
1050
1051         $thiz->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
1052
1053         $thiz->table_orig = $table;
1054         $thiz->table      = 0;
1055         $thiz->table_pos  = $table_pos;
1056
1057         $thiz->step_inc();
1058
1059         return ($thiz);
1060     }
1061
1062     function step_set($step)
1063     {
1064         $this->step = $step & 0x7fffffff;
1065
1066         return TRUE;
1067     }
1068
1069     function step_inc($delta = 1) {
1070         $this->step += $delta;
1071         /* modularization because unpack() not manage unsigned 32bit int correctly */
1072         $this->step &= 0x7fffffff;
1073
1074         return (TRUE);
1075     }
1076
1077     static function load_step($tab_id, $sess)
1078     {
1079         $fp = FALSE;
1080         do {
1081             if (validate_sess($sess) == FALSE)
1082                 break;
1083
1084             if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1085                 mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
1086             if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step", 'rb')) == FALSE)
1087                 break;
1088             if (($s = fread($fp, 8)) == FALSE)
1089                 break;
1090             if (mb_strlen($s, "ASCII") != 8)
1091                 break;
1092             $arr = unpack('Ls/Li', $s);
1093             fclose($fp);
1094
1095             // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
1096             return ($arr);
1097         } while (0);
1098
1099         if ($fp != FALSE)
1100             fclose($fp);
1101
1102         log_rd2("STEP_GET [".$sess."]: return false ");
1103
1104         return (FALSE);
1105     }
1106
1107     function save_step()
1108     {
1109         do {
1110             if (validate_sess($this->sess) == FALSE)
1111                 break;
1112             if (file_exists(BIN5_PROXY_PATH."/table".$this->table_orig) == FALSE)
1113                 mkdir(BIN5_PROXY_PATH."/table".$this->table_orig, 0775, TRUE);
1114             if (($fp = @fopen(BIN5_PROXY_PATH."/table".$this->table_orig."/".$this->sess.".step", 'w')) == FALSE)
1115                 break;
1116             fwrite($fp, pack("LL",$this->step, $this->idx));
1117             fclose($fp);
1118
1119             log_main("step_set [".$this->sess. "] [".$this->step."]");
1120
1121             return (TRUE);
1122         } while (0);
1123
1124         return (FALSE);
1125     }
1126
1127     static function unproxy_step($tab_id, $sess)
1128     {
1129         log_rd2("UNPROXY: ".BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
1130         if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
1131             return;
1132
1133         @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
1134     }
1135
1136     function destroy_data($tab_id)
1137     {
1138         do {
1139             if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos, "B")) == -1) {
1140                 log_crit("BIN5 USER DATA REMOVE FAILED 1 [".FTOK_PATH."/bin5/table".$tab_id."/user".$this->table_pos."]");
1141                 break;
1142             }
1143
1144             if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
1145                 log_crit("BIN5 USER DATA REMOVE FAILED 2");
1146                 break;
1147             }
1148             if (shmop_delete($shm) == 0) {
1149                 log_crit("BIN5 USER DATA REMOVE FAILED 3");
1150                 break;
1151             }
1152             $shm = FALSE;
1153
1154             log_main("BIN5 USER DATA DESTROY SUCCESS");
1155
1156             // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1157             $ret = TRUE;
1158         } while (0);
1159
1160         if ($shm)
1161             shm_detach($shm);
1162
1163         return ($ret);
1164     }
1165
1166     static function blocking_error($is_unrecoverable)
1167     {
1168         log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
1169         return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
1170     }
1171
1172     protected function page_sync($sess, $page)
1173     {
1174         log_rd2("PAGE_SYNC");
1175         printf("xXx BIN5_USER::PAGE_SYNC\n");
1176         return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
1177     }
1178
1179     protected function maincheck($get, $post, $cookie)
1180     {
1181         GLOBAL $G_lang;
1182         GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
1183         GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
1184
1185         GLOBAL $S_load_stat;
1186
1187         log_rd("maincheck begin");
1188
1189         $ret = FALSE;
1190         $curtime = time();
1191
1192       /* Nothing changed, return. */
1193       if ($this->rd_step == $this->step)
1194           return (FALSE);
1195
1196       log_rd2("do other cur_stat[".$this->rd_stat."] user->stat[".$this->stat."] cur_step[".$this->rd_step."] user_step[".$this->step."]");
1197
1198       if ($this->rd_step == -1) {
1199           /*
1200            *  if $this->rd_step == -1 load the current state from the main struct
1201            */
1202
1203           $S_load_stat['wR_minusone']++;
1204
1205           // if ($this->the_end == TRUE) {
1206           // log_rd2("main_check: the end".var_export(debug_backtrace()));
1207           // }
1208
1209           if ($this->trans_step != -1) {
1210               log_rd2("TRANS USATO ".$this->trans_step);
1211               $this->rd_step = $this->trans_step;
1212               $this->trans_step = -1;
1213           }
1214           else {
1215               log_rd2("TRANS NON ATTIVATO");
1216           }
1217       }
1218
1219
1220       /* this part I suppose is read only on $this->room structure */
1221       if ($this->rd_step == -1) {
1222           log_rd2("PRE-NEWSTAT");
1223
1224           /***************
1225            *             *
1226            *    TABLE    *
1227            *             *
1228            ***************/
1229           if ($this->stat == 'table') {
1230               log_load("RESYNC");
1231               /* NOTE: $this->room is associated with the current $bri object */
1232               printf("xXx CLASS NAME [%s]\n", get_class($this->room));
1233               $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
1234           }
1235           log_rd2("NEWSTAT: ".$this->stat);
1236
1237           $this->rd_stat  = $this->stat;
1238           $this->rd_subst = $this->subst;
1239           $this->rd_step  = $this->step;
1240       } /* if ($this->rd_step == -1) { */
1241       else {
1242           $S_load_stat['rU_heavy']++;
1243
1244           if ($this->rd_step < $this->step) {
1245               do {
1246                   if ($this->rd_step + COMM_N < $this->step) {
1247                       if ($this->rd_stat != $this->stat) {
1248                           $to_stat = $this->stat;
1249                           log_load("RESYNC");
1250                           printf("xXx BIN5_USER::MAINCHECK\n");
1251                           return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
1252                       }
1253                       log_rd2("lost history, refresh from scratch");
1254                       printf("xXx LOST HISTORY!\n");
1255                       $this->rd_step = -1;
1256                       break;
1257                   }
1258                   for ($i = $this->rd_step ; $i < $this->step ; $i++) {
1259                       $ii = $i % COMM_N;
1260                       if ($this->comm[$ii] == "") {
1261                           if ($i == $this->rd_step)
1262                               continue;
1263                           else
1264                               break;
1265                       }
1266                       log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
1267                       $ret .= $this->comm[$ii];
1268                   }
1269                   $this->rd_stat  = $this->stat;
1270                   $this->rd_subst = $this->subst;
1271                   $this->rd_step  = $this->step;
1272               } while (0);
1273
1274               log_rd2($this->step, 'index_rd.php: after ret set');
1275
1276               // if ($this->the_end == TRUE) { management is moved
1277               // in the spush scope
1278           } /* if ($this->rd_step < $this->step) { */
1279       }  /* else of if ($this->rd_step == -1) { */
1280
1281
1282       return ($ret);
1283     }  //   function maincheck(...
1284
1285     function continue_set($match_code)
1286     {
1287         $this->continue = $match_code;
1288     }
1289
1290     function continue_get() {
1291         return ($this->continue);
1292     }
1293
1294 } // end class Bin5_user
1295
1296
1297 class Bin5 {
1298     static $delta_t = array();
1299     var $brisk;// room object reference
1300
1301     var $user;
1302     var $table;
1303     var $comm; // commands for many people
1304     var $step; // current step of the comm array
1305     var $garbage_timeout;
1306     var $shm_sz;
1307
1308     var $table_idx;
1309     var $table_token;
1310
1311     var $the_end;
1312     var $tok;
1313
1314     var $delay_mgr;
1315
1316     function Bin5($brisk, $table_idx, $table_token, $get, $post, $cookie) {
1317         $this->user = array();
1318         $this->table = array();
1319
1320         $this->the_end = FALSE;
1321         $this->shm_sz = BIN5_SHM_MIN;
1322         if (($this->tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
1323             echo "FTOK FAILED";
1324             exit;
1325         }
1326
1327         $this->brisk = $brisk;
1328         $user  = $brisk->user;
1329         $table = $brisk->table[$table_idx];
1330         log_wr("Bin5 constructor");
1331
1332         for ($i = 0 ; $i < $table->player_n ; $i++) {
1333             $user[$table->player[$i]]->table_token = $table_token;
1334             $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
1335         }
1336         $this->table[0] = Bin5_table::spawn(&$table);
1337
1338         log_main("TABLE_OLD_WIN - Bin5:".$this->table[0]->old_asta_win);
1339
1340         $this->table_idx = $table_idx;
1341         $this->table_token = $table_token;
1342         $this->garbage_timeout = 0;
1343
1344         $this->delay_mgr = new Delay_Manager((GARBAGE_TIMEOUT *3.0) / 2.0);
1345
1346         log_wr("Bin5 constructor end");
1347     }
1348
1349
1350     function get_user($sess, &$idx)
1351     {
1352         GLOBAL $PHP_SELF;
1353
1354         if (validate_sess($sess)) {
1355             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1356                 if ($this->user[$i]->is_empty())
1357                     continue;
1358                 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1359                     // find it
1360                     $idx = $i;
1361                     $ret = &$this->user[$i];
1362                     return ($ret);
1363                 }
1364             }
1365             log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1366             // for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++)
1367             // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1368         }
1369         else {
1370             log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1371         }
1372
1373         return (FALSE);
1374     }
1375     function banned_kickoff()
1376     {
1377         $is_ban = FALSE;
1378
1379         for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1380             $user_cur = $this->user[$i];
1381
1382             // check if the IP is blacklisted
1383             if ($this->brisk->black_check($user_cur->ip)) {
1384                 $user_cur->lacc = 0;
1385                 $is_ban = TRUE;
1386                 continue;
1387             }
1388
1389             // if authorized not check if banlisted
1390             if ($user_cur->is_auth()) {
1391                 continue;
1392             }
1393
1394             if ($this->brisk->ban_check($user_cur->ip)) {
1395                 $user_cur->lacc = 0;
1396                 $is_ban = TRUE;
1397             }
1398         }
1399         return ($is_ban);
1400     }
1401
1402     function garbage_manager($force)
1403     {
1404         GLOBAL $G_base;
1405
1406         /* Garbage collector degli utenti in timeout */
1407         $ismod = FALSE;
1408         $curtime = time();
1409
1410         $delta = $this->delay_mgr->delta_get($curtime);
1411
1412         if ($force || $this->garbage_timeout < $curtime) {
1413             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
1414                 $user_cur = $this->user[$i];
1415                 if ($user_cur->is_active() == FALSE || // is not active user or
1416                     ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
1417                     continue;
1418
1419                 if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) { // Auto logout dell'utente
1420                     log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
1421
1422                     if ($user_cur->stat == 'table') {
1423                         log_auth($user_cur->sess," bin5 Autologout session.");
1424
1425                         /* main garbage_manager is delegate as autologout management */
1426                         $user_cur->the_end = TRUE;
1427
1428                         /* se gli altri utenti non erano d'accordo questo utente viene bannato */
1429                         $remcalc = $this->table[0]->exitlock_calc(&$this->user, $user_cur->table_pos);
1430                         if ($remcalc < 3) {
1431                             require_once("${G_base}Obj/hardban.phh");
1432                             Hardbans::add(($user_cur->is_auth() ? $user_cur->name : FALSE),
1433                                           $user_cur->ip, $user_cur->sess, $user_cur->laccwr + BAN_TIME);
1434                         }
1435                         //      $user->bantime = $user->laccwr + BAN_TIME;
1436
1437                         $this->table_wakeup($user_cur);
1438                     }
1439                 }
1440             }
1441             log_rd2($user_cur->sess." GARBAGE UPDATED!");
1442
1443             $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
1444
1445             $ismod = TRUE;
1446         }
1447
1448         $this->delay_mgr->lastcheck_set($curtime);
1449         return ($ismod);
1450     }
1451
1452
1453     function destroy_data()
1454     {
1455         GLOBAL $sess;
1456
1457         $ret =   FALSE;
1458         $shm =   FALSE;
1459         log_main("DESTROY BRISKIN5 DATA");
1460
1461         do {
1462             log_main("DESTROY2 BRISKIN5 DATA [".$this->table_idx."]");
1463             for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1464                 $this->user[$i]->destroy_data($this->table_idx);
1465                 Bin5_user::unproxy_step($this->table_idx, $this->user[$i]->sess);
1466             }
1467             if (($tok = @ftok(FTOK_PATH."/bin5/table".$this->table_idx."/table", "B")) == -1)
1468                 break;
1469
1470             if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
1471                 break;
1472
1473             if (shmop_delete($shm) == 0) {
1474                 log_only("REMOVE FALLITA ");
1475                 break;
1476             }
1477
1478             $shm = FALSE;
1479             log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
1480
1481             // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
1482             $ret = TRUE;
1483         } while (0);
1484
1485         if ($shm)
1486             shm_detach($shm);
1487
1488         return ($ret);
1489     }
1490
1491     static function lock_data($is_exclusive, $table_idx)
1492     {
1493         if (($res = file_lock(FTOK_PATH."/bin5/table".$table_idx."/table", $is_exclusive)) != FALSE) {
1494             self::$delta_t = microtime(TRUE);
1495             log_lock("LOCK   table [".$table_idx."]         [".self::$delta_t[$table_idx]."]");
1496
1497             return (new Vect(array('res' => $res, 'tab' => $table_idx)));
1498         }
1499
1500         return (FALSE);
1501     }
1502
1503
1504     static function unlock_data($res_vect)
1505     {
1506         GLOBAL $sess;
1507
1508         $res = $res_vect->getbyid('res');
1509         $tab = $res_vect->getbyid('tab');
1510
1511         log_lock("UNLOCK table [".$tab."]         [".(microtime(TRUE) - (self::$delta_t[$tab]))."]");
1512
1513         file_unlock($res);
1514     }
1515
1516
1517     function chatt_send($user, $mesg, $mlang_indwr = NULL)
1518     {
1519         GLOBAL $mlang_brisk, $G_lang;
1520
1521         if ($user->stat != 'table') {
1522             return;
1523         }
1524         $curtime = time();
1525
1526         $table = &$this->table[$user->table];
1527         $user_mesg = substr($mesg,6);
1528
1529         $ret = FALSE;
1530         $mesg = "";
1531
1532         $dt = date("H:i ", $curtime);
1533         if (strncmp($user_mesg, "/cont ", 6) == 0) {
1534             log_main($user->sess." chatt_send BEGIN");
1535
1536             $match_id = substr($user_mesg, 6);
1537             $table->match_continue($this, $user, $match_id);
1538         }
1539         else {
1540             for ($i = 0 ; $i < ($user->stat == 'room' ? BIN5_MAX_PLAYERS : BIN5_PLAYERS_N) ; $i++) {
1541                 if ($user->stat == 'room') {
1542                     $user_cur = &$this->user[$i];
1543                     if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or stat isn't 'room'
1544                         continue;
1545                 }
1546                 else {
1547                     $user_cur = &$this->user[$table->player[$i]];
1548                 }
1549
1550                 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1551                 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
1552                                                                      $dt, $user->flags, xcape($user->name), xcape($user_mesg));
1553                 $user_cur->step_inc();
1554             }
1555             log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
1556         }
1557     }
1558
1559     function table_wakeup($user)
1560     {
1561         $table = &$this->table[0];
1562
1563         log_main("BIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
1564
1565         $curtime = time();
1566
1567         log_main("BIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1568
1569         for ($i = 0 ; $i < $table->player_n ; $i++) {
1570             $user_cur = &$this->user[$i];
1571             log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1572
1573             if ($user_cur == $user)
1574                 $user_cur->subst = "shutdowner";
1575             else
1576                 $user_cur->subst = "shutdowned";
1577             $user_cur->laccwr = $curtime;
1578
1579             $ret = "gst.st = ".($user_cur->step+1)."; ";
1580             $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
1581
1582             log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
1583             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1584             $user_cur->step_inc();
1585         }
1586
1587         $this->the_end = TRUE;
1588     }
1589
1590     /*
1591      *  If all players are freezed the room garbage_manager clean up table and users.
1592      */
1593     function is_abandoned()
1594     {
1595         $is_ab = TRUE;
1596         $curtime = time();
1597
1598         $table = &$this->table[0];
1599
1600         for ($i = 0 ; $i < $table->player_n ; $i++) {
1601             $user_cur = &$this->user[$i];
1602
1603             if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
1604                 $is_ab = FALSE;
1605                 break;
1606             }
1607         }
1608
1609         return ($is_ab);
1610     }
1611
1612     static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
1613     {
1614         GLOBAL $G_ban_list, $G_black_list;
1615
1616         printf("NEW_SOCKET (root): %d\n", intval($new_socket));
1617
1618         $enc = get_encoding($header);
1619         if (isset($header['User-Agent'])) {
1620             if (strstr($header['User-Agent'], "MSIE")) {
1621                 $transp_type = "htmlfile";
1622             }
1623             else {
1624                 $transp_type = "xhr";
1625             }
1626         }
1627         else {
1628             $transp_type = "iframe";
1629         }
1630         force_no_cache($header_out);
1631
1632         if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE)
1633             unset($table_idx);
1634
1635         if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE)
1636             unset($table_token);
1637
1638
1639         switch ($path) {
1640         case "":
1641         case "index.php":
1642             ob_start();
1643             bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
1644             $content = ob_get_contents();
1645             ob_end_clean();
1646
1647             $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1648             return TRUE;
1649
1650         break;
1651         case "index_wr.php":
1652             if (isset($table_idx) && isset($table_token)) {
1653                 if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
1654                     ob_start();
1655                     bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
1656                     $content = ob_get_contents();
1657                     ob_end_clean();
1658                 }
1659                 else {
1660                     $content = "Bin5 Load data error";
1661                 }
1662             }
1663             else {
1664                 $content = "Bin5 Load data error";
1665             }
1666             $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1667             return TRUE;
1668
1669             break;
1670         case "index_rd.php":
1671             if (($transp  = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
1672                 $transp = "iframe";
1673             if ($transp == 'websocket')
1674                 $enc = 'plain';
1675
1676             do {
1677                 if (!isset($table_idx)
1678                     || !isset($table_token)
1679                     || !isset($cookie['sess'])
1680                     || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
1681                     || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
1682
1683                     $content = Bin5_user::stream_fini($transp_type, $s_a_p->rndstr, TRUE);
1684                     $s_a_p->pendpage_try_addflush($new_socket, 20, $enc, $header_out, $content);
1685
1686                     return TRUE;
1687                     break;
1688                 }
1689                 $bri->brisk->sess_cur_set($user->sess);
1690
1691                 // close a previous opened index_read_ifra socket, if exists
1692                 if (($prev = $user->rd_socket_get()) != NULL) {
1693                     $s_a_p->socks_unset($user->rd_socket_get());
1694                     fclose($user->rd_socket_get());
1695                     printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
1696                     $user->rd_socket_set(NULL);
1697                 }
1698
1699                 $content = "";
1700                 $user->stream_init($s_a_p->rndstr, $enc, $header, $header_out, $content, $get, $post, $cookie);
1701
1702                 $response = headers_render($header_out, -1).$user->chunked_content($content);
1703                 $response_l = mb_strlen($response, "ASCII");
1704
1705                 $wret = @fwrite($new_socket, $response, $response_l);
1706                 if ($wret < $response_l) {
1707                     printf("TROUBLES WITH FWRITE: %d\n", $wret);
1708                     $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
1709                 }
1710                 else {
1711                     $user->rd_cache_set("");
1712                 }
1713                 fflush($new_socket);
1714
1715
1716                 $s_a_p->socks_set($new_socket, $user, NULL);
1717                 $user->rd_socket_set($new_socket);
1718                 printf(" - qui ci siamo - ");
1719                 return TRUE;
1720             } while (FALSE);
1721
1722             return FALSE;
1723             break;
1724
1725         default:
1726             return FALSE;
1727             break;
1728       }
1729
1730       return (FALSE);
1731   }
1732
1733
1734
1735 } // end class Bin5
1736
1737 function locshm_exists($tok)
1738 {
1739     // return (TRUE);
1740
1741     if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
1742         log_main($tok." SHM NOT exists");
1743
1744         return (FALSE);
1745     }
1746     else {
1747         shmop_close($id);
1748         log_main($tok." SHM exists");
1749
1750         return (TRUE);
1751     }
1752
1753 }
1754
1755
1756 /* show table
1757    is_transition (is from room to table ?)
1758    is_again      (is another game)
1759
1760    Examples                    of $is_transition, $is_again:
1761    from reload of the page:              FALSE, FALSE
1762    from sitdown in room:                  TRUE, FALSE
1763    from table: asta cmd e tutti passano:  TRUE, TRUE
1764    from table: fine partita:              TRUE, TRUE
1765 */
1766 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1767 {
1768     $table_idx = $user->table;
1769     $table     = $bri->table[$table_idx];
1770     $table_pos = $user->table_pos;
1771
1772     $ret = "table_init();";
1773     $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1774     if (!$is_again) {
1775         /* GENERAL STATUS */
1776         $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1777                          $sendstep, $user->stat, $user->subst, $table_pos);
1778
1779         log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1780
1781         /* BACKGROUND */
1782         $ret .= "background_set();";
1783
1784         /* USERS INFO */
1785         $ret .= $user->myname_innerHTML();
1786         $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1787                         $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
1788                         xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
1789
1790                         $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
1791                         xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
1792
1793                         $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
1794                         xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
1795
1796                         (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
1797                         (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
1798
1799                         (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
1800                         (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
1801     }
1802     /* NOTIFY FOR THE CARD MAKER */
1803     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1804         $ret .= show_table_info(&$bri, &$table, $table_pos);
1805         $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
1806     }
1807     else {
1808         $ret .= "\$('imgct').innerHTML = \$('imgct').innerHTML = mlang_commons['imgload_a'][g_lang]+\"100%.\";";
1809     }
1810     if (!$is_again)
1811         $ret .= table_welcome($user);
1812
1813     if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1814         $ret .= playsound("cow.mp3");
1815     }
1816
1817
1818     /* CARDS */
1819     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1820         $ret .= "|";
1821
1822         for ($i = 0 ; $i < BIN5_CARD_HAND ; $i++) {
1823             for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++) {
1824                 $ct = 0;
1825                 for ($o = 0 ; $o < (BIN5_CARD_HAND * BIN5_PLAYERS_N) && $ct < $i+1 ; $o++) {
1826                     // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1827                     if ($table->card[$o]->owner == (($e + $table->gstart) % BIN5_PLAYERS_N)) {
1828                         $ct++;
1829                         if ($ct == $i+1)
1830                             break;
1831                     }
1832                 }
1833                 log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1834
1835                 $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BIN5_PLAYERS_N,
1836                                  $i, ((($e + BIN5_PLAYERS_N - $table_pos + $table->gstart) % BIN5_PLAYERS_N) == 0 ?
1837                                       $table->card[$o]->value : -1),
1838                                  ($i == 7 && $e == (BIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1839             }
1840         }
1841     }
1842     else {
1843         $taked  = array(0,0,0,0,0);
1844         $inhand = array(0,0,0,0,0);
1845         $ontabl  = array(-1,-1,-1,-1,-1);
1846         $cards  = array();
1847
1848         for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1849             // for ($i = 0 ; $i < 40 ; $i++) {
1850             if ($table->card[$i]->stat == 'hand') {
1851                 if ($table->card[$i]->owner == $table_pos) {
1852                     $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1853                 }
1854                 $inhand[$table->card[$i]->owner]++;
1855             }
1856             else if ($table->card[$i]->stat == 'take') {
1857                 log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1858                 $taked[$table->card[$i]->owner]++;
1859             }
1860             else if ($table->card[$i]->stat == 'table') {
1861                 $ontabl[$table->card[$i]->owner] = $i;
1862             }
1863         }
1864         $logg = "\n";
1865         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1866             $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1867         }
1868         log_main("Stat table: ".$logg);
1869
1870         /* Set ours cards. */
1871         $oursarg = "";
1872         for ($i = 0 ; $i < $inhand[$table_pos] ; $i++)
1873             $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1874         for ($i = $inhand[$table_pos] ; $i < BIN5_CARD_HAND ; $i++)
1875             $oursarg .= ($i == 0 ? "" : ", ")."-1";
1876         $ret .= sprintf('card_setours(%s);', $oursarg);
1877
1878         /* Dispose all cards */
1879         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1880             /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1881             $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1882                             ($inhand[$i] <= BIN5_CARD_HAND ? $inhand[$i] : BIN5_CARD_HAND), $taked[$i]);
1883
1884             if ($ontabl[$i] != -1) {
1885                 $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i],
1886                                 $table->card[$ontabl[$i]]->value,
1887                                 $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1888             }
1889         }
1890     }
1891
1892     /* Show auction */
1893     if ($user->subst == 'asta') {
1894
1895         /* show users auction status */
1896         $showst = "";
1897         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1898             $user_cur = &$bri->user[$table->player[$i]];
1899             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "),
1900                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1901         }
1902         if (BIN5_PLAYERS_N == 3)
1903             $showst .= ",-2,-2";
1904         $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1905         $ret .= sprintf('show_astat(%s);', $showst);
1906
1907         if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1908             /* show card chooser */
1909             $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1910                             $table->asta_card);
1911         }
1912         else {
1913             /* show auction */
1914             if ($table_pos == ($table->gstart % BIN5_PLAYERS_N) &&
1915                 $table->asta_win == -1)
1916                 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1917                                 $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1918             else
1919                 $ret .= sprintf('dispose_asta(%d,%d, %s);',
1920                                 $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1921         }
1922
1923         /* Remark */
1924         if ($table->asta_win == -1) { // auction case
1925             if ($table_pos == ($table->gstart % BIN5_PLAYERS_N))
1926                 $ret .= "remark_on();";
1927             else
1928                 $ret .= "remark_off();";
1929         }
1930         else { // chooseed case
1931             if ($table_pos == $table->asta_win)
1932                 $ret .= "remark_on();";
1933             else
1934                 $ret .= "remark_off();";
1935         }
1936     }
1937     else if ($user->subst == 'game') {
1938         /* HIGHLIGHT */
1939         if (($table->gstart + $table->turn) % BIN5_PLAYERS_N == $table_pos)
1940             $ret .= "is_my_time = true; remark_on();";
1941         else
1942             $ret .= "remark_off();";
1943
1944         /* WHO CALL AND WHAT */
1945         $ret .= briscola_show($bri, $table, $user);
1946
1947     }
1948     return ($ret);
1949 } // end function show_table(...
1950
1951 function calculate_winner(&$table)
1952 {
1953     $briontab = FALSE;
1954     $ontab = array();
1955     $ontid = array();
1956     $cur_win  =  -1;
1957     $cur_val  = 100;
1958     $cur_seed = $table->briscola - ($table->briscola % 10);
1959
1960     for ($i = 0 ; $i < (BIN5_CARD_HAND * BIN5_PLAYERS_N) ; $i++) {
1961         // for ($i = 0 ; $i < 40 ; $i++) {
1962         if ($table->card[$i]->stat != "table")
1963             continue;
1964
1965         log_wr(sprintf("Card On table: [%d]", $i));
1966
1967         $v = $table->card[$i]->value;
1968         $ontab[$table->card[$i]->owner] = $v;
1969         $ontid[$table->card[$i]->owner] = $i;
1970         /* se briscola setto il flag */
1971         if (($v - ($v % 10)) == $cur_seed)
1972             $briontab = TRUE;
1973     }
1974
1975     if ($briontab == FALSE) {
1976         $cur_win  = $table->gstart;
1977         $cur_val  = $ontab[$cur_win];
1978         $cur_seed = $cur_val - ($cur_val % 10);
1979     }
1980
1981     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1982         if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1983             if ($ontab[$i] < $cur_val) {
1984                 $cur_val = $ontab[$i];
1985                 $cur_win = $i;
1986             }
1987         }
1988     }
1989
1990     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
1991         $table->card[$ontid[$i]]->owner = $cur_win;
1992         $table->card[$ontid[$i]]->stat =  "take"; // Card stat
1993     }
1994     return ($cur_win);
1995 }
1996
1997 function show_table_info(&$bri, &$table, $table_pos)
1998 {
1999     GLOBAL $G_lang, $mlang_bin5_bin5;
2000
2001     $tg_br = "<br>";
2002     $tg_hr = "<hr>";
2003     $tg_bo = "<b>";
2004     $tg_bc = "</b>";
2005
2006     $ret = "";
2007     $user = $bri->user[$table->player[$table_pos]];
2008
2009     // TAG: POINTS_MANAGEMENT
2010     $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2011     $noty = sprintf('<table class=\"points\"><tr><th></th>');
2012
2013     // Names.
2014     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++)
2015         $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
2016     $noty .= sprintf("</tr>");
2017
2018     // Points.
2019     log_main("show_table_info: pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
2020
2021     for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2022         $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2023         for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
2024             $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2025         $noty .= "</tr>";
2026     }
2027
2028     // Total points.
2029     $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2030     for ($e = 0 ; $e < BIN5_PLAYERS_N ; $e++)
2031         $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2032     $noty .= "</tr></table>";
2033     $noty .= "<hr>";
2034     if ($table->old_reason != "") {
2035         $noty .= sprintf($mlang_bin5_bin5['info_last'][$G_lang],
2036                          $tg_br, $tg_hr, $tg_bo, $tg_bc);
2037         $noty .= '<br>';
2038         $noty .= $table->old_reason;
2039     }
2040
2041     $noty .= sprintf($mlang_bin5_bin5['info_curr'][$G_lang],
2042                      $tg_br, $tg_hr, $tg_bo, $tg_bc);
2043     $noty .= '<br>';
2044     /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
2045     if ($table->mazzo == $table_pos)
2046         $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
2047     else {
2048         $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
2049         $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
2050     }
2051
2052     if ($user->subst == 'asta') {
2053         if ($table->asta_win == -1)  // auction case
2054             $curplayer = $table->gstart % BIN5_PLAYERS_N;
2055         else
2056             $curplayer = $table->asta_win;
2057     }
2058     else if ($user->subst == 'game') {
2059         $curplayer = ($table->gstart + $table->turn) % BIN5_PLAYERS_N;
2060     }
2061
2062     /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
2063     if ($curplayer == $table_pos) {
2064         $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
2065     }
2066     else {
2067         $unam = xcape($bri->user[$table->player[$curplayer]]->name);
2068         $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
2069     }
2070
2071     $multer = $table->rules->multer(TRUE);
2072     if ($multer > 1) {
2073         $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
2074     }
2075     $noty .= "<hr>";
2076     if ($table->match_id != -1) {
2077         $noty .= sprintf($mlang_bin5_bin5['info_match'][$G_lang], $table->match_id);
2078         $noty .= "<hr>";
2079     }
2080     $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
2081     /* NOTE: show_notify($noty, 3000, "torna alla partita", 500,
2082      *                   130 + ($table->points_n > 0 ? 50 : 0) +
2083      *                   (120 * ($table->points_n / MAX_POINTS)));
2084      *       will be used when we refact notify js function following
2085      *       photoo class logic
2086      */
2087
2088     return ($ret);
2089 }
2090
2091 function table_welcome($user)
2092 {
2093     GLOBAL $table_wellarr, $G_lang;
2094     $ret = "";
2095
2096     for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
2097         $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
2098
2099     return ($ret);
2100 }
2101
2102
2103 function briscola_show($bri, $table, $user)
2104 {
2105     GLOBAL $G_lang, $mlang_bin5_bin5;
2106     $ptnadd = "";
2107     $ret = "";
2108
2109     if ($table->asta_card == 9)
2110         $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
2111
2112     /* text of caller cell */
2113     if ($user->table_pos == $table->asta_win) {
2114         $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
2115         $ret .= sprintf($prestr, $ptnadd);
2116     }
2117     else {
2118         $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
2119         $ret .= sprintf($prestr,
2120                         xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
2121     }
2122     $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);
2123
2124     $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
2125                     $table->asta_win);
2126     $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
2127     $ret .= sprintf('$("caller").style.visibility = "visible";');
2128     $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
2129     $ret .= sprintf('$("astalascio").style.visibility = "";');
2130     $ret .= sprintf('$("asta").style.visibility = "hidden";');
2131     $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
2132
2133     return ($ret);
2134 }
2135
2136 /* FIXME
2137 function game_result_new($asta_pnt, $pnt)
2138 {
2139     if ($pnt >= $asta_pnt)
2140         return (1);
2141     else
2142         return (-1);
2143 }
2144 */
2145
2146 function log_points($remote_addr, $curtime, $user, $where, $mesg)
2147 {
2148     if (($fp = @fopen(LEGAL_PATH."/points.log", 'a')) != FALSE) {
2149         /* Unix time | session | nickname | IP | where was | mesg */
2150         fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2151                             ($user->is_auth() ? 'A' : 'N'),
2152                             $user->name, $remote_addr, $where , $mesg));
2153         fclose($fp);
2154     }
2155 }
2156
2157
2158 ?>