Save chr slots to PG

This commit is contained in:
P0nk
2024-09-27 17:55:42 +02:00
parent 2044166967
commit 082e0c0486
6 changed files with 59 additions and 20 deletions

View File

@@ -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;

View File

@@ -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;
}
}
}

View File

@@ -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());

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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);
}
}