Completed
Push — trunk ( 1f2f20...9c1015 )
by SuperNova.WS
04:43
created

artifacts.php ➔ art_buy()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 4
nop 4
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
* artifacts.php
5
* Artifact actions
6
*
7
* @package roleplay
8
* @version 1.0
9
*
10
* Revision History
11
* ================
12
* 1.0 copyright (c) 2011 by Gorlum for http://supernova.ws
13
*
14
*/
15
16
global $lang, $user, $planetrow;
17
18
include('common.' . substr(strrchr(__FILE__, '.'), 1));
19
20
lng_include('infos');
21
lng_include('artifacts');
22
23
include('includes/includes/art_artifact.php');
24
25
$sn_group_artifacts = sn_get_groups('artifacts');
26
27
/**
28
 * @param $user
29
 * @param $unit_id
30
 * @param $planetrow
31
 * @param $lang
32
 *
33
 * @return string
34
 */
35
function art_buy($user, $unit_id, $planetrow, $lang) {
36
  $Message = '';
37
  sn_db_transaction_start();
38
39
  $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.

This function has been deprecated.

Loading history...
40
  $artifact_level = mrc_get_level($user, array(), $unit_id, true);
41
42
  $build_data = eco_get_build_data($user, $planetrow, $unit_id, $artifact_level, true);
43
  $darkmater_cost = $build_data[BUILD_CREATE][RES_DARK_MATTER];
44
45
  // TODO: more correct check - with "FOR UPDATE"
46
  if (mrc_get_level($user, null, RES_DARK_MATTER) >= $darkmater_cost) {
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
    $unit_max_stack = get_unit_param($unit_id, P_MAX_STACK);
48
    if (!isset($unit_max_stack) || $unit_max_stack > mrc_get_level($user, $planetrow, $unit_id)) {
49
      if (!DBStaticUnit::dbUserAdd($user['id'], $unit_id, 1)) {
50
        $Message = '{Ошибка записи в БД}';
51
      } else {
52
        rpg_points_change($user['id'], RPG_ARTIFACT, -($darkmater_cost), "Spent for artifact {$lang['tech'][$unit_id]} ID {$unit_id}");
53
        sn_db_transaction_commit();
54
        sys_redirect("artifacts.php#{$unit_id}");
55
      }
56
    } else {
57
      $Message = $lang['off_maxed_out'];
58
    }
59
  } else {
60
    $Message = $lang['sys_no_points'];
61
  }
62
  sn_db_transaction_rollback();
63
64
  return $Message;
65
}
66
67
if(($action = sys_get_param_int('action')) && in_array($unit_id = sys_get_param_int('unit_id'), $sn_group_artifacts))
68
{
69
  $Message = '';
70
  switch($action)
71
  {
72
    case ACTION_BUY:
73
      $Message = art_buy($user, $unit_id, $planetrow, $lang);
74
    break;
75
76
    case ACTION_USE:
77
      art_use($user, $planetrow, $unit_id);
78
      sys_redirect("artifacts.php#{$unit_id}");
79
    break;
80
  }
81
  messageBox($Message, $lang['tech'][UNIT_ARTIFACTS], 'artifacts.' . PHP_EX, 5);
82
}
83
84
$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.

This function has been deprecated.

Loading history...
85
86
$template = gettemplate('artifacts', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a object<template>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
88
foreach($sn_group_artifacts as $artifact_id)
89
{
90
  $artifact_level = mrc_get_level($user, [], $artifact_id, true);
91
  $build_data = eco_get_build_data($user, $planetrow, $artifact_id, $artifact_level);
92
    $artifact_data = get_unit_param($artifact_id);
93
    $artifact_data_bonus = tpl_render_unit_bonus_data($artifact_data);
94
95
    $template->assign_block_vars('artifact', array(
96
      'ID'          => $artifact_id,
97
      'NAME'        => $lang['tech'][$artifact_id],
98
      'DESCRIPTION' => $lang['info'][$artifact_id]['description'],
99
      'EFFECT'      => $lang['info'][$artifact_id]['effect'],
100
      'COST'        => $build_data[BUILD_CREATE][RES_DARK_MATTER],
101
      'COST_TEXT'   => HelperString::numberFloorAndFormat($build_data[BUILD_CREATE][RES_DARK_MATTER]),
102
      'LEVEL'       => intval($artifact_level),
103
      'LEVEL_MAX'   => intval($artifact_data['max']),
104
      'BONUS'       => $artifact_data_bonus,
105
      'BONUS_TYPE'  => $artifact_data['bonus_type'],
106
      'CAN_BUY'     => $build_data['CAN'][BUILD_CREATE],
107
    ));
108
}
109
110
$template->assign_vars(array(
111
  'PAGE_HEADER' => $lang['tech'][UNIT_ARTIFACTS],
112
  'PAGE_HINT' => $lang['art_page_hint'],
113
));
114
115
display($template, $lang['tech'][UNIT_ARTIFACTS]);
116