2 define(MAX_BRISKIN5_PLAYERS, 3);
10 var $comm; // commands for many people
11 var $step; // current step of the comm array
16 function Briskin5 (&$room, $table_idx, $table_token) {
17 $this->user = array();
18 $this->table = array();
20 if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
26 $table =& $room->table[$table_idx];
28 log_wr("xxx", "Briskin5 constructor");
30 for ($i = 0 ; $i < $table->player_n ; $i++) {
31 $user[$table->player[$i]]->table_token = $table_token;
32 $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
34 $this->table[0] =& Table::spawn(&$table);
35 $this->table_idx = $table_idx;
36 $this->table_token = $table_token;
37 $this->garbage_timeout = 0;
39 log_wr("xxx", "Briskin5 constructor end");
43 function &get_user($sess, &$idx)
45 GLOBAL $PHP_SELF, $G_false;
47 if (validate_sess($sess)) {
48 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
49 if (strcmp($sess, $this->user[$i]->sess) == 0) {
52 $ret = &$this->user[$i];
56 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
57 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
58 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
61 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
68 function garbage_manager($force)
71 /* Garbage collector degli utenti in timeout */
74 if ($force || $this->garbage_timeout < $curtime) {
76 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
77 $user_cur = &$this->user[$i];
78 if ($user_cur->sess == "")
81 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
82 log_rd2($user_cur->sess, "AUTO LOGOUT.");
84 if ($user_cur->stat == 'table') {
85 log_auth($user_cur->sess, "Autologout session.");
87 /* SI DELEGA AL garbage_manager principale LA RIMOZIONE DELL'UTENTE
89 $tmp_sess = $user_cur->sess;
91 step_unproxy($tmp_sess);
93 $user_cur->the_end = FALSE;
97 log_rd2($user_cur->sess, "AUTO LOGOUT.");
99 $this->table_wakeup(&$user_cur);
103 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
105 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
118 function &load_data($table_idx, $table_token = "")
120 GLOBAL $G_false, $sess;
123 log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
124 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
130 // if (shmchk_exists($tok) == FALSE)
131 if (locshm_exists($tok) == FALSE)
134 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
137 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
140 if ($table_token != "" && $bri->table_token != $table_token) {
141 log_wr($sess, "bri->table_token: ".$bri->table_token."table_token: ".$table_token);
156 log_wr($sess, "briskin5 load_data failed");
162 function save_data(&$bri)
170 log_main("XXX", "SAVE BRISKIN5 DATA");
173 if (!isset($bri->tok))
179 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
182 // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
183 if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
184 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
185 log_only($sess, serialize($bri));
188 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
198 function destroy_data($table_idx)
206 log_main("XXX", "DESTROY BRISKIN5 DATA");
212 log_main("XXX", "DESTROY2 BRISKIN5 DATA");
214 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1)
217 if (($shm = shm_attach($tok, SHM_DIMS)) === FALSE)
220 if (shm_remove($shm) === FALSE) {
221 log_only($sess, "REMOVE FALLITA ".strlen(serialize($bri)));
222 log_only($sess, serialize($bri));
227 log_main("XXX", "DESTROY2 BRISKIN5 DATA SUCCESS");
229 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
239 function lock_data($table_idx)
243 log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
244 // echo "LOCK: ".FTOK_PATH."/main";
246 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
250 // echo "FTOK ".$tok."<br>";
251 if (($res = sem_get($tok)) == FALSE) {
252 echo "SEM_GET FAILED";
255 if (sem_acquire($res)) {
256 log_only($sess, "LOCK");
263 function unlock_data($res)
267 log_only($sess, "UNLOCK");
269 return (sem_release($res));
273 function chatt_send(&$user, $mesg)
275 if ($user->stat == 'table') {
276 $table = &$this->table[$user->table];
279 $user_mesg = substr($mesg,6);
283 $dt = date("H:i ", $timecur);
284 if (strncmp($user_mesg, "/nick ", 6) == 0) {
285 log_main($user->sess, "chatt_send BEGIN");
287 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
288 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
289 $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));
294 $user_mesg = "COMMAND ".$user_mesg;
297 // update local graph
298 // update remote graphs
299 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
300 $user_cur = &$this->user[$i];
301 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
302 if ($user_cur->sess == '')
304 if ($user_cur->name == $name_new) {
305 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
306 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
311 if ($i == MAX_PLAYERS) {
312 $user->name = $name_new;
314 log_main($user->sess, "chatt_send start set");
317 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
318 log_main($user->sess, "chatt_send set loop");
320 $user_cur = &$this->user[$i];
321 if ($user_cur->sess == '')
323 if ($user_cur->stat == 'room') {
324 if ($user->stat == 'room' && $user->subst == 'standup') {
325 $this->standup_update(&$user);
327 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
328 $user->stat == 'table') {
329 log_main($user->sess, "chatt_send pre table update");
331 $this->table_update(&$user);
333 log_main($user->sess, "chatt_send post table update");
336 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
337 $table = &$this->table[$user->table];
339 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
340 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
341 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
342 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
343 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
344 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
345 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
346 if ($user_cur == $user)
347 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
348 xcape($user->name,ENT_COMPAT,"UTF-8"));
349 $user_cur->step_inc();
355 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
356 if ($user->stat == 'room') {
357 $user_cur = &$this->user[$i];
358 if ($user_cur->sess == '' || $user_cur->stat != 'room')
362 $user_cur = &$this->user[$table->player[$i]];
365 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
366 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
367 $dt.xcape($user->name), xcape($user_mesg));
368 $user_cur->step_inc();
370 log_legal($timecur, $user->sess, $user->name,
371 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
375 function table_wakeup(&$user)
377 $table = &$this->table[0];
379 log_main("BRISKIN5_WAKEUP", "begin function table stat: ".$user->stat." subst: ".$user->subst);
383 log_main("BRISKIN5_WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
385 for ($i = 0 ; $i < $table->player_n ; $i++) {
386 $user_cur = &$this->user[$i];
387 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
389 if ($user_cur == $user)
390 $user_cur->subst = "shutdowner";
392 $user_cur->subst = "shutdowned";
393 $user_cur->laccwr = $curtime;
395 $ret = "gst.st = ".($user_cur->step+1)."; ";
396 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
398 log_wr($user_cur->sess, "BRISKIN5_WAKEUP: ".$ret);
399 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
400 $user_cur->step_inc();
405 * If all players are freezed the room garbage_manager clean up table and users.
407 function is_abandoned()
412 $table = &$this->table[0];
414 for ($i = 0 ; $i < $table->player_n ; $i++) {
415 $user_cur = &$this->user[$i];
417 if ($user_cur->lacc + (EXPIRE_TIME_RD * 2) >= $curtime) {
425 } // end of class Briskin5
427 function locshm_exists($tok)
431 if (($id = shmop_open($tok,"a", 0, 0)) == FALSE) {
432 log_main($tok, "SHM NOT exists");
438 log_main($tok, "SHM exists");