2 define(BRISKIN5_PLAYERS_N, 3);
3 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
4 define(BRISKIN5_SHM_DIMS, (50000 * BRISKIN5_MAX_PLAYERS));
12 var $comm; // commands for many people
13 var $step; // current step of the comm array
20 function Briskin5 (&$room, $table_idx, $table_token) {
21 $this->user = array();
22 $this->table = array();
24 $this->the_end = FALSE;
26 if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
32 $table =& $room->table[$table_idx];
34 log_wr("Briskin5 constructor");
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);
40 $this->table[0] =& Table::spawn(&$table);
41 $this->table_idx = $table_idx;
42 $this->table_token = $table_token;
43 $this->garbage_timeout = 0;
45 log_wr("Briskin5 constructor end");
49 function &get_user($sess, &$idx)
51 GLOBAL $PHP_SELF, $G_false;
53 if (validate_sess($sess)) {
54 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
55 if (strcmp($sess, $this->user[$i]->sess) == 0) {
58 $ret = &$this->user[$i];
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));
67 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
74 function garbage_manager($force)
77 /* Garbage collector degli utenti in timeout */
80 if ($force || $this->garbage_timeout < $curtime) {
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')))
88 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
89 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
91 if ($user_cur->stat == 'table') {
92 log_auth($user_cur->sess," bin5 Autologout session.");
94 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
96 $tmp_sess = $user_cur->sess;
98 step_unproxy($tmp_sess);
100 $user_cur->the_end = FALSE;
104 $this->table_wakeup(&$user_cur);
108 log_rd2($user_cur->sess." GARBAGE UPDATED!");
110 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
123 function &load_data($table_idx, $table_token = "")
125 GLOBAL $G_false, $sess;
128 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
129 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
135 // if (shmchk_exists($tok) == FALSE)
136 if (locshm_exists($tok) == FALSE)
139 if (($shm = shm_attach($tok, BRISKIN5_SHM_DIMS)) == FALSE)
142 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
145 if ($table_token != "" && $bri->table_token != $table_token) {
146 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
161 log_wr("briskin5 load_data failed");
167 function save_data(&$bri)
175 log_main("SAVE BRISKIN5 DATA");
178 if (!isset($bri->tok))
184 if (($shm = shm_attach($bri->tok, BRISKIN5_SHM_DIMS)) == FALSE)
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));
193 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
203 function destroy_data($table_idx)
211 log_main("DESTROY BRISKIN5 DATA");
217 log_main("DESTROY2 BRISKIN5 DATA");
219 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
222 if (($shm = shm_attach($tok, BRISKIN5_SHM_DIMS)) === FALSE)
225 if (shm_remove($shm) === FALSE) {
226 log_only("REMOVE FALLITA ".strlen(serialize($bri)));
227 log_only(serialize($bri));
232 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
234 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
244 function lock_data($table_idx)
248 log_wr("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
249 // echo "LOCK: ".FTOK_PATH."/main";
251 // FIXME monitor this step
252 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
256 // FIXME monitor this step
257 if (($res = @sem_get($tok)) == FALSE) {
258 echo "SEM_GET FAILED";
261 if (sem_acquire($res)) {
262 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
266 log_lock("LOCK table ".$table_idx.":FAILED");
271 function unlock_data($res)
275 log_lock("UNLOCK table [res: ".$res."]");
277 return (sem_release($res));
281 function chatt_send(&$user, $mesg)
283 if ($user->stat == 'table') {
284 $table = &$this->table[$user->table];
287 $user_mesg = substr($mesg,6);
291 $dt = date("H:i ", $timecur);
292 if (strncmp($user_mesg, "/nick ", 6) == 0) {
293 log_main($user->sess." chatt_send BEGIN");
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));
302 $user_mesg = "COMMAND ".$user_mesg;
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 == '')
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à in uso.");', $dt.NICKSERV, xcape($name_new));
319 if ($i == BRISKIN5_MAX_PLAYERS) {
320 $user->name = $name_new;
322 log_main($user->sess." chatt_send start set");
325 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
326 log_main($user->sess." chatt_send set loop");
328 $user_cur = &$this->user[$i];
329 if ($user_cur->sess == '')
331 if ($user_cur->stat == 'room') {
332 if ($user->stat == 'room' && $user->subst == 'standup') {
333 $this->standup_update(&$user);
335 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
336 $user->stat == 'table') {
337 log_main($user->sess." chatt_send pre table update");
339 $this->table_update(&$user);
341 log_main($user->sess." chatt_send post table update");
344 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
345 $table = &$this->table[$user->table];
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();
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')
370 $user_cur = &$this->user[$table->player[$i]];
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();
378 log_legal($timecur, $user->sess, $user->name,
379 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
383 function table_wakeup(&$user)
385 $table = &$this->table[0];
387 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
391 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
393 for ($i = 0 ; $i < $table->player_n ; $i++) {
394 $user_cur = &$this->user[$i];
395 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
397 if ($user_cur == $user)
398 $user_cur->subst = "shutdowner";
400 $user_cur->subst = "shutdowned";
401 $user_cur->laccwr = $curtime;
403 $ret = "gst.st = ".($user_cur->step+1)."; ";
404 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
406 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
407 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
408 $user_cur->step_inc();
411 $this->the_end = TRUE;
415 * If all players are freezed the room garbage_manager clean up table and users.
417 function is_abandoned()
422 $table = &$this->table[0];
424 for ($i = 0 ; $i < $table->player_n ; $i++) {
425 $user_cur = &$this->user[$i];
427 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
435 } // end class Briskin5
437 function locshm_exists($tok)
441 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
442 log_main($tok." SHM NOT exists");
448 log_main($tok." SHM exists");