2 define(MAX_BRISKIN5_PLAYERS, 3);
8 var $comm; // commands for many people
9 var $step; // current step of the comm array
14 function Briskin5 (&$room, $table_idx) {
15 $this->user = array();
16 $this->table = array();
19 $table =& $room->table[$table_idx];
21 log_wr("xxx", "Briskin5 constructor");
23 for ($i = 0 ; $i < $table->player_n ; $i++)
24 $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
26 $this->table[0] =& Table::spawn(&$table);
27 $this->table_idx = $table_idx;
28 $this->garbage_timeout = 0;
30 log_wr("xxx", "Briskin5 constructor end");
34 function &get_user($sess, &$idx)
36 GLOBAL $PHP_SELF, $G_false;
38 if (validate_sess($sess)) {
39 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
40 if (strcmp($sess, $this->user[$i]->sess) == 0) {
43 $ret = &$this->user[$i];
47 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
48 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
49 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
52 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
59 function garbage_manager($force)
62 /* Garbage collector degli utenti in timeout */
64 if ($force || $this->garbage_timeout < $curtime) {
66 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
67 $user_cur = &$this->user[$i];
68 if ($user_cur->sess == "")
71 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
72 log_rd2($user_cur->sess, "AUTO LOGOUT.");
74 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
75 log_auth($user_cur->sess, "Autologout session.");
77 $tmp_sess = $user_cur->sess;
79 step_unproxy($tmp_sess);
81 $user_cur->the_end = FALSE;
83 log_rd2($user_cur->sess, "AUTO LOGOUT.");
84 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
85 $this->room_wakeup(&$user_cur);
86 else if ($user_cur->subst == 'standup')
87 $this->room_outstandup(&$user_cur);
89 log_rd2($sess, "LOGOUT FROM WHAT ???");
93 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
94 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
95 $this->room_wakeup(&$user_cur);
96 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
97 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 0, "torna ai tavoli", 400, 100);
98 $user_cur->step_inc();
102 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
104 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
117 function &load_data($table_idx)
119 GLOBAL $G_false, $sess;
122 log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
123 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
129 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
132 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
150 function save_data(&$bri)
160 if (!isset($bri->tok))
166 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
169 // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
170 if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
171 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
172 log_only($sess, serialize($bri));
175 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
185 function destroy_data(&$bri)
195 if (!isset($bri->tok))
201 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
204 if (shm_remove($shm) == FALSE) {
205 log_only($sess, "REMOVE FALLITA ".strlen(serialize($bri)));
206 log_only($sess, serialize($bri));
209 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
219 function lock_data($table_idx)
223 log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
224 // echo "LOCK: ".FTOK_PATH."/main";
226 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
230 // echo "FTOK ".$tok."<br>";
231 if (($res = sem_get($tok)) == FALSE) {
232 echo "SEM_GET FAILED";
235 if (sem_acquire($res)) {
236 log_only($sess, "LOCK");
243 function unlock_data($res)
247 log_only($sess, "UNLOCK");
249 return (sem_release($res));
253 function chatt_send(&$user, $mesg)
255 if ($user->stat == 'table') {
256 $table = &$this->table[$user->table];
259 $user_mesg = substr($mesg,6);
263 $dt = date("H:i ", $timecur);
264 if (strncmp($user_mesg, "/nick ", 6) == 0) {
265 log_main($user->sess, "chatt_send BEGIN");
267 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
268 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
269 $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));
274 $user_mesg = "COMMAND ".$user_mesg;
277 // update local graph
278 // update remote graphs
279 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
280 $user_cur = &$this->user[$i];
281 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
282 if ($user_cur->sess == '')
284 if ($user_cur->name == $name_new) {
285 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
286 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
291 if ($i == MAX_PLAYERS) {
292 $user->name = $name_new;
294 log_main($user->sess, "chatt_send start set");
297 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
298 log_main($user->sess, "chatt_send set loop");
300 $user_cur = &$this->user[$i];
301 if ($user_cur->sess == '')
303 if ($user_cur->stat == 'room') {
304 if ($user->stat == 'room' && $user->subst == 'standup') {
305 $this->standup_update(&$user);
307 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
308 $user->stat == 'table') {
309 log_main($user->sess, "chatt_send pre table update");
311 $this->table_update(&$user);
313 log_main($user->sess, "chatt_send post table update");
316 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
317 $table = &$this->table[$user->table];
319 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
320 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
321 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
322 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
323 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
324 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
325 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
326 if ($user_cur == $user)
327 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
328 xcape($user->name,ENT_COMPAT,"UTF-8"));
329 $user_cur->step_inc();
335 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
336 if ($user->stat == 'room') {
337 $user_cur = &$this->user[$i];
338 if ($user_cur->sess == '' || $user_cur->stat != 'room')
342 $user_cur = &$this->user[$table->player[$i]];
345 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
346 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
347 $dt.xcape($user->name), xcape($user_mesg));
348 $user_cur->step_inc();
350 log_legal($timecur, $user->sess, $user->name,
351 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
355 function table_wakeup(&$user)
357 $table = &$this->table[0];
359 log_main("WAKEUP", "begin function table stat: ".$user->stat." subst: ".$user->subst);
363 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
365 for ($i = 0 ; $i < $table->player_n ; $i++) {
366 $user_cur = &$this->user[$i];
367 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
369 if ($user_cur == $user)
370 $user_cur->subst = "shutdowner";
372 $user_cur->subst = "shutdowned";
373 $user_cur->laccwr = $curtime;
375 $ret = "gst.st = ".($user_cur->step+1)."; ";
376 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
378 log_wr($user_cur->sess, "BRISKIN5_WAKEUP: ".$ret);
379 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
380 $user_cur->step_inc();
385 } // end of class Briskin5