Completed
Push — work-fleets ( 2bd11a...17dd3b )
by SuperNova.WS
06:36
created

DBStaticUnit::db_unit_by_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 1
ccs 0
cts 3
cp 0
crap 2
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_location($user_id = 0, $location_type, $location_id, $unit_snid = 0, $for_update = false, $fields = '*') {
14
    // apply time restrictions ????
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
15
    return classSupernova::db_get_unit_by_location($user_id, $location_type, $location_id, $unit_snid, $for_update, $fields);
16
  }
17
18
  public static function db_unit_count_by_user_and_type_and_snid($user_id, $unit_type = 0, $unit_snid = 0) {
19
    $query = classSupernova::$db->doSelect(
20
      "SELECT unit_snid, sum(unit_level) as `qty`  FROM {{unit}} WHERE `unit_player_id` = {$user_id} " .
21
      ($unit_type ? "AND `unit_type` = {$unit_type} " : '') .
22
      ($unit_snid ? "AND `unit_snid` = {$unit_snid} " : '') .
23
      'GROUP BY `unit_snid`'
24
    );
25
    $result = array();
26
    while($row = db_fetch($query)) {
27
      $result[$row['unit_snid']] = $row;
28
    }
29
30
    return $result;
31
  }
32
33
// Used by UNIT_CAPTAIN module TODO
34
  public static function db_unit_in_fleet_by_user($user_id, $location_id, $unit_snid, $for_update) {
35
    return classSupernova::$db->doSelectFetch(
36
      "SELECT *
37
    FROM {{fleets}} AS f
38
      JOIN {{unit}} AS u ON u.`unit_location_id` = f.fleet_id
39
    WHERE
40
      f.fleet_owner = {$user_id} AND
41
      (f.fleet_start_planet_id = {$location_id} OR f.fleet_end_planet_id = {$location_id})
42
      AND u.unit_snid = {$unit_snid} AND u.`unit_location_type` = " . LOC_FLEET . " AND " . self::db_unit_time_restrictions() .
43
      " LIMIT 1" .
44
      ($for_update ? ' FOR UPDATE' : ''));
45
  }
46
47
48
  public static function db_unit_list_laboratories($user_id) {
49
    return classSupernova::$db->doSelect("SELECT DISTINCT unit_location_id AS `id`
50
    FROM {{unit}}
51
    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 . ");");
52
  }
53
54
  public static function db_unit_set_by_id($unit_id, $set) {
55
    return classSupernova::db_upd_record_by_id(LOC_UNIT, $unit_id, $set);
56
  }
57
58
  /**
59
   * @param string $set
60
   *
61
   * @return array|bool|false|mysqli_result|null
62
   */
63
  public static function db_unit_set_insert($set) {
64
    return classSupernova::db_ins_record(LOC_UNIT, $set);
65
  }
66
67
  public static function db_unit_list_delete($user_id = 0, $unit_location_type, $unit_location_id = 0, $unit_snid = 0) {
68
    return classSupernova::db_del_record_list(LOC_UNIT,
69
      "`unit_location_type` = {$unit_location_type}" .
70
      ($unit_location_id = idval($unit_location_id) ? " AND `unit_location_id` = {$unit_location_id}" : '') .
71
      ($user_id = idval($user_id) ? " AND `unit_player_id` = {$user_id}" : '') .
72
      ($unit_snid = idval($unit_snid) ? " AND `unit_snid` = {$unit_snid}" : ''));
73
  }
74
75
  public static function db_unit_list_stat_calculate() {
76
    return classSupernova::$db->doSelect(
77
      "SELECT unit_player_id, unit_type, unit_snid, unit_level, count(*) AS unit_amount
78
    FROM `{{unit}}`
79
    WHERE unit_level > 0 AND " . self::db_unit_time_restrictions() .
80
      " GROUP BY unit_player_id, unit_type, unit_snid, unit_level"
81
    );
82
  }
83
84
85
  public static function db_unit_change_owner($location_type, $location_id, $new_owner_id) {
86
    classSupernova::$db->doUpdate("UPDATE {{unit}} SET `unit_player_id` = {$new_owner_id} WHERE `unit_location_type` = {$location_type} AND `unit_location_id` = {$location_id}");
87
  }
88
89
90
  public static function db_unit_list_admin_delete_mercenaries_finished() {
91
    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);
92
  }
93
94
  public static function db_unit_list_admin_set_mercenaries_expire_time($default_length) {
95
    return classSupernova::$db->doUpdate(
96
      "UPDATE `{{unit}}`
97
    SET
98
      unit_time_start = FROM_UNIXTIME(" . SN_TIME_NOW . "),
99
      unit_time_finish = FROM_UNIXTIME(" . (SN_TIME_NOW + $default_length) . ")
100
    WHERE unit_type = " . UNIT_MERCENARIES
101
    );
102
  }
103
104
}
105