room for wakeupper reconstructed by index_rd.php and wellcome sentence updated
[brisk.git] / web / Obj / brisk.phh
index 9f63b98..78a7aef 100644 (file)
@@ -26,8 +26,7 @@ define(TABLES_N, 12);
 define(PLAYERS_N, 3);
 define(MAX_POINTS, 5);
 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
-define(SHM_DIMS, (50000 * MAX_PLAYERS));
-// define(COMM_N, 6);
+define(SHM_DIMS, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
 define(COMM_N, 12);
 define(COMM_GEN_N, 50);
 define(SESS_LEN, 13);
@@ -38,15 +37,27 @@ define(EXPIRE_TIME_SMAMMA, 360);
 define(BAN_TIME, 900); 
 define(GARBAGE_TIMEOUT, 10);
 define(NICKSERV, "<i>SERVER</i>");
-define(BRISK_DEBUG, FALSE);
+
+define(DBG_ONL2, 0x0001);
+define(DBG_ONLY, 0x0002);
+define(DBG_MAIN, 0x0004);
+define(DBG_READ, 0x0008);
+define(DBG_REA2, 0x0010);
+define(DBG_SEND, 0x0020);
+define(DBG_LOCK, 0x0040);
+define(DBG_WRIT, 0x0080);
+define(DBG_LOAD, 0x0100);
+define(DBG_AUTH, 0x0200);
+
+define(BRISK_DEBUG, 0);
 // define(DEBUGGING, "local");
 
 $G_false = FALSE;
 
 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
-$G_brisk_version = "0.8.2";
+$G_brisk_version = "spawn-0.0.6";
 
-$root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette pi&ugrave; tavoli, pi&ugrave; tavoli.',
+$root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: riscritta tutta l\'architettura dati del server, tutto pi&egrave; veloce e leggero.',
                         'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
 $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.');
 
@@ -134,7 +145,7 @@ class Card {
     $this->stat = 'take'; // Card stat
     $this->owner = $newown;
   }
-}
+} // end class Card
 
 class Table {
   var $player;
@@ -164,6 +175,9 @@ class Table {
   var $old_win;
   var $old_friend;
 
+  var $table_token;
+  var $table_start;
+
   function Table() 
   {
   }
@@ -172,12 +186,12 @@ class Table {
   {
     GLOBAL $G_false;
 
-    if (($thiz = new Table()) == FALSE)
+    if (($thiz =& new Table()) == FALSE)
       return ($G_false);
 
     $thiz->player    =   array();
     $thiz->player_n  =   0;
-    $thiz->card      =  &$thiz->bunch_create();
+    $thiz->card      =   FALSE;
     $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
     $thiz->asta_pla_n=  -1;
     $thiz->asta_card =  -1;
@@ -190,11 +204,14 @@ class Table {
     $thiz->briscola  =  -1;
     $thiz->friend    =  -1;
     $thiz->turn      =   0;
-    $thiz->old_reason = "";
+    $thiz->old_reason   = "";
     $thiz->old_asta_pnt = -1;
     $thiz->old_pnt      = -1;
-    $thiz->old_win   =  -1;
-    $thiz->old_friend=  -1;
+    $thiz->old_win      = -1;
+    $thiz->old_friend   = -1;
+
+    $thiz->table_token  = "";
+    $thiz->table_start  = 0;
 
     return ($thiz);
   }
@@ -203,7 +220,7 @@ class Table {
   {
     GLOBAL $G_false;
     
-    if (($thiz = new Table()) == FALSE)
+    if (($thiz =& new Table()) == FALSE)
       return ($G_false);
     
     $thiz->player = array();
@@ -235,6 +252,9 @@ class Table {
     $thiz->old_win = $from->old_win;
     $thiz->old_friend = $from->old_friend;
 
+    $thiz->table_token  = $from->table_token;
+    $thiz->table_start  = $from->table_start;
+
     return ($thiz);
   }
   
@@ -246,7 +266,7 @@ class Table {
       return ($G_false);
     
     $thiz->player_n = $from->player_n;
-    $thiz->card = $from->card;
+    $thiz->card = &$thiz->bunch_create();
     $thiz->mazzo = $from->mazzo;
     $thiz->gstart = $from->gstart;
     $thiz->turn = $from->turn;
@@ -276,6 +296,9 @@ class Table {
     for ($i = 0 ; $i < $from->player_n ; $i++)
       $thiz->player[$i] = $i;
 
+    $thiz->table_token  = $from->table_token;
+    $thiz->table_start  = $from->table_start;
+
     return ($thiz);
   }
   
@@ -304,7 +327,7 @@ class Table {
       $rn = rand(0, $i);
       
       if ($rn == 0)
-       log_main("RND ZERO", "");
+       log_main("RND ZERO");
       
       $id = $rest[$rn];
 
@@ -329,12 +352,12 @@ class Table {
       $user_cur->exitislock = TRUE;
     }
 
-    log_main("table::init","ci siamo");
+    log_main("table::initci siamo");
   }
 
   function game_init(&$userarr)
   {
-    log_rd2("XXX", "GSTART 4");
+    log_rd2("GSTART 4");
 
     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
     $this->bunch_make();
@@ -398,7 +421,7 @@ class Table {
       $this->player_n--;
     }
     else {
-      log_main($user->sess, "INCONSISTENCY ON TABLE.");
+      log_main("INCONSISTENCY ON TABLE.");
     }
   }
 
@@ -439,7 +462,7 @@ class Table {
 
     return ($ct);
   }
-} // End class Table
+} // end class Table
   
 class User {
   var $name;       // name of the user
@@ -459,6 +482,7 @@ class User {
   var $exitislock; // Player can exit from the table ?
   var $table;      // id of the current table (if in table state)
   var $table_pos;  // idx on the table
+  var $table_token;// token that identify a game on a table
   var $the_end;    // Flag to change the end of the session
 
   function User() {
@@ -487,7 +511,9 @@ class User {
     $thiz->exitislock = TRUE;
     
     $thiz->table = $table;
-    
+    $thiz->table_pos = -1;
+    $thiz->table_token = "";
+
     return ($thiz);
   }
 
@@ -495,33 +521,34 @@ class User {
   {
     GLOBAL $G_false;
     
-    if (($thiz = new User()) == FALSE)
+    if (($thiz =& new User()) == FALSE)
       return ($G_false);
     
-    $thiz->name = $from->name;
-    $thiz->sess = $from->sess;
-    $thiz->ip = $from->ip;
-    $thiz->lacc = $from->lacc;
-    $thiz->laccwr = $from->laccwr;
-    $thiz->bantime = $from->bantime;
-    $thiz->stat = $from->stat;
-    $thiz->subst = $from->subst;
-    $thiz->step = $from->step;
+    $thiz->name       = $from->name;
+    $thiz->sess       = $from->sess;
+    $thiz->ip         = $from->ip;
+    $thiz->lacc       = $from->lacc;
+    $thiz->laccwr     = $from->laccwr;
+    $thiz->bantime    = $from->bantime;
+    $thiz->stat       = $from->stat;
+    $thiz->subst      = $from->subst;
+    $thiz->step       = $from->step;
     $thiz->trans_step = $from->trans_step;
-    $thiz->comm = array();
+    $thiz->comm       = array();
 
     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
     for ($i = $i_start ; $i < $from->step ; $i++) {
       $ii = $i % COMM_N;
-      $thiz->comm[$ii]   = $from->comm[$ii];
+      $thiz->comm[$ii] = $from->comm[$ii];
     }
-    $thiz->asta_card = $from->asta_card;
-    $thiz->asta_pnt = $from->asta_pnt;
-    $thiz->handpt = $from->handpt;
+    $thiz->asta_card  = $from->asta_card;
+    $thiz->asta_pnt   = $from->asta_pnt;
+    $thiz->handpt     = $from->handpt;
     $thiz->exitislock = $from->exitislock;
-    $thiz->table = $from->table;
-    $thiz->table_pos = $from->table_pos;
-    $thiz->the_end = $from->the_end;
+    $thiz->table      = $from->table;
+    $thiz->table_pos  = $from->table_pos;
+    $thiz->table_token = $from->table_token;
+    $thiz->the_end    = $from->the_end;
 
     return ($thiz);
   }
@@ -547,7 +574,7 @@ class User {
 
     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
     for ($i = $i_start ; $i < $from->step ; $i++) {
-      log_wr("XX", "TRY PUSH:".$i);
+      log_wr("TRY PUSH:".$i);
       $ii = $i % COMM_N;
       $thiz->comm[$ii]   = $from->comm[$ii];
     }
@@ -560,11 +587,13 @@ class User {
 
     $thiz->table      = $table;
     $thiz->table_pos  = $table_pos;
+    $thiz->table_token = $from->table_token;
 
     return ($thiz);
   }
   
   function stat_set($stat) {
+    log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]"); 
     $this->stat = "$stat";
     
     /*
@@ -606,7 +635,8 @@ class User {
     
     return (FALSE);
   }
-}
+} // end class User
+
 
 function step_get($sess) {
   $fp = FALSE;
@@ -627,19 +657,19 @@ function step_get($sess) {
     $arr = unpack('l', $s);
     fclose($fp);
 
-    // log_rd2($sess, "A0: ".$arr[0]."  A1: ".$arr[1]);
+    // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
     return ($arr[1]);
   } while (0);
 
   if ($fp != FALSE)
     fclose($fp);
 
-  log_rd2($sess, "STEP_GET: return false ".$ct);
+  log_rd2("STEP_GET: return false ".$ct);
   return (FALSE);
 }
 
 function step_unproxy($sess) {
-  log_rd2($sess, "UNPROXY: ".PROXY_PATH."/".$sess.".step");
+  log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
   @unlink(PROXY_PATH."/".$sess.".step");
 }
 
@@ -653,6 +683,7 @@ class Room {
 
   function Room () {
     $this->user = array();
+    $this->table = array();
 
     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
       $this->user[$i] =& User::create("", "");
@@ -665,19 +696,96 @@ class Room {
 
   function garbage_manager($force)
   {
-    
+    $ismod = FALSE;
+
+    log_rd2("garbage_manager START");
+
     /* Garbage collector degli utenti in timeout */
     $curtime = time();
     if ($force || $this->garbage_timeout < $curtime) {
       
+      // Before all align times with table timeout
+      for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+       $table_cur =& $this->table[$table_idx];
+       // if the table is complete and exists its shared mem we get the info about users lacc
+
+       if ($table_cur->player_n == PLAYERS_N) {
+         log_main("PLAYERS == N TABLE ".$table_idx);
+         
+         if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
+           log_main("bin5 lock data success");
+           
+           if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+             if ($table_cur->table_token != $bri->table_token) {
+               log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
+               log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start."  Table: ".$bri->table_start);
+               $bri = FALSE;
+             }
+           }
+           
+           if ($bri != FALSE) {
+             //
+             //  SPAWN: JOIN
+             //
+             log_main("garbage_manager: bri loaded successfully.");
+             $bri->garbage_manager(TRUE);
+             
+             $bri_table = &$bri->table[0];
+
+             // is the end of the table
+             if ($bri->the_end == TRUE) {
+               /*
+                *  DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
+                */
+               for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
+                 // stat must be "table" by definition
+                 $user_cur =& $this->user[$table_cur->player[$i]];
+                 $bri_user =& $bri->user[$i];
+
+                 $user_cur->subst      = $bri_user->subst;
+                 $user_cur->step       = $bri_user->step;
+                 $user_cur->trans_step = $bri_user->step;
+                 $user_cur->lacc       = $bri_user->lacc;
+                 $user_cur->laccwr     = $bri_user->lacc;
+                 $user_cur->bantime    = $bri_user->bantime;
+               }
+             
+               $this->room_join_wakeup(&$user_cur); 
+               $table_cur->table_token = "";
+
+               Briskin5::destroy_data($table_idx);
+             }
+             else {
+               log_main("gm:: save_data");
+
+               for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
+                 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
+               }
+             
+               Briskin5::save_data(&$bri);
+             }
+           } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+           else {
+             log_main("ERROR: table ".$table_idx." unrecoverable join");
+           }
+         } // bri::lock_data
+       } //  if ($table_cur->player_n == PLAYERS_N) {
+      } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+    
+      log_rd2("out new loop.");
+               
       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
        $user_cur = &$this->user[$i];
-       if ($user_cur->sess == "")
+       
+       log_rd2("User: ".$user_cur->name."  stat: ".$user_cur->stat."  subst: ".$user_cur->subst);
+         
+       if ($user_cur->sess == "") 
          continue;
        
-       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
-         log_rd2($user_cur->sess, "AUTO LOGOUT.");
-
+       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
+         // Auto logout dell'utente
+         log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
+         
          if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
            log_auth($user_cur->sess, "Autologout session.");
            
@@ -685,15 +793,16 @@ class Room {
            $user_cur->sess = "";
            step_unproxy($tmp_sess);
            $user_cur->name = "";
+           $user_cur->step = 0;
            $user_cur->the_end = FALSE;
            
-           log_rd2($user_cur->sess, "AUTO LOGOUT.");
+           log_rd2("AUTO LOGOUT.");
            if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
              $this->room_wakeup(&$user_cur);
            else if ($user_cur->subst == 'standup')
              $this->room_outstandup(&$user_cur);
            else
-             log_rd2($sess, "LOGOUT FROM WHAT ???");
+             log_rd2("LOGOUT FROM WHAT ???");
          }
        }
 
@@ -706,13 +815,13 @@ class Room {
          }
        }
       }
-      log_rd2($user_cur->sess, "GARBAGE UPDATED!");
+      log_rd2("GARBAGE UPDATED!");
       
       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
+      $ismod = TRUE;
     }
 
-    // BAN_IP_CLEAN
-
+    return ($ismod);
   }
 
 
