eco_mrk_trader()   F
last analyzed

Complexity

Conditions 25
Paths 5772

Size

Total Lines 129
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 81
nc 5772
nop 2
dl 0
loc 129
rs 0
c 0
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
use DBAL\db_mysql;
4
5
if((!defined('SN_IN_MARKET') || SN_IN_MARKET !== true) && (!defined('SN_IN_ALLY') || SN_IN_ALLY !== true)) {
6
  $debug->error("Attempt to call market page mode {$mode} directly - not from market.php", 'Forbidden', 403);
7
}
8
9
function eco_mrk_trader($user, $planetrow = null) {
10
  global $debug, $mode;
11
  // global $lang;
12
13
  // $page_title = " - {$lang['eco_mrk_trader']}";
14
  $template = SnTemplate::gettemplate('market_trader', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type null|template expected by parameter $template of SnTemplate::gettemplate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
  $template = SnTemplate::gettemplate('market_trader', /** @scrutinizer ignore-type */ true);
Loading history...
15
16
  $intError = MARKET_DEAL;
17
  $planetrow = isset($planetrow) ? $planetrow : $user;
18
19
  global $page_title, $config, $lang;
20
21
  lng_include('market');
22
23
  $rates = array(
24
    RES_METAL => $config->rpg_exchange_metal,
25
    RES_CRYSTAL => $config->rpg_exchange_crystal,
26
    RES_DEUTERIUM => $config->rpg_exchange_deuterium,
27
    RES_DARK_MATTER => $config->rpg_exchange_darkMatter
28
  );
29
30
  // $dm_db_name = pname_resource_name(RES_DARK_MATTER);
31
  $exchangeTo = in_array($exchangeTo = sys_get_param_int('exchangeTo'), sn_get_groups('resources_trader')) ? $exchangeTo : 0;
32
  if($exchangeTo && is_array($tradeList = $_POST['spend'])) {
33
    $value = 0;
34
    $qry = array();
35
36
    db_mysql::db_transaction_start();
37
    if($planetrow['id_owner']) {
38
      $global_data = sys_o_get_updated($planetrow['id_owner'], $planetrow['id'], SN_TIME_NOW);
39
      $planetrow = $global_data['planet'];
40
    }
41
    else {
42
      // Locking user record
43
      $user = db_user_by_id($user['id'], true);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

43
      $user = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['id'], true);
Loading history...
44
    }
45
46
    foreach(sn_get_groups('resources_trader') as $resource_id) {
47
      $amount = floatval($tradeList[$resource_id]);
48
      if($amount < 0) {
49
        $debug->error('Trying to supply negative resource amount on Black Market Page', 'Hack Attempt', 305);
50
      }
51
52
      if($resource_id == RES_DARK_MATTER && $exchangeTo == RES_DARK_MATTER) {
53
        continue;
54
      }
55
56
      $resource_db_name = pname_resource_name($resource_id);
57
      if($exchangeTo == RES_DARK_MATTER) {
58
        $sign = '+';
59
        $amount = floor($tradeList[RES_DARK_MATTER] / 3 * $rates[RES_DARK_MATTER] / $rates[$resource_id]);
60
        $value += $amount;
61
      } else {
62
        $value += floor($amount * $rates[$resource_id] / $rates[$exchangeTo]);
63
        if($resource_id == RES_DARK_MATTER) {
64
          $amount = 0;
65
        } else {
66
          if(mrc_get_level($user, $planetrow, $resource_id, true) < $amount) {
67
            $intError = MARKET_NO_RESOURCES;
68
            break;
69
          }
70
71
          $sign = '-';
72
        }
73
      }
74
75
      if($amount) {
76
        $qry[] = "`{$resource_db_name}` = `{$resource_db_name}` {$sign} {$amount}";
77
      }
78
    }
79
80
    if($exchangeTo != RES_DARK_MATTER) {
81
      $amount = floor($value);
82
      $exchange_to_db_name = pname_resource_name($exchangeTo);
83
      $qry[] = "`{$exchange_to_db_name}` = `{$exchange_to_db_name}` + {$amount}";
84
    }
85
86
    $operation_cost = $config->rpg_cost_trader * ($exchangeTo == RES_DARK_MATTER ? 3 : 1) + $tradeList[RES_DARK_MATTER];
87
88
    $intError = $value <= 0 ? MARKET_ZERO_DEAL : $intError;
89
    $intError = mrc_get_level($user, null, RES_DARK_MATTER) < $operation_cost ? MARKET_NO_DM : $intError;
90
91
    if($intError == MARKET_DEAL) {
92
      $qry = implode(', ', $qry);
93
      $table = $planetrow['id_owner'] ? 'planets' : 'users';
94
95
      doquery("UPDATE {{{$table}}} SET {$qry} WHERE `id` = {$planetrow['id']} LIMIT 1;");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
      /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{{$table}}} SET {$qry} WHERE `id` = {$planetrow['id']} LIMIT 1;");
Loading history...
96
      rpg_points_change($user['id'], RPG_MARKET_EXCHANGE, -$operation_cost, "Using Black Market page {$lang['eco_mrk_trader']}");
97
      db_mysql::db_transaction_commit();
98
99
      $intError = MARKET_DEAL_TRADE;
100
      $_SERVER['REQUEST_URI'] = ($has_message = strpos($_SERVER['REQUEST_URI'], '&message=')) ? substr($_SERVER['REQUEST_URI'], 0, $has_message) : $_SERVER['REQUEST_URI'];
101
      sys_redirect("{$_SERVER['REQUEST_URI']}&message={$intError}");
102
    }
103
    db_mysql::db_transaction_rollback();
104
    $template->assign_block_vars('result', array(
105
      'STATUS' => $intError == MARKET_DEAL ? ERR_NONE : ERR_ERROR,
106
      'MESSAGE' => $lang['eco_mrk_errors'][$intError],
107
    ));
108
  }
109
110
  $template->assign_vars(array(
111
    'EXCHANGE_TO_RESOURCE_ID' => $exchangeTo,
112
  ));
113
114
  foreach(sn_get_groups('resources_trader') as $resource_id) {
0 ignored issues
show
Comprehensibility Bug introduced by
$resource_id is overwriting a variable from outer foreach loop.
Loading history...
115
    if($resource_id == RES_DARK_MATTER) {
116
      $amount = floor(mrc_get_level($user, null, RES_DARK_MATTER) - $config->rpg_cost_trader);
117
    } else {
118
      $amount = floor(mrc_get_level($user, $planetrow, $resource_id));
0 ignored issues
show
Bug introduced by
It seems like mrc_get_level($user, $planetrow, $resource_id) can also be of type boolean; however, parameter $num of floor() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
      $amount = floor(/** @scrutinizer ignore-type */ mrc_get_level($user, $planetrow, $resource_id));
Loading history...
119
    }
120
121
    $template->assign_block_vars('resources', array(
122
      'ID'         => $resource_id,
123
      'NAME'       => $lang['tech'][$resource_id],
124
      'AVAIL'      => $amount,
125
      'AVAIL_TEXT' => HelperString::numberFloorAndFormat($amount),
126
      'SPENT'      => ($intError == MARKET_DEAL) ? 0 : max(0, $tradeList[$resource_id]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tradeList does not seem to be defined for all execution paths leading up to this point.
Loading history...
127
      'RATE'       => $rates[$resource_id],
128
    ));
129
  }
130
131
  $template->assign_vars(array(
132
    'rpg_cost_trader'   => $config->rpg_cost_trader,
133
    // 'message' => $message,
134
    'MODE' => $mode,
135
  ));
136
137
  SnTemplate::display($template, $page_title);
138
}
139