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