Archived
0

add support position of station

This commit is contained in:
iMoHax
2014-08-06 16:10:33 +04:00
parent 9c5bbdc0c2
commit fe9abae295
18 changed files with 193 additions and 19 deletions

View File

@@ -50,6 +50,9 @@ public class OrdersController {
@FXML
private TableColumn<OfferModel, Double> curProfit;
@FXML
private TableColumn<OfferModel, Double> curDistance;
private OrderModel order;
@FXML
@@ -62,6 +65,10 @@ public class OrdersController {
OfferModel offer = param.getValue();
return order !=null ? order.getProfit(offer) : new SimpleDoubleProperty(Double.NaN).asObject();
});
curDistance.setCellValueFactory(param -> {
OfferModel offer = param.getValue();
return new SimpleDoubleProperty(order !=null ? order.getVendor().getDistance(offer.getVendor()) :Double.NaN).asObject();
});
}

View File

@@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
import ru.trader.core.OFFER_TYPE;
import ru.trader.model.*;
import ru.trader.model.support.BindingsHelper;
import ru.trader.view.support.NumberField;
import ru.trader.view.support.PriceStringConverter;
import ru.trader.view.support.ViewUtils;
import ru.trader.view.support.cells.TextFieldCell;
@@ -54,6 +55,14 @@ public class VendorEditorController {
@FXML
private TableColumn<FakeOffer, Double> sell;
@FXML
private NumberField x;
@FXML
private NumberField y;
@FXML
private NumberField z;
@FXML
private void initialize() {
items.getSelectionModel().setCellSelectionEnabled(true);
@@ -78,6 +87,9 @@ public class VendorEditorController {
private void fill(){
name.setText(vendor.getName());
x.setValue(vendor.getX());
y.setValue(vendor.getY());
z.setValue(vendor.getZ());
vendor.getSells().forEach(this::fillOffer);
vendor.getBuys().forEach(this::fillOffer);
}
@@ -151,11 +163,13 @@ public class VendorEditorController {
if (vendor == null) {
market.setAlert(false);
vendor = market.newVendor(name.getText());
vendor.setPosition(x.getValue().doubleValue(), y.getValue().doubleValue(), z.getValue().doubleValue());
items.getItems().forEach((o) -> commit(market, vendor, o));
market.setAlert(true);
market.add(vendor);
} else {
vendor.setName(name.getText());
vendor.setPosition(x.getValue().doubleValue(), y.getValue().doubleValue(), z.getValue().doubleValue());
items.getItems().forEach((o) -> commit(market, vendor, o));
}
}

View File

@@ -67,6 +67,15 @@ public class MarketModel {
return listener;
}
void updatePosition(VendorModel model, double x, double y, double z) {
Vendor vendor = model.getVendor();
double oldX = vendor.getX();
double oldY = vendor.getY();
double oldZ = vendor.getZ();
market.updatePosition(vendor, x, y, z);
if (alert) listener.forEach((c) -> c.positionChange(model, oldX, oldY, oldZ, x, y, z));
}
void updateName(ItemModel model, String value) {
Item item = model.getItem();
String old = item.getName();

View File

@@ -14,6 +14,7 @@ public class OrderModel {
private final ObjectProperty<OfferModel> buyer = new SimpleObjectProperty<>();
private long max;
private DoubleProperty profit;
private DoubleProperty distance;
private DoubleProperty bestProfit;
public OrderModel(OfferDescModel offer) {
@@ -87,12 +88,13 @@ public class OrderModel {
return profitProperty().get();
}
public ObjectProperty<OfferModel> buyerProperty() {
public ReadOnlyObjectProperty<OfferModel> buyerProperty() {
return buyer;
}
public void setBuyer(OfferModel buyer) {
this.buyer.set(buyer);
if (distance!=null) distance.set(getVendor().getDistance(buyer.getVendor()));
}
public OfferModel getBuyer() {
@@ -115,4 +117,13 @@ public class OrderModel {
return offer.getBuyer();
}
public ReadOnlyDoubleProperty distanceProperty() {
if (distance == null){
OfferModel buyOffer = getBuyer();
distance = new SimpleDoubleProperty(buyOffer!=null ? getVendor().getDistance(buyOffer.getVendor()) : Double.NaN);
}
return distance;
}
}

View File

@@ -36,6 +36,40 @@ public class VendorModel {
return name;
}
public void setPosition(double x, double y, double z){
if (x == vendor.getX() && y == vendor.getY() && z == vendor.getZ()) return;
LOG.info("Change position of vendor {} to ({};{};{})", vendor, x, y, z);
market.updatePosition(this, x, y, z);
}
public double getX(){
return vendor.getX();
}
public double getY(){
return vendor.getY();
}
public double getZ(){
return vendor.getZ();
}
public ReadOnlyDoubleProperty xProperty(){
return new SimpleDoubleProperty(vendor.getX());
}
public ReadOnlyDoubleProperty yProperty(){
return new SimpleDoubleProperty(vendor.getY());
}
public ReadOnlyDoubleProperty zProperty(){
return new SimpleDoubleProperty(vendor.getZ());
}
public double getDistance(VendorModel other){
return vendor.getDistance(other.vendor);
}
public List<OfferModel> getSells() {
return vendor.getAllSellOffers().stream().map(market::asModel).collect(Collectors.toList());
}

View File

@@ -31,4 +31,7 @@ public class ChangeMarketListener {
public void remove(OfferModel offer) {
}
public void positionChange(VendorModel vendor, double oldX, double oldY, double oldZ, double x, double y, double z) {
}
}

View File

@@ -12,7 +12,7 @@ public class NumberField extends TextField {
private final static NumberStringConverter converter = new NumberStringConverter("#0.#");
private final Tooltip tooltip = new Tooltip();
private final ObjectProperty<Number> number = new SimpleObjectProperty<>(0);
private final ObjectProperty<Number> number = new SimpleObjectProperty<Number>(0);
private final BooleanProperty wrong = new SimpleBooleanProperty(false);
public ObjectProperty<Number> numberProperty() {

View File

@@ -1,11 +1,21 @@
package ru.trader.view.support.cells;
import javafx.beans.NamedArg;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import ru.trader.view.support.NaNComparator;
public class DoubleCell<T> implements Callback<TableColumn<T, Double>, TableCell<T, Double>> {
private String format = "%.0f";
public DoubleCell() {
}
public DoubleCell(@NamedArg("format")String format) {
this.format = format;
}
@Override
public TableCell<T, Double> call(TableColumn<T, Double> param) {
param.setComparator(new NaNComparator<>());
@@ -17,7 +27,7 @@ public class DoubleCell<T> implements Callback<TableColumn<T, Double>, TableCell
setText(null);
setGraphic(null);
} else {
setText(String.format("%.0f", item));
setText(String.format(format, item));
setGraphic(null);
}
}

View File

@@ -11,7 +11,7 @@
<?import ru.trader.view.support.cells.OfferTableCell?>
<HBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="ru.trader.controllers.OrdersController" styleClass="dialog"
prefWidth="850">
prefWidth="1010">
<TableView fx:id="tblOrders" editable="true">
<columns>
<TableColumn minWidth="160.0" text="Товар">
@@ -24,7 +24,7 @@
<TableColumn fx:id="count" minWidth="60.0" text="Кол-во">
<cellValueFactory><PropertyValueFactory property="count"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="120.0" text="Покупатель">
<TableColumn minWidth="160.0" text="Покупатель">
<cellValueFactory><OfferCellValueImpl property="buyer"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="80.0" text="Прибыль">
@@ -51,15 +51,17 @@
<fx:reference source="bestProfit"/>
</sortOrder>
</TableView>
<TableView fx:id="tblBuyers" minWidth="215">
<TableView fx:id="tblBuyers" minWidth="335">
<columns>
<TableColumn minWidth="120.0" text="Покупатель">
<TableColumn minWidth="160.0" text="Покупатель">
<cellFactory><OfferTableCell/></cellFactory>
<cellValueFactory><PropertyValueFactory property="price"/></cellValueFactory>
</TableColumn>
<TableColumn fx:id="curDistance" minWidth="80.0" text="Дистанция">
<cellFactory><DoubleCell format="\%.2f LY"/></cellFactory>
</TableColumn>
<TableColumn fx:id="curProfit" minWidth="80.0" text="Прибыль" sortType="DESCENDING">
<cellFactory><DoubleCell/></cellFactory>
<cellValueFactory><PropertyValueFactory property="profit"/></cellValueFactory>
</TableColumn>
</columns>
<columnResizePolicy>

View File

@@ -56,10 +56,10 @@
</VBox>
<TableView fx:id="tblOrders" HBox.hgrow="ALWAYS">
<columns>
<TableColumn minWidth="230.0" text="Продавец">
<TableColumn minWidth="200.0" text="Продавец">
<cellValueFactory><PropertyValueFactory property="vendor"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="230.0" text="Товар">
<TableColumn minWidth="180.0" text="Товар">
<cellValueFactory><PropertyValueFactory property="name"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="80.0" text="Цена">
@@ -72,6 +72,10 @@
<TableColumn minWidth="120.0" text="Покупатель">
<cellValueFactory><OfferCellValueImpl property="buyer"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="80.0" text="Дистанция">
<cellFactory><DoubleCell format="\%.2f LY"/></cellFactory>
<cellValueFactory><PropertyValueFactory property="distance"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="80.0" text="Прибыль">
<cellFactory><DoubleCell/></cellFactory>
<cellValueFactory><PropertyValueFactory property="profit"/></cellValueFactory>

View File

@@ -11,21 +11,25 @@
<?import ru.trader.view.support.cells.OfferTableCell?>
<HBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="ru.trader.controllers.TopOrdersController" styleClass="dialog"
prefWidth="555">
prefWidth="715">
<TableView fx:id="tblOrders" editable="true">
<columns>
<TableColumn minWidth="160.0" text="Товар">
<cellValueFactory><PropertyValueFactory property="name"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="120.0" text="Продавец">
<TableColumn minWidth="160.0" text="Продавец">
<cellValueFactory><OfferCellValueImpl property="offer"/></cellValueFactory>
</TableColumn>
<TableColumn fx:id="count" minWidth="60.0" text="Кол-во">
<cellValueFactory><PropertyValueFactory property="count"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="120.0" text="Покупатель">
<TableColumn minWidth="160.0" text="Покупатель">
<cellValueFactory><OfferCellValueImpl property="buyer"/></cellValueFactory>
</TableColumn>
<TableColumn minWidth="80.0" text="Дистанция">
<cellFactory><DoubleCell format="\%.2f LY"/></cellFactory>
<cellValueFactory><PropertyValueFactory property="distance"/></cellValueFactory>
</TableColumn>
<TableColumn fx:id="profit" minWidth="80.0" text="Прибыль" sortType="DESCENDING">
<cellFactory><DoubleCell/></cellFactory>
<cellValueFactory><PropertyValueFactory property="profit"/></cellValueFactory>

View File

@@ -7,16 +7,27 @@
<?import org.controlsfx.glyphfont.Glyph?>
<?import ru.trader.view.support.NumberField?>
<?import javafx.geometry.Insets?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="ru.trader.controllers.VendorEditorController" styleClass="dialog"
vgap="10" hgap="4">
<TextField fx:id="name" alignment="CENTER" GridPane.columnSpan="2"/>
<VBox GridPane.rowIndex="1" alignment="CENTER" spacing="10">
<fx:define><Insets fx:id="hbox_margin" left="10" /></fx:define>
<TextField fx:id="name" GridPane.columnSpan="2" alignment="CENTER" />
<HBox GridPane.rowIndex="1" GridPane.columnIndex="1" spacing="4" alignment="BASELINE_CENTER">
<Label text="x:" HBox.margin="$hbox_margin" />
<NumberField fx:id="x" prefWidth="60" />
<Label text="y:" HBox.margin="$hbox_margin" />
<NumberField fx:id="y" prefWidth="60" />
<Label text="z:" HBox.margin="$hbox_margin" />
<NumberField fx:id="z" prefWidth="60" />
</HBox>
<VBox GridPane.rowIndex="2" alignment="CENTER" spacing="10">
<Button prefWidth="30" onAction="#up"><graphic><Glyph text="FontAwesome|ARROW_UP"/></graphic></Button>
<Button prefWidth="30" onAction="#down"><graphic><Glyph text="FontAwesome|ARROW_DOWN"/></graphic></Button>
<Button prefWidth="30" onAction="#add"><graphic><Glyph text="FontAwesome|PLUS"/></graphic></Button>
</VBox>
<TableView fx:id="items" minWidth="375.0" editable="true" GridPane.columnIndex="1" GridPane.rowIndex="1">
<TableView fx:id="items" prefWidth="375.0" editable="true" GridPane.columnIndex="1" GridPane.rowIndex="2">
<columns>
<TableColumn minWidth="200.0" text="Товар" editable="false">
<cellValueFactory><PropertyValueFactory property="name"/></cellValueFactory>

View File

@@ -47,4 +47,6 @@ public interface Market {
void updateName(Vendor vendor, String name);
void updateName(Item item, String name);
void updatePosition(Vendor vendor, double x, double y, double z);
}

View File

@@ -144,6 +144,14 @@ public abstract class MarketSupport implements Market {
vendor.setName(name);
}
@Override
public void updatePosition(Vendor vendor, double x, double y, double z){
change = true;
vendor.setX(x);
vendor.setY(y);
vendor.setZ(z);
}
@Override
public void updateName(Item item, String name){
change = true;

View File

@@ -14,6 +14,9 @@ public abstract class Vendor implements Comparable<Vendor> {
private final static Logger LOG = LoggerFactory.getLogger(Vendor.class);
private String name;
private double x;
private double y;
private double z;
protected abstract Collection<Offer> getOffers();
protected abstract Collection<Item> getItems(OFFER_TYPE offerType);
@@ -107,4 +110,37 @@ public abstract class Vendor implements Comparable<Vendor> {
if (this == other) return 0;
return name != null ? other.name != null ? name.compareTo(other.name) : -1 : 0;
}
public double getDistance(Vendor other){
return getDistance(other.x, other.y, other.z);
}
public double getDistance(double x, double y, double z){
return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y-this.y, 2) + Math.pow(z - this.z, 2));
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}

View File

@@ -9,6 +9,7 @@ import org.xml.sax.helpers.DefaultHandler;
import ru.trader.core.*;
import java.util.HashMap;
import java.util.OptionalLong;
public class MarketDocHandler extends DefaultHandler {
private final static Logger LOG = LoggerFactory.getLogger(MarketDocHandler.class);
@@ -25,6 +26,9 @@ public class MarketDocHandler extends DefaultHandler {
protected final static String TYPE_ATTR = "type";
protected final static String PRICE_ATTR = "price";
protected final static String ITEM_ATTR = "item";
protected final static String X_ATTR = "x";
protected final static String Y_ATTR = "y";
protected final static String Z_ATTR = "z";
protected Market world;
protected Vendor curVendor;
@@ -62,12 +66,17 @@ public class MarketDocHandler extends DefaultHandler {
protected void parseVendor(Attributes attributes) throws SAXException {
String name = attributes.getValue(NAME_ATTR);
onVendor(name);
String x = attributes.getValue(X_ATTR);
String y = attributes.getValue(Y_ATTR);
String z = attributes.getValue(Z_ATTR);
LOG.debug("parse vendor {} position ({};{};{})", name, x, y, z);
onVendor(name, x != null ? Double.valueOf(x) : 0, y != null ? Double.valueOf(y) : 0, z != null ? Double.valueOf(z) : 0);
}
protected void parseItem(Attributes attributes) throws SAXException {
String name = attributes.getValue(NAME_ATTR);
String id = attributes.getValue(ID_ATTR);
LOG.debug("parse item {} ({})", name, id);
onItem(name, id);
}
@@ -86,8 +95,11 @@ public class MarketDocHandler extends DefaultHandler {
curVendor.add(offer);
}
protected void onVendor(String name){
protected void onVendor(String name, double x, double y, double z){
curVendor = new SimpleVendor(name);
curVendor.setX(x);
curVendor.setY(y);
curVendor.setZ(z);
}
protected void onItem(String name, String id) {

View File

@@ -75,6 +75,10 @@ public class MarketStreamWriter {
protected void writeVendor(Vendor vendor) throws XMLStreamException {
out.writeStartElement(MarketDocHandler.VENDOR);
out.writeAttribute(MarketDocHandler.NAME_ATTR, vendor.getName());
out.writeAttribute(MarketDocHandler.X_ATTR, String.valueOf(vendor.getX()));
out.writeAttribute(MarketDocHandler.Y_ATTR, String.valueOf(vendor.getY()));
out.writeAttribute(MarketDocHandler.Z_ATTR, String.valueOf(vendor.getZ()));
for (Offer offer : vendor.getAllOffers()) {
writeOffer(offer);
}

View File

@@ -32,6 +32,9 @@
<xs:element name="offer" type="offerType" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="x" type="xs:double" use="optional"/>
<xs:attribute name="y" type="xs:double" use="optional"/>
<xs:attribute name="z" type="xs:double" use="optional"/>
</xs:complexType>
<xs:complexType name="offerType">