Work on merchant.
This commit is contained in:
@@ -33,7 +33,7 @@ public class DbMerchantDAO implements MerchantDAO {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("update merchant set transaction_price=?, transaction_date=?, transaction_type=? where blueprint_id=?");
|
||||
PreparedStatement statement = connection.prepareStatement("update merchant_data set transaction_price=?, transaction_date=?, transaction_type=? where blueprint_id=?");
|
||||
try {
|
||||
statement.setFloat(1, price);
|
||||
statement.setTimestamp(2, new Timestamp(date.getTime()));
|
||||
@@ -55,7 +55,7 @@ public class DbMerchantDAO implements MerchantDAO {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("insert into merchant (transaction_price, transaction_date, transaction_type, blueprint_id) values (?,?,?,?)");
|
||||
PreparedStatement statement = connection.prepareStatement("insert into merchant_data (transaction_price, transaction_date, transaction_type, blueprint_id) values (?,?,?,?)");
|
||||
try {
|
||||
statement.setFloat(1, price);
|
||||
statement.setTimestamp(2, new Timestamp(date.getTime()));
|
||||
@@ -82,7 +82,7 @@ public class DbMerchantDAO implements MerchantDAO {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("select blueprint_id, transaction_price, transaction_date, transaction_type from merchant where blueprint_id=?");
|
||||
PreparedStatement statement = connection.prepareStatement("select blueprint_id, transaction_price, transaction_date, transaction_type from merchant_data where blueprint_id=?");
|
||||
try {
|
||||
statement.setString(1, blueprintId);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
@@ -23,12 +23,15 @@ public class MerchantService {
|
||||
private ReadWriteLock _lock = new ReentrantReadWriteLock(true);
|
||||
private Set<CardItem> _merchantableItems = new HashSet<CardItem>();
|
||||
private Set<String> _merchantableStrings = new HashSet<String>();
|
||||
|
||||
private Map<String, Integer> _fixedPriceItems = new HashMap<String, Integer>();
|
||||
|
||||
private CollectionType _permanentCollection = new CollectionType("permanent", "My cards");
|
||||
private CollectionsManager _collectionsManager;
|
||||
|
||||
public MerchantService(LotroCardBlueprintLibrary library, CollectionsManager collectionsManager, MerchantDAO merchantDAO) {
|
||||
_collectionsManager = collectionsManager;
|
||||
ParametrizedMerchant parametrizedMerchant = new ParametrizedMerchant();
|
||||
ParametrizedMerchant parametrizedMerchant = new ParametrizedMerchant(library);
|
||||
parametrizedMerchant.setMerchantSetupDate(new Date());
|
||||
parametrizedMerchant.setMerchantDao(merchantDAO);
|
||||
_merchant = parametrizedMerchant;
|
||||
@@ -42,6 +45,28 @@ public class MerchantService {
|
||||
_merchantableStrings.add(baseBlueprintId);
|
||||
}
|
||||
}
|
||||
|
||||
addBooster("FotR - Booster", 1000);
|
||||
addBooster("MoM - Booster", 1000);
|
||||
addBooster("RotEL - Booster", 1000);
|
||||
addBooster("TTT - Booster", 1000);
|
||||
addBooster("BoHD - Booster", 1000);
|
||||
addBooster("EoF - Booster", 1000);
|
||||
addBooster("RotK - Booster", 1000);
|
||||
addBooster("SoG - Booster", 1000);
|
||||
addBooster("MD - Booster", 1000);
|
||||
addBooster("SH - Booster", 1000);
|
||||
addBooster("BR - Booster", 1000);
|
||||
addBooster("BL - Booster", 1000);
|
||||
addBooster("HU - Booster", 1000);
|
||||
addBooster("RoS - Booster", 1000);
|
||||
addBooster("TaD - Booster", 1000);
|
||||
}
|
||||
|
||||
private void addBooster(String blueprintId, int price) {
|
||||
_fixedPriceItems.put(blueprintId, price);
|
||||
_merchantableItems.add(new BasicCardItem(blueprintId));
|
||||
_merchantableStrings.add(blueprintId);
|
||||
}
|
||||
|
||||
public Set<CardItem> getSellableItems() {
|
||||
@@ -58,13 +83,18 @@ public class MerchantService {
|
||||
for (CardItem cardItem : cardBlueprintIds) {
|
||||
String blueprintId = cardItem.getBlueprintId();
|
||||
|
||||
Integer buyPrice = _merchant.getCardBuyPrice(blueprintId, currentTime);
|
||||
if (buyPrice != null)
|
||||
buyPrices.put(blueprintId, buyPrice);
|
||||
if (_merchantableStrings.contains(blueprintId)) {
|
||||
Integer sellPrice = _merchant.getCardSellPrice(blueprintId, currentTime);
|
||||
if (sellPrice != null)
|
||||
sellPrices.put(blueprintId, sellPrice);
|
||||
Integer fixedPrice = _fixedPriceItems.get(blueprintId);
|
||||
if (fixedPrice != null) {
|
||||
sellPrices.put(blueprintId, fixedPrice);
|
||||
} else {
|
||||
Integer buyPrice = _merchant.getCardBuyPrice(blueprintId, currentTime);
|
||||
if (buyPrice != null)
|
||||
buyPrices.put(blueprintId, buyPrice);
|
||||
if (_merchantableStrings.contains(blueprintId)) {
|
||||
Integer sellPrice = _merchant.getCardSellPrice(blueprintId, currentTime);
|
||||
if (sellPrice != null)
|
||||
sellPrices.put(blueprintId, sellPrice);
|
||||
}
|
||||
}
|
||||
}
|
||||
PriceGuarantee priceGuarantee = new PriceGuarantee(sellPrices, buyPrices, currentTime);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.gempukku.lotro.merchant;
|
||||
import com.gempukku.lotro.cards.packs.RarityReader;
|
||||
import com.gempukku.lotro.cards.packs.SetRarity;
|
||||
import com.gempukku.lotro.db.MerchantDAO;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@@ -23,8 +24,10 @@ public class ParametrizedMerchant implements Merchant {
|
||||
|
||||
private MerchantDAO _merchantDao;
|
||||
private Map<Integer, SetRarity> _rarity = new HashMap<Integer, SetRarity>();
|
||||
private LotroCardBlueprintLibrary _library;
|
||||
|
||||
public ParametrizedMerchant() {
|
||||
public ParametrizedMerchant(LotroCardBlueprintLibrary library) {
|
||||
_library = library;
|
||||
RarityReader rarityReader = new RarityReader();
|
||||
for (int i = 0; i <= 19; i++)
|
||||
_rarity.put(i, rarityReader.getSetRarity(String.valueOf(i)));
|
||||
@@ -40,10 +43,21 @@ public class ParametrizedMerchant implements Merchant {
|
||||
|
||||
@Override
|
||||
public Integer getCardBuyPrice(String blueprintId, Date currentTime) {
|
||||
boolean foil = false;
|
||||
if (blueprintId.endsWith("*"))
|
||||
foil = true;
|
||||
|
||||
blueprintId = _library.getBaseBlueprintId(blueprintId);
|
||||
|
||||
Double normalPrice = getNormalPrice(blueprintId, currentTime);
|
||||
if (normalPrice == null)
|
||||
return null;
|
||||
return (int) Math.floor(_profitMargin * normalPrice / getSetupComponent(currentTime));
|
||||
|
||||
int price = (int) Math.floor(_profitMargin * normalPrice / getSetupComponent(currentTime));
|
||||
|
||||
if (foil)
|
||||
return 2 * price;
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,6 +112,9 @@ public class ParametrizedMerchant implements Merchant {
|
||||
|
||||
@Override
|
||||
public void cardBought(String blueprintId, Date currentTime, int price) {
|
||||
if (blueprintId.endsWith("*"))
|
||||
price = price / 2;
|
||||
blueprintId = _library.getBaseBlueprintId(blueprintId);
|
||||
_merchantDao.addTransaction(blueprintId, (price / _profitMargin), currentTime, MerchantDAO.TransactionType.BUY);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,4 +24,8 @@ public class MockMerchantDAO implements MerchantDAO {
|
||||
return new Transaction(_dates.get(blueprintId), _prices.get(blueprintId), _transactionTypes.get(blueprintId));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearCache() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.gempukku.lotro.merchant;
|
||||
|
||||
import com.gempukku.lotro.db.MerchantDAO;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ParametrizedMerchantTest {
|
||||
@Test
|
||||
public void checkTransactionRevertsPriceAfterFiveDays() {
|
||||
@@ -15,7 +15,7 @@ public class ParametrizedMerchantTest {
|
||||
|
||||
MerchantDAO merchantDao = new MockMerchantDAO();
|
||||
|
||||
ParametrizedMerchant merchant = new ParametrizedMerchant();
|
||||
ParametrizedMerchant merchant = new ParametrizedMerchant(new LotroCardBlueprintLibrary());
|
||||
merchant.setMerchantDao(merchantDao);
|
||||
merchant.setMerchantSetupDate(setupDate);
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ParametrizedMerchantTest {
|
||||
|
||||
MerchantDAO merchantDao = new MockMerchantDAO();
|
||||
|
||||
ParametrizedMerchant merchant = new ParametrizedMerchant();
|
||||
ParametrizedMerchant merchant = new ParametrizedMerchant(new LotroCardBlueprintLibrary());
|
||||
merchant.setMerchantDao(merchantDao);
|
||||
merchant.setMerchantSetupDate(setupDate);
|
||||
|
||||
|
||||
@@ -95,9 +95,10 @@ public class MerchantResource extends AbstractResource {
|
||||
|
||||
Document doc = documentBuilder.newDocument();
|
||||
|
||||
Element collectionElem = doc.createElement("merchant");
|
||||
collectionElem.setAttribute("count", String.valueOf(filteredResult.size()));
|
||||
doc.appendChild(collectionElem);
|
||||
Element merchantElem = doc.createElement("merchant");
|
||||
merchantElem.setAttribute("currency", String.valueOf(_collectionsManager.getPlayerCollection(resourceOwner, "permanent").getCurrency()));
|
||||
merchantElem.setAttribute("count", String.valueOf(filteredResult.size()));
|
||||
doc.appendChild(merchantElem);
|
||||
|
||||
for (CardItem cardItem : pageToDisplay) {
|
||||
String blueprintId = cardItem.getBlueprintId();
|
||||
@@ -118,7 +119,7 @@ public class MerchantResource extends AbstractResource {
|
||||
Integer sellPrice = sellPrices.get(blueprintId);
|
||||
if (sellPrice != null)
|
||||
elem.setAttribute("sellPrice", sellPrice.toString());
|
||||
collectionElem.appendChild(elem);
|
||||
merchantElem.appendChild(elem);
|
||||
}
|
||||
|
||||
return doc;
|
||||
|
||||
Reference in New Issue
Block a user