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