Issues (1369)

classes/Unit/DBStaticUnit.php (16 issues)

1
<?php
2
3
/** @noinspection SqlResolve */
4
5
namespace Unit;
6
7
use Exception;
8
use mysqli_result;
9
use Planet\Planet;
10
use SN;
11
12
class DBStaticUnit {
13
14
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
15
    $date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'";
16
17
    return
18
      "(unit_time_start IS NULL OR unit_time_start <= {$date}) AND
19
    (unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})";
20
  }
21
22
  /**
23
   * Used by `unit_captain`
24
   *
25
   * @param int $unit_id
26
   *
27
   * @return array|bool|mysqli_result|null
28
   */
29
  public static function db_unit_by_id($unit_id) {
30
    return doquery("SELECT * FROM `{{unit}}` WHERE `unit_id` = " . idval($unit_id), true);
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

30
    return /** @scrutinizer ignore-deprecated */ doquery("SELECT * FROM `{{unit}}` WHERE `unit_id` = " . idval($unit_id), true);
Loading history...
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

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

30
    return doquery("SELECT * FROM `{{unit}}` WHERE `unit_id` = " . idval($unit_id), /** @scrutinizer ignore-type */ true);
Loading history...
31
  }
32
33
  /**
34
   * @param int $user_id
35
   * @param int $location_type
36
   * @param int $location_id
37
   * @param int $unit_snid
38
   *
39
   * @return array|false|mixed|null
40
   */
41
  public static function db_unit_by_location($user_id, $location_type, $location_id, $unit_snid = 0) {
0 ignored issues
show
The parameter $user_id is not used and could be removed. ( Ignorable by Annotation )

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

41
  public static function db_unit_by_location(/** @scrutinizer ignore-unused */ $user_id, $location_type, $location_id, $unit_snid = 0) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    // apply time restrictions ????
43
    $allUnits = DBStaticUnit::db_get_unit_list_by_location($location_type, $location_id);
44
45
    $unit_snid = intval($unit_snid);
46
47
    $resultOld = $unit_snid ? (isset($allUnits[$unit_snid]) ? $allUnits[$unit_snid] : null ) : $allUnits;
48
49
    return $resultOld;
50
  }
51
52
  public static $locator = array(); // Кэширует соответствия между расположением объектов - в частности юнитов
53
54
  /**
55
   * @param $location_type
56
   * @param $location_id
57
   *
58
   * @return array|false
59
   */
60
  public static function db_get_unit_list_by_location($location_type, $location_id) {
61
    if (!($location_type = idval($location_type)) || !($location_id = idval($location_id))) {
62
      return false;
63
    }
64
65
    if (!isset(DBStaticUnit::$locator[$location_type][$location_id])) {
66
      $got_data = SN::db_get_record_list(LOC_UNIT, "unit_location_type = {$location_type} AND unit_location_id = {$location_id} AND " . DBStaticUnit::db_unit_time_restrictions());
67
      if (is_array($got_data)) {
68
        foreach ($got_data as $unitRow) {
69
          DBStaticUnit::$locator[$unitRow['unit_location_type']][$unitRow['unit_location_id']][$unitRow['unit_snid']] = $unitRow;
70
        }
71
      }
72
    }
73
74
    $result = false;
75
76
    if (is_array(DBStaticUnit::$locator[$location_type][$location_id])) {
77
      foreach (DBStaticUnit::$locator[$location_type][$location_id] as $key => $value) {
78
        $result[$key] = $value;
79
      }
80
    }
81
82
    return $result;
83
  }
84
85
  public static function cache_clear() {
86
    DBStaticUnit::$locator = [];
87
  }
88
89
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
90
    $query  = doquery(
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

90
    $query  = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
91
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
92
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
93
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
94
      'GROUP BY `unit_snid`'
95
    );
96
    $result = array();
97
    while ($row = db_fetch($query)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() 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

97
    while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($query)) {
Loading history...
98
      $result[$row['unit_snid']] = $row;
99
    }
100
101
    return $result;
102
  }
103
104
// Used by UNIT_CAPTAIN module TODO
105
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
106
    return doquery(
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

106
    return /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
107
      "SELECT *
108
    FROM {{fleets}} AS f
109
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
110
    WHERE
111
      f.fleet_owner = {$user_id} AND
112
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
113
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
114
      " LIMIT 1" .
115
      ($for_update ? ' FOR UPDATE' : '')
116
      , true);
