Merchant now uses DAO when transactions made.

This commit is contained in:
marcins78@gmail.com
2012-03-13 13:08:17 +00:00
parent 1c5f7ddd92
commit e0677b6825
3 changed files with 41 additions and 23 deletions

View File

@@ -7,7 +7,21 @@ public interface Merchant {
public Integer getCardBuyPrice(String blueprintId, Date currentTime); 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); 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); public void cardBought(String blueprintId, Date currentTime, int price);
} }

View File

@@ -75,27 +75,7 @@ public class MerchantService {
} }
} }
public void sellCard(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();
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 {
Date currentTime = new Date(); Date currentTime = new Date();
Lock lock = _lock.writeLock(); Lock lock = _lock.writeLock();
lock.lock(); lock.lock();
@@ -107,9 +87,33 @@ public class MerchantService {
if (guaranteedPrice == null || price != guaranteedPrice) if (guaranteedPrice == null || price != guaranteedPrice)
throw new MerchantException("Guaranteed price does not match the user asked price"); 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); boolean success = _collectionsManager.buyCardToPlayerCollection(player, _permanentCollection, blueprintId, price);
if (!success) if (!success)
throw new MerchantException("Unable to remove required currency from your collection"); throw new MerchantException("Unable to remove required currency from your collection");
_merchant.cardSold(blueprintId, currentTime, price);
} finally { } finally {
lock.unlock(); lock.unlock();
} }

View File

@@ -135,7 +135,7 @@ public class MerchantResource extends AbstractResource {
@Context HttpServletResponse response) throws ParserConfigurationException { @Context HttpServletResponse response) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId); Player resourceOwner = getResourceOwnerSafely(request, participantId);
try { try {
_merchantService.sellCard(resourceOwner, blueprintId, price); _merchantService.merchantSellsCard(resourceOwner, blueprintId, price);
return null; return null;
} catch (MerchantException exp) { } catch (MerchantException exp) {
return marshalException(exp); return marshalException(exp);
@@ -153,7 +153,7 @@ public class MerchantResource extends AbstractResource {
@Context HttpServletResponse response) throws ParserConfigurationException { @Context HttpServletResponse response) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId); Player resourceOwner = getResourceOwnerSafely(request, participantId);
try { try {
_merchantService.buyCard(resourceOwner, blueprintId, price); _merchantService.merchantBuysCard(resourceOwner, blueprintId, price);
return null; return null;
} catch (MerchantException exp) { } catch (MerchantException exp) {
return marshalException(exp); return marshalException(exp);