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