source IP address stored into log
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index f4e266d..1d11f36 100644 (file)
@@ -2,7 +2,7 @@
 /*
  *  brisk - briskin5.phh
  *
- *  Copyright (C) 2006-2011 Matteo Nastasi
+ *  Copyright (C) 2006-2012 Matteo Nastasi
  *                          mailto: nastasi@alternativeoutput.it 
  *                                  matteo.nastasi@milug.org
  *                          web: http://www.alternativeoutput.it
@@ -183,7 +183,7 @@ class Bin5_table extends Table {
        $thiz->asta_pla_n=  -1;
        $thiz->asta_card =  -1;
        $thiz->asta_pnt  =  -1;
-       $thiz->mult      =   1;
+       $thiz->mult      =   0;
     
        $thiz->points    =   array( );
        $thiz->points_n  =   0;
@@ -260,7 +260,7 @@ class Bin5_table extends Table {
         $thiz->bunch_create();
         $thiz->mazzo    = rand(0,PLAYERS_N-1);
         $thiz->points_n = 0;
-        $thiz->mult     = 1;
+        $thiz->mult     = 0;
         $thiz->old_win    = -1;
         $thiz->old_reason = "";
 
@@ -274,6 +274,32 @@ class Bin5_table extends Table {
         return ($thiz);
     }
 
+    function asta2mult($asta_pnt)
+    {
+        if ($asta_pnt > 110)
+            return (6);
+        else if ($asta_pnt > 100)
+            return (5);
+        else if ($asta_pnt > 90)
+            return (4);
+        else if ($asta_pnt > 80)
+            return (3);
+        else if ($asta_pnt > 70)
+            return (2);
+        else
+            return (1);
+    }
+
+    function multer($is_new)
+    {
+        if ($is_new) {
+            return (pow(2, $this->mult) * $this->asta2mult($this->asta_pnt));
+        }
+        else {
+            return (pow(2, $this->old_mult) * $this->asta2mult($this->old_asta_pnt));
+        }
+    }
+
 
     //   function bunch_create_old() function AND 
     //   {
@@ -329,7 +355,7 @@ class Bin5_table extends Table {
         /* MOVED INTO SPAWN
            $this->mazzo    =  rand(0,PLAYERS_N-1);
            $this->points_n =  0;
-           $this->mult     =  1;
+           $this->mult     =  0;
            $this->old_win  = -1;
            $this->old_reason = "";
         */
@@ -477,17 +503,20 @@ class Bin5_user extends User {
        } 
     */
     
