|
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
|
|
|
class Buddy extends Entity { |
|
14
|
|
|
|
|
15
|
|
|
public static $tableName = 'buddy'; |
|
16
|
|
|
public static $idField = 'BUDDY_ID'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Who makes buddy request |
|
20
|
|
|
* |
|
21
|
|
|
* @var int|float|string $playerSenderId |
|
22
|
|
|
*/ |
|
23
|
|
|
public $playerSenderId = 0; |
|
24
|
|
|
/** |
|
25
|
|
|
* To whom this buddy request made |
|
26
|
|
|
* |
|
27
|
|
|
* @var int|float|string $playerOwnerId |
|
28
|
|
|
*/ |
|
29
|
|
|
public $playerOwnerId = 0; |
|
30
|
|
|
/** |
|
31
|
|
|
* Current buddy request status |
|
32
|
|
|
* |
|
33
|
|
|
* @var int $buddyStatusId |
|
34
|
|
|
*/ |
|
35
|
|
|
public $buddyStatusId = BUDDY_REQUEST_NOT_SET; |
|
36
|
|
|
/** |
|
37
|
|
|
* Request text |
|
38
|
|
|
* |
|
39
|
|
|
* @var string $requestText |
|
40
|
|
|
*/ |
|
41
|
|
|
public $requestText = ''; |
|
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->row['BUDDY_ID']); |
|
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
|
|
View Code Duplication |
public function db_buddy_check_relation($user, $new_friend_row) { |
|
|
|
|
|
|
53
|
|
|
return static::$dbStatic->doQueryFetch( |
|
54
|
|
|
"SELECT `BUDDY_ID` FROM `{{buddy}}` WHERE |
|
55
|
|
|
(`BUDDY_SENDER_ID` = {$user['id']} AND `BUDDY_OWNER_ID` = {$new_friend_row['id']}) |
|
56
|
|
|
OR |
|
57
|
|
|
(`BUDDY_SENDER_ID` = {$new_friend_row['id']} AND `BUDDY_OWNER_ID` = {$user['id']}) |
|
58
|
|
|
LIMIT 1 FOR UPDATE;" |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param db_mysql $db |
|
64
|
|
|
* @param mixed $user_id |
|
65
|
|
|
* |
|
66
|
|
|
* @return DbEmptyIterator|DbMysqliResultIterator |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
public static function db_buddy_list_by_user($db, $user_id) { |
|
|
|
|
|
|
69
|
|
|
return ($user_id = idval($user_id)) ? $db->doQueryIterator( |
|
70
|
|
|
"SELECT |
|
71
|
|
|
b.*, |
|
72
|
|
|
IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) AS BUDDY_USER_ID, |
|
73
|
|
|
u.username AS BUDDY_USER_NAME, |
|
74
|
|
|
p.name AS BUDDY_PLANET_NAME, |
|
75
|
|
|
p.galaxy AS BUDDY_PLANET_GALAXY, |
|
76
|
|
|
p.system AS BUDDY_PLANET_SYSTEM, |
|
77
|
|
|
p.planet AS BUDDY_PLANET_PLANET, |
|
78
|
|
|
a.id AS BUDDY_ALLY_ID, |
|
79
|
|
|
a.ally_name AS BUDDY_ALLY_NAME, |
|
80
|
|
|
u.onlinetime |
|
81
|
|
|
FROM {{buddy}} AS b |
|
82
|
|
|
LEFT JOIN {{users}} AS u ON u.id = IF(b.BUDDY_OWNER_ID = {$user_id}, b.BUDDY_SENDER_ID, b.BUDDY_OWNER_ID) |
|
83
|
|
|
LEFT JOIN {{planets}} AS p ON p.id_owner = u.id AND p.id = id_planet |
|
84
|
|
|
LEFT JOIN {{alliance}} AS a ON a.id = u.ally_id |
|
85
|
|
|
WHERE (`BUDDY_OWNER_ID` = {$user_id}) OR `BUDDY_SENDER_ID` = {$user_id} |
|
86
|
|
|
ORDER BY BUDDY_STATUS, BUDDY_ID" |
|
87
|
|
|
) : new DbEmptyIterator(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $user |
|
92
|
|
|
* |
|
93
|
|
|
* @throws BuddyException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function accept($user) { |
|
96
|
|
|
$buddy_row = $this->row; |
|
97
|
|
|
|
|
98
|
|
|
if ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) { |
|
99
|
|
|
throw new BuddyException('buddy_err_accept_own', ERR_ERROR); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($buddy_row['BUDDY_OWNER_ID'] != $user['id']) { |
|
103
|
|
|
throw new BuddyException('buddy_err_accept_alien', ERR_ERROR); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) { |
|
107
|
|
|
throw new BuddyException('buddy_err_accept_already', ERR_WARNING); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_DENIED) { |
|
111
|
|
|
throw new BuddyException('buddy_err_accept_denied', ERR_ERROR); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($buddy_row['BUDDY_STATUS'] != 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
|
|
View Code Duplication |
if ($result) { |
|
|
|
|
|
|
120
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $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
|
|
|
$buddy_row = $this->row; |
|
134
|
|
|
|
|
135
|
|
|
if ($buddy_row['BUDDY_SENDER_ID'] != $user['id'] && $buddy_row['BUDDY_OWNER_ID'] != $user['id']) { |
|
136
|
|
|
throw new BuddyException('buddy_err_delete_alien', ERR_ERROR); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_ACTIVE) { |
|
140
|
|
|
// Existing friendship |
|
141
|
|
|
$ex_friend_id = $buddy_row['BUDDY_SENDER_ID'] == $user['id'] ? $buddy_row['BUDDY_OWNER_ID'] : $buddy_row['BUDDY_SENDER_ID']; |
|
142
|
|
|
|
|
143
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($ex_friend_id, $user, 'buddy_msg_unfriend_title', 'buddy_msg_unfriend_text'); |
|
144
|
|
|
|
|
145
|
|
|
$this->delete(); |
|
146
|
|
|
throw new BuddyException('buddy_err_unfriend_none', ERR_NONE); |
|
147
|
|
|
} elseif ($buddy_row['BUDDY_SENDER_ID'] == $user['id']) { |
|
148
|
|
|
// Player's outcoming request - either denied or waiting |
|
149
|
|
|
$this->delete(); |
|
150
|
|
|
throw new BuddyException('buddy_err_delete_own', ERR_NONE); |
|
151
|
|
|
} elseif ($buddy_row['BUDDY_STATUS'] == BUDDY_REQUEST_WAITING) { |
|
152
|
|
|
// Deny incoming request |
|
153
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($buddy_row['BUDDY_SENDER_ID'], $user, 'buddy_msg_deny_title', 'buddy_msg_deny_text'); |
|
154
|
|
|
|
|
155
|
|
|
$this->db_buddy_update_status(BUDDY_REQUEST_DENIED); |
|
156
|
|
|
throw new BuddyException('buddy_err_deny_none', ERR_NONE); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param array $user |
|
162
|
|
|
* @param mixed $new_friend_id |
|
163
|
|
|
* @param string $new_friend_name_unsafe |
|
164
|
|
|
* @param string $new_request_text_safe |
|
165
|
|
|
* |
|
166
|
|
|
* @throws BuddyException |
|
167
|
|
|
*/ |
|
168
|
|
|
public function beFriend($user, $new_friend_id, $new_friend_name_unsafe, $new_request_text_safe) { |
|
169
|
|
|
$new_friend_row = array(); |
|
170
|
|
|
if ($new_friend_id) { |
|
171
|
|
|
$new_friend_row = DBStaticUser::db_user_by_id($new_friend_id, true, '`id`, `username`'); |
|
172
|
|
|
} elseif ($new_friend_name_unsafe) { |
|
173
|
|
|
$new_friend_row = DBStaticUser::db_user_by_username($new_friend_name_unsafe, true, '`id`, `username`'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (empty($new_friend_row) || empty($new_friend_row['id'])) { |
|
177
|
|
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
View Code Duplication |
if ($new_friend_row['id'] == $user['id']) { |
|
|
|
|
|
|
181
|
|
|
unset($new_friend_row); |
|
182
|
|
|
throw new BuddyException('buddy_err_adding_self', ERR_ERROR); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// Checking for user name & request text - in case if it was request to adding new request |
|
186
|
|
|
if ($new_request_text_safe) { |
|
187
|
|
|
$check_relation = $this->db_buddy_check_relation($user, $new_friend_row); |
|
188
|
|
|
if (isset($check_relation['BUDDY_ID'])) { |
|
189
|
|
|
throw new BuddyException('buddy_err_adding_exists', ERR_WARNING); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
DBStaticMessages::msgSendFromPlayerBuddy($new_friend_row['id'], $user, 'buddy_msg_adding_title', 'buddy_msg_adding_text'); |
|
193
|
|
|
|
|
194
|
|
|
$this->row['BUDDY_SENDER_ID'] = idval($user['id']); |
|
195
|
|
|
$this->row['BUDDY_OWNER_ID'] = idval($new_friend_row['id']); |
|
196
|
|
|
$this->row['BUDDY_STATUS'] = BUDDY_REQUEST_WAITING; |
|
197
|
|
|
$this->row['BUDDY_REQUEST'] = $new_request_text_safe; |
|
198
|
|
|
|
|
199
|
|
|
$this->insert(); |
|
200
|
|
|
throw new BuddyException('buddy_err_adding_none', ERR_NONE); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function isEmpty() { |
|
205
|
|
|
return $this->row['BUDDY_STATUS'] == BUDDY_REQUEST_NOT_SET || empty($this->row['BUDDY_SENDER_ID']) || empty($this->row['BUDDY_OWNER_ID']); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
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.