title change when asta is finish, privflags added to manage preference
[brisk.git] / web / briskin5 / Obj / briskin5.phh
1 <?php
2 /*
3  *  brisk - briskin5.phh
4  *
5  *  Copyright (C) 2006-2009 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  * $Id$
24  *
25  */
26 define(BRISKIN5_PLAYERS_N, 3);
27 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
28 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
29 define(BRISKIN5_SHM_MIN, 32768);
30 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
31 define(BRISKIN5_SHM_DLT, 32768);
32
33 $mlang_bin5_bin5 = array( 
34                          'info_part' => array( 'it' => '<hr>Nell\'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>',
35                                                'en' => '<hr>In the last hand the declarer was <b>%s</b>, the partner was <b>%s</b>,<br>'),
36                          'info_capp' => array( 'it' => 'hanno fatto <b>cappotto</b> EBBRAVI!.<hr>',
37                                                'en' => 'and he made <b>capot</b> WELL DONE!.<hr>'),
38                          'info_pnt'  => array( 'it' => 'dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: %s.<hr>',
39                                                'en' => 'they had to do <b>%s</b> points and they had made <b>%d</b>: %s.<hr>'),
40                          'info_alea' => array( 'it' => 'almeno ',
41                                                'en' => 'at least '),
42                          'info_more' => array( 'it' => 'pi&ugrave; di 60',
43                                                'en' => 'over 60'),
44                          'info_win'  => array( 'it' => 'hanno <b>vinto</b>',
45                                                'en' => 'they have <b>win</b>'),
46                          'info_peer' => array( 'it' => 'hanno <b>pareggiato</b>',
47                                                'en' => 'they have <b>drew</b>'),
48                          'info_lost' => array( 'it' => 'hanno <b>perso</b>',
49                                                'en' => 'they have <b>lost</b>'),
50                          'info_yturn'=> array( 'it' => ' tocca a <b>te</b> giocare.',
51                                                'en' => ' it\'s <b>your</b> turn.'),
52                          'info_turn' => array( 'it' => 'tocca a <b>%s</b> giocare.',
53                                                'en' => 'it\'s the <b>%s</b>\'s turn.'),
54                          'info_mult' => array( 'it' => ' La partita vale <b>%s</b>.',
55                                                'en' => ' The game worth <b>%s</b>.' ),
56                          'info_yshuf'=> array( 'it' => 'Fai <b>tu</b> il mazzo, ',
57                                                'en' => 'It\'s <b>your</b> shuffled the cards, '),
58                          'info_shuf' => array( 'it' => 'Il mazzo a <b>%s</b>, ',
59                                                'en' => '<b>%s</b> shuffled the cards, '),
60                          'btn_bkgame'=> array( 'it' => 'torna alla partita',
61                                                'en' => 'back to the game'),
62                          'call_wptn' => array( 'it' => '<br>con %d punti',
63                                                'en' => '<br>with %d points'),
64                          'call_ycall'=> array( 'it' => 'Chiami%s:',
65                                                'en' => 'Call%s:'),
66                          'call_call' => array( 'it' => 'Chiama %s%s:',
67                                                'en' => 'The declarer is %s%s:')
68
69                          
70                          );
71
72                          // MLANG
73                          $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.'),
74                                                  '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.') );
75
76 function multoval($mult)
77 {
78   GLOBAL $G_lang;
79
80   if ($mult == 2)
81     return ($G_lang == 'en' ? 'double' : 'doppio');
82   if ($mult == 3)
83     return ($G_lang == 'en' ? 'triple' : 'triplo');
84   else if ($mult == 4)
85     return ($G_lang == 'en' ? 'quadruple' : 'quadruplo');
86   else
87     return (sprintf(($G_lang == 'en' ? "%d-ple" : "%d-plo"), $mult));
88 }
89
90
91 class Card {
92   var $value; /* 0 - 39 card value */
93   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
94   var $owner; /* (table position 0-4) */
95   // var $pos;   /* Pos in hand. */
96   var $x;     /* When played the X position on the table of the owner. */
97   var $y;     /* When played the Y position on the table of the owner. */
98
99   function Card($value, $stat, $owner)
100   {
101     $this->value = $value;
102     $this->stat  = $stat; // Card stat
103     $this->owner = $owner;
104   }
105
106   function assign($stat,$owner)
107   {
108     $this->stat  = $stat; // Card stat
109     $this->owner = $owner;
110   }
111
112   function setpos($pos)
113   {
114     $this->pos   = $pos;
115   }
116
117   function play($x,$y)
118   {
119     $this->stat = 'table'; // Card stat
120     $this->x = $x;
121     $this->y = $y;
122   }
123
124   function take($newown)
125   {
126     $this->stat = 'take'; // Card stat
127     $this->owner = $newown;
128   }
129 } // end class Card
130
131 class Table_briskin5 extends Table {
132   var $card;       // il mazzo di carte
133   var $mazzo;      // chi e' di mazzo
134   var $gstart;
135   var $turn;
136
137   var $asta_pla;
138   var $asta_pla_n;
139   var $asta_card;
140   var $asta_pnt;
141   
142   var $mult;
143   var $points;    // points array
144   var $points_n;  // number of row of points
145   var $total;
146
147   var $asta_win;
148   var $briscola;
149   var $friend;
150   
151   var $old_reason;
152   var $old_asta_pnt;
153   var $old_pnt;
154   var $old_win;
155   var $old_friend;
156
157   function Table_briskin5() 
158   {
159   }
160
161
162   /* CREATE() NOT USED
163   function &create($idx) 
164   {
165     GLOBAL $G_false;
166
167     if (($thiz =& new Table_briskin5()) == FALSE)
168       return ($G_false);
169
170     $thiz->create($idx);
171
172     $thiz->card      =   FALSE;
173     $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
174     $thiz->asta_pla_n=  -1;
175     $thiz->asta_card =  -1;
176     $thiz->asta_pnt  =  -1;
177     $thiz->mult      =   1;
178     
179     $thiz->points    =   array( );
180     $thiz->points_n  =   0;
181     $thiz->total     =   array( 0, 0, 0, 0, 0);
182     $thiz->asta_win  =  -1;
183     $thiz->briscola  =  -1;
184     $thiz->friend    =  -1;
185     $thiz->turn      =   0;
186
187     $thiz->old_reason   = "";
188     $thiz->old_asta_pnt = -1;
189     $thiz->old_pnt      = -1;
190     $thiz->old_win      = -1;
191     $thiz->old_friend   = -1;
192
193     return ($thiz);
194   }
195   */
196
197   /* CLONE() NOT USED
198   function &myclone(&$from)
199   {
200     GLOBAL $G_false;
201     
202     if (($thiz =& new Table_briskin5()) == FALSE)
203       return ($G_false);
204     
205     parent::copy($from);
206
207     $thiz->card = $from->card;
208     $thiz->mazzo = $from->mazzo; // REVIEW
209     $thiz->gstart = $from->gstart;
210     $thiz->turn = $from->turn;
211
212     $thiz->asta_pla = $from->asta_pla;
213     $thiz->asta_pla_n = $from->asta_pla_n;
214     $thiz->asta_card = $from->asta_card;
215     $thiz->asta_pnt = $from->asta_pnt;
216     
217     $thiz->mult = $from->mult;
218     $thiz->points = $from->points;
219     $thiz->points_n = $from->points_n;
220     $thiz->total = $from->total;
221     
222     $thiz->asta_win = $from->asta_win;
223     $thiz->briscola = $from->briscola;
224     $thiz->friend = $from->friend;
225     
226     $thiz->old_reason = $from->old_reason;
227     $thiz->old_asta_pnt = $from->old_asta_pnt;
228     $thiz->old_pnt = $from->old_pnt;
229     $thiz->old_win = $from->old_win;
230     $thiz->old_friend = $from->old_friend;
231
232     return ($thiz);
233   }
234   */
235
236   function parentcopy(&$from)
237   {
238     parent::copy($from);
239   }
240
241   function &spawn(&$from)
242   {
243     GLOBAL $G_false;
244     
245     if (($thiz =& new Table_briskin5()) == FALSE)
246       return ($G_false);
247     
248     $thiz->parentcopy($from);
249
250     log_main("PLAYER_N - spawn.".$thiz->player_n);
251
252     $thiz->card = &$thiz->bunch_create();
253     $thiz->mazzo    = rand(0,PLAYERS_N-1);
254     $thiz->points_n = 0;
255     $thiz->mult     = 1;
256     $thiz->old_win    = -1;
257     $thiz->old_reason = "";
258
259     // players are rearranged in an dedicated array
260     $thiz->player = array();
261     for ($i = 0 ; $i < $from->player_n ; $i++)
262       $thiz->player[$i] = $i;
263
264     log_main("TABLE_OLD_WIN - spawn:".$thiz->old_win);
265
266     return ($thiz);
267   }
268
269
270   function &bunch_create()
271   {
272     $ret = array();
273
274     for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
275       // for ($i = 0 ; $i < 40 ; $i++) {
276       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
277     }
278
279     $oret = &$ret;
280     return ($oret);
281   }
282
283   function bunch_make()
284   {
285     log_main("bunch_make start");
286     $ct = array(0,0,0,0,0);
287     
288     mt_srand(make_seed());
289     
290     for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) 
291       $rest[$i] = $i;
292
293     for ($i = (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) - 1 ; $i >= 0 ; $i--) {
294       $rn = rand(0, $i);
295       
296       if ($rn == 0)
297         log_main("RND ZERO");
298       
299       $id = $rest[$rn];
300
301       $owner = $i % BRISKIN5_PLAYERS_N;
302       $this->card[$id]->assign('hand', $owner);
303
304       $rest[$rn] = $rest[$i];
305       // $pubbpos[$rn2] = $pubbpos[$i];
306     }
307     log_main("bunch_make end");
308   }
309
310   function init(&$userarr)
311   {
312     /* MOVED INTO SPAWN
313     $this->mazzo    =  rand(0,PLAYERS_N-1);
314     $this->points_n =  0;
315     $this->mult     =  1;
316     $this->old_win  = -1;
317     $this->old_reason = "";
318     */
319     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
320       $this->total[$i] = 0;
321       $user_cur = &$userarr[$this->player[$i]];
322       $user_cur->exitislock = TRUE;
323     }
324
325     log_main("table::init: ci siamo");
326   }
327
328   function game_init(&$userarr)
329   {
330     log_rd2("GSTART 4");
331
332     $this->gstart = ($this->mazzo+1) % BRISKIN5_PLAYERS_N;
333     $this->bunch_make();
334     
335     
336     $this->asta_pla_n = BRISKIN5_PLAYERS_N;
337     $this->asta_card = -1;
338     $this->asta_pnt  = 60;
339     $this->asta_win  = -1;
340     $this->briscola  = -1;
341     $this->friend    = -1;
342     $this->turn      =  0;
343     
344     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
345       $this->asta_pla[$i] = TRUE;
346       $user_cur = &$userarr[$this->player[$i]];
347       $user_cur->subst = 'asta';
348       $user_cur->asta_card = -2;
349       $user_cur->asta_pnt  = -1;
350       $user_cur->handpt = $this->hand_points($i);
351       // SEE function calculate_points(&$table)
352     }
353     log_rd2("GEND 4");
354   }
355
356   function game_next()
357   {
358     $this->mazzo  = ($this->mazzo + 1) % BRISKIN5_PLAYERS_N;
359   }
360
361
362   function hand_points($idx)
363   {
364     GLOBAL $G_all_points; 
365     
366     $tot = 0;
367     
368     for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
369       // for ($i = 0 ; $i < 40 ; $i++) {
370       if ($this->card[$i]->owner != $idx)
371         continue;
372
373       $ctt = $this->card[$i]->value % 10;
374       $tot += $G_all_points[$ctt];
375     }
376
377     return ($tot);
378   }
379
380
381   function exitlock_show(&$userarr, $table_pos)
382   {
383     $ct = $this->exitlock_calc(&$userarr, $table_pos);
384
385     $ret = sprintf('exitlock_show(%d, %s);', $ct, 
386                    ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
387     return ($ret);
388   }
389
390   function exitlock_calc(&$userarr, $table_pos)
391   {
392     $ct = 0;
393
394     for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {    
395       if ($userarr[$this->player[$i]]->exitislock == FALSE)
396         $ct++;
397     }
398
399     return ($ct);
400   }
401 } // end class Table_briskin5
402
403
404
405
406 define(BIN5_USER_FLAG_RING_ENDAUCT, 0x01);
407
408 class User_briskin5 extends User {
409   var $asta_card;  // 
410   var $asta_pnt;   //
411   var $handpt;     // Total card points at the beginning of the current hand.
412   var $exitislock; // Player can exit from the table ?
413   var $privflags;  // Flags for briskin5 only 
414
415   function User() {
416   }
417
418   /* CREATE NOT USED
419   function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
420     GLOBAL $G_false;
421
422     if (($thiz =& new User()) == FALSE)
423       return ($G_false);
424
425     $thiz->asta_card = -2;
426     $thiz->asta_pnt  = -1;
427     $thiz->handpt = -1;
428     $thiz->exitislock = TRUE;
429     $thiz->privflags = 0;
430
431     return ($thiz);
432   }
433   */
434
435   function parentcopy(&$from)
436   {
437     parent::copy($from);
438   }
439
440   function copy(&$from)
441   {
442     $this->parentcopy($from);
443
444     $this->asta_card  = $from->asta_card;
445     $this->asta_pnt   = $from->asta_pnt;
446     $this->handpt     = $from->handpt;
447     $this->exitislock = $from->exitislock;
448     $this->privflags  = $from->privflags;
449   }
450
451   /* CLONE NOT USED
452   function &myclone(&$from)
453   {
454     GLOBAL $G_false;
455     
456     if (($thiz =& new User()) == FALSE)
457       return ($G_false);
458
459     $thiz->copy($from);
460
461     return ($thiz);
462   } 
463   */
464   
465   function &spawn(&$from, $table, $table_pos)
466   {
467     GLOBAL $G_false, $CO_bin5_pref_ring_endauct;
468     
469     if (($thiz =& new User_briskin5()) == FALSE)
470       return ($G_false);
471     
472     $thiz->parentcopy($from);
473
474     $thiz->asta_card = -2;
475     $thiz->asta_pnt  = -1;
476     $thiz->handpt = -1;
477     $thiz->exitislock = TRUE;
478
479     log_wr("Briskin5 constructor");
480
481     $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
482
483     $thiz->table_orig = $table;
484     $thiz->table      = 0;
485     $thiz->table_pos  = $table_pos;
486
487     return ($thiz);
488   }
489 } // end class User_briskin5
490
491
492
493 class Briskin5 {
494   var $user;
495   var $table;
496   var $comm; // commands for many people
497   var $step; // current step of the comm array
498   var $garbage_timeout;
499   var $shm_sz;
500
501   var $table_idx;
502   var $table_token;
503
504   var $the_end;
505   var $tok;
506
507   function Briskin5 (&$room, $table_idx, $table_token) {
508     $this->user = array();
509     $this->table = array();
510
511     $this->the_end = FALSE;
512     $this->shm_sz = BRISKIN5_SHM_MIN;
513     if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
514       echo "FTOK FAILED";
515       exit;
516     }
517
518     $user  =& $room->user;
519     $table =& $room->table[$table_idx];
520
521     log_wr("Briskin5 constructor");
522
523     for ($i = 0 ; $i < $table->player_n ; $i++) {
524       $user[$table->player[$i]]->table_token = $table_token;
525       $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
526     }
527     $this->table[0] =& Table_briskin5::spawn(&$table);
528
529     log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
530
531     $this->table_idx = $table_idx;
532     $this->table_token = $table_token;
533     $this->garbage_timeout = 0;
534     
535     log_wr("Briskin5 constructor end");
536   }
537
538
539   function &get_user($sess, &$idx)
540   {
541     GLOBAL $PHP_SELF, $G_false;
542
543     if (validate_sess($sess)) {
544       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
545         if (strcmp($sess, $this->user[$i]->sess) == 0) {
546           // find it
547           $idx = $i;
548           $ret = &$this->user[$i];
549           return ($ret);
550         }
551       }
552       log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
553       // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) 
554       // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
555     }
556     else {
557       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
558     }
559
560     return ($G_false);
561   }
562
563
564   function garbage_manager($force)
565   {
566     
567     /* Garbage collector degli utenti in timeout */
568     $ismod = FALSE;
569     $curtime = time();
570     if ($force || $this->garbage_timeout < $curtime) {
571       
572       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
573         $user_cur = &$this->user[$i];
574         if ($user_cur->sess == "" || 
575             ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
576           continue;
577         
578         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
579           log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
580
581           if ($user_cur->stat == 'table') {
582             log_auth($user_cur->sess," bin5 Autologout session.");
583
584             /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE 
585
586             $tmp_sess = $user_cur->sess;
587             $user_cur->sess = "";
588             step_unproxy($tmp_sess);
589             $user_cur->name = "";
590             $user_cur->the_end = FALSE;
591             
592             */
593
594             $this->table_wakeup(&$user_cur);
595           }
596         }
597       }
598       log_rd2($user_cur->sess." GARBAGE UPDATED!");
599       
600       $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
601       $ismod = TRUE;
602     }
603
604     return ($ismod);
605   }
606
607
608
609
610   //
611   //  static functions
612   //
613   function &load_data($table_idx, $table_token = "") 
614   {
615     GLOBAL $G_false, $sess;
616     $doexit = FALSE;
617     $shm = FALSE;
618
619     log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
620     
621     do {
622       if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
623         log_main("ftok failed");
624         $doexit = TRUE;
625         break;
626       }
627
628       if (($shm_sz = sharedmem_sz($tok)) == -1) {
629         log_main("shmop_open failed");
630         break;
631       }
632         
633       if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
634         break;
635
636       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
637         break;
638
639       if ($table_token != "" && $bri->table_token != $table_token) {
640         log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
641         
642         break;
643       }
644       $bri->tok = $tok;
645
646       shm_detach($shm);
647         
648       $ret = &$bri;
649       return ($ret); 
650     } while (FALSE);
651
652     if ($shm != FALSE)
653       shm_detach($shm);
654
655     log_wr("briskin5 load_data failed");
656     if ($doexit)
657       exit();
658     
659     return ($G_false);
660   }
661   
662
663
664   function save_data(&$bri) 
665   {
666     GLOBAL $sess;
667     
668     $ret =   FALSE;
669     $shm =   FALSE;
670     
671     log_main("SAVE BRISKIN5 DATA");
672     
673     if (!isset($bri->tok))
674       return (FALSE);
675     
676     while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
677       if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
678         break;
679       
680       if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
681         shm_detach($shm);
682         return (TRUE);
683       }
684       if (shm_remove($shm) === FALSE) {
685         log_only("REMOVE FALLITA");
686         break;
687       }
688       shm_detach($shm);
689       $bri->shm_sz += BRISKIN5_SHM_DLT;
690     } 
691
692     log_crit("save data failed!");
693
694     if ($shm)
695       shm_detach($shm);
696     
697     return ($ret);
698   }
699
700
701
702   function destroy_data($table_idx) 
703   {
704     GLOBAL $sess;
705
706     $ret =   FALSE;
707     $shm =   FALSE;
708     log_main("DESTROY BRISKIN5 DATA");
709     
710     do {
711       log_main("DESTROY2 BRISKIN5 DATA");
712
713       if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
714         break;
715
716       if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
717         break;
718       
719       if (shmop_delete($shm) == 0) {
720         log_only("REMOVE FALLITA ");
721         break;
722       }
723    
724       $shm = FALSE;
725       log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
726       
727       // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
728       $ret = TRUE;
729     } while (0);
730     
731     if ($shm)
732       shm_detach($shm);
733     
734     return ($ret);
735   }
736
737   function lock_data($table_idx)
738   {
739     GLOBAL $sess; 
740     
741     log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
742     //  echo "LOCK: ".FTOK_PATH."/main";
743     //  exit;
744     // WARNING monitor this step
745     if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
746       return (FALSE);
747     }
748     // WARNING monitor this step
749     if (($res = @sem_get($tok)) == FALSE) {
750       return (FALSE);
751     }
752     if (sem_acquire($res)) {   
753       log_lock("LOCK table ".$table_idx."[res: ".$res."]");
754       return ($res);
755     }
756     else {
757       log_lock("LOCK table ".$table_idx.":FAILED");
758       return (FALSE);
759     }
760   }
761   
762   function unlock_data($res)
763   {
764     GLOBAL $sess; 
765     
766     log_lock("UNLOCK table [res: ".$res."]");
767
768     return (sem_release($res));
769   }
770
771
772   function chatt_send(&$user, $mesg)
773   {
774     GLOBAL $mlang_brisk, $G_lang;
775
776     if ($user->stat == 'table') {
777       $table = &$this->table[$user->table];
778     }
779     
780     $user_mesg = substr($mesg,6);
781     
782     $curtime = time();
783     
784     $dt = date("H:i ", $curtime);
785     if (strncmp($user_mesg, "/nick ", 6) == 0) {
786       log_main($user->sess." chatt_send BEGIN");
787
788       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
789           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
790           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
791           $user->step_inc();
792
793           return;
794       }
795       $user_mesg = "COMMAND ".$user_mesg;
796       // Search dup name
797       // change
798       // update local graph
799       // update remote graphs
800       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
801         $user_cur = &$this->user[$i];
802         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
803         if ($user_cur->sess == '')
804           continue;
805         if ($user_cur->name == $name_new) {
806           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
807
808           $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
809           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
810           $user->step_inc();
811           break;
812         }
813       }
814       if ($i == BRISKIN5_MAX_PLAYERS) {
815         if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
816           if ($this->table[$user->table]->auth_only == TRUE) {
817             $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
818             $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
819             $user->step_inc();
820             return;
821           }
822           else {
823             $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
824           }
825         }
826     
827         $user->name = $name_new;
828
829         log_main($user->sess." chatt_send start set");
830         
831
832         for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
833           log_main($user->sess." chatt_send set loop");
834           
835           $user_cur = &$this->user[$i];
836           if ($user_cur->sess == '')
837             continue;
838           
839           if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
840             $table = &$this->table[$user->table];
841             
842             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
843             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
844                       $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
845                       xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
846
847                       $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
848                       xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
849
850                       $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
851                       xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
852
853                       (BRISKIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
854                       (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
855
856                       (BRISKIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
857                       (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
858             if ($user_cur == $user) {
859               $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
860               $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
861               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', 
862                                                                    $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
863             }
864             $user_cur->step_inc();
865           }
866         }
867       }
868     }
869     else {
870       for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
871         if ($user->stat == 'room') {
872           $user_cur = &$this->user[$i];
873           if ($user_cur->sess == '' || $user_cur->stat != 'room')
874             continue;
875         }
876         else {
877           $user_cur = &$this->user[$table->player[$i]];
878         }
879         
880         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
881         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
882                                                              $dt, $user->flags, xcape($user->name), xcape($user_mesg));
883         $user_cur->step_inc();
884       }
885       log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
886     }
887   }
888
889   function table_wakeup(&$user)
890   {
891     $table = &$this->table[0];
892
893     log_main("BRISKIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
894
895     $curtime = time();
896
897     log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
898     
899     for ($i = 0 ; $i < $table->player_n ; $i++) {
900       $user_cur = &$this->user[$i];
901       log_main("PREIMPOST INLOOP name: ".$user_cur->name);
902       
903       if ($user_cur == $user)
904         $user_cur->subst = "shutdowner";
905       else
906         $user_cur->subst = "shutdowned";
907       $user_cur->laccwr = $curtime;
908
909       $ret = "gst.st = ".($user_cur->step+1)."; ";
910       $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
911
912       log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
913       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
914       $user_cur->step_inc();
915     }
916
917     $this->the_end = TRUE;
918   }
919   
920   /*
921    *  If all players are freezed the room garbage_manager clean up table and users.
922    */ 
923   function is_abandoned() 
924   {
925     $is_ab = TRUE;
926     $curtime = time();
927
928     $table = &$this->table[0];
929
930     for ($i = 0 ; $i < $table->player_n ; $i++) {
931       $user_cur = &$this->user[$i];
932
933       if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) { 
934         $is_ab = FALSE;
935         break;
936       }
937     }
938
939     return ($is_ab);
940   }
941 } // end class Briskin5
942
943 function locshm_exists($tok)
944 {
945   // return (TRUE);
946
947   if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
948     log_main($tok." SHM NOT exists");
949
950     return (FALSE);
951   }
952   else {
953     shmop_close($id);
954     log_main($tok." SHM exists");
955
956     return (TRUE);
957   }
958     
959 }
960
961 // rendiamo qui l'elenco dei punti come return della func
962 function calculate_points(&$table)
963 {
964   GLOBAL $G_all_points; 
965
966   $ret = array();
967   $pro = 0;
968
969   if ($table->asta_pnt == 60)
970     $table->asta_pnt = 61;
971
972   $table->old_reason = "";
973   $table->old_win = $table->asta_win;
974   $table->old_friend = $table->friend;
975   $table->old_asta_pnt = $table->asta_pnt;
976
977   for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
978     // for ($i = 0 ; $i < 40 ; $i++) {
979     $ctt = $table->card[$i]->value % 10;
980     $own = $table->card[$i]->owner;
981     if ($own == $table->asta_win || $own == $table->friend) 
982       $pro += $G_all_points[$ctt];
983   }
984
985   log_wr(sprintf("PRO: [%d]", $pro));
986
987   
988   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
989     $table->points[$table->points_n % MAX_POINTS] = array();
990     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
991       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
992       $ret[$i] = 0;
993     }
994     $table->points_n++;
995     $table->old_pnt = $pro;
996     $table->mult *= 2;
997
998     return($ret);
999   }
1000
1001   if ($pro >= $table->asta_pnt) 
1002     $sig = 1;
1003   else
1004     $sig = -1;
1005
1006   $table->points[$table->points_n % MAX_POINTS] = array();
1007   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1008     if ($i == $table->asta_win) 
1009       $pt = ($i == $table->friend ? 4 : 2);
1010     else if ($i == $table->friend) 
1011       $pt = 1;
1012     else
1013       $pt = -1;
1014
1015     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1016
1017     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1018
1019     log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1020     
1021     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1022     $table->total[$i] += $pt;
1023     $ret[$i] = $pt;
1024   }
1025   $table->points_n++;
1026   $table->old_pnt = $pro;
1027   $table->mult = 1;
1028   
1029   return($ret);
1030 }
1031
1032 /* show table 
1033 is_transition (is from room to table ?)
1034 is_again      (is another game)
1035
1036 Examples                    of $is_transition, $is_again:
1037   from reload of the page:              FALSE, FALSE
1038   from sitdown in room:                  TRUE, FALSE
1039   from table: asta cmd e tutti passano:  TRUE, TRUE
1040   from table: fine partita:              TRUE, TRUE
1041  */
1042 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1043 {
1044   $table_idx = $user->table;
1045   $table = &$room->table[$table_idx];
1046   $table_pos = $user->table_pos;
1047
1048   $ret = "table_init();";
1049   $ret .= $table->exitlock_show(&$room->user, $table_pos);
1050   if (!$is_again) {
1051     /* GENERAL STATUS */
1052     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1053                      $sendstep, $user->stat, $user->subst, $table_pos);
1054
1055     log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1056
1057     /* BACKGROUND */
1058     $ret .= "background_set();";
1059     
1060     /* USERS INFO */
1061     $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1062     $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1063
1064     $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1065     $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1066                     $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1067                     xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1068
1069                     $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1070                     xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1071
1072                     $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1073                     xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1074
1075                     (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1076                     (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1077
1078                     (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1079                     (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1080   }
1081   /* NOTIFY FOR THE CARD MAKER */
1082   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1083     $ret .= show_table_info(&$room, &$table, $table_pos);
1084   }
1085   if (!$is_again) 
1086     $ret .= table_wellcome($user);
1087
1088   if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1089     $ret .= playsound("cow.mp3");
1090   }
1091
1092
1093   /* CARDS */
1094   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1095     $ret .= "|";
1096     
1097     for ($i = 0 ; $i < 8 ; $i++) {
1098       for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1099         $ct = 0;
1100         for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1101           // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1102           if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1103             $ct++;
1104             if ($ct == $i+1)
1105               break;
1106           }
1107         }
1108         log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1109         
1110         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N, 
1111                          $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1112                               $table->card[$o]->value : -1), 
1113                          ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1114       }
1115     }    
1116   }
1117   else {
1118     $taked  = array(0,0,0,0,0);
1119     $inhand = array(0,0,0,0,0);
1120     $ontabl  = array(-1,-1,-1,-1,-1);
1121     $cards  = array();
1122
1123     for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1124       // for ($i = 0 ; $i < 40 ; $i++) {
1125       if ($table->card[$i]->stat == 'hand') {
1126         if ($table->card[$i]->owner == $table_pos) {
1127           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1128         }
1129         $inhand[$table->card[$i]->owner]++;
1130       }
1131       else if ($table->card[$i]->stat == 'take') {
1132         log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1133         $taked[$table->card[$i]->owner]++;
1134       }
1135       else if ($table->card[$i]->stat == 'table') {
1136         $ontabl[$table->card[$i]->owner] = $i;
1137       }
1138     }
1139     $logg = "\n";
1140     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1141       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1142     }
1143     log_main("Stat table: ".$logg);
1144
1145     /* Set ours cards. */
1146     $oursarg = "";
1147     for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
1148       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1149     for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
1150       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1151     $ret .= sprintf('card_setours(%s);', $oursarg);
1152
1153     /* Dispose all cards */
1154     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1155       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1156       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1157                       ($inhand[$i] <= 8 ? $inhand[$i] : 8)  , $taked[$i]);
1158
1159       if ($ontabl[$i] != -1) {
1160         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1161                         $table->card[$ontabl[$i]]->value, 
1162                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1163       }
1164     }
1165   }
1166
1167   /* Show auction */
1168   if ($user->subst == 'asta') {
1169
1170     /* show users auction status */
1171     $showst = "";
1172     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1173       $user_cur = &$room->user[$table->player[$i]];
1174       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1175                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1176     }
1177     if (BRISKIN5_PLAYERS_N == 3)
1178         $showst .= ",-2,-2";
1179     $ret .= sprintf('document.title = "Brisk - Tavolo %d (asta)";', $user->table_orig);
1180     $ret .= sprintf('show_astat(%s);', $showst);
1181
1182     if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1183       /* show card chooser */
1184       $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1185                       $table->asta_card);
1186     }
1187     else {
1188       /* show auction */
1189       if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1190           $table->asta_win == -1) 
1191         $ret .= sprintf('dispose_asta(%d,%d, %s);', 
1192                         $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1193       else
1194         $ret .= sprintf('dispose_asta(%d,%d, %s);',
1195                         $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1196     }
1197
1198     /* Remark */
1199     if ($table->asta_win == -1) { // auction case
1200       if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N)) 
1201         $ret .= "remark_on();";
1202       else
1203         $ret .= "remark_off();";
1204     }
1205     else { // chooseed case
1206       if ($table_pos == $table->asta_win) 
1207         $ret .= "remark_on();";
1208       else
1209         $ret .= "remark_off();";
1210     }
1211   }
1212   else if ($user->subst == 'game') {
1213     /* HIGHLIGHT */
1214     if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos) 
1215       $ret .= "is_my_time = true; remark_on();";
1216     else
1217       $ret .= "remark_off();";
1218     
1219     /* WHO CALL AND WATH */
1220     $ret .= briscola_show($room, $table, $user);
1221     
1222   }
1223   return ($ret);
1224 } // end function show_table(...
1225
1226 function calculate_winner(&$table)
1227 {
1228   $briontab = FALSE;
1229   $ontab = array();
1230   $ontid = array();
1231   $cur_win  =  -1;
1232   $cur_val  = 100;
1233   $cur_seed = $table->briscola - ($table->briscola % 10);
1234
1235   for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1236     // for ($i = 0 ; $i < 40 ; $i++) {
1237     if ($table->card[$i]->stat != "table")
1238       continue;
1239
1240     log_wr(sprintf("Card On table: [%d]", $i));
1241
1242     $v = $table->card[$i]->value; 
1243     $ontab[$table->card[$i]->owner] = $v;
1244     $ontid[$table->card[$i]->owner] = $i;
1245     /* se briscola setto il flag */
1246     if (($v - ($v % 10)) == $cur_seed)
1247       $briontab = TRUE;
1248   }
1249
1250   if ($briontab == FALSE) {
1251     $cur_win  = $table->gstart;
1252     $cur_val  = $ontab[$cur_win];
1253     $cur_seed = $cur_val - ($cur_val % 10);
1254   }
1255
1256   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1257     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1258       if ($ontab[$i] < $cur_val) {
1259         $cur_val = $ontab[$i];
1260         $cur_win = $i;
1261       }
1262     }
1263   }
1264
1265   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1266     $table->card[$ontid[$i]]->owner = $cur_win;
1267     $table->card[$ontid[$i]]->stat =  "take"; // Card stat
1268   }
1269   return ($cur_win);
1270 }
1271
1272 function show_table_info(&$room, &$table, $table_pos)
1273 {
1274   GLOBAL $G_lang, $mlang_bin5_bin5;
1275
1276   $ret = "";
1277   $user = &$room->user[$table->player[$table_pos]];
1278
1279   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1280   $noty = sprintf('<table class=\"points\"><tr><th></th>');
1281   
1282   // Names.
1283   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) 
1284     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1285   $noty .= sprintf("</tr>");
1286
1287   // Points.
1288   log_main("show_table_info: pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1289
1290   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1291     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1292     for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) 
1293       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1294     $noty .= "</tr>";
1295   }
1296
1297   // Total points.
1298   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1299   for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) 
1300     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1301   $noty .= "</tr></table>";
1302
1303   if ($table->old_reason != "") {
1304     $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1305   }
1306
1307   if ($table->old_win != -1) {
1308     log_main("TABLE_OLD_WIN:".$table->old_win);
1309     $win = $table->player[$table->old_win];
1310     log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1311     $fri = $table->player[$table->old_friend];
1312
1313     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1314
1315     if ($win != $fri) {
1316       /* 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" */
1317       
1318       $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang], 
1319                        xcape($room->user[$win]->name),
1320                        xcape($room->user[$fri]->name));
1321       if ($table->old_pnt == 120) {
1322         $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1323       }
1324       else {
1325         $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1326                          ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1327                           $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1328                          ($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])));
1329       }
1330     }
1331     else {
1332       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1333                        xcape($room->user[$win]->name));
1334       if ($table->old_pnt == 120) {
1335         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1336       }
1337       else {
1338         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1339                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1340                           'pi&ugrave; di 60'), $table->old_pnt,
1341                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1342       }
1343     }
1344   }
1345   /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1346   if ($table->mazzo == $table_pos) 
1347     $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1348   else {
1349     $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1350     $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1351   }
1352
1353   if ($user->subst == 'asta') {
1354     if ($table->asta_win == -1)  // auction case
1355       $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1356     else 
1357       $curplayer = $table->asta_win;
1358   }
1359   else if ($user->subst == 'game') {
1360     $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1361   }
1362
1363   /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1364   if ($curplayer == $table_pos) {
1365     $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1366   }
1367   else {
1368     $unam = xcape($room->user[$table->player[$curplayer]]->name);
1369     $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1370   }
1371   
1372   if ($table->mult > 1) {
1373     $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult));
1374   }
1375   $noty .= "<hr><br>";
1376   $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1377   /* NOTE: show_notify($noty, 3000, "torna alla partita", 500, 
1378    *                   130 + ($table->points_n > 0 ? 50 : 0) + 
1379    *                   (120 * ($table->points_n / MAX_POINTS)));
1380    *       will be used when we refact notify js function following 
1381    *       photoo class logic 
1382    */ 
1383   
1384   return ($ret);
1385 }
1386
1387 function table_wellcome($user)
1388 {
1389   GLOBAL $table_wellarr, $G_lang;
1390   $ret = "";
1391
1392   for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1393     $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1394
1395   return ($ret);
1396 }
1397
1398
1399 function briscola_show($room, $table, $user)
1400 {
1401   GLOBAL $G_lang, $mlang_bin5_bin5;
1402   $ptnadd = "";
1403   $ret = "";
1404
1405   if ($table->asta_card == 9) 
1406     $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1407   
1408   /* text of caller cell */
1409   if ($user->table_pos == $table->asta_win) {
1410     $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1411     $ret .= sprintf($prestr, $ptnadd);
1412   }
1413   else {
1414     $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1415     $ret .= sprintf($prestr, 
1416                     xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1417   }
1418
1419   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1420                   $table->asta_win);
1421   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1422   $ret .= sprintf('$("caller").style.visibility = "visible";');
1423   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1424   $ret .= sprintf('$("astalascio").style.visibility = "";');
1425   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1426   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1427   
1428   return ($ret);
1429 }
1430
1431
1432 function game_result($asta_pnt, $pnt)
1433 {
1434   if ($asta_pnt == 61) {
1435     if ($pnt > 60)
1436       return (1);
1437     else if ($pnt == 60)
1438       return (0);
1439     else
1440       return (-1);
1441   }
1442   else {
1443     if ($pnt >= $asta_pnt)
1444       return (1);
1445     else
1446       return (-1);
1447   }
1448 }
1449
1450 ?>