| Conditions | 21 |
| Paths | > 20000 |
| Total Lines | 286 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | function chess_player_stats($player_uid, $player_uname, $show_option = _CHESS_SHOW_EXCEPT_SELFPLAY, $cstart = 0, $gstart = 0) |
||
| 92 | { |
||
| 93 | global $xoopsDB, $xoopsTpl; |
||
| 94 | |||
| 95 | $rating_system = chess_moduleConfig('rating_system'); |
||
| 96 | |||
| 97 | $num_provisional_games = chess_ratings_num_provisional_games(); |
||
| 98 | |||
| 99 | // set show_option to default if appropriate |
||
| 100 | |||
| 101 | if (!$show_option || ('none' == $rating_system && _CHESS_SHOW_RATED_ONLY == $show_option)) { |
||
| 102 | $show_option = _CHESS_SHOW_EXCEPT_SELFPLAY; |
||
| 103 | } |
||
| 104 | |||
| 105 | // get maximum number of items to display on a page, and constrain it to a reasonable value |
||
| 106 | |||
| 107 | $max_items_to_display = chess_moduleConfig('max_items'); |
||
| 108 | |||
| 109 | $max_items_to_display = min(max($max_items_to_display, 1), 1000); |
||
| 110 | |||
| 111 | $challenges_table = $xoopsDB->prefix('chess_challenges'); |
||
| 112 | |||
| 113 | $games_table = $xoopsDB->prefix('chess_games'); |
||
| 114 | |||
| 115 | $ratings_table = $xoopsDB->prefix('chess_ratings'); |
||
| 116 | |||
| 117 | $player = []; |
||
| 118 | |||
| 119 | $player['uid'] = $player_uid; |
||
| 120 | |||
| 121 | $player['uname'] = $player_uname; |
||
| 122 | |||
| 123 | // --------------------------------------------- |
||
| 124 | |||
| 125 | // form for selecting player and display-options |
||
| 126 | |||
| 127 | // --------------------------------------------- |
||
| 128 | |||
| 129 | // security token not needed for this form |
||
| 130 | |||
| 131 | $form = new XoopsThemeForm(_MD_CHESS_SELECT_PLAYER, 'form1', 'player_stats.php'); |
||
| 132 | |||
| 133 | $form->addElement(new XoopsFormText('', 'player_uname', 25, 50, $player_uname)); |
||
| 134 | |||
| 135 | $form->addElement(new XoopsFormButton('', 'submit_select_player', _MD_CHESS_SUBMIT_BUTTON, 'submit')); |
||
| 136 | |||
| 137 | $menu_show_option = new XoopsFormSelect('', 'show_option', $show_option, 1, false); |
||
| 138 | |||
| 139 | $menu_show_option->addOption(_CHESS_SHOW_ALL_GAMES, _MD_CHESS_SHOW_ALL_GAMES); |
||
| 140 | |||
| 141 | $menu_show_option->addOption(_CHESS_SHOW_EXCEPT_SELFPLAY, _MD_CHESS_SHOW_EXCEPT_SELFPLAY); // default |
||
| 142 | |||
| 143 | if ('none' != $rating_system) { |
||
| 144 | $menu_show_option->addOption(_CHESS_SHOW_RATED_ONLY, _MD_CHESS_SHOW_RATED_ONLY); |
||
| 145 | } |
||
| 146 | |||
| 147 | $form->addElement($menu_show_option); |
||
| 148 | |||
| 149 | $form->assign($xoopsTpl); |
||
| 150 | |||
| 151 | // user IDs that will require mapping to usernames |
||
| 152 | |||
| 153 | $userids = []; |
||
| 154 | |||
| 155 | // -------------- |
||
| 156 | |||
| 157 | // player's games |
||
| 158 | |||
| 159 | // -------------- |
||
| 160 | |||
| 161 | // Two queries are performed, one without a limit clause to count the total number of rows for the page navigator, |
||
| 162 | |||
| 163 | // and one with a limit clause to get the data for display on the current page. |
||
| 164 | |||
| 165 | // SQL_CALC_FOUND_ROWS and FOUND_ROWS(), available in MySQL 4.0.0, provide a more efficient way of doing this. |
||
| 166 | |||
| 167 | $where = "'$player_uid' IN (white_uid, black_uid)"; |
||
| 168 | |||
| 169 | if (_CHESS_SHOW_EXCEPT_SELFPLAY == $show_option) { |
||
| 170 | $where .= ' AND white_uid != black_uid'; |
||
| 171 | } elseif (_CHESS_SHOW_RATED_ONLY == $show_option) { |
||
| 172 | $where .= ' AND is_rated = "1" AND white_uid != black_uid'; |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = $xoopsDB->query("SELECT COUNT(*) FROM $games_table WHERE $where"); |
||
| 176 | |||
| 177 | [$num_items] = $xoopsDB->fetchRow($result); |
||
| 178 | |||
| 179 | $xoopsDB->freeRecordSet($result); |
||
| 180 | |||
| 181 | $result = $xoopsDB->query( |
||
| 182 | " |
||
| 183 | SELECT game_id, fen_active_color, white_uid, black_uid, pgn_result, is_rated, |
||
| 184 | UNIX_TIMESTAMP(GREATEST(create_date,start_date,last_date)) AS last_activity |
||
| 185 | FROM $games_table |
||
| 186 | WHERE $where |
||
| 187 | ORDER BY last_activity DESC |
||
| 188 | LIMIT $gstart, $max_items_to_display |
||
| 189 | " |
||
| 190 | ); |
||
| 191 | |||
| 192 | $games = []; |
||
| 193 | |||
| 194 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 195 | $games[] = [ |
||
| 196 | 'game_id' => $row['game_id'], |
||
| 197 | 'white_uid' => $row['white_uid'], |
||
| 198 | 'black_uid' => $row['black_uid'], |
||
| 199 | 'fen_active_color' => $row['fen_active_color'], |
||
| 200 | 'pgn_result' => $row['pgn_result'], |
||
| 201 | 'last_activity' => $row['last_activity'], |
||
| 202 | 'is_rated' => $row['is_rated'], |
||
| 203 | ]; |
||
| 204 | |||
| 205 | // save user IDs that will require mapping to usernames |
||
| 206 | |||
| 207 | if ($row['white_uid']) { |
||
| 208 | $userids[$row['white_uid']] = 1; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($row['black_uid']) { |
||
| 212 | $userids[$row['black_uid']] = 1; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $xoopsDB->freeRecordSet($result); |
||
| 217 | |||
| 218 | $show_option_urlparam = "&show_option=$show_option"; |
||
| 219 | |||
| 220 | $games_pagenav = new XoopsPageNav($num_items, $max_items_to_display, $gstart, 'gstart', "player_uid=$player_uid$show_option_urlparam"); |
||
| 221 | |||
| 222 | // ------------------- |
||
| 223 | |||
| 224 | // player's challenges |
||
| 225 | |||
| 226 | // ------------------- |
||
| 227 | |||
| 228 | // Two queries are performed, one without a limit clause to count the total number of rows for the page navigator, |
||
| 229 | |||
| 230 | // and one with a limit clause to get the data for display on the current page. |
||
| 231 | |||
| 232 | // SQL_CALC_FOUND_ROWS and FOUND_ROWS(), available in MySQL 4.0.0, provide a more efficient way of doing this. |
||
| 233 | |||
| 234 | $where = "'$player_uid' IN (player1_uid, player2_uid)"; |
||
| 235 | |||
| 236 | if (_CHESS_SHOW_RATED_ONLY == $show_option) { |
||
| 237 | $where .= ' AND is_rated = "1"'; |
||
| 238 | } |
||
| 239 | |||
| 240 | $result = $xoopsDB->query("SELECT COUNT(*) FROM $challenges_table WHERE $where"); |
||
| 241 | |||
| 242 | [$num_items] = $xoopsDB->fetchRow($result); |
||
| 243 | |||
| 244 | $xoopsDB->freeRecordSet($result); |
||
| 245 | |||
| 246 | $result = $xoopsDB->query( |
||
| 247 | " |
||
| 248 | SELECT challenge_id, game_type, color_option, player1_uid, player2_uid, UNIX_TIMESTAMP(create_date) AS create_date, is_rated |
||
| 249 | FROM $challenges_table |
||
| 250 | WHERE $where |
||
| 251 | ORDER BY create_date DESC |
||
| 252 | LIMIT $cstart, $max_items_to_display |
||
| 253 | " |
||
| 254 | ); |
||
| 255 | |||
| 256 | $challenges = []; |
||
| 257 | |||
| 258 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 259 | $challenges[] = [ |
||
| 260 | 'challenge_id' => $row['challenge_id'], |
||
| 261 | 'game_type' => $row['game_type'], |
||
| 262 | 'color_option' => $row['color_option'], |
||
| 263 | 'player1_uid' => $row['player1_uid'], |
||
| 264 | 'player2_uid' => $row['player2_uid'], |
||
| 265 | 'create_date' => $row['create_date'], |
||
| 266 | 'is_rated' => $row['is_rated'], |
||
| 267 | ]; |
||
| 268 | |||
| 269 | // save user IDs that will require mapping to usernames |
||
| 270 | |||
| 271 | if ($row['player1_uid']) { |
||
| 272 | $userids[$row['player1_uid']] = 1; |
||
| 273 | } |
||
| 274 | |||
| 275 | if ($row['player2_uid']) { |
||
| 276 | $userids[$row['player2_uid']] = 1; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | $xoopsDB->freeRecordSet($result); |
||
| 281 | |||
| 282 | $show_option_urlparam = "&show_option=$show_option"; |
||
| 283 | |||
| 284 | $challenges_pagenav = new XoopsPageNav($num_items, $max_items_to_display, $cstart, 'cstart', "player_uid=$player_uid$show_option_urlparam"); |
||
| 285 | |||
| 286 | // --------- |
||
| 287 | |||
| 288 | // usernames |
||
| 289 | |||
| 290 | // --------- |
||
| 291 | |||
| 292 | // get mapping of user IDs to usernames |
||
| 293 | |||
| 294 | $memberHandler = xoops_getHandler('member'); |
||
| 295 | |||
| 296 | $criteria = new \Criteria('uid', '(' . implode(',', array_keys($userids)) . ')', 'IN'); |
||
| 297 | |||
| 298 | $usernames = $memberHandler->getUserList($criteria); |
||
| 299 | |||
| 300 | // add usernames to $games |
||
| 301 | |||
| 302 | foreach ($games as $k => $game) { |
||
| 303 | $games[$k]['white_uname'] = $usernames[$game['white_uid']] ?? '?'; |
||
| 304 | |||
| 305 | $games[$k]['black_uname'] = $usernames[$game['black_uid']] ?? '?'; |
||
| 306 | } |
||
| 307 | |||
| 308 | // add usernames to $challenges |
||
| 309 | |||
| 310 | foreach ($challenges as $k => $challenge) { |
||
| 311 | $challenges[$k]['player1_uname'] = $usernames[$challenge['player1_uid']] ?? '?'; |
||
| 312 | |||
| 313 | $challenges[$k]['player2_uname'] = $usernames[$challenge['player2_uid']] ?? '?'; |
||
| 314 | } |
||
| 315 | |||
| 316 | // --------------------------------------------------- |
||
| 317 | |||
| 318 | // player's rating info (if rating feature is enabled) |
||
| 319 | |||
| 320 | // --------------------------------------------------- |
||
| 321 | |||
| 322 | if ('none' != $rating_system) { |
||
| 323 | $result = $xoopsDB->query( |
||
| 324 | " |
||
| 325 | SELECT player_uid, rating, games_won, games_lost, games_drawn, (games_won+games_lost+games_drawn) AS games_played |
||
| 326 | FROM $ratings_table |
||
| 327 | ORDER BY rating DESC, player_uid ASC |
||
| 328 | " |
||
| 329 | ); |
||
| 330 | |||
| 331 | $ranking = 0; |
||
| 332 | |||
| 333 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 334 | if ($row['games_played'] >= $num_provisional_games) { |
||
| 335 | ++$ranking; |
||
| 336 | } |
||
| 337 | |||
| 338 | if ($row['player_uid'] == $player_uid) { |
||
| 339 | break; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | $xoopsDB->freeRecordSet($result); |
||
| 344 | |||
| 345 | if ($row['player_uid'] == $player_uid) { |
||
| 346 | $player['ranking'] = $ranking; |
||
| 347 | |||
| 348 | $player['rating'] = $row['rating']; |
||
| 349 | |||
| 350 | $player['games_won'] = $row['games_won']; |
||
| 351 | |||
| 352 | $player['games_lost'] = $row['games_lost']; |
||
| 353 | |||
| 354 | $player['games_drawn'] = $row['games_drawn']; |
||
| 355 | |||
| 356 | $player['games_played'] = $row['games_played']; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | // Template variables |
||
| 361 | |||
| 362 | $player['games'] = $games; |
||
| 363 | |||
| 364 | $player['challenges'] = $challenges; |
||
| 365 | |||
| 366 | $xoopsTpl->assign('chess_player', $player); |
||
| 367 | |||
| 368 | $xoopsTpl->assign('chess_rating_system', chess_moduleConfig('rating_system')); |
||
| 369 | |||
| 370 | $xoopsTpl->assign('chess_provisional_games', $num_provisional_games); |
||
| 371 | |||
| 372 | $xoopsTpl->assign('chess_show_option_urlparam', $show_option_urlparam); |
||
| 373 | |||
| 374 | $xoopsTpl->assign('chess_games_pagenav', $games_pagenav->renderNav()); |
||
| 375 | |||
| 376 | $xoopsTpl->assign('chess_challenges_pagenav', $challenges_pagenav->renderNav()); |
||
| 377 | } |
||
| 378 |