Issues (1369)

classes/Pages/Deprecated/PageMercenary.php (7 issues)

Labels
1
<?php
2
/**
3
 * Created by Gorlum 30.09.2017 8:28
4
 */
5
6
namespace Pages\Deprecated;
7
8
use DBAL\db_mysql;
9
use \Exception;
0 ignored issues
show
The type \Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use \SN;
0 ignored issues
show
The type \SN was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SnTemplate;
12
use Unit\DBStaticUnit;
13
use \template;
0 ignored issues
show
The type \template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class PageMercenary {
16
17
  /**
18
   * @var \classConfig $config
19
   */
20
  protected $config;
21
  /**
22
   * @var \classLocale $lang
23
   */
24
  protected $lang;
25
26
  /**
27
   * @var float[] $sn_powerup_buy_discounts
28
   */
29
  protected $sn_powerup_buy_discounts;
30
31
  /**
32
   * What we purchasing - Plans or Mercenaries?
33
   * @var int $mode
34
   */
35
  protected $mode = UNIT_MERCENARIES; // Or UNIT_PLANS
36
37
  /**
38
   * If purchased units are permanent?
39
   * @var bool $isUnitsPermanent
40
   */
41
  protected $isUnitsPermanent = false;
42
43
  /**
44
   * Multiplier for Alliance's purchases
45
   *
46
   * @var float $cost_alliance_multiplier
47
   */
48
  protected $cost_alliance_multiplier;
49
50
  public function __construct() {
51
    global $lang, $sn_powerup_buy_discounts;
52
53
    lng_include('mrc_mercenary');
54
    lng_include('infos');
55
56
    $this->config = SN::$config;
57
    $this->lang = $lang;
58
    $this->sn_powerup_buy_discounts = $sn_powerup_buy_discounts;
59
60
    $this->loadParams();
61
  }
62
63
  protected function loadParams() {
64
    // Getting page mode
65
    $this->mode = sys_get_param_int('mode', UNIT_MERCENARIES);
66
    $this->mode = in_array($this->mode, array(UNIT_MERCENARIES, UNIT_PLANS)) ? $this->mode : UNIT_MERCENARIES;
67
68
    $this->isUnitsPermanent = $this->mode == UNIT_PLANS || !$this->config->empire_mercenary_temporary;
69
    $this->cost_alliance_multiplier = min(1, defined('SN_IN_ALLY') && (SN_IN_ALLY === true) && $this->mode == UNIT_PLANS ? $this->config->ali_bonus_members : 1);
70
  }
71
72
73
  /**
74
   * @param array $user
75
   */
76
  public function mrc_mercenary_render($user) {
77
    $template = SnTemplate::gettemplate('mrc_mercenary_hire');
78
79
    $operation_result = $this->modelMercenaryHire($user);
80
    if (!empty($operation_result)) {
81
      $template->assign_block_vars('result', $operation_result);
82
    }
83
84
    $this->fillDiscountTable($template);
85
86
    $user_dark_matter = mrc_get_level($user, [], RES_DARK_MATTER);
87
    foreach (sn_get_groups($this->mode == UNIT_PLANS ? 'plans' : 'mercenaries') as $mercenary_id) {
88
      $mercenary = get_unit_param($mercenary_id);
89
90
      $mercenary_level = mrc_get_level($user, [], $mercenary_id, false, true);
91
      $mercenary_level_bonus = max(0, mrc_get_level($user, [], $mercenary_id) - $mercenary_level);
92
93
      $currentUnitCostDM = 0;
94
      if ($this->isUnitsPermanent) {
95
        $currentUnitCostDM = eco_get_total_cost($mercenary_id, $mercenary_level);
96
        $currentUnitCostDM = $currentUnitCostDM[BUILD_CREATE][RES_DARK_MATTER] * $this->cost_alliance_multiplier;
97
      }
98
      $nextLevelCostData = eco_get_total_cost($mercenary_id, $mercenary_level + 1);
99
      $nextLevelCostDM = $nextLevelCostData[BUILD_CREATE][RES_DARK_MATTER] * $this->cost_alliance_multiplier;
100
101
      $mercenary_unit = DBStaticUnit::db_unit_by_location($user['id'], LOC_USER, $user['id'], $mercenary_id);
102
      $mercenary_time_start = strtotime($mercenary_unit['unit_time_start']);
103
      $mercenary_time_finish = strtotime($mercenary_unit['unit_time_finish']);
104
      $unitIsOutdated = $mercenary_time_finish && $mercenary_time_finish >= SN_TIME_NOW;
105
      $template->assign_block_vars('officer', array(
106
        'ID'                => $mercenary_id,
107
        'NAME'              => $this->lang['tech'][$mercenary_id],
108
        'DESCRIPTION'       => $this->lang['info'][$mercenary_id]['description'],
109
        'EFFECT'            => $this->lang['info'][$mercenary_id]['effect'],
110
        'COST'              => $nextLevelCostDM - $currentUnitCostDM,
111
        'COST_TEXT'         => prettyNumberStyledCompare($nextLevelCostDM - $currentUnitCostDM, $user_dark_matter),
0 ignored issues
show
It seems like $user_dark_matter can also be of type boolean; however, parameter $compareTo of prettyNumberStyledCompare() 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

111
        'COST_TEXT'         => prettyNumberStyledCompare($nextLevelCostDM - $currentUnitCostDM, /** @scrutinizer ignore-type */ $user_dark_matter),
Loading history...
112
        'LEVEL'             => $mercenary_level,
113
        'LEVEL_BONUS'       => $mercenary_level_bonus,
114
        'LEVEL_MAX'         => $mercenary['max'],
115
        'BONUS'             => SnTemplate::tpl_render_unit_bonus_data($mercenary),
116
        'BONUS_TYPE'        => $mercenary[P_BONUS_TYPE],
117
        'HIRE_END'          => $unitIsOutdated ? date(FMT_DATE_TIME, $mercenary_time_finish) : '',
118
        'HIRE_LEFT_PERCENT' => $unitIsOutdated ? round(($mercenary_time_finish - SN_TIME_NOW) / ($mercenary_time_finish - $mercenary_time_start) * 100, 1) : 0,
119
        'CAN_BUY'           => $this->mrc_officer_accessible($user, $mercenary_id),
120
      ));
121
122
      $this->renderMercenaryLevelsAvail($user_dark_matter, $mercenary_id, $currentUnitCostDM, $template, $mercenary_level, $mercenary['max']);
0 ignored issues
show
It seems like $user_dark_matter can also be of type boolean; however, parameter $user_dark_matter of Pages\Deprecated\PageMer...rMercenaryLevelsAvail() does only seem to accept double, 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

122
      $this->renderMercenaryLevelsAvail(/** @scrutinizer ignore-type */ $user_dark_matter, $mercenary_id, $currentUnitCostDM, $template, $mercenary_level, $mercenary['max']);
Loading history...
It seems like $mercenary_level can also be of type boolean and double; however, parameter $mercenary_level of Pages\Deprecated\PageMer...rMercenaryLevelsAvail() does only seem to accept 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

122
      $this->renderMercenaryLevelsAvail($user_dark_matter, $mercenary_id, $currentUnitCostDM, $template, /** @scrutinizer ignore-type */ $mercenary_level, $mercenary['max']);
Loading history...
123
124
      $this->renderMercenaryReq($user, $mercenary, $template);
125
    }
126
127
    $template->assign_vars(array(
128
      'PAGE_HEADER'                => $this->lang['tech'][$this->mode],
129
      'MODE'                       => $this->mode,
130
      'IS_PERMANENT'               => intval($this->isUnitsPermanent),
131
      'EMPIRE_MERCENARY_TEMPORARY' => $this->config->empire_mercenary_temporary,
132
      'DARK_MATTER'                => $user_dark_matter,
133
    ));
134
135
    SnTemplate::display($template, $this->lang['tech'][$this->mode]);
136
  }
137
138
  protected function mrc_mercenary_hire($user, $mercenary_id) {
139
    if (!in_array($mercenary_id, sn_get_groups($this->mode == UNIT_PLANS ? 'plans' : 'mercenaries'))) {
140
      throw new Exception('mrc_msg_error_wrong_mercenary', ERR_ERROR);
141
    }
142
143
    if (!$this->mrc_officer_accessible($user, $mercenary_id)) {
144
      throw new Exception('mrc_msg_error_requirements', ERR_ERROR);
145
    }
146
147
    $mercenary_level = sys_get_param_int('mercenary_level');
148
    if ($mercenary_level < 0 || $mercenary_level > get_unit_param($mercenary_id, P_MAX_STACK)) {
149
      throw new Exception('mrc_msg_error_wrong_level', ERR_ERROR);
150
    }
151
152
    $mercenary_period = sys_get_param_int('mercenary_period');
153
    if ($mercenary_level && !array_key_exists($mercenary_period, $this->sn_powerup_buy_discounts)) {
154
      throw new Exception('mrc_msg_error_wrong_period', ERR_ERROR);
155
    }
156
157
    db_mysql::db_transaction_start();
158
159
    $mercenary_level_old = mrc_get_level($user, [], $mercenary_id, true, true);
160
    if ($this->config->empire_mercenary_temporary && $mercenary_level_old && $mercenary_level) {
161
      throw new Exception('mrc_msg_error_already_hired', ERR_ERROR); // Can't hire already hired temp mercenary - dismiss first
162
    } elseif ($this->config->empire_mercenary_temporary && !$mercenary_level_old && !$mercenary_level) {
163
      throw new Exception('', ERR_NONE); // Can't dismiss (!$mercenary_level) not hired (!$mercenary_level_old) temp mercenary. But no error
164
    }
165
166
    if ($mercenary_level) {
167
      $darkmater_cost = eco_get_total_cost($mercenary_id, $mercenary_level);
168
      if (!$this->config->empire_mercenary_temporary && $mercenary_level_old) {
169
        $darkmater_cost_old = eco_get_total_cost($mercenary_id, $mercenary_level_old);
170
        $darkmater_cost[BUILD_CREATE][RES_DARK_MATTER] -= $darkmater_cost_old[BUILD_CREATE][RES_DARK_MATTER];
171
      }
172
      $darkmater_cost = ceil($darkmater_cost[BUILD_CREATE][RES_DARK_MATTER] * $mercenary_period * $this->sn_powerup_buy_discounts[$mercenary_period] / $this->config->empire_mercenary_base_period);
173
    } else {
174
      $darkmater_cost = 0;
175
    }
176
    $darkmater_cost *= $this->cost_alliance_multiplier;
177
178
    if (mrc_get_level($user, [], RES_DARK_MATTER) < $darkmater_cost) {
179
      throw new Exception('mrc_msg_error_no_resource', ERR_ERROR);
180
    }
181
182
    $this->mercenaryDismiss($user, $mercenary_id, $darkmater_cost, $mercenary_level);
183
184
    if ($darkmater_cost && $mercenary_level) {
185
      DBStaticUnit::db_unit_set_insert(
186
        "unit_player_id = {$user['id']},
187
        unit_location_type = " . LOC_USER . ",
188
        unit_location_id = {$user['id']},
189
        unit_type = {$this->mode},
190
        unit_snid = {$mercenary_id},
191
        unit_level = {$mercenary_level},
192
        unit_time_start = " . (!$this->isUnitsPermanent ? 'FROM_UNIXTIME(' . SN_TIME_NOW . ')' : 'null') . ",
193
        unit_time_finish = " . (!$this->isUnitsPermanent ? 'FROM_UNIXTIME(' . (SN_TIME_NOW + $mercenary_period) . ')' : 'null')
194
      );
195
196
      rpg_points_change($user['id'], $this->mode == UNIT_PLANS ? RPG_PLANS : RPG_MERCENARY, -($darkmater_cost),
197
        sprintf($this->lang[$this->mode == UNIT_PLANS ? 'mrc_plan_bought_log' : 'mrc_mercenary_hired_log'], $this->lang['tech'][$mercenary_id], $mercenary_id, $darkmater_cost, round($mercenary_period / PERIOD_DAY)));
198
    }
199
    db_mysql::db_transaction_commit();
200
    sys_redirect($_SERVER['REQUEST_URI']);
201
  }
202
203
  protected function mrc_officer_accessible(&$user, $mercenary_id) {
204
    $mercenary_info = get_unit_param($mercenary_id);
205
206
    if ($this->config->empire_mercenary_temporary || $mercenary_info[P_UNIT_TYPE] == UNIT_PLANS) {
207
      return true;
208
    }
209
210
    return eco_can_build_unit($user, [], $mercenary_id) == BUILD_ALLOWED;
211
  }
212
213
  /**
214
   * @param \template $template
215
   */
216
  protected function fillDiscountTable($template) {
217
    foreach ($this->sn_powerup_buy_discounts as $hire_period => $hire_discount) {
218
      $template->assign_block_vars('period', array(
219
        'LENGTH'   => $hire_period,
220
        'TEXT'     => $this->lang['mrc_period_list'][$hire_period],
221
        'DISCOUNT' => $hire_period / $this->config->empire_mercenary_base_period * $hire_discount,
222
        'SELECTED' => $hire_period == $this->config->empire_mercenary_base_period,
223
      ));
224
    }
225
  }
226
227
  /**
228
   * @param $user
229
   *
230
   * @return array
231
   */
232
  protected function modelMercenaryHire($user) {
233
    $operation_result = [];
234
    if ($mercenary_id = sys_get_param_int('mercenary_id')) {
235
      try {
236
        $this->mrc_mercenary_hire($user, $mercenary_id);
237
      } catch (Exception $e) {
238
        db_mysql::db_transaction_rollback();
239
        $operation_result = array(
240
          'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
241
          'MESSAGE' => $this->lang[$e->getMessage()],
242
        );
243
      }
244
    }
245
246
    return $operation_result;
247
  }
248
249
  /**
250
   * @param array $user
251
   * @param int   $mercenary_id
252
   * @param float $darkmater_cost
253
   * @param int   $mercenary_level
254
   */
255
  protected function mercenaryDismiss($user, $mercenary_id, $darkmater_cost, $mercenary_level) {
256
    if ((!$darkmater_cost || !$mercenary_level) && $this->isUnitsPermanent) {
257
      return;
258
    }
259
260
    $unit_row = DBStaticUnit::db_unit_by_location($user['id'], LOC_USER, $user['id'], $mercenary_id);
261
    if (is_array($unit_row) && ($dismiss_left_days = floor((strtotime($unit_row['unit_time_finish']) - SN_TIME_NOW) / PERIOD_DAY))) {
262
      $dismiss_full_cost = eco_get_total_cost($mercenary_id, $unit_row['unit_level']);
263
      $dismiss_full_cost = $dismiss_full_cost[BUILD_CREATE][RES_DARK_MATTER];
264
265
      $dismiss_full_days = round((strtotime($unit_row['unit_time_finish']) - strtotime($unit_row['unit_time_start'])) / PERIOD_DAY);
266
      rpg_points_change($user['id'], RPG_MERCENARY_DISMISSED, 0,
267
        sprintf($this->lang['mrc_mercenary_dismissed_log'], $this->lang['tech'][$mercenary_id], $mercenary_id, $dismiss_full_cost, $dismiss_full_days,
0 ignored issues
show
sprintf($this->lang['mrc... / $dismiss_full_days)) of type string is incompatible with the type boolean expected by parameter $comment of rpg_points_change(). ( Ignorable by Annotation )

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

267
        /** @scrutinizer ignore-type */ sprintf($this->lang['mrc_mercenary_dismissed_log'], $this->lang['tech'][$mercenary_id], $mercenary_id, $dismiss_full_cost, $dismiss_full_days,
Loading history...
268
          $unit_row['unit_time_start'], $unit_row['unit_time_finish'], $dismiss_left_days, floor($dismiss_full_cost * $dismiss_left_days / $dismiss_full_days)
269
        ));
270
    }
271
    DBStaticUnit::db_unit_list_delete($user['id'], LOC_USER, $user['id'], $mercenary_id);
272
  }
273
274
  /**
275
   * @param           $user
276
   * @param           $mercenary
277
   * @param template  $template
278
   */
279
  protected function renderMercenaryReq(&$user, $mercenary, $template) {
280
    if (empty($mercenary[P_REQUIRE]) || !is_array($mercenary[P_REQUIRE])) {
281
      return;
282
    }
283
284
    foreach ($mercenary[P_REQUIRE] as $requireUnitId => $requireLevel) {
285
      $template->assign_block_vars('officer.require', $q = [
286
        'ID'             => $requireUnitId,
287
        'LEVEL_GOT'      => mrc_get_level($user, [], $requireUnitId),
288
        'LEVEL_REQUIRED' => $requireLevel,
289
        'NAME'           => \HelperString::htmlSafe($this->lang['tech'][$requireUnitId])
290
      ]);
291
    }
292
  }
293
294
  /**
295
   * @param float    $user_dark_matter
296
   * @param int      $mercenary_id
297
   * @param float    $currentCost
298
   * @param template $template
299
   * @param int      $mercenary_level
300
   * @param          $mercenary_max_level
301
   */
302
  protected function renderMercenaryLevelsAvail($user_dark_matter, $mercenary_id, $currentCost, $template, $mercenary_level, $mercenary_max_level) {
303
    $upgrade_cost = 1;
304
    for (
305
      $i = $this->config->empire_mercenary_temporary ? 1 : $mercenary_level + 1;
306
      $mercenary_max_level ? ($i <= $mercenary_max_level) : $upgrade_cost <= $user_dark_matter;
307
      $i++
308
    ) {
309
      $newCost = eco_get_total_cost($mercenary_id, $i);
310
      $upgrade_cost = $newCost[BUILD_CREATE][RES_DARK_MATTER] * $this->cost_alliance_multiplier - $currentCost;
311
      $template->assign_block_vars('officer.level', array(
312
        'VALUE' => $i,
313
        'PRICE' => $upgrade_cost,
314
      ));
315
    }
316
  }
317
318
}
319