Completed
Push — work-fleets ( 04acf9...8f8df9 )
by SuperNova.WS
07:02
created

UBEMoonCalculator::report_generate_sql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
1
<?php
2
3
class UBEMoonCalculator {
4
  /**
5
   * @var int
6
   */
7
  protected $status = UBE_MOON_NONE;
8
  /**
9
   * @var string
10
   */
11
  protected $moon_name = ''; // TODO - UNUSED! USE IT, LUKE!
12
  /**
13
   * @var int
14
   */
15
  protected $create_chance = 0;
16
  /**
17
   * @var int
18
   */
19
  protected $moon_diameter = 0;
20
21
  /**
22
   * Что случилось с кораблями, которые могли уничтожить луну
23
   *
24
   * @var int
25
   */
26
  protected $reapers_status = UBE_MOON_REAPERS_NONE;
27
28
  /**
29
   * Шанс уничтожить луну
30
   *
31
   * @var int
32
   */
33
  protected $destroy_chance = 0;
34
35
  /**
36
   * Шанс взрыва кораблей, уничтожающих луну
37
   *
38
   * @var int
39
   */
40
  protected $reaper_die_chance = 0;
41
42
  public function __construct() {
43
    $this->status = UBE_MOON_NONE;
44
    $this->moon_name = '';
45
    $this->create_chance = 0;
46
    $this->moon_diameter = 0;
47
    $this->reapers_status = UBE_MOON_REAPERS_NONE;
48
    $this->destroy_chance = 0;
49
    $this->reaper_die_chance = 0;
50
  }
51
52
  public function load_from_report($report_row) {
53
    $this->status = $report_row['ube_report_moon'];
54
    $this->moon_name = ''; // TODO - load name
55
    $this->create_chance = $report_row['ube_report_moon_chance'];
56
    $this->moon_diameter = $report_row['ube_report_moon_size'];
57
    $this->reapers_status = $report_row['ube_report_moon_reapers'];
58
    $this->destroy_chance = $report_row['ube_report_moon_destroy_chance'];
59
    $this->reaper_die_chance = $report_row['ube_report_moon_reapers_die_chance'];
60
  }
61
62
  /**
63
   * @param UBEDebris $debris
64
   * @param bool      $is_simulator
65
   *
66
   * @version 2016-02-25 23:42:45 41a4.68
67
   */
68
  protected function moon_create_try(UBEDebris $debris, $is_simulator = false) {
69
    $this->status = UBE_MOON_NONE;
70
71
    $debris_for_moon = $debris->debris_total();
72
73
    if(!$debris_for_moon) {
74
      return;
75
    }
76
77
    // TODO uni_calculate_moon_chance
78
    $moon_chance = min($debris_for_moon / UBE_MOON_DEBRIS_PER_PERCENT, UBE_MOON_PERCENT_MAX); // TODO Configure
79
    $moon_chance = $moon_chance >= UBE_MOON_PERCENT_MIN ? $moon_chance : 0;
80
    $this->create_chance = $moon_chance;
0 ignored issues
show
Documentation Bug introduced by
It seems like $moon_chance can also be of type double. However, the property $create_chance is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
    if($moon_chance) {
82
      if($is_simulator || mt_rand(1, 100) <= $moon_chance) {
83
        $this->status = UBE_MOON_CREATE_SUCCESS;
84
        $this->moon_diameter = round($is_simulator ? $moon_chance * 150 + 1999 : mt_rand($moon_chance * 100 + 1000, $moon_chance * 200 + 2999));
0 ignored issues
show
Documentation Bug introduced by
The property $moon_diameter was declared of type integer, but round($is_simulator ? $m...n_chance * 200 + 2999)) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
85
86
        if($debris_for_moon <= UBE_MOON_DEBRIS_MAX_SPENT) {
87
          $debris->_reset();
88
        } else {
89
          $moon_debris_left_percent = ($debris_for_moon - UBE_MOON_DEBRIS_MAX_SPENT) / $debris_for_moon;
90
          $debris->debris_adjust_proportional($moon_debris_left_percent);
91
        }
92
      } else {
93
        $this->status = UBE_MOON_CREATE_FAILED;
94
      }
95
    }
96
  }
