alert command added
[brisk.git] / web / Obj / brisk.phh
1 <?php
2 /*
3  *  brisk - brisk.phh
4  *
5  *  Copyright (C) 2006-2008 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22
23 define(BRISK_CONF, "brisk.conf.pho");
24 define(FTOK_PATH, "/var/lib/brisk");
25 define(LEGAL_PATH, "/tmp/legal_brisk");
26 define(PROXY_PATH, "/var/lib/brisk_proxy");
27 define(TABLES_N, 24);
28 define(PLAYERS_N, 3);
29 define(MAX_POINTS, 5);
30 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
31 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
32 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
33 define(SHM_DIMS_DLT, 65536);
34  
35 define(COMM_N, 18);
36 define(COMM_GEN_N, 50);
37
38 define(CHAT_N, 3);
39 define(CHAT_ILL_TIME, 6);
40
41 define(SESS_LEN, 13);
42 define(STREAM_TIMEOUT, 20);
43 define(EXPIRE_TIME_RD, 180);
44 define(EXPIRE_TIME_SMAMMA, 360); 
45 // BAN_TIME da allineare anche in commons.js
46 define(BAN_TIME, 900); 
47 define(GARBAGE_TIMEOUT, 10);
48 define(NICKSERV, "<i>BriskServ</i>");
49
50 define(DBG_ONL2, 0x0001);
51 define(DBG_ONLY, 0x0002);
52 define(DBG_MAIN, 0x0004);
53 define(DBG_READ, 0x0008);
54 define(DBG_REA2, 0x0010);
55 define(DBG_SEND, 0x0020);
56 define(DBG_LOCK, 0x0040);
57 define(DBG_WRIT, 0x0080);
58 define(DBG_LOAD, 0x0100);
59 define(DBG_AUTH, 0x0200);
60 define(DBG_CRIT, 0x0400);
61
62 define(BRISK_DEBUG, 0);
63
64 define(BRISK_SINGLE_DEBUG,0);
65 define(BRISK_SINGLE_SESS, "");
66 // define(DEBUGGING, "local");
67
68 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
69
70
71 $G_false = FALSE;
72
73 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
74 $G_brisk_version = "spawn-0.12.0";
75
76 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: aggiunta messaggistica a tutti i tavoli - versione beta2.',
77                         'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
78 $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.');
79
80
81 $G_room_help= '
82 <div style=\\"text-align: left; padding: 8px;\\">
83 <b>Descrizione</b><br>
84 Questa &egrave; un\'implementazione della briscola in cinque, cos&igrave; come &egrave; spiegata su
85 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve &egrave; la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
86 <b>Configurazione del browser.</b><br>
87 Occorre abilitare i cookies.<br>
88 <br>
89 <b>Uso del sito</b><br>
90 Potete sedervi a un tavolo o rimanere in piedi.<br>
91 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
92 <br>
93 <b>Partita</b><br>
94 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sar&agrave; il vostro turno, potrete scegliere se andare avanti o passare cliccando sulle icone corrispondenti. Se si arriva ai punti, scrivete nella textbox il vostro rilancio e cliccate PUNTI.<br><br>
95 Chi vince l\'asta dovr&agrave; decidere il seme della carta scelta e inizier&agrave; la mano.<br>
96 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
97 Il vostro turno &egrave; sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
98 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
99 Dopo che &egrave; iniziata una partita per uscirne dovete chiedere agli altri giocatori di sbloccarla cliccando sul lucchetto. Se non si segue questa prassi, una volta usciti, non vi potrete sedere a nessun tavolo per '.floor(BAN_TIME/60).' minuti.
100 <dl>
101 <dt><b>Comandi della chat</b>
102 <dd><b>/nick <i>&lt;nuovo_nickname&gt;</i></b> - cambio di nickname
103 <dd>.. to be continue ..
104 </dl>
105 </div>
106 ';
107
108 $G_room_about= '<br>
109 <div id=\\"header\\" class=\\"header\\">
110   <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
111   briscola chiamata in salsa ajax
112 </div>
113 <br><b>version '.$G_brisk_version.'</b><br><br>
114 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
115
116
117 function xcape($s)
118 {
119   $from = array (   '\\',     '@',        '|' );
120   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
121
122   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
123 }
124
125
126 class Card {
127   var $value; /* 0 - 39 card value */
128   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
129   var $owner; /* (table position 0-4) */
130   // var $pos;   /* Pos in hand. */
131   var $x;     /* When played the X position on the table of the owner. */
132   var $y;     /* When played the Y position on the table of the owner. */
133
134   function Card($value, $stat, $owner)
135   {
136     $this->value = $value;
137     $this->stat  = $stat; // Card stat
138     $this->owner = $owner;
139   }
140
141   function assign($stat,$owner)
142   {
143     $this->stat  = $stat; // Card stat
144     $this->owner = $owner;
145   }
146
147   function setpos($pos)
148   {
149     $this->pos   = $pos;
150   }
151
152   function play($x,$y)
153   {
154     $this->stat = 'table'; // Card stat
155     $this->x = $x;
156     $this->y = $y;
157   }
158
159   function take($newown)
160   {
161     $this->stat = 'take'; // Card stat
162     $this->owner = $newown;
163   }
164 } // end class Card
165
166 class Table {
167   var $player;
168   var $player_n;
169   var $card;
170   var $mazzo;
171   var $gstart;
172   var $turn;
173
174   var $asta_pla;
175   var $asta_pla_n;
176   var $asta_card;
177   var $asta_pnt;
178   
179   var $mult;
180   var $points;    // points array
181   var $points_n;  // number of row of points
182   var $total;
183
184   var $asta_win;
185   var $briscola;
186   var $friend;
187   
188   var $old_reason;
189   var $old_asta_pnt;
190   var $old_pnt;
191   var $old_win;
192   var $old_friend;
193
194   var $table_token;
195   var $table_start;
196
197   function Table() 
198   {
199   }
200   
201   function &create() 
202   {
203     GLOBAL $G_false;
204
205     if (($thiz =& new Table()) == FALSE)
206       return ($G_false);
207
208     $thiz->player    =   array();
209     $thiz->player_n  =   0;
210     $thiz->card      =   FALSE;
211     $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
212     $thiz->asta_pla_n=  -1;
213     $thiz->asta_card =  -1;
214     $thiz->asta_pnt  =  -1;
215     $thiz->mult      =   1;
216     $thiz->points    =   array( );
217     $thiz->points_n  =   0;
218     $thiz->total     =   array( 0, 0, 0, 0, 0);
219     $thiz->asta_win  =  -1;
220     $thiz->briscola  =  -1;
221     $thiz->friend    =  -1;
222     $thiz->turn      =   0;
223     $thiz->old_reason   = "";
224     $thiz->old_asta_pnt = -1;
225     $thiz->old_pnt      = -1;
226     $thiz->old_win      = -1;
227     $thiz->old_friend   = -1;
228
229     $thiz->table_token  = "";
230     $thiz->table_start  = 0;
231
232     return ($thiz);
233   }
234
235   function &clone(&$from)
236   {
237     GLOBAL $G_false;
238     
239     if (($thiz =& new Table()) == FALSE)
240       return ($G_false);
241     
242     $thiz->player = array();
243     for ($i = 0 ; $i < $from->player_n ; $i++)
244       $thiz->player[$i] = $from->player[$i];
245     $thiz->player_n = $from->player_n;
246     $thiz->card = $from->card;
247     $thiz->mazzo = $from->mazzo; // REVIEW
248     $thiz->gstart = $from->gstart;
249     $thiz->turn = $from->turn;
250
251     $thiz->asta_pla = $from->asta_pla;
252     $thiz->asta_pla_n = $from->asta_pla_n;
253     $thiz->asta_card = $from->asta_card;
254     $thiz->asta_pnt = $from->asta_pnt;
255     
256     $thiz->mult = $from->mult;
257     $thiz->points = $from->points;
258     $thiz->points_n = $from->points_n;
259     $thiz->total = $from->total;
260     
261     $thiz->asta_win = $from->asta_win;
262     $thiz->briscola = $from->briscola;
263     $thiz->friend = $from->friend;
264     
265     $thiz->old_reason = $from->old_reason;
266     $thiz->old_asta_pnt = $from->old_asta_pnt;
267     $thiz->old_pnt = $from->old_pnt;
268     $thiz->old_win = $from->old_win;
269     $thiz->old_friend = $from->old_friend;
270
271     $thiz->table_token  = $from->table_token;
272     $thiz->table_start  = $from->table_start;
273
274     return ($thiz);
275   }
276   
277   function &spawn(&$from)
278   {
279     GLOBAL $G_false;
280     
281     if (($thiz =& new Table()) == FALSE)
282       return ($G_false);
283     
284     $thiz->player_n = $from->player_n;
285     $thiz->card = &$thiz->bunch_create();
286     $thiz->mazzo = $from->mazzo;
287     $thiz->gstart = $from->gstart;
288     $thiz->turn = $from->turn;
289
290     $thiz->asta_pla = $from->asta_pla;
291     $thiz->asta_pla_n = $from->asta_pla_n;
292     $thiz->asta_card = $from->asta_card;
293     $thiz->asta_pnt = $from->asta_pnt;
294     
295     $thiz->mult = $from->mult;
296     $thiz->points = $from->points;
297     $thiz->points_n = $from->points_n;
298     $thiz->total = $from->total;
299     
300     $thiz->asta_win = $from->asta_win;
301     $thiz->briscola = $from->briscola;
302     $thiz->friend = $from->friend;
303     
304     $thiz->old_reason = $from->old_reason;
305     $thiz->old_asta_pnt = $from->old_asta_pnt;
306     $thiz->old_pnt = $from->old_pnt;
307     $thiz->old_win = $from->old_win;
308     $thiz->old_friend = $from->old_friend;
309
310     // players are rearranged in an dedicated array
311     $thiz->player = array();
312     for ($i = 0 ; $i < $from->player_n ; $i++)
313       $thiz->player[$i] = $i;
314
315     $thiz->table_token  = $from->table_token;
316     $thiz->table_start  = $from->table_start;
317
318     return ($thiz);
319   }
320   
321   function &bunch_create()
322   {
323     $ret = array();
324
325     for ($i = 0 ; $i < 40 ; $i++) {
326       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
327     }
328
329     $oret = &$ret;
330     return ($oret);
331   }
332
333   function bunch_make()
334   {
335     $ct = array(0,0,0,0,0);
336     
337     mt_srand(make_seed());
338     
339     for ($i = 39 ; $i >= 0 ; $i--) 
340       $rest[$i] = $i;
341
342     for ($i = 39 ; $i >= 0 ; $i--) {
343       $rn = rand(0, $i);
344       
345       if ($rn == 0)
346         log_main("RND ZERO");
347       
348       $id = $rest[$rn];
349
350       $owner = $i % 5;
351       $this->card[$id]->assign('hand', $owner);
352
353       $rest[$rn] = $rest[$i];
354       // $pubbpos[$rn2] = $pubbpos[$i];
355     }
356   }
357
358   function init(&$userarr)
359   {
360     $this->mazzo    = rand(0,PLAYERS_N-1);
361     $this->points_n = 0;
362     $this->mult     = 1;
363     $this->old_win  =-1;
364     $this->old_reason = "";
365     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
366       $this->total[$i] = 0;
367       $user_cur = &$userarr[$this->player[$i]];
368       $user_cur->exitislock = TRUE;
369     }
370
371     log_main("table::init: ci siamo");
372   }
373
374   function game_init(&$userarr)
375   {
376     log_rd2("GSTART 4");
377
378     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
379     $this->bunch_make();
380     
381     
382     $this->asta_pla_n = PLAYERS_N;
383     $this->asta_card = -1;
384     $this->asta_pnt  = 60;
385     $this->asta_win  = -1;
386     $this->briscola  = -1;
387     $this->friend    = -1;
388     $this->turn      =  0;
389     
390     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
391       $this->asta_pla[$i] = TRUE;
392       $user_cur = &$userarr[$this->player[$i]];
393       $user_cur->subst = 'asta';
394       $user_cur->asta_card = -2;
395       $user_cur->asta_pnt  = -1;
396       $user_cur->handpt = $this->hand_points($i);
397       // SEE function calculate_points(&$table)
398     }
399   }
400
401   function game_next()
402   {
403     $this->mazzo  = ($this->mazzo + 1) % PLAYERS_N;
404   }
405
406   function getPlayer($idx)
407   {
408     return ($this->player[$idx]);
409   }
410
411   function setPlayer($idx, $player)
412   {
413     $this->player[$idx] = $player;
414   }
415
416   function user_add($idx)
417   {
418     $this->player[$this->player_n] = $idx;
419     $this->player_n++;
420     
421     return ($this->player_n - 1);
422   }
423   
424   function user_rem(&$room, &$user)
425   {
426     $tabpos = $user->table_pos;
427     
428     /* verifico la consistenza dei dati */
429     if ($room->user[$this->player[$tabpos]] == $user) {
430       
431       /* aggiorna l'array dei giocatori al tavolo. */
432       for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
433         $this->player[$i] = $this->player[$i+1];
434         $user_cur = &$room->user[$this->player[$i]];
435         $user_cur->table_pos = $i;
436       }
437       $this->player_n--;
438     }
439     else {
440       log_main("INCONSISTENCY ON TABLE.");
441     }
442   }
443
444   function hand_points($idx)
445   {
446     GLOBAL $G_all_points; 
447     
448     $tot = 0;
449     
450     for ($i = 0 ; $i < 40 ; $i++) {
451       if ($this->card[$i]->owner != $idx)
452         continue;
453
454       $ctt = $this->card[$i]->value % 10;
455       $tot += $G_all_points[$ctt];
456     }
457
458     return ($tot);
459   }
460
461   function exitlock_show(&$userarr, $table_pos)
462   {
463     $ct = $this->exitlock_calc(&$userarr, $table_pos);
464
465     $ret = sprintf('exitlock_show(%d, %s);', $ct, 
466                    ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
467     return ($ret);
468   }
469
470   function exitlock_calc(&$userarr, $table_pos)
471   {
472     $ct = 0;
473
474     for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {    
475       if ($userarr[$this->player[$i]]->exitislock == FALSE)
476         $ct++;
477     }
478
479     return ($ct);
480   }
481 } // end class Table
482   
483 class User {
484   var $name;       // name of the user
485   var $sess;       // session of the user
486   var $ip;         // ip of the user
487   var $lacc;       // last access (for the cleanup)
488   var $laccwr;     // last access (for the cleanup)
489   var $bantime;    // timeout to temporary ban
490   var $stat;       // status (outdoor, room, table, game, ...)
491   var $subst;      // substatus for each status   
492   var $step;       // step of the current status
493   var $trans_step; // step to enable transition between pages (disable == -1)
494   var $comm;       // commands array
495   var $asta_card;  // 
496   var $asta_pnt;   //
497   var $handpt;     // Total card points at the beginning of the current hand.
498   var $exitislock; // Player can exit from the table ?
499   var $table;      // id of the current table (if in table state)
500   var $table_pos;  // idx on the table
501   var $table_token;// token that identify a game on a table
502   var $the_end;    // Flag to change the end of the session
503
504   var $chat_lst;      // Last chat line
505   var $chattime;      // Array of chat times
506   var $chat_cur;      // Current chat line number
507   var $chat_ban;      // Time for ban chat
508   var $chat_dlt;      // Delta t for ban
509   function User() {
510   }
511
512   function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
513     GLOBAL $G_false;
514
515     if (($thiz =& new User()) == FALSE)
516       return ($G_false);
517
518     $thiz->name  = $name;
519     $thiz->sess  = $sess;
520     $thiz->ip    = $ip;
521     $thiz->lacc   = time();
522     $thiz->laccwr = time();
523     $thiz->bantime = 0;
524     $thiz->stat  = $stat;
525     $thiz->subst  = $subst;
526     $thiz->step  = 1;
527     $thiz->trans_step  = -1;
528     $thiz->comm  = array();
529     $thiz->asta_card = -2;
530     $thiz->asta_pnt  = -1;
531     $thiz->handpt = -1;
532     $thiz->exitislock = TRUE;
533     
534     $thiz->chattime = array_fill(0, CHAT_N, 0);
535     $thiz->chat_cur = 0;
536     $thiz->chat_lst = "";
537     $thiz->chat_ban = 0;
538     $thiz->chat_dlt = 0;
539
540     $thiz->table = $table;
541     $thiz->table_pos = -1;
542     $thiz->table_token = "";
543
544     return ($thiz);
545   }
546
547   function &clone(&$from)
548   {
549     GLOBAL $G_false;
550     
551     if (($thiz =& new User()) == FALSE)
552       return ($G_false);
553     
554     $thiz->name       = $from->name;
555     $thiz->sess       = $from->sess;
556     $thiz->ip         = $from->ip;
557     $thiz->lacc       = $from->lacc;
558     $thiz->laccwr     = $from->laccwr;
559     $thiz->bantime    = $from->bantime;
560     $thiz->stat       = $from->stat;
561     $thiz->subst      = $from->subst;
562     $thiz->step       = $from->step;
563     $thiz->trans_step = $from->trans_step;
564     $thiz->comm       = array();
565
566     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
567     for ($i = $i_start ; $i < $from->step ; $i++) {
568       $ii = $i % COMM_N;
569       $thiz->comm[$ii] = $from->comm[$ii];
570     }
571     $thiz->asta_card  = $from->asta_card;
572     $thiz->asta_pnt   = $from->asta_pnt;
573     $thiz->handpt     = $from->handpt;
574     $thiz->exitislock = $from->exitislock;
575
576     $thiz->chattime = array();
577     for ($i = 0 ; $i < CHAT_N ; $i++)
578       $thiz->chattime[$i] = $from->chattime[$i];
579     $thiz->chat_cur = $from->chat_cur;
580     $thiz->chat_lst = $from->chat_lst;
581     $thiz->chat_ban = $from->chat_ban;
582     $thiz->chat_dlt = $from->chat_dlt;
583
584     $thiz->table      = $from->table;
585     $thiz->table_pos  = $from->table_pos;
586     $thiz->table_token = $from->table_token;
587     $thiz->the_end    = $from->the_end;
588
589     return ($thiz);
590   }
591   
592   function &spawn(&$from, $table, $table_pos)
593   {
594     GLOBAL $G_false;
595     
596     if (($thiz =& new User()) == FALSE)
597       return ($G_false);
598     
599     $thiz->name       = $from->name;
600     $thiz->sess       = $from->sess;
601     $thiz->ip         = $from->ip;
602     $thiz->lacc       = $from->lacc;
603     $thiz->laccwr     = $from->laccwr;
604     $thiz->bantime    = $from->bantime;
605     $thiz->stat       = $from->stat;
606     $thiz->subst      = $from->subst;
607     $thiz->step       = $from->step;
608     $thiz->trans_step = $from->trans_step;
609     $thiz->comm       = array();
610
611     /*
612     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
613     for ($i = $i_start ; $i < $from->step ; $i++) {
614       log_wr("TRY PUSH:".$i);
615       $ii = $i % COMM_N;
616       $thiz->comm[$ii]   = $from->comm[$ii];
617     }
618     */
619     $thiz->asta_card  = $from->asta_card;
620     $thiz->asta_pnt   = $from->asta_pnt;
621     $thiz->handpt     = $from->handpt;
622     $thiz->exitislock = $from->exitislock;
623     $thiz->the_end    = $from->the_end;
624
625     $thiz->chattime   = array_fill(0, CHAT_N, 0);
626     $thiz->chat_cur   = 0;
627     $thiz->chat_lst   = "";
628     $thiz->chat_ban   = 0;
629     $thiz->chat_dlt   = 0;
630
631     $thiz->table      = $table;
632     $thiz->table_pos  = $table_pos;
633     $thiz->table_token = $from->table_token;
634
635     return ($thiz);
636   }
637   
638   function stat_set($stat) {
639     log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]"); 
640     $this->stat = "$stat";
641     
642     /*
643     if (validate_sess($this->sess)) {
644       $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
645       fwrite($fp, sprintf("%s\n",$this->stat));
646       fclose($fp);
647     }
648     */
649   }
650
651   function step_set($step) {
652     $this->step = $step;
653     
654     do {
655       if (validate_sess($this->sess) == FALSE)
656         break;
657       if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
658         break;
659       fwrite($fp, pack("l",$this->step), 4);
660       fclose($fp);
661
662       return (TRUE);
663     } while (0);
664
665     return (FALSE);
666   }
667
668   function step_inc($delta = 1) {
669     $this->step += $delta;
670     
671     if (validate_sess($this->sess)) {
672       $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
673       fwrite($fp, pack("l",$this->step), 4);
674       fclose($fp);
675
676       return (TRUE);
677     }
678     
679     return (FALSE);
680   }
681
682   function reset() {
683     $tmp_sess = $this->sess;
684     $this->sess = "";
685     step_unproxy($tmp_sess);
686     $this->name = "";
687     while (array_pop($this->comm) != NULL);
688     $this->step = 0;
689     $this->the_end = FALSE;
690   }
691 } // end class User
692
693
694 function step_get($sess) {
695   $fp = FALSE;
696   $ct = 0;
697   do {
698     if (validate_sess($sess) == FALSE)
699       break;
700   $ct = 1;
701     if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
702       break;
703   $ct = 2;
704     if (($s = fread($fp, 4)) == FALSE)
705       break;
706   $ct = 3;
707     if (strlen($s) != 4)
708       break;
709   $ct = 4;
710     $arr = unpack('l', $s);
711     fclose($fp);
712
713     // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
714     return ($arr[1]);
715   } while (0);
716
717   if ($fp != FALSE)
718     fclose($fp);
719
720   log_rd2("STEP_GET: return false ".$ct);
721   return (FALSE);
722 }
723
724 function step_unproxy($sess) {
725   log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
726   @unlink(PROXY_PATH."/".$sess.".step");
727 }
728
729
730 class Room {
731   var $user;
732   var $table;
733   var $comm; // commands for many people
734   var $step; // current step of the comm array
735   var $garbage_timeout;
736   var $shm_sz;
737
738   function Room () {
739     $this->user = array();
740     $this->table = array();
741
742     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
743       $this->user[$i] =& User::create("", "");
744     }
745
746     for ($i = 0 ; $i < TABLES_N ; $i++) 
747       $this->table[$i] =& Table::create();
748     $this->garbage_timeout = 0;
749   }
750
751   function garbage_manager($force)
752   {
753     $ismod = FALSE;
754
755     log_rd2("garbage_manager START");
756
757     /* Garbage collector degli utenti in timeout */
758     $curtime = time();
759     if ($force || $this->garbage_timeout < $curtime) {
760       
761       // Before all align times with table timeout
762       for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
763         $table_cur =& $this->table[$table_idx];
764         // if the table is complete and exists its shared mem we get the info about users lacc
765
766         if ($table_cur->player_n == PLAYERS_N) {
767           log_main("PLAYERS == N TABLE ".$table_idx);
768           
769           if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
770             log_main("bin5 lock data success");
771             
772             $no_recovery = FALSE;
773             if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
774               if ($table_cur->table_token != $bri->table_token) {
775                 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
776                 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start."  Table: ".$bri->table_start);
777                 $no_recovery = TRUE;
778                 $bri = FALSE;
779               }
780             }
781             
782             if ($bri != FALSE) {
783               //
784               //  SPAWN: JOIN
785               //
786               log_main("garbage_manager: bri loaded successfully.");
787               $bri->garbage_manager(TRUE);
788               
789               $bri_table = &$bri->table[0];
790
791               // is the end of the table
792               if ($bri->the_end == TRUE) {
793                 /*
794                  *  DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
795                  */
796                 log_main("garbage_manager: INSIDE THE END.");
797
798                 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
799                   // stat must be "table" by definition
800                   $user_cur =& $this->user[$table_cur->player[$i]];
801                   $bri_user =& $bri->user[$i];
802                   
803                   $user_cur->subst      = $bri_user->subst;
804                   $user_cur->step       = $bri_user->step;
805                   $user_cur->lacc       = $bri_user->lacc;
806                   $user_cur->laccwr     = $bri_user->lacc;
807                   $user_cur->bantime    = $bri_user->bantime;
808                 }
809               
810                 $this->room_join_wakeup(&$user_cur, FALSE, 0); 
811                 $table_cur->table_token = "";
812
813                 Briskin5::destroy_data($table_idx);
814               }
815               else {
816                 log_main("gm:: save_data");
817
818                 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
819                   $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
820                 }
821               
822                 Briskin5::save_data(&$bri);
823               }
824             } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
825             else if ($no_recovery == FALSE) {
826               log_crit("ERROR: table ".$table_idx." unrecoverable join");
827
828               for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
829                 $user_cur = &$this->user[$table_cur->player[$i]];
830                 $user_cur->subst = "shutdowner";
831                 $user_cur->step_inc();
832                 
833                 $ret = sprintf('stat = "%s"; subst = "%s";',  $user_cur->stat, $user_cur->subst);
834                 $ret .= "gst.st = ".($user_cur->step+1)."; ";
835                 $ret .= show_notify("<br>I dati del tavolo n&deg; ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>", 2000, "Chiudi.", 400, 110);
836                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
837                 $user_cur->step_inc();
838               }
839
840               $this->room_join_wakeup(&$user_cur, TRUE, -2); 
841               $table_cur->table_token = "";
842             }
843
844             Briskin5::unlock_data($sem);
845           } // bri::lock_data
846         } //  if ($table_cur->player_n == PLAYERS_N) {
847       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
848     
849       log_rd2("out new loop.");
850                 
851       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
852         $user_cur = &$this->user[$i];
853         
854         log_rd2("User: ".$user_cur->name."  stat: ".$user_cur->stat."  subst: ".$user_cur->subst);
855           
856         if ($user_cur->sess == "") 
857           continue;
858         
859         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
860           // Auto logout dell'utente
861           log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
862           
863           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
864             log_auth($user_cur->sess, "Autologout session.");
865             
866             $user_cur->reset();
867             /* factorized with ->reset()
868             $tmp_sess = $user_cur->sess;
869             $user_cur->sess = "";
870             step_unproxy($tmp_sess);
871             $user_cur->name = "";
872             while (array_pop($user_cur->comm) != NULL);
873             $user_cur->step = 0;
874             $user_cur->the_end = FALSE;
875             */
876             
877             log_rd2("AUTO LOGOUT.");
878             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
879               $this->room_wakeup(&$user_cur);
880             else if ($user_cur->subst == 'standup')
881               $this->room_outstandup(&$user_cur);
882             else
883               log_rd2("LOGOUT FROM WHAT ???");
884           }
885         }
886
887         if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
888           if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
889             $this->room_wakeup(&$user_cur);
890             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
891             $user_cur->comm[$user_cur->step % COMM_N] .=  show_notify("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 0, "torna ai tavoli", 400, 100);
892             $user_cur->step_inc();
893           }
894         }
895       }
896       log_rd2("GARBAGE UPDATED!");
897       
898       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
899       $ismod = TRUE;
900     }
901
902     return ($ismod);
903   }
904
905
906   function room_wakeup(&$user)
907   {
908     $table_idx = $user->table;
909     $table = &$this->table[$table_idx];
910
911     log_main("WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
912
913     $curtime = time();
914
915     $from_table = ($user->stat == "table");
916     if ($from_table) {
917       log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
918
919       for ($i = 0 ; $i < $table->player_n ; $i++) {
920         $user_cur = &$this->user[$table->player[$i]];
921         log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
922
923         if ($user_cur != $user) {
924           $user_cur->stat_set("room");
925           $user_cur->subst = "sitdown";
926           $user_cur->laccwr = $curtime;
927         }
928         else if ($user->sess != "") {
929           $user_cur->stat_set("room");
930           $user_cur->subst = "standup";
931           $user_cur->laccwr = $curtime;
932           $user_cur->table = -1;
933         }
934       }
935     }
936     else {
937       $user->stat_set("room");
938       $user->subst = "standup";
939       $user->laccwr = $curtime;
940     }
941     /* aggiorna l'array dei giocatori al tavolo. */
942     $table->user_rem(&$this, &$user);
943
944     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
945       $user_cur = &$this->user[$i];
946       if ($user_cur->sess == '' || $user_cur->stat != 'room')
947         continue;
948       
949       log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
950
951       $ret = "gst.st = ".($user_cur->step+1)."; ";
952       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
953         $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
954         // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
955         log_main("DOCUMENT.index.php: from table");
956       }
957       else if ($user_cur->stat == "room") {
958         log_main("DOCUMENT.index.php: from table");
959
960         $ret .= table_content($this, $user_cur, $table_idx);
961         $ret .= standup_content($this, $user_cur);
962         
963         $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
964         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
965         
966         
967         if ($user_cur == $user) {
968           // set the new status 
969           $ret .=  'subst = "standup"; ';
970           // clean the action buttons in other tables
971           for ($e = 0 ; $e < TABLES_N ; $e++) {
972             if ($this->table[$e]->player_n < PLAYERS_N)
973               $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
974           }
975         }
976         else {
977           $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
978           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
979         }
980       }
981       log_wr("ROOM_WAKEUP: ".$ret);
982       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
983       $user_cur->step_inc();
984     }
985   }
986
987   function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
988   {
989     $table_idx = $user->table;
990     $table = &$this->table[$table_idx];
991     
992     log_main("JOIN_WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
993
994     $curtime = time();
995     $user_wup = array();
996     $user_wup_n = 0;
997     $user_tab = array();
998     $user_tab_n = 0;
999     log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1000     
1001     for ($i = 0 ; $i < $table->player_n ; $i++) {
1002       $user_cur = &$this->user[$table->player[$i]];
1003       log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1004       if ($user_cur->sess != "") {
1005         if ($update_lacc == TRUE) {
1006           $user_cur->laccwr = $curtime;
1007         }
1008         log_main("cur: ".$user_cur->name."  subst: ".$user_cur->subst);
1009         if ($user_cur->subst == "shutdowned") {
1010           $user_cur->stat_set("room");
1011           $user_cur->subst = "sitdown";
1012         }
1013         else if ($user_cur->subst == "shutdowner") {
1014           $user_cur->stat_set("room");
1015           $user_cur->subst = "standup";
1016           $user_cur->table = -1;
1017           $user_wup[$user_wup_n++] = &$user_cur;
1018         }
1019         $user_tab[$user_tab_n++] = &$user_cur;
1020       }
1021     }
1022
1023     for ($wup_idx = 0 ; $wup_idx < $user_wup_n  ; $wup_idx++)
1024       $table->user_rem(&$this, &$user_wup[$wup_idx]);
1025
1026     /* aggiorna l'array dei giocatori al tavolo. */
1027
1028     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1029       log_main("START LOOP");
1030       $user_cur = &$this->user[$i];
1031       if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1032         log_main("name: ".$user_cur->name."skip   subst: ".$user_cur->subst);
1033         continue;
1034       }
1035
1036       log_main("___");
1037       log_main("VALORI name: ".$user_cur->name."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
1038
1039       $ret = "gst.st = ".($user_cur->step+1)."; ";
1040       if ($user_cur->stat == "room") {
1041         log_main("DOCUMENT.index.php from table");
1042
1043         $ret .= table_content($this, $user_cur, $table_idx);
1044         $ret .= standup_content($this, $user_cur);
1045         
1046         $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
1047         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1048         
1049         for ($tab_idx = 0 ; $tab_idx < $user_tab_n  ; $tab_idx++)
1050           if ($user_cur == $user_tab[$tab_idx]) 
1051             break;
1052
1053         // for users that wakeup the room will be reconstructed by index_rd.php
1054         if ($tab_idx < $user_tab_n) {
1055           log_main("PRE show_room username: ".$user_cur->name."  STEP: ".$user_cur->step);
1056
1057 //        ARRAY_POP DISABLED
1058 //        if ($trans_delta == 0)
1059 //          while (array_pop($user_cur->comm) != NULL);
1060
1061           $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1062           $user_cur->comm[$user_cur->step % COMM_N] = "";
1063           $user_cur->step_inc();
1064           $user_cur->comm[$user_cur->step % COMM_N] = show_room(&$this, ($user_cur->step + 1), &$user_cur);
1065           $user_cur->step_inc();
1066           log_main("POST show_room username: ".$user_cur->name."  STEP: ".$user_cur->step);
1067
1068           continue;
1069         }
1070         log_main("JOIN_WAKEUP wup_idx ".$wup_idx."  wup_n ".$user_wup_n);
1071
1072         log_main("JOIN_WAKEUP more");
1073         $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
1074         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1075         log_main("JOIN_WAKEUP end more");
1076       }
1077       log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1078       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1079       $user_cur->step_inc();
1080     }
1081   }
1082
1083   function room_outstandup(&$user)
1084   {
1085     $this->room_sitdown(&$user, -1);
1086   }
1087   
1088   function table_update(&$user)
1089   {
1090     log_main("table_update: pre - USER: ".$user->name);
1091
1092     $table_idx = $user->table;
1093
1094     if ($table_idx > -1) 
1095       $table = &$this->table[$table_idx];
1096     
1097     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1098       $ret = "";
1099       $user_cur = &$this->user[$i];
1100       if ($user_cur->sess == '' || $user_cur->stat != 'room')
1101       continue;
1102       
1103       $ret = "gst.st = ".($user_cur->step+1)."; ";
1104       if ($table_idx > -1)
1105         $ret .= table_content($this, $user_cur, $table_idx);
1106       
1107       if ($user_cur == $user) {
1108         $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
1109       }
1110       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1111       $user_cur->step_inc();
1112     }
1113
1114     log_main("table_update: post");
1115   }
1116
1117   function room_sitdown(&$user, $table_idx)
1118   {
1119     log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1120
1121     if ($table_idx > -1) 
1122       $table = &$this->table[$table_idx];
1123     
1124     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1125       $ret = "";
1126       $user_cur = &$this->user[$i];
1127       if ($user_cur->sess == '' || $user_cur->stat != 'room')
1128       continue;
1129       
1130       $ret = "gst.st = ".($user_cur->step+1)."; ";
1131       if ($table_idx > -1)
1132       $ret .= table_content($this, $user_cur, $table_idx);
1133       $ret .= standup_content($this, $user_cur);
1134       
1135       if ($user_cur == $user) {
1136         $ret .=  'subst = "sitdown"; ';
1137         // clean the action buttons in other tables
1138         for ($e = 0 ; $e < TABLES_N ; $e++) {
1139           $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
1140           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
1141         }
1142       }
1143       else if ($table_idx > -1) {
1144         if ($table->player_n == PLAYERS_N) {
1145           $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
1146           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
1147         }
1148       }
1149       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1150       $user_cur->step_inc();
1151     }
1152   }
1153
1154   function chatt_send(&$user, $mesg)
1155   {
1156     GLOBAL $G_alarm_passwd;
1157     $only_you = FALSE;
1158     
1159 //     if ($user->stat == 'table') {
1160 //       $table = &$this->table[$user->table];
1161 //     }
1162     
1163     $user_mesg = substr($mesg,6);
1164     $timecur = time();
1165     
1166     $dt = date("H:i ", $timecur);
1167     if (strncmp($user_mesg, "/alarm ", 7) == 0) {
1168       $alarm_check = "/alarm ".$G_alarm_passwd." ";
1169       if (strncmp($user_mesg, $alarm_check, strlen($alarm_check)) != 0) 
1170         return;
1171       
1172       $user_mesg = "<br><b>".$dt.NICKSERV."<br><br>".xcape(substr($user_mesg, strlen($alarm_check)))."</b><br><br>";
1173       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1174         $user_cur = &$this->user[$i];
1175         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
1176         if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1177           continue;
1178         
1179         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1180 //      $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1181 //                                                           $dt.NICKSERV, xcape($user_mesg));
1182
1183
1184         $user_cur->comm[$user_cur->step % COMM_N] .=  show_notify($user_mesg, 0, "chiudi", 400, 120);
1185         $user_cur->step_inc();
1186       }
1187
1188
1189       // Before all align times with table timeout
1190       for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1191         $table_cur =& $this->table[$table_idx];
1192         // if the table is complete and exists its shared mem we get the info about users lacc
1193         
1194         if ($table_cur->player_n == PLAYERS_N) {
1195           log_main("PLAYERS == N TABLE ".$table_idx);
1196           
1197           if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
1198             log_main("bin5 lock data success");
1199             
1200             $no_recovery = FALSE;
1201             if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1202               if ($table_cur->table_token != $bri->table_token) {
1203                 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
1204                 $bri = FALSE;
1205               }
1206             }
1207             
1208             if ($bri != FALSE) {
1209               $bri_table = &$bri->table[0];
1210               for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1211                 // stat must be "table" by definition
1212                 $bri_user =& $bri->user[$i];
1213
1214                 log_main("writa: ".$user_mesg);
1215                 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1216 //              $bri_user->comm[$bri_user->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1217 //                                                                   $dt.NICKSERV, xcape($user_mesg));
1218                 $bri_user->comm[$bri_user->step % COMM_N] .= show_notify($user_mesg, 0, "chiudi", 400, 120);
1219                 $bri_user->step_inc();
1220               }
1221               Briskin5::save_data(&$bri);
1222             }
1223             Briskin5::unlock_data($sem);
1224           } // bri::lock_data
1225         } //  if ($table_cur->player_n == PLAYERS_N) {
1226       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1227     }
1228     else if (strncmp($user_mesg, "/nick ", 6) == 0) {
1229       log_main("chatt_send BEGIN");
1230
1231       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
1232           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1233           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Il nickname deve contenere almeno una lettera o una cifra.");', $dt.NICKSERV, xcape($name_new));
1234           $user->step_inc();
1235
1236           return;
1237       }
1238       $user_mesg = "COMMAND ".$user_mesg;
1239       // Search dup name
1240       // change
1241       // update local graph
1242       // update remote graphs
1243       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1244         $user_cur = &$this->user[$i];
1245         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
1246         if ($user_cur->sess == '')
1247           continue;
1248         if ($user_cur->name == $name_new) {
1249           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1250           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
1251           $user->step_inc();
1252           break;
1253         }
1254       }
1255       if ($i == MAX_PLAYERS) {
1256         $user->name = $name_new;
1257
1258       log_main("chatt_send start set");
1259         
1260
1261         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1262           log_main("chatt_send set loop");
1263           
1264           $user_cur = &$this->user[$i];
1265           if ($user_cur->sess == '')
1266             continue;
1267           if ($user_cur->stat == 'room') {
1268             if ($user->stat == 'room' && $user->subst == 'standup') {
1269               $this->standup_update(&$user);
1270             }
1271             else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1272               log_main("chatt_send pre table update");
1273
1274               $this->table_update(&$user);
1275
1276               log_main("chatt_send post table update");
1277             }
1278           }
1279 //        else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
1280 //          $table = &$this->table[$user->table];
1281             
1282 //          $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1283 //          $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1284 //              xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
1285 //              xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
1286 //              xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
1287 //              (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
1288 //              (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
1289 //          if ($user_cur == $user)
1290 //            $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
1291 //                                                                 xcape($user->name,ENT_COMPAT,"UTF-8"));
1292 //          $user_cur->step_inc();
1293 //        }
1294         }
1295       }
1296     }
1297     else {
1298       if ($timecur < ($user->chat_ban + $user->chat_dlt)) {
1299         $only_you = TRUE;
1300         $user->chat_dlt = $user->chat_dlt * 2; 
1301         if ($user->chat_dlt > 120)
1302           $user->chat_dlt = 120; 
1303       }
1304       else if ($user->chat_lst == $user_mesg)
1305         $only_you = TRUE;
1306       else if ($timecur - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1307         $user->chat_ban = $timecur;
1308         $user->chat_dlt = 5;
1309         $only_you = TRUE;
1310       }
1311       else {
1312         $user->chat_ban = 0;
1313         $user->chat_dlt = 0;
1314       }
1315
1316       for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
1317         if ($user->stat == 'room') {
1318           $user_cur = &$this->user[$i];
1319           if ($user_cur->sess == '' || $user_cur->stat != 'room')
1320             continue;
1321         }
1322
1323         if ($only_you) {
1324           if ($user_cur != $user)
1325             continue;
1326           else
1327             $user_mesg = "== chat ban ==";
1328         }
1329         
1330         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1331         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
1332                                                              $dt.xcape($user->name), xcape($user_mesg));
1333         $user_cur->step_inc();
1334       }
1335       log_legal($timecur, $user->sess, $user->name, 
1336                 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
1337       
1338       $user->chat_lst = "$user_mesg";
1339       $user->chattime[$user->chat_cur % CHAT_N] = $timecur;
1340       $user->chat_cur++;
1341     }
1342   }
1343
1344   function &get_user($sess, &$idx)
1345   {
1346     GLOBAL $PHP_SELF, $G_false;
1347
1348     if (validate_sess($sess)) {
1349       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1350         if (strcmp($sess, $this->user[$i]->sess) == 0) {
1351           // find it
1352           $idx = $i;
1353           $ret = &$this->user[$i];
1354           return ($ret);
1355         }
1356       }
1357       log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1358       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
1359       // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1360     }
1361     else {
1362       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1363     }
1364
1365     return ($G_false);
1366   }
1367
1368   /*
1369    * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1370    *
1371    * RETURN VALUE:
1372    *   if ($idx != -1 && ret == FALSE)  =>  duplicated nick
1373    *   if ($idx == -2 && ret == FALSE)  =>  invalid name
1374    *   if ($idx == -1 && ret == FALSE)  =>  no space left
1375    *   if (ret == TRUE)                 =>  SUCCESS
1376    */
1377   function &add_user(&$sess, &$idx, $name, $ip)
1378   {
1379     GLOBAL $G_false;
1380
1381     $idx = -1;
1382     $idfree = -1;
1383     
1384     if (($name_new = validate_name($name)) == FALSE) {
1385       $idx = -2;
1386       return ($G_false);
1387     }
1388
1389     log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1390     if (validate_sess($sess) == FALSE) 
1391       $sess = "";
1392
1393     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1394       /* free user ? */
1395       if (strcmp($sess, $this->user[$i]->sess) == 0) {
1396         if ($idx == -1)
1397           $idx = $i;
1398       }
1399       if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1400         $idfree = $i;
1401       }
1402       if (strcmp($this->user[$i]->name, $name_new) == 0) {
1403         $idx = $i;
1404         break;
1405       }
1406     }
1407     if ($idx == -1)
1408       $idx = $idfree;
1409
1410     log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1411
1412     if ($idx != -1 && $i == MAX_PLAYERS) {
1413       /* SUCCESS */
1414       $curtime = time();
1415       if ($sess == "") {
1416         $this->user[$idx]->sess = uniqid("");
1417         $sess = $this->user[$idx]->sess;
1418         
1419       }
1420       else {
1421         $this->user[$idx]->sess = $sess;
1422       }
1423       $this->user[$idx]->name = $name_new;
1424       $this->user[$idx]->stat_set("room");
1425       $this->user[$idx]->step_set(0);
1426       while (array_pop($this->user[$idx]->comm) != NULL);
1427       $this->user[$idx]->subst = "standup";
1428       $this->user[$idx]->lacc =   $curtime;
1429       $this->user[$idx]->laccwr = $curtime;
1430       $this->user[$idx]->bantime = 0;
1431       $this->user[$idx]->ip = $ip;
1432       log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1433       
1434       return ($this->user[$idx]);
1435     }
1436
1437     return ($G_false);
1438   }
1439   
1440   function standup_update(&$user)
1441   {
1442     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1443       $user_cur = &$this->user[$i];
1444       if ($user_cur->sess == '')
1445         continue;
1446
1447       log_main("STANDUP START: ".$user_cur->stat);
1448       
1449       if ($user_cur->stat == 'room') {
1450         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1451         if ($user_cur == $user)
1452           $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
1453         
1454         log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1455         
1456         $user_cur->step_inc();
1457       }
1458     }
1459   }
1460
1461   // Static functions
1462   function &init_data()
1463   {
1464     $room =& new Room();
1465     
1466     return $room;
1467   }
1468   
1469
1470   function &load_data() 
1471   {
1472     GLOBAL $G_false, $sess;
1473     $doexit = FALSE;
1474     do {
1475       if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1476         log_main("ftok failed");
1477         $doexit = TRUE;
1478         break;
1479       }
1480     
1481       if (($shm_sz = sharedmem_sz($tok)) == -1) {
1482         log_main("shmop_open failed");
1483       }
1484         
1485       if ($shm_sz == -1)
1486         $shm_sz = SHM_DIMS_MIN;
1487
1488       if ($shm = shm_attach($tok, $shm_sz)) {
1489         $room = @shm_get_var($shm, $tok);
1490         
1491         log_only("bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
1492         if (isset($room)) 
1493           log_only("bri count ".count($room));
1494         
1495         if ($room == FALSE) {
1496           log_only("INIT MAIN DATA");
1497           
1498           $room =& Room::init_data();
1499           if (@shm_put_var($shm, $tok, $room) == FALSE) {
1500             log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1501             log_only(serialize($room));
1502           }
1503         }
1504         $room->shm_sz = $shm_sz;
1505         
1506         shm_detach($shm);
1507       }
1508       $ret = &$room;
1509       return ($ret);
1510     } while (0);
1511     
1512     if ($doexit)
1513       exit();
1514     
1515     return ($G_false);
1516   }
1517   
1518
1519   function save_data(&$room) 
1520   {
1521     GLOBAL $sess;
1522     
1523     $ret =   FALSE;
1524     $shm =   FALSE;
1525     
1526     // var_dump($room);
1527     
1528     if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) 
1529       return (FALSE);
1530     
1531     while ($room->shm_sz < SHM_DIMS_MAX) {
1532       if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1533         break;
1534       
1535       // log_only("PUT_VAR DI ".strlen(serialize($room)));
1536       if (shm_put_var($shm, $tok, $room) != FALSE) {
1537         shm_detach($shm);
1538         return (TRUE);
1539       }
1540       if (shm_remove($shm) === FALSE) {
1541         log_only("REMOVE FALLITA");
1542         break;
1543       }
1544       shm_detach($shm);
1545       $room->shm_sz += SHM_DIMS_DLT;
1546     } 
1547
1548     if ($shm)
1549       shm_detach($shm);
1550     
1551     return ($ret);
1552   }
1553
1554   function lock_data()
1555   {
1556     GLOBAL $sess; 
1557     
1558     //  echo "LOCK: ".FTOK_PATH."/main";
1559     //  exit;
1560     if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1561       echo "FTOK FAILED";
1562       exit;
1563     }
1564     // echo "FTOK ".$tok."<br>";
1565     if (($res = sem_get($tok)) == FALSE) {
1566       echo "SEM_GET FAILED";
1567       exit;
1568     }
1569     if (sem_acquire($res)) {   
1570       log_lock("LOCK room");
1571       return ($res);
1572     }
1573     else
1574       return (FALSE);
1575   }
1576   
1577   function unlock_data($res)
1578   {
1579     GLOBAL $sess; 
1580     
1581     log_lock("UNLOCK room");
1582
1583     return (sem_release($res));
1584   }
1585 } // end class Room
1586
1587 function make_seed()
1588 {
1589   list($usec, $sec) = explode(' ', microtime());
1590   return (float) $sec + ((float) $usec * 100000);
1591 }
1592
1593
1594 function log_only2($log) {
1595   GLOBAL $sess;
1596
1597   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
1598     return;
1599
1600   if (isset($sess) == FALSE)
1601     $ssess = "XXXX";
1602   else
1603     $ssess = $sess;
1604
1605   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
1606     return;
1607
1608   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1609     fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
1610     fclose($fp);
1611   }
1612 }
1613
1614 function log_crit($log) {
1615   GLOBAL $sess;
1616
1617   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
1618     return;
1619
1620   if (isset($sess) == FALSE)
1621     $ssess = "XXXX";
1622   else
1623     $ssess = $sess;
1624
1625   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
1626     return;
1627
1628   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1629     fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
1630     fclose($fp);
1631   }
1632 }
1633
1634 function log_only($log) {
1635   GLOBAL $sess;
1636
1637   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
1638     return;
1639
1640   if (isset($sess) == FALSE)
1641     $ssess = "XXXX";
1642   else
1643     $ssess = $sess;
1644
1645   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
1646     return;
1647
1648   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1649     fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
1650     fclose($fp);
1651   }
1652 }
1653
1654 function log_main($log) {
1655   GLOBAL $sess;
1656
1657   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
1658     return;
1659
1660   if (isset($sess) == FALSE)
1661     $ssess = "XXXX";
1662   else
1663     $ssess = $sess;
1664
1665   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
1666     return;
1667
1668   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1669     fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
1670     fclose($fp);
1671   }
1672 }
1673
1674 function log_rd($log) {
1675   GLOBAL $sess;
1676
1677   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
1678     return;
1679
1680   if (isset($sess) == FALSE)
1681     $ssess = "XXXX";
1682   else
1683     $ssess = $sess;
1684
1685   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
1686     return;
1687
1688   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1689     fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
1690     fclose($fp);
1691   }
1692 }
1693
1694 function log_rd2($log) {
1695   GLOBAL $sess;
1696
1697   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
1698     return;
1699
1700   if (isset($sess) == FALSE)
1701     $ssess = "XXXX";
1702   else
1703     $ssess = $sess;
1704       
1705   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
1706     return;
1707
1708   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1709     fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
1710     fclose($fp);
1711   }
1712 }
1713
1714 function log_send($log) {
1715   GLOBAL $sess;
1716
1717   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
1718     return;
1719
1720   if (isset($sess) == FALSE)
1721     $ssess = "XXXX";
1722   else
1723     $ssess = $sess;
1724       
1725   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
1726     return;
1727
1728   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1729     fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
1730     fclose($fp);
1731   }
1732 }
1733
1734 function log_lock($log) {
1735   GLOBAL $sess;
1736
1737   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
1738     return;
1739
1740   if (isset($sess) == FALSE)
1741     $ssess = "XXXX";
1742   else
1743     $ssess = $sess;
1744
1745   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
1746     return;
1747
1748   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1749     fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
1750     fclose($fp);
1751   }
1752 }
1753
1754 function log_wr($log) {
1755   GLOBAL $sess;
1756
1757   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
1758     return;
1759
1760   if (isset($sess) == FALSE)
1761     $ssess = "XXXX";
1762   else
1763     $ssess = $sess;
1764       
1765   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
1766     return;
1767
1768   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1769     fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
1770     fclose($fp);
1771   }
1772 }
1773
1774 function log_load($log) {
1775   GLOBAL $sess;
1776
1777   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
1778     return;
1779
1780   if (isset($sess) == FALSE)
1781     $ssess = "XXXX";
1782   else
1783     $ssess = $sess;
1784
1785   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
1786     return;
1787       
1788   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1789     fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
1790     fclose($fp);
1791   }
1792 }
1793
1794 function log_auth($sess, $log) {
1795   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
1796     return;
1797
1798   if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
1799     return;
1800
1801   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
1802     fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
1803     fclose($fp);
1804   }
1805 }
1806
1807
1808 function log_legal($timecur, $sess, $name, $where, $mesg) 
1809 {
1810   GLOBAL $_SERVER;
1811
1812   if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
1813     /* Unix time | session | nickname | IP | where was | mesg */
1814     fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1815     fclose($fp);
1816   }
1817 }
1818
1819
1820
1821
1822 function lock_banlist()
1823 {
1824   if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
1825     echo "FTOK FAILED";
1826     exit;
1827   }
1828   if (($res = sem_get($tok)) == FALSE) {
1829     echo "SEM_GET FAILED";
1830     exit;
1831   }
1832   if (sem_acquire($res)) 
1833     return ($res);
1834   else
1835     return (FALSE);
1836 }
1837
1838 function unlock_banlist($res)
1839 {
1840   return (sem_release($res));
1841 }
1842
1843 function table_act_content($isstanding, $sitted, $table, $cur_table)
1844 {
1845   $ret = "";
1846
1847   if ($isstanding) {
1848     if ($sitted < PLAYERS_N) {
1849       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1850     }
1851   }
1852   else {
1853     if ($table == $cur_table)
1854       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1855     else
1856       $ret = "";
1857   }
1858   return ($ret);
1859 }
1860
1861 function table_content($room, $user, $table_idx)
1862 {
1863   $content = "";
1864   $ret = "";
1865   // TODO
1866   //
1867   //   Si possono usare i dati nella classe table
1868   //
1869
1870   $sess = $user->sess;
1871   $table = &$room->table[$table_idx];
1872
1873   if ($user->stat != 'room')
1874     return;
1875
1876   for ($i = 0 ; $i < $table->player_n ; $i++) {
1877     $user_cur = &$room->user[$table->player[$i]];
1878
1879     if ($user_cur == $user) 
1880         { $hilion = "<b>"; $hilioff = "</b>"; }
1881       else
1882         { $hilion = ""; $hilioff = ""; }
1883
1884     log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
1885     
1886     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1887   }
1888   /*
1889   for ( ; $i < PLAYERS_N ; $i++)
1890     $content .= "<br>";
1891   */
1892
1893   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1894   
1895   return ($ret);
1896 }
1897
1898 function standup_content(&$room, $user)
1899 {
1900   $ret = "";
1901   $content = "";
1902
1903   if ($user->stat != 'room')
1904     return;
1905
1906   for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1907     if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1908       continue;
1909     $ct++;
1910   }
1911
1912   $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1913
1914   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1915     if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1916       continue;
1917
1918
1919     if ($room->user[$e]->subst == "standup") {
1920       if (($ct % 4) == 0) {
1921         $content .= '<tr>';
1922       }
1923       if ($room->user[$e] == $user) 
1924         { $hilion = "<b>"; $hilioff = "</b>"; }
1925       else
1926         { $hilion = ""; $hilioff = ""; }
1927
1928       $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1929       if (($ct % 4) == 3) {
1930         $content .= '</tr>';
1931       }
1932       $ct++;
1933     }
1934   }
1935   $content .= '</table>';
1936         
1937   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1938   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
1939                   $content, $content2);
1940
1941   return ($ret);
1942 }
1943
1944
1945 function show_notify($text, $tout, $butt, $w, $h)
1946 {
1947   log_main("SHOW_NOTIFY: ".$text);
1948   return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1949 }
1950
1951 function briscola_show($room, $table, $user)
1952 {
1953   $ptnadd = "";
1954   $ret = "";
1955
1956   if ($table->asta_card == 9) 
1957     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1958   
1959   /* text of caller cell */
1960   if ($user->table_pos == $table->asta_win) 
1961     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1962   else 
1963     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
1964                     xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1965
1966   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1967                   $table->asta_win);
1968   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1969   $ret .= sprintf('$("caller").style.visibility = "visible";');
1970   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1971   $ret .= sprintf('$("astalascio").style.visibility = "";');
1972   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1973   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1974   
1975   return ($ret);
1976 }
1977
1978
1979 function game_result($asta_pnt, $pnt)
1980 {
1981   if ($asta_pnt == 61) {
1982     if ($pnt > 60)
1983       return (1);
1984     else if ($pnt == 60)
1985       return (0);
1986     else
1987       return (-1);
1988   }
1989   else {
1990     if ($pnt >= $asta_pnt)
1991       return (1);
1992     else
1993       return (-1);
1994   }
1995 }
1996
1997 function multoval($mult)
1998 {
1999   if ($mult == 2)
2000     return ("doppio");
2001   else if ($mult == 4)
2002     return ("quadruplo");
2003   else
2004     return (sprintf("%d-plo", $mult));
2005 }
2006
2007 function show_table_info(&$room, &$table, $table_pos)
2008 {
2009   $ret = "";
2010   $user = &$room->user[$table->player[$table_pos]];
2011
2012   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
2013   $noty = sprintf('<table class=\"points\"><tr><th></th>');
2014   
2015   // Names.
2016   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
2017     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
2018   $noty .= sprintf("</tr>");
2019
2020   // Points.
2021   log_main("show_table_info: pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
2022
2023   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
2024     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
2025     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
2026       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
2027     $noty .= "</tr>";
2028   }
2029
2030   // Total points.
2031   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
2032   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
2033     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
2034   $noty .= "</tr></table>";
2035
2036   if ($table->old_reason != "") {
2037     $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
2038   }
2039
2040   if ($table->old_win != -1) {
2041     $win = $table->player[$table->old_win];
2042     $fri = $table->player[$table->old_friend];
2043
2044     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
2045
2046     if ($win != $fri) {
2047       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
2048                        xcape($room->user[$win]->name),
2049                        xcape($room->user[$fri]->name));
2050       if ($table->old_pnt == 120) {
2051         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
2052       }
2053       else {
2054         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
2055                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2056                           'pi&ugrave; di 60'), $table->old_pnt,
2057                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2058       }
2059     }
2060     else {
2061       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
2062                        xcape($room->user[$win]->name));
2063       if ($table->old_pnt == 120) {
2064         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
2065       }
2066       else {
2067         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
2068                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
2069                           'pi&ugrave; di 60'), $table->old_pnt,
2070                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
2071       }
2072     }
2073   }
2074   if ($table->mazzo == $table_pos) 
2075     $noty .= "Fai <b>tu</b> il mazzo,";
2076   else {
2077     $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
2078     $noty .= "Il mazzo a <b>$unam</b>,";
2079   }
2080
2081   if ($user->subst == 'asta') {
2082     if ($table->asta_win == -1)  // auction case
2083       $curplayer = $table->gstart % PLAYERS_N;
2084     else 
2085       $curplayer = $table->asta_win;
2086   }
2087   else if ($user->subst == 'game') {
2088     $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
2089   }
2090
2091
2092   if ($curplayer == $table_pos) {
2093     $noty .= " tocca a <b>te</b> giocare.";
2094   }
2095   else {
2096     $unam = xcape($room->user[$table->player[$curplayer]]->name);
2097     $noty .= " tocca a <b>$unam</b> giocare.";
2098   }
2099
2100   if ($table->mult > 1) {
2101     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
2102   }
2103   $noty .= "<hr><br>";
2104
2105   $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
2106   
2107   return ($ret);
2108 }
2109
2110 function root_wellcome($user)
2111 {
2112   GLOBAL $root_wellarr;
2113   $ret = "";
2114
2115   $timecur = time();
2116   $dt = date("H:i ", $timecur);
2117     
2118   for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2119     $ret .= sprintf('chatt_sub("%s","%s");', $dt.NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2120
2121   return ($ret);
2122 }
2123
2124 function table_wellcome($user)
2125 {
2126   GLOBAL $table_wellarr;
2127   $ret = "";
2128
2129   for ($i = 0 ; $i < count($table_wellarr) ; $i++)
2130     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
2131
2132   return ($ret);
2133 }
2134
2135 function show_room(&$room, $user_step, &$user)
2136 {
2137   log_main("show_room: username: ".$user->name);
2138
2139
2140   $ret = sprintf('gst.st = %d;',  $user_step);
2141   $ret .= sprintf('stat = "%s";',  $user->stat);
2142
2143   $ret .= root_wellcome($user);
2144   $ret .= sprintf('subst = "%s";', $user->subst);
2145   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2146   for ($i = 0 ; $i < TABLES_N ; $i++) {
2147     $ret .= table_content($room, $user, $i);
2148     $act_content = table_act_content(($user->subst == 'standup'), 
2149                                      $room->table[$i]->player_n, $i, $user->table);
2150     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
2151   }
2152   $ret .= standup_content($room, $user);
2153   
2154   return ($ret);
2155 }
2156
2157
2158
2159 /* show table 
2160 is_transition (is from room to table ?)
2161 is_again      (is another game)
2162
2163 Examples                    of $is_transition, $is_again:
2164   from reload of the page:              FALSE, FALSE
2165   from sitdown in room:                  TRUE, FALSE
2166   from table: asta cmd e tutti passano:  TRUE, TRUE
2167   from table: fine partita:              TRUE, TRUE
2168  */
2169 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
2170 {
2171   $table_idx = $user->table;
2172   $table = &$room->table[$table_idx];
2173   $table_pos = $user->table_pos;
2174
2175   $ret = "table_init();";
2176   $ret .= $table->exitlock_show(&$room->user, $table_pos);
2177   if (!$is_again) {
2178     /* GENERAL STATUS */
2179     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2180                      $sendstep, $user->stat, $user->subst, $table_pos);
2181
2182     log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
2183                      $sendstep, $user->stat, $user->subst, $table_pos));
2184
2185     /* BACKGROUND */
2186     $ret .= "background_set();";
2187     
2188     /* USERS INFO */
2189     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
2190     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
2191                     xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
2192                     xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
2193                     xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
2194                     (PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
2195                     (PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
2196   }
2197   /* NOTIFY FOR THE CARD MAKER */
2198   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
2199     $ret .= show_table_info(&$room, &$table, $table_pos);
2200   }
2201   if (!$is_again) 
2202     $ret .= table_wellcome($user);
2203
2204   if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
2205     $ret .= playsound("cow.mp3");
2206   }
2207
2208
2209   /* CARDS */
2210   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
2211     $ret .= "|";
2212     
2213     for ($i = 0 ; $i < 8 ; $i++) {
2214       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
2215         $ct = 0;
2216         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
2217           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
2218             $ct++;
2219             if ($ct == $i+1)
2220               break;
2221           }
2222         }
2223         log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
2224         
2225         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
2226                          $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
2227                               $table->card[$o]->value : -1), 
2228                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
2229       }
2230     }    
2231   }
2232   else {
2233     $taked  = array(0,0,0,0,0);
2234     $inhand = array(0,0,0,0,0);
2235     $ontabl  = array(-1,-1,-1,-1,-1);
2236     $cards  = array();
2237
2238     for ($i = 0 ; $i < 40 ; $i++) {
2239       if ($table->card[$i]->stat == 'hand') {
2240         if ($table->card[$i]->owner == $table_pos) {
2241           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
2242         }
2243         $inhand[$table->card[$i]->owner]++;
2244       }
2245       else if ($table->card[$i]->stat == 'take') {
2246         log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
2247         $taked[$table->card[$i]->owner]++;
2248       }
2249       else if ($table->card[$i]->stat == 'table') {
2250         $ontabl[$table->card[$i]->owner] = $i;
2251       }
2252     }
2253     $logg = "\n";
2254     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2255       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
2256     }
2257     log_main("Stat table: ".$logg);
2258
2259     /* Set ours cards. */
2260     $oursarg = "";
2261     for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
2262       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
2263     for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
2264       $oursarg .= ($i == 0 ? "" : ", ")."-1";
2265     $ret .= sprintf('card_setours(%s);', $oursarg);
2266
2267     /* Dispose all cards */
2268     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2269       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
2270       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
2271                       $inhand[$i], $taked[$i]);
2272
2273       if ($ontabl[$i] != -1) {
2274         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
2275                         $table->card[$ontabl[$i]]->value, 
2276                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
2277       }
2278     }
2279   }
2280
2281   /* Show auction */
2282   if ($user->subst == 'asta') {
2283
2284     /* show users auction status */
2285     $showst = "";
2286     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2287       $user_cur = &$room->user[$table->player[$i]];
2288       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
2289                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
2290     }
2291     if (PLAYERS_N == 3)
2292         $showst .= ",-2,-2";
2293     $ret .= sprintf('show_astat(%s);', $showst);
2294
2295     if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
2296       /* show card chooser */
2297       $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
2298                       $table->asta_card);
2299     }
2300     else {
2301       /* show auction */
2302       if ($table_pos == ($table->gstart % PLAYERS_N) &&
2303           $table->asta_win == -1) 
2304         $ret .= sprintf('dispose_asta(%d,%d, %s);', 
2305                         $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
2306       else
2307         $ret .= sprintf('dispose_asta(%d,%d, %s);',
2308                         $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
2309     }
2310
2311     /* Remark */
2312     if ($table->asta_win == -1) { // auction case
2313       if ($table_pos == ($table->gstart % PLAYERS_N)) 
2314         $ret .= "remark_on();";
2315       else
2316         $ret .= "remark_off();";
2317     }
2318     else { // chooseed case
2319       if ($table_pos == $table->asta_win) 
2320         $ret .= "remark_on();";
2321       else
2322         $ret .= "remark_off();";
2323     }
2324   }
2325   else if ($user->subst == 'game') {
2326     /* HIGHLIGHT */
2327     if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos) 
2328       $ret .= "is_my_time = true; remark_on();";
2329     else
2330       $ret .= "remark_off();";
2331     
2332     /* WHO CALL AND WATH */
2333     $ret .= briscola_show($room, $table, $user);
2334     
2335   }
2336   return ($ret);
2337 } // end function show_table(...
2338
2339 function calculate_winner(&$table)
2340 {
2341   $briontab = FALSE;
2342   $ontab = array();
2343   $ontid = array();
2344   $cur_win  =  -1;
2345   $cur_val  = 100;
2346   $cur_seed = $table->briscola - ($table->briscola % 10);
2347
2348   for ($i = 0 ; $i < 40 ; $i++) {
2349     if ($table->card[$i]->stat != "table")
2350       continue;
2351
2352     log_wr(sprintf("Card On table: [%d]", $i));
2353
2354     $v = $table->card[$i]->value; 
2355     $ontab[$table->card[$i]->owner] = $v;
2356     $ontid[$table->card[$i]->owner] = $i;
2357     /* se briscola setto il flag */
2358     if (($v - ($v % 10)) == $cur_seed)
2359       $briontab = TRUE;
2360   }
2361
2362   if ($briontab == FALSE) {
2363     $cur_win  = $table->gstart;
2364     $cur_val  = $ontab[$cur_win];
2365     $cur_seed = $cur_val - ($cur_val % 10);
2366   }
2367
2368   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2369     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
2370       if ($ontab[$i] < $cur_val) {
2371         $cur_val = $ontab[$i];
2372         $cur_win = $i;
2373       }
2374     }
2375   }
2376
2377   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
2378     $table->card[$ontid[$i]]->owner = $cur_win;
2379     $table->card[$ontid[$i]]->stat =  "take"; // Card stat
2380   }
2381   return ($cur_win);
2382 }
2383
2384 function calculate_points(&$table)
2385 {
2386   GLOBAL $G_all_points; 
2387
2388   $pro = 0;
2389
2390   if ($table->asta_pnt == 60)
2391     $table->asta_pnt = 61;
2392
2393   $table->old_reason = "";
2394   $table->old_win = $table->asta_win;
2395   $table->old_friend = $table->friend;
2396   $table->old_asta_pnt = $table->asta_pnt;
2397
2398   for ($i = 0 ; $i < 40 ; $i++) {
2399     $ctt = $table->card[$i]->value % 10;
2400     $own = $table->card[$i]->owner;
2401     if ($own == $table->asta_win || $own == $table->friend) 
2402       $pro += $G_all_points[$ctt];
2403   }
2404
2405   log_wr(sprintf("PRO: [%d]", $pro));
2406
2407   
2408   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
2409     $table->points[$table->points_n % MAX_POINTS] = array();
2410     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
2411       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
2412     $table->points_n++;
2413     $table->old_pnt = $pro;
2414     $table->mult *= 2;
2415
2416     return;
2417   }
2418
2419   if ($pro >= $table->asta_pnt) 
2420     $sig = 1;
2421   else
2422     $sig = -1;
2423
2424   $table->points[$table->points_n % MAX_POINTS] = array();
2425   for ($i = 0 ; $i < 5 ; $i++) {
2426     if ($i == $table->asta_win) 
2427       $pt = ($i == $table->friend ? 4 : 2);
2428     else if ($i == $table->friend) 
2429       $pt = 1;
2430     else
2431       $pt = -1;
2432
2433     log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
2434
2435     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
2436
2437     log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
2438     
2439     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
2440     $table->total[$i] += $pt;
2441   }
2442   $table->points_n++;
2443   $table->old_pnt = $pro;
2444   $table->mult = 1;
2445 }
2446
2447 function validate_sess($sess) 
2448 {
2449   if (strlen($sess) == SESS_LEN) 
2450     return (TRUE);
2451   else
2452     return (FALSE);
2453 }
2454
2455 function validate_name($name) 
2456 {
2457   $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2458
2459   for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2460     $c = $name_new[$i];
2461     if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2462       return ($name_new);
2463   }
2464
2465   return (FALSE);
2466 }
2467
2468 function playsound($filename)
2469 {
2470   return (sprintf('playsound("flasou", "%s");', $filename));
2471 }
2472
2473 function secstoword($secs)
2474 {
2475   $mins = floor($secs / 60);
2476   $secs = $secs % 60;
2477   if ($mins > 0) 
2478     $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2479   
2480   if ($secs > 0)
2481     $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2482   
2483   return ($ret);
2484 }
2485
2486 function sharedmem_sz($tok)
2487 {
2488   if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2489     log_main("shmop_open failed");
2490     return (-1);
2491   }
2492   $shm_sz = shmop_size($shm_id);
2493   shmop_close($shm_id);
2494   
2495   log_main("shm_sz: ".$shm_sz."   SHM_DIMS: ".SHM_DIMS);
2496   return ($shm_sz);
2497 }    
2498
2499
2500 ?>