Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java

417 lines
17 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.messenger;
import com.eu.habbo.Emulator;
2019-04-22 01:42:00 +02:00
import com.eu.habbo.habbohotel.achievements.Achievement;
import com.eu.habbo.habbohotel.achievements.AchievementManager;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboInfo;
import com.eu.habbo.habbohotel.users.HabboManager;
import com.eu.habbo.messages.outgoing.friends.UpdateFriendComposer;
import com.eu.habbo.plugin.events.users.friends.UserAcceptFriendRequestEvent;
import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;
2018-11-17 14:28:00 +01:00
import org.apache.commons.lang3.StringUtils;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
2019-05-26 20:14:53 +02:00
public class Messenger {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(Messenger.class);
2018-07-06 15:30:00 +02:00
//Configuration. Loaded from database & updated accordingly.
public static boolean SAVE_PRIVATE_CHATS = false;
public static int MAXIMUM_FRIENDS = 200;
2019-04-22 01:42:00 +02:00
public static int MAXIMUM_FRIENDS_HC = 500;
2018-07-06 15:30:00 +02:00
private final ConcurrentHashMap<Integer, MessengerBuddy> friends;
private final THashSet<FriendRequest> friendRequests;
2019-05-26 20:14:53 +02:00
public Messenger() {
2018-09-28 21:25:00 +02:00
this.friends = new ConcurrentHashMap<>();
this.friendRequests = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public static void unfriend(int userOne, int userTwo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM messenger_friendships WHERE (user_one_id = ? AND user_two_id = ?) OR (user_one_id = ? AND user_two_id = ?)")) {
statement.setInt(1, userOne);
statement.setInt(2, userTwo);
statement.setInt(3, userTwo);
statement.setInt(4, userOne);
statement.execute();
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
}
public static THashSet<MessengerBuddy> searchUsers(String username) {
THashSet<MessengerBuddy> users = new THashSet<>();
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE username LIKE ? ORDER BY username ASC LIMIT " + Emulator.getConfig().getInt("hotel.messenger.search.maxresults"))) {
statement.setString(1, username + "%");
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
users.add(new MessengerBuddy(set, false));
}
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return users;
}
2021-09-01 16:39:03 +02:00
/**
* @deprecated
* This method is no longer used and is only kept to avoid breaking any plugins
*/
@Deprecated
2019-05-26 20:14:53 +02:00
public static boolean canFriendRequest(Habbo habbo, String friend) {
Habbo user = Emulator.getGameEnvironment().getHabboManager().getHabbo(friend);
HabboInfo habboInfo;
if (user != null) {
habboInfo = user.getHabboInfo();
} else {
habboInfo = HabboManager.getOfflineHabboInfo(friend);
}
if (habboInfo == null) {
return false;
}
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM messenger_friendrequests WHERE (user_to_id = ? AND user_from_id = ?) OR (user_to_id = ? AND user_from_id = ?) LIMIT 1")) {
statement.setInt(1, habbo.getHabboInfo().getId());
statement.setInt(2, habboInfo.getId());
statement.setInt(3, habboInfo.getId());
statement.setInt(4, habbo.getHabboInfo().getId());
try (ResultSet set = statement.executeQuery()) {
if (!set.next()) {
return true;
}
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return false;
}
public static boolean friendRequested(int userFrom, int userTo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM messenger_friendrequests WHERE user_to_id = ? AND user_from_id = ? LIMIT 1")) {
statement.setInt(1, userFrom);
statement.setInt(2, userTo);
try (ResultSet set = statement.executeQuery()) {
if (set.next()) {
return true;
}
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return false;
}
public static void makeFriendRequest(int userFrom, int userTo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO messenger_friendrequests (user_to_id, user_from_id) VALUES (?, ?)")) {
statement.setInt(1, userTo);
statement.setInt(2, userFrom);
statement.executeUpdate();
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
}
public static int getFriendCount(int userId) {
Habbo habbo = Emulator.getGameServer().getGameClientManager().getHabbo(userId);
if (habbo != null)
return habbo.getMessenger().getFriends().size();
int count = 0;
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT count(id) as count FROM messenger_friendships WHERE user_one_id = ?")) {
statement.setInt(1, userId);
try (ResultSet set = statement.executeQuery()) {
if (set.next())
count = set.getInt("count");
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return count;
}
public static THashMap<Integer, THashSet<MessengerBuddy>> getFriends(int userId) {
THashMap<Integer, THashSet<MessengerBuddy>> map = new THashMap<>();
map.put(1, new THashSet<>());
map.put(2, new THashSet<>());
map.put(3, new THashSet<>());
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT users.id, users.look, users.username, messenger_friendships.relation FROM messenger_friendships INNER JOIN users ON users.id = messenger_friendships.user_two_id WHERE user_one_id = ? ORDER BY RAND() LIMIT 50")) {
statement.setInt(1, userId);
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
if (set.getInt("relation") == 0)
continue;
MessengerBuddy buddy = new MessengerBuddy(set.getInt("id"), set.getString("username"), set.getString("look"), (short) set.getInt("relation"), userId);
map.get((int) buddy.getRelation()).add(buddy);
}
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return map;
}
public static void checkFriendSizeProgress(Habbo habbo) {
int progress = habbo.getHabboStats().getAchievementProgress(Emulator.getGameEnvironment().getAchievementManager().getAchievement("FriendListSize"));
int toProgress = 1;
Achievement achievement = Emulator.getGameEnvironment().getAchievementManager().getAchievement("FriendListSize");
if (achievement == null)
return;
if (progress > 0) {
toProgress = habbo.getMessenger().getFriends().size() - progress;
if (toProgress < 0) {
return;
}
}
AchievementManager.progressAchievement(habbo, achievement, toProgress);
}
public void loadFriends(Habbo habbo) {
2018-07-06 15:30:00 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection();
2019-05-26 20:14:53 +02:00
PreparedStatement statement = connection.prepareStatement("SELECT " +
"users.id, " +
"users.username, " +
"users.gender, " +
"users.online, " +
"users.look, " +
"users.motto, " +
"messenger_friendships.* FROM messenger_friendships INNER JOIN users ON messenger_friendships.user_two_id = users.id WHERE user_one_id = ?")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, habbo.getHabboInfo().getId());
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
2020-06-07 14:21:29 +02:00
MessengerBuddy buddy = new MessengerBuddy(set);
if (buddy.getId() == habbo.getHabboInfo().getId()) {
continue;
}
this.friends.putIfAbsent(set.getInt("id"), buddy);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public MessengerBuddy loadFriend(Habbo habbo, int userId) {
2018-07-06 15:30:00 +02:00
MessengerBuddy buddy = null;
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT " +
2019-05-26 20:14:53 +02:00
"users.id, " +
"users.username, " +
"users.gender, " +
"users.online, " +
"users.look, " +
"users.motto, " +
"messenger_friendships.* FROM messenger_friendships INNER JOIN users ON messenger_friendships.user_two_id = users.id WHERE user_one_id = ? AND user_two_id = ? LIMIT 1")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, habbo.getHabboInfo().getId());
statement.setInt(2, userId);
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
2018-07-06 15:30:00 +02:00
buddy = new MessengerBuddy(set);
this.friends.putIfAbsent(set.getInt("id"), buddy);
}
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
return buddy;
}
2019-05-26 20:14:53 +02:00
public void loadFriendRequests(Habbo habbo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT users.id, users.username, users.look FROM messenger_friendrequests INNER JOIN users ON user_from_id = users.id WHERE user_to_id = ?")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, habbo.getHabboInfo().getId());
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery()) {
while (set.next()) {
2019-03-18 02:22:00 +01:00
this.friendRequests.add(new FriendRequest(set));
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public void removeBuddy(int id) {
2018-07-06 15:30:00 +02:00
this.friends.remove(id);
}
2019-05-26 20:14:53 +02:00
public void removeBuddy(Habbo habbo) {
2018-07-06 15:30:00 +02:00
this.friends.remove(habbo.getHabboInfo().getId());
}
2019-05-26 20:14:53 +02:00
public void addBuddy(MessengerBuddy buddy) {
this.friends.put(buddy.getId(), buddy);
}
2019-05-26 20:14:53 +02:00
public THashSet<MessengerBuddy> getFriends(String username) {
2018-09-28 21:25:00 +02:00
THashSet<MessengerBuddy> users = new THashSet<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (Map.Entry<Integer, MessengerBuddy> map : this.friends.entrySet()) {
if (StringUtils.containsIgnoreCase(map.getValue().getUsername(), username)) {
2018-07-06 15:30:00 +02:00
users.add(map.getValue());
}
}
return users;
}
2019-05-26 20:14:53 +02:00
public void connectionChanged(Habbo owner, boolean online, boolean inRoom) {
if (owner != null) {
for (Map.Entry<Integer, MessengerBuddy> map : this.getFriends().entrySet()) {
2018-07-06 15:30:00 +02:00
if (map.getValue().getOnline() == 0)
continue;
Habbo habbo = Emulator.getGameServer().getGameClientManager().getHabbo(map.getKey());
2019-05-26 20:14:53 +02:00
if (habbo != null) {
if (habbo.getMessenger() != null) {
2018-07-06 15:30:00 +02:00
MessengerBuddy buddy = habbo.getMessenger().getFriend(owner.getHabboInfo().getId());
2019-05-26 20:14:53 +02:00
if (buddy != null) {
2018-07-06 15:30:00 +02:00
buddy.setOnline(online);
buddy.inRoom(inRoom);
buddy.setLook(owner.getHabboInfo().getLook());
buddy.setGender(owner.getHabboInfo().getGender());
buddy.setUsername(owner.getHabboInfo().getUsername());
2022-03-19 20:34:21 +01:00
habbo.getClient().sendResponse(new UpdateFriendComposer(habbo, buddy, 0));
2018-07-06 15:30:00 +02:00
}
}
}
}
}
}
2019-05-26 20:14:53 +02:00
public void deleteAllFriendRequests(int userTo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM messenger_friendrequests WHERE user_to_id = ?")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, userTo);
statement.executeUpdate();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public int deleteFriendRequests(int userFrom, int userTo) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM messenger_friendrequests WHERE (user_to_id = ? AND user_from_id = ?) OR (user_to_id = ? AND user_from_id = ?)")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, userTo);
statement.setInt(2, userFrom);
statement.setInt(4, userTo);
statement.setInt(3, userFrom);
return statement.executeUpdate();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
return 0;
}
2019-05-26 20:14:53 +02:00
2018-07-06 15:30:00 +02:00
//TODO Needs redesign. userFrom is redundant.
2019-05-26 20:14:53 +02:00
public void acceptFriendRequest(int userFrom, int userTo) {
2019-03-18 02:22:00 +01:00
int count = this.deleteFriendRequests(userFrom, userTo);
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (count > 0) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO messenger_friendships (user_one_id, user_two_id, friends_since) VALUES (?, ?, ?)")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, userFrom);
statement.setInt(2, userTo);
statement.setInt(3, Emulator.getIntUnixTimestamp());
statement.execute();
statement.setInt(1, userTo);
statement.setInt(2, userFrom);
statement.setInt(3, Emulator.getIntUnixTimestamp());
statement.execute();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
Habbo habboTo = Emulator.getGameServer().getGameClientManager().getHabbo(userTo);
Habbo habboFrom = Emulator.getGameServer().getGameClientManager().getHabbo(userFrom);
2019-05-26 20:14:53 +02:00
if (habboTo != null && habboFrom != null) {
2018-07-06 15:30:00 +02:00
MessengerBuddy to = new MessengerBuddy(habboFrom, habboTo.getHabboInfo().getId());
MessengerBuddy from = new MessengerBuddy(habboTo, habboFrom.getHabboInfo().getId());
habboTo.getMessenger().friends.putIfAbsent(habboFrom.getHabboInfo().getId(), to);
habboFrom.getMessenger().friends.putIfAbsent(habboTo.getHabboInfo().getId(), from);
2019-05-26 20:14:53 +02:00
if (Emulator.getPluginManager().fireEvent(new UserAcceptFriendRequestEvent(habboTo, from)).isCancelled()) {
2018-07-06 15:30:00 +02:00
this.removeBuddy(userTo);
return;
}
2022-03-19 20:34:21 +01:00
habboTo.getClient().sendResponse(new UpdateFriendComposer(habboTo, to, 1));
habboFrom.getClient().sendResponse(new UpdateFriendComposer(habboFrom, from, 1));
2019-05-26 20:14:53 +02:00
} else if (habboTo != null) {
2022-03-19 20:34:21 +01:00
habboTo.getClient().sendResponse(new UpdateFriendComposer(habboTo, this.loadFriend(habboTo, userFrom), 1));
2019-05-26 20:14:53 +02:00
} else if (habboFrom != null) {
2022-03-19 20:34:21 +01:00
habboFrom.getClient().sendResponse(new UpdateFriendComposer(habboFrom, this.loadFriend(habboFrom, userTo), 1));
2018-07-06 15:30:00 +02:00
}
}
}
2019-05-26 20:14:53 +02:00
public ConcurrentHashMap<Integer, MessengerBuddy> getFriends() {
2018-07-06 15:30:00 +02:00
return this.friends;
}
2019-05-26 20:14:53 +02:00
public THashSet<FriendRequest> getFriendRequests() {
synchronized (this.friendRequests) {
2018-07-06 15:30:00 +02:00
return this.friendRequests;
}
}
2019-05-26 20:14:53 +02:00
public FriendRequest findFriendRequest(String username) {
synchronized (this.friendRequests) {
for (FriendRequest friendRequest : this.friendRequests) {
if (friendRequest.getUsername().equalsIgnoreCase(username)) {
2018-07-06 15:30:00 +02:00
return friendRequest;
}
}
}
return null;
}
2019-05-26 20:14:53 +02:00
public MessengerBuddy getFriend(int id) {
2018-07-06 15:30:00 +02:00
return this.friends.get(id);
}
2019-05-26 20:14:53 +02:00
public void dispose() {
synchronized (this.friends) {
this.friends.clear();
2019-04-22 01:42:00 +02:00
}
2019-05-26 20:14:53 +02:00
synchronized (this.friendRequests) {
this.friendRequests.clear();
2019-04-22 01:42:00 +02:00
}
}
2018-07-06 15:30:00 +02:00
}