97
98
  /**
99
   * @param int $reapers
100
   *
101
   * @version 2016-02-25 23:42:45 41a4.68
102
   */
103
  protected function moon_destroy_try($reapers) {
104
    // TODO: $is_simulator
105
    if($reapers <= 0) {
106
      return;
107
    }
108
109
    $random = mt_rand(1, 100);
110
    $this->destroy_chance = max(1, min(99, round((100 - sqrt($this->moon_diameter)) * sqrt($reapers))));
0 ignored issues
show
Documentation Bug introduced by
It seems like max(1, min(99, round((10...r)) * sqrt($reapers)))) can also be of type double. However, the property $destroy_chance is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
111
    $this->reaper_die_chance = round(sqrt($this->moon_diameter) / 2 + sqrt($reapers));
0 ignored issues
show
Documentation Bug introduced by
The property $reaper_die_chance was declared of type integer, but round(sqrt($this->moon_d...) / 2 + sqrt($reapers)) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
112
    $this->status = $random <= $this->destroy_chance ? UBE_MOON_DESTROY_SUCCESS : UBE_MOON_DESTROY_FAILED;
113
    $random = mt_rand(1, 100);
114
    $this->reapers_status = $random <= $this->reaper_die_chance ? UBE_MOON_REAPERS_DIED : UBE_MOON_REAPERS_RETURNED;
115
  }
116
117
  /**
118
   * @param UBE $ube
119
   *
120
   * @version 2016-02-25 23:42:45 41a4.68
121
   */
122
  public function calculate_moon(UBE $ube) {
123
    if(UBE_MOON_EXISTS == $this->status) {
124
      if($ube->mission_type_id == MT_DESTROY && $ube->combat_result == UBE_COMBAT_RESULT_WIN) {
125
        $reapers = $ube->fleet_list->ube_calculate_attack_reapers();
126
        $this->moon_destroy_try($reapers);
127
      }
128
    } else {
129
      $this->moon_create_try($ube->debris, $ube->is_simulator);
130
    }
131
  }
132
133
  /**
134
   * @return array
135
   */
136
  public function report_generate_array() {
137
    return array(
138
      'ube_report_moon'                    => (int)$this->status,
139
      'ube_report_moon_chance'             => (int)$this->create_chance,
140
      'ube_report_moon_size'               => (float)$this->moon_diameter,
141
      'ube_report_moon_reapers'            => (int)$this->reapers_status,
142
      'ube_report_moon_destroy_chance'     => (int)$this->destroy_chance,
143
      'ube_report_moon_reapers_die_chance' => (int)$this->reaper_die_chance,
144
    );
145
  }
146
147
  public function report_render_moon() {
148
    return array(
149
      // Data
150
      'UBE_MOON' => $this->status,
151
      'UBE_MOON_CHANCE' => round($this->create_chance, 2),
152
      'UBE_MOON_SIZE' => $this->moon_diameter,
153
      'UBE_MOON_REAPERS' => $this->reapers_status,
154
      'UBE_MOON_DESTROY_CHANCE' => $this->destroy_chance,
155
      'UBE_MOON_REAPERS_DIE_CHANCE' => $this->reaper_die_chance,
156
157
      // Constants
158
      'UBE_MOON_EXISTS' => UBE_MOON_EXISTS,
159
      'UBE_MOON_NONE' => UBE_MOON_NONE,
160
      'UBE_MOON_CREATE_SUCCESS' => UBE_MOON_CREATE_SUCCESS,
161
      'UBE_MOON_CREATE_FAILED' => UBE_MOON_CREATE_FAILED,
162
      'UBE_MOON_REAPERS_NONE' => UBE_MOON_REAPERS_NONE,
163
      'UBE_MOON_DESTROY_SUCCESS' => UBE_MOON_DESTROY_SUCCESS,
164
      'UBE_MOON_REAPERS_RETURNED' => UBE_MOON_REAPERS_RETURNED,
165
    );
166
  }
