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

rpg_points.php ➔ rpg_points_change()   F

Complexity

Conditions 19
Paths 1126

Size

Total Lines 73
Code Lines 50

Duplication

Lines 12
Ratio 16.44 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 19
eloc 50
nc 1126
nop 5
dl 12
loc 73
rs 2.4278
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
 *
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 = 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_total` + '{$dark_matter}'";
62
    }
63
    $dark_matter ? $changeset[] = "`{$sn_data_dark_matter_db_name}` = `{$sn_data_dark_matter_db_name}` + '{$dark_matter}'" : false;
64
    !empty($changeset) ? db_user_set_by_id($user_id, implode(',', $changeset)) : false;
65
    $rows_affected = classSupernova::$db->db_affected_rows();
66
  }
67
68
  if ($rows_affected || !$dark_matter) {
69
    $page_url = db_escape($_SERVER['SCRIPT_NAME']);
70
    if (is_array($comment)) {
71
      $comment = call_user_func_array('sprintf', $comment);
72
    }
73
    $comment = db_escape($comment);
74
    $row = db_user_by_id($user_id, false, 'username');
75
    $row['username'] = db_escape($row['username']);
76
    db_log_dark_matter_insert($user_id, $change_type, $dark_matter, $comment, $row, $page_url);
77
78
    if ($user['id'] == $user_id) {
79
      $user['dark_matter'] += $dark_matter;
80
    }
81
82 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...
83
      $old_referral = db_referral_get_by_id($user_id);
84
      if ($old_referral['id']) {
85
        db_referral_update_dm($user_id, $dark_matter);
86
        $new_referral = db_referral_get_by_id($user_id);
87
88
        $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);
89
        if ($partner_bonus > 0 && $new_referral['dark_matter'] >= classSupernova::$config->rpg_bonus_minimum) {
90
          rpg_points_change($new_referral['id_partner'], RPG_REFERRAL, $partner_bonus, "Incoming From Referral ID {$user_id}");
91
        }
92
      }
93
    }
94
  } else {
95
    classSupernova::$debug->warning("Error adjusting Dark Matter for player ID {$user_id} (Player Not Found?) with {$dark_matter}. Reason: {$comment}", 'Dark Matter Change', 402);
96
  }
97
98
  $dm_change_legit = false;
99
100
  return $rows_affected;
101
}
102
103
function rpg_level_up(&$user, $type, $xp_to_add = 0) {
104
  $q = 1.03;
105
106
  $field_xp = '';
107
  $field_level = '';
108
  $b1 = 10;
109
  $comment = '';
110
  switch ($type) {
111
    case RPG_STRUCTURE:
112
      $field_level = 'lvl_minier';
113
      $field_xp = 'xpminier';
114
      $b1 = 50;
115
      $comment = 'Level Up For Structure Building';
116
    break;
117
118
    case RPG_RAID:
119
      $field_level = 'lvl_raid';
120
      $field_xp = 'xpraid';
121
      $b1 = 10;
122
      $comment = 'Level Up For Raiding';
123
    break;
124
125
    case RPG_TECH:
126
      $field_level = 'player_rpg_tech_level';
127
      $field_xp = 'player_rpg_tech_xp';
128
      $b1 = 50;
129
      $comment = 'Level Up For Research';
130
    break;
131
132
    case RPG_EXPLORE:
133
      $field_level = 'player_rpg_explore_level';
134
      $field_xp = 'player_rpg_explore_xp';
135
      $b1 = 10;
136
      $comment = 'Level Up For Exploration';
137
      $q = 1.05;
138
    break;
139
140
    default:
141
    break;
142
143
  }
144
145
  $xp = &$user[$field_xp];
146
147
  if ($xp_to_add) {
148
    $xp += $xp_to_add;
149
    db_user_set_by_id($user['id'], "`{$field_xp}` = `{$field_xp}` + '{$xp_to_add}'");
150
  }
151
152
  $level = $user[$field_level];
153
  while ($xp > rpg_xp_for_level($level + 1, $b1, $q)) {
154
    $level++;
155
  }
156
  $level -= $user[$field_level];
157
  if ($level > 0) {
158
    db_user_set_by_id($user['id'], "`{$field_level}` = `{$field_level}` + '{$level}'");
159
    rpg_points_change($user['id'], $type, $level * 1000, $comment);
160
    $user[$field_level] += $level;
161
  }
162
}
163
164
function rpg_xp_for_level($level, $b1, $q) {
165
  return floor($b1 * (pow($q, $level) - 1) / ($q - 1));
166
}
167
168
function rpg_get_miner_xp($level) {
169
  return rpg_xp_for_level($level, 50, 1.03);
170
}
171
172
function RPG_get_raider_xp($level) {
173
  return rpg_xp_for_level($level, 10, 1.03);
174
}
175
176
function rpg_get_tech_xp($level) {
177
  return rpg_xp_for_level($level, 50, 1.03);
178
}
179
180
function rpg_get_explore_xp($level) {
181
  return rpg_xp_for_level($level, 10, 1.05);
182
}
183