garbage_manager update
[brisk.git] / web / Obj / brisk.phh
index 9f63b98..8136f8f 100644 (file)
@@ -32,7 +32,8 @@ define(COMM_N, 12);
 define(COMM_GEN_N, 50);
 define(SESS_LEN, 13);
 define(STREAM_TIMEOUT, 20);
-define(EXPIRE_TIME_RD, 180);
+// FIXME original: define(EXPIRE_TIME_RD, 180);
+define(EXPIRE_TIME_RD, 60);
 define(EXPIRE_TIME_SMAMMA, 360); 
 // BAN_TIME da allineare anche in commons.js
 define(BAN_TIME, 900); 
@@ -44,7 +45,7 @@ define(BRISK_DEBUG, FALSE);
 $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.3";
 
 $root_wellarr = Array ( 'Benvenuto in brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo layout che permette pi&ugrave; tavoli, pi&ugrave; tavoli.',
                         'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
@@ -459,6 +460,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 +489,9 @@ class User {
     $thiz->exitislock = TRUE;
     
     $thiz->table = $table;
-    
+    $thiz->table_pos = -1;
+    $thiz->table_token = "";
+
     return ($thiz);
   }
 
@@ -498,30 +502,31 @@ class User {
     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);
   }
@@ -560,6 +565,7 @@ class User {
 
     $thiz->table      = $table;
     $thiz->table_pos  = $table_pos;
+    $thiz->table_token = $from->table_token;
 
     return ($thiz);
   }
@@ -653,6 +659,7 @@ class Room {
 
   function Room () {
     $this->user = array();
+    $this->table = array();
 
     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
       $this->user[$i] =& User::create("", "");
@@ -665,11 +672,74 @@ class Room {
 
   function garbage_manager($force)
   {
-    
+    $ismod = FALSE;
+
     /* 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) {
+         if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
+           if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
+             if ($bri->garbage_manager(TRUE) == TRUE) {
+               // if garbaged you need to save data
+               Briskin5::save_data(&$bri);
+               if ($bri->is_abandoned()) {
+                 // FIXME recovery of room and clean of the table
+                 /*
+
+                 #
+                 #  KILL USERS
+                  #
+
+       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
+         log_rd2($user_cur->sess, "AUTO LOGOUT.");
+
+         if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
+           log_auth($user_cur->sess, "Autologout session.");
+           
+           $tmp_sess = $user_cur->sess;
+           $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.");
+           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 ???");
+         }
+       }
+
+                #
+                # destroy shared
+                #
+
+           Briskin5::destroy_data($user->table);
+
+
+                  */
+               }
+             }
+             $table_bri = &$bri->table[0];
+             for ($i = 0 ; $i < PLAYERS_N ; $i++) {
+               $this->user[$table_cur->player[$i]]->lacc = 
+                 $bri->user[$table_bri->player[$i]]->lacc;
+             }
+           }
+           Briskin5::unlock_data($sem);
+         }
+       }
+      }
+
       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
        $user_cur = &$this->user[$i];
        if ($user_cur->sess == "")
@@ -685,6 +755,7 @@ 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.");
@@ -709,10 +780,10 @@ class Room {
       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
       
       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
+      $ismod = TRUE;
     }
 
-    // BAN_IP_CLEAN
-
+    return ($ismod);
   }
 
 
@@ -796,9 +867,95 @@ class Room {
       $user_cur->step_inc();
     }
   }
-  
 
+  function room_join_wakeup(&$user, $update_lacc = FALSE)
+  {
+    $table_idx = $user->table;
+    $table = &$this->table[$table_idx];
+    
+    log_main($user->sess, "JOIN_WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
+
+    $curtime = time();
+    $user_wup = array();
+    $user_wup_n = 0;
+    log_main($user->sess, "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($user->sess, "PREIMPOST INLOOP name: ".$user_cur->name);
+      if ($user_cur->sess != "") {
+       if ($update_lacc == TRUE) {
+         $user_cur->laccwr = $curtime;
+       }
+       log_load($user->sess, "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($user_cur->sess, "START LOOP");
+      $user_cur = &$this->user[$i];
+      if ($user_cur->sess == '' || $user_cur->stat != 'room') {
+       log_main($user_cur->sess, "name: ".$user_cur->name."skip   subst: ".$user_cur->subst);
+       continue;
+      }
+
+      log_main($user->sess, "___");
+      log_main($user->sess, "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($user->sess, "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;
 
+       log_main($user->sess, "JOIN_WAKEUP wup_idx ".$wup_idx."  wup_n ".$user_wup_n);
+
+       if ($wup_idx < $user_wup_n) {
+         log_main($user->sess, "JOIN_WAKEUP less");
+         // set the new status 
+         $ret .=  '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($user->sess, "JOIN_WAKEUP end less");
+       }
+       else {
+         log_main($user->sess, "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($user->sess, "JOIN_WAKEUP end more");
+       }
+      }
+      log_wr($user->sess, "ROOM_JOIN_WAKEUP: ".$ret);
+      $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+      $user_cur->step_inc();
+    }
+  }
 
   function room_outstandup(&$user)
   {
@@ -1291,7 +1448,7 @@ function log_lock($sess, $log) {
 }
 
 function log_wr($sess, $log) {
-  if (BRISK_DEBUG != TRUE && FALSE)
+  if (BRISK_DEBUG != TRUE)
     return;
 
   if (($fp = @fopen("/tmp/brisk_wr.log", 'a')) != FALSE) {
@@ -1677,6 +1834,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($user->sess, sprintf( 'SHOW_TABLE: gst.st = %d; stat = "%s"; subst = "%s"; table_pos = %d;',
+                    $sendstep, $user->stat, $user->subst, $table_pos));
+
     /* BACKGROUND */
     $ret .= "background_set();";