From e0677b6825df06cf8f2da696603fbf87d2f2ab98 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Tue, 13 Mar 2012 13:08:17 +0000 Subject: [PATCH] Merchant now uses DAO when transactions made. --- .../com/gempukku/lotro/merchant/Merchant.java | 14 ++++++ .../lotro/merchant/MerchantService.java | 46 ++++++++++--------- .../lotro/server/MerchantResource.java | 4 +- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/Merchant.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/Merchant.java index 9d7a2bf5f..194690d3e 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/Merchant.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/Merchant.java @@ -7,7 +7,21 @@ public interface Merchant { public Integer getCardBuyPrice(String blueprintId, Date currentTime); + /** + * Called when card was sold by merchant. + * + * @param blueprintId + * @param currentTime + * @param price + */ public void cardSold(String blueprintId, Date currentTime, int price); + /** + * Called when card was bought by merchant. + * + * @param blueprintId + * @param currentTime + * @param price + */ public void cardBought(String blueprintId, Date currentTime, int price); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/MerchantService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/MerchantService.java index 1ed9ecf20..fce284dd8 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/MerchantService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/merchant/MerchantService.java @@ -75,27 +75,7 @@ public class MerchantService { } } - public void sellCard(Player player, String blueprintId, int price) throws MerchantException { - Date currentTime = new Date(); - Lock lock = _lock.writeLock(); - lock.lock(); - try { - PriceGuarantee guarantee = _priceGuarantees.get(player); - if (guarantee == null || guarantee.getDate().getTime() + _priceGuaranteeExpire < currentTime.getTime()) - throw new MerchantException("Price guarantee has expired"); - Integer guaranteedPrice = guarantee.getSellPrices().get(blueprintId); - if (guaranteedPrice == null || price != guaranteedPrice) - throw new MerchantException("Guaranteed price does not match the user asked price"); - - boolean success = _collectionsManager.sellCardInPlayerCollection(player, _permanentCollection, blueprintId, price); - if (!success) - throw new MerchantException("Unable to remove the sold card from your collection"); - } finally { - lock.unlock(); - } - } - - public void buyCard(Player player, String blueprintId, int price) throws MerchantException { + public void merchantBuysCard(Player player, String blueprintId, int price) throws MerchantException { Date currentTime = new Date(); Lock lock = _lock.writeLock(); lock.lock(); @@ -107,9 +87,33 @@ public class MerchantService { if (guaranteedPrice == null || price != guaranteedPrice) throw new MerchantException("Guaranteed price does not match the user asked price"); + boolean success = _collectionsManager.sellCardInPlayerCollection(player, _permanentCollection, blueprintId, price); + if (!success) + throw new MerchantException("Unable to remove the sold card from your collection"); + + _merchant.cardBought(blueprintId, currentTime, price); + } finally { + lock.unlock(); + } + } + + public void merchantSellsCard(Player player, String blueprintId, int price) throws MerchantException { + Date currentTime = new Date(); + Lock lock = _lock.writeLock(); + lock.lock(); + try { + PriceGuarantee guarantee = _priceGuarantees.get(player); + if (guarantee == null || guarantee.getDate().getTime() + _priceGuaranteeExpire < currentTime.getTime()) + throw new MerchantException("Price guarantee has expired"); + Integer guaranteedPrice = guarantee.getSellPrices().get(blueprintId); + if (guaranteedPrice == null || price != guaranteedPrice) + throw new MerchantException("Guaranteed price does not match the user asked price"); + boolean success = _collectionsManager.buyCardToPlayerCollection(player, _permanentCollection, blueprintId, price); if (!success) throw new MerchantException("Unable to remove required currency from your collection"); + + _merchant.cardSold(blueprintId, currentTime, price); } finally { lock.unlock(); } diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/MerchantResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/MerchantResource.java index aaa58e5aa..decbbdc4d 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/MerchantResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/MerchantResource.java @@ -135,7 +135,7 @@ public class MerchantResource extends AbstractResource { @Context HttpServletResponse response) throws ParserConfigurationException { Player resourceOwner = getResourceOwnerSafely(request, participantId); try { - _merchantService.sellCard(resourceOwner, blueprintId, price); + _merchantService.merchantSellsCard(resourceOwner, blueprintId, price); return null; } catch (MerchantException exp) { return marshalException(exp); @@ -153,7 +153,7 @@ public class MerchantResource extends AbstractResource { @Context HttpServletResponse response) throws ParserConfigurationException { Player resourceOwner = getResourceOwnerSafely(request, participantId); try { - _merchantService.buyCard(resourceOwner, blueprintId, price); + _merchantService.merchantBuysCard(resourceOwner, blueprintId, price); return null; } catch (MerchantException exp) { return marshalException(exp);