Completed
Push — work-fleets ( fff2b6...e0e753 )
by SuperNova.WS
06:54
created

imperator.php ➔ sn_imperator_view()   F

Complexity

Conditions 15
Paths 1514

Size

Total Lines 163
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 15
eloc 101
nc 1514
nop 1
dl 0
loc 163
rs 2
c 3
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * imperator.php
5
 *
6
 * Player's information
7
 *
8
 * @version 2.0 copyright (c) 2010-2012 by Gorlum for http://supernova.ws
9
 */
10
11
classSupernova::$sn_mvc['i18n']['imperator'] = array(
12
  'overview' => 'overview',
13
);
14
15
classSupernova::$sn_mvc['view']['imperator'][] = 'sn_imperator_view';
16
17
function sn_imperator_view($template = null) {
18
  global $user;
19
20
  $stat_fields = array(
21
    'stat_date' => 'STAT_DATE',
22
23
    // 'stat_code' => 'STAT_CODE',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
    'total_rank' => 'TOTAL_RANK',
25
    'total_points' => 'TOTAL_POINTS',
26
    //'total_count' => 'TOTAL_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
    'tech_rank' => 'TECH_RANK',
28
    'tech_points' => 'TECH_POINTS',
29
    //'tech_count' => 'TECH_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
    'build_rank' => 'BUILD_RANK',
31
    'build_points' => 'BUILD_POINTS',
32
    //'build_count' => 'BUILD_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    'defs_rank' => 'DEFS_RANK',
34
    'defs_points' => 'DEFS_POINTS',
35
    //'defs_count' => 'DEFS_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
    'fleet_rank' => 'FLEET_RANK',
37
    'fleet_points' => 'FLEET_POINTS',
38
    //'fleet_count' => 'FLEET_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    'res_rank' => 'RES_RANK',
40
    'res_points' => 'RES_POINTS',
41
    // 'res_count' => 'RES_COUNT',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
  );
43
44
  $user_id = sys_get_param_id('int_user_id', $user['id']);
45
46
  $user_data = ($same_user = $user_id == $user['id']) ? $user : db_user_by_id($user_id);
47
48
//  if($user_id == $user['id']) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
//    $user_data = &$user;
50
//    $same_user = true;
51
//  } else {
52
//    $user_data = db_user_by_id($user_id);
53
//    $same_user = false;
54
//  }
55
56
  if(!$user_data) {
57
    message(classLocale::$lang['imp_imperator_none'], classLocale::$lang['sys_error'], 'index.php', 10);
58
    die();
59
  }
60
61
  $template = gettemplate('imperator', $template);
62
  $StatRecord = db_stat_get_by_user($user_id);
63
64
  $stat_array = array();
65
  $query = db_stat_get_by_user2($user_id);
66
  $stat_count = classSupernova::$db->db_affected_rows();
67
  while($row = db_fetch($query)) {
68
    foreach($stat_fields as $field_db_name => $field_template_name) {
69
      // $stat_count - $row['stat_code'] - для реверсирования ID статы в JS
70
      $stat_array[$field_template_name]['DATA'][$stat_count - $row['stat_code']] = $row[$field_db_name];
71
    }
72
  }
73
74
  $stat_array_date = $stat_array['STAT_DATE'];
75
  foreach($stat_array_date['DATA'] as $key => $value) {
76
    $template->assign_block_vars('stat_date', array(
77
      'ID' => $key,
78
      'VALUE' => $value,
79
      'TEXT' => date(FMT_DATE_TIME, $value),
80
    ));
81
  }
82
  // $stat_count = count($stat_array_date['DATA']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
  // pdump($stat_array_date);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
85
86
  unset($stat_array['STAT_DATE']);
87
  $template_data = array();
88
  foreach($stat_array as $stat_type => &$stat_type_data) {
89
    $reverse_min_max = strpos($stat_type, '_RANK') !== false;
90
    $stat_type_data['MIN'] = $reverse_min_max ? max($stat_type_data['DATA']) : min($stat_type_data['DATA']);
91
    $stat_type_data['MAX'] = $reverse_min_max ? min($stat_type_data['DATA']) : max($stat_type_data['DATA']);
92
    $stat_type_data['AVG'] = average($stat_type_data['DATA']);
93
    foreach($stat_type_data['DATA'] as $key => $value) {
94
      // $stat_type_data['PERCENT'][$key] = $stat_type_data['MAX'] - $value ? ($stat_type_data['MAX'] - $stat_type_data['MIN']) / ($stat_type_data['MAX'] - $value) : 100;
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
      $stat_type_data['PERCENT'][$key] = ($stat_type_data['MAX'] - $value ? ($value - $stat_type_data['MIN']) / ($stat_type_data['MAX'] - $stat_type_data['MIN']) : 1) * 100;
96
      $template_data[$stat_type][$key]['ID'] = $key;
97
      $template_data[$stat_type][$key]['VALUE'] = $value;
98
      $template_data[$stat_type][$key]['DELTA'] = ($reverse_min_max ? $stat_type_data['MIN']  - $value : $value - $stat_type_data['MAX']);
99
      $template_data[$stat_type][$key]['PERCENT'] = $stat_type_data['PERCENT'][$key];
100
101
//$template_data[$stat_type][$key]['PERCENT'] = $key ? $stat_type_data['PERCENT'][$key] : 50; // TODO DEBUG
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
    }
103
  }
104
  // pdump($stat_array['RES_POINTS']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
106
  foreach($template_data as $stat_type => $stat_type_data) {
107
    $template->assign_block_vars('stat', array(
108
      'TYPE' => $stat_type,
109
      'TEXT' => classLocale::$lang['imp_stat_types'][$stat_type],
110
      'MIN' => $stat_array[$stat_type]['MIN'],
111
      'MAX' => $stat_array[$stat_type]['MAX'],
112
      'AVG' => $stat_array[$stat_type]['AVG'],
113
    ));
114
    foreach($stat_type_data as $stat_entry) {
115
      $template->assign_block_vars('stat.entry', $stat_entry);
116
    }
117
  }
118
119
120
  // pdump($template_data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
122
  if($same_user) {
123
    rpg_level_up($user, RPG_STRUCTURE);
124
    rpg_level_up($user, RPG_RAID);
125
    rpg_level_up($user, RPG_TECH);
126
    rpg_level_up($user, RPG_EXPLORE);
127
  }
128
129
130
  $template->assign_vars(array(
131
    'USERS_TOTAL'          => classSupernova::$config->users_amount,
132
133
    'USER_ID'              => $user_id,
134
    'user_username'        => player_nick_render_to_html($user_data, true),
135
    // 'user_gender'             => $user_data['gender'] == 'F' ? 'female' : 'male',
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
    'USER_AVATAR'          => $user_data['avatar'],
137
    'VACATION'             => $user_data['vacation'],
138
    'GENDER_TEXT'          => classLocale::$lang['sys_gender_list'][$user_data['gender']],
139
140
    'NEW_MESSAGES'         => $user_data['new_message'],
141
    'REGISTRATION_DATE'    => date(FMT_DATE_TIME, $user_data['register_time']),
142
143
    'builder_xp'           => pretty_number($user_data['xpminier']),
144
    'builder_lvl'          => pretty_number($user_data['lvl_minier']),
145
    'builder_lvl_st'       => pretty_number(rpg_get_miner_xp($user_data['lvl_minier'])),
146
    'builder_lvl_up'       => pretty_number(rpg_get_miner_xp($user_data['lvl_minier']+1)),
147
    'raid_xp'              => pretty_number($user_data['xpraid']),
148
    'raid_lvl'             => pretty_number($user_data['lvl_raid']),
149
    'raid_lvl_up'          => pretty_number(rpg_get_raider_xp($user_data['lvl_raid']+1)),
150
    'raids'                => pretty_number($user_data['raids']),
151
    'raidswin'             => pretty_number($user_data['raidswin']),
152
    'raidsloose'           => pretty_number($user_data['raidsloose']),
153
    'tech_xp'              => pretty_number($user_data['player_rpg_tech_xp']),
154
    'tech_lvl'             => pretty_number($user_data['player_rpg_tech_level']),
155
    'tech_lvl_st'          => pretty_number(rpg_get_tech_xp($user_data['player_rpg_tech_level'])),
156
    'tech_lvl_up'          => pretty_number(rpg_get_tech_xp($user_data['player_rpg_tech_level']+1)),
157
158
    'explore_xp'           => pretty_number($user_data['player_rpg_explore_xp']),
159
    'explore_lvl'          => pretty_number($user_data['player_rpg_explore_level']),
160
    'explore_lvl_st'       => pretty_number(rpg_get_explore_xp($user_data['player_rpg_explore_level'])),
161
    'explore_lvl_up'       => pretty_number(rpg_get_explore_xp($user_data['player_rpg_explore_level']+1)),
162
163
    'build_points'         => pretty_number( $StatRecord['build_points'] ),
164
    'tech_points'          => pretty_number( $StatRecord['tech_points'] ),
165
    'fleet_points'         => pretty_number( $StatRecord['fleet_points'] ),
166
    'defs_points'          => pretty_number( $StatRecord['defs_points'] ),
167
    'res_points'           => pretty_number( $StatRecord['res_points'] ),
168
    'total_points'         => pretty_number( $StatRecord['total_points'] ),
169
    'user_rank'            => $StatRecord['total_rank'],
170
    'RANK_DIFF'            => $StatRecord['total_old_rank'] - $StatRecord['total_rank'],
171
172
    'STAT_COUNT'           => $stat_count,
173
    'STAT_SPAN'            => $stat_count + 1,
174
175
    'SAME_USER'            => $same_user,
176
  ));
177
178
  return parsetemplate($template);
179
}
180