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