Completed
Push — work-fleets ( 7aaf68...e155c0 )
by SuperNova.WS
06:30
created

BuddyModel::loadTry()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 12
rs 9.6666
1
<?php
2
3
namespace Buddy;
4
5
use classSupernova;
6
use db_mysql;
7
use DbEmptyIterator;
8
use DbMysqliResultIterator;
9
use DBStaticMessages;
10
use DBStaticUser;
11
use Entity;
12
13
/**
14
 * Class BuddyModel
15
 *
16
 * @property int|float $playerSenderId Who makes buddy request
17
 * @property int|float $playerOwnerId To whom this buddy request made
18
 * @property int       $buddyStatusId Current buddy request status
19
 * @property string    $requestText Request text
20
 *
21
 * @package Buddy
22
 */
23
class BuddyModel extends Entity {
24
25
  protected static $tableName = 'buddy';
26
  protected static $idField = 'BUDDY_ID';
27
28
  // TODO - make it work with Model's properties
29
  /**
30
   * Property list
31
   *
32
   * @var array
33
   */
34
  protected static $_properties = array(
35
    'dbId'           => array(
36
      P_DB_FIELD => 'BUDDY_ID',
37
    ),
38
    'playerSenderId' => array(
39
      P_DB_FIELD => 'BUDDY_SENDER_ID',
40
    ),
41
    'playerOwnerId'  => array(
42
      P_DB_FIELD => 'BUDDY_OWNER_ID',
43
    ),
44
    'buddyStatusId'  => array(
45
      P_DB_FIELD => 'BUDDY_STATUS',
46
    ),
47
    'requestText'    => array(
48
      P_DB_FIELD => 'BUDDY_REQUEST',
49
    ),
50
  );
51
52
  // 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...
53
  public function db_buddy_update_status($status) {
54
    $buddy_id = idval($this->dbId);
55
56
    doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;");
57
58
    return classSupernova::$db->db_affected_rows();
59
  }
60
61
  /**
62
   * @param BuddyRoutingParams $cBuddy
63
   *
64
   * @return array|null
65
   */
66
  public function db_buddy_check_relation($cBuddy) {
67
    return static::$dbStatic->doQueryFetch(
68
      "SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE
69
      (`BUDDY_SENDER_ID` = {$cBuddy->user['id']} AND `BUDDY_OWNER_ID` = {$cBuddy->newFriendId})
70
      OR
71
      (`BUDDY_SENDER_ID` = {$cBuddy->newFriendId} AND `BUDDY_OWNER_ID` = {$cBuddy->user['id']})
72
      LIMIT 1 FOR UPDATE;"
73
    );
74
  }
75
76
  /**
77
   * @param db_mysql $db
78
   * @param mixed    $user_id
79
   *
80
   * @return DbEmptyIterator|DbMysqliResultIterator
81
   */
82
  // TODO - make it non-static
83
  public static function db_buddy_list_by_user($db, $user_id) {
84
    return ($user_id = idval($user_id)) ? $db->doQueryIterator(
85
      "SELECT
86
      b.*,
87
      IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID,
88
      u.username AS BUDDY_USER_NAME,
89
      p.name AS BUDDY_PLANET_NAME,
90
      p.galaxy AS BUDDY_PLANET_GALAXY,
91
      p.system AS BUDDY_PLANET_SYSTEM,
92
      p.planet AS BUDDY_PLANET_PLANET,
93
      a.id AS BUDDY_ALLY_ID,
94
      a.ally_name AS BUDDY_ALLY_NAME,
95
      u.onlinetime
96
    FROM {{buddy}} AS b
97
      LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID)
98
      LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet
99
      LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id
100
    WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id}
101
    ORDER BY BUDDY_STATUS, BUDDY_ID"
102
    ) : new DbEmptyIterator();
103
  }
104
105
  /**
106
   * @param BuddyRoutingParams $cBuddy
107
   *
108
   * @throws BuddyException
109
   */
110
  public function accept($cBuddy) {
111
    if ($cBuddy->mode != 'accept') {
112
      return;
113
    }
114
115
    $user = $cBuddy->user;
116
117
    if ($this->playerSenderId == $user['id']) {
118
      throw new BuddyException('buddy_err_accept_own', ERR_ERROR);
119
    }
120
121
    if ($this->playerOwnerId != $user['id']) {
122
      throw new BuddyException('buddy_err_accept_alien', ERR_ERROR);
123
    }
124
125
    if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
126
      throw new BuddyException('buddy_err_accept_already', ERR_WARNING);
127
    }
128
129
    if ($this->buddyStatusId == BUDDY_REQUEST_DENIED) {
130
      throw new BuddyException('buddy_err_accept_denied', ERR_ERROR);
131
    }
132
133
    if ($this->buddyStatusId != BUDDY_REQUEST_WAITING) {
134
      throw new BuddyException('buddy_err_unknown_status', ERR_ERROR);
135
    }
136
137
    $result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE);
138
    if ($result) {
139
      DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text');
140
      throw new BuddyException('buddy_err_accept_none', ERR_NONE);
141
    } else {
142
      throw new BuddyException('buddy_err_accept_internal', ERR_ERROR);
143
    }
