Archived
0

check demand and supply for order limit count

This commit is contained in:
iMoHax
2014-11-27 14:00:31 +03:00
parent 86c4abaa96
commit dd00f7be39
6 changed files with 88 additions and 69 deletions

View File

@@ -49,7 +49,7 @@ public class MarketAnalyzer {
for (Offer sell : vendor.getAllSellOffers()) {
LOG.trace("Sell offer {}", sell);
if (sell.getCount() == 0) continue;
long count = Math.min(sell.getCount(), Math.min(cargo, (long) Math.floor(balance / sell.getPrice())));
long count = Order.getMaxCount(sell, balance, cargo);
LOG.trace("count = {}", count);
if (count == 0) continue;
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
@@ -84,7 +84,7 @@ public class MarketAnalyzer {
for (Vendor seller : from.get()) {
for (Offer sell : seller.getAllSellOffers()) {
if (sell.getCount() == 0) continue;
long count = Math.min(sell.getCount(), Math.min(cargo, (long) Math.floor(balance / sell.getPrice())));
long count = Order.getMaxCount(sell, balance, cargo);
LOG.trace("Sell offer {}, count = {}", sell, count);
if (count == 0) continue;
for (Vendor buyer : to.get()) {
@@ -109,7 +109,7 @@ public class MarketAnalyzer {
}
for (Offer sell : from.getAllSellOffers()) {
if (sell.getCount() == 0) continue;
long count = Math.min(sell.getCount(), Math.min(cargo, (long) Math.floor(balance / sell.getPrice())));
long count = Order.getMaxCount(sell, balance, cargo);
LOG.trace("Sell offer {}, count = {}", sell, count);
if (count == 0) continue;

View File

@@ -19,7 +19,7 @@ public class Order implements Comparable<Order> {
public Order(Offer sell, Offer buy, long count) {
this.sell = sell;
this.buy = buy;
this.count = Math.min(Math.min(buy.getCount(), sell.getCount()), count);
this.count = getMaxCount(sell, buy, count);
this.profit = (buy.getPrice() - sell.getPrice()) * count;
}
@@ -32,7 +32,7 @@ public class Order implements Comparable<Order> {
}
public void setCount(long count){
this.count = Math.min(Math.min(buy.getCount(), sell.getCount()), count);
this.count = getMaxCount(sell, buy, count);
this.profit = (buy.getPrice() - sell.getPrice()) * count;
}
@@ -110,6 +110,24 @@ public class Order implements Comparable<Order> {
}
public void setMax(double balance, long limit) {
setCount((long) Math.min(balance/sell.getPrice(), limit));
setCount(getMaxCount(sell, balance, limit));
}
public static long getMaxCount(Offer sell, double balance, long limit){
return getMaxCount(sell, null, balance, limit);
}
public static long getMaxCount(Offer sell, Offer buy, long limit){
return getMaxCount(sell, buy, Double.POSITIVE_INFINITY, limit);
}
public static long getMaxCount(Offer sell, Offer buy, double balance, long limit){
long supply = sell.getCount();
if (supply == 0) return 0;
if (supply == -1) supply = Long.MAX_VALUE;
long demand = buy != null ? buy.getCount() : -1;
if (demand <= 0) demand = Long.MAX_VALUE;
if (Double.isInfinite(balance)) return Math.min(limit, Math.min(supply, demand));
return (long) Math.min(limit, Math.min(Math.min(supply, demand), Math.floor(balance/sell.getPrice())));
}
}