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