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