Completed
Push — work-fleets ( e12dae...dea33d )
by SuperNova.WS
06:17
created

Buddy::delete()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
nc 6
nop 1
dl 0
loc 27
rs 6.7272
c 2
b 0
f 0
1
<?php
2
3
class Buddy extends Entity {
4
5
  public static $tableName = 'buddy';
6
  public static $idField = 'BUDDY_ID';
7
8
  // TODO - remove public static function db_buddy_update_status($buddy_id, $status) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% 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...
9
  public function db_buddy_update_status($status) {
10
    $buddy_id = idval($this->row['BUDDY_ID']);
11
12
    doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
13
14
    return classSupernova::$db->db_affected_rows();
15
  }
16
17
  public function db_buddy_check_relation($user, $new_friend_row) {
18
    return static::$dbStatic->doQueryFetch(
19
      "SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE
20
      (`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']})
21
      OR
22
      (`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']})
23
      LIMIT 1 FOR UPDATE;"
24
    );
25
  }
26
27
  /**
28
   * @param db_mysql $db
29
   * @param mixed    $user_id
30
   *
31
   * @return DbEmptyIterator
32
   */
33
  public static function db_buddy_list_by_user($db, $user_id) {
34
    return ($user_id = idval($user_id)) ? $db->doQueryIterator(
0 ignored issues
show
Bug Compatibility introduced by
The expression ($user_id = idval($user_...new \DbEmptyIterator(); of type DbMysqliResultIterator|DbEmptyIterator adds the type DbMysqliResultIterator to the return on line 34 which is incompatible with the return type documented by Buddy::db_buddy_list_by_user of type DbEmptyIterator.
Loading history...
35
      "SELECT
36
      b.*,
37
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
38
      u.username AS BUDDY_USER_NAME,
39
      p.name AS BUDDY_PLANET_NAME,
40
      p.galaxy AS BUDDY_PLANET_GALAXY,
41
      p.system AS BUDDY_PLANET_SYSTEM,
42
      p.planet AS BUDDY_PLANET_PLANET,
43
      a.id AS BUDDY_ALLY_ID,
44
      a.ally_name AS BUDDY_ALLY_NAME,
45
      u.onlinetime
46
    FROM {{buddy}} AS b
47
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
48
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
49
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
50
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
51
    ORDER BY BUDDY_STATUS, BUDDY_ID"
52
    ) : new DbEmptyIterator();
53
  }
54
55
  /**
56
   * @param array $user
57
   *
58
   * @throws Exception
59
   */
60
  public function accept($user) {
61
    $buddy_row = $this->row;
62
63
    if ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
64
      throw new Exception('buddy_err_accept_own', ERR_ERROR);
65
    }
66
67
    if ($buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
68
      throw new Exception('buddy_err_accept_alien', ERR_ERROR);
69
    }
70
71
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
72
      throw new Exception('buddy_err_accept_already', ERR_WARNING);
73
    }
74
75
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED) {
76
      throw new Exception('buddy_err_accept_denied', ERR_ERROR);
77
    }
78
79
    if ($buddy_row['BUDDY_STATUS'] != BUDDY_REQUEST_WAITING) {
80
      throw new Exception('buddy_err_unknown_status', ERR_ERROR);
81
    }
82
83
    $result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE);
84
    if ($result) {
85
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
86
      throw new Exception('buddy_err_accept_none', ERR_NONE);
87
    } else {
88
      throw new Exception('buddy_err_accept_internal', ERR_ERROR);
89
    }
90
  }
91
92
  /**
93
   * @param array $user
94
   *
95
   * @throws Exception
96
   */
97
  public function decline($user) {
98
    $buddy_row = $this->row;
99
100
    if ($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id']) {
101
      throw new Exception('buddy_err_delete_alien', ERR_ERROR);
102
    }
103
104
    if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) {
105
      // Existing friendship
106
      $ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID'];
107
108
      DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
109
110
      $this->delete();
111
      throw new Exception('buddy_err_unfriend_none', ERR_NONE);
112
    } elseif ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) {
113
      // Player's outcoming request - either denied or waiting
114
      $this->delete();
115
      throw new Exception('buddy_err_delete_own', ERR_NONE);
116
    } elseif ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) {
117
      // Deny incoming request
118
      DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
119
120
      $this->db_buddy_update_status(BUDDY_REQUEST_DENIED);
121
      throw new Exception('buddy_err_deny_none', ERR_NONE);
122
    }
123
  }
124
125
  /**
126
   * @param array  $user
127
   * @param mixed  $new_friend_id
128
   * @param string $new_friend_name_unsafe
129
   * @param string $new_request_text_safe
130
   *
131
   * @throws Exception
132
   */
133
  public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe) {
134
    $new_friend_row = array();
135
    if ($new_friend_id) {
136
      $new_friend_row = DBStaticUser::db_user_by_id($new_friend_id, true, '`id`, `username`');
137
    } elseif ($new_friend_name_unsafe) {
138
      $new_friend_row = DBStaticUser::db_user_by_username($new_friend_name_unsafe, true, '`id`, `username`');
139
    }
140
141
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
142
      return;
143
    }
144
145
    if ($new_friend_row['id'] == $user['id']) {
146
      unset($new_friend_row);
147
      throw new Exception('buddy_err_adding_self', ERR_ERROR);
148
    }
149
150
    // Checking for user name & request text - in case if it was request to adding new request
151
    if ($new_request_text_safe) {
152
      $check_relation = $this->db_buddy_check_relation($user, $new_friend_row);
153
      if (isset($check_relation['BUDDY_ID'])) {
154
        throw new Exception('buddy_err_adding_exists', ERR_WARNING);
155
      }
156
157
      DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
158
159
      $this->row['BUDDY_SENDER_ID'] = idval($user['id']);
160
      $this->row['BUDDY_OWNER_ID'] = idval($new_friend_row['id']);
161
      $this->row['BUDDY_STATUS'] = BUDDY_REQUEST_WAITING;
162
      $this->row['BUDDY_REQUEST'] = $new_request_text_safe;
163
164
      $this->insert();
165
      throw new Exception('buddy_err_adding_none', ERR_NONE);
166
    }
167
  }
168
169
}
170