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