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