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