@@ -721,17 +830,17 @@ class Room {
     $table_idx = $user->table;
     $table = &$this->table[$table_idx];
 
-    log_main("WAKEUP", "begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
+    log_main("WAKEUPbegin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
 
     $curtime = time();
 
     $from_table = ($user->stat == "table");
     if ($from_table) {
-      log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+      log_main("WAKEUPfrom table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
 
       for ($i = 0 ; $i < $table->player_n ; $i++) {
        $user_cur = &$this->user[$table->player[$i]];
-       log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
+       log_main("PREIMPOSTINLOOP name: ".$user_cur->name);
 
        if ($user_cur != $user) {
          $user_cur->stat_set("room");
@@ -759,16 +868,16 @@ class Room {
       if ($user_cur->sess == '' || $user_cur->stat != 'room')
        continue;
       
-      log_main("VALORI", "name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
+      log_main("VALORIname: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
 
       $ret = "gst.st = ".($user_cur->step+1)."; ";
       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
        $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("index.php");|';
        // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
-       log_main("DOCUMENT.index.php", "from table");
+       log_main("DOCUMENT.index.phpfrom table");
       }
       else if ($user_cur->stat == "room") {
-       log_main("DOCUMENT.index.php", "from table");
+       log_main("DOCUMENT.index.phpfrom table");
 
        $ret .= table_content($this, $user_cur, $table_idx);
        $ret .= standup_content($this, $user_cur);
@@ -791,14 +900,104 @@ class Room {
          $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
        }
       }
-      log_wr($user_cur->sess, "ROOM_WAKEUP: ".$ret);
+      log_wr("ROOM_WAKEUP: ".$ret);
       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
       $user_cur->step_inc();
     }
   }
-  
 
+  function room_join_wakeup(&$user, $update_lacc = FALSE)
+  {
+    $table_idx = $user->table;
+    $table = &$this->table[$table_idx];
+    
+    log_main("JOIN_WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
+
+    $curtime = time();
+    $user_wup = array();
+    $user_wup_n = 0;
+    log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+    
+    for ($i = 0 ; $i < $table->player_n ; $i++) {
+      $user_cur = &$this->user[$table->player[$i]];
+      log_main("PREIMPOST INLOOP name: ".$user_cur->name);
+      if ($user_cur->sess != "") {
+       if ($update_lacc == TRUE) {
+         $user_cur->laccwr = $curtime;
+       }
+       log_main("cur: ".$user_cur->name."  subst: ".$user_cur->subst);
+       if ($user_cur->subst == "shutdowned") {
+         $user_cur->stat_set("room");
+         $user_cur->subst = "sitdown";
+       }
+       else if ($user_cur->subst == "shutdowner") {
+         $user_cur->stat_set("room");
+         $user_cur->subst = "standup";
+         $user_cur->table = -1;
+         $user_wup[$user_wup_n++] = &$user_cur;
+       }
+      }
+    }
+
+    for ($wup_idx = 0 ; $wup_idx < $user_wup_n  ; $wup_idx++)
+      $table->user_rem(&$this, &$user_wup[$wup_idx]);
 
+    /* aggiorna l'array dei giocatori al tavolo. */
+
+    for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+      log_main("START LOOP");
+      $user_cur = &$this->user[$i];
+      if ($user_cur->sess == '' || $user_cur->stat != 'room') {
+       log_main("name: ".$user_cur->name."skip   subst: ".$user_cur->subst);
+       continue;
+      }
+
+      log_main("___");
+      log_main("VALORI name: ".$user_cur->name."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
+
+      $ret = "gst.st = ".($user_cur->step+1)."; ";
+      if ($user_cur->stat == "room") {
+       log_main("DOCUMENT.index.php from table");
+
+       $ret .= table_content($this, $user_cur, $table_idx);
+       $ret .= standup_content($this, $user_cur);
+       
+       $act_content = table_act_content(FALSE, 0, $table_idx, $user_cur->table);
+       $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
+       
+       for ($wup_idx = 0 ; $wup_idx < $user_wup_n  ; $wup_idx++)
+         if ($user_cur == $user_wup[$wup_idx]) 
+           break;
+
+       // for users that wakeup the room will be reconstructed by index_rd.php
+       if ($user_cur == $user_wup[$wup_idx]) 
+         continue;
+
+       log_main("JOIN_WAKEUP wup_idx ".$wup_idx."  wup_n ".$user_wup_n);
+
+       if ($wup_idx < $user_wup_n) {
+         log_main("JOIN_WAKEUP less");
+         // set the new status 
+         $ret .=  'stat = "room"; subst = "standup"; ';
+         // clean the action buttons in other tables
+         for ($e = 0 ; $e < TABLES_N ; $e++) {
+           if ($this->table[$e]->player_n < PLAYERS_N)
+             $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $e, table_act_content(TRUE, 0, $e, $user_cur->table));
+         }
+         log_main("JOIN_WAKEUP end less");
+       }
+       else {
+         log_main("JOIN_WAKEUP more");
+         $act_content = table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table);
+         $ret .= sprintf('$("table_act%d").innerHTML = "%s";', $table_idx, $act_content);
+         log_main("JOIN_WAKEUP end more");
+       }
+      }
+      log_wr("ROOM_JOIN_WAKEUP: ".$ret);
+      $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+      $user_cur->step_inc();
+    }
+  }
 
   function room_outstandup(&$user)
   {
@@ -807,7 +1006,7 @@ class Room {
   
   function table_update(&$user)
   {
-    log_main("table_update", "pre - USER: ".$user->name);
+    log_main("table_updatepre - USER: ".$user->name);
 
     $table_idx = $user->table;
 
@@ -831,12 +1030,12 @@ class Room {
       $user_cur->step_inc();
     }
 
-    log_main("table_update", "post");
+    log_main("table_updatepost");
   }
 
   function room_sitdown(&$user, $table_idx)
   {
-    log_main("room_sitdown", ($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
+    log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
 
     if ($table_idx > -1) 
       $table = &$this->table[$table_idx];
@@ -883,7 +1082,7 @@ class Room {
     
     $dt = date("H:i ", $timecur);
     if (strncmp($user_mesg, "/nick ", 6) == 0) {
-      log_main($user->sess, "chatt_send BEGIN");
+      log_main("chatt_send BEGIN");
 
       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
          $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
@@ -912,11 +1111,11 @@ class Room {
       if ($i == MAX_PLAYERS) {
        $user->name = $name_new;
 
-      log_main($user->sess, "chatt_send start set");
+      log_main("chatt_send start set");
        
 
        for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
-         log_main($user->sess, "chatt_send set loop");
+         log_main("chatt_send set loop");
          
          $user_cur = &$this->user[$i];
          if ($user_cur->sess == '')
@@ -927,11 +1126,11 @@ class Room {
            }
            else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
                     $user->stat == 'table') {
-             log_main($user->sess, "chatt_send pre table update");
+             log_main("chatt_send pre table update");
 
              $this->table_update(&$user);
 
-             log_main($user->sess, "chatt_send post table update");
+             log_main("chatt_send post table update");
            }
          }
          else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
@@ -986,12 +1185,12 @@ class Room {
          return ($ret);
        }
       }
-      log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
+      log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
-      // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
+      // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
     }
     else {
-      log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
+      log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
     }
 
     return ($G_false);
@@ -1060,7 +1259,7 @@ class Room {
       $this->user[$idx]->laccwr = $curtime;
       $this->user[$idx]->bantime = 0;
       $this->user[$idx]->ip = $ip;
-      log_main("XXX", sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
+      log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
       
       return ($this->user[$idx]);
     }
@@ -1075,14 +1274,14 @@ class Room {
       if ($user_cur->sess == '')
        continue;
 
-      log_main("STANDUP START", $user_cur->stat);
+      log_main("STANDUP START: ".$user_cur->stat);
       
       if ($user_cur->stat == 'room') {
        $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".standup_content($this, $user_cur);
        if ($user_cur == $user)
          $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>: ";',  xcape($user->name));
        
-       log_main("FROM STANDUP", "NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
+       log_main("FROM STANDUPNAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
        
        $user_cur->step_inc();
       }
@@ -1110,17 +1309,17 @@ class Room {
     if ($shm = shm_attach($tok, SHM_DIMS)) {
       $room = @shm_get_var($shm, $tok);
       
-      log_only($sess, "bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
+      log_only("bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
       if (isset($room)) 
-       log_only($sess, "bri count ".count($room));
+       log_only("bri count ".count($room));
       
       if ($room == FALSE) {
-       log_only($sess, "INIT MAIN DATA");
+       log_only("INIT MAIN DATA");
        
        $room =& Room::init_data();
        if (shm_put_var($shm, $tok, $room) == FALSE) {
-         log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
-         log_only($sess, serialize($room));
+         log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
+         log_only(serialize($room));
        }
       }
       
@@ -1153,13 +1352,13 @@ class Room {
       if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
        break;
       
-      // log_only($sess, "PUT_VAR DI ".strlen(serialize($room)));
+      // log_only("PUT_VAR DI ".strlen(serialize($room)));
       if (shm_put_var($shm, $tok, $room) == FALSE) {
-       log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($room)));
-       log_only($sess, serialize($room));
+       log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
+       log_only(serialize($room));
        break;
       }
-      // log_main("XXX", "QUI CI ARRIVA [".$room->user[0]->name."]");
+      // log_main("QUI CI ARRIVA [".$room->user[0]->name."]");
       $ret = TRUE;
     } while (0);
     
@@ -1185,7 +1384,7 @@ class Room {
       exit;
     }
     if (sem_acquire($res)) {   
-      log_only($sess, "LOCK");
+      log_lock("LOCK room");
       return ($res);
     }
     else
@@ -1196,7 +1395,7 @@ class Room {
   {
     GLOBAL $sess; 
     
-    log_only($sess, "UNLOCK");
+    log_lock("UNLOCK room");
 
     return (sem_release($res));
   }
@@ -1208,113 +1407,177 @@ function make_seed()
   return (float) $sec + ((float) $usec * 100000);
 }
 
-function log_only2($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+
+function log_only2($log) {
+  if ((BRISK_DEBUG & DBG_ONL2) == 0)
     return;
   
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_only2.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_only($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_only($log) {
+  if ((BRISK_DEBUG & DBG_ONLY) == 0)
     return;
   
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_only.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_main($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_main($log) {
+  if ((BRISK_DEBUG & DBG_MAIN) == 0)
     return;
+  
+  GLOBAL $sess;
+
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
 
-  if (($fp = @fopen("/tmp/brisk_main.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_rd($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_rd($log) {
+  if ((BRISK_DEBUG & DBG_READ) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_rd.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+      
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_rd2($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_rd2($log) {
+  if ((BRISK_DEBUG & DBG_REA2) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_rd2.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+      
+
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_send($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_send($log) {
+  if ((BRISK_DEBUG & DBG_SEND) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_send.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+      
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_auth($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_lock($log) {
+  if ((BRISK_DEBUG & DBG_LOCK) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_auth.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_lock($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_wr($log) {
+  if ((BRISK_DEBUG & DBG_WRIT) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_lock.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%d] [%s] [%s]\n", time(), $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+      
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_wr($sess, $log) {
-  if (BRISK_DEBUG != TRUE && FALSE)
+function log_load($log) {
+  if ((BRISK_DEBUG & DBG_LOAD) == 0)
     return;
+  
+  GLOBAL $sess;
 
-  if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (isset($sess) == FALSE)
+    $ssess = "XXXX";
+  else
+    $ssess = $sess;
+      
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
     fclose($fp);
   }
 }
 
-function log_load($sess, $log) {
-  if (BRISK_DEBUG != TRUE)
+function log_auth($sess, $log) {
+  if ((BRISK_DEBUG & DBG_AUTH) == 0)
     return;
 
-  if (($fp = @fopen("/tmp/brisk_load.log", 'a')) != FALSE) {
-    fwrite($fp, sprintf("SESS: [%s] [%s]\n", $sess, $log));
+  if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
+    fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
     fclose($fp);
   }
 }
 
+
 function log_legal($timecur, $sess, $name, $where, $mesg) 
 {
   GLOBAL $_SERVER;
 
-  if (($fp = @fopen(LEGAL_PATH, 'a')) != FALSE) {
+  if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
     /* Unix time | session | nickname | IP | where was | mesg */
     fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|\n", $timecur, $sess, $name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
     fclose($fp);
@@ -1386,7 +1649,7 @@ function table_content($room, $user, $table_idx)
       else
        { $hilion = ""; $hilioff = ""; }
 
-    log_main($user_cur->name, sprintf("IN TABLE [%d]", $table_idx));
+    log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
     
     $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
   }
@@ -1449,7 +1712,7 @@ function standup_content(&$room, $user)
 
 function show_notify($text, $tout, $butt, $w, $h)
 {
-  log_main("SHOW_NOTIFY", $text);
+  log_main("SHOW_NOTIFY: ".$text);
   return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
 }
 
@@ -1523,7 +1786,7 @@ function show_table_info(&$room, &$table, $table_pos)
   $noty .= sprintf("</tr>");
 
   // Points.
-  log_main("show_table_info", "pnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
+  log_main("show_table_infopnt_min: ".$pnt_min."   Points_n: ".$table->points_n);
 
   for ($i = $pnt_min ; $i < $table->points_n ; $i++) {
     $noty .= sprintf('<tr><th class=\"td_points\">%d</th>', $i+1);
@@ -1677,6 +1940,10 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
     /* GENERAL STATUS */
     $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
                     $sendstep, $user->stat, $user->subst, $table_pos);
+
+    log_rd(sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
+                    $sendstep, $user->stat, $user->subst, $table_pos));
+
     /* BACKGROUND */
     $ret .= "background_set();";
     
@@ -1715,7 +1982,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
              break;
          }
        }
-       log_rd($user->sess, "O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
+       log_rd("O ".$o." VAL ".$table->card[$o]->value." Owner: ".$table->card[$o]->owner);
        
        $ret .= sprintf( ' card_send(%d,%d,%d,%8.2f,%d);|', ($table->gstart + $e) % PLAYERS_N, 
                         $i, ((($e + PLAYERS_N - $table_pos + $table->gstart) % PLAYERS_N) == 0 ?
@@ -1738,7 +2005,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
        $inhand[$table->card[$i]->owner]++;
       }
       else if ($table->card[$i]->stat == 'take') {
-       log_main("Card taked:", $table->card[$i]->value."OWN: ".$table->card[$i]->owner);
+       log_main("Card taked: ".$table->card[$i]->value."OWN: ".$table->card[$i]->owner);
        $taked[$table->card[$i]->owner]++;
       }
       else if ($table->card[$i]->stat == 'table') {
@@ -1749,7 +2016,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
       $logg .= sprintf("INHAND: %d   IN TABLE %d   TAKED %d\n", $inhand[$i], $ontabl[$i], $taked[$i]);
     }
-    log_main("Stat table:", $logg);
+    log_main("Stat table: ".$logg);
 
     /* Set ours cards. */
     $oursarg = "";
@@ -1844,7 +2111,7 @@ function calculate_winner(&$table)
     if ($table->card[$i]->stat != "table")
       continue;
 
-    log_wr($sess, sprintf("Card On table: [%d]", $i));
+    log_wr(sprintf("Card On table: [%d]", $i));
 
     $v = $table->card[$i]->value; 
     $ontab[$table->card[$i]->owner] = $v;
@@ -1897,7 +2164,7 @@ function calculate_points(&$table)
       $pro += $G_all_points[$ctt];
   }
 
-  log_wr("XXX", sprintf("PRO: [%d]", $pro));
+  log_wr(sprintf("PRO: [%d]", $pro));
 
   
   if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
@@ -1925,11 +2192,11 @@ function calculate_points(&$table)
     else
       $pt = -1;
 
-    log_wr("XXX", sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
+    log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
 
     $pt = $pt * $sig * $table->mult * ($pro == 120 ? 2 : 1);
 
-    log_wr("XXX", sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
+    log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
     
     $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
     $table->total[$i] += $pt;