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