Completed
Push — work-fleets ( 22b5bc...08ace7 )
by SuperNova.WS
06:13
created

rpg_points.php ➔ rpg_level_up()   C

Complexity

Conditions 8
Paths 40

Size

Total Lines 71
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 8
eloc 52
c 4
b 1
f 0
nc 40
nop 3
dl 0
loc 71
rs 6.4391

How to fix   Long Method   

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
 * @package rpg
5
 * @version $Id$
6
 * @copyright (c) 2009-2010 Gorlum for http://supernova.ws
7
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
 *
9
 */
10
11
12
/**
13
 *
14
 * This function changes rpg_points for user
15
 * You should ALWAYS use this function and NEVER directly change rpg_points by yourself
16
 * Otherwise refferal system wouldn't work and no logs would be made
17
 * "No logs" means you can never check if the user cheating with DM
18
 *
19
 * @param int    $user_id
20
 * @param int    $change_type
21
 * @param int    $dark_matter
22
 * @param string $comment
23
 * @param bool   $already_changed
24
 *
25
 * @return bool|int
26
 *
27
 * @package rpg
28
 */
29
function rpg_points_change($user_id, $change_type, $dark_matter, $comment = '', $already_changed = false) {
30
  global $dm_change_legit, $user;
31
32
  if (!$user_id) {
33
    return false;
34
  }
35
36
  $dm_change_legit = true;
37
  $sn_data_dark_matter_db_name = pname_resource_name(RES_DARK_MATTER);
38
39
  if ($already_changed) {
40
    $rows_affected = 1;
41
  } else {
42
    $changeset = array();
43
    $a_user = DBStaticUser::db_user_by_id($user_id, true);
44
    if ($dark_matter < 0) {
45
      $dark_matter_exists = mrc_get_level($a_user, null, RES_DARK_MATTER, false, true);
46
      $dark_matter_exists < 0 ? $dark_matter_exists = 0 : false;
47
      $metamatter_to_reduce = -$dark_matter - $dark_matter_exists;
48
      if ($metamatter_to_reduce > 0) {
49
        $metamatter_exists = mrc_get_level($a_user, null, RES_METAMATTER);
50
        if ($metamatter_exists < $metamatter_to_reduce) {
51
          classSupernova::$debug->error('Ошибка снятия ТМ - ММ+ТМ меньше, чем сумма для снятия!', 'Ошибка снятия ТМ', LOG_ERR_INT_NOT_ENOUGH_DARK_MATTER);
52
        }
53
        if (is_array($comment)) {
54
          $comment = call_user_func_array('sprintf', $comment);
55
        }
56
//        mm_points_change($user_id, $change_type, -$metamatter_to_reduce, 'ММ в ТМ: ' . (-$dark_matter) . ' ТМ = ' . $dark_matter_exists . ' ТМ + ' . $metamatter_to_reduce . ' ММ. ' . $comment);
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% 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...
57
        classSupernova::$auth->account->metamatter_change($change_type, -$metamatter_to_reduce, 'ММ в ТМ: ' . (-$dark_matter) . ' ТМ = ' . $dark_matter_exists . ' ТМ + ' . $metamatter_to_reduce . ' ММ. ' . $comment);
58
        $dark_matter = -$dark_matter_exists;
59
      }
60
    } else {
61
      $changeset['dark_matter_total'] = +$dark_matter;
62
    }
63
    if($dark_matter) {
64
      $changeset[$sn_data_dark_matter_db_name] = +$dark_matter;
65
    }
66
    if(!empty($changeset)) {
67
      DBStaticUser::db_user_adjust_by_id($user_id, $changeset);
68
    }
69
    $rows_affected = classSupernova::$db->db_affected_rows();
70
  }
71
72
  if ($rows_affected || !$dark_matter) {
73
    if (is_array($comment)) {
74
      $comment = call_user_func_array('sprintf', $comment);
75
    }
76
    $comment_unsafe = $comment;
77
    $row = DBStaticUser::db_user_by_id($user_id, false, 'username');
78
    $row['username'] = db_escape($row['username']);
79
    db_log_dark_matter_insert($user_id, $change_type, $dark_matter, $comment_unsafe, $row['username'], $_SERVER['SCRIPT_NAME']);
80
81
    if ($user['id'] == $user_id) {
82
      $user['dark_matter'] += $dark_matter;
83
    }
84
85 View Code Duplication
    if ($dark_matter > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
      $old_referral = db_referral_get_by_id($user_id);
87
      if ($old_referral['id']) {
88
        db_referral_update_dm($user_id, $dark_matter);
89
        $new_referral = db_referral_get_by_id($user_id);
90
91
        $partner_bonus = floor($new_referral['dark_matter'] / classSupernova::$config->rpg_bonus_divisor) - ($old_referral['dark_matter'] >= classSupernova::$config->rpg_bonus_minimum ? floor($old_referral['dark_matter'] / classSupernova::$config->rpg_bonus_divisor) : 0);
92
        if ($partner_bonus > 0 && $new_referral['dark_matter'] >= classSupernova::$config->rpg_bonus_minimum) {
93
          rpg_points_change($new_referral['id_partner'], RPG_REFERRAL, $partner_bonus, "Incoming From Referral ID {$user_id}");
94
        }
95
      }
96
    }
97
  } else {
98
    classSupernova::$debug->warning("Error adjusting Dark Matter for player ID {$user_id} (Player Not Found?) with {$dark_matter}. Reason: {$comment}", 'Dark Matter Change', 402);
99
  }
100
101
  $dm_change_legit = false;
102
103
  return $rows_affected;
104
}
105
106
function rpg_level_up(&$user, $type, $xp_to_add = 0) {
107
  $q = 1.03;
108
109
  $field_xp = '';
110
  $field_level = '';
111
  $b1 = 10;
112
  $comment = '';
113
  switch ($type) {
114
    case RPG_STRUCTURE:
115
      $field_level = 'lvl_minier';
116
      $field_xp = 'xpminier';
117
      $b1 = 50;
118
      $comment = 'Level Up For Structure Building';
119
    break;
120
121
    case RPG_RAID:
122
      $field_level = 'lvl_raid';
123
      $field_xp = 'xpraid';
124
      $b1 = 10;
125
      $comment = 'Level Up For Raiding';
126
    break;
127
128
    case RPG_TECH:
129
      $field_level = 'player_rpg_tech_level';
130
      $field_xp = 'player_rpg_tech_xp';
131
      $b1 = 50;
132
      $comment = 'Level Up For Research';
133
    break;
134
135
    case RPG_EXPLORE:
136
      $field_level = 'player_rpg_explore_level';
137
      $field_xp = 'player_rpg_explore_xp';
138
      $b1 = 10;
139
      $comment = 'Level Up For Exploration';
140
      $q = 1.05;
141
    break;
142
143
    default:
144
    break;
145
146
  }
147
148
  $xp = &$user[$field_xp];
149
150
  if ($xp_to_add) {
151
    $xp += $xp_to_add;
152
    DBStaticUser::db_user_adjust_by_id(
153
      $user['id'],
154
      array(
155
        $field_xp => +$xp_to_add
156
      )
157
    );
158
  }
159
160
  $level = $user[$field_level];
161
  while ($xp > rpg_xp_for_level($level + 1, $b1, $q)) {
162
    $level++;
163
  }
164
  $level -= $user[$field_level];
165
  if ($level > 0) {
166
    DBStaticUser::db_user_adjust_by_id(
167
      $user['id'],
168
      array(
169
        $field_level => +$level
170
      )
171
    );
172
173
    rpg_points_change($user['id'], $type, $level * 1000, $comment);
174
    $user[$field_level] += $level;
175
  }
176
}
177
178
function rpg_xp_for_level($level, $b1, $q) {
179
  return floor($b1 * (pow($q, $level) - 1) / ($q - 1));
180
}
181
182
function rpg_get_miner_xp($level) {
183
  return rpg_xp_for_level($level, 50, 1.03);
184
}
185
186
function RPG_get_raider_xp($level) {
187
  return rpg_xp_for_level($level, 10, 1.03);
188
}
189
190
function rpg_get_tech_xp($level) {
191
  return rpg_xp_for_level($level, 50, 1.03);
192
}
193
194
function rpg_get_explore_xp($level) {
195
  return rpg_xp_for_level($level, 10, 1.05);
196
}
197