0 ignored issues
show
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

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

116
      , /** @scrutinizer ignore-type */ true);
Loading history...
117
  }
118
119
120
  public static function db_unit_list_laboratories($user_id) {
121
    return doquery("SELECT DISTINCT unit_location_id AS `id`
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

121
    return /** @scrutinizer ignore-deprecated */ doquery("SELECT DISTINCT unit_location_id AS `id`
Loading history...
122
    FROM {{unit}}
123
    WHERE unit_player_id = {$user_id} AND unit_location_type = " . LOC_PLANET . " AND unit_level > 0 AND unit_snid IN (" . STRUC_LABORATORY . ", " . STRUC_LABORATORY_NANO . ");");
124
  }
125
126
  public static function db_unit_set_by_id($unit_record_id, $set) {
127
    return SN::db_upd_record_by_id(LOC_UNIT, $unit_record_id, $set);
128
  }
129
130
  /**
131
   * @param string $set
132
   *
133
   * @return array|bool|false|mysqli_result|null
134
   */
135
  public static function db_unit_set_insert($set) {
136
    return SN::db_ins_record(LOC_UNIT, $set);
137
  }
138
139
  public static function db_unit_list_delete($user_id, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) {
140
    return SN::db_del_record_list(LOC_UNIT,
141
      "`unit_location_type` = {$unit_location_type}" .
142
      ($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') .
0 ignored issues
show
The assignment to $unit_location_id is dead and can be removed.
Loading history...
143
      ($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') .
0 ignored issues
show
The assignment to $user_id is dead and can be removed.
Loading history...
144
      ($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : ''));
0 ignored issues
show
The assignment to $unit_snid is dead and can be removed.
Loading history...
145
  }
146
147
  public static function db_unit_list_stat_calculate() {
148
    return doquery(
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

148
    return /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
149
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
150
    FROM `{{unit}}`
151
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
152
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
153
    );
154
  }
155
156
157
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
158
    doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
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

158
    /** @scrutinizer ignore-deprecated */ doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
Loading history...
159
  }
160
161
162
  public static function db_unit_list_admin_delete_mercenaries_finished() {
163
    /** @noinspection SqlWithoutWhere */
164
    return doquery("DELETE FROM {{unit}} WHERE unit_time_finish IS NOT NULL AND unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ") AND unit_type = " . UNIT_MERCENARIES);
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

164
    return /** @scrutinizer ignore-deprecated */ doquery("DELETE FROM {{unit}} WHERE unit_time_finish IS NOT NULL AND unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ") AND unit_type = " . UNIT_MERCENARIES);
Loading history...
165
  }
166
167
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
168
    return doquery(
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

168
    return /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
169
      "UPDATE {{unit}}
170
    SET
171
      unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "),
172
      unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ")
173
    WHERE unit_type = " . UNIT_MERCENARIES
174
    );
175
  }
176
177
  /**
178
   * Adjust unit amount
179
   *
180
   * @param int   $playerId
181
   * @param int   $planetId
182
   * @param int   $unitSnId
183
   * @param float $amount
184
   * @param int   $reason
185
   *
186
   * @return bool
187
   *
188
   * @throws Exception
189
   */
190
  public static function dbChangeUnit($playerId, $planetId, $unitSnId, $amount, $reason = RPG_NONE) {
191
    $result = false;
192
193
    // TODO - Lock user
194
    $userArray = db_user_by_id($playerId);
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

194
    $userArray = /** @scrutinizer ignore-deprecated */ db_user_by_id($playerId);
Loading history...
195
196
    if ($unitSnId == RES_DARK_MATTER) {
197
      // Add dark matter to user
198
      $result = boolval(rpg_points_change($playerId, $reason, $amount));
199
    } elseif (in_array($unitSnId, sn_get_groups(UNIT_RESOURCES_STR_LOOT))) {
200
      // Add resources to user's capital
201
      if ($userArray['user_as_ally'] == 1) {
202
        // TODO - If ally - adding resources to user record
203
      } else {
204
        // Adding resources to planet
205
        $planet = new Planet();
206
        $planet->dbLoadRecord($planetId);
207
        $planet->changeResource($unitSnId, $amount);
208
        $planet->save();
209
210
        $result = true;
211
      }
212
    } elseif (in_array($unitSnId, sn_get_groups(UNIT_ARTIFACTS_STR))) {
213
      // Add artifacts to player
214
      $result = self::dbAdd($playerId, 0, $unitSnId, $amount);
215
    } elseif (!empty($planetId) && in_array($unitSnId, sn_get_groups([UNIT_STRUCTURES_STR, UNIT_SHIPS_STR, UNIT_DEFENCE_STR, ]))) {
216
      // Add fleet or defense to user's capital
217
      $result = self::dbAdd($playerId, $planetId, $unitSnId, $amount);
218
    }
219
220
    return $result;
221
  }
222
223
224
  /**
225
   * Add unit to player/planet
226
   *
227
   * Supports units. DOES NOT support resources
228
   *
229
   * DOES NOT autodetect location
230
   *
231
   * @param int   $playerId
232
   * @param int   $planetId 0 - for player units
233
   * @param int   $unitSnId
234
   * @param float $amount
235
   *
236
   * @return bool
237
   */
238
  protected static function dbAdd($playerId, $planetId, $unitSnId, $amount) {
239
    if (!in_array($unitSnId, sn_get_groups([UNIT_SHIPS_STR, UNIT_DEFENCE_STR, UNIT_ARTIFACTS_STR, UNIT_STRUCTURES_STR,]))) {
240
      return false;
241
    }
242
243
    if ($planetId == 0) {
244
      $locationType = LOC_USER;
245
      $locationId   = $playerId;
246
    } else {
247
      $locationType = LOC_PLANET;
248
      $locationId   = $planetId;
249
    }
250
251
    $fields = [
252
      'unit_player_id'     => $playerId,
253
      'unit_location_type' => $locationType,
254
      'unit_location_id'   => $locationId,
255
      'unit_snid'          => $unitSnId,
256
    ];
257
    if (!($unitRecord = RecordUnit::findFirst($fields))) {
258
      if ($amount < 0) {
259
        return false;
260
      }
261
262
      // New unit
263
//      $unitRecord = RecordUnit::build([
264
//        'unit_player_id'     => $playerId,
265
//        'unit_location_type' => $locationType,
266
//        'unit_location_id'   => $locationId,
267
//        'unit_snid'          => $unitSnId,
268
//        'unit_type'          => get_unit_param($unitSnId, P_UNIT_TYPE),
269
//        'unit_level'         => $level,
270
//      ]);
271
272
      $fields     += [
273
        'unit_type'  => get_unit_param($unitSnId, P_UNIT_TYPE),
274
        'unit_level' => $amount,
275
      ];
276
      $unitRecord = RecordUnit::build($fields);
277
278
      return $unitRecord->insert();
279
    } else {
280
      if ($unitRecord->unit_level + $amount < 0) {
281
        // TODO - Log error or throw Exception
282
        return false;
283
      }
284
285
      $unitRecord->inc()->unit_level = $amount;
286
287
      return $unitRecord->update();
288
    }
289
  }
290
291
  public static function dbUserAdd($playerId, $unitSnId, $amount) {
292
//    if (!($unitRecord = RecordUnit::findFirst([
293
//      'unit_player_id'     => $playerId,
294
//      'unit_location_type' => LOC_USER,
295
//      'unit_location_id'   => $playerId,
296
//      'unit_snid'          => $unitSnId,
297
//    ]))) {
298
//      if ($level < 0) {
299
//        return false;
300
//      }
301
//
302
//      // New unit
303
//      $unitRecord = RecordUnit::build([
304
//        'unit_player_id'     => $playerId,
305
//        'unit_location_type' => LOC_USER,
306
//        'unit_location_id'   => $playerId,
307
//        'unit_type'          => get_unit_param($unitSnId, P_UNIT_TYPE),
308
//        'unit_snid'          => $unitSnId,
309
//        'unit_level'         => $level,
310
//      ]);
311
//
312
////      var_dump($unitRecord);die();
313
//
314
//      return $unitRecord->insert();
315
//    } else {
316
//      if ($unitRecord->unit_level + $level < 0) {
317
//        // TODO - Log error or throw Exception
318
//        return false;
319
//      }
320
//
321
//      $unitRecord->inc()->unit_level = $level;
322
//
323
//      return $unitRecord->update();
324
//    }
325
    return self::dbAdd($playerId, 0, $unitSnId, $amount);
326
  }
327
328
}
329