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