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

DBStaticUnit::db_unit_by_location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 6
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
class DBStaticUnit {
4
5 View Code Duplication
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
    $date = is_numeric($date) ? "FROM_UNIXTIME({$date})" : "'{$date}'";
7
8
    return
9
      "(unit_time_start IS NULL OR unit_time_start <= {$date}) AND
10
    (unit_time_finish IS NULL OR unit_time_finish = '1970-01-01 03:00:00' OR unit_time_finish >= {$date})";
11
  }
12
13
  public static function db_unit_by_id($unit_id, $for_update = false, $fields = '*') {
14
    return classSupernova::db_get_unit_by_id($unit_id, $for_update, $fields);
15
  }
16
17
  public static function db_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
18
    // apply time restrictions ????
19
    return classSupernova::db_get_unit_by_location($user_id, $location_type, $location_id, $unit_snid, $for_update, $fields);
20
  }
21
22
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
23
    $query = doquery(
24
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
25
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
26
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
27
      'GROUP BY `unit_snid`'
28
    );
29
    $result = array();
30
    while ($row = db_fetch($query)) {
31
      $result[$row['unit_snid']] = $row;
32
    }
33
34
    return $result;
35
  }
36
37
// Used by UNIT_CAPTAIN module TODO
38
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
39
    return doquery(
40
      "SELECT *
41
    FROM {{fleets}} AS f
42
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
43
    WHERE
44
      f.fleet_owner = {$user_id} AND
45
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
46
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
47
      " LIMIT 1" .
48
      ($for_update ? ' FOR UPDATE' : '')
49
      , true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

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...
50
  }
51
52
53
  public static function db_unit_list_laboratories($user_id) {
54
    return doquery("SELECT DISTINCT unit_location_id AS `id`
55
    FROM {{unit}}
56
    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 . ");");
57
  }
58
59
  public static function db_unit_set_by_id($unit_record_id, $set) {
60
    return classSupernova::db_upd_record_by_id(LOC_UNIT, $unit_record_id, $set);
61
  }
62
63
  /**
64
   * @param string $set
65
   *
66
   * @return array|bool|false|mysqli_result|null
67
   */
68
  public static function db_unit_set_insert($set) {
69
    return classSupernova::db_ins_record(LOC_UNIT, $set);
70
  }
71
72
  public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) {
73
    return classSupernova::db_del_record_list(LOC_UNIT,
74
      "`unit_location_type` = {$unit_location_type}" .
75
      ($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') .
76
      ($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') .
77
      ($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : ''));
78
  }
79
80
  public static function db_unit_list_stat_calculate() {
81
    return doquery(
82
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
83
    FROM `{{unit}}`
84
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
85
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
86
    );
87
  }
88
89
90
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
91
    doquery("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
92
  }
93
94
95
  public static function db_unit_list_admin_delete_mercenaries_finished() {
96
    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);
97
  }
98
99
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
100
    return doquery(
101
      "UPDATE {{unit}}
102
    SET
103
      unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "),
104
      unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ")
105
    WHERE unit_type = " . UNIT_MERCENARIES
106
    );
107
  }
108
109
  public static function dbUserAdd($playerId, $unitSnId, $level) {
110
    if (!($unitRecord = \Unit\RecordUnit::findFirst([
111
      'unit_player_id'     => $playerId,
112
      'unit_location_type' => LOC_USER,
113
      'unit_location_id'   => $playerId,
114
      'unit_snid'          => $unitSnId,
115
    ]))) {
116
      if ($level < 0) {
117
        return false;
118
      }
119
120
      // New unit
121
      $unitRecord = \Unit\RecordUnit::build([
122
        'unit_player_id'     => $playerId,
123
        'unit_location_type' => LOC_USER,
124
        'unit_location_id'   => $playerId,
125
        'unit_type'          => get_unit_param($unitSnId, P_UNIT_TYPE),
126
        'unit_snid'          => $unitSnId,
127
        'unit_level'         => $level,
128
      ]);
129
130
//      var_dump($unitRecord);die();
131
132
      return $unitRecord->insert();
133
    } else {
134
      if ($unitRecord->unit_level + $level < 0) {
135
        return false;
136
      }
137
138
      $unitRecord->inc()->unit_level = $level;
139
140
      return $unitRecord->update();
141
    }
142
143
144
  }
145
146
}
147