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

sys_user.php ➔ player_create()   F

Complexity

Conditions 22
Paths 5120

Size

Total Lines 84
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 22
eloc 55
nc 5120
nop 3
dl 0
loc 84
rs 2.1001
c 5
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
 * Created by PhpStorm.
4
 * User: Gorlum
5
 * Date: 17.04.2015
6
 * Time: 6:37
7
 */
8
9
function sys_user_vacation($user) {
10
  if(sys_get_param_str('vacation') == 'leave') {
11
    if ($user['vacation'] < SN_TIME_NOW) {
12
      $user['vacation'] = 0;
13
      $user['vacation_next'] = SN_TIME_NOW + classSupernova::$config->player_vacation_timeout;
14
      db_user_set_by_id($user['id'], "`vacation` = {$user['vacation']}, `vacation_next` = {$user['vacation_next']}");
15
    }
16
  }
17
18
  if($user['vacation']) {
19
    // sn_sys_logout(false, true);
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...
20
    // core_auth::logout(false, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
21
22
    $template = gettemplate('vacation', true);
23
24
    $template->assign_vars(array(
25
      'NAME' => $user['username'],
26
      'VACATION_END' => date(FMT_DATE_TIME, $user['vacation']),
27
      'CAN_LEAVE' => $user['vacation'] <= SN_TIME_NOW,
28
      'RANDOM' => mt_rand(1, 2),
29
    ));
30
31
    display(parsetemplate($template), '', false, '', false, false);
32
  }
33
34
  return false;
35
}
36
37
function sys_is_multiaccount($user1, $user2) {
38
  return $user1['user_lastip'] == $user2['user_lastip'] && !classSupernova::$config->game_multiaccount_enabled;
39
}
40
41
/**
42
 * @param $UserID
43
 */
44
function DeleteSelectedUser($UserID) {
45
  // TODO: Full rewrite
46
  sn_db_transaction_start();
47
  $TheUser = db_user_by_id($UserID);
48
  if ( $TheUser['ally_id'] != 0 ) {
49
    $TheAlly = doquery ( "SELECT * FROM `{{alliance}}` WHERE `id` = '" . $TheUser['ally_id'] . "';", '', true );
50
    $TheAlly['ally_members'] -= 1;
51
    if ( $TheAlly['ally_members'] > 0 ) {
52
      doquery ( "UPDATE `{{alliance}}` SET `ally_members` = '" . $TheAlly['ally_members'] . "' WHERE `id` = '" . $TheAlly['id'] . "';");
53
    } else {
54
      doquery ( "DELETE FROM `{{alliance}}` WHERE `id` = '" . $TheAlly['id'] . "';");
55
      doquery ( "DELETE FROM `{{statpoints}}` WHERE `stat_type` = '2' AND `id_owner` = '" . $TheAlly['id'] . "';");
56
    }
57
  }
58
  doquery ( "DELETE FROM `{{statpoints}}` WHERE `stat_type` = '1' AND `id_owner` = '" . $UserID . "';");
59
60
  db_planet_list_delete_by_owner($UserID);
61
62
  doquery ( "DELETE FROM `{{messages}}` WHERE `message_sender` = '" . $UserID . "';");
63
  doquery ( "DELETE FROM `{{messages}}` WHERE `message_owner` = '" . $UserID . "';");
64
  doquery ( "DELETE FROM `{{notes}}` WHERE `owner` = '" . $UserID . "';");
65
  FleetList::db_fleet_list_delete_by_owner($UserID);
66
//  doquery ( "DELETE FROM `{{rw}}` WHERE `id_owner1` = '" . $UserID . "';");
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
67
//  doquery ( "DELETE FROM `{{rw}}` WHERE `id_owner2` = '" . $UserID . "';");
68
  doquery ( "DELETE FROM `{{buddy}}` WHERE `BUDDY_SENDER_ID` = '" . $UserID . "';");
69
  doquery ( "DELETE FROM `{{buddy}}` WHERE `BUDDY_OWNER_ID` = '" . $UserID . "';");
70
  doquery ( "DELETE FROM `{{annonce}}` WHERE `user` = '" . $UserID . "';");
71
72
73
  classSupernova::db_del_record_by_id(LOC_USER, $UserID);
74
  doquery ( "DELETE FROM `{{referrals}}` WHERE (`id` = '{$UserID}') OR (`id_partner` = '{$UserID}');");
75
  classSupernova::$config->db_saveItem('users_amount', classSupernova::$config->db_loadItem('users_amount') - 1);
76
  sn_db_transaction_commit();
77
}
78
79
/**
80
 * @param        $banner
81
 * @param        $banned
82
 * @param        $term
83
 * @param bool   $is_vacation
84
 * @param string $reason
85
 */
86
function sys_admin_player_ban($banner, $banned, $term, $is_vacation = true, $reason = '') {
87
  $ban_current = db_user_by_id($banned['id'], false, 'banaday');
88
  $ban_until = ($ban_current['banaday'] ? $ban_current['banaday'] : SN_TIME_NOW) + $term;
89
90
  db_user_set_by_id($banned['id'], "`banaday` = {$ban_until} " . ($is_vacation ? ", `vacation` = '{$ban_until}' " : ''));
91
92
  $banned['username'] = db_escape($banned['username']);
93
  $banner['username'] = db_escape($banner['username']);
94
  db_ban_insert($banner, $banned, $reason, $ban_until);
95
96
  db_planet_set_by_owner($banned['id'],
97
    "`metal_mine_porcent` = 0, `crystal_mine_porcent` = 0, `deuterium_sintetizer_porcent` = 0, `solar_plant_porcent` = 0,
98
    `fusion_plant_porcent` = 0, `solar_satelit_porcent` = 0, `ship_sattelite_sloth_porcent` = 0"
99
  );
100
}
101
102
/**
103
 * @param        $banner
104
 * @param        $banned
105
 * @param string $reason
106
 */
