Archived
0

add eddn updater to client

This commit is contained in:
Mo
2016-09-18 19:25:22 +03:00
parent ea8ea96fc5
commit 4eee75656a
5 changed files with 48 additions and 98 deletions

View File

@@ -4,74 +4,27 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.controllers.MainController;
import ru.trader.emdn.EMDN;
import ru.trader.emdn.ItemData;
import ru.trader.emdn.entities.Station;
import ru.trader.model.MarketModel;
import ru.trader.emdn.entities.Item;
import ru.trader.emdn.entities.Message;
import ru.trader.model.*;
import ru.trader.model.support.StationUpdater;
import java.util.concurrent.*;
import java.util.Optional;
import java.util.function.Consumer;
public class EMDNUpdater {
private final static Logger LOG = LoggerFactory.getLogger(EMDNUpdater.class);
private final static EMDN emdn = new EMDN();
private static ScheduledExecutorService executor;
private static ScheduledFuture<?> autoupdate;
private static MarketModel market;
private static EMDNUpdate emdnUpdater;
private static long interval;
public static void updateFromEMDN(StationUpdater updater){
}
private static void update(StationUpdater updater, Station emdnData){
/* LOG.trace("Update {} from EMDN", updater.getName());
for (StationUpdater.FakeOffer offer : updater.getOffers()) {
if (offer.getItem().isMarketItem()){
ItemData data = emdnData.getData(offer.getItem().getId());
LOG.trace("Update item {} to {}", offer.getItem().getName(), data);
if (data != null){
offer.setSprice(data.getBuy());
offer.setBprice(data.getSell());
} else {
offer.setSprice(0);
offer.setBprice(0);
}
} else {
LOG.trace("Is not market item, skip");
}
}*/
}
private static EMDN emdn;
static void init(){
setMarket(MainController.getMarket());
setSub(Main.SETTINGS.getEMDNSub());
emdn = new EMDN(Main.SETTINGS.getEMDNSub(), new MarketUpdater(MainController.getWorld()));
setActivate(Main.SETTINGS.getEMDNActive());
setUpdateOnly(Main.SETTINGS.getEMDNUpdateOnly());
if (emdn.isActive())
setInterval(Main.SETTINGS.getEMDNAutoUpdate());
}
public static void shutdown(){
if (executor != null) {
LOG.debug("Shutdown auto update");
if (autoupdate != null) autoupdate.cancel(true);
executor.shutdownNow();
}
emdn.shutdown();
}
public static void setMarket(MarketModel market) {
EMDNUpdater.market = market;
EMDNUpdate old = emdnUpdater;
emdnUpdater = new EMDNUpdate();
if (old != null){
setUpdateOnly(old.updater.isUpdateOnly());
}
if (executor != null){
setInterval(interval);
}
}
public static void setSub(String subServer){
emdn.connectTo(subServer);
}
@@ -79,56 +32,61 @@ public class EMDNUpdater {
public static void setActivate(boolean activate){
if (activate) {
emdn.start();
}
else {
setInterval(0);
} else {
emdn.stop();
}
}
public static void setUpdateOnly(boolean updateOnly) {
emdnUpdater.updater.setUpdateOnly(updateOnly);
}
public static void setInterval(long interval) {
if (emdn.isActive()){
if (autoupdate != null){
LOG.debug("Stop auto update");
autoupdate.cancel(true);
autoupdate = null;
}
if (interval > 0) {
if (executor == null) executor = Executors.newSingleThreadScheduledExecutor();
LOG.debug("Start auto update each {} sec", interval);
autoupdate = executor.scheduleAtFixedRate(emdnUpdater, interval, interval, TimeUnit.SECONDS);
}
}
EMDNUpdater.interval = interval;
}
private static class EMDNUpdate implements Runnable {
private static class MarketUpdater implements Consumer<Message> {
private final StationUpdater updater;
private final MarketModel world;
private EMDNUpdate() {
updater = new StationUpdater(market);
public MarketUpdater(MarketModel world) {
this.world = world;
this.updater = new StationUpdater(world);
}
@Override
public void run() {
/* market.getSystemNames().forEach(system -> {
LOG.trace("Auto update {}", system);
if (emdnData != null){
//TODO: implement new model
//updater.init(system);
update(updater, emdnData);
public void accept(Message message) {
if (world == null || message == null) return;
LOG.trace("Update station from EDDN: {}", message);
SystemModel system = world.get(message.getBody().getSystem().getName());
if (!ModelFabric.isFake(system)) {
StationModel station = system.get(message.getBody().getStation().getName());
if (!ModelFabric.isFake(station)){
updater.edit(station);
for (Item commodity : message.getBody().getCommodities()) {
String id = commodity.getName().toLowerCase().replace(" ","_");
Optional<ItemModel> item = world.getItem(id);
if (item.isPresent()) {
Optional<StationUpdater.FakeOffer> offer = updater.getOffer(item.get());
if (offer.isPresent()){
fillOffers(offer.get(), commodity);
} else {
LOG.error("Not found offer in updater, item: {}", item.get());
}
} else {
LOG.warn("Not found item {}, id={}", commodity, id);
}
}
updater.commit();
updater.reset();
} else {
LOG.trace("Not found in EMDN");
LOG.trace("Station not found");
}
});*/
} else {
LOG.trace("System not found");
}
}
private void fillOffers(StationUpdater.FakeOffer offer, Item commodity) {
offer.setSprice(commodity.getBuyPrice());
offer.setSupply(commodity.getSupply());
offer.setBprice(commodity.getSellPrice());
offer.setDemand(commodity.getDemand());
}
}
}

View File

@@ -90,7 +90,7 @@ public class Settings {
}
public String getEMDNSub(){
return values.getProperty("emdn.sub","tcp://firehose.elite-market-data.net:9050");
return values.getProperty("emdn.sub","tcp://eddn-relay.elite-markets.net:9500");
}
public void setEMDNUpdateOnly(boolean updateOnly){

View File

@@ -11,7 +11,6 @@ import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
import javafx.util.Pair;
import ru.trader.EMDNUpdater;
import ru.trader.core.Engine;
import ru.trader.core.MarketFilter;
import ru.trader.core.VendorFilter;
@@ -369,7 +368,6 @@ public class Screeners {
filterController.init();
vFilterController.init();
dbEditorController.init();
EMDNUpdater.setMarket(MainController.getMarket());
}
}

View File

@@ -9,7 +9,6 @@ import javafx.util.Callback;
import javafx.util.converter.LongStringConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.EMDNUpdater;
import ru.trader.core.*;
import ru.trader.model.ItemModel;
import ru.trader.model.StationModel;
@@ -223,10 +222,6 @@ public class StationEditorController {
ViewUtils.show(items, index);
}
public void updateFromEMDN(){
EMDNUpdater.updateFromEMDN(updater);
}
private class FakeOfferDecoratedRow extends DecoratedRowFactory<StationUpdater.FakeOffer> {
public FakeOfferDecoratedRow() {

View File

@@ -64,7 +64,6 @@
<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>
<Button prefWidth="30" onAction="#updateFromEMDN"><graphic><Glyph text="FontAwesome|REFRESH"/></graphic></Button>
</VBox>
<TableView fx:id="items" GridPane.rowIndex="4" prefWidth="575" maxHeight="560" editable="true" GridPane.columnIndex="1">
<columns>