spawned game added
[brisk.git] / web / briskin5 / Obj / briskin5.phh
1 <?php
2 define(MAX_BRISKIN5_PLAYERS, 3);
3
4 class Briskin5 {
5   var $user;
6   var $table;
7   var $table_idx;
8   var $comm; // commands for many people
9   var $step; // current step of the comm array
10   var $garbage_timeout;
11
12   function Briskin5 (&$room, $table_idx) {
13     $this->user = array();
14     $this->table = array();
15
16     $user  =& $room->user;
17     $table =& $room->table[$table_idx];
18
19     log_wr("xxx", "Briskin5 constructor");
20
21     for ($i = 0 ; $i < $table->player_n ; $i++) 
22       $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
23     
24     $this->table[0] =& Table::spawn(&$table);
25     $this->table_idx = $table_idx;
26     $this->garbage_timeout = 0;
27     
28     log_wr("xxx", "Briskin5 constructor end");
29   }
30
31
32   function &get_user($sess, &$idx)
33   {
34     GLOBAL $PHP_SELF, $G_false;
35
36     if (validate_sess($sess)) {
37       for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
38         if (strcmp($sess, $this->user[$i]->sess) == 0) {
39           // find it
40           $idx = $i;
41           $ret = &$this->user[$i];
42           return ($ret);
43         }
44       }
45       log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
46       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
47       // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
48     }
49     else {
50       log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
51     }
52
53     return ($G_false);
54   }
55
56
57   function garbage_manager($force)
58   {
59     
60     /* Garbage collector degli utenti in timeout */
61     $curtime = time();
62     if ($force || $this->garbage_timeout < $curtime) {
63       
64       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
65         $user_cur = &$this->user[$i];
66         if ($user_cur->sess == "")
67           continue;
68         
69         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
70           log_rd2($user_cur->sess, "AUTO LOGOUT.");
71
72           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
73             log_auth($user_cur->sess, "Autologout session.");
74             
75             $tmp_sess = $user_cur->sess;
76             $user_cur->sess = "";
77             step_unproxy($tmp_sess);
78             $user_cur->name = "";
79             $user_cur->the_end = FALSE;
80             
81             log_rd2($user_cur->sess, "AUTO LOGOUT.");
82             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
83               $this->room_wakeup(&$user_cur);
84             else if ($user_cur->subst == 'standup')
85               $this->room_outstandup(&$user_cur);
86             else
87               log_rd2($sess, "LOGOUT FROM WHAT ???");
88           }
89         }
90
91         if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
92           if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
93             $this->room_wakeup(&$user_cur);
94             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
95             $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);
96             $user_cur->step_inc();
97           }
98         }
99       }
100       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
101       
102       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
103     }
104
105     // BAN_IP_CLEAN
106
107   }
108
109
110
111
112   //
113   //  static functions
114   //
115   function &load_data($table_idx) 
116   {
117     GLOBAL $G_false, $sess;
118     $shm = FALSE;
119
120     log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
121     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
122       echo "FTOK FAILED";
123       exit;
124     }
125     
126     do {
127       if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
128         break;
129
130       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
131         break;
132       
133       shm_detach($shm);
134         
135       $ret = &$bri;
136       return ($ret); 
137     } while (FALSE);
138
139     if ($shm != FALSE)
140       shm_detach($shm);
141     
142     return ($G_false);
143   }
144   
145
146   function save_data(&$bri) 
147   {
148     GLOBAL $sess;
149     
150     $ret =   FALSE;
151     $shm =   FALSE;
152     $isacq = FALSE;
153     
154     // var_dump($bri);
155     
156     if (($tok = ftok(FTOK_PATH."/table".$bri->table_idx, "B")) == -1) 
157       return (FALSE);
158     
159     do {
160       $isacq = TRUE;
161       
162       if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
163         break;
164       
165       // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
166       if (shm_put_var($shm, $tok, $bri) == FALSE) {
167         log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
168         log_only($sess, serialize($bri));
169         break;
170       }
171       // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
172       $ret = TRUE;
173     } while (0);
174     
175     if ($shm)
176       shm_detach($shm);
177     
178     return ($ret);
179   }
180
181   function lock_data($table_idx)
182   {
183     GLOBAL $sess; 
184     
185     log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
186     //  echo "LOCK: ".FTOK_PATH."/main";
187     //  exit;
188     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
189       echo "FTOK FAILED";
190       exit;
191     }
192     // echo "FTOK ".$tok."<br>";
193     if (($res = sem_get($tok)) == FALSE) {
194       echo "SEM_GET FAILED";
195       exit;
196     }
197     if (sem_acquire($res)) {   
198       log_only($sess, "LOCK");
199       return ($res);
200     }
201     else
202       return (FALSE);
203   }
204   
205   function unlock_data($res)
206   {
207     GLOBAL $sess; 
208     
209     log_only($sess, "UNLOCK");
210
211     return (sem_release($res));
212   }
213
214
215
216 }
217
218 ?>