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