167
168
  /**
169
   * @param $planet_info
170
   * @param $destination_user_id
171
   * @param $planet_id
172
   */
173
  public function db_apply_result($planet_info, $destination_user_id) {
174
    if($this->status == UBE_MOON_CREATE_SUCCESS) {
175
      $moon_row = uni_create_moon($planet_info[PLANET_GALAXY], $planet_info[PLANET_SYSTEM], $planet_info[PLANET_PLANET], $destination_user_id, $this->moon_diameter, '', false);
176
      $this->moon_name = $moon_row['name'];
177
      unset($moon_row);
178
    } elseif($this->status == UBE_MOON_DESTROY_SUCCESS) {
179
      DBStaticPlanet::db_planet_delete_by_id($planet_info[PLANET_ID]);
180
    }
181
  }
182
183
  public function message_generate(UBE $ube) {
184
    $classLocale = classLocale::$lang;
185
186
    $text_defender = '';
187
    if($this->status == UBE_MOON_CREATE_SUCCESS) {
188
      $text_defender .= "{$classLocale['ube_report_moon_created']} {$this->moon_diameter} {$classLocale['sys_kilometers_short']}<br /><br />";
189
    } elseif($this->status == UBE_MOON_CREATE_FAILED) {
190
      $text_defender .= "{$classLocale['ube_report_moon_chance']} {$this->create_chance}%<br /><br />";
191
    }
192
193
    if($ube->mission_type_id == MT_DESTROY) {
194
      if($this->reapers_status == UBE_MOON_REAPERS_NONE) {
195
        $text_defender .= classLocale::$lang['ube_report_moon_reapers_none'];
196
      } else {
197
        $text_defender .= "{$classLocale['ube_report_moon_reapers_wave']}. {$classLocale['ube_report_moon_reapers_chance']} {$this->destroy_chance}%. ";
198
        $text_defender .= classLocale::$lang[$this->status == UBE_MOON_DESTROY_SUCCESS ? 'ube_report_moon_reapers_success' : 'ube_report_moon_reapers_failure'] . "<br />";
199
200
        $text_defender .= "{$classLocale['ube_report_moon_reapers_outcome']} {$this->reaper_die_chance}%. ";
201
        $text_defender .= classLocale::$lang[$this->reapers_status == UBE_MOON_REAPERS_RETURNED ? 'ube_report_moon_reapers_survive' : 'ube_report_moon_reapers_died'];
202
      }
203
      $text_defender .= '<br /><br />';
204
    }
205
206
    return $text_defender;
207
  }
208
209
  public function get_status() {
210
    return $this->status;
211
  }
212
213
  public function get_reapers_status() {
214
    return $this->reapers_status;
215
  }
216
217
  /**
218
   * @param $destination_planet
219
   *
220
   * @version 2016-02-25 23:42:45 41a4.68
221
   */
222
  public function ubeInitLoadStatis($destination_planet) {
223
    if($destination_planet['planet_type'] == PT_MOON || is_array($moon = DBStaticPlanet::db_planet_by_parent($destination_planet['id'], true, '`id`'))) {
224
      $this->status = UBE_MOON_EXISTS;
225
      $this->moon_diameter = !empty($moon['planet_type']) && $moon['planet_type'] == PT_MOON ? $moon['diameter'] : $destination_planet['diameter'];
226
      $this->reapers_status = UBE_MOON_REAPERS_NONE;
227
    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
228
      // По умолчанию: нет луны итд
229
    }
230
  }
231
232
}
233