moved from /
[brisk.git] / web / Obj / brisk.phh
1 <?php
2 /*
3  *  brisk - brisk.phh
4  *
5  *  Copyright (C) 2006-2007 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 define(FTOK_PATH, "/var/lib/brisk");
23 define(LEGAL_PATH, "/tmp/legal_brisk");
24 define(PROXY_PATH, "/var/lib/brisk_proxy");
25 define(TABLES_N, 12);
26 define(PLAYERS_N, 3);
27 define(MAX_POINTS, 5);
28 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
29 define(SHM_DIMS, (50000 * MAX_PLAYERS));
30 // define(COMM_N, 6);
31 define(COMM_N, 12);
32 define(COMM_GEN_N, 50);
33 define(SESS_LEN, 13);
34 define(STREAM_TIMEOUT, 20);
35 define(EXPIRE_TIME_RD, 180);
36 define(EXPIRE_TIME_SMAMMA, 360); 
37 // BAN_TIME da allineare anche in commons.js
38 define(BAN_TIME, 900); 
39 define(GARBAGE_TIMEOUT, 10);
40 define(NICKSERV, "<i>SERVER</i>");
41 define(BRISK_DEBUG, FALSE);
42 // define(DEBUGGING, "local");
43
44 $G_false = FALSE;
45
46 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
47 $G_brisk_version = "0.8.2";
48
49 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette pi&ugrave; tavoli, pi&ugrave; tavoli.',
50                         'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
51 $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.');
52
53
54 $G_room_help= '
55 <div style=\\"text-align: left; padding: 8px;\\">
56 <b>Descrizione</b><br>
57 Questa &egrave; un\'implementazione della briscola in cinque, cos&igrave; come &egrave; spiegata su
58 <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>
59 <b>Configurazione del browser.</b><br>
60 Occorre abilitare i cookies.<br>
61 <br>
62 <b>Uso del sito</b><br>
63 Potete sedervi a un tavolo o rimanere in piedi.<br>
64 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
65 <br>
66 <b>Partita</b><br>
67 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>
68 Chi vince l\'asta dovr&agrave; decidere il seme della carta scelta e inizier&agrave; la mano.<br>
69 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
70 Il vostro turno &egrave; sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
71 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
72 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.
73 <dl>
74 <dt><b>Comandi della chat</b>
75 <dd><b>/nick <i>&lt;nuovo_nickname&gt;</i></b> - cambio di nickname
76 <dd>.. to be continue ..
77 </dl>
78 </div>
79 ';
80
81 $G_room_about= '<br>
82 <div id=\\"header\\" class=\\"header\\">
83   <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
84   briscola chiamata in salsa ajax
85 </div>
86 <br><b>version '.$G_brisk_version.'</b><br><br>
87 Copyright 2006-2007 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
88
89
90 function xcape($s)
91 {
92   $from = array (   '\\',     '@',        '|' );
93   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
94
95   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
96 }
97
98
99 class Card {
100   var $value; /* 0 - 39 card value */
101   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
102   var $owner; /* (table position 0-4) */
103   // var $pos;   /* Pos in hand. */
104   var $x;     /* When played the X position on the table of the owner. */
105   var $y;     /* When played the Y position on the table of the owner. */
106
107   function Card($value, $stat, $owner)
108   {
109     $this->value = $value;
110     $this->stat  = $stat; // Card stat
111     $this->owner = $owner;
112   }
113
114   function assign($stat,$owner)
115   {
116     $this->stat  = $stat; // Card stat
117     $this->owner = $owner;
118   }
119
120   function setpos($pos)
121   {
122     $this->pos   = $pos;
123   }
124
125   function play($x,$y)
126   {
127     $this->stat = 'table'; // Card stat
128     $this->x = $x;
129     $this->y = $y;
130   }
131
132   function take($newown)
133   {
134     $this->stat = 'take'; // Card stat
135     $this->owner = $newown;
136   }
137 }
138
139 class Table {
140   var $player;
141   var $player_n;
142   var $card;
143   var $mazzo;
144   var $gstart;
145   var $turn;
146
147   var $asta_pla;
148   var $asta_pla_n;
149   var $asta_card;
150   var $asta_pnt;
151   
152   var $mult;
153   var $points;    // points array
154   var $points_n;  // number of row of points
155   var $total;
156
157   var $asta_win;
158   var $briscola;
159   var $friend;
160   
161   var $old_reason;
162   var $old_asta_pnt;
163   var $old_pnt;
164   var $old_win;
165   var $old_friend;
166
167   function Table() 
168   {
169   }
170   
171   function &create() 
172   {
173     GLOBAL $G_false;
174
175     if (($thiz = new Table()) == FALSE)
176       return ($G_false);
177
178     $thiz->player    =   array();
179     $thiz->player_n  =   0;
180     $thiz->card      =  &$thiz->bunch_create();
181     $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
182     $thiz->asta_pla_n=  -1;
183     $thiz->asta_card =  -1;
184     $thiz->asta_pnt  =  -1;
185     $thiz->mult      =   1;
186     $thiz->points    =   array( );
187     $thiz->points_n  =   0;
188     $thiz->total     =   array( 0, 0, 0, 0, 0);
189     $thiz->asta_win  =  -1;
190     $thiz->briscola  =  -1;
191     $thiz->friend    =  -1;
192     $thiz->turn      =   0;
193     $thiz->old_reason = "";
194     $thiz->old_asta_pnt = -1;
195     $thiz->old_pnt      = -1;
196     $thiz->old_win   =  -1;
197     $thiz->old_friend=  -1;
198
199     return ($thiz);
200   }
201
202   function &clone(&$from)
203   {
204     GLOBAL $G_false;
205     
206     if (($thiz = new Table()) == FALSE)
207       return ($G_false);
208     
209     $thiz->player = array();
210     for ($i = 0 ; $i < $from->player_n ; $i++)
211       $thiz->player[$i] = $from->player[$i];
212     $thiz->player_n = $from->player_n;
213     $thiz->card = $from->card;
214     $thiz->mazzo = $from->mazzo; // REVIEW
215     $thiz->gstart = $from->gstart;
216     $thiz->turn = $from->turn;
217
218     $thiz->asta_pla = $from->asta_pla;
219     $thiz->asta_pla_n = $from->asta_pla_n;
220     $thiz->asta_card = $from->asta_card;
221     $thiz->asta_pnt = $from->asta_pnt;
222     
223     $thiz->mult = $from->mult;
224     $thiz->points = $from->points;
225     $thiz->points_n = $from->points_n;
226     $thiz->total = $from->total;
227     
228     $thiz->asta_win = $from->asta_win;
229     $thiz->briscola = $from->briscola;
230     $thiz->friend = $from->friend;
231     
232     $thiz->old_reason = $from->old_reason;
233     $thiz->old_asta_pnt = $from->old_asta_pnt;
234     $thiz->old_pnt = $from->old_pnt;
235     $thiz->old_win = $from->old_win;
236     $thiz->old_friend = $from->old_friend;
237
238     return ($thiz);
239   }
240   
241   function &spawn(&$from)
242   {
243     GLOBAL $G_false;
244     
245     if (($thiz =& new Table()) == FALSE)
246       return ($G_false);
247     
248     $thiz->player_n = $from->player_n;
249     $thiz->card = $from->card;
250     $thiz->mazzo = $from->mazzo;
251     $thiz->gstart = $from->gstart;
252     $thiz->turn = $from->turn;
253
254     $thiz->asta_pla = $from->asta_pla;
255     $thiz->asta_pla_n = $from->asta_pla_n;
256     $thiz->asta_card = $from->asta_card;
257     $thiz->asta_pnt = $from->asta_pnt;
258     
259     $thiz->mult = $from->mult;
260     $thiz->points = $from->points;
261     $thiz->points_n = $from->points_n;
262     $thiz->total = $from->total;
263     
264     $thiz->asta_win = $from->asta_win;
265     $thiz->briscola = $from->briscola;
266     $thiz->friend = $from->friend;
267     
268     $thiz->old_reason = $from->old_reason;
269     $thiz->old_asta_pnt = $from->old_asta_pnt;
270     $thiz->old_pnt = $from->old_pnt;
271     $thiz->old_win = $from->old_win;
272     $thiz->old_friend = $from->old_friend;
273
274     // players are rearranged in an dedicated array
275     $thiz->player = array();
276     for ($i = 0 ; $i < $from->player_n ; $i++)
277       $thiz->player[$i] = $i;
278
279     return ($thiz);
280   }
281   
282   function &bunch_create()
283   {
284     $ret = array();
285
286     for ($i = 0 ; $i < 40 ; $i++) {
287       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
288     }
289
290     $oret = &$ret;
291     return ($oret);
292   }
293
294   function bunch_make()
295   {
296     $ct = array(0,0,0,0,0);
297     
298     mt_srand(make_seed());
299     
300     for ($i = 39 ; $i >= 0 ; $i--) 
301       $rest[$i] = $i;
302
303     for ($i = 39 ; $i >= 0 ; $i--) {
304       $rn = rand(0, $i);
305       
306       if ($rn == 0)
307         log_main("RND ZERO", "");
308       
309       $id = $rest[$rn];
310
311       $owner = $i % 5;
312       $this->card[$id]->assign('hand', $owner);
313
314       $rest[$rn] = $rest[$i];
315       // $pubbpos[$rn2] = $pubbpos[$i];
316     }
317   }
318
319   function init(&$userarr)
320   {
321     $this->mazzo    = rand(0,PLAYERS_N-1);
322     $this->points_n = 0;
323     $this->mult     = 1;
324     $this->old_win  =-1;
325     $this->old_reason = "";
326     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
327       $this->total[$i] = 0;
328       $user_cur = &$userarr[$this->player[$i]];
329       $user_cur->exitislock = TRUE;
330     }
331
332     log_main("table::init","ci siamo");
333   }
334
335   function game_init(&$userarr)
336   {
337     log_rd2("XXX", "GSTART 4");
338
339     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
340     $this->bunch_make();
341     
342     
343     $this->asta_pla_n = PLAYERS_N;
344     $this->asta_card = -1;
345     $this->asta_pnt  = 60;
346     $this->asta_win  = -1;
347     $this->briscola  = -1;
348     $this->friend    = -1;
349     $this->turn      =  0;
350     
351     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
352       $this->asta_pla[$i] = TRUE;
353       $user_cur = &$userarr[$this->player[$i]];
354       $user_cur->subst = 'asta';
355       $user_cur->asta_card = -2;
356       $user_cur->asta_pnt  = -1;
357       $user_cur->handpt = $this->hand_points($i);
358       // SEE function calculate_points(&$table)
359     }
360   }
361
362   function game_next()
363   {
364     $this->mazzo  = ($this->mazzo + 1) % PLAYERS_N;
365   }
366
367   function getPlayer($idx)
368   {
369     return ($this->player[$idx]);
370   }
371
372   function setPlayer($idx, $player)
373   {
374     $this->player[$idx] = $player;
375   }
376
377   function user_add($idx)
378   {
379     $this->player[$this->player_n] = $idx;
380     $this->player_n++;
381     
382     return ($this->player_n - 1);
383   }
384   
385   function user_rem(&$room, &$user)
386   {
387     $tabpos = $user->table_pos;
388     
389     /* verifico la consistenza dei dati */
390     if ($room->user[$this->player[$tabpos]] == $user) {
391       
392       /* aggiorna l'array dei giocatori al tavolo. */
393       for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
394         $this->player[$i] = $this->player[$i+1];
395         $user_cur = &$room->user[$this->player[$i]];
396         $user_cur->table_pos = $i;
397       }
398       $this->player_n--;
399     }
400     else {
401       log_main($user->sess, "INCONSISTENCY ON TABLE.");
402     }
403   }
404
405   function hand_points($idx)
406   {
407     GLOBAL $G_all_points; 
408     
409     $tot = 0;
410     
411     for ($i = 0 ; $i < 40 ; $i++) {
412       if ($this->card[$i]->owner != $idx)
413         continue;
414
415       $ctt = $this->card[$i]->value % 10;
416       $tot += $G_all_points[$ctt];
417     }
418
419     return ($tot);
420   }
421
422   function exitlock_show(&$userarr, $table_pos)
423   {
424     $ct = $this->exitlock_calc(&$userarr, $table_pos);
425
426     $ret = sprintf('exitlock_show(%d, %s);', $ct, 
427                    ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
428     return ($ret);
429   }
430
431   function exitlock_calc(&$userarr, $table_pos)
432   {
433     $ct = 0;
434
435     for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {    
436       if ($userarr[$this->player[$i]]->exitislock == FALSE)
437         $ct++;
438     }
439
440     return ($ct);
441   }
442 } // End class Table
443   
444 class User {
445   var $name;       // name of the user
446   var $sess;       // session of the user
447   var $ip;         // ip of the user
448   var $lacc;       // last access (for the cleanup)
449   var $laccwr;     // last access (for the cleanup)
450   var $bantime;    // timeout to temporary ban
451   var $stat;       // status (outdoor, room, table, game, ...)
452   var $subst;      // substatus for each status   
453   var $step;       // step of the current status
454   var $trans_step; // step to enable transition between pages (disable == -1)
455   var $comm;       // commands array
456   var $asta_card;  // 
457   var $asta_pnt;   //
458   var $handpt;     // Total card points at the beginning of the current hand.
459   var $exitislock; // Player can exit from the table ?
460   var $table;      // id of the current table (if in table state)
461   var $table_pos;  // idx on the table
462   var $the_end;    // Flag to change the end of the session
463
464   function User() {
465   }
466
467   function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
468     GLOBAL $G_false;
469
470     if (($thiz =& new User()) == FALSE)
471       return ($G_false);
472
473     $thiz->name  = $name;
474     $thiz->sess  = $sess;
475     $thiz->ip    = $ip;
476     $thiz->lacc   = time();
477     $thiz->laccwr = time();
478     $thiz->bantime = 0;
479     $thiz->stat  = $stat;
480     $thiz->subst  = $subst;
481     $thiz->step  = 1;
482     $thiz->trans_step  = -1;
483     $thiz->comm  = array();
484     $thiz->asta_card = -2;
485     $thiz->asta_pnt  = -1;
486     $thiz->handpt = -1;
487     $thiz->exitislock = TRUE;
488     
489     $thiz->table = $table;
490     
491     return ($thiz);
492   }
493
494   function &clone(&$from)
495   {
496     GLOBAL $G_false;
497     
498     if (($thiz = new User()) == FALSE)
499       return ($G_false);
500     
501     $thiz->name = $from->name;
502     $thiz->sess = $from->sess;
503     $thiz->ip = $from->ip;
504     $thiz->lacc = $from->lacc;
505     $thiz->laccwr = $from->laccwr;
506     $thiz->bantime = $from->bantime;
507     $thiz->stat = $from->stat;
508     $thiz->subst = $from->subst;
509     $thiz->step = $from->step;
510     $thiz->trans_step = $from->trans_step;
511     $thiz->comm = array();
512
513     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
514     for ($i = $i_start ; $i < $from->step ; $i++) {
515       $ii = $i % COMM_N;
516       $thiz->comm[$ii]   = $from->comm[$ii];
517     }
518     $thiz->asta_card = $from->asta_card;
519     $thiz->asta_pnt = $from->asta_pnt;
520     $thiz->handpt = $from->handpt;
521     $thiz->exitislock = $from->exitislock;
522     $thiz->table = $from->table;
523     $thiz->table_pos = $from->table_pos;
524     $thiz->the_end = $from->the_end;
525
526     return ($thiz);
527   }
528   
529   function &spawn(&$from, $table, $table_pos)
530   {
531     GLOBAL $G_false;
532     
533     if (($thiz =& new User()) == FALSE)
534       return ($G_false);
535     
536     $thiz->name       = $from->name;
537     $thiz->sess       = $from->sess;
538     $thiz->ip         = $from->ip;
539     $thiz->lacc       = $from->lacc;
540     $thiz->laccwr     = $from->laccwr;
541     $thiz->bantime    = $from->bantime;
542     $thiz->stat       = $from->stat;
543     $thiz->subst      = $from->subst;
544     $thiz->step       = $from->step;
545     $thiz->trans_step = $from->trans_step;
546     $thiz->comm       = array();
547
548     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
549     for ($i = $i_start ; $i < $from->step ; $i++) {
550       log_wr("XX", "TRY PUSH:".$i);
551       $ii = $i % COMM_N;
552       $thiz->comm[$ii]   = $from->comm[$ii];
553     }
554     
555     $thiz->asta_card  = $from->asta_card;
556     $thiz->asta_pnt   = $from->asta_pnt;
557     $thiz->handpt     = $from->handpt;
558     $thiz->exitislock = $from->exitislock;
559     $thiz->the_end    = $from->the_end;
560
561     $thiz->table      = $table;
562     $thiz->table_pos  = $table_pos;
563
564     return ($thiz);
565   }
566   
567   function stat_set($stat) {
568     $this->stat = "$stat";
569     
570     /*
571     if (validate_sess($this->sess)) {
572       $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
573       fwrite($fp, sprintf("%s\n",$this->stat));
574       fclose($fp);
575     }
576     */
577   }
578
579   function step_set($step) {
580     $this->step = $step;
581     
582     do {
583       if (validate_sess($this->sess) == FALSE)
584         break;
585       if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
586         break;
587       fwrite($fp, pack("l",$this->step), 4);
588       fclose($fp);
589
590       return (TRUE);
591     } while (0);
592
593     return (FALSE);
594   }
595
596   function step_inc() {
597     $this->step++;
598     
599     if (validate_sess($this->sess)) {
600       $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
601       fwrite($fp, pack("l",$this->step), 4);
602       fclose($fp);
603
604       return (TRUE);
605     }
606     
607     return (FALSE);
608   }
609 }
610
611 function step_get($sess) {
612   $fp = FALSE;
613   $ct = 0;
614   do {
615     if (validate_sess($sess) == FALSE)
616       break;
617   $ct = 1;
618     if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
619       break;
620   $ct = 2;
621     if (($s = fread($fp, 4)) == FALSE)
622       break;
623   $ct = 3;
624     if (strlen($s) != 4)
625       break;
626   $ct = 4;
627     $arr = unpack('l', $s);
628     fclose($fp);
629
630     // log_rd2($sess, "A0: ".$arr[0]."  A1: ".$arr[1]);
631     return ($arr[1]);
632   } while (0);
633
634   if ($fp != FALSE)
635     fclose($fp);
636
637   log_rd2($sess, "STEP_GET: return false ".$ct);
638   return (FALSE);
639 }
640
641 function step_unproxy($sess) {
642   log_rd2($sess, "UNPROXY: ".PROXY_PATH."/".$sess.".step");
643   @unlink(PROXY_PATH."/".$sess.".step");
644 }
645
646
647 class Room {
648   var $user;
649   var $table;
650   var $comm; // commands for many people
651   var $step; // current step of the comm array
652   var $garbage_timeout;
653
654   function Room () {
655     $this->user = array();
656
657     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
658       $this->user[$i] =& User::create("", "");
659     }
660
661     for ($i = 0 ; $i < TABLES_N ; $i++) 
662       $this->table[$i] =& Table::create();
663     $this->garbage_timeout = 0;
664   }
665
666   function garbage_manager($force)
667   {
668     
669     /* Garbage collector degli utenti in timeout */
670     $curtime = time();
671     if ($force || $this->garbage_timeout < $curtime) {
672       
673       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
674         $user_cur = &$this->user[$i];
675         if ($user_cur->sess == "")
676           continue;
677         
678         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
679           log_rd2($user_cur->sess, "AUTO LOGOUT.");
680
681           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
682             log_auth($user_cur->sess, "Autologout session.");
683             
684             $tmp_sess = $user_cur->sess;
685             $user_cur->sess = "";
686             step_unproxy($tmp_sess);
687             $user_cur->name = "";
688             $user_cur->the_end = FALSE;
689             
690             log_rd2($user_cur->sess, "AUTO LOGOUT.");
691             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
692               $this->room_wakeup(&$user_cur);
693             else if ($user_cur->subst == 'standup')
694               $this->room_outstandup(&$user_cur);
695             else
696               log_rd2($sess, "LOGOUT FROM WHAT ???");
697           }
698         }
699
700         if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
701           if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
702             $this->room_wakeup(&$user_cur);
703             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
704             $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);
705             $user_cur->step_inc();
706           }
707         }
708       }
709       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
710       
711       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
712     }
713
714     // BAN_IP_CLEAN
715
716   }
717
718
719   function room_wakeup(&$user)
720   {
721     $table_idx = $user->table;
722     $table = &$this->table[$table_idx];
723
724     log_main("WAKEUP", "begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
725
726     $curtime = time();
727
728     $from_table = ($user->stat == "table");
729     if ($from_table) {
730       log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
731
732       for ($i = 0 ; $i < $table->player_n ; $i++) {
733         $user_cur = &$this->user[$table->player[$i]];
734         log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
735
736         if ($user_cur != $user) {
737           $user_cur->stat_set("room");
738           $user_cur->subst = "sitdown";
739           $user_cur->laccwr = $curtime;
740         }
741         else if ($user->sess != "") {
742           $user_cur->stat_set("room");
743           $user_cur->subst = "standup";
744           $user_cur->laccwr = $curtime;
745           $user_cur->table = -1;
746         }
747       }
748     }
749     else {
750       $user->stat_set("room");
751       $user->subst = "standup";
752       $user->laccwr = $curtime;
753     }
754     /* aggiorna l'array dei giocatori al tavolo. */
755     $table->user_rem(&$this, &$user);
756
757     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
758       $user_cur = &$this->user[$i];
759       if ($user_cur->sess == '' || $user_cur->stat != 'room')
760         continue;
761       
762       log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
763
764       $ret = "gst.st = ".($user_cur->step+1)."; ";
765       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
766         $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
767         // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
768         log_main("DOCUMENT.index.php", "from table");
769       }
770       else if ($user_cur->stat == "room") {
771         log_main("DOCUMENT.index.php", "from table");
772
773         $ret .= table_content($this, $user_cur, $table_idx);
774         $ret .= standup_content($this, $user_cur);
775         
776         $act_content = table_act_content(FALSE, 0, $table_idx, $user->table);
777         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
778         
779         
780         if ($user_cur == $user) {
781           // set the new status 
782           $ret .=  'subst = "standup"; ';
783           // clean the action buttons in other tables
784           for ($e = 0 ; $e < TABLES_N ; $e++) {
785             if ($this->table[$e]->player_n < PLAYERS_N)
786               $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user->table));
787           }
788         }
789         else {
790           $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
791           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
792         }
793       }
794       log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
795       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
796       $user_cur->step_inc();
797     }
798   }
799   
800
801
802
803   function room_outstandup(&$user)
804   {
805     $this->room_sitdown(&$user, -1);
806   }
807   
808   function table_update(&$user)
809   {
810     log_main("table_update", "pre - USER: ".$user->name);
811
812     $table_idx = $user->table;
813
814     if ($table_idx > -1) 
815       $table = &$this->table[$table_idx];
816     
817     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
818       $ret = "";
819       $user_cur = &$this->user[$i];
820       if ($user_cur->sess == '' || $user_cur->stat != 'room')
821       continue;
822       
823       $ret = "gst.st = ".($user_cur->step+1)."; ";
824       if ($table_idx > -1)
825         $ret .= table_content($this, $user_cur, $table_idx);
826       
827       if ($user_cur == $user) {
828         $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
829       }
830       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
831       $user_cur->step_inc();
832     }
833
834     log_main("table_update", "post");
835   }
836
837   function room_sitdown(&$user, $table_idx)
838   {
839     log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
840
841     if ($table_idx > -1) 
842       $table = &$this->table[$table_idx];
843     
844     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
845       $ret = "";
846       $user_cur = &$this->user[$i];
847       if ($user_cur->sess == '' || $user_cur->stat != 'room')
848       continue;
849       
850       $ret = "gst.st = ".($user_cur->step+1)."; ";
851       if ($table_idx > -1)
852       $ret .= table_content($this, $user_cur, $table_idx);
853       $ret .= standup_content($this, $user_cur);
854       
855       if ($user_cur == $user) {
856         $ret .=  'subst = "sitdown"; ';
857         // clean the action buttons in other tables
858         for ($e = 0 ; $e < TABLES_N ; $e++) {
859           $act_content = table_act_content(FALSE, 0, $e, $user_cur->table);
860           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, $act_content);
861         }
862       }
863       else if ($table_idx > -1) {
864         if ($table->player_n == PLAYERS_N) {
865           $act_content = table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table);
866           $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
867         }
868       }
869       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
870       $user_cur->step_inc();
871     }
872   }
873
874   function chatt_send(&$user, $mesg)
875   {
876     if ($user->stat == 'table') {
877       $table = &$this->table[$user->table];
878     }
879     
880     $user_mesg = substr($mesg,6);
881     
882     $timecur = time();
883     
884     $dt = date("H:i ", $timecur);
885     if (strncmp($user_mesg, "/nick ", 6) == 0) {
886       log_main($user->sess, "chatt_send BEGIN");
887
888       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
889           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
890           $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));
891           $user->step_inc();
892
893           return;
894       }
895       $user_mesg = "COMMAND ".$user_mesg;
896       // Search dup name
897       // change
898       // update local graph
899       // update remote graphs
900       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
901         $user_cur = &$this->user[$i];
902         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
903         if ($user_cur->sess == '')
904           continue;
905         if ($user_cur->name == $name_new) {
906           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
907           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
908           $user->step_inc();
909           break;
910         }
911       }
912       if ($i == MAX_PLAYERS) {
913         $user->name = $name_new;
914
915       log_main($user->sess, "chatt_send start set");
916         
917
918         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
919           log_main($user->sess, "chatt_send set loop");
920           
921           $user_cur = &$this->user[$i];
922           if ($user_cur->sess == '')
923             continue;
924           if ($user_cur->stat == 'room') {
925             if ($user->stat == 'room' && $user->subst == 'standup') {
926               $this->standup_update(&$user);
927             }
928             else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
929                      $user->stat == 'table') {
930               log_main($user->sess, "chatt_send pre table update");
931
932               $this->table_update(&$user);
933
934               log_main($user->sess, "chatt_send post table update");
935             }
936           }
937           else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
938             $table = &$this->table[$user->table];
939             
940             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
941             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
942                 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
943                 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
944                 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
945                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
946                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
947             if ($user_cur == $user)
948               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
949                                                                    xcape($user->name,ENT_COMPAT,"UTF-8"));
950             $user_cur->step_inc();
951           }
952         }
953       }
954     }
955     else {
956       for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
957         if ($user->stat == 'room') {
958           $user_cur = &$this->user[$i];
959           if ($user_cur->sess == '' || $user_cur->stat != 'room')
960             continue;
961         }
962         else {
963           $user_cur = &$this->user[$table->player[$i]];
964         }
965         
966         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
967         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
968                                                              $dt.xcape($user->name), xcape($user_mesg));
969         $user_cur->step_inc();
970       }
971       log_legal($timecur, $user->sess, $user->name, 
972                 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
973     }
974   }
975
976   function &get_user($sess, &$idx)
977   {
978     GLOBAL $PHP_SELF, $G_false;
979
980     if (validate_sess($sess)) {
981       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
982         if (strcmp($sess, $this->user[$i]->sess) == 0) {
983           // find it
984           $idx = $i;
985           $ret = &$this->user[$i];
986           return ($ret);
987         }
988       }
989       log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
990       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
991       // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
992     }
993     else {
994       log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
995     }
996
997     return ($G_false);
998   }
999
1000   /*
1001    * function &add_user(&$room, &$sess, &$idx, $name, $ip)
1002    *
1003    * RETURN VALUE:
1004    *   if ($idx != -1 && ret == FALSE)  =>  duplicated nick
1005    *   if ($idx == -2 && ret == FALSE)  =>  invalid name
1006    *   if ($idx == -1 && ret == FALSE)  =>  no space left
1007    *   if (ret == TRUE)                 =>  SUCCESS
1008    */
1009   function &add_user(&$sess, &$idx, $name, $ip)
1010   {
1011     GLOBAL $G_false;
1012
1013     $idx = -1;
1014     $idfree = -1;
1015     
1016     if (($name_new = validate_name($name)) == FALSE) {
1017       $idx = -2;
1018       return ($G_false);
1019     }
1020
1021     log_auth("XXX", sprintf("ARRIVA: [%s]", $sess));
1022     if (validate_sess($sess) == FALSE) 
1023       $sess = "";
1024
1025     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1026       /* free user ? */
1027       if (strcmp($sess, $this->user[$i]->sess) == 0) {
1028         if ($idx == -1)
1029           $idx = $i;
1030       }
1031       if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1032         $idfree = $i;
1033       }
1034       if (strcmp($this->user[$i]->name, $name_new) == 0) {
1035         $idx = $i;
1036         break;
1037       }
1038     }
1039     if ($idx == -1)
1040       $idx = $idfree;
1041
1042     log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1043
1044     if ($idx != -1 && $i == MAX_PLAYERS) {
1045       /* SUCCESS */
1046       $curtime = time();
1047       if ($sess == "") {
1048         $this->user[$idx]->sess = uniqid("");
1049         $sess = $this->user[$idx]->sess;
1050         
1051       }
1052       else {
1053         $this->user[$idx]->sess = $sess;
1054       }
1055       $this->user[$idx]->name = $name_new;
1056       $this->user[$idx]->stat_set("room");
1057       // MOP $this->user[$idx]->step_set(0);
1058       $this->user[$idx]->subst = "standup";
1059       $this->user[$idx]->lacc =   $curtime;
1060       $this->user[$idx]->laccwr = $curtime;
1061       $this->user[$idx]->bantime = 0;
1062       $this->user[$idx]->ip = $ip;
1063       log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1064       
1065       return ($this->user[$idx]);
1066     }
1067
1068     return ($G_false);
1069   }
1070   
1071   function standup_update(&$user)
1072   {
1073     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1074       $user_cur = &$this->user[$i];
1075       if ($user_cur->sess == '')
1076         continue;
1077
1078       log_main("STANDUP START", $user_cur->stat);
1079       
1080       if ($user_cur->stat == 'room') {
1081         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
1082         if ($user_cur == $user)
1083           $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
1084         
1085         log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1086         
1087         $user_cur->step_inc();
1088       }
1089     }
1090   }
1091
1092   // Static functions
1093   function &init_data()
1094   {
1095     $room =& new Room();
1096     
1097     return $room;
1098   }
1099   
1100
1101   function &load_data() 
1102   {
1103     GLOBAL $G_false, $sess;
1104     
1105     if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1106       echo "FTOK FAILED";
1107       exit;
1108     }
1109     
1110     if ($shm = shm_attach($tok, SHM_DIMS)) {
1111       $room = @shm_get_var($shm, $tok);
1112       
1113       log_only($sess, "bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
1114       if (isset($room)) 
1115         log_only($sess, "bri count ".count($room));
1116       
1117       if ($room == FALSE) {
1118         log_only($sess, "INIT MAIN DATA");
1119         
1120         $room =& Room::init_data();
1121         if (shm_put_var($shm, $tok, $room) == FALSE) {
1122           log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
1123           log_only($sess, serialize($room));
1124         }
1125       }
1126       
1127       shm_detach($shm);
1128       
1129       $ret = &$room;
1130       return ($ret);
1131     }
1132     
1133     return ($G_false);
1134   }
1135   
1136
1137   function save_data(&$room) 
1138   {
1139     GLOBAL $sess;
1140     
1141     $ret =   FALSE;
1142     $shm =   FALSE;
1143     $isacq = FALSE;
1144     
1145     // var_dump($room);
1146     
1147     if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
1148       return (FALSE);
1149     
1150     do {
1151       $isacq = TRUE;
1152       
1153       if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
1154         break;
1155       
1156       // log_only($sess, "PUT_VAR DI ".strlen(serialize($room)));
1157       if (shm_put_var($shm, $tok, $room) == FALSE) {
1158         log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
1159         log_only($sess, serialize($room));
1160         break;
1161       }
1162       // log_main("XXX", "QUI CI ARRIVA [".$room->user[0]->name."]");
1163       $ret = TRUE;
1164     } while (0);
1165     
1166     if ($shm)
1167       shm_detach($shm);
1168     
1169     return ($ret);
1170   }
1171
1172   function lock_data()
1173   {
1174     GLOBAL $sess; 
1175     
1176     //  echo "LOCK: ".FTOK_PATH."/main";
1177     //  exit;
1178     if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
1179       echo "FTOK FAILED";
1180       exit;
1181     }
1182     // echo "FTOK ".$tok."<br>";
1183     if (($res = sem_get($tok)) == FALSE) {
1184       echo "SEM_GET FAILED";
1185       exit;
1186     }
1187     if (sem_acquire($res)) {   
1188       log_only($sess, "LOCK");
1189       return ($res);
1190     }
1191     else
1192       return (FALSE);
1193   }
1194   
1195   function unlock_data($res)
1196   {
1197     GLOBAL $sess; 
1198     
1199     log_only($sess, "UNLOCK");
1200
1201     return (sem_release($res));
1202   }
1203 } // end class Room
1204
1205 function make_seed()
1206 {
1207   list($usec, $sec) = explode(' ', microtime());
1208   return (float) $sec + ((float) $usec * 100000);
1209 }
1210
1211 function log_only2($sess, $log) {
1212   if (BRISK_DEBUG != TRUE)
1213     return;
1214   
1215
1216   if (($fp = @fopen("/tmp/brisk_only2.log", 'a')) != FALSE) {
1217     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1218     fclose($fp);
1219   }
1220 }
1221
1222 function log_only($sess, $log) {
1223   if (BRISK_DEBUG != TRUE)
1224     return;
1225   
1226
1227   if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
1228     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1229     fclose($fp);
1230   }
1231 }
1232
1233 function log_main($sess, $log) {
1234   if (BRISK_DEBUG != TRUE)
1235     return;
1236
1237   if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
1238     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1239     fclose($fp);
1240   }
1241 }
1242
1243 function log_rd($sess, $log) {
1244   if (BRISK_DEBUG != TRUE)
1245     return;
1246
1247   if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
1248     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1249     fclose($fp);
1250   }
1251 }
1252
1253 function log_rd2($sess, $log) {
1254   if (BRISK_DEBUG != TRUE)
1255     return;
1256
1257   if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
1258     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1259     fclose($fp);
1260   }
1261 }
1262
1263 function log_send($sess, $log) {
1264   if (BRISK_DEBUG != TRUE)
1265     return;
1266
1267   if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
1268     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1269     fclose($fp);
1270   }
1271 }
1272
1273 function log_auth($sess, $log) {
1274   if (BRISK_DEBUG != TRUE)
1275     return;
1276
1277   if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
1278     fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1279     fclose($fp);
1280   }
1281 }
1282
1283 function log_lock($sess, $log) {
1284   if (BRISK_DEBUG != TRUE)
1285     return;
1286
1287   if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
1288     fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
1289     fclose($fp);
1290   }
1291 }
1292
1293 function log_wr($sess, $log) {
1294   if (BRISK_DEBUG != TRUE && FALSE)
1295     return;
1296
1297   if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
1298     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1299     fclose($fp);
1300   }
1301 }
1302
1303 function log_load($sess, $log) {
1304   if (BRISK_DEBUG != TRUE)
1305     return;
1306
1307   if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
1308     fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
1309     fclose($fp);
1310   }
1311 }
1312
1313 function log_legal($timecur, $sess, $name, $where, $mesg) 
1314 {
1315   GLOBAL $_SERVER;
1316
1317   if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
1318     /* Unix time | session | nickname | IP | where was | mesg */
1319     fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
1320     fclose($fp);
1321   }
1322 }
1323
1324
1325
1326
1327 function lock_banlist()
1328 {
1329   if (($tok = ftok(FTOK_PATH."/main", "L")) == -1) {
1330     echo "FTOK FAILED";
1331     exit;
1332   }
1333   if (($res = sem_get($tok)) == FALSE) {
1334     echo "SEM_GET FAILED";
1335     exit;
1336   }
1337   if (sem_acquire($res)) 
1338     return ($res);
1339   else
1340     return (FALSE);
1341 }
1342
1343 function unlock_banlist($res)
1344 {
1345   return (sem_release($res));
1346 }
1347
1348 function table_act_content($isstanding, $sitted, $table, $cur_table)
1349 {
1350   $ret = "";
1351
1352   if ($isstanding) {
1353     if ($sitted < PLAYERS_N) {
1354       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
1355     }
1356   }
1357   else {
1358     if ($table == $cur_table)
1359       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
1360     else
1361       $ret = "";
1362   }
1363   return ($ret);
1364 }
1365
1366 function table_content($room, $user, $table_idx)
1367 {
1368   $content = "";
1369   $ret = "";
1370   // TODO
1371   //
1372   //   Si possono usare i dati nella classe table
1373   //
1374
1375   $sess = $user->sess;
1376   $table = &$room->table[$table_idx];
1377
1378   if ($user->stat != 'room')
1379     return;
1380
1381   for ($i = 0 ; $i < $table->player_n ; $i++) {
1382     $user_cur = &$room->user[$table->player[$i]];
1383
1384     if ($user_cur == $user) 
1385         { $hilion = "<b>"; $hilioff = "</b>"; }
1386       else
1387         { $hilion = ""; $hilioff = ""; }
1388
1389     log_main($user_cur->name, sprintf("IN TABLE [%d]", $table_idx));
1390     
1391     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1392   }
1393   /*
1394   for ( ; $i < PLAYERS_N ; $i++)
1395     $content .= "<br>";
1396   */
1397
1398   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1399   
1400   return ($ret);
1401 }
1402
1403 function standup_content(&$room, $user)
1404 {
1405   $ret = "";
1406   $content = "";
1407
1408   if ($user->stat != 'room')
1409     return;
1410
1411   for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1412     if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1413       continue;
1414     $ct++;
1415   }
1416
1417   $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1418
1419   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1420     if ($room->user[$e]->sess == "" || $room->user[$e]->stat != "room" || $room->user[$e]->name == "")
1421       continue;
1422
1423
1424     if ($room->user[$e]->subst == "standup") {
1425       if (($ct % 4) == 0) {
1426         $content .= '<tr>';
1427       }
1428       if ($room->user[$e] == $user) 
1429         { $hilion = "<b>"; $hilioff = "</b>"; }
1430       else
1431         { $hilion = ""; $hilioff = ""; }
1432
1433       $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($room->user[$e]->name), $hilioff);
1434       if (($ct % 4) == 3) {
1435         $content .= '</tr>';
1436       }
1437       $ct++;
1438     }
1439   }
1440   $content .= '</table>';
1441         
1442   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1443   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
1444                   $content, $content2);
1445
1446   return ($ret);
1447 }
1448
1449
1450 function show_notify($text, $tout, $butt, $w, $h)
1451 {
1452   log_main("SHOW_NOTIFY", $text);
1453   return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1454 }
1455
1456 function briscola_show($room, $table, $user)
1457 {
1458   $ptnadd = "";
1459   $ret = "";
1460
1461   if ($table->asta_card == 9) 
1462     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1463   
1464   /* text of caller cell */
1465   if ($user->table_pos == $table->asta_win) 
1466     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1467   else 
1468     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
1469                     xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
1470
1471   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1472                   $table->asta_win);
1473   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1474   $ret .= sprintf('$("caller").style.visibility = "visible";');
1475   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1476   $ret .= sprintf('$("astalascio").style.visibility = "";');
1477   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1478   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1479   
1480   return ($ret);
1481 }
1482
1483
1484 function game_result($asta_pnt, $pnt)
1485 {
1486   if ($asta_pnt == 61) {
1487     if ($pnt > 60)
1488       return (1);
1489     else if ($pnt == 60)
1490       return (0);
1491     else
1492       return (-1);
1493   }
1494   else {
1495     if ($pnt >= $asta_pnt)
1496       return (1);
1497     else
1498       return (-1);
1499   }
1500 }
1501
1502 function multoval($mult)
1503 {
1504   if ($mult == 2)
1505     return ("doppio");
1506   else if ($mult == 4)
1507     return ("quadruplo");
1508   else
1509     return (sprintf("%d-plo", $mult));
1510 }
1511
1512 function show_table_info(&$room, &$table, $table_pos)
1513 {
1514   $ret = "";
1515   $user = &$room->user[$table->player[$table_pos]];
1516
1517   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1518   $noty = sprintf('<table class=\"points\"><tr><th></th>');
1519   
1520   // Names.
1521   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1522     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
1523   $noty .= sprintf("</tr>");
1524
1525   // Points.
1526   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1527
1528   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1529     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1530     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1531       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1532     $noty .= "</tr>";
1533   }
1534
1535   // Total points.
1536   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1537   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1538     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1539   $noty .= "</tr></table>";
1540
1541   if ($table->old_reason != "") {
1542     $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1543   }
1544
1545   if ($table->old_win != -1) {
1546     $win = $table->player[$table->old_win];
1547     $fri = $table->player[$table->old_friend];
1548
1549     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1550
1551     if ($win != $fri) {
1552       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
1553                        xcape($room->user[$win]->name),
1554                        xcape($room->user[$fri]->name));
1555       if ($table->old_pnt == 120) {
1556         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1557       }
1558       else {
1559         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1560                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1561                           'pi&ugrave; di 60'), $table->old_pnt,
1562                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1563       }
1564     }
1565     else {
1566       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1567                        xcape($room->user[$win]->name));
1568       if ($table->old_pnt == 120) {
1569         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1570       }
1571       else {
1572         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1573                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1574                           'pi&ugrave; di 60'), $table->old_pnt,
1575                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1576       }
1577     }
1578   }
1579   if ($table->mazzo == $table_pos) 
1580     $noty .= "Fai <b>tu</b> il mazzo,";
1581   else {
1582     $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
1583     $noty .= "Il mazzo a <b>$unam</b>,";
1584   }
1585
1586   if ($user->subst == 'asta') {
1587     if ($table->asta_win == -1)  // auction case
1588       $curplayer = $table->gstart % PLAYERS_N;
1589     else 
1590       $curplayer = $table->asta_win;
1591   }
1592   else if ($user->subst == 'game') {
1593     $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1594   }
1595
1596
1597   if ($curplayer == $table_pos) {
1598     $noty .= " tocca a <b>te</b> giocare.";
1599   }
1600   else {
1601     $unam = xcape($room->user[$table->player[$curplayer]]->name);
1602     $noty .= " tocca a <b>$unam</b> giocare.";
1603   }
1604
1605   if ($table->mult > 1) {
1606     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1607   }
1608   $noty .= "<hr><br>";
1609
1610   $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1611   
1612   return ($ret);
1613 }
1614
1615 function root_wellcome($user)
1616 {
1617   GLOBAL $root_wellarr;
1618   $ret = "";
1619
1620   for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1621     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1622
1623   return ($ret);
1624 }
1625
1626 function table_wellcome($user)
1627 {
1628   GLOBAL $table_wellarr;
1629   $ret = "";
1630
1631   for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1632     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1633
1634   return ($ret);
1635 }
1636
1637 function show_room(&$room, &$user)
1638 {
1639   $ret = sprintf('gst.st = %d;',  $user->step);
1640   $ret .= sprintf('stat = "%s";',  $user->stat);
1641
1642   $ret .= root_wellcome($user);
1643   $ret .= sprintf('subst = "%s";', $user->subst);
1644   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1645   for ($i = 0 ; $i < TABLES_N ; $i++) {
1646     $ret .= table_content($room, $user, $i);
1647     $act_content = table_act_content(($user->subst == 'standup'), 
1648                                      $room->table[$i]->player_n, $i, $user->table);
1649     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1650   }
1651   $ret .= standup_content($room, $user);
1652   
1653   return ($ret);
1654 }
1655
1656
1657
1658 /* show table 
1659 is_transition (is from room to table ?)
1660 is_again      (is another game)
1661
1662 Examples                    of $is_transition, $is_again:
1663   from reload of the page:              FALSE, FALSE
1664   from sitdown in room:                  TRUE, FALSE
1665   from table: asta cmd e tutti passano:  TRUE, TRUE
1666   from table: fine partita:              TRUE, TRUE
1667  */
1668 function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
1669 {
1670   $table_idx = $user->table;
1671   $table = &$room->table[$table_idx];
1672   $table_pos = $user->table_pos;
1673
1674   $ret = "table_init();";
1675   $ret .= $table->exitlock_show(&$room->user, $table_pos);
1676   if (!$is_again) {
1677     /* GENERAL STATUS */
1678     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1679                      $sendstep, $user->stat, $user->subst, $table_pos);
1680     /* BACKGROUND */
1681     $ret .= "background_set();";
1682     
1683     /* USERS INFO */
1684     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1685     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1686                     xcape($room->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1687                     xcape($room->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1688                     xcape($room->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1689                     (PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1690                     (PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1691   }
1692   /* NOTIFY FOR THE CARD MAKER */
1693   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1694     $ret .= show_table_info(&$room, &$table, $table_pos);
1695   }
1696   if (!$is_again) 
1697     $ret .= table_wellcome($user);
1698
1699   if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1700     $ret .= playsound("cow.mp3");
1701   }
1702
1703
1704   /* CARDS */
1705   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1706     $ret .= "|";
1707     
1708     for ($i = 0 ; $i < 8 ; $i++) {
1709       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1710         $ct = 0;
1711         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1712           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1713             $ct++;
1714             if ($ct == $i+1)
1715               break;
1716           }
1717         }
1718         log_rd($user->sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1719         
1720         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1721                          $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1722                               $table->card[$o]->value : -1), 
1723                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1724       }
1725     }    
1726   }
1727   else {
1728     $taked  = array(0,0,0,0,0);
1729     $inhand = array(0,0,0,0,0);
1730     $ontabl  = array(-1,-1,-1,-1,-1);
1731     $cards  = array();
1732
1733     for ($i = 0 ; $i < 40 ; $i++) {
1734       if ($table->card[$i]->stat == 'hand') {
1735         if ($table->card[$i]->owner == $table_pos) {
1736           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1737         }
1738         $inhand[$table->card[$i]->owner]++;
1739       }
1740       else if ($table->card[$i]->stat == 'take') {
1741         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1742         $taked[$table->card[$i]->owner]++;
1743       }
1744       else if ($table->card[$i]->stat == 'table') {
1745         $ontabl[$table->card[$i]->owner] = $i;
1746       }
1747     }
1748     $logg = "\n";
1749     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1750       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1751     }
1752     log_main("Stat table:", $logg);
1753
1754     /* Set ours cards. */
1755     $oursarg = "";
1756     for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
1757       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1758     for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
1759       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1760     $ret .= sprintf('card_setours(%s);', $oursarg);
1761
1762     /* Dispose all cards */
1763     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1764       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1765       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1766                       $inhand[$i], $taked[$i]);
1767
1768       if ($ontabl[$i] != -1) {
1769         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1770                         $table->card[$ontabl[$i]]->value, 
1771                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1772       }
1773     }
1774   }
1775
1776   /* Show auction */
1777   if ($user->subst == 'asta') {
1778
1779     /* show users auction status */
1780     $showst = "";
1781     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1782       $user_cur = &$room->user[$table->player[$i]];
1783       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1784                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1785     }
1786     if (PLAYERS_N == 3)
1787         $showst .= ",-2,-2";
1788     $ret .= sprintf('show_astat(%s);', $showst);
1789
1790     if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1791       /* show card chooser */
1792       $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1793                       $table->asta_card);
1794     }
1795     else {
1796       /* show auction */
1797       if ($table_pos == ($table->gstart % PLAYERS_N) &&
1798           $table->asta_win == -1) 
1799         $ret .= sprintf('dispose_asta(%d,%d, %s);', 
1800                         $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1801       else
1802         $ret .= sprintf('dispose_asta(%d,%d, %s);',
1803                         $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1804     }
1805
1806     /* Remark */
1807     if ($table->asta_win == -1) { // auction case
1808       if ($table_pos == ($table->gstart % PLAYERS_N)) 
1809         $ret .= "remark_on();";
1810       else
1811         $ret .= "remark_off();";
1812     }
1813     else { // chooseed case
1814       if ($table_pos == $table->asta_win) 
1815         $ret .= "remark_on();";
1816       else
1817         $ret .= "remark_off();";
1818     }
1819   }
1820   else if ($user->subst == 'game') {
1821     /* HIGHLIGHT */
1822     if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos) 
1823       $ret .= "is_my_time = true; remark_on();";
1824     else
1825       $ret .= "remark_off();";
1826     
1827     /* WHO CALL AND WATH */
1828     $ret .= briscola_show($room, $table, $user);
1829     
1830   }
1831   return ($ret);
1832 } // end function show_table(...
1833
1834 function calculate_winner(&$table)
1835 {
1836   $briontab = FALSE;
1837   $ontab = array();
1838   $ontid = array();
1839   $cur_win  =  -1;
1840   $cur_val  = 100;
1841   $cur_seed = $table->briscola - ($table->briscola % 10);
1842
1843   for ($i = 0 ; $i < 40 ; $i++) {
1844     if ($table->card[$i]->stat != "table")
1845       continue;
1846
1847     log_wr($sess, sprintf("Card On table: [%d]", $i));
1848
1849     $v = $table->card[$i]->value; 
1850     $ontab[$table->card[$i]->owner] = $v;
1851     $ontid[$table->card[$i]->owner] = $i;
1852     /* se briscola setto il flag */
1853     if (($v - ($v % 10)) == $cur_seed)
1854       $briontab = TRUE;
1855   }
1856
1857   if ($briontab == FALSE) {
1858     $cur_win  = $table->gstart;
1859     $cur_val  = $ontab[$cur_win];
1860     $cur_seed = $cur_val - ($cur_val % 10);
1861   }
1862
1863   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1864     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1865       if ($ontab[$i] < $cur_val) {
1866         $cur_val = $ontab[$i];
1867         $cur_win = $i;
1868       }
1869     }
1870   }
1871
1872   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1873     $table->card[$ontid[$i]]->owner = $cur_win;
1874     $table->card[$ontid[$i]]->stat =  "take"; // Card stat
1875   }
1876   return ($cur_win);
1877 }
1878
1879 function calculate_points(&$table)
1880 {
1881   GLOBAL $G_all_points; 
1882
1883   $pro = 0;
1884
1885   if ($table->asta_pnt == 60)
1886     $table->asta_pnt = 61;
1887
1888   $table->old_reason = "";
1889   $table->old_win = $table->asta_win;
1890   $table->old_friend = $table->friend;
1891   $table->old_asta_pnt = $table->asta_pnt;
1892
1893   for ($i = 0 ; $i < 40 ; $i++) {
1894     $ctt = $table->card[$i]->value % 10;
1895     $own = $table->card[$i]->owner;
1896     if ($own == $table->asta_win || $own == $table->friend) 
1897       $pro += $G_all_points[$ctt];
1898   }
1899
1900   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1901
1902   
1903   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1904     $table->points[$table->points_n % MAX_POINTS] = array();
1905     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1906       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1907     $table->points_n++;
1908     $table->old_pnt = $pro;
1909     $table->mult *= 2;
1910
1911     return;
1912   }
1913
1914   if ($pro >= $table->asta_pnt) 
1915     $sig = 1;
1916   else
1917     $sig = -1;
1918
1919   $table->points[$table->points_n % MAX_POINTS] = array();
1920   for ($i = 0 ; $i < 5 ; $i++) {
1921     if ($i == $table->asta_win) 
1922       $pt = ($i == $table->friend ? 4 : 2);
1923     else if ($i == $table->friend) 
1924       $pt = 1;
1925     else
1926       $pt = -1;
1927
1928     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1929
1930     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1931
1932     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1933     
1934     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1935     $table->total[$i] += $pt;
1936   }
1937   $table->points_n++;
1938   $table->old_pnt = $pro;
1939   $table->mult = 1;
1940 }
1941
1942 function validate_sess($sess) 
1943 {
1944   if (strlen($sess) == SESS_LEN) 
1945     return (TRUE);
1946   else
1947     return (FALSE);
1948 }
1949
1950 function validate_name($name) 
1951 {
1952   $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1953
1954   for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1955     $c = $name_new[$i];
1956     if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1957       return ($name_new);
1958   }
1959
1960   return (FALSE);
1961 }
1962
1963 function playsound($filename)
1964 {
1965   return (sprintf('playsound("flasou", "%s");', $filename));
1966 }
1967
1968 function secstoword($secs)
1969 {
1970   $mins = floor($secs / 60);
1971   $secs = $secs % 60;
1972   if ($mins > 0) 
1973     $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
1974   
1975   if ($secs > 0)
1976     $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
1977   
1978   return ($ret);
1979 }
1980
1981 ?>