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