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 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
255 // echo "FTOK ".$tok."<br>";
256 if (($res = sem_get($tok)) == FALSE) {
257 echo "SEM_GET FAILED";
260 if (sem_acquire($res)) {
261 log_lock("LOCK table ".$table_idx."[res: ".$res."]");
265 log_lock("LOCK table ".$table_idx.":FAILED");
270 function unlock_data($res)
274 log_lock("UNLOCK table [res: ".$res."]");
276 return (sem_release($res));
280 function chatt_send(&$user, $mesg)
282 if ($user->stat == 'table') {
283 $table = &$this->table[$user->table];
286 $user_mesg = substr($mesg,6);
290 $dt = date("H:i ", $timecur);
291 if (strncmp($user_mesg, "/nick ", 6) == 0) {
292 log_main($user->sess." chatt_send BEGIN");
294 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
295 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
296 $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));
301 $user_mesg = "COMMAND ".$user_mesg;
304 // update local graph
305 // update remote graphs
306 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
307 $user_cur = &$this->user[$i];
308 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
309 if ($user_cur->sess == '')
311 if ($user_cur->name == $name_new) {
312 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
313 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
318 if ($i == BRISKIN5_MAX_PLAYERS) {
319 $user->name = $name_new;
321 log_main($user->sess." chatt_send start set");
324 for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
325 log_main($user->sess." chatt_send set loop");
327 $user_cur = &$this->user[$i];
328 if ($user_cur->sess == '')
330 if ($user_cur->stat == 'room') {
331 if ($user->stat == 'room' && $user->subst == 'standup') {
332 $this->standup_update(&$user);
334 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
335 $user->stat == 'table') {
336 log_main($user->sess." chatt_send pre table update");
338 $this->table_update(&$user);
340 log_main($user->sess." chatt_send post table update");
343 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
344 $table = &$this->table[$user->table];
346 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
347 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
348 xcape($this->user[$table->player[($user_cur->table_pos) % BRISKIN5_PLAYERS_N]]->name),
349 xcape($this->user[$table->player[($user_cur->table_pos+1) % BRISKIN5_PLAYERS_N]]->name),
350 xcape($this->user[$table->player[($user_cur->table_pos+2) % BRISKIN5_PLAYERS_N]]->name),
351 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3) % BRISKIN5_PLAYERS_N]]->name)),
352 (BRISKIN5_PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4) % BRISKIN5_PLAYERS_N]]->name)));
353 if ($user_cur == $user)
354 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
355 xcape($user->name,ENT_COMPAT,"UTF-8"));
356 $user_cur->step_inc();
362 for ($i = 0 ; $i < ($user->stat == 'room' ? BRISKIN5_MAX_PLAYERS : BRISKIN5_PLAYERS_N) ; $i++) {
363 if ($user->stat == 'room') {
364 $user_cur = &$this->user[$i];
365 if ($user_cur->sess == '' || $user_cur->stat != 'room')
369 $user_cur = &$this->user[$table->player[$i]];
372 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
373 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
374 $dt.xcape($user->name), xcape($user_mesg));
375 $user_cur->step_inc();
377 log_legal($timecur, $user->sess, $user->name,
378 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
382 function table_wakeup(&$user)
384 $table = &$this->table[0];
386 log_main("BRISKIN5_WAKEUP begin function table stat: ".$user->stat." subst: ".$user->subst);
390 log_main("BRISKIN5_WAKEUP from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
392 for ($i = 0 ; $i < $table->player_n ; $i++) {
393 $user_cur = &$this->user[$i];
394 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
396 if ($user_cur == $user)
397 $user_cur->subst = "shutdowner";
399 $user_cur->subst = "shutdowned";
400 $user_cur->laccwr = $curtime;
402 $ret = "gst.st = ".($user_cur->step+1)."; ";
403 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
405 log_wr($user_cur->sess." BRISKIN5_WAKEUP: ".$ret);
406 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
407 $user_cur->step_inc();
410 $this->the_end = TRUE;
414 * If all players are freezed the room garbage_manager clean up table and users.
416 function is_abandoned()
421 $table = &$this->table[0];
423 for ($i = 0 ; $i < $table->player_n ; $i++) {
424 $user_cur = &$this->user[$i];
426 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
434 } // end class Briskin5
436 function locshm_exists($tok)
440 if (($id = @shmop_open($tok,"a", 0, 0)) == FALSE) {
441 log_main($tok." SHM NOT exists");
447 log_main($tok." SHM exists");