destroy_data and chatt_send funcs 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   var $tok;
13
14   function Briskin5 (&$room, $table_idx) {
15     $this->user = array();
16     $this->table = array();
17
18     $user  =& $room->user;
19     $table =& $room->table[$table_idx];
20
21     log_wr("xxx", "Briskin5 constructor");
22
23     for ($i = 0 ; $i < $table->player_n ; $i++) 
24       $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
25     
26     $this->table[0] =& Table::spawn(&$table);
27     $this->table_idx = $table_idx;
28     $this->garbage_timeout = 0;
29     
30     log_wr("xxx", "Briskin5 constructor end");
31   }
32
33
34   function &get_user($sess, &$idx)
35   {
36     GLOBAL $PHP_SELF, $G_false;
37
38     if (validate_sess($sess)) {
39       for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
40         if (strcmp($sess, $this->user[$i]->sess) == 0) {
41           // find it
42           $idx = $i;
43           $ret = &$this->user[$i];
44           return ($ret);
45         }
46       }
47       log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
48       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
49       // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
50     }
51     else {
52       log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
53     }
54
55     return ($G_false);
56   }
57
58
59   function garbage_manager($force)
60   {
61     
62     /* Garbage collector degli utenti in timeout */
63     $curtime = time();
64     if ($force || $this->garbage_timeout < $curtime) {
65       
66       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
67         $user_cur = &$this->user[$i];
68         if ($user_cur->sess == "")
69           continue;
70         
71         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
72           log_rd2($user_cur->sess, "AUTO LOGOUT.");
73
74           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
75             log_auth($user_cur->sess, "Autologout session.");
76             
77             $tmp_sess = $user_cur->sess;
78             $user_cur->sess = "";
79             step_unproxy($tmp_sess);
80             $user_cur->name = "";
81             $user_cur->the_end = FALSE;
82             
83             log_rd2($user_cur->sess, "AUTO LOGOUT.");
84             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
85               $this->room_wakeup(&$user_cur);
86             else if ($user_cur->subst == 'standup')
87               $this->room_outstandup(&$user_cur);
88             else
89               log_rd2($sess, "LOGOUT FROM WHAT ???");
90           }
91         }
92
93         if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
94           if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
95             $this->room_wakeup(&$user_cur);
96             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
97             $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);
98             $user_cur->step_inc();
99           }
100         }
101       }
102       log_rd2($user_cur->sess, "GARBAGE UPDATED!");
103       
104       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
105     }
106
107     // BAN_IP_CLEAN
108
109   }
110
111
112
113
114   //
115   //  static functions
116   //
117   function &load_data($table_idx) 
118   {
119     GLOBAL $G_false, $sess;
120     $shm = FALSE;
121
122     log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
123     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
124       echo "FTOK FAILED";
125       exit;
126     }
127     
128     do {
129       if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
130         break;
131
132       if (($bri = @shm_get_var($shm, $tok)) == FALSE) 
133         break;
134
135       $bri->tok = $tok;
136
137       shm_detach($shm);
138         
139       $ret = &$bri;
140       return ($ret); 
141     } while (FALSE);
142
143     if ($shm != FALSE)
144       shm_detach($shm);
145     
146     return ($G_false);
147   }
148   
149
150   function save_data(&$bri) 
151   {
152     GLOBAL $sess;
153     
154     $ret =   FALSE;
155     $shm =   FALSE;
156     $isacq = FALSE;
157     
158     // var_dump($bri);
159     
160     if (!isset($bri->tok))
161       return (FALSE);
162     
163     do {
164       $isacq = TRUE;
165       
166       if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
167         break;
168       
169       // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
170       if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
171         log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
172         log_only($sess, serialize($bri));
173         break;
174       }
175       // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
176       $ret = TRUE;
177     } while (0);
178     
179     if ($shm)
180       shm_detach($shm);
181     
182     return ($ret);
183   }
184
185   function destroy_data(&$bri) 
186   {
187     GLOBAL $sess;
188     
189     $ret =   FALSE;
190     $shm =   FALSE;
191     $isacq = FALSE;
192     
193     // var_dump($bri);
194     
195     if (!isset($bri->tok))
196       return (FALSE);
197     
198     do {
199       $isacq = TRUE;
200       
201       if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
202         break;
203       
204       if (shm_remove($shm) == FALSE) {
205         log_only($sess, "REMOVE FALLITA ".strlen(serialize($bri)));
206         log_only($sess, serialize($bri));
207         break;
208       }
209       // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
210       $ret = TRUE;
211     } while (0);
212     
213     if ($shm)
214       shm_detach($shm);
215     
216     return ($ret);
217   }
218
219   function lock_data($table_idx)
220   {
221     GLOBAL $sess; 
222     
223     log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
224     //  echo "LOCK: ".FTOK_PATH."/main";
225     //  exit;
226     if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
227       echo "FTOK FAILED";
228       exit;
229     }
230     // echo "FTOK ".$tok."<br>";
231     if (($res = sem_get($tok)) == FALSE) {
232       echo "SEM_GET FAILED";
233       exit;
234     }
235     if (sem_acquire($res)) {   
236       log_only($sess, "LOCK");
237       return ($res);
238     }
239     else
240       return (FALSE);
241   }
242   
243   function unlock_data($res)
244   {
245     GLOBAL $sess; 
246     
247     log_only($sess, "UNLOCK");
248
249     return (sem_release($res));
250   }
251
252
253   function chatt_send(&$user, $mesg)
254   {
255     if ($user->stat == 'table') {
256       $table = &$this->table[$user->table];
257     }
258     
259     $user_mesg = substr($mesg,6);
260     
261     $timecur = time();
262     
263     $dt = date("H:i ", $timecur);
264     if (strncmp($user_mesg, "/nick ", 6) == 0) {
265       log_main($user->sess, "chatt_send BEGIN");
266
267       if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
268           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
269           $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));
270           $user->step_inc();
271
272           return;
273       }
274       $user_mesg = "COMMAND ".$user_mesg;
275       // Search dup name
276       // change
277       // update local graph
278       // update remote graphs
279       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
280         $user_cur = &$this->user[$i];
281         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
282         if ($user_cur->sess == '')
283           continue;
284         if ($user_cur->name == $name_new) {
285           $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
286           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> gi&agrave; in uso.");', $dt.NICKSERV, xcape($name_new));
287           $user->step_inc();
288           break;
289         }
290       }
291       if ($i == MAX_PLAYERS) {
292         $user->name = $name_new;
293
294       log_main($user->sess, "chatt_send start set");
295         
296
297         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
298           log_main($user->sess, "chatt_send set loop");
299           
300           $user_cur = &$this->user[$i];
301           if ($user_cur->sess == '')
302             continue;
303           if ($user_cur->stat == 'room') {
304             if ($user->stat == 'room' && $user->subst == 'standup') {
305               $this->standup_update(&$user);
306             }
307             else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
308                      $user->stat == 'table') {
309               log_main($user->sess, "chatt_send pre table update");
310
311               $this->table_update(&$user);
312
313               log_main($user->sess, "chatt_send post table update");
314             }
315           }
316           else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
317             $table = &$this->table[$user->table];
318             
319             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
320             $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
321                 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
322                 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
323                 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
324                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
325                 (PLAYERS_N == 3 ? "" :  xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
326             if ($user_cur == $user)
327               $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";', 
328                                                                    xcape($user->name,ENT_COMPAT,"UTF-8"));
329             $user_cur->step_inc();
330           }
331         }
332       }
333     }
334     else {
335       for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
336         if ($user->stat == 'room') {
337           $user_cur = &$this->user[$i];
338           if ($user_cur->sess == '' || $user_cur->stat != 'room')
339             continue;
340         }
341         else {
342           $user_cur = &$this->user[$table->player[$i]];
343         }
344         
345         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
346         $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
347                                                              $dt.xcape($user->name), xcape($user_mesg));
348         $user_cur->step_inc();
349       }
350       log_legal($timecur, $user->sess, $user->name, 
351                 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
352     }
353   }
354
355   function table_wakeup(&$user)
356   {
357     $table = &$this->table[0];
358
359     log_main("WAKEUP", "begin function table  stat: ".$user->stat."  subst: ".$user->subst);
360
361     $curtime = time();
362
363     log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
364     
365     for ($i = 0 ; $i < $table->player_n ; $i++) {
366       $user_cur = &$this->user[$i];
367       log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
368       
369       if ($user_cur == $user)
370         $user_cur->subst = "shutdowner";
371       else
372         $user_cur->subst = "shutdowned";
373       $user_cur->laccwr = $curtime;
374
375       $ret = "gst.st = ".($user_cur->step+1)."; ";
376       $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
377
378       log_wr($user_cur->sess, "BRISKIN5_WAKEUP: ".$ret);
379       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
380       $user_cur->step_inc();
381     }
382   }
383
384
385 } // end of class Briskin5
386
387 ?>