|
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
|
|
|
public static $tableName = 'buddy'; |
|
26
|
|
|
public 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' => true, |
|
36
|
|
|
'playerSenderId' => true, |
|
37
|
|
|
'playerOwnerId' => true, |
|
38
|
|
|
'buddyStatusId' => true, |
|
39
|
|
|
'requestText' => true, |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
// TODO - remove public static function db_buddy_update_status($buddy_id, $status) { |
|
|
|
|
|
|
44
|
|
|
public function db_buddy_update_status($status) { |
|
45
|
|
|
$buddy_id = idval($this->dbId); |
|
46
|
|
|
|
|
47
|
|
|
doquery("UPDATE `{{buddy}}` SET `BUDDY_STATUS` = {$status} WHERE `BUDDY_ID` = '{$buddy_id}' LIMIT 1;"); |
|
48
|
|
|
|
|
49
|
|
|
return classSupernova::$db->db_affected_rows(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// TODO - make it non-static |
|
53
|
|
|
public function db_buddy_check_relation($user, $new_friend_row) { |
|
54
|
|
|
return static::$dbStatic->doQueryFetch( |
|
55
|
|
|
"SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE |
|
56
|
|
|
(`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']}) |
|
57
|
|
|
OR |
|
58
|
|
|
(`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']}) |
|
59
|
|
|
LIMIT 1 FOR UPDATE;" |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param db_mysql $db |
|
65
|
|
|
* @param mixed $user_id |
|
66
|
|
|
* |
|
67
|
|
|
* @return DbEmptyIterator|DbMysqliResultIterator |
|
68
|
|
|
*/ |
|
69
|
|
|
// TODO - make it non-static |
|
70
|
|
|
public static function db_buddy_list_by_user($db, $user_id) { |
|
71
|
|
|
return ($user_id = idval($user_id)) ? $db->doQueryIterator( |
|
72
|
|
|
"SELECT |
|
73
|
|
|
b.*, |
|
74
|
|
|
IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID, |
|
75
|
|
|
u.username AS BUDDY_USER_NAME, |
|
76
|
|
|
p.name AS BUDDY_PLANET_NAME, |
|
77
|
|
|
p.galaxy AS BUDDY_PLANET_GALAXY, |
|
78
|
|
|
p.system AS BUDDY_PLANET_SYSTEM, |
|
79
|
|
|
p.planet AS BUDDY_PLANET_PLANET, |
|
80
|
|
|
a.id AS BUDDY_ALLY_ID, |
|
81
|
|
|
a.ally_name AS BUDDY_ALLY_NAME, |
|
82
|
|
|
u.onlinetime |
|
83
|
|
|
FROM {{buddy}} AS b |
|
84
|
|
|
LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) |
|
85
|
|
|
LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet |
|
86
|
|
|
LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id |
|
87
|
|
|
WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id} |
|
88
|
|
|
ORDER BY BUDDY_STATUS, BUDDY_ID" |
|
89
|
|
|
) : new DbEmptyIterator(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $user |
|
94
|
|
|
* |
|
95
|
|
|
* @throws BuddyException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function accept($user) { |
|
98
|
|
|
if ($this->playerSenderId == $user['id']) { |
|
99
|
|
|
throw new BuddyException('buddy_err_accept_own', ERR_ERROR); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($this->playerOwnerId != $user['id']) { |
|
103
|
|
|
throw new BuddyException('buddy_err_accept_alien', ERR_ERROR); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) { |
|
107
|
|
|
throw new BuddyException('buddy_err_accept_already', ERR_WARNING); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($this->buddyStatusId == BUDDY_REQUEST_DENIED) { |
|
111
|
|
|
throw new BuddyException('buddy_err_accept_denied', ERR_ERROR); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($this->buddyStatusId != BUDDY_REQUEST_WAITING) { |
|
115
|
|
|
throw new BuddyException('buddy_err_unknown_status', ERR_ERROR); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$result = $this->db_buddy_update_status(BUDDY_REQUEST_ACTIVE); |
|
119
|
|
|
if ($result) { |
|
120
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_accept_title', 'buddy_msg_accept_text'); |
|
121
|
|
|
throw new BuddyException('buddy_err_accept_none', ERR_NONE); |
|
122
|
|
|
} else { |
|
123
|
|
|
throw new BuddyException('buddy_err_accept_internal', ERR_ERROR); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $user |
|
129
|
|
|
* |
|
130
|
|
|
* @throws BuddyException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function decline($user) { |
|
133
|
|
|
if ($this->playerSenderId != $user['id'] && $this->playerOwnerId != $user['id']) { |
|
134
|
|
|
throw new BuddyException('buddy_err_delete_alien', ERR_ERROR); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($this->buddyStatusId == BUDDY_REQUEST_ACTIVE) { |
|
138
|
|
|
// Existing friendship |
|
139
|
|
|
$ex_friend_id = $this->playerSenderId == $user['id'] ? $this->playerOwnerId : $this->playerSenderId; |
|
140
|
|
|
|
|
141
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text'); |
|
142
|
|
|
|
|
143
|
|
|
$this->delete(); |
|
144
|
|
|
throw new BuddyException('buddy_err_unfriend_none', ERR_NONE); |
|
145
|
|
|
} elseif ($this->playerSenderId == $user['id']) { |
|
146
|
|
|
// Player's outcoming request - either denied or waiting |
|
147
|
|
|
$this->delete(); |
|
148
|
|
|
throw new BuddyException('buddy_err_delete_own', ERR_NONE); |
|
149
|
|
|
} elseif ($this->buddyStatusId == BUDDY_REQUEST_WAITING) { |
|
150
|
|
|
// Deny incoming request |
|
151
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($this->playerSenderId, $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text'); |
|
152
|
|
|
|
|
153
|
|
|
$this->db_buddy_update_status(BUDDY_REQUEST_DENIED); |
|
154
|
|
|
throw new BuddyException('buddy_err_deny_none', ERR_NONE); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $user |
|
160
|
|
|
* @param mixed $new_friend_id |
|
161
|
|
|
* @param string $new_friend_name_unsafe |
|
162
|
|
|
* @param string $new_request_text_safe |
|
163
|
|
|
* @param BuddyRoutingParams $cBuddy |
|
164
|
|
|
* |
|
165
|
|
|
* @throws BuddyException |
|
166
|
|
|
*/ |
|
167
|
|
|
public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe, $cBuddy) { |
|
|
|
|
|
|
168
|
|
|
$new_friend_row = array(); |
|
169
|
|
|
if ($cBuddy->new_friend_id_safe) { |
|
170
|
|
|
$new_friend_row = DBStaticUser::db_user_by_id($cBuddy->new_friend_id_safe, true, '`id`, `username`'); |
|
171
|
|
|
} elseif ($cBuddy->new_friend_name_unsafe) { |
|
172
|
|
|
$new_friend_row = DBStaticUser::db_user_by_username($cBuddy->new_friend_name_unsafe, true, '`id`, `username`'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (empty($new_friend_row) || empty($new_friend_row['id'])) { |
|
176
|
|
|
return; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($new_friend_row['id'] == $user['id']) { |
|
180
|
|
|
unset($new_friend_row); |
|
181
|
|
|
throw new BuddyException('buddy_err_adding_self', ERR_ERROR); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// Checking for user name & request text - in case if it was request to adding new request |
|
185
|
|
|
if ($new_request_text_safe) { |
|
186
|
|
|
$check_relation = $this->db_buddy_check_relation($user, $new_friend_row); |
|
187
|
|
|
if (isset($check_relation['BUDDY_ID'])) { |
|
188
|
|
|
throw new BuddyException('buddy_err_adding_exists', ERR_WARNING); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text'); |
|
192
|
|
|
|
|
193
|
|
|
$this->playerSenderId = idval($user['id']); |
|
194
|
|
|
$this->playerOwnerId = idval($new_friend_row['id']); |
|
195
|
|
|
$this->buddyStatusId = BUDDY_REQUEST_WAITING; |
|
196
|
|
|
$this->requestText = $new_request_text_safe; |
|
197
|
|
|
|
|
198
|
|
|
$this->insert(); |
|
199
|
|
|
throw new BuddyException('buddy_err_adding_none', ERR_NONE); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function isEmpty() { |
|
204
|
|
|
return |
|
205
|
|
|
$this->buddyStatusId == BUDDY_REQUEST_NOT_SET |
|
206
|
|
|
|| |
|
207
|
|
|
empty($this->playerSenderId) |
|
208
|
|
|
|| |
|
209
|
|
|
empty($this->playerOwnerId); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param array $row |
|
214
|
|
|
*/ |
|
215
|
|
|
// TODO - PROOF OF CONCEPTION |
|
216
|
|
|
public function setRow($row) { |
|
217
|
|
|
// foreach($this->getProperties() as $propertyName => $cork) { |
|
|
|
|
|
|
218
|
|
|
// } |
|
219
|
|
|
$this->dbId = $row[static::$idField]; |
|
220
|
|
|
$this->playerSenderId = $row['BUDDY_SENDER_ID']; |
|
221
|
|
|
$this->playerOwnerId = $row['BUDDY_OWNER_ID']; |
|
222
|
|
|
$this->buddyStatusId = $row['BUDDY_STATUS']; |
|
223
|
|
|
$this->requestText = $row['BUDDY_REQUEST']; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Compiles object data into db row |
|
228
|
|
|
* |
|
229
|
|
|
* @return array |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getRow($withDbId = true) { |
|
232
|
|
|
$row = array( |
|
233
|
|
|
static::$idField => $this->dbId, |
|
234
|
|
|
'BUDDY_SENDER_ID' => $this->playerSenderId, |
|
235
|
|
|
'BUDDY_OWNER_ID' => $this->playerOwnerId, |
|
236
|
|
|
'BUDDY_STATUS' => $this->buddyStatusId, |
|
237
|
|
|
'BUDDY_REQUEST' => $this->requestText, |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
if (!$withDbId) { |
|
241
|
|
|
unset($row[static::$idField]); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $row; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param BuddyRoutingParams $cBuddy |
|
249
|
|
|
* |
|
250
|
|
|
* @throws BuddyException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function route($cBuddy) { |
|
253
|
|
|
if ($cBuddy->buddy_id) { |
|
254
|
|
|
$this->load($cBuddy->buddy_id); |
|
255
|
|
|
|
|
256
|
|
|
if ($this->isEmpty()) { |
|
257
|
|
|
throw new BuddyException('buddy_err_not_exist', ERR_ERROR); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
switch ($cBuddy->mode) { |
|
261
|
|
|
case 'accept': |
|
262
|
|
|
$this->accept($cBuddy->user); |
|
263
|
|
|
break; |
|
264
|
|
|
case 'delete': |
|
265
|
|
|
$this->decline($cBuddy->user); |
|
266
|
|
|
break; |
|
267
|
|
|
} |
|
268
|
|
|
} else { |
|
269
|
|
|
// New request? |
|
270
|
|
|
// Checking for user ID - in case if it was request from outside buddy system |
|
271
|
|
|
if (!empty($cBuddy->new_friend_id_safe) || !empty($cBuddy->new_friend_name_unsafe)) { |
|
272
|
|
|
$this->beFriend($cBuddy->user, $cBuddy->new_friend_id_safe, $cBuddy->new_friend_name_unsafe, $cBuddy->new_request_text, $cBuddy); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
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.