Archived
0

implement add missions to route which don't have target

This commit is contained in:
iMoHax
2015-11-19 15:38:43 +03:00
parent 5d7ffaca6a
commit 34f789eae0
5 changed files with 179 additions and 25 deletions

View File

@@ -8,6 +8,7 @@ import javafx.fxml.FXML;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import ru.trader.analysis.CrawlerSpecificator;
import ru.trader.model.*; import ru.trader.model.*;
import ru.trader.model.support.BindingsHelper; import ru.trader.model.support.BindingsHelper;
import ru.trader.view.support.Track; import ru.trader.view.support.Track;
@@ -17,6 +18,8 @@ import ru.trader.view.support.autocomplete.CachedSuggestionProvider;
import ru.trader.view.support.autocomplete.SystemsProvider; import ru.trader.view.support.autocomplete.SystemsProvider;
import ru.trader.view.support.cells.OrderListCell; import ru.trader.view.support.cells.OrderListCell;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class RouteTrackController { public class RouteTrackController {
@@ -129,10 +132,6 @@ public class RouteTrackController {
if (index != -1) { if (index != -1) {
RouteEntryModel entry = route.get(index); RouteEntryModel entry = route.get(index);
missionsController.setStation(entry.getStation()); missionsController.setStation(entry.getStation());
ObservableList<String> stations = BindingsHelper.observableList(route.getStations(index), StationModel::getFullName);
missionsController.setStations(stations);
ObservableList<ItemModel> items = FXCollections.observableList(route.getSellOffers(index).stream().map(OfferModel::getItem).collect(Collectors.toList()));
missionsController.setItems(items);
station.setText(entry.getStation().getName()); station.setText(entry.getStation().getName());
system.setText(entry.getStation().getSystem().getName()); system.setText(entry.getStation().getSystem().getName());
@@ -143,8 +142,6 @@ public class RouteTrackController {
missionsList.setItems(entry.missions()); missionsList.setItems(entry.missions());
} else { } else {
missionsController.setStation(ModelFabric.NONE_STATION); missionsController.setStation(ModelFabric.NONE_STATION);
missionsController.setStations(FXCollections.emptyObservableList());
missionsController.setItems(FXCollections.emptyObservableList());
station.setText(""); station.setText("");
system.setText(""); system.setText("");
@@ -169,8 +166,43 @@ public class RouteTrackController {
@FXML @FXML
private void addMissionsToTrack(){ private void addMissionsToTrack(){
int startIndex = trackNode.getActive(); addMissionsToTrack(addMissionsList.getItems(), false);
route.addAll(startIndex, addMissionsList.getItems()); }
@FXML
private void addAllMissionsToTrack(){
addMissionsToTrack(addMissionsList.getItems(), true);
}
private void addMissionsToTrack(Collection<MissionModel> missions, boolean all){
if (missions.isEmpty()) return;
final int startIndex = trackNode.getActive();
final Collection<MissionModel> notAdded = route.addAll(startIndex, missions);
if (all && !notAdded.isEmpty()){
CrawlerSpecificator specificator = new CrawlerSpecificator();
specificator.setFullScan(false);
final Collection<MissionModel> oldMissions = route.getMissions(startIndex);
oldMissions.forEach(m -> m.toSpecification(specificator));
notAdded.forEach(m -> m.toSpecification(specificator));
StationModel from = route.get(startIndex).getStation();
StationModel to = route.getLast().getStation();
route.getMarket().getRoutes(from, to, route.getBalance(startIndex), specificator, routes -> {
Optional<RouteModel> path = Screeners.showRouters(routes);
if (path.isPresent()) {
route.removeAll(oldMissions);
RouteModel newRoute = route.set(startIndex, path.get());
newRoute.addAll(startIndex, notAdded);
newRoute.addAll(startIndex, oldMissions);
setRoute(newRoute);
}
});
} else {
if (notAdded.isEmpty()){
Screeners.showInfo("Результат операции", null, "Миссии добавлены");
} else {
Screeners.showInfo("Результат операции", "Миссии не добавлены", notAdded.toString());
}
}
} }

View File

@@ -122,6 +122,16 @@ public class OrderModel {
return buyOffer != null ? buyer.get().getStation() : null; return buyOffer != null ? buyer.get().getStation() : null;
} }
public double getCredit(){
return getCount() * getOffer().getPrice();
}
public double getDebet(){
OfferModel buyOffer = getBuyOffer();
return buyOffer != null ? getCount() * getBuyOffer().getPrice() : 0;
}
public double getProfit() { public double getProfit() {
return profitProperty().get(); return profitProperty().get();
} }

View File

@@ -59,14 +59,23 @@ public class RouteModel {
} }
} }
public MarketModel getMarket(){
return market;
}
Route getRoute() { Route getRoute() {
return _route; return _route;
} }
private RouteModel getCopy(){
RouteModel res = new RouteModel(_route, market); private RouteModel copyFill(Route route){
return copyFill(route, entries.size()-1);
}
private RouteModel copyFill(Route route, int index){
RouteModel res = new RouteModel(route, market);
res.setCurrentEntry(getCurrentEntry()); res.setCurrentEntry(getCurrentEntry());
int size = Math.min(entries.size(), res.entries.size()); int size = Math.min(index+1, res.entries.size());
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
RouteEntryModel entry = entries.get(i); RouteEntryModel entry = entries.get(i);
RouteEntryModel rEntry = res.entries.get(i); RouteEntryModel rEntry = res.entries.get(i);
@@ -83,6 +92,11 @@ public class RouteModel {
return entries; return entries;
} }
public RouteEntryModel getLast(){
if (entries.size() == 1) return entries.get(0);
return entries.get(entries.size()-1);
}
public double getDistance() { public double getDistance() {
return _route.getDistance(); return _route.getDistance();
} }
@@ -137,8 +151,9 @@ public class RouteModel {
public RouteModel add(OrderModel order){ public RouteModel add(OrderModel order){
Route path = market._getPath(order); Route path = market._getPath(order);
if (path == null) return this; if (path == null) return this;
_route.join(path); Route route = Route.clone(_route);
return getCopy(); route.join(path);
return copyFill(route);
} }
public RouteModel add(SystemModel system){ public RouteModel add(SystemModel system){
@@ -156,17 +171,26 @@ public class RouteModel {
} }
public RouteModel add(RouteModel route){ public RouteModel add(RouteModel route){
_route.join(ModelFabric.get(route)); Route res = Route.clone(_route);
return getCopy(); res.join(ModelFabric.get(route));
return copyFill(res);
}
public RouteModel set(int offset, RouteModel route){
Route res = Route.clone(_route);
res.dropTo(offset);
res.join(ModelFabric.get(route));
return copyFill(res, offset);
} }
public RouteModel remove(OrderModel order) { public RouteModel remove(OrderModel order) {
_route.dropTo(ModelFabric.get(order.getStation())); Route res = Route.clone(_route);
return getCopy(); res.dropTo(ModelFabric.get(order.getStation()));
return copyFill(res);
} }
public void add(int offset, MissionModel mission){ public boolean add(int offset, MissionModel mission){
mission = mission.getCopy(); mission = MissionModel.copy(mission);
int completeIndex = -1; int completeIndex = -1;
Offer offer = mission.getOffer(); Offer offer = mission.getOffer();
if (offer != null){ if (offer != null){
@@ -198,16 +222,24 @@ public class RouteModel {
} }
if (completeIndex != -1){ if (completeIndex != -1){
entries.get(completeIndex).add(mission); entries.get(completeIndex).add(mission);
return true;
} }
return false;
} }
public void addAll(int offset, Collection<MissionModel> missions){ public Collection<MissionModel> addAll(int offset, Collection<MissionModel> missions){
for (MissionModel mission : missions) { Collection<MissionModel> notAdded = new ArrayList<>();
mission = mission.getCopy(); for (MissionModel m : missions) {
MissionModel mission = MissionModel.copy(m);
Offer offer = mission.getOffer(); Offer offer = mission.getOffer();
int completeIndex = -1; int completeIndex = -1;
if (offer != null){ if (offer != null){
Collection<RouteReserve> reserves = RouteFiller.getReserves(_route, offset, offer); Collection<RouteReserve> reserves;
if (m.getReserves() != null){
reserves = RouteFiller.changeReserves(_route, offset, offer, m.getReserves());
} else {
reserves = RouteFiller.getReserves(_route, offset, offer);
}
if (!reserves.isEmpty()) { if (!reserves.isEmpty()) {
_route.reserve(reserves); _route.reserve(reserves);
mission.setReserves(reserves); mission.setReserves(reserves);
@@ -228,6 +260,34 @@ public class RouteModel {
if (completeIndex != -1){ if (completeIndex != -1){
if (completeIndex == 0 && _route.isLoop()) completeIndex = _route.getJumps()-1; if (completeIndex == 0 && _route.isLoop()) completeIndex = _route.getJumps()-1;
entries.get(completeIndex).add(mission); entries.get(completeIndex).add(mission);
} else {
notAdded.add(mission);
}
}
refresh();
return notAdded;
}
public void remove(MissionModel mission){
Collection<RouteReserve> reserves = mission.getReserves();
if (reserves != null) {
_route.unreserve(reserves);
}
for (RouteEntryModel entry : entries) {
entry.remove(mission);
}
refresh();
}
public void removeAll(Collection<MissionModel> missions){
for (MissionModel mission : missions) {
Collection<RouteReserve> reserves = mission.getReserves();
if (reserves != null) {
_route.unreserve(reserves);
}
for (RouteEntryModel entry : entries) {
entry.remove(mission);
} }
} }
refresh(); refresh();
@@ -245,6 +305,15 @@ public class RouteModel {
return res; return res;
} }
public Collection<MissionModel> getMissions(int offset){
int startIndex = offset+1;
if (startIndex >= entries.size()) return Collections.emptyList();
List<MissionModel> res = entries.subList(startIndex, entries.size()).stream()
.flatMap(e -> e.missions().stream())
.collect(Collectors.toList());
return res;
}
public Collection<OfferModel> getSellOffers(int offset){ public Collection<OfferModel> getSellOffers(int offset){
Map<ItemModel, OfferModel> res = new HashMap<>(); Map<ItemModel, OfferModel> res = new HashMap<>();
for (StationModel station : getStations(offset)) { for (StationModel station : getStations(offset)) {
@@ -260,6 +329,15 @@ public class RouteModel {
return res.values(); return res.values();
} }
public double getBalance(int index){
int endIndex = index+1;
if (endIndex > entries.size()) endIndex = entries.size();
double balance = _route.getBalance();
balance -= entries.subList(0, endIndex).stream().flatMap(e -> e.orders().stream()).mapToDouble(OrderModel::getCredit).sum();
balance += entries.subList(0, endIndex).stream().flatMap(e -> e.sellOrders().stream()).mapToDouble(OrderModel::getDebet).sum();
return balance;
}
public int getCurrentEntry() { public int getCurrentEntry() {
return currentEntry.get(); return currentEntry.get();
} }

View File

@@ -21,7 +21,10 @@
<Button prefWidth="30" onAction="#removeMission"><graphic><Glyph text="FontAwesome|MINUS"/></graphic></Button> <Button prefWidth="30" onAction="#removeMission"><graphic><Glyph text="FontAwesome|MINUS"/></graphic></Button>
<Button prefWidth="30" onAction="#clearMissions"><graphic><Glyph text="FontAwesome|TRASH"/></graphic></Button> <Button prefWidth="30" onAction="#clearMissions"><graphic><Glyph text="FontAwesome|TRASH"/></graphic></Button>
</VBox> </VBox>
<Button prefWidth="80" text="Добавить" onAction="#addMissionsToTrack" /> <VBox>
<Button prefWidth="80" text="Добавить" onAction="#addMissionsToTrack" />
<Button prefWidth="80" text="Добавить все" onAction="#addAllMissionsToTrack" />
</VBox>
</HBox> </HBox>
</VBox> </VBox>
<VBox fx:id="infoGroup"> <VBox fx:id="infoGroup">

View File

@@ -514,4 +514,35 @@ public class RouteFiller {
} }
return reserves; return reserves;
} }
}
public static Collection<RouteReserve> changeReserves(final Route route, final int fromIndex, final Offer buyOffer, Collection<RouteReserve> oldReserves){
List<RouteReserve> reserves = new ArrayList<>();
int need = 0;
for (RouteReserve r : oldReserves) {
if (r.getFromIndex() > fromIndex){
need += r.getOrder().getCount();
}
}
int newEndIndex;
if (need > 0){
for (RouteReserve r : getReserves(route, fromIndex, buyOffer)) {
if (need <= 0) break;
if (r.getOrder().getCount() >= need){
r.getOrder().setCount(need);
}
reserves.add(r);
need -= r.getOrder().getCount();
}
newEndIndex = reserves.get(0).getToIndex();
} else {
newEndIndex = route.find(buyOffer.getVendor(), fromIndex);
}
for (RouteReserve r : oldReserves) {
if (r.getFromIndex() <= fromIndex){
RouteReserve reserve = new RouteReserve(r.getOrder(), r.getFromIndex(), newEndIndex);
reserves.add(reserve);
}
}
return reserves;
}
}