921baae7b34cd069418b2bb2e875e75141de9e41
[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       $this->user[$idx]->bantime = 0;
784       
785       log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
786       
787       return ($this->user[$idx]);
788     }
789
790     return (FALSE);
791   }
792   
793   function standup_update(&$user)
794   {
795     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
796       $user_cur = &$this->user[$i];
797       if ($user_cur->sess == '')
798         continue;
799
800       log_main("STANDUP START", $user_cur->stat);
801       
802       if ($user_cur->stat == 'room') {
803         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
804         if ($user_cur == $user)
805           $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
806         
807         log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
808         
809         $user_cur->step++;
810       }
811     }
812   }
813
814
815 } // end class brisco
816
817 function make_seed()
818 {
819   list($usec, $sec) = explode(' ', microtime());
820   return (float) $sec + ((float) $usec * 100000);
821 }
822
823 function log_main($sess, $log) {
824   if (BRISK_DEBUG != TRUE)
825     return;
826
827   $fp = fopen("/tmp/brisk_main.log", 'a');
828   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
829   fclose($fp);
830 }
831
832 function log_rd($sess, $log) {
833   if (BRISK_DEBUG != TRUE)
834     return;
835
836   $fp = fopen("/tmp/brisk_rd.log", 'a');
837   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
838   fclose($fp);
839 }
840
841 function log_rd2($sess, $log) {
842   if (BRISK_DEBUG != TRUE)
843     return;
844
845   $fp = fopen("/tmp/brisk_rd2.log", 'a');
846   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
847   fclose($fp);
848 }
849
850 function log_send($sess, $log) {
851   if (BRISK_DEBUG != TRUE)
852     return;
853
854   $fp = fopen("/tmp/brisk_send.log", 'a');
855   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
856   fclose($fp);
857 }
858
859 function log_auth($sess, $log) {
860   if (BRISK_DEBUG != TRUE)
861     return;
862
863   $fp = fopen("/tmp/brisk_auth.log", 'a');
864   fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
865   fclose($fp);
866 }
867
868 function log_wr($sess, $log) {
869   if (BRISK_DEBUG != TRUE)
870     return;
871
872   $fp = fopen("/tmp/brisk_wr.log", 'a');
873   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
874   fclose($fp);
875 }
876
877 function log_load($sess, $log) {
878   if (BRISK_DEBUG != TRUE)
879     return;
880
881   $fp = fopen("/tmp/brisk_load.log", 'a');
882   fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
883   fclose($fp);
884 }
885
886 function init_data()
887 {
888   $brisco = new brisco();
889
890   return $brisco;
891 }
892
893 function lock_data()
894 {
895         //  echo "LOCK: ".FTOK_PATH."/main";
896         //  exit;
897   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
898     echo "FTOK FAILED";
899     exit;
900   }
901   // echo "FTOK ".$tok."<br>";
902   if (($res = sem_get($tok)) == FALSE) {
903     echo "SEM_GET FAILED";
904     exit;
905   }
906   if (sem_acquire($res)) 
907     return ($res);
908   else
909     return (false);
910 }
911
912 function unlock_data($res)
913 {
914   return (sem_release($res));
915 }
916
917
918 function &load_data() 
919 {
920   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
921     echo "FTOK FAILED";
922     exit;
923   }
924
925   if ($shm = shm_attach($tok,100000 * TABLES_N)) {
926     if(($bri = @shm_get_var($shm, $tok)) == false) {
927       log_main("XXX", "INIT MAIN DATA");
928
929       $bri = init_data();
930       shm_put_var($shm, $tok, $bri);
931     }
932     
933     shm_detach($shm);
934
935     return ($bri);
936   }
937
938   return (NULL);
939 }
940
941
942 function save_data(&$bri) 
943 {
944   $ret =   FALSE;
945   $shm =   FALSE;
946   $isacq = FALSE;
947
948   // var_dump($bri);
949
950   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
951     return (FALSE);
952
953   do {
954     $isacq = TRUE;
955     
956     if (($shm = shm_attach($tok,100000 * TABLES_N)) == FALSE)
957       break;
958     
959     if (shm_put_var($shm, $tok, $bri) == FALSE)
960       break;
961     // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
962     $ret = TRUE;
963   } while (0);
964   
965   if ($shm)
966     shm_detach($shm);
967      
968   return ($ret);
969 }
970
971 function table_act_content($isstanding, $sitted, $table, $cur_table)
972 {
973   $ret = "";
974
975   if ($isstanding) {
976     if ($sitted < PLAYERS_N) {
977       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
978     }
979   }
980   else {
981     if ($table == $cur_table)
982       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
983     else
984       $ret = "";
985   }
986   return ($ret);
987 }
988
989 function table_content($bri, $user, $table_idx)
990 {
991   $content = "";
992
993   // TODO
994   //
995   //   Si possono usare i dati nella classe table
996   //
997
998   $sess = $user->sess;
999   $table = &$bri->table[$table_idx];
1000
1001   if ($user->stat != 'room')
1002     return;
1003
1004   for ($i = 0 ; $i < $table->player_n ; $i++) {
1005     $user_cur = &$bri->user[$table->player[$i]];
1006
1007     if ($user_cur == $user) 
1008         { $hilion = "<b>"; $hilioff = "</b>"; }
1009       else
1010         { $hilion = ""; $hilioff = ""; }
1011
1012     log_main($bri->user[$e]->name, sprintf("IN TABLE [%d]", $table_idx));
1013     
1014     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
1015   }
1016   /*
1017   for ( ; $i < PLAYERS_N ; $i++)
1018     $content .= "<br>";
1019   */
1020
1021   $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
1022   
1023   return ($ret);
1024 }
1025
1026 function standup_content(&$bri, $user)
1027 {
1028   $ret = "";
1029   $content = "";
1030
1031   if ($user->stat != 'room')
1032     return;
1033
1034   for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
1035     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1036       continue;
1037     $ct++;
1038   }
1039
1040   $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
1041
1042   for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
1043     if ($bri->user[$e]->sess == "" || $bri->user[$e]->stat != "room" || $bri->user[$e]->name == "")
1044       continue;
1045
1046
1047     if ($bri->user[$e]->subst == "standup") {
1048       if (($ct % 4) == 0) {
1049         $content .= '<tr>';
1050       }
1051       if ($bri->user[$e] == $user) 
1052         { $hilion = "<b>"; $hilioff = "</b>"; }
1053       else
1054         { $hilion = ""; $hilioff = ""; }
1055
1056       $content .= sprintf('<td class=\\"room_standup\\">%s%s%s</td>',$hilion, xcape($bri->user[$e]->name), $hilioff);
1057       if (($ct % 4) == 3) {
1058         $content .= '</tr>';
1059       }
1060       $ct++;
1061     }
1062   }
1063   $content .= '</table>';
1064         
1065   $content2 = '<input class=\\"button\\" name=\\"logout\\" value=\\"Esco.\\" onclick=\\"window.onunload = null; act_logout();\\" type=\\"button\\">';
1066   $ret .= sprintf('$("standup").innerHTML = "%s";  $("esco").innerHTML = "%s";', 
1067                   $content, $content2);
1068
1069   return ($ret);
1070 }
1071
1072
1073 function show_notify($text, $tout, $butt, $w, $h)
1074 {
1075   log_main("SHOW_NOTIFY", $text);
1076   return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
1077 }
1078
1079 function briscola_show($bri, $table, $user)
1080 {
1081   $ptnadd = "";
1082   $ret = "";
1083
1084   if ($table->asta_card == 9) 
1085     $ptnadd = sprintf("<br>con %d punti", $table->asta_pnt);
1086   
1087   /* text of caller cell */
1088   if ($user->table_pos == $table->asta_win) 
1089     $ret .= sprintf('$("callerinfo").innerHTML = "Chiami%s:";', $ptnadd);
1090   else 
1091     $ret .= sprintf('$("callerinfo").innerHTML = "Chiama %s%s:";', 
1092                     xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
1093
1094   $ret .= sprintf('$("caller").style.backgroundImage = \'url("img/brisk_caller_sand%d.png")\';',
1095                   $table->asta_win);
1096   $ret .= sprintf('$("callerimg").src = "img/%02d.png";', $table->briscola);
1097   $ret .= sprintf('$("caller").style.visibility = "visible";');
1098   $ret .= sprintf('$("chooseed").style.visibility = "hidden";');
1099   $ret .= sprintf('$("astalascio").style.visibility = "";');
1100   $ret .= sprintf('$("asta").style.visibility = "hidden";');
1101   $ret .= sprintf('show_astat(-2,-2,-2,-2,-2);');
1102   
1103   return ($ret);
1104 }
1105
1106
1107 function game_result($asta_pnt, $pnt)
1108 {
1109   if ($asta_pnt == 61) {
1110     if ($pnt > 60)
1111       return (1);
1112     else if ($pnt == 60)
1113       return (0);
1114     else
1115       return (-1);
1116   }
1117   else {
1118     if ($pnt >= $asta_pnt)
1119       return (1);
1120     else
1121       return (-1);
1122   }
1123 }
1124
1125 function multoval($mult)
1126 {
1127   if ($mult == 2)
1128     return ("doppio");
1129   else if ($mult == 4)
1130     return ("quadruplo");
1131   else
1132     return (sprintf("%d-plo", $mult));
1133 }
1134
1135 function show_table_info(&$bri, &$table, $table_pos)
1136 {
1137   $user = &$bri->user[$table->player[$table_pos]];
1138
1139   $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
1140   $noty = sprintf('<table class=\"points\"><tr><th></th>');
1141   
1142   // Names.
1143   for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1144     $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
1145   $noty .= sprintf("</tr>");
1146
1147   // Points.
1148   log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
1149
1150   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
1151     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
1152     for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1153       $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->points[$i % MAX_POINTS][$e]);
1154     $noty .= "</tr>";
1155   }
1156
1157   // Total points.
1158   $noty .= '<tr><th class=\"td_points\">Tot.</th>';
1159   for ($e = 0 ; $e < PLAYERS_N ; $e++) 
1160     $noty .= sprintf('<td class=\"td_points\">%d</td>', $table->total[$e]);
1161   $noty .= "</tr></table>";
1162
1163   if ($table->old_reason != "") {
1164     $noty .= sprintf("<hr><b>%s</b><br>", xcape($table->old_reason));
1165   }
1166
1167   if ($table->old_win != -1) {
1168     $win = $table->player[$table->old_win];
1169     $fri = $table->player[$table->old_friend];
1170
1171     $wol = game_result($table->old_asta_pnt, $table->old_pnt);
1172
1173     if ($win != $fri) {
1174       $noty .= sprintf("<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", 
1175                        xcape($bri->user[$win]->name),
1176                        xcape($bri->user[$fri]->name));
1177       if ($table->old_pnt == 120) {
1178         $noty .= sprintf("hanno fatto <b>cappotto</b> EBBRAVI!.<hr>");
1179       }
1180       else {
1181         $noty .= sprintf("dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>",
1182                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1183                           'pi&ugrave; di 60'), $table->old_pnt,
1184                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1185       }
1186     }
1187     else {
1188       $noty .= sprintf("<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", 
1189                        xcape($bri->user[$win]->name));
1190       if ($table->old_pnt == 120) {
1191         $noty .= sprintf("ha fatto <b>cappotto</b> EBBRAVO!.<hr>");
1192       }
1193       else {
1194         $noty .= sprintf("doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>",
1195                          ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt :
1196                           'pi&ugrave; di 60'), $table->old_pnt,
1197                          ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso")));
1198       }
1199     }
1200   }
1201   if ($table->mazzo == $table_pos) 
1202     $noty .= "Fai <b>tu</b> il mazzo,";
1203   else {
1204     $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
1205     $noty .= "Il mazzo a <b>$unam</b>,";
1206   }
1207
1208   if ($user->subst == 'asta') {
1209     if ($table->asta_win == -1)  // auction case
1210       $curplayer = $table->gstart % PLAYERS_N;
1211     else 
1212       $curplayer = $table->asta_win;
1213   }
1214   else if ($user->subst == 'game') {
1215     $curplayer = ($table->gstart + $table->turn) % PLAYERS_N;
1216   }
1217
1218
1219   if ($curplayer == $table_pos) {
1220     $noty .= " tocca a <b>te</b> giocare.";
1221   }
1222   else {
1223     $unam = xcape($bri->user[$table->player[$curplayer]]->name);
1224     $noty .= " tocca a <b>$unam</b> giocare.";
1225   }
1226
1227   if ($table->mult > 1) {
1228     $noty .= sprintf(" La partita vale <b>%s</b>.", multoval($table->mult));
1229   }
1230   $noty .= "<hr><br>";
1231
1232   $ret .= show_notify($noty, 3000, "torna alla partita", 500, 400);
1233   
1234   return ($ret);
1235 }
1236
1237 function root_wellcome($user)
1238 {
1239   GLOBAL $root_wellarr;
1240
1241   for ($i = 0 ; $i < count($root_wellarr) ; $i++)
1242     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $root_wellarr[$i]));
1243
1244   return ($ret);
1245 }
1246
1247 function table_wellcome($user)
1248 {
1249   GLOBAL $table_wellarr;
1250
1251   for ($i = 0 ; $i < count($table_wellarr) ; $i++)
1252     $ret .= sprintf('chatt_sub("ChanServ: ","%s");', str_replace('"', '\"', $table_wellarr[$i]));
1253
1254   return ($ret);
1255 }
1256
1257 function show_room(&$bri, &$user)
1258 {
1259   $ret .= sprintf('gst.st = %d;',  $user->step);
1260   $ret .= sprintf('stat = "%s";',  $user->stat);
1261
1262   $ret .= root_wellcome($user);
1263   $ret .= sprintf('subst = "%s";', $user->subst);
1264   $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1265   for ($i = 0 ; $i < TABLES_N ; $i++) {
1266     $ret .= table_content($bri, $user, $i);
1267     $act_content = table_act_content(($user->subst == 'standup'), 
1268                                      $bri->table[$i]->player_n, $i, $user->table);
1269     $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $i, $act_content);
1270   }
1271   $ret .= standup_content($bri, $user);
1272   
1273   return ($ret);
1274 }
1275
1276
1277
1278 /* show table 
1279 is_transition (is from room to table ?)
1280 is_again      (is another game)
1281
1282 Examples                    of $is_transition, $is_again:
1283   from reload of the page:              FALSE, FALSE
1284   from sitdown in room:                  TRUE, FALSE
1285   from table: asta cmd e tutti passano:  TRUE, TRUE
1286   from table: fine partita:              TRUE, TRUE
1287  */
1288 function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
1289 {
1290   $table_idx = $user->table;
1291   $table = &$bri->table[$table_idx];
1292   $table_pos = $user->table_pos;
1293
1294   $ret = "table_init();";
1295   $ret .= $table->exitlock_show(&$bri->user, $table_pos);
1296   if (!$is_again) {
1297     /* GENERAL STATUS */
1298     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
1299                      $sendstep, $user->stat, $user->subst, $table_pos);
1300     /* BACKGROUND */
1301     $ret .= "background_set();";
1302     
1303     /* USERS INFO */
1304     $ret .= sprintf('$("myname").innerHTML = "<b>%s</b>";', xcape($user->name,ENT_COMPAT,"UTF-8"));
1305     $ret .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
1306                     xcape($bri->user[$table->player[($table_pos)%PLAYERS_N]]->name),
1307                     xcape($bri->user[$table->player[($table_pos+1)%PLAYERS_N]]->name),
1308                     xcape($bri->user[$table->player[($table_pos+2)%PLAYERS_N]]->name),
1309                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+3)%PLAYERS_N]]->name)),
1310                     (PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+4)%PLAYERS_N]]->name)));
1311   }
1312   /* NOTIFY FOR THE CARD MAKER */
1313   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1314     $ret .= show_table_info(&$bri, &$table, $table_pos);
1315   }
1316   if (!$is_again) 
1317     $ret .= table_wellcome($user);
1318
1319   if ($is_transition && !$is_again) { // appena seduti al tavolo, play della mucca
1320     $ret .= playsound("cow.mp3");
1321   }
1322
1323
1324   /* CARDS */
1325   if ($is_transition) { //  && $user->subst ==  "asta" superfluo
1326     $ret .= "|";
1327     
1328     for ($i = 0 ; $i < 8 ; $i++) {
1329       for ($e = 0 ; $e < PLAYERS_N ; $e++) {
1330         $ct = 0;
1331         for ($o = 0 ; $o < 40 && $ct < $i+1 ; $o++) {
1332           if ($table->card[$o]->owner == (($e + $table->gstart) % PLAYERS_N)) {
1333             $ct++;
1334             if ($ct == $i+1)
1335               break;
1336           }
1337         }
1338         log_rd($sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
1339         
1340         $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
1341                          $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
1342                               $table->card[$o]->value : -1), 
1343                          ($i == 7 && $e == (PLAYERS_N - 1) ? 1 : 0.5),$i+1);
1344       }
1345     }    
1346   }
1347   else {
1348     $taked  = array(0,0,0,0,0);
1349     $inhand = array(0,0,0,0,0);
1350     $ontabl  = array(-1,-1,-1,-1,-1);
1351     $cards  = array();
1352
1353     for ($i = 0 ; $i < 40 ; $i++) {
1354       if ($table->card[$i]->stat == 'hand') {
1355         if ($table->card[$i]->owner == $table_pos) {
1356           $cards[$inhand[$table->card[$i]->owner]] = $table->card[$i]->value;
1357         }
1358         $inhand[$table->card[$i]->owner]++;
1359       }
1360       else if ($table->card[$i]->stat == 'take') {
1361         log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
1362         $taked[$table->card[$i]->owner]++;
1363       }
1364       else if ($table->card[$i]->stat == 'table') {
1365         $ontabl[$table->card[$i]->owner] = $i;
1366       }
1367     }
1368     $logg = "\n";
1369     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1370       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
1371     }
1372     log_main("Stat table:", $logg);
1373
1374     /* Set ours cards. */
1375     $oursarg = "";
1376     for ($i = 0 ; $i < $inhand[$table_pos] ; $i++) 
1377       $oursarg .= ($i == 0 ? "" : ", ").$cards[$i];
1378     for ($i = $inhand[$table_pos] ; $i < 8 ; $i++) 
1379       $oursarg .= ($i == 0 ? "" : ", ")."-1";
1380     $ret .= sprintf('card_setours(%s);', $oursarg);
1381
1382     /* Dispose all cards */
1383     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1384       /* Qui sotto al posto di + 1 c'era + ->gstart ... credo in modo errato */
1385       $ret .= sprintf('cards_dispose(%d,%d,%d);', $i,
1386                       $inhand[$i], $taked[$i]);
1387
1388       if ($ontabl[$i] != -1) {
1389         $ret .= sprintf('card_place(%d,%d,%d,%d,%d);',$i, $inhand[$i], 
1390                         $table->card[$ontabl[$i]]->value, 
1391                         $table->card[$ontabl[$i]]->x, $table->card[$ontabl[$i]]->y);
1392       }
1393     }
1394   }
1395
1396   /* Show auction */
1397   if ($user->subst == 'asta') {
1398
1399     /* show users auction status */
1400     $showst = "";
1401     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1402       $user_cur = &$bri->user[$table->player[$i]];
1403       $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
1404                          ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
1405     }
1406     if (PLAYERS_N == 3)
1407         $showst .= ",-2,-2";
1408     $ret .= sprintf('show_astat(%s);', $showst);
1409
1410     if ($table->asta_win != -1 && $table->asta_win == $table_pos) {
1411       /* show card chooser */
1412       $ret .= sprintf('choose_seed(%s); $("astalascio").style.visibility = ""; $("asta").style.visibility = "hidden";',
1413                       $table->asta_card);
1414     }
1415     else {
1416       /* show auction */
1417       if ($table_pos == ($table->gstart % PLAYERS_N) &&
1418           $table->asta_win == -1) 
1419         $ret .= sprintf('dispose_asta(%d,%d, %s);', 
1420                         $table->asta_card + 1, $table->asta_pnt+1, ($user->handpt <= 2 ? "true" : "false"));
1421       else
1422         $ret .= sprintf('dispose_asta(%d,%d, %s);',
1423                         $table->asta_card + 1, -($table->asta_pnt+1), ($user->handpt <= 2 ?  "true" : "false"));
1424     }
1425
1426     /* Remark */
1427     if ($table->asta_win == -1) { // auction case
1428       if ($table_pos == ($table->gstart % PLAYERS_N)) 
1429         $ret .= "remark_on();";
1430       else
1431         $ret .= "remark_off();";
1432     }
1433     else { // chooseed case
1434       if ($table_pos == $table->asta_win) 
1435         $ret .= "remark_on();";
1436       else
1437         $ret .= "remark_off();";
1438     }
1439   }
1440   else if ($user->subst == 'game') {
1441     /* HIGHLIGHT */
1442     if (($table->gstart + $table->turn) % PLAYERS_N == $table_pos) 
1443       $ret .= "is_my_time = true; remark_on();";
1444     else
1445       $ret .= "remark_off();";
1446     
1447     /* WHO CALL AND WATH */
1448     $ret .= briscola_show($bri, $table, $user);
1449     
1450   }
1451   return ($ret);
1452   }
1453
1454 function calculate_winner(&$table)
1455 {
1456   $briontab = FALSE;
1457   $ontab = array();
1458   $ontid = array();
1459   $cur_win  =  -1;
1460   $cur_val  = 100;
1461   $cur_seed = $table->briscola - ($table->briscola % 10);
1462
1463   for ($i = 0 ; $i < 40 ; $i++) {
1464     if ($table->card[$i]->stat != "table")
1465       continue;
1466
1467     log_wr($sess, sprintf("Card On table: [%d]", $i));
1468
1469     $v = $table->card[$i]->value; 
1470     $ontab[$table->card[$i]->owner] = $v;
1471     $ontid[$table->card[$i]->owner] = $i;
1472     /* se briscola setto il flag */
1473     if (($v - ($v % 10)) == $cur_seed)
1474       $briontab = TRUE;
1475   }
1476
1477   if ($briontab == FALSE) {
1478     $cur_win  = $table->gstart;
1479     $cur_val  = $ontab[$cur_win];
1480     $cur_seed = $cur_val - ($cur_val % 10);
1481   }
1482
1483   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1484     if (($ontab[$i] - ($ontab[$i] % 10)) == $cur_seed) {
1485       if ($ontab[$i] < $cur_val) {
1486         $cur_val = $ontab[$i];
1487         $cur_win = $i;
1488       }
1489     }
1490   }
1491
1492   for ($i = 0 ; $i < PLAYERS_N ; $i++) {
1493     $table->card[$ontid[$i]]->owner = $cur_win;
1494     $table->card[$ontid[$i]]->stat =  "take";
1495   }
1496   return ($cur_win);
1497 }
1498
1499 function calculate_points(&$table)
1500 {
1501   GLOBAL $G_all_points; 
1502
1503   $pro = 0;
1504
1505   if ($table->asta_pnt == 60)
1506     $table->asta_pnt = 61;
1507
1508   $table->old_reason = "";
1509   $table->old_win = $table->asta_win;
1510   $table->old_friend = $table->friend;
1511   $table->old_asta_pnt = $table->asta_pnt;
1512
1513   for ($i = 0 ; $i < 40 ; $i++) {
1514     $ctt = $table->card[$i]->value % 10;
1515     $own = $table->card[$i]->owner;
1516     if ($own == $table->asta_win || $own == $table->friend) 
1517       $pro += $G_all_points[$ctt];
1518   }
1519
1520   log_wr("XXX", sprintf("PRO: [%d]", $pro));
1521
1522   
1523   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
1524     $table->points[$table->points_n % MAX_POINTS] = array();
1525     for ($i = 0 ; $i < PLAYERS_N ; $i++) 
1526       $table->points[$table->points_n % MAX_POINTS][$i] = 0;
1527     $table->points_n++;
1528     $table->old_pnt = $pro;
1529     $table->mult *= 2;
1530
1531     return;
1532   }
1533
1534   if ($pro >= $table->asta_pnt) 
1535     $sig = 1;
1536   else
1537     $sig = -1;
1538
1539   $table->points[$table->points_n % MAX_POINTS] = array();
1540   for ($i = 0 ; $i < 5 ; $i++) {
1541     if ($i == $table->asta_win) 
1542       $pt = ($i == $table->friend ? 4 : 2);
1543     else if ($i == $table->friend) 
1544       $pt = 1;
1545     else
1546       $pt = -1;
1547
1548     log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
1549
1550     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
1551
1552     log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
1553     
1554     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
1555     $table->total[$i] += $pt;
1556   }
1557   $table->points_n++;
1558   $table->old_pnt = $pro;
1559   $table->mult = 1;
1560 }
1561
1562 function validate_sess($sess) 
1563 {
1564   if (strlen($sess) == SESS_LEN) 
1565     return (TRUE);
1566   else
1567     return (FALSE);
1568 }
1569
1570 function validate_name($name) 
1571 {
1572   $name_new = str_replace(' ', '_', substr(trim($name),0,12));
1573
1574   for ($i = 0 ; $i < strlen($name_new) ; $i++) {
1575     $c = $name_new[$i];
1576     if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
1577       return ($name_new);
1578   }
1579
1580   return (FALSE);
1581 }
1582
1583 function playsound($filename)
1584 {
1585   return (sprintf('playsound("flasou", "%s");', $filename));
1586 }
1587
1588 function secstoword($secs)
1589 {
1590   $mins = floor($secs / 60);
1591   $secs = $secs % 60;
1592   if ($mins > 0) 
1593     $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
1594   
1595   if ($secs > 0)
1596     $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
1597   
1598   return ($ret);
1599 }
1600
1601 ?>