107
function sys_admin_player_ban_unset($banner, $banned, $reason = '') {
108
  db_user_set_by_id($banned['id'], "`banaday` = 0, `vacation` = " . SN_TIME_NOW . "");
109
110
  $banned['username'] = db_escape($banned['username']);
111
  $banner['username'] = db_escape($banner['username']);
112
  $reason = db_escape($reason);
113
  db_ban_insert_unset($banner, $banned, $reason);
114
}
115
116
function player_create($username_unsafe, $email_unsafe, $options) {
117
  sn_db_transaction_check(true);
118
119
  static $player_options_string = 'opt_mnl_spy^1|opt_email_mnl_spy^0|opt_email_mnl_joueur^0|opt_email_mnl_alliance^0|opt_mnl_attaque^1|opt_email_mnl_attaque^0|opt_mnl_exploit^1|opt_email_mnl_exploit^0|opt_mnl_transport^1|opt_email_mnl_transport^0|opt_email_msg_admin^1|opt_mnl_expedition^1|opt_email_mnl_expedition^0|opt_mnl_buildlist^1|opt_email_mnl_buildlist^0|opt_int_navbar_resource_force^1|';
120
121
  empty($options['planet_options']) ? $options['planet_options'] = array() : false;
122
123
  $field_set = array(
124
    'server_name' => SN_ROOT_VIRTUAL,
125
    'register_time' => SN_TIME_NOW,
126
    'user_bot' => $options['user_bot'] = empty($options['user_bot']) ? USER_BOT_PLAYER : $options['total_points'],
127
128
    'username' => $username_unsafe,
129
    'email' => $email_unsafe,
130
    'email_2' => $email_unsafe,
131
132
    'lang' => $options['language_iso'] ? $options['language_iso'] : DEFAULT_LANG,
133
    'dpath' => DEFAULT_SKINPATH,
134
135
    'total_points' => $options['total_points'] = empty($options['total_points']) ? 0 : $options['total_points'],
136
137
    'options' => (empty($options['options']) ? $player_options_string : $options['options']) . (empty($options['options_extra']) ? '' : $options['options_extra']),
138
139
    'galaxy' => $options['galaxy'] = intval($options['galaxy'] ? $options['galaxy'] : 0),
140
    'system' => $options['system'] = intval($options['system'] ? $options['system'] : 0),
141
    'planet' => $options['planet'] = intval($options['planet'] ? $options['planet'] : 0),
142
  );
143
144
  !empty($options['salt']) ? $field_set['salt'] = $options['salt'] : false;
145
  !empty($options['password_encoded_unsafe']) ? $field_set['password'] = $options['password_encoded_unsafe'] : false;
146
147
  $user_new = classSupernova::db_ins_field_set(LOC_USER, $field_set);
148
  if(!($options['galaxy'] && $options['system'] && $options['planet'])) {
149
    $options['galaxy'] = classSupernova::$config->LastSettedGalaxyPos;
150
    $options['system'] = classSupernova::$config->LastSettedSystemPos;
151
    $segment_size = floor(Vector::$knownPlanets/ 3);
152
    $segment = floor(classSupernova::$config->LastSettedPlanetPos / $segment_size);
153
    $segment++;
154
    $options['planet'] = mt_rand(1 + $segment * $segment_size, ($segment + 1) * $segment_size);
155
156
    // $new_planet_id = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
157
    while(true) {
158
      if($options['planet'] > Vector::$knownPlanets) {
159
        $options['planet'] = mt_rand(0, $segment_size - 1) + 1;
160
        $options['system']++;
161
      }
162
      if($options['system'] > Vector::$knownSystems) {
163
        $options['system'] = 1;
164
        $options['galaxy']++;
165
      }
166
      $options['galaxy'] > Vector::$knownGalaxies? $options['galaxy'] = 1 : false;
167
168
      $galaxy_row = db_planet_by_gspt($options['galaxy'], $options['system'], $options['planet'], PT_PLANET, true, 'id');
169
      if(!$galaxy_row['id']) {
170
        classSupernova::$config->db_saveItem(array(
171
          'LastSettedGalaxyPos' => $options['galaxy'],
172
          'LastSettedSystemPos' => $options['system'],
173
          'LastSettedPlanetPos' => $options['planet'],
174
        ));
175
        break;
176
      }
177
      $options['planet'] += 3;
178
    }
179
  }
180
  $new_planet_id = uni_create_planet($options['galaxy'], $options['system'], $options['planet'], $user_new['id'], classLocale::$lang['sys_capital'], true, $options['planet_options']);
181
182
  db_user_set_by_id($user_new['id'],
183
    "`id_planet` = '{$new_planet_id}', `current_planet` = '{$new_planet_id}',
184
    `galaxy` = '{$options['galaxy']}', `system` = '{$options['system']}', `planet` = '{$options['planet']}'"
185
  );
186
187
  classSupernova::$config->db_saveItem('users_amount', classSupernova::$config->users_amount + 1);
188
189
  $username_safe = db_escape($username_unsafe);
190
  db_player_name_history_replace($user_new, $username_safe);
191
192
  if(!empty($options['partner_id']) && ($referral_row = db_user_by_id($options['partner_id'], true))) {
193
    db_referral_insert($options, $user_new);
194
  }
195
196
  sys_player_new_adjust($user_new['id'], $new_planet_id);
197
198
  return $result = db_user_by_id($user_new['id']);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
199
}
200