2 define(MAX_BRISKIN5_PLAYERS, 3);
10 var $comm; // commands for many people
11 var $step; // current step of the comm array
18 function Briskin5 (&$room, $table_idx, $table_token) {
19 $this->user = array();
20 $this->table = array();
22 $this->the_end = FALSE;
24 if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
30 $table =& $room->table[$table_idx];
32 log_wr("Briskin5 constructor");
34 for ($i = 0 ; $i < $table->player_n ; $i++) {
35 $user[$table->player[$i]]->table_token = $table_token;
36 $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
38 $this->table[0] =& Table::spawn(&$table);
39 $this->table_idx = $table_idx;
40 $this->table_token = $table_token;
41 $this->garbage_timeout = 0;
43 log_wr("Briskin5 constructor end");
47 function &get_user($sess, &$idx)
49 GLOBAL $PHP_SELF, $G_false;
51 if (validate_sess($sess)) {
52 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
53 if (strcmp($sess, $this->user[$i]->sess) == 0) {
56 $ret = &$this->user[$i];
60 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
61 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
62 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
65 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
72 function garbage_manager($force)
75 /* Garbage collector degli utenti in timeout */
78 if ($force || $this->garbage_timeout < $curtime) {
80 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
81 $user_cur = &$this->user[$i];
82 if ($user_cur->sess == "" ||
83 ($user_cur->stat == 'table' && ($user_cur->subst == 'shutdowned' || $user_cur->subst == 'shutdowner')))
86 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
87 log_rd2($user_cur->sess." bin5 AUTO LOGOUT.");
89 if ($user_cur->stat == 'table') {
90 log_auth($user_cur->sess," bin5 Autologout session.");
92 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
94 $tmp_sess = $user_cur->sess;
96 step_unproxy($tmp_sess);
98 $user_cur->the_end = FALSE;
102 $this->table_wakeup(&$user_cur);
106 log_rd2($user_cur->sess." GARBAGE UPDATED!");
108 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
121 function &load_data($table_idx, $table_token = "")
123 GLOBAL $G_false, $sess;
126 log_wr("TABLE_IDX ".FTOK_PATH."/table".$table_idx);
127 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
133 // if (shmchk_exists($tok) == FALSE)
134 if (locshm_exists($tok) == FALSE)
137 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
140 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
143 if ($table_token != "" && $bri->table_token != $table_token) {
144 log_wr("bri->table_token: ".$bri->table_token."table_token: ".$table_token);
159 log_wr("briskin5 load_data failed");
165 function save_data(&$bri)
173 log_main("SAVE BRISKIN5 DATA");
176 if (!isset($bri->tok))
182 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
185 // log_only("PUT_VAR DI ".strlen(serialize($bri)));
186 if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
187 log_only("PUT_VAR FALLITA ".strlen(serialize($bri)));
188 log_only(serialize($bri));
191 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
201 function destroy_data($table_idx)
209 log_main("DESTROY BRISKIN5 DATA");
215 log_main("DESTROY2 BRISKIN5 DATA");
217 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
220 if (($shm = shm_attach($tok, SHM_DIMS)) === FALSE)
223 if (shm_remove($shm) === FALSE) {
224 log_only("REMOVE FALLITA ".strlen(serialize($bri)));
225 log_only(serialize($bri));
230 log_main("DESTROY2 BRISKIN5 DATA SUCCESS");
232 // log_main("QUI CI ARRIVA [".$bri->user[0]->name."]");
242 function lock_data($table_idx)
246 log_wr("LOCK_DATA ".FTOK_PATH."/table".$table_idx);
247 // echo "LOCK: ".FTOK_PATH."/main";
249 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
253 // echo "FTOK ".$tok."<br>";
254 if (($res = sem_get($tok)) == FALSE) {
255 echo "SEM_GET FAILED";
258 if (sem_acquire($res)) {
259 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
263 log_lock("LOCK table ".$table_idx.":FAILED");
268 function unlock_data($res)
272 log_lock("UNLOCK table [res: ".$res."]");
274 return (sem_release($res));
278 function chatt_send(&$user, $mesg)
280 if ($user->stat == 'table') {
281 $table = &$this->table[$user->table];
284 $user_mesg = substr($mesg,6);
288 $dt = date("H:i ", $timecur);
289 if (strncmp($user_mesg, "/nick ", 6) == 0) {
290 log_main($user->sess." chatt_send BEGIN");
292 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
293 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
294 $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));
299 $user_mesg = "COMMAND ".$user_mesg;
302 // update local graph
303 // update remote graphs
304 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
305 $user_cur = &$this->user[$i];
306 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
307 if ($user_cur->sess == '')
309 if ($user_cur->name == $name_new) {
310 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
311 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
316 if ($i == MAX_PLAYERS) {
317 $user->name = $name_new;
319 log_main($user->sess." chatt_send start set");
322 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
323 log_main($user->sess." chatt_send set loop");
325 $user_cur = &$this->user[$i];
326 if ($user_cur->sess == '')
328 if ($user_cur->stat == 'room') {
329 if ($user->stat == 'room' && $user->subst == 'standup') {
330 $this->standup_update(&$user);
332 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
333 $user->stat == 'table') {
334 log_main($user->sess." chatt_send pre table update");
336 $this->table_update(&$user);
338 log_main($user->sess." chatt_send post table update");
341 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
342 $table = &$this->table[$user->table];
344 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
345 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
346 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
347 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
348 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
349 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
350 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
351 if ($user_cur == $user)
352 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
353 xcape($user->name,ENT_COMPAT,"UTF-8"));
354 $user_cur->step_inc();
360 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
361 if ($user->stat == 'room') {
362 $user_cur = &$this->user[$i];
363 if ($user_cur->sess == '' || $user_cur->stat != 'room')
367 $user_cur = &$this->user[$table->player[$i]];
370 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
371 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
372 $dt.xcape($user->name), xcape($user_mesg));
373 $user_cur->step_inc();
375 log_legal($timecur, $user->sess, $user->name,
376 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
380 function table_wakeup(&$user)
382 $table = &$this->table[0];
384 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
388 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
390 for ($i = 0 ; $i < $table->player_n ; $i++) {
391 $user_cur = &$this->user[$i];
392 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
394 if ($user_cur == $user)
395 $user_cur->subst = "shutdowner";
397 $user_cur->subst = "shutdowned";
398 $user_cur->laccwr = $curtime;
400 $ret = "gst.st = ".($user_cur->step+1)."; ";
401 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
403 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
404 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
405 $user_cur->step_inc();
408 $this->the_end = TRUE;
412 * If all players are freezed the room garbage_manager clean up table and users.
414 function is_abandoned()
419 $table = &$this->table[0];
421 for ($i = 0 ; $i < $table->player_n ; $i++) {
422 $user_cur = &$this->user[$i];
424 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
432 } // end of class Briskin5
434 function locshm_exists($tok)
438 if (($id = shmop_open($tok,"a", 0, 0)) == FALSE) {
439 log_main($tok." SHM NOT exists");
445 log_main($tok." SHM exists");