144
  }
145
146
  /**
147
   * Declining buddy request
148
   *
149
   * If it is own request - it will be deleted
150
   *
151
   * @param BuddyRoutingParams $cBuddy
152
   *
153
   * @throws BuddyException
154
   */
155
  public function decline($cBuddy) {
156
    if ($cBuddy->mode != 'delete') {
157
      return;
158
    }
159
160
    $user = $cBuddy->user;
161
162
    if ($this->playerSenderId != $user['id'] && $this->playerOwnerId != $user['id']) {
163
      throw new BuddyException('buddy_err_delete_alien', ERR_ERROR);
164
    }
165
166
    if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) {
167
      // Existing friendship
168
      $ex_friend_id = $this->playerSenderId == $user['id'] ? $this->playerOwnerId : $this->playerSenderId;
169
170
      DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text');
171
172
      $this->delete();
173
      throw new BuddyException('buddy_err_unfriend_none', ERR_NONE);
174
    } elseif ($this->playerSenderId == $user['id']) {
175
      // Player's outcoming request - either denied or waiting
176
      $this->delete();
177
      throw new BuddyException('buddy_err_delete_own', ERR_NONE);
178
    } elseif ($this->buddyStatusId == BUDDY_REQUEST_WAITING) {
179
      // Deny incoming request
180
      DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text');
181
182
      $this->db_buddy_update_status(BUDDY_REQUEST_DENIED);
183
      throw new BuddyException('buddy_err_deny_none', ERR_NONE);
184
    }
185
  }
186
187
  /**
188
   * @param BuddyRoutingParams $cBuddy
189
   *
190
   * @throws BuddyException
191
   */
192
  public function beFriend($cBuddy) {
193
    if (empty($cBuddy->new_friend_id_safe) && empty($cBuddy->new_friend_name_unsafe)) {
194
      return;
195
    }
196
197
    $user = $cBuddy->user;
198
199
200
    $new_friend_row = array();
201
    if ($cBuddy->new_friend_id_safe) {
202
      $new_friend_row = DBStaticUser::db_user_by_id($cBuddy->new_friend_id_safe, true, '`id`, `username`');
203
    } elseif ($cBuddy->new_friend_name_unsafe) {
204
      $new_friend_row = DBStaticUser::db_user_by_username($cBuddy->new_friend_name_unsafe, true, '`id`, `username`');
205
    }
206
207
    if (empty($new_friend_row) || empty($new_friend_row['id'])) {
208
      return;
209
    }
210
211
    if ($new_friend_row['id'] == $user['id']) {
212
      unset($new_friend_row);
213
      throw new BuddyException('buddy_err_adding_self', ERR_ERROR);
214
    }
215
216
    // Checking for user name & request text - in case if it was request to adding new request
217
    if ($cBuddy->new_request_text_unsafe) {
218
      $cBuddy->newFriendId = $new_friend_row['id'];
219
      $check_relation = $this->db_buddy_check_relation($cBuddy);
220
      if (isset($check_relation[$this->getIdFieldName()])) {
221
        throw new BuddyException('buddy_err_adding_exists', ERR_WARNING);
222
      }
223
224
      DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text');
225
226
      $this->playerSenderId = idval($user['id']);
227
      $this->playerOwnerId = idval($new_friend_row['id']);
228
      $this->buddyStatusId = BUDDY_REQUEST_WAITING;
229
      $this->requestText = $cBuddy->new_request_text_unsafe;
230
231
      $this->insert();
232
      throw new BuddyException('buddy_err_adding_none', ERR_NONE);
233
    }
234
  }
235
236
  public function isContainerEmpty() {
237
    return
238
      $this->buddyStatusId == null
239
      ||
240
      $this->buddyStatusId == BUDDY_REQUEST_NOT_SET
241
      ||
242
      empty($this->playerSenderId)
243
      ||
244
      empty($this->playerOwnerId);
245
  }
246
247
  /**
248
   * Trying to load object info by buddy ID - if it is supplied
249
   *
250
   * @param BuddyRoutingParams $cBuddy
251
   *
252
   * @throws BuddyException
253
   */
254
  protected function loadTry($cBuddy) {
255
    if ($cBuddy->buddy_id) {
256
      $this->load($cBuddy->buddy_id);
257
258
      if ($this->isContainerEmpty()) {
259
        throw new BuddyException('buddy_err_not_exist', ERR_ERROR);
260
      }
261
    }
262
  }
263
264
  /**
265
   * @param BuddyRoutingParams $cBuddy
266
   *
267
   * @throws BuddyException
268
   */
269
  public function route($cBuddy) {
270
    // Trying to load buddy record with supplied dbId
271
    $this->loadTry($cBuddy);
272
    // Trying to accept buddy request
273
    $this->accept($cBuddy);
274
    // Trying to decline buddy request. If it's own request - it will be deleted
275
    $this->decline($cBuddy);
276
    // New request?
277
    $this->beFriend($cBuddy);
278
  }
279
280
}
281