Modifying merchant.

This commit is contained in:
marcins78@gmail.com
2012-03-13 12:29:33 +00:00
parent 187b458ad9
commit 3429a62f92
5 changed files with 91 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ public class DbMerchantDAO implements MerchantDAO {
}
@Override
public void addTransaction(String blueprintId, float price, Date date) {
public void addTransaction(String blueprintId, float price, Date date, TransactionType transactionType) {
}

View File

@@ -5,15 +5,21 @@ import java.util.Date;
public interface MerchantDAO {
public Transaction getLastTransaction(String blueprintId);
public void addTransaction(String blueprintId, float price, Date date);
public void addTransaction(String blueprintId, float price, Date date, TransactionType transactionType);
public enum TransactionType {
SELL, BUY
}
public static class Transaction {
private float _price;
private Date _date;
private TransactionType _transactionType;
public Transaction(Date date, float price) {
public Transaction(Date date, float price, TransactionType transactionType) {
_date = date;
_price = price;
_transactionType = transactionType;
}
public Date getDate() {
@@ -23,5 +29,9 @@ public interface MerchantDAO {
public float getPrice() {
return _price;
}
public TransactionType getTransactionType() {
return _transactionType;
}
}
}

View File

@@ -14,10 +14,12 @@ public class ParametrizedMerchant implements Merchant {
private Date _merchantSetupDate;
private float _fluctuationValue = 0.1f;
private long _priceRevertTimeMs = 5 * DAY;
private long _easingTimeMs = 30 * DAY;
private float _profitMargin = 0.7f;
private final float _fluctuationValue = 0.1f;
private final long _priceRevertTimeMs = 5 * DAY;
private final long _easingTimeMs = 30 * DAY;
private final float _profitMargin = 0.7f;
private final double _returnPrizeSlope = 0.3;
private final double _decreasePrizeSlope = 0.8;
private MerchantDAO _merchantDao;
private Map<Integer, SetRarity> _rarity = new HashMap<Integer, SetRarity>();
@@ -65,10 +67,16 @@ public class ParametrizedMerchant implements Merchant {
Integer basePrice = getBasePrice(blueprintId);
if (basePrice == null)
return null;
lastTrans = new MerchantDAO.Transaction(_merchantSetupDate, basePrice);
lastTrans = new MerchantDAO.Transaction(_merchantSetupDate, basePrice, MerchantDAO.TransactionType.SELL);
}
if (lastTrans.getDate().getTime() + _priceRevertTimeMs > currentTime.getTime())
if (lastTrans.getTransactionType() == MerchantDAO.TransactionType.SELL) {
return (1 + _fluctuationValue) * lastTrans.getPrice() / (1 + (_fluctuationValue * Math.pow(1f * (currentTime.getTime() - lastTrans.getDate().getTime()) / _priceRevertTimeMs, _returnPrizeSlope)));
} else {
return (1 - _fluctuationValue) * lastTrans.getPrice() / (1 - (_fluctuationValue * Math.pow(1f * (currentTime.getTime() - lastTrans.getDate().getTime()) / _priceRevertTimeMs, _returnPrizeSlope)));
}
// (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)));
return lastTrans.getPrice() / (1 + (_fluctuationValue * Math.pow(1f * (currentTime.getTime() - lastTrans.getDate().getTime() - _priceRevertTimeMs) / _priceRevertTimeMs, _decreasePrizeSlope)));
}
private Integer getBasePrice(String blueprintId) {
@@ -90,11 +98,11 @@ public class ParametrizedMerchant implements Merchant {
@Override
public void cardBought(String blueprintId, Date currentTime, int price) {
_merchantDao.addTransaction(blueprintId, (price / _profitMargin) * (1 + _fluctuationValue), currentTime);
_merchantDao.addTransaction(blueprintId, (price / _profitMargin), currentTime, MerchantDAO.TransactionType.BUY);
}
@Override
public void cardSold(String blueprintId, Date currentTime, int price) {
_merchantDao.addTransaction(blueprintId, price * (1 + _fluctuationValue), currentTime);
_merchantDao.addTransaction(blueprintId, price, currentTime, MerchantDAO.TransactionType.SELL);
}
}

View File

@@ -9,17 +9,19 @@ import java.util.Map;
public class MockMerchantDAO implements MerchantDAO {
private Map<String, Float> _prices = new HashMap<String, Float>();
private Map<String, Date> _dates = new HashMap<String, Date>();
private Map<String, TransactionType> _transactionTypes = new HashMap<String, TransactionType>();
@Override
public void addTransaction(String blueprintId, float price, Date date) {
public void addTransaction(String blueprintId, float price, Date date, TransactionType transactionType) {
_prices.put(blueprintId, price);
_dates.put(blueprintId, date);
_transactionTypes.put(blueprintId, transactionType);
}
@Override
public Transaction getLastTransaction(String blueprintId) {
if (_dates.containsKey(blueprintId))
return new Transaction(_dates.get(blueprintId), _prices.get(blueprintId));
return new Transaction(_dates.get(blueprintId), _prices.get(blueprintId), _transactionTypes.get(blueprintId));
return null;
}
}

View File

@@ -5,10 +5,35 @@ import org.junit.Test;
import java.util.Date;
import static org.junit.Assert.assertTrue;
public class ParametrizedMerchantTest {
@Test
public void checkBuying() {
Date setupDate = new Date(0);
public void checkTransactionRevertsPriceAfterFiveDays() {
Date setupDate = new Date(-1000 * 60 * 60 * 24 * 50L);
Date firstTrans = new Date(0);
MerchantDAO merchantDao = new MockMerchantDAO();
ParametrizedMerchant merchant = new ParametrizedMerchant();
merchant.setMerchantDao(merchantDao);
merchant.setMerchantSetupDate(setupDate);
merchant.cardSold("1_1", firstTrans, 1000);
merchant.cardBought("1_2", firstTrans, 1000);
Date currentTime = new Date(1000 * 60 * 60 * 24 * 5L);
assertEqualsMoreOrLess(1100, (int) merchant.getCardSellPrice("1_1", firstTrans));
assertEqualsMoreOrLess(900, (int) merchant.getCardBuyPrice("1_2", firstTrans));
assertEqualsMoreOrLess(1000, (int) merchant.getCardSellPrice("1_1", currentTime));
assertEqualsMoreOrLess(1000, (int) merchant.getCardBuyPrice("1_2", currentTime));
}
@Test
public void plotPriceAfterSold() {
Date setupDate = new Date(-1000 * 60 * 60 * 24 * 50L);
Date firstTrans = new Date(0);
MerchantDAO merchantDao = new MockMerchantDAO();
@@ -19,8 +44,36 @@ public class ParametrizedMerchantTest {
merchant.cardSold("1_1", firstTrans, 1000);
Date buyTime = new Date(1000 * 60 * 60 * 12);
System.out.println(merchant.getCardBuyPrice("1_1", buyTime));
System.out.println(merchant.getCardSellPrice("1_1", buyTime));
long hour = 1000 * 60 * 60;
System.out.println("-2,1000");
for (long time = 0; time < hour * 24 * 20L; time += hour * 2) {
System.out.println(time / hour + "," + (int) merchant.getCardSellPrice("1_1", new Date(time)));
}
}
@Test
public void plotPriceAfterBought() {
Date setupDate = new Date(-1000 * 60 * 60 * 24 * 50L);
Date firstTrans = new Date(0);
MerchantDAO merchantDao = new MockMerchantDAO();
ParametrizedMerchant merchant = new ParametrizedMerchant();
merchant.setMerchantDao(merchantDao);
merchant.setMerchantSetupDate(setupDate);
merchant.cardBought("1_1", firstTrans, 700);
long hour = 1000 * 60 * 60;
System.out.println("-2,1000");
for (long time = 0; time < hour * 24 * 20L; time += hour * 2) {
System.out.println(time / hour + "," + (int) merchant.getCardSellPrice("1_1", new Date(time)));
}
}
private void assertEqualsMoreOrLess(int expected, int given) {
assertTrue("Expected " + expected + " more or less, but received " + given, given <= expected + 1 && given >= expected - 1);
}
}