From 082e0c0486abddd02d6784f41291b9acb0be8463 Mon Sep 17 00:00:00 2001 From: P0nk Date: Fri, 27 Sep 2024 17:55:42 +0200 Subject: [PATCH] Save chr slots to PG --- src/main/java/client/Client.java | 15 +++-------- .../database/account/AccountRepository.java | 13 +++++++++ src/main/java/net/PacketProcessor.java | 5 ++-- .../handlers/CashOperationHandler.java | 12 +++++---- .../channel/handlers/UseCashItemHandler.java | 7 +++-- src/main/java/service/AccountService.java | 27 +++++++++++++++++++ 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/main/java/client/Client.java b/src/main/java/client/Client.java index 40b60392d8..0b49c514dc 100644 --- a/src/main/java/client/Client.java +++ b/src/main/java/client/Client.java @@ -82,6 +82,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; public class Client extends ChannelInboundHandlerAdapter { private static final Logger log = LoggerFactory.getLogger(Client.class); private static final int MAX_FAILED_LOGIN_ATTEMPTS = 5; + private static final int MAX_CHR_SLOTS = 15; public static final int LOGIN_NOTLOGGEDIN = 0; public static final int LOGIN_SERVER_TRANSITION = 1; @@ -917,20 +918,12 @@ public class Client extends ChannelInboundHandlerAdapter { } public boolean canGainCharacterSlot() { - return characterSlots < 15; + return characterSlots < MAX_CHR_SLOTS; } - public synchronized boolean gainCharacterSlot() { + public boolean gainCharacterSlot() { if (canGainCharacterSlot()) { - try (Connection con = DatabaseConnection.getConnection(); - PreparedStatement ps = con.prepareStatement("UPDATE accounts SET characterslots = ? WHERE id = ?")) { - ps.setInt(1, this.characterSlots += 1); - ps.setInt(2, accId); - ps.executeUpdate(); - - } catch (SQLException e) { - e.printStackTrace(); - } + characterSlots++; return true; } return false; diff --git a/src/main/java/database/account/AccountRepository.java b/src/main/java/database/account/AccountRepository.java index 4a2ba0aea3..a8ad89c290 100644 --- a/src/main/java/database/account/AccountRepository.java +++ b/src/main/java/database/account/AccountRepository.java @@ -81,4 +81,17 @@ public class AccountRepository { .execute() > 0; } } + + public boolean setChrSlots(int accountId, int chrSlots) { + String sql = """ + UPDATE account + SET chr_slots = :chrSlots + WHERE id = :id"""; + try (Handle handle = connection.getHandle()) { + return handle.createUpdate(sql) + .bind("id", accountId) + .bind("chrSlots", chrSlots) + .execute() > 0; + } + } } diff --git a/src/main/java/net/PacketProcessor.java b/src/main/java/net/PacketProcessor.java index 575e6201c4..e5003cf705 100644 --- a/src/main/java/net/PacketProcessor.java +++ b/src/main/java/net/PacketProcessor.java @@ -327,7 +327,7 @@ public final class PacketProcessor { registerHandler(RecvOpcode.TAKE_DAMAGE, new TakeDamageHandler()); registerHandler(RecvOpcode.MOVE_PLAYER, new MovePlayerHandler()); registerHandler(RecvOpcode.USE_CASH_ITEM, new UseCashItemHandler(channelDeps.noteService(), - channelDeps.shopFactory())); + channelDeps.shopFactory(), channelDeps.accountService())); registerHandler(RecvOpcode.USE_ITEM, new UseItemHandler()); registerHandler(RecvOpcode.USE_RETURN_SCROLL, new UseItemHandler()); registerHandler(RecvOpcode.USE_UPGRADE_SCROLL, new ScrollHandler()); @@ -369,7 +369,8 @@ public final class PacketProcessor { registerHandler(RecvOpcode.MESSENGER, new MessengerHandler()); registerHandler(RecvOpcode.NPC_ACTION, new NPCAnimationHandler()); registerHandler(RecvOpcode.CHECK_CASH, new TouchingCashShopHandler()); - registerHandler(RecvOpcode.CASHSHOP_OPERATION, new CashOperationHandler(channelDeps.noteService())); + registerHandler(RecvOpcode.CASHSHOP_OPERATION, new CashOperationHandler(channelDeps.noteService(), + channelDeps.accountService())); registerHandler(RecvOpcode.COUPON_CODE, new CouponCodeHandler()); registerHandler(RecvOpcode.SPAWN_PET, new SpawnPetHandler()); registerHandler(RecvOpcode.MOVE_PET, new MovePetHandler()); diff --git a/src/main/java/net/server/channel/handlers/CashOperationHandler.java b/src/main/java/net/server/channel/handlers/CashOperationHandler.java index 8fa81503ed..445e57918a 100644 --- a/src/main/java/net/server/channel/handlers/CashOperationHandler.java +++ b/src/main/java/net/server/channel/handlers/CashOperationHandler.java @@ -42,6 +42,7 @@ import server.CashShop; import server.CashShop.CashItem; import server.CashShop.CashItemFactory; import server.ItemInformationProvider; +import service.AccountService; import service.NoteService; import tools.PacketCreator; import tools.Pair; @@ -56,9 +57,11 @@ public final class CashOperationHandler extends AbstractPacketHandler { private static final Logger log = LoggerFactory.getLogger(CashOperationHandler.class); private final NoteService noteService; + private final AccountService accountService; - public CashOperationHandler(NoteService noteService) { + public CashOperationHandler(NoteService noteService, AccountService accountService) { this.noteService = noteService; + this.accountService = accountService; } @Override @@ -257,14 +260,13 @@ public final class CashOperationHandler extends AbstractPacketHandler { return; } cs.gainCash(cash, cItem, chr.getWorld()); - if (c.gainCharacterSlot()) { - c.sendPacket(PacketCreator.showBoughtCharacterSlot(c.getCharacterSlots())); - c.sendPacket(PacketCreator.showCash(chr)); - } else { + if (!accountService.addChrSlot(c)) { log.warn("Could not add a chr slot to {}'s account", Character.makeMapleReadable(chr.getName())); c.enableCSActions(); return; } + c.sendPacket(PacketCreator.showBoughtCharacterSlot(c.getCharacterSlots())); + c.sendPacket(PacketCreator.showCash(chr)); } else if (action == 0x0D) { // Take from Cash Inventory Item item = cs.findByCashId(p.readInt()); if (item == null) { diff --git a/src/main/java/net/server/channel/handlers/UseCashItemHandler.java b/src/main/java/net/server/channel/handlers/UseCashItemHandler.java index 84196923d3..405a8a62b6 100644 --- a/src/main/java/net/server/channel/handlers/UseCashItemHandler.java +++ b/src/main/java/net/server/channel/handlers/UseCashItemHandler.java @@ -64,6 +64,7 @@ import server.maps.MapleTVEffect; import server.maps.PlayerShopItem; import server.shop.Shop; import server.shop.ShopFactory; +import service.AccountService; import service.NoteService; import tools.PacketCreator; import tools.Pair; @@ -82,10 +83,12 @@ public final class UseCashItemHandler extends AbstractPacketHandler { private final NoteService noteService; private final ShopFactory shopFactory; + private final AccountService accountService; - public UseCashItemHandler(NoteService noteService, ShopFactory shopFactory) { + public UseCashItemHandler(NoteService noteService, ShopFactory shopFactory, AccountService accountService) { this.noteService = noteService; this.shopFactory = shopFactory; + this.accountService = accountService; } @Override @@ -493,7 +496,7 @@ public final class UseCashItemHandler extends AbstractPacketHandler { remove(c, position, itemId); c.sendPacket(PacketCreator.enableActions()); } else if (itemType == 543) { - if (itemId == ItemId.MAPLE_LIFE_B && !c.gainCharacterSlot()) { + if (itemId == ItemId.MAPLE_LIFE_B && !accountService.addChrSlot(c)) { player.dropMessage(1, "You have already used up all 12 extra character slots."); c.sendPacket(PacketCreator.enableActions()); return; diff --git a/src/main/java/service/AccountService.java b/src/main/java/service/AccountService.java index c9f55b4f09..a7d3141945 100644 --- a/src/main/java/service/AccountService.java +++ b/src/main/java/service/AccountService.java @@ -1,5 +1,6 @@ package service; +import client.Client; import database.account.Account; import database.account.AccountRepository; import lombok.extern.slf4j.Slf4j; @@ -151,4 +152,30 @@ public class AccountService { log.error("Failed to set pic - account:{}, pin:{}", accountId, pic, e); } } + + public boolean addChrSlot(Client c) { + if (!c.gainCharacterSlot()) { + return false; + } + + int newChrSlots = c.getCharacterSlots() + 1; + setChrSlotsMysql(c.getAccID(), newChrSlots); + return setChrSlotsPostgres(c.getAccID(), newChrSlots); + } + + private void setChrSlotsMysql(int accountId, int chrSlots) { + try (Connection con = DatabaseConnection.getConnection(); + PreparedStatement ps = con.prepareStatement("UPDATE accounts SET characterslots = ? WHERE id = ?")) { + ps.setInt(1, chrSlots); + ps.setInt(2, accountId); + ps.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private boolean setChrSlotsPostgres(int accountId, int chrSlots) { + return accountRepository.setChrSlots(accountId, chrSlots); + } + }