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