calculate_points f() return points array
[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>$unam</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 &clone(&$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
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
414   function User() {
415   }
416
417   /* CREATE NOT USED
418   function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
419     GLOBAL $G_false;
420
421     if (($thiz =& new User()) == FALSE)
422       return ($G_false);
423
424     $thiz->asta_card = -2;
425     $thiz->asta_pnt  = -1;
426     $thiz->handpt = -1;
427     $thiz->exitislock = TRUE;
428
429     return ($thiz);
430   }
431   */
432
433   function parentcopy(&$from)
434   {
435     parent::copy($from);
436   }
437
438   function copy(&$from)
439   {
440     $this->parentcopy($from);
441
442     $this->asta_card  = $from->asta_card;
443     $this->asta_pnt   = $from->asta_pnt;
444     $this->handpt     = $from->handpt;
445     $this->exitislock = $from->exitislock;
446   }
447
448   /* CLONE NOT USED
449   function &clone(&$from)
450   {
451     GLOBAL $G_false;
452     
453     if (($thiz =& new User()) == FALSE)
454       return ($G_false);
455
456     $thiz->copy($from);
457
458     return ($thiz);
459   } 
460   */
461   
462   function &spawn(&$from, $table, $table_pos)
463   {
464     GLOBAL $G_false;
465     
466     if (($thiz =& new User_briskin5()) == FALSE)
467       return ($G_false);
468     
469     $thiz->parentcopy($from);
470
471     $thiz->asta_card = -2;
472     $thiz->asta_pnt  = -1;
473     $thiz->handpt = -1;
474     $thiz->exitislock = TRUE;
475
476     $thiz->table_orig = $table;
477     $thiz->table      = 0;
478     $thiz->table_pos  = $table_pos;
479
480     return ($thiz);
481   }
482 } // end class User_briskin5
483
484
485
486 class Briskin5 {
487   var $user;
488   var $table;
489   var $comm; // commands for many people
490   var $step; // current step of the comm array
491   var $garbage_timeout;
492   var $shm_sz;
493
494   var $table_idx;
495   var $table_token;
496
497   var $the_end;
498   var $tok;
499
500   function Briskin5 (&$room, $table_idx, $table_token) {
501     $this->user = array();
502     $this->table = array();
503
504     $this->the_end = FALSE;
505     $this->shm_sz = BRISKIN5_SHM_MIN;
506     if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
507       echo "FTOK FAILED";
508       exit;
509     }
510
511     $user  =& $room->user;
512     $table =& $room->table[$table_idx];
513
514     log_wr("Briskin5 constructor");
515
516     for ($i = 0 ; $i < $table->player_n ; $i++) {
517       $user[$table->player[$i]]->table_token = $table_token;
518       $this->user[$i] =& User_briskin5::spawn(&$user[$table->player[$i]], $table_idx, $i);
519     }
520     $this->table[0] =& Table_briskin5::spawn(&$table);
521
522     log_main("TABLE_OLD_WIN - Briskin5:".$this->table[0]->old_win);
523
524     $this->table_idx = $table_idx;
525     $this->table_token = $table_token;
526     $this->garbage_timeout = 0;
527     
528     log_wr("Briskin5 constructor end");
529   }
530
531
532   function &get_user($sess, &$idx)
533   {
534     GLOBAL $PHP_SELF, $G_false;
535
536     if (validate_sess($sess)) {
537       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
538         if (strcmp($sess, $this->user[$i]->sess) == 0) {
539           // find it
540           $idx = $i;
541           $ret = &$this->user[$i];
542           return ($ret);
543         }
544       }
545       log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
546       // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) 
547       // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
548     }
549     else {
550       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
551     }
552
553     return ($G_false);
554   }
555
556
557   function garbage_manager($force)
558   {
559     
560     /* Garbage collector degli utenti in timeout */
561     $ismod = FALSE;
562     $curtime = time();
563     if ($force || $this->garbage_timeout < $curtime) {
564       
565       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
566         $user_cur = &$this->user[$i];
567         if ($user_cur->sess == "" || 
568             ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
569           continue;
570         
571         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
572           log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
573
574           if ($user_cur->stat == 'table') {
575             log_auth($user_cur->sess," bin5 Autologout session.");
576
577             /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE 
578
579             $tmp_sess = $user_cur->sess;
580             $user_cur->sess = "";
581             step_unproxy($tmp_sess);
582             $user_cur->name = "";
583             $user_cur->the_end = FALSE;
584             
585             */
586
587             $this->table_wakeup(&$user_cur);
588           }
589         }
590       }
591       log_rd2($user_cur->sess." GARBAGE UPDATED!");
592       
593       $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
594       $ismod = TRUE;
595     }
596
597     return ($ismod);
598   }
599
600
601
602
603   //
604   //  static functions
605   //
606   function &load_data($table_idx, $table_token = "") 
607   {
608     GLOBAL $G_false, $sess;
609     $doexit = FALSE;
610     $shm = FALSE;
611
612     log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
613     
614     do {
615       if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
616         log_main("ftok failed");
617         $doexit = TRUE;
618         break;
619       }
620
621       if (($shm_sz = sharedmem_sz($tok)) == -1) {
622         log_main("shmop_open failed");
623         break;
624       }
625         
626       if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
627         break;
628
629       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
630         break;
631
632       if ($table_token != "" && $bri->table_token != $table_token) {
633         log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
634         
635         break;
636       }
637       $bri->tok = $tok;
638
639       shm_detach($shm);
640         
641       $ret = &$bri;
642       return ($ret); 
643     } while (FALSE);
644
645     if ($shm != FALSE)
646       shm_detach($shm);
647
648     log_wr("briskin5 load_data failed");
649     if ($doexit)
650       exit();
651     
652     return ($G_false);
653   }
654   
655
656
657   function save_data(&$bri) 
658   {
659     GLOBAL $sess;
660     
661     $ret =   FALSE;
662     $shm =   FALSE;
663     
664     log_main("SAVE BRISKIN5 DATA");
665     
666     if (!isset($bri->tok))
667       return (FALSE);
668     
669     while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
670       if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
671         break;
672       
673       if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
674         shm_detach($shm);
675         return (TRUE);
676       }
677       if (shm_remove($shm) === FALSE) {
678         log_only("REMOVE FALLITA");
679         break;
680       }
681       shm_detach($shm);
682       $bri->shm_sz += BRISKIN5_SHM_DLT;
683     } 
684
685     log_crit("save data failed!");
686
687     if ($shm)
688       shm_detach($shm);
689     
690     return ($ret);
691   }
692
693
694
695   function destroy_data($table_idx) 
696   {
697     GLOBAL $sess;
698
699     $ret =   FALSE;
700     $shm =   FALSE;
701     log_main("DESTROY BRISKIN5 DATA");
702     
703     do {
704       log_main("DESTROY2 BRISKIN5 DATA");
705
706       if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) 
707         break;
708
709       if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
710         break;
711       
712       if (shmop_delete($shm) == 0) {
713         log_only("REMOVE FALLITA ");
714         break;
715       }
716    
717       $shm = FALSE;
718       log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
719       
720       // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
721       $ret = TRUE;
722     } while (0);
723     
724     if ($shm)
725       shm_detach($shm);
726     
727     return ($ret);
728   }
729
730   function lock_data($table_idx)
731   {
732     GLOBAL $sess; 
733     
734     log_lock("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
735     //  echo "LOCK: ".FTOK_PATH."/main";
736     //  exit;
737     // WARNING monitor this step
738     if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
739       return (FALSE);
740     }
741     // WARNING monitor this step
742     if (($res = @sem_get($tok)) == FALSE) {
743       return (FALSE);
744     }
745     if (sem_acquire($res)) {   
746       log_lock("LOCK table ".$table_idx."[res: ".$res."]");
747       return ($res);
748     }
749     else {
750       log_lock("LOCK table ".$table_idx.":FAILED");
751       return (FALSE);
752     }
753   }
754   
755   function unlock_data($res)
756   {
757     GLOBAL $sess; 
758     
759     log_lock("UNLOCK table [res: ".$res."]");
760
761     return (sem_release($res));
762   }
763
764
765   function chatt_send(&$user, $mesg)
766   {
767     GLOBAL $mlang_brisk, $G_lang;
768
769     if ($user->stat == 'table') {
770       $table = &$this->table[$user->table];
771     }
772     
773     $user_mesg = substr($mesg,6);
774     
775     $curtime = time();
776     
777     $dt = date("H:i ", $curtime);
778     if (strncmp($user_mesg, "/nick ", 6) == 0) {
779       log_main($user->sess." chatt_send BEGIN");
780
781       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
782           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
783           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
784           $user->step_inc();
785
786           return;
787       }
788       $user_mesg = "COMMAND ".$user_mesg;
789       // Search dup name
790       // change
791       // update local graph
792       // update remote graphs
793       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
794         $user_cur = &$this->user[$i];
795         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
796         if ($user_cur->sess == '')
797           continue;
798         if ($user_cur->name == $name_new) {
799           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
800
801           $premsg = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
802           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $premsg);
803           $user->step_inc();
804           break;
805         }
806       }
807       if ($i == BRISKIN5_MAX_PLAYERS) {
808         if ($user->flags & USER_FLAG_AUTH && strcasecmp($user->name,$name_new) != 0) {
809           if ($this->table[$user->table]->auth_only == TRUE) {
810             $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
811             $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>%s</b>");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
812             $user->step_inc();
813             return;
814           }
815           else {
816             $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
817           }
818         }
819     
820         $user->name = $name_new;
821
822         log_main($user->sess." chatt_send start set");
823         
824
825         for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
826           log_main($user->sess." chatt_send set loop");
827           
828           $user_cur = &$this->user[$i];
829           if ($user_cur->sess == '')
830             continue;
831           
832           if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
833             $table = &$this->table[$user->table];
834             
835             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
836             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
837                       $this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->flags,
838                       xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
839
840                       $this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
841                       xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
842
843                       $this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
844                       xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
845
846                       (BRISKIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
847                       (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
848
849                       (BRISKIN5_PLAYERS_N == 3 ? 0:  $this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
850                       (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
851             if ($user_cur == $user) {
852               $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
853               $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
854               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', 
855                                                                    $itin, xcape($user->name,ENT_COMPAT,"UTF-8"), $itou);
856             }
857             $user_cur->step_inc();
858           }
859         }
860       }
861     }
862     else {
863       for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
864         if ($user->stat == 'room') {
865           $user_cur = &$this->user[$i];
866           if ($user_cur->sess == '' || $user_cur->stat != 'room')
867             continue;
868         }
869         else {
870           $user_cur = &$this->user[$table->player[$i]];
871         }
872         
873         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
874         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s", [%d, "%s"],"%s");',
875                                                              $dt, $user->flags, xcape($user->name), xcape($user_mesg));
876         $user_cur->step_inc();
877       }
878       log_legal($curtime, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
879     }
880   }
881
882   function table_wakeup(&$user)
883   {
884     $table = &$this->table[0];
885
886     log_main("BRISKIN5_WAKEUP begin function table  stat: ".$user->stat."  subst: ".$user->subst);
887
888     $curtime = time();
889
890     log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
891     
892     for ($i = 0 ; $i < $table->player_n ; $i++) {
893       $user_cur = &$this->user[$i];
894       log_main("PREIMPOST INLOOP name: ".$user_cur->name);
895       
896       if ($user_cur == $user)
897         $user_cur->subst = "shutdowner";
898       else
899         $user_cur->subst = "shutdowned";
900       $user_cur->laccwr = $curtime;
901
902       $ret = "gst.st = ".($user_cur->step+1)."; ";
903       $ret .= 'gst.st_loc++; the_end=true; window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
904
905       log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
906       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
907       $user_cur->step_inc();
908     }
909
910     $this->the_end = TRUE;
911   }
912   
913   /*
914    *  If all players are freezed the room garbage_manager clean up table and users.
915    */ 
916   function is_abandoned() 
917   {
918     $is_ab = TRUE;
919     $curtime = time();
920
921     $table = &$this->table[0];
922
923     for ($i = 0 ; $i < $table->player_n ; $i++) {
924       $user_cur = &$this->user[$i];
925
926       if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) { 
927         $is_ab = FALSE;
928         break;
929       }
930     }
931
932     return ($is_ab);
933   }
934 } // end class Briskin5
935
936 function locshm_exists($tok)
937 {
938   // return (TRUE);
939
940   if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
941     log_main($tok." SHM NOT exists");
942
943     return (FALSE);
944   }
945   else {
946     shmop_close($id);
947     log_main($tok." SHM exists");
948
949     return (TRUE);
950   }
951     
952 }
953
954 // rendiamo qui l'elenco dei punti come return della func
955 function calculate_points(&$table)
956 {
957   GLOBAL $G_all_points; 
958
959   $ret = array();
960   $pro = 0;
961
962   if ($table->asta_pnt == 60)
963     $table->asta_pnt = 61;
964
965   $table->old_reason = "";
966   $table->old_win = $table->asta_win;
967   $table->old_friend = $table->friend;
968   $table->old_asta_pnt = $table->asta_pnt;
969
970   for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
971     // for ($i = 0 ; $i < 40 ; $i++) {
972     $ctt = $table->card[$i]->value % 10;
973     $own = $table->card[$i]->owner;
974     if ($own == $table->asta_win || $own == $table->friend) 
975       $pro += $G_all_points[$ctt];
976   }
977
978   log_wr(sprintf("PRO: [%d]", $pro));
979
980   
981   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
982     $table->points[$table->points_n % MAX_POINTS] = array();
983     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) 
984       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
985     $table->points_n++;
986     $table->old_pnt = $pro;
987     $table->mult *= 2;
988
989     return;
990   }
991
992   if ($pro >= $table->asta_pnt) 
993     $sig = 1;
994   else
995     $sig = -1;
996
997   $table->points[$table->points_n % MAX_POINTS] = array();
998   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
999     if ($i == $table->asta_win) 
1000       $pt = ($i == $table->friend ? 4 : 2);
1001     else if ($i == $table->friend) 
1002       $pt = 1;
1003     else
1004       $pt = -1;
1005
1006     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1007
1008     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1009
1010     log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1011     
1012     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1013     $table->total[$i] += $pt;
1014     $ret[$i] = $pt;
1015   }
1016   $table->points_n++;
1017   $table->old_pnt = $pro;
1018   $table->mult = 1;
1019   
1020   return($ret);
1021 }
1022
1023 /* show table 
1024 is_transition (is from room to table ?)
1025 is_again      (is another game)
1026
1027 Examples                    of $is_transition, $is_again:
1028   from reload of the page:              FALSE, FALSE
1029   from sitdown in room:                  TRUE, FALSE
1030   from table: asta cmd e tutti passano:  TRUE, TRUE
1031   from table: fine partita:              TRUE, TRUE
1032  */
1033 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1034 {
1035   $table_idx = $user->table;
1036   $table = &$room->table[$table_idx];
1037   $table_pos = $user->table_pos;
1038
1039   $ret = "table_init();";
1040   $ret .= $table->exitlock_show(&$room->user, $table_pos);
1041   if (!$is_again) {
1042     /* GENERAL STATUS */
1043     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1044                      $sendstep, $user->stat, $user->subst, $table_pos);
1045
1046     log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;', $sendstep, $user->stat, $user->subst, $table_pos));
1047
1048     /* BACKGROUND */
1049     $ret .= "background_set();";
1050     
1051     /* USERS INFO */
1052     $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1053     $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1054
1055     $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1056     $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
1057                     $room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->flags,
1058                     xcape($room->user[$table->player[($table_pos) % BRISKIN5_PLAYERS_N]]->name),
1059
1060                     $room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->flags,
1061                     xcape($room->user[$table->player[($table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
1062
1063                     $room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->flags,
1064                     xcape($room->user[$table->player[($table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
1065
1066                     (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->flags),
1067                     (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
1068
1069                     (BRISKIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->flags),
1070                     (BRISKIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
1071   }
1072   /* NOTIFY FOR THE CARD MAKER */
1073   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1074     $ret .= show_table_info(&$room, &$table, $table_pos);
1075   }
1076   if (!$is_again) 
1077     $ret .= table_wellcome($user);
1078
1079   if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1080     $ret .= playsound("cow.mp3");
1081   }
1082
1083
1084   /* CARDS */
1085   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1086     $ret .= "|";
1087     
1088     for ($i = 0 ; $i < 8 ; $i++) {
1089       for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) {
1090         $ct = 0;
1091         for ($o = 0 ; $o < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) && $ct < $i+1 ; $o++) {
1092           // for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1093           if ($table->card[$o]->owner == (($e + $table->gstart) % BRISKIN5_PLAYERS_N)) {
1094             $ct++;
1095             if ($ct == $i+1)
1096               break;
1097           }
1098         }
1099         log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1100         
1101         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % BRISKIN5_PLAYERS_N, 
1102                          $i, ((($e + BRISKIN5_PLAYERS_N - $table_pos + $table->gstart) % BRISKIN5_PLAYERS_N) == 0 ?
1103                               $table->card[$o]->value : -1), 
1104                          ($i == 7 && $e == (BRISKIN5_PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1105       }
1106     }    
1107   }
1108   else {
1109     $taked  = array(0,0,0,0,0);
1110     $inhand = array(0,0,0,0,0);
1111     $ontabl  = array(-1,-1,-1,-1,-1);
1112     $cards  = array();
1113
1114     for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1115       // for ($i = 0 ; $i < 40 ; $i++) {
1116       if ($table->card[$i]->stat == 'hand') {
1117         if ($table->card[$i]->owner == $table_pos) {
1118           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1119         }
1120         $inhand[$table->card[$i]->owner]++;
1121       }
1122       else if ($table->card[$i]->stat == 'take') {
1123         log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1124         $taked[$table->card[$i]->owner]++;
1125       }
1126       else if ($table->card[$i]->stat == 'table') {
1127         $ontabl[$table->card[$i]->owner] = $i;
1128       }
1129     }
1130     $logg = "\n";
1131     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1132       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1133     }
1134     log_main("Stat table: ".$logg);
1135
1136     /* Set ours cards. */
1137     $oursarg = "";
1138     for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
1139       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1140     for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
1141       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1142     $ret .= sprintf('card_setours(%s);', $oursarg);
1143
1144     /* Dispose all cards */
1145     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1146       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1147       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1148                       ($inhand[$i] <= 8 ? $inhand[$i] : 8)  , $taked[$i]);
1149
1150       if ($ontabl[$i] != -1) {
1151         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1152                         $table->card[$ontabl[$i]]->value, 
1153                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1154       }
1155     }
1156   }
1157
1158   /* Show auction */
1159   if ($user->subst == 'asta') {
1160
1161     /* show users auction status */
1162     $showst = "";
1163     for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1164       $user_cur = &$room->user[$table->player[$i]];
1165       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1166                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1167     }
1168     if (BRISKIN5_PLAYERS_N == 3)
1169         $showst .= ",-2,-2";
1170     $ret .= sprintf('show_astat(%s);', $showst);
1171
1172     if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1173       /* show card chooser */
1174       $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1175                       $table->asta_card);
1176     }
1177     else {
1178       /* show auction */
1179       if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N) &&
1180           $table->asta_win == -1) 
1181         $ret .= sprintf('dispose_asta(%d,%d, %s);', 
1182                         $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1183       else
1184         $ret .= sprintf('dispose_asta(%d,%d, %s);',
1185                         $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1186     }
1187
1188     /* Remark */
1189     if ($table->asta_win == -1) { // auction case
1190       if ($table_pos == ($table->gstart % BRISKIN5_PLAYERS_N)) 
1191         $ret .= "remark_on();";
1192       else
1193         $ret .= "remark_off();";
1194     }
1195     else { // chooseed case
1196       if ($table_pos == $table->asta_win) 
1197         $ret .= "remark_on();";
1198       else
1199         $ret .= "remark_off();";
1200     }
1201   }
1202   else if ($user->subst == 'game') {
1203     /* HIGHLIGHT */
1204     if (($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N == $table_pos) 
1205       $ret .= "is_my_time = true; remark_on();";
1206     else
1207       $ret .= "remark_off();";
1208     
1209     /* WHO CALL AND WATH */
1210     $ret .= briscola_show($room, $table, $user);
1211     
1212   }
1213   return ($ret);
1214 } // end function show_table(...
1215
1216 function calculate_winner(&$table)
1217 {
1218   $briontab = FALSE;
1219   $ontab = array();
1220   $ontid = array();
1221   $cur_win  =  -1;
1222   $cur_val  = 100;
1223   $cur_seed = $table->briscola - ($table->briscola % 10);
1224
1225   for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
1226     // for ($i = 0 ; $i < 40 ; $i++) {
1227     if ($table->card[$i]->stat != "table")
1228       continue;
1229
1230     log_wr(sprintf("Card On table: [%d]", $i));
1231
1232     $v = $table->card[$i]->value; 
1233     $ontab[$table->card[$i]->owner] = $v;
1234     $ontid[$table->card[$i]->owner] = $i;
1235     /* se briscola setto il flag */
1236     if (($v - ($v % 10)) == $cur_seed)
1237       $briontab = TRUE;
1238   }
1239
1240   if ($briontab == FALSE) {
1241     $cur_win  = $table->gstart;
1242     $cur_val  = $ontab[$cur_win];
1243     $cur_seed = $cur_val - ($cur_val % 10);
1244   }
1245
1246   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1247     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1248       if ($ontab[$i] < $cur_val) {
1249         $cur_val = $ontab[$i];
1250         $cur_win = $i;
1251       }
1252     }
1253   }
1254
1255   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) {
1256     $table->card[$ontid[$i]]->owner = $cur_win;
1257     $table->card[$ontid[$i]]->stat =  "take"; // Card stat
1258   }
1259   return ($cur_win);
1260 }
1261
1262 function show_table_info(&$room, &$table, $table_pos)
1263 {
1264   GLOBAL $G_lang, $mlang_bin5_bin5;
1265
1266   $ret = "";
1267   $user = &$room->user[$table->player[$table_pos]];
1268
1269   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1270   $noty = sprintf('<table class=\"points\"><tr><th></th>');
1271   
1272   // Names.
1273   for ($i = 0 ; $i < BRISKIN5_PLAYERS_N ; $i++) 
1274     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1275   $noty .= sprintf("</tr>");
1276
1277   // Points.
1278   log_main("show_table_info: pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1279
1280   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1281     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1282     for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) 
1283       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1284     $noty .= "</tr>";
1285   }
1286
1287   // Total points.
1288   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1289   for ($e = 0 ; $e < BRISKIN5_PLAYERS_N ; $e++) 
1290     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1291   $noty .= "</tr></table>";
1292
1293   if ($table->old_reason != "") {
1294     $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1295   }
1296
1297   if ($table->old_win != -1) {
1298     log_main("TABLE_OLD_WIN:".$table->old_win);
1299     $win = $table->player[$table->old_win];
1300     log_main("TABLE_OLD_FRIEND:".$table->old_friend);
1301     $fri = $table->player[$table->old_friend];
1302
1303     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1304
1305     if ($win != $fri) {
1306       /* 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" */
1307       
1308       $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang], 
1309                        xcape($room->user[$win]->name),
1310                        xcape($room->user[$fri]->name));
1311       if ($table->old_pnt == 120) {
1312         $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
1313       }
1314       else {
1315         $noty .= sprintf($mlang_bin5_bin5['info_pnt'][$G_lang],
1316                          ($table->old_asta_pnt > 61 ? $mlang_bin5_bin5['info_alea'][$G_lang].$table->old_asta_pnt :
1317                           $mlang_bin5_bin5['info_more'][$G_lang]), $table->old_pnt,
1318                          ($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])));
1319       }
1320     }
1321     else {
1322       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1323                        xcape($room->user[$win]->name));
1324       if ($table->old_pnt == 120) {
1325         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1326       }
1327       else {
1328         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1329                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1330                           'pi&ugrave; di 60'), $table->old_pnt,
1331                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1332       }
1333     }
1334   }
1335   /* MLANG: "Fai <b>tu</b> il mazzo,", "Il mazzo a <b>$unam</b>," */
1336   if ($table->mazzo == $table_pos) 
1337     $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
1338   else {
1339     $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1340     $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
1341   }
1342
1343   if ($user->subst == 'asta') {
1344     if ($table->asta_win == -1)  // auction case
1345       $curplayer = $table->gstart % BRISKIN5_PLAYERS_N;
1346     else 
1347       $curplayer = $table->asta_win;
1348   }
1349   else if ($user->subst == 'game') {
1350     $curplayer = ($table->gstart + $table->turn) % BRISKIN5_PLAYERS_N;
1351   }
1352
1353   /* MLANG: " tocca a <b>te</b> giocare.", " tocca a <b>$unam</b> giocare.", " La partita vale <b>%s</b>.", "torna alla partita" */
1354   if ($curplayer == $table_pos) {
1355     $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
1356   }
1357   else {
1358     $unam = xcape($room->user[$table->player[$curplayer]]->name);
1359     $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
1360   }
1361   
1362   if ($table->mult > 1) {
1363     $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult));
1364   }
1365   $noty .= "<hr><br>";
1366   $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
1367   /* NOTE: show_notify($noty, 3000, "torna alla partita", 500, 
1368    *                   130 + ($table->points_n > 0 ? 50 : 0) + 
1369    *                   (120 * ($table->points_n / MAX_POINTS)));
1370    *       will be used when we refact notify js function following 
1371    *       photoo class logic 
1372    */ 
1373   
1374   return ($ret);
1375 }
1376
1377 function table_wellcome($user)
1378 {
1379   GLOBAL $table_wellarr, $G_lang;
1380   $ret = "";
1381
1382   for ($i = 0 ; $i < count($table_wellarr[$G_lang]) ; $i++)
1383     $ret .= sprintf('chatt_sub("%s", [2, "ChanServ: "],"%s");', "", str_replace('"', '\"', $table_wellarr[$G_lang][$i]));
1384
1385   return ($ret);
1386 }
1387
1388
1389 function briscola_show($room, $table, $user)
1390 {
1391   GLOBAL $G_lang, $mlang_bin5_bin5;
1392   $ptnadd = "";
1393   $ret = "";
1394
1395   if ($table->asta_card == 9) 
1396     $ptnadd = sprintf($mlang_bin5_bin5['call_wptn'][$G_lang], $table->asta_pnt);
1397   
1398   /* text of caller cell */
1399   if ($user->table_pos == $table->asta_win) {
1400     $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_ycall'][$G_lang]);
1401     $ret .= sprintf($prestr, $ptnadd);
1402   }
1403   else {
1404     $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
1405     $ret .= sprintf($prestr, 
1406                     xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1407   }
1408
1409   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1410                   $table->asta_win);
1411   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1412   $ret .= sprintf('$("caller").style.visibility = "visible";');
1413   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1414   $ret .= sprintf('$("astalascio").style.visibility = "";');
1415   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1416   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1417   
1418   return ($ret);
1419 }
1420
1421
1422 function game_result($asta_pnt, $pnt)
1423 {
1424   if ($asta_pnt == 61) {
1425     if ($pnt > 60)
1426       return (1);
1427     else if ($pnt == 60)
1428       return (0);
1429     else
1430       return (-1);
1431   }
1432   else {
1433     if ($pnt >= $asta_pnt)
1434       return (1);
1435     else
1436       return (-1);
1437   }
1438 }
1439
1440 ?>