Completed
Push — trunk ( 5a98ee...c2d255 )
by SuperNova.WS
04:09
created

Ube4_1Prepare::prepareFromMissionArray()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 16
nop 3
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
ccs 0
cts 21
cp 0
crap 30
1
<?php
2
/**
3
 * Created by Gorlum 13.02.2018 8:17
4
 */
5
6
namespace Ube\Ube4_1;
7
8
use DBStaticPlanet;
9
10
class Ube4_1Prepare {
11
  const CONVERT_TECHS = [
12
    TECH_WEAPON => UBE_ATTACK,
13
    TECH_ARMOR  => UBE_ARMOR,
14
    TECH_SHIELD => UBE_SHIELD,
15
  ];
16
  const CONVERT_UNIT_PARAMS = [
17
    UBE_ATTACK => 'attack',
18
    UBE_ARMOR  => 'armor',
19
    UBE_SHIELD => 'shield',
20
  ];
21
22
  /**
23
   * Заполняет начальные данные по данным миссии
24
   *
25
   * @param $mission_data
26
   *
27
   * @return array
28
   */
29
  public function prepareFromMissionArray(&$mission_data, &$fleet_list_on_hold, $acs_fleet_list) {
30
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
31
    UBE_OPTIONS[UBE_LOADED]
32
    UBE_OPTIONS[UBE_SIMULATOR]
33
    UBE_OPTIONS[UBE_EXCHANGE]
34
    UBE_OPTIONS[UBE_MOON_WAS]
35
    */
36
37
    $fleet_row = &$mission_data['fleet'];
38
    $destination_planet = &$mission_data['dst_planet'];
39
40
    $ube_time = $fleet_row['fleet_start_time'];
41
    $combat_data = [
42
      UBE_TIME           => $ube_time,
43
      UBE_OBJ_PREPARATOR => $this,
44
    ];
45
    // TODO: Не допускать атаки игроком своих же флотов - т.е. холд против атаки
46
    // Готовим инфу по атакуемой планете
47
    $this->ube_attack_prepare_planet($combat_data, $destination_planet);
48
49
    // Готовим инфу по удержанию
50
    foreach ($fleet_list_on_hold as $fleet) {
51
      $this->ube_attack_prepare_fleet($combat_data, $fleet, false);
0 ignored issues
show
Deprecated Code introduced by
The function Ube\Ube4_1\Ube4_1Prepare..._attack_prepare_fleet() 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

51
      /** @scrutinizer ignore-deprecated */ $this->ube_attack_prepare_fleet($combat_data, $fleet, false);
Loading history...
52
    }
53
54
    // Готовим инфу по атакующим
55
    foreach ($acs_fleet_list as $fleet) {
56
      $this->ube_attack_prepare_fleet($combat_data, $fleet, true);
0 ignored issues
show
Deprecated Code introduced by
The function Ube\Ube4_1\Ube4_1Prepare..._attack_prepare_fleet() 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

56
      /** @scrutinizer ignore-deprecated */ $this->ube_attack_prepare_fleet($combat_data, $fleet, true);
Loading history...
57
    }
58
59
    // Готовим опции
60
    $combat_data[UBE_OPTIONS][UBE_MOON_WAS] = $destination_planet['planet_type'] == PT_MOON || is_array(DBStaticPlanet::db_planet_by_parent($destination_planet['id'], true, '`id`'));
61
    $combat_data[UBE_OPTIONS][UBE_MISSION_TYPE] = $fleet_row['fleet_mission'];
62
    global $config;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
63
    $combat_data[UBE_OPTIONS][UBE_METHOD] = $config->game_ube_method ? $config->game_ube_method : 0;
64
65
    $this->sn_ube_combat_prepare_first_round($combat_data);
66
67
    return $combat_data;
68
  }
69
70
  // ------------------------------------------------------------------------------------------------
71
  // Заполняет данные по игроку
72
  protected function ube_attack_prepare_player(&$combat_data, $player_id, $is_attacker) {
73
    if (!isset($combat_data[UBE_PLAYERS][$player_id])) {
74
      $combat_data[UBE_PLAYERS][$player_id] = [
75
        UBE_ATTACKER => $is_attacker,
76
      ];
77
      $player_info = &$combat_data[UBE_PLAYERS][$player_id];
78
79
      $player_data = db_user_by_id($player_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

79
      $player_data = /** @scrutinizer ignore-deprecated */ db_user_by_id($player_id, true);
Loading history...
80
      $player_info[UBE_NAME] = $player_data['username'];
81
      $player_info[UBE_AUTH_LEVEL] = $player_data['authlevel'];
82
      $combat_data[UBE_OPTIONS][UBE_COMBAT_ADMIN] = $combat_data[UBE_OPTIONS][UBE_COMBAT_ADMIN] || $player_data['authlevel']; // Участвует ли админ в бою?
83
      $player_info[UBE_PLAYER_DATA] = $player_data;
84
85
      $admiral_bonus = mrc_get_level($player_data, false, MRC_ADMIRAL) * get_unit_param(MRC_ADMIRAL, P_BONUS_VALUE) / 100;
0 ignored issues
show
Bug introduced by
$player_data of type false is incompatible with the type array expected by parameter $user of mrc_get_level(). ( Ignorable by Annotation )

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

85
      $admiral_bonus = mrc_get_level(/** @scrutinizer ignore-type */ $player_data, false, MRC_ADMIRAL) * get_unit_param(MRC_ADMIRAL, P_BONUS_VALUE) / 100;
Loading history...
Bug introduced by
false of type false is incompatible with the type array expected by parameter $planet of mrc_get_level(). ( Ignorable by Annotation )

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

85
      $admiral_bonus = mrc_get_level($player_data, /** @scrutinizer ignore-type */ false, MRC_ADMIRAL) * get_unit_param(MRC_ADMIRAL, P_BONUS_VALUE) / 100;
Loading history...
86
      foreach (static::CONVERT_TECHS as $unit_id => $ube_id) {
87
        $player_info[UBE_BONUSES][$ube_id] += mrc_get_level($player_data, [], $unit_id) * get_unit_param($unit_id, P_BONUS_VALUE) / 100 + $admiral_bonus;
88
      }
89
    } else {
90
      $combat_data[UBE_PLAYERS][$player_id][UBE_ATTACKER] = $combat_data[UBE_PLAYERS][$player_id][UBE_ATTACKER] || $is_attacker;
91
    }
92
  }
93
94
95
  /**
96
   * Заполняет данные по флоту
97
   *
98
   * Через жопу для сохранения обратной совместимости
99
   *
100
   * @param $combat_data
101
   * @param $fleet
102
   * @param $is_attacker
103
   *
104
   * @deprecated
105
   */
106
  public function ube_attack_prepare_fleet(&$combat_data, &$fleet, $is_attacker) {
107
    $fleet_owner_id = $fleet['fleet_owner'];
108
    $fleet_id = $fleet['fleet_id'];
109
110
    $this->ube_attack_prepare_player($combat_data, $fleet_owner_id, $is_attacker);
111
112
    $fleet_data = sys_unit_str2arr($fleet['fleet_array']);
113
114
    $combat_data[UBE_FLEETS][$fleet_id][UBE_OWNER] = $fleet_owner_id;
115
    $fleet_info = &$combat_data[UBE_FLEETS][$fleet_id];
116
    $fleet_info[UBE_FLEET_GROUP] = $fleet['fleet_group'];
117
    foreach ($fleet_data as $unit_id => $unit_count) {
118
      if (!$unit_count) {
119
        continue;
120
      }
121
122
      $unit_type = get_unit_param($unit_id, P_UNIT_TYPE);
123
      if ($unit_type == UNIT_SHIPS || $unit_type == UNIT_DEFENCE) {
124
        $fleet_info[UBE_COUNT][$unit_id] = $unit_count;
125
      }
126
    }
127
128
    $fleet_info[UBE_RESOURCES] = array(
129
      RES_METAL     => $fleet['fleet_resource_metal'],
130
      RES_CRYSTAL   => $fleet['fleet_resource_crystal'],
131
      RES_DEUTERIUM => $fleet['fleet_resource_deuterium'],
132
    );
133
134
    $fleet_info[UBE_PLANET] = array(
135
      // TODO: Брать имя и кэшировать ИД и имя планеты?
136
      PLANET_GALAXY => $fleet['fleet_start_galaxy'],
137
      PLANET_SYSTEM => $fleet['fleet_start_system'],
138
      PLANET_PLANET => $fleet['fleet_start_planet'],
139
      PLANET_TYPE   => $fleet['fleet_start_type'],
140
    );
141
142
    // Calling other functions in call-chain
143
    // TODO - THIS IS WRONG! YOU SHOULD NEVER DO THIS! THIS IS DIRTY HACK!
144
    sn_function_call('ube_attack_prepare_fleet', array(&$combat_data, &$fleet, $is_attacker));
145
  }
146
147
  /**
148
   * Заполняет данные по планете
149
   *
150
   * @param $combat_data
151
   * @param $planet
152
   */
153
  protected function ube_attack_prepare_planet(&$combat_data, &$planet) {
154
    $player_id = $planet['id_owner'];
155
156
    $this->ube_attack_prepare_player($combat_data, $player_id, false);
157
158
    $player = &$combat_data[UBE_PLAYERS][$player_id][UBE_PLAYER_DATA];
159
160
    $combat_data[UBE_FLEETS][0] = array(UBE_OWNER => $player_id);
161
    $fleet_info = &$combat_data[UBE_FLEETS][0];
162
163
    foreach (sn_get_groups('combat') as $unit_id) {
164
      if ($unit_count = mrc_get_level($player, $planet, $unit_id)) {
165
        $fleet_info[UBE_COUNT][$unit_id] = $unit_count;
166
      }
167
    }
168
169
    foreach (sn_get_groups('resources_loot') as $resource_id) {
170
      $fleet_info[UBE_RESOURCES][$resource_id] = floor(mrc_get_level($player, $planet, $resource_id));
0 ignored issues
show
Bug introduced by
It seems like mrc_get_level($player, $planet, $resource_id) can also be of type boolean; however, parameter $value of floor() 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

170
      $fleet_info[UBE_RESOURCES][$resource_id] = floor(/** @scrutinizer ignore-type */ mrc_get_level($player, $planet, $resource_id));
Loading history...
171
    }
172
173
    if ($fortifier_level = mrc_get_level($player, $planet, MRC_FORTIFIER)) {
174
      $fortifier_bonus = $fortifier_level * get_unit_param(MRC_FORTIFIER, P_BONUS_VALUE) / 100;
175
      foreach (Ube4_1Calc::BONUS_LIST as $ube_id) {
176
        $fleet_info[UBE_BONUSES][$ube_id] += $fortifier_bonus;
177
      }
178
    }
179
180
    $combat_data[UBE_OUTCOME][UBE_PLANET] = $fleet_info[UBE_PLANET] = array(
181
      PLANET_ID     => $planet['id'],
182
      PLANET_NAME   => $planet['name'],
183
      PLANET_GALAXY => $planet['galaxy'],
184
      PLANET_SYSTEM => $planet['system'],
185
      PLANET_PLANET => $planet['planet'],
186
      PLANET_TYPE   => $planet['planet_type'],
187
      PLANET_SIZE   => $planet['diameter'],
188
    );
189
190
    $combat_data[UBE_OPTIONS][UBE_DEFENDER_ACTIVE] = $player['onlinetime'] >= $combat_data[UBE_TIME] - PLAYER_INACTIVE_TIMEOUT;
191
  }
192
193
194
  public function sn_ube_simulator_fleet_converter($sym_attacker, $sym_defender) {
195
    $combat_data = [
196
      UBE_OPTIONS => [
197
//      UBE_SIMULATOR    => sys_get_param_int('simulator'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
198
        UBE_SIMULATOR    => 1,
199
        UBE_MISSION_TYPE => MT_ATTACK,
200
      ],
201
202
      UBE_PLAYERS => [],
203
204
      UBE_FLEETS => [],
205
206
      UBE_OBJ_PREPARATOR => $this,
207
    ];
208
209
    $this->sn_ube_simulator_fill_side($combat_data, $sym_defender, false);
210
    $this->sn_ube_simulator_fill_side($combat_data, $sym_attacker, true);
211
212
    $this->sn_ube_combat_prepare_first_round($combat_data);
213
214
    return $combat_data;
215
  }
216
217
  // ------------------------------------------------------------------------------------------------
218
  // Преобразовывает данные симулятора в данные для расчета боя
219
  protected function sn_ube_simulator_fill_side(&$combat_data, $side_info, $attacker, $player_id = -1) {
220
    $player_id = $player_id == -1 ? count($combat_data[UBE_PLAYERS]) : $player_id;
221
222
    foreach ($side_info as $fleet_data) {
223
      $combat_data[UBE_PLAYERS][$player_id][UBE_NAME] = $attacker ? 'Attacker' : 'Defender';
224
      $combat_data[UBE_PLAYERS][$player_id][UBE_ATTACKER] = $attacker;
225
226
      $combat_data[UBE_FLEETS][$player_id][UBE_OWNER] = $player_id;
227
      foreach ($fleet_data as $unit_id => $unit_count) {
228
        if (!$unit_count) {
229
          continue;
230
        }
231
232
        $unit_type = get_unit_param($unit_id, P_UNIT_TYPE);
233
234
        if ($unit_type == UNIT_SHIPS || $unit_type == UNIT_DEFENCE) {
235
          $combat_data[UBE_FLEETS][$player_id][UBE_COUNT][$unit_id] = $unit_count;
236
        } elseif ($unit_type == UNIT_RESOURCES) {
237
          $combat_data[UBE_FLEETS][$player_id][UBE_RESOURCES][$unit_id] = $unit_count;
238
        } elseif ($unit_type == UNIT_TECHNOLOGIES) {
239
          $combat_data[UBE_PLAYERS][$player_id][UBE_BONUSES][static::CONVERT_TECHS[$unit_id]] += $unit_count * get_unit_param($unit_id, P_BONUS_VALUE) / 100;
240
        } elseif ($unit_type == UNIT_GOVERNORS) {
241
          if ($unit_id == MRC_FORTIFIER) {
242
            foreach (static::CONVERT_TECHS as $ube_id) {
243
              $combat_data[UBE_FLEETS][$player_id][UBE_BONUSES][$ube_id] += $unit_count * get_unit_param($unit_id, P_BONUS_VALUE) / 100;
244
            }
245
          }
246
        } elseif ($unit_type == UNIT_MERCENARIES) {
247
          if ($unit_id == MRC_ADMIRAL) {
248
            foreach (static::CONVERT_TECHS as $ube_id) {
249
              $combat_data[UBE_PLAYERS][$player_id][UBE_BONUSES][$ube_id] += $unit_count * get_unit_param($unit_id, P_BONUS_VALUE) / 100;
250
            }
251
          }
252
        }
253
      }
254
    }
255
  }
256
257
  // ------------------------------------------------------------------------------------------------
258
  protected function sn_ube_combat_prepare_first_round(&$combat_data) {
259
    // Готовим информацию для первого раунда - проводим все нужные вычисления из исходных данных
260
    $first_round_data = array();
261
    foreach ($combat_data[UBE_FLEETS] as $fleet_id => &$fleet_info) {
262
      $fleet_info[UBE_COUNT] = is_array($fleet_info[UBE_COUNT]) ? $fleet_info[UBE_COUNT] : array();
263
      $player_data = &$combat_data[UBE_PLAYERS][$fleet_info[UBE_OWNER]];
264
      $fleet_info[UBE_FLEET_TYPE] = $player_data[UBE_ATTACKER] ? UBE_ATTACKERS : UBE_DEFENDERS;
265
266
      foreach (Ube4_1Calc::BONUS_LIST as $bonus_id => $bonus_value) {
267
        // Вычисляем бонус игрока
268
        $bonus_value = isset($player_data[UBE_BONUSES][$bonus_id]) ? $player_data[UBE_BONUSES][$bonus_id] : 0;
269
        // Добавляем к бонусам флота бонусы игрока
270
        $fleet_info[UBE_BONUSES][$bonus_id] += $bonus_value;
271
      }
272
273
      $first_round_data[$fleet_id][UBE_COUNT] = $fleet_info[UBE_PRICE] = array();
274
      foreach ($fleet_info[UBE_COUNT] as $unit_id => $unit_count) {
275
        if ($unit_count <= 0) {
276
          continue;
277
        }
278
279
        $unit_info = get_unit_param($unit_id);
280
        // Заполняем информацию о кораблях в информации флота
281
        foreach (Ube4_1Calc::BONUS_LIST as $bonus_id => $bonus_value) {
282
          $fleet_info[$bonus_id][$unit_id] = floor($unit_info[static::CONVERT_UNIT_PARAMS[$bonus_id]] * (1 + $fleet_info[UBE_BONUSES][$bonus_id]));
283
        }
284
        $fleet_info[UBE_AMPLIFY][$unit_id] = $unit_info[P_AMPLIFY];
285
        // TODO: Переделать через get_ship_data()
286
        $fleet_info[UBE_CAPACITY][$unit_id] = $unit_info[P_CAPACITY];
287
        $fleet_info[UBE_TYPE][$unit_id] = $unit_info[P_UNIT_TYPE];
288
        // TODO: Переделать через список ресурсов
289
        $fleet_info[UBE_PRICE][RES_METAL]    [$unit_id] = $unit_info[P_COST][RES_METAL];
290
        $fleet_info[UBE_PRICE][RES_CRYSTAL]  [$unit_id] = $unit_info[P_COST][RES_CRYSTAL];
291
        $fleet_info[UBE_PRICE][RES_DEUTERIUM][$unit_id] = $unit_info[P_COST][RES_DEUTERIUM];
292
        $fleet_info[UBE_PRICE][RES_DARK_MATTER][$unit_id] = $unit_info[P_COST][RES_DARK_MATTER];
293
294
        // Копируем её в информацию о первом раунде
295
        $first_round_data[$fleet_id][UBE_ARMOR][$unit_id] = $fleet_info[UBE_ARMOR][$unit_id] * $unit_count;
296
        $first_round_data[$fleet_id][UBE_COUNT][$unit_id] = $unit_count;
297
        $first_round_data[$fleet_id][UBE_ARMOR_REST][$unit_id] = $fleet_info[UBE_ARMOR][$unit_id];
298
        $first_round_data[$fleet_id][UBE_SHIELD_REST][$unit_id] = $fleet_info[UBE_SHIELD][$unit_id];
299
      }
300
    }
301
    $combat_data[UBE_ROUNDS][0][UBE_FLEETS] = $first_round_data;
302
    $combat_data[UBE_ROUNDS][1][UBE_FLEETS] = $first_round_data;
303
  }
304
305
}
306