Archived
0

implement recompute, refresh path, add station to path

This commit is contained in:
iMoHax
2015-01-16 13:11:36 +03:00
parent 04957e5a86
commit 4b0c2f0a0e
13 changed files with 221 additions and 49 deletions

View File

@@ -1,7 +1,6 @@
package ru.trader.controllers;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
@@ -35,6 +34,9 @@ public class RouterController {
@FXML
private ScrollPane path;
@FXML
private Button addBtn;
@FXML
private Button editBtn;
@@ -117,6 +119,11 @@ public class RouterController {
distance.setValue(Main.SETTINGS.getDistance());
jumps.setValue(Main.SETTINGS.getJumps());
addBtn.disableProperty().bind(Bindings.createBooleanBinding(()-> {
StationModel st = tStation.getValue();
return st == null || st == ModelFabric.NONE_STATION;
}, tStation.valueProperty()));
editBtn.disableProperty().bind(tblOrders.getSelectionModel().selectedIndexProperty().isEqualTo(-1));
removeBtn.disableProperty().bind(Bindings.createBooleanBinding(()-> {
int sel = tblOrders.getSelectionModel().getSelectedIndex();
@@ -155,6 +162,7 @@ public class RouterController {
totalBalance.add(order.getProfit());
source.setValue(order.getBuyer().getSystem());
sStation.setValue(order.getBuyer());
target.setValue(ModelFabric.NONE_SYSTEM);
balance.setDisable(true);
}
@@ -163,12 +171,27 @@ public class RouterController {
totalBalance.sub(order.getProfit());
source.setValue(order.getSystem());
sStation.setValue(order.getStation());
target.setValue(ModelFabric.NONE_SYSTEM);
if (orders.isEmpty()) {
balance.setDisable(false);
source.setDisable(false);
}
}
public void addStationToRoute(){
StationModel sS = sStation.getValue();
StationModel tS = tStation.getValue();
PathRouteModel r = market.getPath(sS, tS);
if (route != null){
route = route.add(r);
} else {
route = r;
}
refreshPath();
source.setValue(tS.getSystem());
sStation.setValue(tS);
}
public void editOrders(){
OrderModel sel = tblOrders.getSelectionModel().getSelectedItem();
int index = tblOrders.getSelectionModel().getSelectedIndex();
@@ -195,6 +218,29 @@ public class RouterController {
}
}
public void recompute(){
if (route != null){
route.recompute(balance.getValue().doubleValue(), cargo.getValue().longValue());
orders.clear();
orders.addAll(route.getOrders());
refreshPath();
}
}
public void rebuild(){
if (route != null){
PathRouteModel r = market.getRoute(route, balance.getValue().doubleValue());
if (r != null){
route = r;
orders.clear();
orders.addAll(route.getOrders());
refreshPath();
} else {
recompute();
}
}
}
public void removeAll(){
orders.clear();
totalBalance.setValue(balance.getValue());
@@ -256,14 +302,14 @@ public class RouterController {
if (this.route == null){
this.route = route;
} else {
this.route.add(route.getPath());
this.route = this.route.add(route);
}
refreshPath();
}
private void addOrderToPath(OrderModel order){
if (route != null){
route.add(order);
route = route.add(order);
} else {
route = market.getPath(order);
}

View File

@@ -20,8 +20,6 @@ import ru.trader.services.OrdersSearchTask;
import ru.trader.services.RoutesSearchTask;
import ru.trader.view.support.Localization;
import java.util.Collection;
import java.util.concurrent.*;
import java.util.function.Consumer;
@@ -192,10 +190,22 @@ public class MarketModel {
getRoutes(ModelFabric.NONE_SYSTEM, ModelFabric.NONE_STATION, ModelFabric.NONE_SYSTEM, ModelFabric.NONE_STATION, balance, result);
}
PathRoute getPath(StationModel from, StationModel to) {
public PathRouteModel getRoute(PathRouteModel path, double balance) {
PathRoute p = analyzer.getPath(path.getPath().getEntries(), balance);
if (p == null) return null;
return modeler.get(p);
}
PathRoute _getPath(StationModel from, StationModel to) {
return analyzer.getPath(from.getStation(), to.getStation());
}
public PathRouteModel getPath(StationModel from, StationModel to) {
PathRoute p = analyzer.getPath(from.getStation(), to.getStation());
if (p == null) return null;
return modeler.get(p);
}
public PathRouteModel getPath(OrderModel order) {
PathRoute p = analyzer.getPath(order.getStation().getStation(), order.getBuyer().getStation());
if (p == null) return null;

View File

@@ -14,11 +14,11 @@ public class PathRouteModel {
private final int refuels;
private final int lands;
private final PathRoute path;
private final PathRoute _path;
PathRouteModel(PathRoute path, MarketModel market) {
this.market = market;
this.path = path;
this._path = path;
PathRoute p = path.getRoot();
totalProfit = p.getProfit();
lands = p.getLandsCount();
@@ -51,7 +51,7 @@ public class PathRouteModel {
}
public PathRoute getPath() {
return path;
return _path;
}
public int getLands() {
@@ -64,37 +64,48 @@ public class PathRouteModel {
public Collection<OrderModel> getOrders(){
Collection<OrderModel> res = new ArrayList<>(lands);
PathRoute p = path.getRoot();
PathRoute path = _path.getRoot();
Order cargo = null;
while (p.hasNext()){
p = p.getNext();
if (cargo == null && p.getBest()!=null){
cargo = p.getBest();
while (path.hasNext()){
path = path.getNext();
if (cargo == null && path.getBest()!=null){
cargo = path.getBest();
OrderModel order = market.getModeler().get(cargo);
order.setPath(p);
order.setPath(path);
res.add(order);
}
if (cargo!=null && cargo.isBuyer(p.get())){
if (cargo!=null && cargo.isBuyer(path.get())){
cargo = null;
}
}
return res;
}
public void add(OrderModel order){
PathRoute p = market.getPath(order.getStation(), order.getBuyer());
if (p == null) return;
p.getRoot().getNext().setOrder(new Order(order.getOffer().getOffer(), order.getBuyOffer().getOffer(), order.getCount()));
PathRoute head = path.getEnd();
add(p);
order.setPath(head);
PathRoute _add(PathRoute route){
return _path.add(route, true);
}
public void add(PathRoute route){
path.getEnd().add(route, true);
public PathRouteModel add(OrderModel order){
PathRoute path = market._getPath(order.getStation(), order.getBuyer());
if (path == null) return this;
path.getRoot().getNext().setOrder(new Order(order.getOffer().getOffer(), order.getBuyOffer().getOffer(), order.getCount()));
PathRoute head = _path.getEnd();
PathRouteModel res = new PathRouteModel(_add(path), market);
order.setPath(head);
return res;
}
public PathRouteModel add(PathRouteModel route){
return new PathRouteModel(_add(route.getPath()), market);
}
public PathRouteModel remove(OrderModel order) {
return new PathRouteModel(path.dropTo(order.getStation().getStation()), market);
return new PathRouteModel(_path.dropTo(order.getStation().getStation()), market);
}
public void recompute(double balance, long cargo) {
_path.refresh();
_path.sort(balance, cargo);
}
}