From cd5c4779c730fe7cedafe44433668a6b79f9fe97 Mon Sep 17 00:00:00 2001 From: alterwain Date: Sat, 8 Mar 2025 21:01:06 +0300 Subject: [PATCH] Goods purchasing fixed --- .../alterdekim/game/component/GameServer.java | 9 +++++---- .../game/inventory/BuyGoodResultGood.java | 13 +++++++++---- .../game/message/amf/AMFDeserializer.java | 18 +++++++++++++++--- .../repository/AvatarInventoryRepository.java | 2 +- .../game/service/AvatarInventoryService.java | 2 +- .../alterdekim/game/service/UserService.java | 4 +++- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/alterdekim/game/component/GameServer.java b/src/main/java/com/alterdekim/game/component/GameServer.java index 87eca22..f0d68e1 100644 --- a/src/main/java/com/alterdekim/game/component/GameServer.java +++ b/src/main/java/com/alterdekim/game/component/GameServer.java @@ -285,14 +285,15 @@ public class GameServer { private void buyOne(int playerId, AMFObject item, UserMessage message) { Map obj = (Map) item.getRaw(); - long goodId = obj.get("GoodId").getLong(); - boolean isMagic = obj.get("Currency").getLong() == 2; + obj = (Map) obj.get("item").getRaw(); + long goodId = obj.get("id").getLong(); Optional tGood = this.goodsService.findById(goodId); if( tGood.isEmpty() ) { this.sendResult(playerId, message.getTransactionId(), new BuyGoodResult(new ArrayList<>())); return; } Good good = tGood.get(); + boolean isMagic = good.getMagicTickets() != 0; AvatarInventoryType invType = AvatarInventoryType.fromGoodType(good.getGoodTypeId()); if( isMagic ) { if( !subtractMagicTickets(playerId, good.getMagicTickets()) ) { @@ -300,7 +301,7 @@ public class GameServer { return; } this.users.pushToInventory(playerId, invType, goodId); - this.sendResult(playerId, message.getTransactionId(), new BuyGoodResultGood(true, new ArrayList<>())); + this.sendResult(playerId, message.getTransactionId(), new BuyGoodResultGood(new BuyGoodResultGood.BuyGoodBody(false, new ArrayList<>()))); return; } if( !subtractUsualTickets(playerId, good.getUsualTickets()) ) { @@ -308,7 +309,7 @@ public class GameServer { return; } this.users.pushToInventory(playerId, invType, goodId); - this.sendResult(playerId, message.getTransactionId(), new BuyGoodResultGood(true, new ArrayList<>())); + this.sendResult(playerId, message.getTransactionId(), new BuyGoodResultGood(new BuyGoodResultGood.BuyGoodBody(false, new ArrayList<>()))); } private boolean subtractUsualTickets(int userId, int amount) { diff --git a/src/main/java/com/alterdekim/game/component/game/inventory/BuyGoodResultGood.java b/src/main/java/com/alterdekim/game/component/game/inventory/BuyGoodResultGood.java index 421a7e9..42564fb 100644 --- a/src/main/java/com/alterdekim/game/component/game/inventory/BuyGoodResultGood.java +++ b/src/main/java/com/alterdekim/game/component/game/inventory/BuyGoodResultGood.java @@ -7,9 +7,14 @@ import java.util.List; @AllArgsConstructor public class BuyGoodResultGood { - @AMFKey(name = "Error") - private Boolean error; + private BuyGoodBody body; - @AMFKey(name = "GoodInfo") - private List goodInfo; + @AllArgsConstructor + public static class BuyGoodBody { + @AMFKey(name = "Error") + private Boolean error; + + @AMFKey(name = "GoodInfo") + private List goodInfo; + } } diff --git a/src/main/java/com/alterdekim/game/message/amf/AMFDeserializer.java b/src/main/java/com/alterdekim/game/message/amf/AMFDeserializer.java index cdbf837..ba996ed 100644 --- a/src/main/java/com/alterdekim/game/message/amf/AMFDeserializer.java +++ b/src/main/java/com/alterdekim/game/message/amf/AMFDeserializer.java @@ -63,6 +63,10 @@ public class AMFDeserializer { Map map = new HashMap<>(); boolean endMarker = false; while(!endMarker) { + if( bytes.size() < 2 ) { + bytes.clear(); + break; + } var a = bytes.subList(0, 2); int keyLen = BinaryMessageSerializer.deserializeInteger(a); a.clear(); @@ -75,9 +79,13 @@ public class AMFDeserializer { String key = new String(SerializerUtils.bytesToPrimitive(a)); a.clear(); AMFObject object = new AMFObject(); - object.setType(AMFValueType.toValueType(bytes.remove())); - object.setValue(processSingle(object.getType())); - map.put(key, object); + if(!bytes.isEmpty()) { + object.setType(AMFValueType.toValueType(bytes.remove())); + object.setValue(processSingle(object.getType())); + map.put(key, object); + } else { + break; + } } return map; } @@ -87,6 +95,10 @@ public class AMFDeserializer { bytes.subList(0, 4).clear(); boolean endMarker = false; while(!endMarker) { + if( bytes.size() < 2 ) { + bytes.clear(); + break; + } var a = bytes.subList(0, 2); int keyLen = BinaryMessageSerializer.deserializeInteger(a); a.clear(); diff --git a/src/main/java/com/alterdekim/game/repository/AvatarInventoryRepository.java b/src/main/java/com/alterdekim/game/repository/AvatarInventoryRepository.java index ce847aa..53e9e52 100644 --- a/src/main/java/com/alterdekim/game/repository/AvatarInventoryRepository.java +++ b/src/main/java/com/alterdekim/game/repository/AvatarInventoryRepository.java @@ -27,7 +27,7 @@ public interface AvatarInventoryRepository extends JpaRepository findUsedItemsByUserIdAndType(@Param("uid") Integer userId, @Param("type") AvatarInventoryType type); @Query(value = "SELECT a FROM AvatarInventory a WHERE a.user.id = :uid AND a.type = :type AND a.goodId = :good_id") - Optional findSpecificitem(@Param("uid") Integer userId, @Param("type") AvatarInventoryType type, @Param("good_id") Long goodId); + List findSpecificitem(@Param("uid") Integer userId, @Param("type") AvatarInventoryType type, @Param("good_id") Long goodId); @Transactional @Modifying diff --git a/src/main/java/com/alterdekim/game/service/AvatarInventoryService.java b/src/main/java/com/alterdekim/game/service/AvatarInventoryService.java index 00cf2b8..cf6c567 100644 --- a/src/main/java/com/alterdekim/game/service/AvatarInventoryService.java +++ b/src/main/java/com/alterdekim/game/service/AvatarInventoryService.java @@ -75,7 +75,7 @@ public class AvatarInventoryService { } public boolean isSpecificItemExists(Integer userId, AvatarInventoryType type, Long goodId) { - return this.inventoryRepository.findSpecificitem(userId, type, goodId).isPresent(); + return !this.inventoryRepository.findSpecificitem(userId, type, goodId).isEmpty(); } public InventoryGroup getBodyPartInventoryGroup(Integer userId, BodyPartType type, boolean addEmpty) { diff --git a/src/main/java/com/alterdekim/game/service/UserService.java b/src/main/java/com/alterdekim/game/service/UserService.java index fe5a8e1..9241315 100644 --- a/src/main/java/com/alterdekim/game/service/UserService.java +++ b/src/main/java/com/alterdekim/game/service/UserService.java @@ -354,6 +354,8 @@ public class UserService { } public void pushToInventory(int userId, AvatarInventoryType type, long goodId) { - this.inventoryService.addGoodToInventory(new AvatarInventory(userRepository.getReferenceById(userId), goodId, false, type)); + if( !this.inventoryService.isSpecificItemExists(userId, type, goodId) ) { + this.inventoryService.addGoodToInventory(new AvatarInventory(userRepository.getReferenceById(userId), goodId, false, type)); + } } }