-    function spawn($from, $table, $table_pos)
+    function spawn($from, &$bri, $table, $table_pos, $get, $post, $cookie)
     {
-        GLOBAL $CO_bin5_pref_ring_endauct;
-        
         if (($thiz = new Bin5_user()) == FALSE)
             return (FALSE);
-        
+
+        if (($CO_bin5_pref_ring_endauct = gpcs_var("CO_bin5_pref_ring_endauct", $get, $post, $cookie)) === FALSE) {
+            $CO_bin5_pref_ring_endauct = "";
+        }
+
         $thiz->parentcopy($from);
         
         /* NOTE: at this moment idx and table_pos fields have the same value 
            but diffentent functions, we keep them separated for a while */
+        $thiz->room       = $bri;
         $thiz->idx        = $table_pos;
         $thiz->asta_card  = -2;
         $thiz->asta_pnt   = -1;
@@ -497,7 +526,7 @@ class Bin5_user extends User {
         log_wr("Bin5 constructor");
         
         $this->privflags  = ($CO_bin5_pref_ring_endauct == "true" ? BIN5_USER_FLAG_RING_ENDAUCT : 0) | 0;
-        
+
         $thiz->table_orig = $table;
         $thiz->table      = 0;
         $thiz->table_pos  = $table_pos;
@@ -581,112 +610,6 @@ class Bin5_user extends User {
         @unlink(BIN5_PROXY_PATH."/table".$tab_id."/".$sess.".step");
     }
     
-    static function load_data($tab_id, $id, $sess) 
-    {
-        log_load("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
-        
-        do {
-            if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
-                log_main("ftok failed");
-                break;
-            }
-            
-            if (($shm_sz = sharedmem_sz($tok)) == -1) {
-                log_main("shmop_open failed");
-            }
-            
-            if ($shm_sz == -1)
-                $shm_sz = SHM_DIMS_U_MIN;
-            
-            if ($shm = shm_attach($tok, $shm_sz)) {
-                if (($user = @shm_get_var($shm, $tok)) == FALSE) {
-                    break;
-                }
-                
-                if ($sess != FALSE && $user->sess != $sess) {
-                    break;
-                }
-                log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
-                
-                if ($user == FALSE) {
-                    log_only("INIT MAIN DATA");
-                    
-                    // SHSPLIT FIXME: init_data for User class ??
-                    $user = User::create($id, "", "");
-                    if (@shm_put_var($shm, $tok, $user) == FALSE) {
-                        log_shme("Bin5_user::save_data2");
-
-                        log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
-                        log_only(serialize($user));
-                    }
-                }
-                else {
-                    if ($sess != FALSE) {
-                        // This part isn't strictly required but is good to verify
-                        // the coerence of cached and User class saved value of step field.
-                        $old_step = $user->step;
-                        $arr = Bin5_user::load_step($tab_id, $sess);
-                        $user->step = $arr['s'];
-                        if ($old_step != $user->step) {
-                            log_crit("Bin5:: steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
-                        }
-
-                    }
-                }
-                
-                $user->shm_sz = $shm_sz;
-                
-                shm_detach($shm);
-            }
-            
-            //  
-            // SHSPLIT: load users from the shared memory
-            //
-            return ($user);
-        } while (0);
-        
-        log_crit("Bin5_user::load_data:ret FALSE");
-        
-        return (FALSE);
-    }
-
-    static function save_data($user, $tab_id, $id) 
-    {
-        GLOBAL $sess;
-      
-        $shm =   FALSE;
-      
-        if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
-            return (FALSE);
-        }
-        while ($user->shm_sz < SHM_DIMS_U_MAX) {
-            if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
-                break;
-          
-            // log_only("PUT_VAR DI ".strlen(serialize($user)));
-            if (@shm_put_var($shm, $tok, $user) != FALSE) {
-                shm_detach($shm);
-                $user->save_step();
-                log_main("User[".$id."] saved.");
-
-                log_shme("Bin5_user::save_data");
-
-                return (TRUE);
-            }
-            if (shm_remove($shm) === FALSE) {
-                log_only("REMOVE FALLITA");
-                break;
-            }
-            shm_detach($shm);
-            $user->shm_sz += SHM_DIMS_U_DLT;
-        } 
-      
-        if ($shm)
-            shm_detach($shm);
-      
-        return (FALSE);
-    }
-
     function destroy_data($tab_id) 
     {
         do {
@@ -717,6 +640,147 @@ class Bin5_user extends User {
         return ($ret);
     }
 
+    static function blocking_error($is_unrecoverable)
+    {
+        log_crit("BLOCKING_ERROR UNREC: ".($is_unrecoverable ? "TRUE" : "FALSE"));
+        return (sprintf(($is_unrecoverable ? 'xstm.stop(); ' : '').'window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");'));
+    }
+    
+    protected function page_sync($sess, $page)
+    {
+        log_rd2("PAGE_SYNC");
+        printf("xXx BIN5_USER::PAGE_SYNC\n");
+        return (sprintf('xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("%s");', $page));
+    }
+
+    protected function maincheck($cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_subst, &$new_step, $splashdate, $table_idx, $table_token)
+    {
+        GLOBAL $G_lang, $mlang_indrd;
+        GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
+        GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
+        $CO_splashdate = "CO_splashdate".$G_splash_idx;
+        $$CO_splashdate = $splashdate;
+        
+        GLOBAL $S_load_stat;
+        
+        log_rd("maincheck begin");
+        
+        $ret = FALSE;
+        $curtime = time();
+        
+      /* Nothing changed, return. */
+      if ($cur_step == $this->step) 
+          return (FALSE);
+      
+      log_rd2("do other cur_stat[".$cur_stat."] user->stat[".$this->stat."] cur_step[".$cur_step."] user_step[".$this->step."]");
+      
+      if ($cur_step == -1) {
+          /*
+           *  if $cur_step == -1 load the current state from the main struct
+           */
+          
+          $S_load_stat['wR_minusone']++;
+          
+          // if ($this->the_end == TRUE) {
+          // log_rd2("main_check: the end".var_export(debug_backtrace()));
+          // }
+          
+          if ($this->trans_step != -1) {
+              log_rd2("TRANS USATO ".$this->trans_step);
+              $cur_step = $this->trans_step;
+              $this->trans_step = -1;
+          }
+          else {
+              log_rd2("TRANS NON ATTIVATO");
+          }
+      }
+      
+      
+      /* this part I suppose is read only on $this->room structure */
+      if ($cur_step == -1) {
+          log_rd2("PRE-NEWSTAT");
+              
+          /***************
+           *             *
+           *    TABLE    *
+           *             *
+           ***************/
+          if ($this->stat == 'table') {
+              log_load("RESYNC");
+              /* NOTE: $this->room is associated with the current $bri object */
+              printf("xXx CLASS NAME [%s]\n", get_class($this->room));
+              $ret = show_table(&$this->room, $this, $this->step, FALSE, FALSE);
+          }
+          log_rd2("NEWSTAT: ".$this->stat);
+
+          $new_stat =  $this->stat;
+          $new_subst = $this->subst;
+          $new_step =  $this->step;
+      } /* if ($cur_step == -1) { */
+      else {
+          /* $sem = Room::lock_data(FALSE); */
+          $S_load_stat['rU_heavy']++;
+          
+          if ($cur_step < $this->step) {
+              do {
+                  if ($cur_step + COMM_N < $this->step) {
+                      if (($cur_stat != $this->stat)) {
+                          $to_stat = $this->stat;
+                          /* Room::unlock_data($sem); */
+                          log_load("RESYNC");
+                          printf("xXx BIN5_USER::MAINCHECK\n");
+                          return ($this->page_sync($this->sess, ($to_stat == "table" ? "index.php" : "../index.php"), $this->table, $this->table_token));
+                      }
+                      log_rd2("lost history, refresh from scratch");
+                      printf("xXx LOST HISTORY!\n");
+                      $new_step = -1;
+                      break;
+                  } 
+                  for ($i = $cur_step ; $i < $this->step ; $i++) {
+                      $ii = $i % COMM_N;
+                      log_rd2("ADDED TO THE STREAM: ".$this->comm[$ii]);
+                      $ret .= $this->comm[$ii];
+                  }
+                  $new_stat =  $this->stat;
+                  $new_subst = $this->subst;
+                  $new_step =  $this->step;
+              } while (0);
+              
+              log_rd2($this->step, 'index_rd.php: after ret set');
+              
+              if ($this->the_end == TRUE) {
+                  log_rd2("LOGOUT BYE BYE!!");
+                  log_auth($this->sess, "Explicit logout.");
+                  
+                  if ($this->the_end == TRUE) {
+                      $this->reset();
+                      
+                      // FIXME !!! 
+                      /* if ($this->subst == 'sitdown') { */
+                      /*     log_load("ROOM WAKEUP"); */
+                      /*     $this->room->room_wakeup($this); */
+                      /* } */
+                      /* else if ($this->subst == 'standup') */
+                      /*     $this->room->room_outstandup($this); */
+                      /* else */
+                      /*     log_rd2("LOGOUT FROM WHAT ???"); */
+                      
+                  } /* if ($this->the_end == TRUE) { ... */
+              } /* if ($this->the_end == TRUE) { ... */
+          } /* if ($cur_step < $this->step) { */
+          
+          /* Room::unlock_data($sem); */
+      }  /* else of if ($cur_step == -1) { */
+      
+    
+      return ($ret);
+  }  //   function maincheck (...
+
+
+
+
+
+
 } // end class Bin5_user
 
 
@@ -728,7 +792,7 @@ class Bin5 {
     var $table;
     var $comm; // commands for many people
     var $step; // current step of the comm array
-    // externalized var $garbage_timeout;
+    var $garbage_timeout;
     var $shm_sz;
     
     var $table_idx;
@@ -760,7 +824,7 @@ class Bin5 {
         return TRUE;
     }
 
-    function Bin5 ($room, $table_idx, $table_token) {
+    function Bin5 ($room, $table_idx, $table_token, $get, $post, $cookie) {
         $this->user = array();
         $this->table = array();
         
@@ -778,7 +842,7 @@ class Bin5 {
         
         for ($i = 0 ; $i < $table->player_n ; $i++) {
             $user[$table->player[$i]]->table_token = $table_token;
-            $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $table_idx, $i);
+            $this->user[$i] = Bin5_user::spawn($user[$table->player[$i]], $this, $table_idx, $i, $get, $post, $cookie);
         }
         $this->table[0] = Bin5_table::spawn(&$table);
         
@@ -786,7 +850,7 @@ class Bin5 {
         
         $this->table_idx = $table_idx;
         $this->table_token = $table_token;
-        Bin5::garbage_time_expire_set($table_idx, 0);
+        $this->garbage_timeout = 0;
         
         log_wr("Bin5 constructor end");
     }
@@ -824,8 +888,7 @@ class Bin5 {
         /* Garbage collector degli utenti in timeout */
         $ismod = FALSE;
         $curtime = time();
-        // externalized if ($force || $this->garbage_timeout < $curtime) {
-        if ($force || Bin5::garbage_time_is_expired($this->table_idx, $curtime)) {
+        if ($force || $this->garbage_timeout < $curtime) {
             for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
                 $user_cur = $this->user[$i];
                 if ($user_cur->sess == "" || 
@@ -863,8 +926,7 @@ class Bin5 {
             }
             log_rd2($user_cur->sess." GARBAGE UPDATED!");
             
-            // externalized $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
-            Bin5::garbage_time_expire_set($this->table_idx, $curtime + GARBAGE_TIMEOUT);
+            $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
             
             $ismod = TRUE;
         }
@@ -872,112 +934,6 @@ class Bin5 {
         return ($ismod);
     }
     
-    // Bin5::load_data
-    static function load_data($table_idx, $table_token = "") 
-    {
-        $shm = FALSE;
-        
-        log_wr("TABLE_FTOK ".FTOK_PATH."/bin5/table".$table_idx."/table");
-        
-        do {
-            if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
-                log_main("ftok failed");
-                break;
-            }
-            
-            if (($shm_sz = sharedmem_sz($tok)) == -1) {
-                log_main("shmop_open failed");
-                break;
-            }
-       
-            if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
-                break;
-
-            if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
-                break;
-
-            if ($table_token != "" && $bri->table_token != $table_token) {
-                log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
-                break;
-            }
-            $bri->tok = $tok;
-
-            shm_detach($shm);
-      
-            for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-                if (($bri->user[$i] = Bin5_user::load_data($table_idx, $i, FALSE)) == FALSE) {
-                    log_crit("Bin5_user::load_data failed");
-                    break;
-                }
-            }
-            if ($i < BIN5_MAX_PLAYERS) {
-                break;
-            }
-      
-            return ($bri); 
-        } while (FALSE);
-
-        if ($shm != FALSE)
-            shm_detach($shm);
-
-        log_wr("briskin5 load_data failed");
-    
-        return (FALSE);
-    }
-  
-
-
-    function save_data($bri) 
-    {
-        GLOBAL $sess;
-      
-        $ret =   FALSE;
-        $shm =   FALSE;
-      
-        log_main("SAVE BRISKIN5 DATA");
-      
-        if (!isset($bri->tok))
-            return (FALSE);
-      
-        $user_park = array();
-        for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-            $user_park[$i] = $bri->user[$i];
-            $bri->user[$i] = FALSE;
-        }
-      
-        while ($bri->shm_sz < BIN5_SHM_MAX) {
-            if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
-                break;
-          
-            if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
-
-                log_shme("Bin5::save_data");
-
-                $ret = TRUE;
-                break;
-            }
-            if (shm_remove($shm) === FALSE) {
-                log_only("REMOVE FALLITA");
-                break;
-            }
-            shm_detach($shm);
-            $bri->shm_sz += BIN5_SHM_DLT;
-        } 
-      
-        if ($shm)
-            shm_detach($shm);
-      
-        // SHSPLIT: reattach users to the room class
-        for ($i = 0 ; $i < BIN5_MAX_PLAYERS ; $i++) {
-            Bin5_user::save_data($user_park[$i], $bri->table_idx, $i);
-            $bri->user[$i] = $user_park[$i];
-        }
-        log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
-      
-        return ($ret);
-    }
-  
-
 
     function destroy_data() 
     {
@@ -1153,7 +1109,7 @@ class Bin5 {
                                                                      $dt, $user->flags, xcape($user->name), xcape($user_mesg));
                 $user_cur->step_inc();
             }
-            log_legal($curtime, 'xxx', $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
+            log_legal($curtime, $user->ip, $user, ($user->stat == 'room' ? 'room' : 'table '.$user->table_orig),$user_mesg);
         }
     }
 
@@ -1178,7 +1134,7 @@ class Bin5 {
             $user_cur->laccwr = $curtime;
 
             $ret = "gst.st = ".($user_cur->step+1)."; ";
-            $ret .= 'gst.st_loc++; hstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
+            $ret .= 'gst.st_loc++; xstm.stop(); window.onbeforeunload = null; window.onunload = null; document.location.assign("../index.php");|';
 
             log_wr($user_cur->sess." BIN5_WAKEUP: ".$ret);
             $user_cur->comm[$user_cur->step % COMM_N] = $ret;
@@ -1210,98 +1166,75 @@ class Bin5 {
         return ($is_ab);
     }
 
-
-    static function garbage_time_is_expired($tab_id, $tm) 
-    {
-        $ret = TRUE;
-        $fp = FALSE;
-        do {
-            if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
-                mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'rb')) == FALSE)
-                break;
-            if (($s = fread($fp, 4)) == FALSE)
-                break;
-            if (mb_strlen($s, "ASCII") != 4)
-                break;
-            $arr = unpack('Le', $s);
-            if ($arr['e'] > $tm)
-                $ret = FALSE;
-        } while (0);
-      
-        if ($fp != FALSE)
-            fclose($fp);
-      
-        log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
-      
-        return ($ret);
-    }
-
-    static function garbage_time_expire_set($tab_id, $tm) 
-    {
-        do {
-            if (file_exists(BIN5_PROXY_PATH."/table".$tab_id) == FALSE)
-                mkdir(BIN5_PROXY_PATH."/table".$tab_id, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/table".$tab_id."/garbage_time.expired", 'wb')) == FALSE)
-                break;
-            fwrite($fp, pack("L",$tm));
-            fclose($fp);
-          
-            return (TRUE);
-        } while (0);
-      
-        return (FALSE);
-    }
-
-    static function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
+    static function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
     {
         printf("NEW_SOCKET (root): %d\n", intval($new_socket));
         
+        $enc = get_encoding($header);
+        if (isset($header['User-Agent'])) {
+            if (strstr($header['User-Agent'], "MSIE")) {
+                $transp_type = "htmlfile";
+            }
+            else {
+                $transp_type = "xhr";
+            }
+        }
+        else {
+            $transp_type = "iframe";
+        }
+        force_no_cache($header_out);
+
         if (($table_idx = gpcs_var('table_idx', $get, $post, $cookie)) === FALSE) 
             unset($table_idx);
         
         if (($table_token = gpcs_var('table_token', $get, $post, $cookie)) === FALSE) 
             unset($table_token);
 
+
         switch ($path) {
         case "":
         case "index.php":
             ob_start();
-            bin5_index_main($header_out, $addr, $get, $post, $cookie);
+            bin5_index_main($transp_type, $header_out, $addr, $get, $post, $cookie);
             $content = ob_get_contents();
             ob_end_clean();
         
-            force_no_cache($header_out);
-
-            $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
+            $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
             return TRUE;
         
         break;
         case "index_wr.php":
-            $bri = $s_a_p->app->match_get($table_idx, $table_token);
-            ob_start();
-            bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
-            $content = ob_get_contents();
-            ob_end_clean();
-            
-            force_no_cache($header_out);
-
-            $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
+            // FIXME $content can be unset
+            if (isset($table_idx) && isset($table_token)) {
+                if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) != FALSE) {
+                    ob_start();
+                    bin5_index_wr_main($bri, $addr, $get, $post, $cookie);
+                    $content = ob_get_contents();
+                    ob_end_clean();
+                }
+                else {
+                    $content = "Bin5 Load data error";
+                }
+            }
+            else {
+                $content = "Bin5 Load data error";
+            }
+            $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
             return TRUE;
             
             break;
         case "index_rd_ifra.php":
             do {
-                if (($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL) {
-                    return (FALSE);
-                }
-                if (!isset($cookie['sess'])
+                if (!isset($table_idx)
+                    || !isset($table_token)
+                    || !isset($cookie['sess'])
+                    || ($bri = $s_a_p->app->match_get($table_idx, $table_token)) == NULL
                     || (($user = $bri->get_user($cookie['sess'], $idx)) == FALSE)) {
-                    $content = Bin5_user::stream_fini(TRUE);
-                    
-                    $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
+
+                    $content = Bin5_user::stream_fini($s_a_p->rndstr, TRUE);
+                    $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
+
                     return TRUE;
-                    
                     break;
                 }
                 // close a previous opened index_read_ifra socket, if exists
@@ -1313,8 +1246,8 @@ class Bin5 {
                 }
                 
                 $content = "";
-                $user->stream_init($header_out, $content, $get, $post, $cookie);
-                $response = headers_render($header_out, -1).chunked_content($content);
+                $user->stream_init($s_a_p->rndstr, $enc, $header_out, $content, $get, $post, $cookie);
+                $response = headers_render($header_out, -1).chunked_content($user->rd_zls_get(), $content);
                 $response_l = mb_strlen($response, "ASCII");
                 
                 $wret = @fwrite($new_socket, $response, $response_l);
@@ -1367,21 +1300,6 @@ function locshm_exists($tok)
     
 }
 
-function asta2mult($asta_pnt)
-{ 
-    if ($asta_pnt > 110) 
-        return (5);
-    else if ($asta_pnt > 100) 
-        return (4);
-    else if ($asta_pnt > 90) 
-        return (3);
-    else if ($asta_pnt > 80) 
-        return (2);
-    else if ($asta_pnt > 70) 
-        return (1);
-    else
-        return (0);
-}
 
 // rendiamo qui l'elenco dei punti come return della func
 function calculate_points(&$table)
@@ -1400,6 +1318,7 @@ function calculate_points(&$table)
     $table->old_asta_pnt = $table->asta_pnt;
     $table->old_mult = $table->mult;
 
+    // count points for the temporary 2 teams
     for ($i = 0 ; $i < (BIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
         // for ($i = 0 ; $i < 40 ; $i++) {
         $ctt = $table->card[$i]->value % 10;
@@ -1410,8 +1329,8 @@ function calculate_points(&$table)
 
     log_wr(sprintf("PRO: [%d]", $pro));
 
-  
-    if ($table->asta_pnt == 61 && $pro == 60) { // PATTA !
+    // PATTA case !
+    if ($table->asta_pnt == 61 && $pro == 60) {
         $table->points[$table->points_n % MAX_POINTS] = array();
         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
             $table->points[$table->points_n % MAX_POINTS][$i] = 0;
@@ -1429,8 +1348,6 @@ function calculate_points(&$table)
     else
         $sig = -1;
 
-    $gamult = asta2mult($table->asta_pnt);
-  
     $table->points[$table->points_n % MAX_POINTS] = array();
     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
         if ($i == $table->asta_win) 
@@ -1444,9 +1361,9 @@ function calculate_points(&$table)
 
         log_wr(sprintf("PRO: pt[%d][%d] = %d", $table->points_n % MAX_POINTS, $i, $pt));
 
-        $pt = $pt * $sig * ($gamult + $table->mult) * ($pro == 120 ? 2 : 1);
+        $pt = $pt * $sig * $table->multer(TRUE) * ($pro == 120 ? 2 : 1);
 
-        log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->mult, ($pro == 120 ? 2 : 1)));
+        log_wr(sprintf("PRO:[%d][%d][%d]", $sig, $table->multer(TRUE), ($pro == 120 ? 2 : 1)));
     
         $table->points[$table->points_n % MAX_POINTS][$i] = $pt;
         $table->total[$i] += $pt;
@@ -1454,7 +1371,7 @@ function calculate_points(&$table)
     }
     $table->points_n++;
     $table->old_pnt = $pro;
-    $table->mult = 1;
+    $table->mult = 0;
   
     return($ret);
 }
@@ -1469,14 +1386,14 @@ function calculate_points(&$table)
    from table: asta cmd e tutti passano:  TRUE, TRUE
    from table: fine partita:              TRUE, TRUE
 */
-function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
+function show_table(&$bri, &$user, $sendstep, $is_transition, $is_again)
 {
     $table_idx = $user->table;
-    $table     = $room->table[$table_idx];
+    $table     = $bri->table[$table_idx];
     $table_pos = $user->table_pos;
 
     $ret = "table_init();";
-    $ret .= $table->exitlock_show(&$room->user, $table_pos);
+    $ret .= $table->exitlock_show(&$bri->user, $table_pos);
     if (!$is_again) {
         /* GENERAL STATUS */
         $ret .= sprintf( 'gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
@@ -1490,24 +1407,25 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
         /* USERS INFO */
         $ret .= $user->myname_innerHTML();
         $ret .= sprintf('set_names([%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"], [%d, "%s"]); ',
-                        $room->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
-                        xcape($room->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
+                        $bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->flags,
+                        xcape($bri->user[$table->player[($table_pos) % BIN5_PLAYERS_N]]->name),
 
-                        $room->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
-                        xcape($room->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
+                        $bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->flags,
+                        xcape($bri->user[$table->player[($table_pos+1) % BIN5_PLAYERS_N]]->name),
 
-                        $room->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
-                        xcape($room->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
+                        $bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->flags,
+                        xcape($bri->user[$table->player[($table_pos+2) % BIN5_PLAYERS_N]]->name),
 
-                        (BIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
-                        (BIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
+                        (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->flags),
+                        (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+3) % BIN5_PLAYERS_N]]->name)),
 
-                        (BIN5_PLAYERS_N == 3 ? 0 : $room->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
-                        (BIN5_PLAYERS_N == 3 ? "" :  xcape($room->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
+                        (BIN5_PLAYERS_N == 3 ? 0 : $bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->flags),
+                        (BIN5_PLAYERS_N == 3 ? "" :  xcape($bri->user[$table->player[($table_pos+4) % BIN5_PLAYERS_N]]->name)));
     }
     /* NOTIFY FOR THE CARD MAKER */
     if ($is_transition) { //  && $user->subst ==  "asta" superfluo
-        $ret .= show_table_info(&$room, &$table, $table_pos);
+        $ret .= show_table_info(&$bri, &$table, $table_pos);
+        $ret .= "setTimeout(preload_images, 500, g_preload_img_arr, g_imgct);";
     }
     if (!$is_again) 
         $ret .= table_wellcome($user);
@@ -1597,7 +1515,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
         /* show users auction status */
         $showst = "";
         for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) {
-            $user_cur = &$room->user[$table->player[$i]];
+            $user_cur = &$bri->user[$table->player[$i]];
             $showst .= sprintf("%s%d", ($i == 0 ? "" : ", "), 
                                ($user_cur->asta_card < 9 ? $user_cur->asta_card : $user_cur->asta_pnt));
         }
@@ -1644,7 +1562,7 @@ function show_table(&$room, &$user, $sendstep, $is_transition, $is_again)
             $ret .= "remark_off();";
     
         /* WHO CALL AND WHAT */
-        $ret .= briscola_show($room, $table, $user);
+        $ret .= briscola_show($bri, $table, $user);
     
     }
     return ($ret);
@@ -1696,19 +1614,19 @@ function calculate_winner(&$table)
     return ($cur_win);
 }
 
-function show_table_info(&$room, &$table, $table_pos)
+function show_table_info(&$bri, &$table, $table_pos)
 {
     GLOBAL $G_lang, $mlang_bin5_bin5;
 
     $ret = "";
-    $user = $room->user[$table->player[$table_pos]];
+    $user = $bri->user[$table->player[$table_pos]];
 
     $pnt_min = $table->points_n - MAX_POINTS < 0 ? 0 : $table->points_n - MAX_POINTS;
     $noty = sprintf('<table class=\"points\"><tr><th></th>');
   
     // Names.
     for ($i = 0 ; $i < BIN5_PLAYERS_N ; $i++) 
-        $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($room->user[$table->player[$i]]->name));
+        $noty .= sprintf('<th class=\"td_points\">%s</th>', xcape($bri->user[$table->player[$i]]->name));
     $noty .= sprintf("</tr>");
 
     // Points.
@@ -1743,8 +1661,8 @@ function show_table_info(&$room, &$table, $table_pos)
             /* MLANG: "<hr>Nell'ultima mano ha chiamato <b>%s</b>, il socio era <b>%s</b>,<br>", "hanno fatto <b>cappotto</b> EBBRAVI!.<hr>", "dovevano fare <b>%s</b> punti e ne hanno fatti <b>%d</b>: hanno <b>%s</b>.<hr>", "<hr>Nell'ultima mano <b>%s</b> si &egrave; chiamato in mano,<br>", "ha fatto <b>cappotto</b> EBBRAVO!.<hr>", "doveva fare <b>%s</b> punti e ne ha fatti <b>%d</b>: ha <b>%s</b>.<hr>", ($table->old_asta_pnt > 61 ? "almeno ".$table->old_asta_pnt : 'pi&ugrave; di 60'), $table->old_pnt, ($wol == 1 ? "vinto" : ($wol == 0 ? "pareggiato" : "perso" */
       
             $noty .= sprintf($mlang_bin5_bin5['info_part'][$G_lang], 
-                             xcape($room->user[$win]->name),
-                             xcape($room->user[$fri]->name));
+                             xcape($bri->user[$win]->name),
+                             xcape($bri->user[$fri]->name));
             if ($table->old_pnt == 120) {
                 $noty .= sprintf($mlang_bin5_bin5['info_capp'][$G_lang]);
             }
@@ -1757,7 +1675,7 @@ function show_table_info(&$room, &$table, $table_pos)
         }
         else {
             $noty .= sprintf($mlang_bin5_bin5['info_alon'][$G_lang],
-                             xcape($room->user[$win]->name));
+                             xcape($bri->user[$win]->name));
             if ($table->old_pnt == 120) {
                 $noty .= sprintf($mlang_bin5_bin5['info_acap'][$G_lang]);
             }
@@ -1770,8 +1688,9 @@ function show_table_info(&$room, &$table, $table_pos)
                                  ($wol == 1 ? $mlang_bin5_bin5['info_win'][$G_lang] : ($wol == 0 ? $mlang_bin5_bin5['info_peer'][$G_lang] : $mlang_bin5_bin5['info_lost'][$G_lang])));
             }
         }
-        if (($table->old_mult + asta2mult($table->old_asta_pnt)) > 1) {
-            $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($table->old_mult + asta2mult($table->old_asta_pnt)));
+        $old_multer = $table->multer(FALSE);
+        if ($old_multer > 1) {
+            $noty .= sprintf($mlang_bin5_bin5['info_omul'][$G_lang], multoval($old_multer));
         }
         $noty .= "<hr><br>";
     }
@@ -1779,7 +1698,7 @@ function show_table_info(&$room, &$table, $table_pos)
     if ($table->mazzo == $table_pos) 
         $noty .= $mlang_bin5_bin5['info_yshuf'][$G_lang];
     else {
-        $unam = xcape($room->user[$table->player[$table->mazzo]]->name);
+        $unam = xcape($bri->user[$table->player[$table->mazzo]]->name);
         $noty .= sprintf($mlang_bin5_bin5['info_shuf'][$G_lang], $unam);
     }
 
@@ -1798,12 +1717,13 @@ function show_table_info(&$room, &$table, $table_pos)
         $noty .= $mlang_bin5_bin5['info_yturn'][$G_lang];
     }
     else {
-        $unam = xcape($room->user[$table->player[$curplayer]]->name);
+        $unam = xcape($bri->user[$table->player[$curplayer]]->name);
         $noty .= sprintf($mlang_bin5_bin5['info_turn'][$G_lang], $unam);
     }
   
-    if (($table->mult + asta2mult($table->asta_pnt)) > 1) {
-        $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($table->mult + asta2mult($table->asta_pnt)));
+    $multer = $table->multer(TRUE);
+    if ($multer > 1) {
+        $noty .= sprintf($mlang_bin5_bin5['info_mult'][$G_lang], multoval($multer) );
     }
     $noty .= "<hr><br>";
     $ret .= show_notify($noty, 3000, $mlang_bin5_bin5['btn_bkgame'][$G_lang], 500, 400);
@@ -1829,7 +1749,7 @@ function table_wellcome($user)
 }
 
 
-function briscola_show($room, $table, $user)
+function briscola_show($bri, $table, $user)
 {
     GLOBAL $G_lang, $mlang_bin5_bin5;
     $ptnadd = "";
@@ -1846,7 +1766,7 @@ function briscola_show($room, $table, $user)
     else {
         $prestr = sprintf('$("callerinfo").innerHTML = "%s";', $mlang_bin5_bin5['call_call'][$G_lang]);
         $ret .= sprintf($prestr, 
-                        xcape($room->user[$table->player[$table->asta_win]]->name), $ptnadd);
+                        xcape($bri->user[$table->player[$table->asta_win]]->name), $ptnadd);
     }
     $ret .= sprintf('set_iscalling(%d);', ($table->asta_win - $user->table_pos + BIN5_PLAYERS_N) % BIN5_PLAYERS_N);