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