add missions to route entry
This commit is contained in:
@@ -12,6 +12,7 @@ import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.stage.Stage;
|
||||
import ru.trader.KeyBinding;
|
||||
import ru.trader.model.MissionModel;
|
||||
import ru.trader.model.OrderModel;
|
||||
import ru.trader.model.RouteEntryModel;
|
||||
import ru.trader.model.RouteModel;
|
||||
@@ -36,6 +37,8 @@ public class HelperController {
|
||||
private ListView<OrderModel> buyOrders;
|
||||
@FXML
|
||||
private ListView<OrderModel> sellOrders;
|
||||
@FXML
|
||||
private ListView<MissionModel> missions;
|
||||
|
||||
private Stage stage;
|
||||
private RouteModel route;
|
||||
@@ -81,6 +84,7 @@ public class HelperController {
|
||||
refuel.setText(String.valueOf(entry.getRefill()));
|
||||
buyOrders.setItems(entry.orders());
|
||||
sellOrders.setItems(entry.sellOrders());
|
||||
missions.setItems(entry.missions());
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
||||
@@ -82,7 +82,7 @@ public class RouteSearchController {
|
||||
Optional<RouteModel> path = Screeners.showRouters(routes);
|
||||
if (path.isPresent()){
|
||||
RouteModel route = path.get();
|
||||
route.addAll(missionsList.getItems());
|
||||
route.addAll(0, missionsList.getItems());
|
||||
Screeners.showHelper(route);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -9,12 +9,14 @@ public class MissionModel {
|
||||
private final ItemModel item;
|
||||
private final long count;
|
||||
private final double profit;
|
||||
private final Offer offer;
|
||||
|
||||
public MissionModel(StationModel target, double profit) {
|
||||
this.target = target;
|
||||
this.profit = profit;
|
||||
item = null;
|
||||
count = 0;
|
||||
offer = null;
|
||||
}
|
||||
|
||||
public MissionModel(StationModel target, long count, double profit) {
|
||||
@@ -22,6 +24,7 @@ public class MissionModel {
|
||||
this.count = count;
|
||||
this.profit = profit;
|
||||
this.item = null;
|
||||
offer = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +33,7 @@ public class MissionModel {
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
this.profit = profit;
|
||||
offer = SimpleOffer.fakeBuy(target.getStation(), item.getItem(), profit/count, count);
|
||||
}
|
||||
|
||||
public StationModel getTarget() {
|
||||
@@ -49,7 +53,7 @@ public class MissionModel {
|
||||
}
|
||||
|
||||
public boolean isSupply(){
|
||||
return item != null;
|
||||
return offer != null;
|
||||
}
|
||||
|
||||
public boolean isDelivery(){
|
||||
@@ -72,14 +76,14 @@ public class MissionModel {
|
||||
|
||||
public void toSpecification(CrawlerSpecificator specificator){
|
||||
if (isSupply()){
|
||||
specificator.buy(toOffer());
|
||||
specificator.buy(offer);
|
||||
} else
|
||||
if (isCourier() || isDelivery()){
|
||||
specificator.add(target.getStation(), true);
|
||||
}
|
||||
}
|
||||
|
||||
Offer toOffer(){
|
||||
return isSupply() ? SimpleOffer.fakeBuy(target.getStation(), item.getItem(), profit/count, count) : null;
|
||||
Offer getOffer(){
|
||||
return offer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public class RouteEntryModel {
|
||||
private final DoubleProperty profit;
|
||||
private final ObservableList<OrderModel> orders;
|
||||
private final ObservableList<OrderModel> sellOrders;
|
||||
private final ObservableList<MissionModel> missions;
|
||||
|
||||
RouteEntryModel(RouteEntry entry, MarketModel market) {
|
||||
this.entry = entry;
|
||||
@@ -24,6 +25,7 @@ public class RouteEntryModel {
|
||||
List<Order> orderList = entry.getOrders();
|
||||
orders = BindingsHelper.observableList(orderList, market.getModeler()::get);
|
||||
sellOrders = FXCollections.observableArrayList();
|
||||
missions = FXCollections.observableArrayList();
|
||||
profit = new SimpleDoubleProperty();
|
||||
profit.bind(BindingsHelper.group(Double::sum, OrderModel::profitProperty, orders));
|
||||
}
|
||||
@@ -32,6 +34,14 @@ public class RouteEntryModel {
|
||||
sellOrders.add(order);
|
||||
}
|
||||
|
||||
void add(MissionModel mission){
|
||||
missions.add(mission);
|
||||
}
|
||||
|
||||
void remove(MissionModel mission){
|
||||
missions.remove(mission);
|
||||
}
|
||||
|
||||
public StationModel getStation() {
|
||||
return station;
|
||||
}
|
||||
@@ -68,6 +78,10 @@ public class RouteEntryModel {
|
||||
return sellOrders;
|
||||
}
|
||||
|
||||
public ObservableList<MissionModel> missions() {
|
||||
return missions;
|
||||
}
|
||||
|
||||
void refresh(MarketModel market){
|
||||
orders.clear();
|
||||
orders.addAll(BindingsHelper.observableList(entry.getOrders(), market.getModeler()::get));
|
||||
|
||||
@@ -121,25 +121,48 @@ public class RouteModel {
|
||||
return new RouteModel(_route, market);
|
||||
}
|
||||
|
||||
public void add(MissionModel mission){
|
||||
public void add(int offset, MissionModel mission){
|
||||
int completeIndex = -1;
|
||||
long cargo = MainController.getProfile().getShipCargo();
|
||||
Offer offer = mission.toOffer();
|
||||
Offer offer = mission.getOffer();
|
||||
if (offer != null){
|
||||
RouteFiller.addOrders(_route, 0, offer, cargo);
|
||||
completeIndex = RouteFiller.addOrders(_route, offset, offer, cargo);
|
||||
for (RouteEntryModel entry : entries) {
|
||||
entry.sellOrders().clear();
|
||||
entry.refresh(market);
|
||||
}
|
||||
fillSellOrders();
|
||||
} else
|
||||
if (mission.isDelivery()){
|
||||
completeIndex = RouteFiller.reservedCargo(_route, offset, mission.getTarget().getStation(), mission.getCount(), cargo);
|
||||
for (RouteEntryModel entry : entries) {
|
||||
entry.refresh(market);
|
||||
}
|
||||
} else
|
||||
if (mission.isCourier()){
|
||||
completeIndex = _route.find(mission.getTarget().getStation(), offset);
|
||||
}
|
||||
if (completeIndex != -1){
|
||||
entries.get(completeIndex).add(mission);
|
||||
}
|
||||
}
|
||||
|
||||
public void addAll(Collection<MissionModel> missions){
|
||||
public void addAll(int offset, Collection<MissionModel> missions){
|
||||
long cargo = MainController.getProfile().getShipCargo();
|
||||
for (MissionModel mission : missions) {
|
||||
Offer offer = mission.toOffer();
|
||||
Offer offer = mission.getOffer();
|
||||
int completeIndex = -1;
|
||||
if (offer != null){
|
||||
RouteFiller.addOrders(_route, 0, offer, cargo);
|
||||
completeIndex = RouteFiller.addOrders(_route, offset, offer, cargo);
|
||||
} else
|
||||
if (mission.isDelivery()){
|
||||
completeIndex = RouteFiller.reservedCargo(_route, offset, mission.getTarget().getStation(), mission.getCount(), cargo);
|
||||
} else
|
||||
if (mission.isCourier()){
|
||||
completeIndex = _route.find(mission.getTarget().getStation(), offset);
|
||||
}
|
||||
if (completeIndex != -1){
|
||||
entries.get(completeIndex).add(mission);
|
||||
}
|
||||
}
|
||||
for (RouteEntryModel entry : entries) {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<HBox><Label text="Станция:" /><Label fx:id="station" /></HBox>
|
||||
<HBox><Label text="Время:" /><Label fx:id="time" /></HBox>
|
||||
<HBox><Label text="Заправить:" /><Label fx:id="refuel" /></HBox>
|
||||
<HBox>
|
||||
<HBox maxHeight="200">
|
||||
<VBox>
|
||||
<Label text="Продать:" />
|
||||
<ListView fx:id="sellOrders"/>
|
||||
@@ -24,6 +24,10 @@
|
||||
<ListView fx:id="buyOrders"/>
|
||||
</VBox>
|
||||
</HBox>
|
||||
<VBox>
|
||||
<Label text="Сдать миссии:" />
|
||||
<ListView fx:id="missions"/>
|
||||
</VBox>
|
||||
<HBox>
|
||||
<Button prefWidth="30" onAction="#previous">
|
||||
<graphic><Glyph text="FontAwesome|ARROW_LEFT"/></graphic>
|
||||
|
||||
@@ -110,6 +110,22 @@ public class Route implements Comparable<Route> {
|
||||
return vendors;
|
||||
}
|
||||
|
||||
public int find(Vendor vendor, int offset){
|
||||
int size = entries.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
int index = i + offset;
|
||||
if (index >= size){
|
||||
if (isLoop()) index -= size;
|
||||
else break;
|
||||
}
|
||||
RouteEntry entry = entries.get(index);
|
||||
if (entry.is(vendor)){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean contains(Collection<Vendor> vendors){
|
||||
return vendors.isEmpty()
|
||||
|| vendors.size() <= entries.size()
|
||||
|
||||
@@ -395,7 +395,9 @@ public class RouteFiller {
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void reservedCargo(final Route route, final int offset, Vendor target, long count, long cargo){
|
||||
public static int reservedCargo(final Route route, final int offset, Vendor target, long count, long cargo){
|
||||
//TODO: compute current cargo if already reserved
|
||||
int lastIndex = -1;
|
||||
List<RouteEntry> entries = route.getEntries();
|
||||
int size = entries.size() - (route.isLoop() ? 1 : 0);
|
||||
for (int i = 0; i < size; i++) {
|
||||
@@ -403,7 +405,9 @@ public class RouteFiller {
|
||||
if (index >= size) index -= size;
|
||||
RouteEntry entry = entries.get(index);
|
||||
if (entry.isTransit()) continue;
|
||||
if (entry.is(target)) {
|
||||
if (i > 0 && entry.is(target)) {
|
||||
lastIndex = index;
|
||||
if (index == 0 && route.isLoop()) lastIndex = size;
|
||||
break;
|
||||
}
|
||||
long empty = cargo - entry.getCargo();
|
||||
@@ -412,6 +416,7 @@ public class RouteFiller {
|
||||
entry.reserve(need);
|
||||
}
|
||||
}
|
||||
return lastIndex;
|
||||
}
|
||||
|
||||
public static void fillCargo(final Route route, final int offset, Vendor target, long count){
|
||||
@@ -443,7 +448,7 @@ public class RouteFiller {
|
||||
}
|
||||
}
|
||||
|
||||
public static void addOrders(final Route route, final int startEntry, final Offer buyOffer, final long cargo){
|
||||
public static int addOrders(final Route route, final int startEntry, final Offer buyOffer, final long cargo){
|
||||
final double[] profits = getLostProfits(route, startEntry, buyOffer.getVendor(), buyOffer.getCount(), cargo);
|
||||
|
||||
class SortHelper {
|
||||
@@ -474,18 +479,23 @@ public class RouteFiller {
|
||||
}
|
||||
sortedEntries.sort((e1, e2) -> Double.compare(e1.getProfit(), e2.getProfit()));
|
||||
|
||||
int completeIndex = -1;
|
||||
long need = buyOffer.getCount();
|
||||
double balance = route.getBalance();
|
||||
for (SortHelper helper : sortedEntries) {
|
||||
RouteEntry entry = helper.entry;
|
||||
if (helper.sell != null){
|
||||
Order order = new Order(helper.sell.getSell(), buyOffer, balance, need);
|
||||
reservedCargo(route, helper.index, buyOffer.getVendor(), order.getCount(), cargo);
|
||||
int lastIndex = reservedCargo(route, helper.index, buyOffer.getVendor(), order.getCount(), cargo);
|
||||
if (completeIndex == -1 || completeIndex+startEntry < completeIndex + startEntry){
|
||||
completeIndex = lastIndex;
|
||||
}
|
||||
entry.addOrder(order);
|
||||
need -= order.getCount();
|
||||
if (need <= 0) break;
|
||||
}
|
||||
balance += entry.getProfit();
|
||||
}
|
||||
return completeIndex;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -660,7 +660,8 @@ public class RouteFillerTest extends Assert {
|
||||
|
||||
/* v1 3x100 + 2x20 -> v2 5x50 -> v3 3x60 -> v4 5x20 -> v3 3x10 -> v1 */
|
||||
Offer offer = SimpleOffer.fakeBuy(v2, ITEM3, 210, 3);
|
||||
RouteFiller.addOrders(route, 1, offer, cargo);
|
||||
int completeIndex = RouteFiller.addOrders(route, 1, offer, cargo);
|
||||
assertEquals(1, completeIndex);
|
||||
|
||||
Order order1 = new Order(v1.getSell(ITEM2), v2.getBuy(ITEM2), 3);
|
||||
Order order2 = new Order(v1.getSell(ITEM4), v2.getBuy(ITEM4), 1);
|
||||
|
||||
Reference in New Issue
Block a user