Work on merchant.

This commit is contained in:
marcins78@gmail.com
2012-03-13 01:05:21 +00:00
parent 7304750a52
commit 214512f4aa
5 changed files with 90 additions and 14 deletions

View File

@@ -0,0 +1,21 @@
package com.gempukku.lotro.db;
import java.util.Date;
public class DbMerchantDAO implements MerchantDAO {
private DbAccess _dbAccess;
public DbMerchantDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
@Override
public void addTransaction(String blueprintId, float price, Date date) {
}
@Override
public Transaction getLastTransaction(String blueprintId) {
return null;
}
}

View File

@@ -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.collection.CollectionsManager;
import com.gempukku.lotro.db.MerchantDAO;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.game.CardItem;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
@@ -21,20 +22,25 @@ public class MerchantService {
private ReadWriteLock _lock = new ReentrantReadWriteLock(true);
private Set<CardItem> _merchantableItems = new HashSet<CardItem>();
private Set<String> _merchantableStrings = new HashSet<String>();
private CollectionType _permanentCollection = new CollectionType("permanent", "My cards");
private CollectionsManager _collectionsManager;
public MerchantService(LotroCardBlueprintLibrary library, CollectionsManager collectionsManager) {
public MerchantService(LotroCardBlueprintLibrary library, CollectionsManager collectionsManager, MerchantDAO merchantDAO) {
_collectionsManager = collectionsManager;
ParametrizedMerchant parametrizedMerchant = new ParametrizedMerchant();
parametrizedMerchant.setMerchantSetupDate(new Date());
parametrizedMerchant.setMerchantDao(merchantDAO);
_merchant = parametrizedMerchant;
RarityReader rarityReader = new RarityReader();
for (int i = 0; i <= 19; i++) {
SetRarity rarity = rarityReader.getSetRarity(String.valueOf(i));
for (String blueprintId : rarity.getAllCards())
_merchantableItems.add(new BasicCardItem(library.getBaseBlueprintId(blueprintId)));
for (String blueprintId : rarity.getAllCards()) {
String baseBlueprintId = library.getBaseBlueprintId(blueprintId);
_merchantableItems.add(new BasicCardItem(baseBlueprintId));
_merchantableStrings.add(baseBlueprintId);
}
}
}
@@ -50,12 +56,16 @@ public class MerchantService {
Map<String, Integer> buyPrices = new HashMap<String, Integer>();
Map<String, Integer> sellPrices = new HashMap<String, Integer>();
for (CardItem cardItem : cardBlueprintIds) {
Integer buyPrice = _merchant.getCardBuyPrice(cardItem.getBlueprintId(), currentTime);
String blueprintId = cardItem.getBlueprintId();
Integer buyPrice = _merchant.getCardBuyPrice(blueprintId, currentTime);
if (buyPrice != null)
buyPrices.put(cardItem.getBlueprintId(), buyPrice);
Integer sellPrice = _merchant.getCardSellPrice(cardItem.getBlueprintId(), currentTime);
if (sellPrice != null)
sellPrices.put(cardItem.getBlueprintId(), sellPrice);
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);
_priceGuarantees.put(player, priceGuarantee);

View File

@@ -9,7 +9,8 @@ import java.util.HashMap;
import java.util.Map;
public class ParametrizedMerchant implements Merchant {
private static final long DAY = 1000 * 60 * 60 * 24;
private static final int BOOSTER_PRICE = 1000;
private static final long DAY = BOOSTER_PRICE * 60 * 60 * 24;
private Date _merchantSetupDate;
@@ -37,15 +38,18 @@ public class ParametrizedMerchant implements Merchant {
@Override
public Integer getCardBuyPrice(String blueprintId, Date currentTime) {
double normalPrice = getNormalPrice(blueprintId, currentTime);
Double normalPrice = getNormalPrice(blueprintId, currentTime);
if (normalPrice == null)
return null;
return (int) Math.floor(_profitMargin * normalPrice / getSetupComponent(currentTime));
}
@Override
public Integer getCardSellPrice(String blueprintId, Date currentTime) {
double normalPrice = getNormalPrice(blueprintId, currentTime);
Double normalPrice = getNormalPrice(blueprintId, currentTime);
if (normalPrice == null)
return null;
double setupComponent = getSetupComponent(currentTime);
System.out.println(setupComponent);
return (int) Math.ceil(normalPrice * setupComponent);
}
@@ -55,12 +59,35 @@ public class ParametrizedMerchant implements Merchant {
return 1;
}
private double getNormalPrice(String blueprintId, Date currentTime) {
private Double getNormalPrice(String blueprintId, Date currentTime) {
MerchantDAO.Transaction lastTrans = _merchantDao.getLastTransaction(blueprintId);
if (lastTrans == null) {
Integer basePrice = getBasePrice(blueprintId);
if (basePrice == null)
return null;
lastTrans = new MerchantDAO.Transaction(_merchantSetupDate, basePrice);
}
// (stored price)/(1+(fluctuation value * ms since last transaction)/(price revert time in ms))
return lastTrans.getPrice() / (1 + (_fluctuationValue * Math.pow(1f * (currentTime.getTime() - lastTrans.getDate().getTime()) / _priceRevertTimeMs, 0.9)));
}
private Integer getBasePrice(String blueprintId) {
int underscoreIndex = blueprintId.indexOf("_");
if (underscoreIndex == -1)
return null;
SetRarity rarity = _rarity.get(Integer.parseInt(blueprintId.substring(0, blueprintId.indexOf("_"))));
String cardRarity = rarity.getCardRarity(blueprintId);
if (cardRarity.equals("X"))
return 3 * BOOSTER_PRICE;
if (cardRarity.equals("R") || cardRarity.equals("P"))
return BOOSTER_PRICE;
if (cardRarity.equals("U") || cardRarity.equals("S"))
return BOOSTER_PRICE / 3;
if (cardRarity.equals("C"))
return BOOSTER_PRICE / 7;
throw new RuntimeException("Unkown rarity for priced card: " + cardRarity);
}
@Override
public void cardBought(String blueprintId, Date currentTime, int price) {
_merchantDao.addTransaction(blueprintId, (price / _profitMargin) * (1 + _fluctuationValue), currentTime);

View File

@@ -15,6 +15,7 @@ import java.lang.reflect.Type;
public class DaoProvider implements InjectableProvider<Context, Type> {
private Injectable<PlayerDAO> _playerDaoInjectable;
private Injectable<CollectionDAO> _collectionDaoInjectable;
private Injectable<MerchantDAO> _merchantDaoInjectable;
private Injectable<DeckDAO> _deckDaoInjectable;
private Injectable<GameHistoryDAO> _gameHistoryDAOInjectable;
@@ -46,6 +47,8 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return getLeagueMatchDaoSafely();
else if (type.equals(LeaguePointsDAO.class))
return getLeaguePointsDaoSafely();
else if (type.equals(MerchantDAO.class))
return getMerchantDaoSafely();
return null;
}
@@ -75,6 +78,19 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return _leagueMatchDAOInjectable;
}
private synchronized Injectable<MerchantDAO> getMerchantDaoSafely() {
if (_merchantDaoInjectable == null) {
final MerchantDAO merchantDAO = new DbMerchantDAO(_dbAccess);
_merchantDaoInjectable = new Injectable<MerchantDAO>() {
@Override
public MerchantDAO getValue() {
return merchantDAO;
}
};
}
return _merchantDaoInjectable;
}
private synchronized Injectable<LeagueDAO> getLeagueDaoSafely() {
if (_leagueDaoInjectable == null) {
final LeagueDAO leagueDao = new LeagueDAO(_dbAccess, _collectionSerializer);

View File

@@ -39,6 +39,8 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
@Context
private CollectionDAO _collectionDao;
@Context
private MerchantDAO _merchantDao;
@Context
private LeagueDAO _leagueDao;
@Context
private LeagueMatchDAO _leagueMatchDao;
@@ -100,7 +102,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private synchronized Injectable<MerchantService> getMerchantServiceInjectable() {
if (_merchantServiceInjectable == null) {
final MerchantService merchantService = new MerchantService(_library, getCollectionsManagerInjectable().getValue());
final MerchantService merchantService = new MerchantService(_library, getCollectionsManagerInjectable().getValue(), _merchantDao);
_merchantServiceInjectable = new Injectable<MerchantService>() {
@Override
public MerchantService getValue() {