+ $thiz->table[$i] = Table::create($i);
+ }
+ $thiz->garbage_timeout = 0;
+ $thiz->shm_sz = SHM_DIMS_MIN;
+
+ $thiz->delay_mgr = new Delay_Manager(1.5);
+
+ static::$sess_cur = FALSE;
+
+ $thiz->reload(TRUE, $ban_list, $black_list, $cloud_smasher);
+
+ return ($thiz);
+ }
+
+ function reload($is_first, $ban_list, $black_list, $cloud_smasher)
+ {
+ fprintf(STDERR, "RELOAD STUFF (%d)(%d)(%d)\n",
+ count($ban_list), count($black_list), count($cloud_smasher));
+
+ if (defined('CURL_DE_SAC_VERS')) {
+ if (brisk_cds_reload($this) == FALSE) {
+ exit(12);
+ }
+ }
+ $this->ban_list->update($ban_list);
+ $this->black_list->update($black_list);
+ $this->cloud_smasher->update($cloud_smasher);
+
+ if (!$is_first) {
+ $this->banned_kickoff();
+ $this->garbage_manager(TRUE);
+ }
+ }
+
+ function banned_kickoff()
+ {
+ $is_ban = FALSE;
+
+ for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+ $table_cur = $this->table[$table_idx];
+ // if the table is complete and exists we check users IP
+
+ if ($table_cur->player_n == PLAYERS_N) {
+ if (isset($this->match[$table_idx]) &&
+ $table_cur->table_token == $bin5->table_token) {
+ log_main("PLAYERS == N TABLE ".$table_idx);
+
+ $bin5 = $this->match[$table_idx];
+
+ $is_ban |= $bin5->banned_kickoff();
+ }
+ }
+ }
+
+ for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+ $user_cur = $this->user[$i];
+
+ if ($user_cur->is_active() == FALSE)
+ continue;
+
+ // check if the IP is blacklisted
+ if ($this->black_check($user_cur->ip) ||
+ $this->cloud_check($user_cur->ip)) {
+ $user_cur->lacc = 0;
+ $is_ban = TRUE;
+ continue;
+ }
+
+ // if authorized not check if banlisted
+ if ($user_cur->is_auth()) {
+ continue;
+ }
+
+ if ($this->ban_check($user_cur->ip)) {
+ $user_cur->lacc = 0;
+ $is_ban = TRUE;
+ }
+ }
+
+ return $is_ban;
+ }
+
+ function ban_check($ip_str)
+ {
+ return ($this->ban_list->check($ip_str));
+ }
+
+ function black_check($ip_str)
+ {
+ return ($this->black_list->check($ip_str));
+ }
+
+ function cloud_check($ip_str)
+ {
+ return ($this->cloud_smasher->check($ip_str));
+ }
+
+ function users_cleanup()
+ {
+ $curtime = time();
+ for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+ $user_cur = $this->user[$i];
+
+ if ($user_cur->the_end
+ && (($user_cur->rd_toflush == FALSE
+ && $user_cur->rd_step == $user_cur->step)
+ || $user_cur->rd_endtime_is_expired($curtime))
+ ) {
+ $user_cur->reset(); // users_cleanup, OK
+ }
+ }
+ }
+
+ function garbage_manager($force)
+ {
+ GLOBAL $G_lang, $mlang_brisk, $G_base;
+
+ $ismod = FALSE;
+
+ log_rd2("garbage_manager START");
+
+ /* Garbage collector degli utenti in timeout */
+ $curtime = microtime(TRUE);
+
+ $delta = $this->delay_mgr->delta_get($curtime);
+
+ if (!$force && !($this->garbage_timeout < $curtime)) {
+ $this->delay_mgr->lastcheck_set($curtime);
+ return ($ismod);
+ }
+
+ // Before all align times with table timeout
+ for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+ $table_cur = $this->table[$table_idx];
+ // if the table is complete and exists its shared mem we get the info about users lacc
+
+ if ($table_cur->player_n == PLAYERS_N) {
+ log_main("PLAYERS == N TABLE ".$table_idx);
+
+
+ $no_recovery = FALSE;
+ if (isset($this->match[$table_idx])) {
+ $bin5 = $this->match[$table_idx];
+
+ if ($table_cur->table_token != $bin5->table_token) {
+ log_main("ERROR: not matching table_token. Brisk: ".$table_cur->table_token." Table: ".$bin5->table_token);
+ log_main("ERROR: not matching table_start. Brisk: ".$table_cur->table_start." Table: ".$bin5->table_start);
+ $no_recovery = TRUE;
+ $bin5 = FALSE;
+ }
+
+ if ($bin5 != FALSE) {
+ //
+ // SPAWN: JOIN
+ //
+ log_main("garbage_manager: bri loaded successfully.");
+ $bin5->garbage_manager(TRUE);
+
+ $bin5_table = $bin5->table[0];
+
+ // is the end of the table
+
+ if ($bin5->the_end == TRUE) {
+ /*
+ * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
+ */
+ log_main("garbage_manager: INSIDE THE END.");
+
+ $plist = "$table_cur->table_token|$table_cur->idx|$table_cur->player_n";
+ for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
+ $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
+ }
+
+ for ($i = 0 ; $i < $bin5_table->player_n ; $i++) {
+ // stat must be "table" by definition
+ $user_cur = $this->user[$table_cur->player[$i]];
+ $bin5_user = $bin5->user[$i];
+
+ $user_cur->subst = $bin5_user->subst;
+ $user_cur->rd_step = $bin5_user->rd_step;
+ $user_cur->step = $bin5_user->step;
+ $user_cur->lacc = $bin5_user->lacc;
+ $user_cur->laccwr = $bin5_user->lacc;
+ $user_cur->bantime = $bin5_user->bantime;
+ $user_cur->the_end = $bin5_user->the_end;
+ if ($user_cur->the_end) {
+ $this->ghost_sess->push($curtime, $user_cur->sess, GHOST_SESS_REAS_TTOT);
+ }
+ }
+
+ log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME", $plist);
+
+ $this->room_join_wakeup($user_cur, FALSE, 0);
+ $table_cur->table_token = "";
+ $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
+
+ $this->match_del($table_idx);
+ }
+ else {
+ log_main("gm:: save_data");
+
+ for ($i = 0 ; $i < $bin5_table->player_n ; $i++) {
+ $this->user[$table_cur->player[$i]]->lacc = $bin5->user[$i]->lacc;
+ }
+ }
+ } // if ($bin5 == FALSE
+ else if ($no_recovery == FALSE) {
+ log_crit("ERROR: table ".$table_idx." unrecoverable join");
+
+ for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
+ $user_cur = $this->user[$table_cur->player[$i]];
+ $user_cur->subst = "shutdowner";
+ $user_cur->step_inc();
+
+ $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
+ $ret .= "gst.st = ".($user_cur->step+1)."; ";
+ // MLANG <br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>
+ $prestr = sprintf($mlang_brisk['tabincon'][$G_lang], $user_cur->table);
+ $ret .= show_notify($prestr, 2000, $mlang_brisk['btn_close'][$G_lang], 400, 110);
+ $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+ $user_cur->step_inc();
+ }
+
+ $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
+ for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
+ $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
+ }
+ log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
+
+ $this->room_join_wakeup($user_cur, TRUE, -2);
+ $table_cur->table_token = "";
+ }
+ }
+ } // if ($table_cur->player_n == PLAYERS_N) {
+ } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+
+ log_rd2("out new loop.");
+
+ for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+ $user_cur = $this->user[$i];
+
+ log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
+
+ if ($user_cur->is_active() == FALSE)
+ continue;
+
+ if ($user_cur->lacc + (($user_cur->ping_req ? 1.5 : 1.0) * EXPIRE_TIME_RD) < ($curtime - $delta)) {
+ // Auto logout dell'utente
+ log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime - delta ".($curtime - $delta));
+
+ $this->ghost_sess->push($curtime, $user_cur->sess, GHOST_SESS_REAS_TOUT);
+ $user_cur->the_end = TRUE;
+
+ log_rd2("AUTO LOGOUT.");
+ if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
+ log_auth($user_cur->sess, "Autologout session.");
+
+ if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
+ $this->room_wakeup($user_cur);
+ else if ($user_cur->subst == 'standup')
+ $this->room_outstandup($user_cur);
+ else
+ log_rd2("LOGOUT FROM WHAT ???");
+ }
+ }
+
+ if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < ($curtime - $delta)) { // lo rimettiamo in piedi
+ if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
+ $this->room_wakeup($user_cur);
+ $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
+ /* MLANG: <br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", "torna ai tavoli" */
+ $user_cur->comm[$user_cur->step % COMM_N] .= show_notify($mlang_brisk['tabtout_a'][$G_lang].(EXPIRE_TIME_SMAMMA/60.0).$mlang_brisk['tabtout_b'][$G_lang], 0, $mlang_brisk['btn_backstand'][$G_lang], 400, 100);
+ $user_cur->step_inc();
+ }
+ }
+ }
+ log_rd2("GARBAGE UPDATED!");
+
+ $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
+ $ismod = TRUE;
+
+ $this->ghost_sess->garbage_manager($curtime);
+
+ $this->delay_mgr->lastcheck_set($curtime);
+ return ($ismod);
+ }
+
+ function show_room($user_step, $user)
+ {
+ GLOBAL $G_lang, $mlang_brisk;
+ log_main("show_room: username: ".$user->name);
+
+ $ret = sprintf('gst.st = %d; ', $user_step);
+
+ $prefs = Client_prefs::from_user($user);
+ $ret .= sprintf('prefs_load(\'%s\', false, false);', json_encode($prefs));
+
+ if(false) {
+ if ($user->flags & USER_FLAG_ISOLAUTH) {
+ $ret .= 'list_set(\'isolation\', false, \''.$mlang_brisk['tit_onisol'][$G_lang].'\' ); ';
+ }
+ else {
+ $ret .= 'list_set(\'all\', false, \'\' ); ';
+ }
+ }
+
+ if ($user->subst == 'standup')
+ $ret .= "tra.show(); ";
+ else
+ $ret .= "tra.hide(); ";
+
+ $ret .= sprintf('stat = "%s";', $user->stat);
+
+ $ret .= root_welcome($user);
+ if ($user->flags & USER_FLAG_DBFAILED) {
+ $ret .= "gst.st = ".($user->step+1)."; ";
+ $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140);
+ }
+
+ $ret .= sprintf('subst = "%s";', $user->subst);
+ $ret .= $user->myname_innerHTML();
+
+ for ($i = 0 ; $i < TABLES_N ; $i++) {
+
+ $ret .= $this->table_content($user, $i);
+
+ $ret .= $this->table[$i]->act_content($user);
+ if ($this->table[$i]->wag_own != -1)
+ $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->user[$this->table[$i]->wag_own]->name, $this->table[$i]->wag_com);
+ else
+ $ret .= sprintf('tra.rem(%d); ', $i);
+ }
+ $ret .= $this->standup_content($user);
+ $ret .= "setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); ";
+
+ return ($ret);
+ }
+
+ function room_wakeup($user)
+ {
+ $table_idx = $user->table;
+ $table = $this->table[$table_idx];
+
+ log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
+
+ $curtime = time();
+
+ $from_table = ($user->stat == "table");
+ if ($from_table) {
+ log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+
+ for ($i = 0 ; $i < $table->player_n ; $i++) {
+ $user_cur = $this->user[$table->player[$i]];
+ log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
+
+ if ($user->idx_get() != $table->player[$i]) {
+ $user_cur->stat_set("room");
+ $user_cur->subst = "sitdown";
+ $user_cur->laccwr = $curtime;
+ }
+ else if ($user->is_active()) {
+ $user_cur->stat_set("room");
+ $user_cur->subst = "standup";
+ $user_cur->laccwr = $curtime;
+ $user_cur->table = -1;
+ }
+ }
+ }
+ else {
+ $user->stat_set("room");
+ $user->subst = "standup";
+ $user->laccwr = $curtime;
+ }
+
+ $remove_wagon = FALSE;
+ if($table->wag_own == $user->idx_get()) {
+ $table->wag_reset($curtime);
+ $remove_wagon = TRUE;
+ }
+
+ /* aggiorna l'array dei giocatori al tavolo. */
+ $table->user_rem($this, $user);
+
+ for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+ $user_cur = $this->user[$i];
+ if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') // is not active user or the stat isn't 'room'
+ continue;
+
+ // log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
+
+ $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
+ if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
+ $ret .= 'gst.st_loc++; xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
+ // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
+ log_main("DOCUMENT.index.php: from table");
+ }
+ else if ($user_cur->stat == "room") {
+ log_main("DOCUMENT.index.php: from table");
+
+ $ret .= $this->table_content($user_cur, $table_idx);
+ $ret .= $this->standup_content($user_cur);
+
+ // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
+ $ret .= $table->act_content($user);
+
+ if ($user->idx_get() == $i) {
+ // set the new status
+ $ret .= 'subst = "standup"; tra.show(); ';
+ // clean the action buttons in other tables
+ for ($e = 0 ; $e < TABLES_N ; $e++) {
+ if ($this->table[$e]->player_n < PLAYERS_N) {
+ $ret .= $this->table[$e]->act_content($user);
+ }
+ }
+ }
+ else {
+ $ret .= $table->act_content($user_cur);
+ }
+ }
+ log_wr("ROOM_WAKEUP: ".$ret);
+ $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+ $user_cur->step_inc();
+ }
+ }
+
+ function room_join_wakeup($user, $update_lacc = FALSE, $trans_delta)
+ {
+ $table_idx = $user->table;
+ $table = $this->table[$table_idx];
+
+ log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
+
+ $curtime = time();
+ $user_wup = array();
+ $user_wup_n = 0;
+ $user_tab = array();
+ $user_tab_n = 0;
+ log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
+
+ for ($i = 0 ; $i < $table->player_n ; $i++) {
+ $user_cur = $this->user[$table->player[$i]];
+ log_main("PREIMPOST INLOOP name: ".$user_cur->name);
+ if ($user_cur->is_empty()) {
+ continue;
+ }
+ if ($update_lacc == TRUE) {
+ $user_cur->laccwr = $curtime;
+ }
+ log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
+ if ($user_cur->subst == "shutdowned") {
+ $user_cur->stat_set("room");
+ $user_cur->subst = "sitdown";
+ }
+ else if ($user_cur->subst == "shutdowner") {
+ $user_cur->stat_set("room");
+ $user_cur->subst = "standup";
+ $user_cur->table = -1;
+ $user_wup[$user_wup_n++] = $user_cur;
+
+ $remove_wagon = FALSE;
+ if($table->wag_own == $table->player[$i]) {
+ $remove_wagon = TRUE;
+ $table->wag_reset($curtime);
+ }
+ }
+ $user_tab[$user_tab_n++] = $table->player[$i];
+ }
+
+ for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
+ $table->user_rem($this, $user_wup[$wup_idx]);
+
+ /* aggiorna l'array dei giocatori al tavolo. */
+
+ for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+ log_main("START LOOP");
+ $user_cur = $this->user[$i];
+ if ($user_cur->is_active() == FALSE || $user_cur->stat != 'room') { // is not active user or the stat isn't 'room'
+ log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
+ continue;
+ }
+
+ log_main("___");
+ log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx);
+
+ $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
+ if ($user_cur->stat == "room") {
+ log_main("DOCUMENT.index.php from table");
+
+ $ret .= $this->table_content($user_cur, $table_idx);
+ $ret .= $this->standup_content($user_cur);
+
+ $ret .= $table->act_content($user_cur);
+
+
+ for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
+ if ($user_tab[$tab_idx] == $i)
+ break;
+
+ // for users that wakeup the room will be reconstructed by index_rd.php
+ if ($tab_idx < $user_tab_n) {
+ log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
+
+// ARRAY_POP DISABLED
+// if ($trans_delta == 0)
+// while (array_pop($user_cur->comm) != NULL);
+
+ $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
+ $user_cur->comm[$user_cur->step % COMM_N] = "";
+ $user_cur->step_inc();
+ $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), $user_cur);
+ $user_cur->step_inc();
+ log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
+
+ continue;
+ }
+ log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
+
+ log_main("JOIN_WAKEUP more");
+
+ $ret .= $table->act_content($user_cur);
+
+ log_main("JOIN_WAKEUP end more");
+ }
+ log_wr("ROOM_JOIN_WAKEUP: ".$ret);
+ $user_cur->comm[$user_cur->step % COMM_N] = $ret;
+ $user_cur->step_inc();
+ }
+ }
+
+ /*
+ function info_show($user, $target, $dt)
+
+ if success return assoc. array with "ret" => 0 and other fields
+ else return an assoc. array with "ret: != 0 and "mesg" with an error description
+ */
+ function info_show($user, $target, $dt)
+ {
+ GLOBAL $G_lang, $mlang_brisk, $G_base;
+
+ $mesg = "";
+ $user_code = -1;
+ $ret = 0;
+
+ do {
+ if ($target == "") {
+ $ret = 1;
+ break;
+ }
+ if (($bdb = BriskDB::create()) == FALSE) {
+ $ret = 2;
+ break;
+ }
+ if ($target == $user->name) {
+ $ret = 3;
+ $mesg = sprintf($mlang_brisk['inf_self'][$G_lang]);
+ break;
+ }
+ if (($user_item = $bdb->getitem_bylogin($target, $user_code)) == FALSE) {
+ $ret = 4;
+ break;
+ }
+ if (($guar_item = $bdb->getitem_bycode($user_item->guar_code_get())) != FALSE) {
+ $guar_login = $guar_item->login_get();
+ }
+ else {
+ $guar_login = "";
+ }
+ $user_tos_vers = $user_item->tos_vers_get();
+
+ $partyskill = $bdb->usersnet_partyskill($user->code, $user_item->code);
+
+ $widefriend = $bdb->usersnet_widefriend($user->code, $user_item->code);
+ $widefriend['skill'] = $bdb->usersnet_wideskill($user->code, $user_item->code);
+ $narrowfriend = $bdb->usersnet_narrowfriend($user->code, $user_item->code);
+ $narrowfriend['skill'] = $bdb->usersnet_narrowskill($user->code, $user_item->code);
+
+ if (($usersnet_item = $bdb->usersnet_bycode($user->code, $user_item->code,
+ $widefriend, $narrowfriend)) == FALSE) {
+ $usersnet_item = $bdb->usersnet_default($user->code, $user_item->code,
+ $widefriend, $narrowfriend);
+ }
+
+ if (versions_cmp($user_tos_vers, "1.2") < 0) {
+ $mesg = sprintf($mlang_brisk['tos_old'][$G_lang], xcape($target));
+ }
+ else if ($guar_login == "") {
+ $mesg = sprintf($mlang_brisk['inf_nfd'][$G_lang], xcape($target));