2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.habbohotel.rooms;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.habbohotel.users.Habbo;
|
|
|
|
import com.eu.habbo.habbohotel.users.HabboItem;
|
|
|
|
import com.eu.habbo.messages.outgoing.MessageComposer;
|
|
|
|
import com.eu.habbo.messages.outgoing.inventory.AddHabboItemComposer;
|
|
|
|
import com.eu.habbo.messages.outgoing.inventory.InventoryRefreshComposer;
|
|
|
|
import com.eu.habbo.messages.outgoing.rooms.users.RoomUserStatusComposer;
|
|
|
|
import com.eu.habbo.messages.outgoing.trading.*;
|
2019-08-04 17:11:11 +02:00
|
|
|
import com.eu.habbo.threading.runnables.QueryDeleteHabboItem;
|
2018-07-06 15:30:00 +02:00
|
|
|
import gnu.trove.set.hash.THashSet;
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public class RoomTrade {
|
2018-07-06 15:30:00 +02:00
|
|
|
//Configuration. Loaded from database & updated accordingly.
|
|
|
|
public static boolean TRADING_ENABLED = true;
|
2018-10-07 00:28:00 +02:00
|
|
|
public static boolean TRADING_REQUIRES_PERK = true;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
private final List<RoomTradeUser> users;
|
|
|
|
private final Room room;
|
2019-05-26 20:14:53 +02:00
|
|
|
private boolean tradeCompleted;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public RoomTrade(Habbo userOne, Habbo userTwo, Room room) {
|
2018-09-28 21:25:00 +02:00
|
|
|
this.users = new ArrayList<>();
|
2018-07-06 15:30:00 +02:00
|
|
|
this.tradeCompleted = false;
|
|
|
|
|
|
|
|
this.users.add(new RoomTradeUser(userOne));
|
|
|
|
this.users.add(new RoomTradeUser(userTwo));
|
|
|
|
this.room = room;
|
2018-10-07 00:28:00 +02:00
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void start() {
|
2018-10-07 00:28:00 +02:00
|
|
|
this.initializeTradeStatus();
|
|
|
|
this.openTrade();
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
protected void initializeTradeStatus() {
|
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
|
|
|
if (!roomTradeUser.getHabbo().getRoomUnit().hasStatus(RoomUnitStatus.TRADING)) {
|
2018-09-12 18:45:00 +02:00
|
|
|
roomTradeUser.getHabbo().getRoomUnit().setStatus(RoomUnitStatus.TRADING, "");
|
2019-05-26 20:14:53 +02:00
|
|
|
if (!roomTradeUser.getHabbo().getRoomUnit().isWalking())
|
2019-03-18 02:22:00 +01:00
|
|
|
this.room.sendComposer(new RoomUserStatusComposer(roomTradeUser.getHabbo().getRoomUnit()).compose());
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-07 00:28:00 +02:00
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
protected void openTrade() {
|
2019-03-18 02:22:00 +01:00
|
|
|
this.sendMessageToUsers(new TradeStartComposer(this));
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void offerItem(Habbo habbo, HabboItem item) {
|
2019-03-18 02:22:00 +01:00
|
|
|
RoomTradeUser user = this.getRoomTradeUserForHabbo(habbo);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (user.getItems().contains(item))
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
habbo.getInventory().getItemsComponent().removeHabboItem(item);
|
2018-07-06 15:30:00 +02:00
|
|
|
user.getItems().add(item);
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
this.clearAccepted();
|
|
|
|
this.updateWindow();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
|
|
|
|
public void offerMultipleItems(Habbo habbo, THashSet<HabboItem> items) {
|
2019-03-18 02:22:00 +01:00
|
|
|
RoomTradeUser user = this.getRoomTradeUserForHabbo(habbo);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : items) {
|
|
|
|
if (!user.getItems().contains(item)) {
|
2018-10-07 00:28:00 +02:00
|
|
|
habbo.getInventory().getItemsComponent().removeHabboItem(item);
|
2018-07-06 15:30:00 +02:00
|
|
|
user.getItems().add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
this.clearAccepted();
|
|
|
|
this.updateWindow();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void removeItem(Habbo habbo, HabboItem item) {
|
2019-03-18 02:22:00 +01:00
|
|
|
RoomTradeUser user = this.getRoomTradeUserForHabbo(habbo);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (!user.getItems().contains(item))
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
habbo.getInventory().getItemsComponent().addItem(item);
|
2018-07-06 15:30:00 +02:00
|
|
|
user.getItems().remove(item);
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
this.clearAccepted();
|
|
|
|
this.updateWindow();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void accept(Habbo habbo, boolean value) {
|
2019-03-18 02:22:00 +01:00
|
|
|
RoomTradeUser user = this.getRoomTradeUserForHabbo(habbo);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
user.setAccepted(value);
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
this.sendMessageToUsers(new TradeAcceptedComposer(user));
|
2018-07-06 15:30:00 +02:00
|
|
|
boolean accepted = true;
|
2019-05-26 20:14:53 +02:00
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
|
|
|
if (!roomTradeUser.getAccepted())
|
2018-07-06 15:30:00 +02:00
|
|
|
accepted = false;
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
if (accepted) {
|
2018-10-07 00:28:00 +02:00
|
|
|
this.sendMessageToUsers(new TradingWaitingConfirmComposer());
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void confirm(Habbo habbo) {
|
2019-03-18 02:22:00 +01:00
|
|
|
RoomTradeUser user = this.getRoomTradeUserForHabbo(habbo);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
user.confirm();
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
this.sendMessageToUsers(new TradeAcceptedComposer(user));
|
2018-07-06 15:30:00 +02:00
|
|
|
boolean accepted = true;
|
2019-05-26 20:14:53 +02:00
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
|
|
|
if (!roomTradeUser.getConfirmed())
|
2018-07-06 15:30:00 +02:00
|
|
|
accepted = false;
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
if (accepted) {
|
|
|
|
if (this.tradeItems()) {
|
2018-10-07 00:28:00 +02:00
|
|
|
this.closeWindow();
|
|
|
|
this.sendMessageToUsers(new TradeCompleteComposer());
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2018-10-07 00:28:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
this.room.stopTrade(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
boolean tradeItems() {
|
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
|
|
|
for (HabboItem item : roomTradeUser.getItems()) {
|
|
|
|
if (roomTradeUser.getHabbo().getInventory().getItemsComponent().getHabboItem(item.getId()) != null) {
|
2018-10-07 00:28:00 +02:00
|
|
|
this.sendMessageToUsers(new TradeClosedComposer(roomTradeUser.getHabbo().getRoomUnit().getId(), TradeClosedComposer.ITEMS_NOT_FOUND));
|
2018-07-06 15:30:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomTradeUser userOne = this.users.get(0);
|
|
|
|
RoomTradeUser userTwo = this.users.get(1);
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
int tradeId = 0;
|
|
|
|
|
|
|
|
boolean logTrades = Emulator.getConfig().getBoolean("hotel.log.trades");
|
2019-05-26 20:14:53 +02:00
|
|
|
if (logTrades) {
|
|
|
|
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO room_trade_log (user_one_id, user_two_id, user_one_ip, user_two_ip, timestamp, user_one_item_count, user_two_item_count) VALUES (?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
|
2018-07-06 15:30:00 +02:00
|
|
|
statement.setInt(1, userOne.getHabbo().getHabboInfo().getId());
|
|
|
|
statement.setInt(2, userTwo.getHabbo().getHabboInfo().getId());
|
|
|
|
statement.setString(3, userOne.getHabbo().getHabboInfo().getIpLogin());
|
|
|
|
statement.setString(4, userTwo.getHabbo().getHabboInfo().getIpLogin());
|
|
|
|
statement.setInt(5, Emulator.getIntUnixTimestamp());
|
|
|
|
statement.setInt(6, userOne.getItems().size());
|
|
|
|
statement.setInt(7, userTwo.getItems().size());
|
|
|
|
statement.executeUpdate();
|
2019-05-26 20:14:53 +02:00
|
|
|
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
|
|
|
|
if (generatedKeys.next()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
tradeId = generatedKeys.getInt(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int userOneId = userOne.getHabbo().getHabboInfo().getId();
|
|
|
|
int userTwoId = userTwo.getHabbo().getHabboInfo().getId();
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try (PreparedStatement statement = connection.prepareStatement("UPDATE items SET user_id = ? WHERE id = ? LIMIT 1")) {
|
|
|
|
try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO room_trade_log_items (id, item_id, user_id) VALUES (?, ?, ?)")) {
|
|
|
|
for (HabboItem item : userOne.getItems()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
item.setUserId(userTwoId);
|
|
|
|
statement.setInt(1, userTwoId);
|
|
|
|
statement.setInt(2, item.getId());
|
|
|
|
statement.addBatch();
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (logTrades) {
|
2018-07-06 15:30:00 +02:00
|
|
|
stmt.setInt(1, tradeId);
|
|
|
|
stmt.setInt(2, item.getId());
|
|
|
|
stmt.setInt(3, userOneId);
|
|
|
|
stmt.addBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : userTwo.getItems()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
item.setUserId(userOneId);
|
|
|
|
statement.setInt(1, userOneId);
|
|
|
|
statement.setInt(2, item.getId());
|
|
|
|
statement.addBatch();
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (logTrades) {
|
2018-07-06 15:30:00 +02:00
|
|
|
stmt.setInt(1, tradeId);
|
|
|
|
stmt.setInt(2, item.getId());
|
|
|
|
stmt.setInt(3, userTwoId);
|
|
|
|
stmt.addBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (logTrades) {
|
2018-07-06 15:30:00 +02:00
|
|
|
stmt.executeBatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
statement.executeBatch();
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (SQLException e) {
|
2020-05-03 01:46:07 +02:00
|
|
|
logger.error("Caught SQL exception", e);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
THashSet<HabboItem> itemsUserOne = new THashSet<>(userOne.getItems());
|
|
|
|
THashSet<HabboItem> itemsUserTwo = new THashSet<>(userTwo.getItems());
|
|
|
|
|
|
|
|
userOne.clearItems();
|
|
|
|
userTwo.clearItems();
|
|
|
|
|
2019-08-04 17:11:11 +02:00
|
|
|
int creditsForUserTwo = 0;
|
|
|
|
THashSet<HabboItem> creditFurniUserOne = new THashSet<>();
|
|
|
|
for (HabboItem item : itemsUserOne) {
|
|
|
|
int worth = RoomTrade.getCreditsByItem(item);
|
|
|
|
if (worth > 0) {
|
|
|
|
creditsForUserTwo += worth;
|
|
|
|
creditFurniUserOne.add(item);
|
2019-08-04 17:13:14 +02:00
|
|
|
new QueryDeleteHabboItem(item).run();
|
2019-08-04 17:11:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
itemsUserOne.removeAll(creditFurniUserOne);
|
|
|
|
|
|
|
|
int creditsForUserOne = 0;
|
|
|
|
THashSet<HabboItem> creditFurniUserTwo = new THashSet<>();
|
|
|
|
for (HabboItem item : itemsUserTwo) {
|
|
|
|
int worth = RoomTrade.getCreditsByItem(item);
|
|
|
|
if (worth > 0) {
|
|
|
|
creditsForUserOne += worth;
|
|
|
|
creditFurniUserTwo.add(item);
|
2019-08-04 17:13:14 +02:00
|
|
|
new QueryDeleteHabboItem(item).run();
|
2019-08-04 17:11:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
itemsUserTwo.removeAll(creditFurniUserTwo);
|
|
|
|
|
|
|
|
userOne.getHabbo().giveCredits(creditsForUserOne);
|
|
|
|
userTwo.getHabbo().giveCredits(creditsForUserTwo);
|
|
|
|
|
2018-10-07 00:28:00 +02:00
|
|
|
userOne.getHabbo().getInventory().getItemsComponent().addItems(itemsUserTwo);
|
|
|
|
userTwo.getHabbo().getInventory().getItemsComponent().addItems(itemsUserOne);
|
|
|
|
|
|
|
|
userOne.getHabbo().getClient().sendResponse(new AddHabboItemComposer(itemsUserTwo));
|
|
|
|
userTwo.getHabbo().getClient().sendResponse(new AddHabboItemComposer(itemsUserOne));
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
userOne.getHabbo().getClient().sendResponse(new InventoryRefreshComposer());
|
|
|
|
userTwo.getHabbo().getClient().sendResponse(new InventoryRefreshComposer());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
protected void clearAccepted() {
|
|
|
|
for (RoomTradeUser user : this.users) {
|
2018-07-06 15:30:00 +02:00
|
|
|
user.setAccepted(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
protected void updateWindow() {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.sendMessageToUsers(new TradeUpdateComposer(this));
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
private void returnItems() {
|
|
|
|
for (RoomTradeUser user : this.users) {
|
2018-10-07 00:28:00 +02:00
|
|
|
user.putItemsIntoInventory();
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
|
|
|
|
private void closeWindow() {
|
2019-03-18 02:22:00 +01:00
|
|
|
this.removeStatusses();
|
|
|
|
this.sendMessageToUsers(new TradeCloseWindowComposer());
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void stopTrade(Habbo habbo) {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.removeStatusses();
|
|
|
|
this.clearAccepted();
|
2018-10-07 00:28:00 +02:00
|
|
|
this.returnItems();
|
2019-05-26 20:14:53 +02:00
|
|
|
for (RoomTradeUser user : this.users) {
|
2018-07-06 15:30:00 +02:00
|
|
|
user.clearItems();
|
|
|
|
}
|
|
|
|
this.updateWindow();
|
|
|
|
this.sendMessageToUsers(new TradeClosedComposer(habbo.getHabboInfo().getId(), TradeClosedComposer.USER_CANCEL_TRADE));
|
|
|
|
this.room.stopTrade(this);
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
private void removeStatusses() {
|
|
|
|
for (RoomTradeUser user : this.users) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Habbo habbo = user.getHabbo();
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (habbo == null)
|
2018-07-06 15:30:00 +02:00
|
|
|
continue;
|
|
|
|
|
2018-09-12 18:45:00 +02:00
|
|
|
habbo.getRoomUnit().removeStatus(RoomUnitStatus.TRADING);
|
2018-07-06 15:30:00 +02:00
|
|
|
this.room.sendComposer(new RoomUserStatusComposer(habbo.getRoomUnit()).compose());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public RoomTradeUser getRoomTradeUserForHabbo(Habbo habbo) {
|
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
|
|
|
if (roomTradeUser.getHabbo() == habbo)
|
2018-07-06 15:30:00 +02:00
|
|
|
return roomTradeUser;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void sendMessageToUsers(MessageComposer message) {
|
|
|
|
for (RoomTradeUser roomTradeUser : this.users) {
|
2018-07-06 15:30:00 +02:00
|
|
|
roomTradeUser.getHabbo().getClient().sendResponse(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public List<RoomTradeUser> getRoomTradeUsers() {
|
2018-07-06 15:30:00 +02:00
|
|
|
return this.users;
|
|
|
|
}
|
2019-08-04 17:11:11 +02:00
|
|
|
|
|
|
|
public static int getCreditsByItem(HabboItem item) {
|
|
|
|
if (!item.getBaseItem().getName().startsWith("CF_") && !item.getBaseItem().getName().startsWith("CFC_")) return 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Integer.valueOf(item.getBaseItem().getName().split("_")[1]);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|