Completed
Push — work-fleets ( 77ad6e...489db6 )
by SuperNova.WS
06:17
created

DBStaticUnit::dbUpdateOrInsertUnit()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 2
b 0
f 0
nc 24
nop 4
dl 0
loc 31
rs 8.439
ccs 0
cts 27
cp 0
crap 42
1
<?php
2
3
class DBStaticUnit {
4
5
  public static function db_unit_time_restrictions($date = SN_TIME_NOW) {
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
14
  /**
15
   * @param int $user_id
16
   * @param int $location_type
17
   * @param int $location_id
18
   *
19
   * @return array|bool
20
   */
21
  public static function db_get_unit_list_by_location($user_id = 0, $location_type, $location_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

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

Loading history...
22
    if (!($location_type = idval($location_type)) || !($location_id = idval($location_id))) {
23
      return false;
24
    }
25
26
    if (SnCache::isUnitLocatorNotSet($location_type, $location_id)) {
27
      $got_data = classSupernova::db_get_record_list(LOC_UNIT, "unit_location_type = {$location_type} AND unit_location_id = {$location_id} AND " . DBStaticUnit::db_unit_time_restrictions());
28
      if (!empty($got_data) && is_array($got_data)) {
29
        foreach ($got_data as $unit_id => $unit_data) {
30
          SnCache::setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data);
31
        }
32
      }
33
    }
34
35
    $result = false;
36
    foreach (SnCache::getUnitLocatorByFullLocation($location_type, $location_id) as $key => $value) {
37
      $result[$key] = $value;
38
    }
39
40
    return $result;
41
  }
42
43
44
  /**
45
   * @param int    $user_id
46
   * @param        $location_type
47
   * @param        $location_id
48
   * @param int    $unit_snid
49
   * @param bool   $for_update
50
   * @param string $fields
51
   *
52
   * @return mixed
53
   */
54
  public static function db_get_unit_by_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

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

Loading history...
55
    DBStaticUnit::db_get_unit_list_by_location($user_id, $location_type, $location_id);
56
57
    return SnCache::getUnitLocator($location_type, $location_id, $unit_snid);
58
  }
59
60
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
61
    $query = classSupernova::$db->doSelect(
62
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
63
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
64
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
65
      'GROUP BY `unit_snid`'
66
    );
67
    $result = array();
68
    while ($row = db_fetch($query)) {
69
      $result[$row['unit_snid']] = $row;
70
    }
71
72
    return $result;
73
  }
74
75
// Used by UNIT_CAPTAIN module TODO
76
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
77
    return classSupernova::$db->doSelectFetch(
78
      "SELECT *
79
    FROM {{fleets}} AS f
80
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
81
    WHERE
82
      f.fleet_owner = {$user_id} AND
83
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
84
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
85
      " LIMIT 1" .
86
      ($for_update ? ' FOR UPDATE' : ''));
87
  }
88
89
90
  public static function db_unit_list_laboratories($user_id) {
91
    return classSupernova::$db->doSelect("SELECT DISTINCT unit_location_id AS `id`
92
    FROM {{unit}}
93
    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 . ");");
94
  }
95
96
  public static function db_unit_set_by_id($unit_id, $set) {
97
    return classSupernova::db_upd_record_by_id(LOC_UNIT, $unit_id, $set);
98
  }
99
100
  /**
101
   * @param string $set
102
   *
103
   * @return array|bool|false|mysqli_result|null
104
   */
105
  public static function db_unit_set_insert($set) {
106
    return classSupernova::db_ins_record(LOC_UNIT, $set);
107
  }
108
109
  public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) {
110
    return classSupernova::db_del_record_list(LOC_UNIT,
111
      "`unit_location_type` = {$unit_location_type}" .
112
      ($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') .
113
      ($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') .
114
      ($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : ''));
115
  }
116
117
  public static function db_unit_list_stat_calculate() {
118
    return classSupernova::$db->doSelect(
119
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
120
    FROM `{{unit}}`
121
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
122
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
123
    );
124
  }
125
126
127
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
128
    classSupernova::$db->doUpdate("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
129
  }
130
131
132
  public static function db_unit_list_admin_delete_mercenaries_finished() {
133
    return classSupernova::$db->doDelete("DELETE FROM `{{unit}}` WHERE unit_time_finish IS NOT NULL AND unit_time_finish < FROM_UNIXTIME(" . SN_TIME_NOW . ") AND unit_type = " . UNIT_MERCENARIES);
134
  }
135
136
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
137
    return classSupernova::$db->doUpdate(
138
      "UPDATE `{{unit}}`
139
    SET
140
      unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "),
141
      unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ")
142
    WHERE unit_type = " . UNIT_MERCENARIES
143
    );
144
  }
145
146
147
  /**
148
   * @param      $unit_id
149
   * @param      $unit_value
150
   * @param      $user
151
   * @param null $planet_id
152
   *
153
   * @return bool
154
   */
155
  public static function dbUpdateOrInsertUnit($unit_id, $unit_value, $user, $planet_id = null) {
156
    DBStaticUser::validateUserRecord($user);
157
158
    $planet_id = !empty($planet_id['id']) ? $planet_id['id'] : $planet_id;
159
160
    $unit_location = sys_get_unit_location($user, array(), $unit_id);
161
    $location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id;
162
    $location_id = $location_id ? $location_id : 'NULL';
163
164
    $temp = DBStaticUnit::db_get_unit_by_location($user['id'], $unit_location, $location_id, $unit_id, true, 'unit_id');
165
    if (!empty($temp['unit_id'])) {
166
      $result = (bool)classSupernova::db_upd_record_list(
167
        LOC_UNIT, "`unit_level` = `unit_level` + ($unit_value)", "`unit_id` = {$temp['unit_id']}"
168
      );
169
    } else {
170
      $locationIdRendered = $unit_location == LOC_USER ? $user['id'] : $planet_id;
171
      $unitType = get_unit_param($unit_id, P_UNIT_TYPE);
172
      $result = (bool)classSupernova::db_ins_record(
173
        LOC_UNIT,
174
        "unit_player_id = {$user['id']}, 
175
        unit_location_type = {$unit_location}, 
176
        unit_location_id = {$locationIdRendered},
177
        unit_type = {$unitType},
178
        unit_snid = {$unit_id},
179
        unit_level = {$unit_value}
180
        "
181
      );
182
    }
183
184
    return $result;
185
  }
186
187
}
188