try add item, if it not found on import json of EDCE
implement import json of EDCE from file
This commit is contained in:
@@ -17,6 +17,9 @@ import ru.trader.edce.entities.System;
|
|||||||
import ru.trader.model.*;
|
import ru.trader.model.*;
|
||||||
import ru.trader.model.support.StationUpdater;
|
import ru.trader.model.support.StationUpdater;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -81,17 +84,7 @@ public class EDCE {
|
|||||||
cache.remove(0);
|
cache.remove(0);
|
||||||
}
|
}
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
checkCmd(packet.getCommander());
|
imp(packet);
|
||||||
if (checkSystem(packet.getLastSystem())){
|
|
||||||
if (packet.getCommander().isDocked()) {
|
|
||||||
checkStarport(packet.getLastStarport());
|
|
||||||
profile.setDocked(true);
|
|
||||||
} else {
|
|
||||||
profile.setDocked(false);
|
|
||||||
profile.setStation(ModelFabric.NONE_STATION);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
checkShip(packet.getShip());
|
|
||||||
forceUpdate = false;
|
forceUpdate = false;
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -100,6 +93,20 @@ public class EDCE {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void imp(EDPacket packet){
|
||||||
|
checkCmd(packet.getCommander());
|
||||||
|
if (checkSystem(packet.getLastSystem())){
|
||||||
|
if (packet.getCommander().isDocked()) {
|
||||||
|
checkStarport(packet.getLastStarport());
|
||||||
|
profile.setDocked(true);
|
||||||
|
} else {
|
||||||
|
profile.setDocked(false);
|
||||||
|
profile.setStation(ModelFabric.NONE_STATION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checkShip(packet.getShip());
|
||||||
|
}
|
||||||
|
|
||||||
private void checkCmd(Commander commander){
|
private void checkCmd(Commander commander){
|
||||||
if (commander == null){
|
if (commander == null){
|
||||||
LOG.warn("Don't read commander info");
|
LOG.warn("Don't read commander info");
|
||||||
@@ -153,6 +160,17 @@ public class EDCE {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Optional<ItemModel> item = world.getItem(id);
|
Optional<ItemModel> item = world.getItem(id);
|
||||||
|
if (!item.isPresent()){
|
||||||
|
LOG.warn("Not found {}, id={}, adding", commodity, id);
|
||||||
|
String gid = Converter.getGroupId(commodity.getCategoryname());
|
||||||
|
Optional<GroupModel> group = world.getGroups().stream().filter(g -> g.getId().equals(gid)).findAny();
|
||||||
|
if (group.isPresent()) {
|
||||||
|
ItemModel i = world.add(id, group.get());
|
||||||
|
item = Optional.ofNullable(i);
|
||||||
|
} else {
|
||||||
|
LOG.warn("Not found group, id={}, skip item", gid);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (item.isPresent()){
|
if (item.isPresent()){
|
||||||
Optional<StationUpdater.FakeOffer> offer = updater.getOffer(item.get());
|
Optional<StationUpdater.FakeOffer> offer = updater.getOffer(item.get());
|
||||||
if (offer.isPresent()){
|
if (offer.isPresent()){
|
||||||
@@ -160,8 +178,6 @@ public class EDCE {
|
|||||||
} else {
|
} else {
|
||||||
LOG.error("Not found offer in updater, item: {}", item.get());
|
LOG.error("Not found offer in updater, item: {}", item.get());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
LOG.warn("Not found {}, id={}", commodity, id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Shipyard shipyard = starport.getShips();
|
Shipyard shipyard = starport.getShips();
|
||||||
@@ -303,6 +319,17 @@ public class EDCE {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void parseFile(File file) {
|
||||||
|
String line = null;
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))){
|
||||||
|
while ((line = reader.readLine()) != null){
|
||||||
|
EDPacket packet = EDCEParser.parseJSON(line);
|
||||||
|
imp(packet);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.warn("Error on parse json:");
|
||||||
|
LOG.warn("{}", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import ru.trader.Main;
|
import ru.trader.Main;
|
||||||
|
import ru.trader.ServicesManager;
|
||||||
import ru.trader.World;
|
import ru.trader.World;
|
||||||
import ru.trader.model.*;
|
import ru.trader.model.*;
|
||||||
import ru.trader.services.MaddavoParserTask;
|
import ru.trader.services.MaddavoParserTask;
|
||||||
@@ -124,6 +125,18 @@ public class MainController {
|
|||||||
profController.initEDCEBtn();
|
profController.initEDCEBtn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void importEDCE() {
|
||||||
|
FileChooser fileChooser = new FileChooser();
|
||||||
|
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
|
||||||
|
fileChooser.getExtensionFilters().add(extFilter);
|
||||||
|
fileChooser.setInitialDirectory(new File("."));
|
||||||
|
File file = fileChooser.showOpenDialog(null);
|
||||||
|
if (file !=null) {
|
||||||
|
ServicesManager.getEdce().parseFile(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
try {
|
try {
|
||||||
World.save();
|
World.save();
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ public class StationUpdater {
|
|||||||
|
|
||||||
public StationUpdater(MarketModel market) {
|
public StationUpdater(MarketModel market) {
|
||||||
this.market = market;
|
this.market = market;
|
||||||
|
market.getNotificator().add(marketListener);
|
||||||
this.offers = BindingsHelper.observableList(MainController.getMarket().itemsProperty(), FakeOffer::new);
|
this.offers = BindingsHelper.observableList(MainController.getMarket().itemsProperty(), FakeOffer::new);
|
||||||
this.name = new SimpleStringProperty();
|
this.name = new SimpleStringProperty();
|
||||||
this.type = new SimpleObjectProperty<>();
|
this.type = new SimpleObjectProperty<>();
|
||||||
@@ -206,7 +207,13 @@ public class StationUpdater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void add(int index, ItemModel item){
|
public void add(int index, ItemModel item){
|
||||||
offers.add(index, new FakeOffer(item));
|
Optional<FakeOffer> offer = getOffer(item);
|
||||||
|
if (offer.isPresent()){
|
||||||
|
offers.remove(offer.get());
|
||||||
|
offers.add(index, offer.get());
|
||||||
|
} else {
|
||||||
|
offers.add(index, new FakeOffer(item));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public StationModel commit(){
|
public StationModel commit(){
|
||||||
@@ -476,4 +483,15 @@ public class StationUpdater {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final ChangeMarketListener marketListener = new ChangeMarketListener(){
|
||||||
|
@Override
|
||||||
|
public void add(ItemModel item) {
|
||||||
|
offers.add(new FakeOffer(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(ItemModel item) {
|
||||||
|
offers.removeIf(o -> o.hasItem(item));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
<MenuItem text="%market.offers" onAction="#impMadOffers"/>
|
<MenuItem text="%market.offers" onAction="#impMadOffers"/>
|
||||||
</Menu>
|
</Menu>
|
||||||
<MenuItem text="XML" onAction="#importWorld"/>
|
<MenuItem text="XML" onAction="#importWorld"/>
|
||||||
|
<MenuItem text="EDCE JSON" onAction="#importEDCE"/>
|
||||||
</Menu>
|
</Menu>
|
||||||
<MenuItem text="%main.menu.file.export" onAction="#exportWorld"/>
|
<MenuItem text="%main.menu.file.export" onAction="#exportWorld"/>
|
||||||
</Menu>
|
</Menu>
|
||||||
|
|||||||
Reference in New Issue
Block a user