2 define(BRISKIN5_PLAYERS_N, 3);
3 define(BRISKIN5_MAX_PLAYERS, BRISKIN5_PLAYERS_N);
4 // define(BRISKIN5_SHM_MIN, (50000 * BRISKIN5_MAX_PLAYERS));
5 define(BRISKIN5_SHM_MIN, 10000);
6 define(BRISKIN5_SHM_MAX, (BRISKIN5_SHM_MIN + 1048576));
7 define(BRISKIN5_SHM_DLT, 65536);
15 var $comm; // commands for many people
16 var $step; // current step of the comm array
24 function Briskin5 (&$room, $table_idx, $table_token) {
25 $this->user = array();
26 $this->table = array();
28 $this->the_end = FALSE;
29 $this->shm_sz = BRISKIN5_SHM_MIN;
30 if (($this->tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
36 $table =& $room->table[$table_idx];
38 log_wr("Briskin5 constructor");
40 for ($i = 0 ; $i < $table->player_n ; $i++) {
41 $user[$table->player[$i]]->table_token = $table_token;
42 $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
44 $this->table[0] =& Table::spawn(&$table);
45 $this->table_idx = $table_idx;
46 $this->table_token = $table_token;
47 $this->garbage_timeout = 0;
49 log_wr("Briskin5 constructor end");
53 function &get_user($sess, &$idx)
55 GLOBAL $PHP_SELF, $G_false;
57 if (validate_sess($sess)) {
58 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
59 if (strcmp($sess, $this->user[$i]->sess) == 0) {
62 $ret = &$this->user[$i];
66 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
67 // for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++)
68 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
71 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
78 function garbage_manager($force)
81 /* Garbage collector degli utenti in timeout */
84 if ($force || $this->garbage_timeout < $curtime) {
86 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
87 $user_cur = &$this->user[$i];
88 if ($user_cur->sess == "" ||
89 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
92 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
93 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
95 if ($user_cur->stat == 'table') {
96 log_auth($user_cur->sess," bin5 Autologout session.");
98 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
100 $tmp_sess = $user_cur->sess;
101 $user_cur->sess = "";
102 step_unproxy($tmp_sess);
103 $user_cur->name = "";
104 $user_cur->the_end = FALSE;
108 $this->table_wakeup(&$user_cur);
112 log_rd2($user_cur->sess." GARBAGE UPDATED!");
114 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
127 function &load_data($table_idx, $table_token = "")
129 GLOBAL $G_false, $sess;
133 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
136 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
137 log_main("ftok failed");
142 if (($shm_sz = sharedmem_sz($tok)) == -1) {
143 log_main("shmop_open failed");
147 if (($shm = shm_attach($tok, $shm_sz)) == FALSE)
150 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
153 if ($table_token != "" && $bri->table_token != $table_token) {
154 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
169 log_wr("briskin5 load_data failed");
178 function save_data(&$bri)
185 log_main("SAVE BRISKIN5 DATA");
187 if (!isset($bri->tok))
190 while ($bri->shm_sz < BRISKIN5_SHM_MAX) {
191 if (($shm = shm_attach($bri->tok, $bri->shm_sz)) == FALSE)
194 // log_only("PUT_VAR DI ".strlen(serialize($room)));
195 if (@shm_put_var($shm, $bri->tok, $bri) != FALSE) {
199 if (shm_remove($shm) === FALSE) {
200 log_only("REMOVE FALLITA");
204 $bri->shm_sz += BRISKIN5_SHM_DLT;
215 function save_data_orig(&$bri)
223 log_main("SAVE BRISKIN5 DATA");
226 if (!isset($bri->tok))
232 if (($shm = shm_attach($bri->tok, BRISKIN5_SHM_DIMS)) == FALSE)
235 // log_only("PUT_VAR DI ".strlen(serialize($bri)));
236 if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
237 log_only("PUT_VAR FALLITA ".strlen(serialize($bri)));
238 log_only(serialize($bri));
241 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
251 function destroy_data($table_idx)
257 log_main("DESTROY BRISKIN5 DATA");
260 log_main("DESTROY2 BRISKIN5 DATA");
262 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
265 if (($shm = @shmop_open($tok, 'a', 0, 0)) == FALSE)
268 if (shmop_delete($shm) == 0) {
269 log_only("REMOVE FALLITA ");
274 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
276 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
286 function lock_data($table_idx)
290 log_wr("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
291 // echo "LOCK: ".FTOK_PATH."/main";
293 // FIXME monitor this step
294 if (($tok = @ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
298 // FIXME monitor this step
299 if (($res = @sem_get($tok)) == FALSE) {
300 echo "SEM_GET FAILED";
303 if (sem_acquire($res)) {
304 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
308 log_lock("LOCK table ".$table_idx.":FAILED");
313 function unlock_data($res)
317 log_lock("UNLOCK table [res: ".$res."]");
319 return (sem_release($res));
323 function chatt_send(&$user, $mesg)
325 if ($user->stat == 'table') {
326 $table = &$this->table[$user->table];
329 $user_mesg = substr($mesg,6);
333 $dt = date("H:i ", $timecur);
334 if (strncmp($user_mesg, "/nick ", 6) == 0) {
335 log_main($user->sess." chatt_send BEGIN");
337 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
338 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
339 $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));
344 $user_mesg = "COMMAND ".$user_mesg;
347 // update local graph
348 // update remote graphs
349 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
350 $user_cur = &$this->user[$i];
351 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
352 if ($user_cur->sess == '')
354 if ($user_cur->name == $name_new) {
355 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
356 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
361 if ($i == BRISKIN5_MAX_PLAYERS) {
362 $user->name = $name_new;
364 log_main($user->sess." chatt_send start set");
367 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
368 log_main($user->sess." chatt_send set loop");
370 $user_cur = &$this->user[$i];
371 if ($user_cur->sess == '')
373 if ($user_cur->stat == 'room') {
374 if ($user->stat == 'room' && $user->subst == 'standup') {
375 $this->standup_update(&$user);
377 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
378 $user->stat == 'table') {
379 log_main($user->sess." chatt_send pre table update");
381 $this->table_update(&$user);
383 log_main($user->sess." chatt_send post table update");
386 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
387 $table = &$this->table[$user->table];
389 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
390 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
391 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
392 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
393 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
394 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
395 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
396 if ($user_cur == $user)
397 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
398 xcape($user->name,ENT_COMPAT,"UTF-8"));
399 $user_cur->step_inc();
405 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
406 if ($user->stat == 'room') {
407 $user_cur = &$this->user[$i];
408 if ($user_cur->sess == '' || $user_cur->stat != 'room')
412 $user_cur = &$this->user[$table->player[$i]];
415 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
416 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
417 $dt.xcape($user->name), xcape($user_mesg));
418 $user_cur->step_inc();
420 log_legal($timecur, $user->sess, $user->name,
421 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
425 function table_wakeup(&$user)
427 $table = &$this->table[0];
429 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
433 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
435 for ($i = 0 ; $i < $table->player_n ; $i++) {
436 $user_cur = &$this->user[$i];
437 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
439 if ($user_cur == $user)
440 $user_cur->subst = "shutdowner";
442 $user_cur->subst = "shutdowned";
443 $user_cur->laccwr = $curtime;
445 $ret = "gst.st = ".($user_cur->step+1)."; ";
446 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
448 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
449 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
450 $user_cur->step_inc();
453 $this->the_end = TRUE;
457 * If all players are freezed the room garbage_manager clean up table and users.
459 function is_abandoned()
464 $table = &$this->table[0];
466 for ($i = 0 ; $i < $table->player_n ; $i++) {
467 $user_cur = &$this->user[$i];
469 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
477 } // end class Briskin5
479 function locshm_exists($tok)
483 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
484 log_main($tok." SHM NOT exists");
490 log_main($tok." SHM exists");