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();
18 if (($this->tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
24 $table =& $room->table[$table_idx];
26 log_wr("xxx", "Briskin5 constructor");
28 for ($i = 0 ; $i < $table->player_n ; $i++)
29 $this->user[$i] =& User::spawn(&$user[$table->player[$i]], 0, $i);
31 $this->table[0] =& Table::spawn(&$table);
32 $this->table_idx = $table_idx;
33 $this->garbage_timeout = 0;
35 log_wr("xxx", "Briskin5 constructor end");
39 function &get_user($sess, &$idx)
41 GLOBAL $PHP_SELF, $G_false;
43 if (validate_sess($sess)) {
44 for ($i = 0 ; $i < MAX_BRISKIN5_PLAYERS ; $i++) {
45 if (strcmp($sess, $this->user[$i]->sess) == 0) {
48 $ret = &$this->user[$i];
52 log_main($sess, sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
53 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
54 // log_main($sess, sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
57 log_main($sess, sprintf("get_user: Wrong strlen [%s]",$sess));
64 function garbage_manager($force)
67 /* Garbage collector degli utenti in timeout */
69 if ($force || $this->garbage_timeout < $curtime) {
71 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
72 $user_cur = &$this->user[$i];
73 if ($user_cur->sess == "")
76 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) { // Auto logout dell'utente
77 log_rd2($user_cur->sess, "AUTO LOGOUT.");
79 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
80 log_auth($user_cur->sess, "Autologout session.");
82 $tmp_sess = $user_cur->sess;
84 step_unproxy($tmp_sess);
86 $user_cur->the_end = FALSE;
88 log_rd2($user_cur->sess, "AUTO LOGOUT.");
89 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
90 $this->room_wakeup(&$user_cur);
91 else if ($user_cur->subst == 'standup')
92 $this->room_outstandup(&$user_cur);
94 log_rd2($sess, "LOGOUT FROM WHAT ???");
98 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
99 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
100 $this->room_wakeup(&$user_cur);
101 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
102 $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);
103 $user_cur->step_inc();
107 log_rd2($user_cur->sess, "GARBAGE UPDATED!");
109 $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
122 function &load_data($table_idx)
124 GLOBAL $G_false, $sess;
127 log_wr($sess, "TABLE_IDX ".FTOK_PATH."/table".$table_idx);
128 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
134 if (($shm = shm_attach($tok, SHM_DIMS)) == FALSE)
137 if (($bri = @shm_get_var($shm, $tok)) == FALSE)
155 function save_data(&$bri)
165 if (!isset($bri->tok))
171 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
174 // log_only($sess, "PUT_VAR DI ".strlen(serialize($bri)));
175 if (shm_put_var($shm, $bri->tok, $bri) == FALSE) {
176 log_only($sess, "PUT_VAR FALLITA ".strlen(serialize($bri)));
177 log_only($sess, serialize($bri));
180 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
190 function destroy_data(&$bri)
200 if (!isset($bri->tok))
206 if (($shm = shm_attach($bri->tok, SHM_DIMS)) == FALSE)
209 if (shm_remove($shm) == FALSE) {
210 log_only($sess, "REMOVE FALLITA ".strlen(serialize($bri)));
211 log_only($sess, serialize($bri));
214 // log_main("XXX", "QUI CI ARRIVA [".$bri->user[0]->name."]");
224 function lock_data($table_idx)
228 log_wr($sess, "LOCK_DATA ".FTOK_PATH."/table".$table_idx);
229 // echo "LOCK: ".FTOK_PATH."/main";
231 if (($tok = ftok(FTOK_PATH."/table".$table_idx, "B")) == -1) {
235 // echo "FTOK ".$tok."<br>";
236 if (($res = sem_get($tok)) == FALSE) {
237 echo "SEM_GET FAILED";
240 if (sem_acquire($res)) {
241 log_only($sess, "LOCK");
248 function unlock_data($res)
252 log_only($sess, "UNLOCK");
254 return (sem_release($res));
258 function chatt_send(&$user, $mesg)
260 if ($user->stat == 'table') {
261 $table = &$this->table[$user->table];
264 $user_mesg = substr($mesg,6);
268 $dt = date("H:i ", $timecur);
269 if (strncmp($user_mesg, "/nick ", 6) == 0) {
270 log_main($user->sess, "chatt_send BEGIN");
272 if (($name_new = validate_name(substr($user_mesg, 6))) == FALSE) {
273 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
274 $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));
279 $user_mesg = "COMMAND ".$user_mesg;
282 // update local graph
283 // update remote graphs
284 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
285 $user_cur = &$this->user[$i];
286 // if ($user_cur->sess == '' || $user_cur->stat != 'room')
287 if ($user_cur->sess == '')
289 if ($user_cur->name == $name_new) {
290 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
291 $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s","Nickname <b>%s</b> già in uso.");', $dt.NICKSERV, xcape($name_new));
296 if ($i == MAX_PLAYERS) {
297 $user->name = $name_new;
299 log_main($user->sess, "chatt_send start set");
302 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
303 log_main($user->sess, "chatt_send set loop");
305 $user_cur = &$this->user[$i];
306 if ($user_cur->sess == '')
308 if ($user_cur->stat == 'room') {
309 if ($user->stat == 'room' && $user->subst == 'standup') {
310 $this->standup_update(&$user);
312 else if ($user->stat == 'room' && $user->subst == 'sitdown' ||
313 $user->stat == 'table') {
314 log_main($user->sess, "chatt_send pre table update");
316 $this->table_update(&$user);
318 log_main($user->sess, "chatt_send post table update");
321 else if ($user_cur->stat == 'table' && $user_cur->table == $user->table) {
322 $table = &$this->table[$user->table];
324 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
325 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('set_names(" %s", " %s", " %s", " %s", " %s"); ',
326 xcape($this->user[$table->player[($user_cur->table_pos)%PLAYERS_N]]->name),
327 xcape($this->user[$table->player[($user_cur->table_pos+1)%PLAYERS_N]]->name),
328 xcape($this->user[$table->player[($user_cur->table_pos+2)%PLAYERS_N]]->name),
329 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+3)%PLAYERS_N]]->name)),
330 (PLAYERS_N == 3 ? "" : xcape($this->user[$table->player[($user_cur->table_pos+4)%PLAYERS_N]]->name)));
331 if ($user_cur == $user)
332 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s</b>";',
333 xcape($user->name,ENT_COMPAT,"UTF-8"));
334 $user_cur->step_inc();
340 for ($i = 0 ; $i < ($user->stat == 'room' ? MAX_PLAYERS : PLAYERS_N) ; $i++) {
341 if ($user->stat == 'room') {
342 $user_cur = &$this->user[$i];
343 if ($user_cur->sess == '' || $user_cur->stat != 'room')
347 $user_cur = &$this->user[$table->player[$i]];
350 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
351 $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('chatt_sub("%s","%s");',
352 $dt.xcape($user->name), xcape($user_mesg));
353 $user_cur->step_inc();
355 log_legal($timecur, $user->sess, $user->name,
356 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$user_mesg);
360 function table_wakeup(&$user)
362 $table = &$this->table[0];
364 log_main("WAKEUP", "begin function table stat: ".$user->stat." subst: ".$user->subst);
368 log_main("WAKEUP", "from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
370 for ($i = 0 ; $i < $table->player_n ; $i++) {
371 $user_cur = &$this->user[$i];
372 log_main("PREIMPOST", "INLOOP name: ".$user_cur->name);
374 if ($user_cur == $user)
375 $user_cur->subst = "shutdowner";
377 $user_cur->subst = "shutdowned";
378 $user_cur->laccwr = $curtime;
380 $ret = "gst.st = ".($user_cur->step+1)."; ";
381 $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; document.location.assign("../index.php");|';
383 log_wr($user_cur->sess, "BRISKIN5_WAKEUP: ".$ret);
384 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
385 $user_cur->step_inc();
390 } // end of class Briskin5