| Total Complexity | 122 |
| Total Lines | 586 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Chat often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Chat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Chat { |
||
| 15 | |||
| 16 | use TSingleton; |
||
| 17 | |||
| 18 | const CHAT_DEFAULT_LINES_PER_PAGE = 20; |
||
| 19 | |||
| 20 | protected $_chat_aliases = array( |
||
| 21 | 'h' => 'help', |
||
| 22 | '?' => 'help', |
||
| 23 | 'help' => 'help', |
||
| 24 | 'w' => 'whisper', |
||
| 25 | 'whisper' => 'whisper', |
||
| 26 | 'b' => 'ban', |
||
| 27 | 'ban' => 'ban', |
||
| 28 | 'ub' => 'unban', |
||
| 29 | 'unban' => 'unban', |
||
| 30 | 'm' => 'mute', |
||
| 31 | 'mute' => 'mute', |
||
| 32 | 'um' => 'unmute', |
||
| 33 | 'unmute' => 'unmute', |
||
| 34 | 'iv' => 'invisible', |
||
| 35 | 'invisible' => 'invisible', |
||
| 36 | // * /i /ignore |
||
| 37 | // * /ck /kick |
||
| 38 | // * /ci /invite |
||
| 39 | // * /cj /join |
||
| 40 | // * /cc /create |
||
| 41 | ); |
||
| 42 | |||
| 43 | protected $_chat_commands = array( |
||
| 44 | 'invisible' => array( |
||
| 45 | 'access' => array(0, 1, 2, 3, 4), |
||
| 46 | 'options' => array( |
||
| 47 | CHAT_OPTION_SWITCH => CHAT_OPTION_SWITCH, |
||
| 48 | ), |
||
| 49 | ), |
||
| 50 | 'whisper' => array( |
||
| 51 | 'access' => array(0, 1, 2, 3, 4), |
||
| 52 | 'options' => array(), |
||
| 53 | ), |
||
| 54 | 'mute' => array( |
||
| 55 | 'access' => array(1, 2, 3, 4), |
||
| 56 | 'options' => array(), |
||
| 57 | ), |
||
| 58 | 'unmute' => array( |
||
| 59 | 'access' => array(1, 2, 3, 4), |
||
| 60 | 'options' => array(), |
||
| 61 | ), |
||
| 62 | 'ban' => array( |
||
| 63 | 'access' => array(1, 2, 3, 4), |
||
| 64 | 'options' => array(), |
||
| 65 | ), |
||
| 66 | 'unban' => array( |
||
| 67 | 'access' => array(1, 2, 3, 4), |
||
| 68 | 'options' => array(), |
||
| 69 | ), |
||
| 70 | 'help' => array( |
||
| 71 | 'access' => array(0, 1, 2, 3, 4), |
||
| 72 | 'options' => array(), |
||
| 73 | ), |
||
| 74 | ); |
||
| 75 | |||
| 76 | |||
| 77 | public static function chatModel() { |
||
| 78 | static::me()->_chatModel(); |
||
|
|
|||
| 79 | } |
||
| 80 | |||
| 81 | public static function chatAddModel() { |
||
| 82 | static::me()->_chatAddModel(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public static function chatView($template = null) { |
||
| 86 | return static::me()->_chatView($template); |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function chatMsgView($template = null) { |
||
| 90 | static::me()->_chatMsgView($template); |
||
| 91 | } |
||
| 92 | |||
| 93 | public static function chatFrameView($template = null) { |
||
| 94 | static::me()->_chatFrameView($template); |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function _chatView($template = null) { |
||
| 98 | defined('IN_AJAX') or define('IN_AJAX', true); |
||
| 99 | |||
| 100 | global $config, $lang; |
||
| 101 | |||
| 102 | $iframe = sys_get_param_id('iframe'); |
||
| 103 | |||
| 104 | // $template = $this->addModuleTemplate('chat_body', $template); |
||
| 105 | $template = SnTemplate::gettemplate('chat/chat_body', $template); |
||
| 106 | $template->assign_var('CHAT_REFRESH_RATE', $config->chat_refresh_rate); |
||
| 107 | $template->assign_var('CHAT_MODE', $iframe); |
||
| 108 | $template->assign_var('MENU', !$iframe); |
||
| 109 | $template->assign_var('NAVBAR', !$iframe); |
||
| 110 | $template->assign_var('CHAT_IFRAME', $iframe); |
||
| 111 | foreach ($lang['chat_advanced_command_interval'] as $interval => $locale) { |
||
| 112 | $template->assign_block_vars('chat_advanced_command_interval', array( |
||
| 113 | 'INTERVAL' => $interval, |
||
| 114 | 'NAME' => $locale, |
||
| 115 | )); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $template; |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function _chatMsgView($template = null) { |
||
| 122 | defined('IN_AJAX') or define('IN_AJAX', true); |
||
| 123 | |||
| 124 | global $config, $user, $lang; |
||
| 125 | |||
| 126 | |||
| 127 | $history = sys_get_param_str('history'); |
||
| 128 | if (!$history) { |
||
| 129 | $config->array_set('users', $user['id'], 'chat_last_refresh', SN_TIME_MICRO); |
||
| 130 | // $chat_player_row = $this->sn_chat_advanced_get_chat_player_record($user['id']); |
||
| 131 | doquery("UPDATE {{chat_player}} SET `chat_player_refresh_last` = " . SN_TIME_NOW . " WHERE `chat_player_player_id` = {$user['id']} LIMIT 1;"); |
||
| 132 | } |
||
| 133 | |||
| 134 | $page = 0; |
||
| 135 | $last_message = ''; |
||
| 136 | $alliance = 0; |
||
| 137 | $template_result['.']['chat'] = array(); |
||
| 138 | if (!$history && $config->getMode() != classCache::CACHER_NO_CACHE && $config->chat_timeout && SN_TIME_MICRO - $config->array_get('users', $user['id'], 'chat_last_activity') > $config->chat_timeout) { |
||
| 139 | $result['disable'] = true; |
||
| 140 | $template_result['.']['chat'][] = array( |
||
| 141 | 'TIME' => date(FMT_DATE_TIME, htmlentities(SN_TIME_NOW + SN_CLIENT_TIME_DIFF, ENT_QUOTES, 'utf-8')), |
||
| 142 | 'DISABLE' => true, |
||
| 143 | ); |
||
| 144 | } else { |
||
| 145 | $alliance = sys_get_param_str('ally') && $user['ally_id'] ? $user['ally_id'] : 0; |
||
| 146 | |||
| 147 | $page_limit = sys_get_param_id('line_per_page', self::CHAT_DEFAULT_LINES_PER_PAGE); // Chat rows Limit |
||
| 148 | |||
| 149 | $where_add = ''; |
||
| 150 | $last_message = 0; |
||
| 151 | if ($history) { |
||
| 152 | $rows = doquery("SELECT count(1) AS CNT |
||
| 153 | FROM {{chat}} |
||
| 154 | WHERE |
||
| 155 | ( |
||
| 156 | (ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR |
||
| 157 | (ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR |
||
| 158 | (ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR |
||
| 159 | (ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL) |
||
| 160 | ) |
||
| 161 | ", true); |
||
| 162 | $page_count = ceil($rows['CNT'] / $page_limit); |
||
| 163 | |||
| 164 | for ($i = 1; $i <= $page_count; $i++) { |
||
| 165 | $template_result['.']['page'][] = array( |
||
| 166 | 'NUMBER' => $i |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | $page = min($page_count, max(1, sys_get_param_int('sheet'))); |
||
| 171 | } else { |
||
| 172 | $last_message = sys_get_param_id('last_message'); |
||
| 173 | $where_add = $last_message ? "AND `messageid` > {$last_message}" : ''; |
||
| 174 | $page = 1; |
||
| 175 | } |
||
| 176 | |||
| 177 | $start_row = ($page - 1) * $page_limit;// OR `chat_message_recipient_id` = {$user['id']} |
||
| 178 | $start_row = $start_row < 0 ? 0 : $start_row; |
||
| 179 | $query = doquery( |
||
| 180 | "SELECT c.*, u.authlevel |
||
| 181 | FROM |
||
| 182 | {{chat}} AS c |
||
| 183 | LEFT JOIN {{users}} AS u ON u.id = c.chat_message_sender_id |
||
| 184 | WHERE |
||
| 185 | ( |
||
| 186 | (c.ally_id = '{$alliance}' AND `chat_message_recipient_id` IS NULL) OR |
||
| 187 | (c.ally_id = 0 AND `chat_message_recipient_id` = {$user['id']}) OR |
||
| 188 | (c.ally_id = 0 AND `chat_message_sender_id` = {$user['id']} AND `chat_message_recipient_id` IS NOT NULL) OR |
||
| 189 | (c.ally_id = 0 AND `chat_message_sender_id` IS NULL AND `chat_message_recipient_id` IS NULL) |
||
| 190 | ) |
||
| 191 | {$where_add} |
||
| 192 | ORDER BY messageid DESC |
||
| 193 | LIMIT {$start_row}, {$page_limit}"); |
||
| 194 | while ($chat_row = db_fetch($query)) { |
||
| 195 | $chat_row['user'] = player_nick_render_to_html($chat_row['user']); |
||
| 196 | $nick_stripped = htmlentities(strip_tags($chat_row['user']), ENT_QUOTES, 'utf-8'); |
||
| 197 | $template_result['.']['chat'][] = array( |
||
| 198 | 'TIME' => SN::$gc->bbCodeParser->expandBbCode(date(FMT_DATE_TIME, $chat_row['timestamp'] + SN_CLIENT_TIME_DIFF)), |
||
| 199 | 'NICK' => $chat_row['user'], |
||
| 200 | 'NICK_STRIPPED' => $nick_stripped, |
||
| 201 | 'TEXT' => SN::$gc->bbCodeParser->expandBbCode($chat_row['message'], $chat_row['authlevel']), |
||
| 202 | 'SENDER_ID' => $chat_row['chat_message_sender_id'], |
||
| 203 | 'SENDER_NAME' => $safe_name = sys_safe_output($chat_row['chat_message_sender_name']), |
||
| 204 | 'SENDER_NAME_SAFE' => $safe_name <> $chat_row['chat_message_sender_name'] || strpbrk($safe_name, ' /\\\'') ? '"' . $safe_name . '"' : $safe_name, |
||
| 205 | 'RECIPIENT_ID' => $chat_row['chat_message_recipient_id'], |
||
| 206 | 'RECIPIENT_NAME' => $safe_name_recipient = sys_safe_output($chat_row['chat_message_recipient_name']), |
||
| 207 | 'RECIPIENT_NAME_SAFE' => $safe_name_recipient <> $chat_row['chat_message_recipient_name'] || strpbrk($safe_name_recipient, ' /\\\'') ? '"' . $safe_name_recipient . '"' : $safe_name_recipient, |
||
| 208 | ); |
||
| 209 | |||
| 210 | $last_message = max($last_message, $chat_row['messageid']); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | $template_result['.']['chat'] = array_reverse($template_result['.']['chat']); |
||
| 215 | |||
| 216 | $template_result += array( |
||
| 217 | 'PAGE' => $page, |
||
| 218 | 'PAGES_TOTAL' => $page_count, |
||
| 219 | 'PAGE_NEXT' => $page < $page_count ? $page + 1 : $page, |
||
| 220 | 'PAGE_PREV' => $page > 1 ? $page - 1 : $page, |
||
| 221 | 'ALLY' => $alliance, |
||
| 222 | 'HISTORY' => $history, |
||
| 223 | 'USER_ID' => $user['id'], |
||
| 224 | ); |
||
| 225 | |||
| 226 | // $template = $this->addModuleTemplate('chat_messages', $template); |
||
| 227 | $template = SnTemplate::gettemplate('chat/chat_messages', $template); |
||
| 228 | $template->assign_recursive($template_result); |
||
| 229 | |||
| 230 | if ($history) { |
||
| 231 | $pageTitle = "{$lang['chat_history']} - {$lang[$alliance ? 'chat_ally' : 'chat_common']}"; |
||
| 232 | SnTemplate::display($template, $pageTitle); |
||
| 233 | } else { |
||
| 234 | $result['last_message'] = $last_message; |
||
| 235 | $result['html'] = SnTemplate::templateRenderToHtml($template); |
||
| 236 | |||
| 237 | // $template = $this->addModuleTemplate('chat_online', $template); |
||
| 238 | $template = SnTemplate::gettemplate('chat/chat_online', null); |
||
| 239 | // $template->assign_recursive($template_result); |
||
| 240 | $chat_online_players = 0; |
||
| 241 | $chat_online_invisibles = 0; |
||
| 242 | $chat_player_invisible = 0; |
||
| 243 | $template_result['.']['online_player'] = array(); |
||
| 244 | $ally_add = $alliance ? "AND u.ally_id = {$alliance}" : ''; |
||
| 245 | |||
| 246 | $chat_player_list = db_chat_player_list_online($config->chat_refresh_rate, $ally_add); |
||
| 247 | |||
| 248 | // TODO: Добавить ограничение по бану |
||
| 249 | while ($chat_player_row = db_fetch($chat_player_list)) { |
||
| 250 | $chat_online_players++; |
||
| 251 | if ($chat_player_row['chat_player_invisible']) { |
||
| 252 | $chat_online_invisibles++; |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!$chat_player_row['chat_player_invisible'] || $user['authlevel']) { |
||
| 256 | $safe_name = sys_safe_output($chat_player_row['username']); |
||
| 257 | $safe_name = $chat_player_row['username'] <> $safe_name || strpbrk($safe_name, ' /\\\'') !== false ? '"' . $safe_name . '"' : $safe_name; |
||
| 258 | $template_result['.']['online_player'][] = array( |
||
| 259 | 'ID' => $chat_player_row['id'], |
||
| 260 | 'NAME' => player_nick_render_to_html($chat_player_row, array( |
||
| 261 | 'color' => true, |
||
| 262 | 'icons' => true, |
||
| 263 | 'ally' => !$alliance, |
||
| 264 | 'class' => 'class="chat_nick_whisper" safe_name="' . $safe_name . '"', |
||
| 265 | )), |
||
| 266 | 'NAME_SAFE' => $safe_name, |
||
| 267 | 'MUTED' => $chat_player_row['chat_player_muted'] && $chat_player_row['chat_player_muted'] >= SN_TIME_NOW ? date(FMT_DATE_TIME, $chat_player_row['chat_player_muted']) : false, |
||
| 268 | 'MUTE_REASON' => htmlentities($chat_player_row['chat_player_mute_reason'], ENT_COMPAT, 'UTF-8'), |
||
| 269 | 'INVISIBLE' => $chat_player_row['chat_player_invisible'], |
||
| 270 | 'AUTH_LEVEL' => $chat_player_row['authlevel'], |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | if ($user['id'] == $chat_player_row['id']) { |
||
| 274 | $chat_player_invisible = $chat_player_row['chat_player_invisible']; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | $chat_commands = &$this->_chat_commands; |
||
| 279 | $template_result = array_merge($template_result, array( |
||
| 280 | 'USER_ID' => $user['id'], |
||
| 281 | 'USER_AUTHLEVEL' => $user['authlevel'], |
||
| 282 | 'USER_CAN_BAN' => in_array($user['authlevel'], $chat_commands['ban']['access']), |
||
| 283 | 'USER_CAN_MUTE' => in_array($user['authlevel'], $chat_commands['mute']['access']), |
||
| 284 | 'USER_CAN_UNMUTE' => in_array($user['authlevel'], $chat_commands['unmute']['access']), |
||
| 285 | )); |
||
| 286 | $template->assign_recursive($template_result); |
||
| 287 | $result['online'] = SnTemplate::templateRenderToHtml($template); |
||
| 288 | |||
| 289 | $result['online_players'] = $chat_online_players; |
||
| 290 | $result['online_invisibles'] = $chat_online_invisibles; |
||
| 291 | $result['chat_player_invisible'] = $chat_player_invisible; |
||
| 292 | $result['users_total'] = SN::$config->users_amount; |
||
| 293 | $result['users_online'] = SN::$config->var_online_user_count; |
||
| 294 | print(json_encode($result)); |
||
| 295 | } |
||
| 296 | die(); |
||
| 297 | } |
||
| 298 | |||
| 299 | protected function _chatFrameView($template = null) { |
||
| 300 | defined('IN_AJAX') or define('IN_AJAX', true); |
||
| 301 | |||
| 302 | // $template = $this->addModuleTemplate('chat_frame', $template); |
||
| 303 | $template = SnTemplate::gettemplate('chat/chat_frame', $template); |
||
| 304 | require_once($template->files['chat_frame']); |
||
| 305 | die(); |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | protected function _chatModel() { |
||
| 310 | global $user, $template_result, $lang; |
||
| 311 | |||
| 312 | $this->update_chat_activity(); |
||
| 313 | |||
| 314 | $mode = sys_get_param_int('mode'); |
||
| 315 | switch ($mode) { |
||
| 316 | case CHAT_MODE_ALLY: |
||
| 317 | $template_result['ALLY'] = intval($user['ally_id']); |
||
| 318 | $template_result['CHAT_MODE'] = CHAT_MODE_ALLY; |
||
| 319 | $page_title = $lang['chat_ally']; |
||
| 320 | break; |
||
| 321 | |||
| 322 | case CHAT_MODE_COMMON: |
||
| 323 | default: |
||
| 324 | $page_title = $lang['chat_common']; |
||
| 325 | $template_result['CHAT_MODE'] = CHAT_MODE_COMMON; |
||
| 326 | break; |
||
| 327 | } |
||
| 328 | |||
| 329 | $template_result['PAGE_HEADER'] = $page_title; |
||
| 330 | |||
| 331 | $template_result['.']['smiles'] = array(); |
||
| 332 | |||
| 333 | foreach (SN::$gc->design->getSmilesList() as $auth_level => $replaces) { |
||
| 334 | if ($auth_level > $user['authlevel']) { |
||
| 335 | continue; |
||
| 336 | } |
||
| 337 | |||
| 338 | foreach ($replaces as $bbcode => $filename) { |
||
| 339 | $template_result['.']['smiles'][] = array( |
||
| 340 | 'BBCODE' => htmlentities($bbcode, ENT_COMPAT, 'UTF-8'), |
||
| 341 | 'FILENAME' => $filename, |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | protected function _chatAddModel() { |
||
| 571 | } |
||
| 572 | |||
| 573 | |||
| 574 | protected function update_chat_activity($refresh = false) { |
||
| 575 | global $config, $user; |
||
| 576 | |||
| 577 | $config->array_set('users', $user['id'], 'chat_last_activity', SN_TIME_MICRO); |
||
| 578 | $config->array_set('users', $user['id'], 'chat_last_refresh', $refresh ? 0 : SN_TIME_MICRO); |
||
| 579 | |||
| 580 | $activity_row = doquery("SELECT `chat_player_id` FROM {{chat_player}} WHERE `chat_player_player_id` = {$user['id']} LIMIT 1", true); |
||
| 581 | if (!$activity_row) { |
||
| 582 | doquery("INSERT INTO {{chat_player}} SET `chat_player_player_id` = {$user['id']}"); |
||
| 583 | } else { |
||
| 584 | doquery("UPDATE {{chat_player}} SET `chat_player_activity` = '" . SN::$db->db_escape(SN_TIME_SQL) . "' WHERE `chat_player_player_id` = {$user['id']} LIMIT 1"); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | protected function sn_chat_advanced_get_chat_player_record($player_id, $fields = '*', $return_data = true) { |
||
| 600 | } |
||
| 601 | |||
| 602 | |||
| 604 |