diff --git a/client/src/main/java/ru/trader/EMDNUpdater.java b/client/src/main/java/ru/trader/EMDNUpdater.java index 26d3902..36bc164 100644 --- a/client/src/main/java/ru/trader/EMDNUpdater.java +++ b/client/src/main/java/ru/trader/EMDNUpdater.java @@ -4,11 +4,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.trader.controllers.MainController; import ru.trader.emdn.EMDN; -import ru.trader.emdn.entities.Item; import ru.trader.emdn.entities.Message; import ru.trader.model.*; import ru.trader.model.support.StationUpdater; +import ru.trader.store.imp.entities.ItemData; +import ru.trader.store.imp.entities.StarSystemData; +import ru.trader.store.imp.entities.StationData; +import java.util.Collection; import java.util.Optional; import java.util.function.Consumer; @@ -64,39 +67,46 @@ public class EMDNUpdater { 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()); + StarSystemData data = message.getImportData(); + SystemModel system = world.get(data.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 item = world.getItem(id); - if (item.isPresent()) { - Optional offer = updater.getOffer(item.get()); - if (offer.isPresent()){ - fillOffers(offer.get(), commodity); + Collection stations = data.getStations(); + if (stations == null) return; + for (StationData stationData : stations) { + StationModel station = system.get(stationData.getName()); + if (!ModelFabric.isFake(station)){ + Collection commodities = stationData.getCommodities(); + if (commodities == null) return; + updater.edit(station); + for (ItemData commodity : commodities) { + String id = commodity.getName().toLowerCase().replace(" ","_"); + Optional item = world.getItem(id); + if (item.isPresent()) { + Optional 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.error("Not found offer in updater, item: {}", item.get()); + LOG.warn("Not found item {}, id={}", commodity, id); } - } else { - LOG.warn("Not found item {}, id={}", commodity, id); } + updater.commit(); + updater.reset(); + } else { + LOG.trace("Station not found"); } - updater.commit(); - updater.reset(); - } else { - LOG.trace("Station not found"); } } else { LOG.trace("System not found"); } } - private void fillOffers(StationUpdater.FakeOffer offer, Item commodity) { - offer.setSprice(commodity.getBuyPrice()); + private void fillOffers(StationUpdater.FakeOffer offer, ItemData commodity) { + offer.setSprice(commodity.getSellOfferPrice()); offer.setSupply(commodity.getSupply()); - offer.setBprice(commodity.getSellPrice()); + offer.setBprice(commodity.getBuyOfferPrice()); offer.setDemand(commodity.getDemand()); } diff --git a/utils/src/main/java/ru/trader/emdn/EMDNParser.java b/utils/src/main/java/ru/trader/emdn/EMDNParser.java index 359d639..d3a5a46 100644 --- a/utils/src/main/java/ru/trader/emdn/EMDNParser.java +++ b/utils/src/main/java/ru/trader/emdn/EMDNParser.java @@ -11,10 +11,6 @@ import ru.trader.emdn.entities.*; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; public class EMDNParser { private final static Logger LOG = LoggerFactory.getLogger(EMDNParser.class); @@ -34,236 +30,41 @@ public class EMDNParser { public Message parse(String json) throws IOException { JsonParser parser = mapper.getFactory().createParser(json); JsonNode node = parser.readValueAsTree(); - String schema = node.get("$schemaRef").asText(); - PARSERS p = PARSERS.getParser(schema); - if (p == null){ + String schema = node.get("$schemaRef").asText(""); + SUPPORT_VERSIONS version = SUPPORT_VERSIONS.getVersion(schema); + if (version == null){ LOG.warn("Unknown EDDN message schema {}", schema); return null; } - return p.parse(node); + return parse(node, version); } - private enum PARSERS { - V1_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/1"), - V2_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/2"), - V1("http://schemas.elite-markets.net/eddn/commodity/1"){ - @Override - protected Body parseBody(JsonNode node) { - if (node == null){ - LOG.warn("Not found message body on parse EDDN message"); - return null; - } - JsonNode systemName = node.get("systemName"); - JsonNode stationName = node.get("stationName"); - JsonNode timestamp = node.get("timestamp"); - if (systemName == null || stationName == null || timestamp == null){ - LOG.warn("Body EDDN message don't have required fields"); - return null; - } - StarSystem starSystem = new StarSystem(systemName.asText()); - Station station = new Station(stationName.asText()); - LocalDateTime dt = LocalDateTime.parse(timestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); - Body body = new Body(starSystem, station); - body.setTimestamp(dt); - body.addAll(parseCommodities(node)); - return body; - } + protected Message parse(JsonNode node, SUPPORT_VERSIONS version){ + Header header = parseHeader(node.get("header")); + JsonNode body = node.get("message"); + if (header == null || body == null) return null; + return new Message(version, header, body); + } - @Override - protected Collection parseCommodities(JsonNode node) { - JsonNode name = node.get("itemName"); - JsonNode buyPrice = node.get("buyPrice"); - JsonNode supply = node.get("stationStock"); - JsonNode supplyLevel = node.get("supplyLevel"); - JsonNode sellPrice = node.get("sellPrice"); - JsonNode demand = node.get("demand"); - JsonNode demandLevel = node.get("demandLevel"); - if (name == null || buyPrice == null || supply == null || sellPrice == null || demand == null){ - LOG.warn("Commodity of EDDN message don't have required fields"); - return null; - } - Item item = new Item(name.asText(), buyPrice.asLong(), supply.asLong(), sellPrice.asLong(), demand.asLong()); - if (supplyLevel != null){ - item.setSupplyLevel(LEVEL_TYPE.fromJSON(supplyLevel.asText())); - } - if (demandLevel != null){ - item.setDemandLevel(LEVEL_TYPE.fromJSON(demandLevel.asText())); - } - return Collections.singleton(item); - } - }, - - V2("http://schemas.elite-markets.net/eddn/commodity/2"), - - V3("http://schemas.elite-markets.net/eddn/commodity/3"){ - @Override - protected Body parseBody(JsonNode node) { - if (node == null){ - LOG.warn("Not found message body on parse EDDN message"); - return null; - } - JsonNode systemNode = node.get("system"); - JsonNode stationNode = node.get("station"); - JsonNode timestamp = node.get("timestamp"); - JsonNode commodities = node.get("commodities"); - if (systemNode == null || stationNode == null || timestamp == null || commodities == null){ - LOG.warn("Body EDDN message don't have required fields"); - return null; - } - StarSystem starSystem = parseSystem(systemNode); - Station station = parseStation(stationNode); - LocalDateTime dt = LocalDateTime.parse(timestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); - Body body = new Body(starSystem, station); - body.setTimestamp(dt); - body.addAll(parseCommodities(commodities)); - return body; - } - - private Station parseStation(JsonNode node) { - JsonNode name = node.get("name"); - JsonNode id = node.get("id"); - if (name == null){ - LOG.warn("Station in EDDN message don't have required fields"); - return null; - } - Station station = new Station(name.asText()); - if (id != null){ - station.setId(id.asLong()); - } - return station; - } - - private StarSystem parseSystem(JsonNode node) { - JsonNode name = node.get("name"); - JsonNode id = node.get("id"); - JsonNode address = node.get("address"); - if (name == null){ - LOG.warn("System in EDDN message don't have required fields"); - return null; - } - StarSystem system = new StarSystem(name.asText()); - if (id != null){ - system.setId(id.asLong()); - } - if (address != null){ - system.setAddress(address.asLong()); - } - return system; - } - - - }; - - private final String schema; - - public Message parse(JsonNode node){ - Header header = parseHeader(node.get("header")); - Body body = parseBody(node.get("message")); - if (header == null || body == null) return null; - return new Message(schema, header, body); - } - - protected Header parseHeader(JsonNode node){ - if (node == null){ - LOG.warn("Not found header on parse EDDN message"); - return null; - } - JsonNode uploaderID = node.get("uploaderID"); - JsonNode softwareName = node.get("softwareName"); - JsonNode softwareVersion = node.get("softwareVersion"); - JsonNode gatewayTimestamp = node.get("gatewayTimestamp"); - if (uploaderID == null || softwareName == null || softwareVersion == null){ - LOG.warn("Header EDDN message don't have required fields"); - return null; - } - Header header = new Header(uploaderID.asText(), softwareName.asText(), softwareVersion.asText()); - if (gatewayTimestamp != null){ - LocalDateTime dt = LocalDateTime.parse(gatewayTimestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); - header.setGatewayTimestamp(dt); - } - return header; - } - - protected Body parseBody(JsonNode node){ - if (node == null){ - LOG.warn("Not found message body on parse EDDN message"); - return null; - } - JsonNode systemName = node.get("systemName"); - JsonNode stationName = node.get("stationName"); - JsonNode timestamp = node.get("timestamp"); - JsonNode commodities = node.get("commodities"); - JsonNode ships = node.get("ships"); - if (systemName == null || stationName == null || timestamp == null || (commodities == null && ships == null)){ - LOG.warn("Body EDDN message don't have required fields"); - return null; - } - StarSystem starSystem = new StarSystem(systemName.asText()); - Station station = new Station(stationName.asText()); - LocalDateTime dt = LocalDateTime.parse(timestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); - Body body = new Body(starSystem, station); - body.setTimestamp(dt); - if (commodities != null){ - body.addAll(parseCommodities(commodities)); - } - if (ships != null){ - body.addShips(parseShips(ships)); - } - return body; - } - - protected Collection parseCommodities(JsonNode commodities) { - Collection res = new ArrayList<>(); - for (Iterator iterator = commodities.elements(); iterator.hasNext(); ) { - JsonNode node = iterator.next(); - JsonNode name = node.get("name"); - JsonNode id = node.get("id"); - JsonNode buyPrice = node.get("buyPrice"); - JsonNode supply = node.get("supply"); - JsonNode supplyLevel = node.get("supplyLevel"); - JsonNode sellPrice = node.get("sellPrice"); - JsonNode demand = node.get("demand"); - JsonNode demandLevel = node.get("demandLevel"); - if (name == null || buyPrice == null || supply == null || sellPrice == null || demand == null){ - LOG.warn("Commodity of EDDN message don't have required fields"); - return null; - } - Item item = new Item(name.asText(), buyPrice.asLong(), supply.asLong(), sellPrice.asLong(), demand.asLong()); - if (id != null){ - item.setId(id.asLong()); - } - if (supplyLevel != null){ - item.setSupplyLevel(LEVEL_TYPE.fromJSON(supplyLevel.asText())); - } - if (demandLevel != null){ - item.setDemandLevel(LEVEL_TYPE.fromJSON(demandLevel.asText())); - } - res.add(item); - } - return res; - } - - protected Collection parseShips(JsonNode ships) { - Collection res = new ArrayList<>(); - for (Iterator iterator = ships.elements(); iterator.hasNext(); ) { - JsonNode node = iterator.next(); - String name = node.asText(); - Ship ship = new Ship(name); - res.add(ship); - } - return res; - } - - private PARSERS(String schema) { - this.schema = schema; - } - - public static PARSERS getParser(String schema){ - for (PARSERS parser : PARSERS.values()) { - if (parser.schema.equals(schema)) return parser; - } + protected Header parseHeader(JsonNode node){ + if (node == null){ + LOG.warn("Not found header on parse EDDN message"); return null; } + JsonNode uploaderID = node.get("uploaderID"); + JsonNode softwareName = node.get("softwareName"); + JsonNode softwareVersion = node.get("softwareVersion"); + JsonNode gatewayTimestamp = node.get("gatewayTimestamp"); + if (uploaderID == null || softwareName == null || softwareVersion == null){ + LOG.warn("Header EDDN message don't have required fields"); + return null; + } + Header header = new Header(uploaderID.asText(), softwareName.asText(), softwareVersion.asText()); + if (gatewayTimestamp != null){ + LocalDateTime dt = LocalDateTime.parse(gatewayTimestamp.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); + header.setGatewayTimestamp(dt); + } + return header; } } diff --git a/utils/src/main/java/ru/trader/emdn/ItemData.java b/utils/src/main/java/ru/trader/emdn/ItemData.java deleted file mode 100644 index 7508a73..0000000 --- a/utils/src/main/java/ru/trader/emdn/ItemData.java +++ /dev/null @@ -1,69 +0,0 @@ -package ru.trader.emdn; - -public class ItemData { - private final String name; - private double buy; - private double sell; - private long demand; - private long stock; - - - public ItemData(String name) { - this.name = name; - } - - public void setSell(double price, long count){ - sell = price; - demand = count; - } - - public void setBuy(double price, long count){ - buy = price; - stock = count; - } - - public double getBuy() { - return buy; - } - - public double getSell() { - return sell; - } - - public long getDemand() { - return demand; - } - - public long getStock() { - return stock; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ItemData)) return false; - ItemData itemData = (ItemData) o; - return !(name != null ? !name.equals(itemData.name) : itemData.name != null); - } - - @Override - public int hashCode() { - return name != null ? name.hashCode() : 0; - } - - public String getName() { - return name; - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder("{"); - sb.append(name); - sb.append(" buy=").append(buy); - sb.append(" (").append(stock).append(")"); - sb.append(" sell=").append(sell); - sb.append(" (").append(demand).append(")"); - sb.append('}'); - return sb.toString(); - } -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/Body.java b/utils/src/main/java/ru/trader/emdn/entities/Body.java deleted file mode 100644 index 2911550..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/Body.java +++ /dev/null @@ -1,71 +0,0 @@ -package ru.trader.emdn.entities; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collection; - -public class Body { - private final StarSystem system; - private final Station station; - private LocalDateTime timestamp; - private final Collection commodities; - private final Collection ships; - - public Body(StarSystem system, Station station) { - this.system = system; - this.station = station; - commodities = new ArrayList<>(); - ships = new ArrayList<>(); - } - - public StarSystem getSystem() { - return system; - } - - public Station getStation() { - return station; - } - - public LocalDateTime getTimestamp() { - return timestamp; - } - - public void setTimestamp(LocalDateTime timestamp) { - this.timestamp = timestamp; - } - - public Collection getCommodities() { - return commodities; - } - - public void add(Item item){ - commodities.add(item); - } - - public void addAll(Collection items){ - commodities.addAll(items); - } - - public Collection getShips() { - return ships; - } - - public void add(Ship ship){ - ships.add(ship); - } - - public void addShips(Collection ships){ - this.ships.addAll(ships); - } - - @Override - public String toString() { - return "Body{" + - "system=" + system + - ", station=" + station + - ", timestamp=" + timestamp + - ", commodities=" + commodities + - ", ships=" + ships + - '}'; - } -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/EDDNItemData.java b/utils/src/main/java/ru/trader/emdn/entities/EDDNItemData.java new file mode 100644 index 0000000..e9bf3db --- /dev/null +++ b/utils/src/main/java/ru/trader/emdn/entities/EDDNItemData.java @@ -0,0 +1,94 @@ +package ru.trader.emdn.entities; + +import com.fasterxml.jackson.databind.JsonNode; +import ru.trader.store.imp.ImportDataError; +import ru.trader.store.imp.entities.ItemDataBase; + +public class EDDNItemData extends ItemDataBase { + private final JsonNode node; + private final SUPPORT_VERSIONS version; + + public EDDNItemData(JsonNode node, SUPPORT_VERSIONS version) { + this.node = node; + this.version = version; + } + + @Override + public Long getId() { + JsonNode n = node.get("id"); + if (n != null && n.isNumber()){ + return n.asLong(); + } + return null; + } + + @Override + public String getName() { + String name = null; + JsonNode n; + switch (version) { + case V1: + n = node.get("itemName"); + if (n != null) name = n.asText(); + break; + default: + n = node.get("name"); + if (n != null) name = n.asText(); + break; + } + if (name == null){ + throw new ImportDataError("EDDN message don't have commodity name"); + } + return name; + } + + @Override + public long getBuyOfferPrice() { + // buy offer price in trader = sell price in EDCE + JsonNode n = node.get("sellPrice"); + if (n != null && n.isNumber()){ + return n.asLong(); + } else { + throw new ImportDataError("EDDN message don't have commodity sell price"); + } + } + + @Override + public long getSellOfferPrice() { + // sell offer price in trader = buy price in EDCE + JsonNode n = node.get("buyPrice"); + if (n != null && n.isNumber()){ + return n.asLong(); + } else { + throw new ImportDataError("EDDN message don't have commodity buy price"); + } + } + + @Override + public long getSupply() { + JsonNode n; + switch (version) { + case V1: + n = node.get("stationStock"); + break; + default: + n = node.get("supply"); + break; + } + if (n != null && n.isNumber()){ + return n.asLong(); + } else { + throw new ImportDataError("EDDN message don't have commodity supply"); + } + } + + @Override + public long getDemand() { + JsonNode n = node.get("demand"); + if (n != null && n.isNumber()){ + return n.asLong(); + } else { + throw new ImportDataError("EDDN message don't have commodity demand"); + } + } +} diff --git a/utils/src/main/java/ru/trader/emdn/entities/EDDNShipData.java b/utils/src/main/java/ru/trader/emdn/entities/EDDNShipData.java new file mode 100644 index 0000000..8f3adc6 --- /dev/null +++ b/utils/src/main/java/ru/trader/emdn/entities/EDDNShipData.java @@ -0,0 +1,38 @@ +package ru.trader.emdn.entities; + +import com.fasterxml.jackson.databind.JsonNode; +import ru.trader.store.imp.ImportDataError; +import ru.trader.store.imp.entities.ShipDataBase; + +public class EDDNShipData extends ShipDataBase { + private final JsonNode node; + private final SUPPORT_VERSIONS version; + + public EDDNShipData(JsonNode node, SUPPORT_VERSIONS version) { + this.node = node; + this.version = version; + } + + @Override + public Long getId() { + JsonNode n = node.get("id"); + if (n != null && n.isNumber()){ + return n.asLong(); + } + return null; + } + + @Override + public String getName() { + if (node != null && node.isTextual()){ + return node.asText(); + } else { + throw new ImportDataError("EDDN message don't have ship name"); + } + } + + @Override + public Long getPrice() { + return null; + } +} diff --git a/utils/src/main/java/ru/trader/emdn/entities/EDDNStationData.java b/utils/src/main/java/ru/trader/emdn/entities/EDDNStationData.java new file mode 100644 index 0000000..9ad8ca2 --- /dev/null +++ b/utils/src/main/java/ru/trader/emdn/entities/EDDNStationData.java @@ -0,0 +1,106 @@ +package ru.trader.emdn.entities; + +import com.fasterxml.jackson.databind.JsonNode; +import org.jetbrains.annotations.Nullable; +import ru.trader.store.imp.ImportDataError; +import ru.trader.store.imp.entities.ItemData; +import ru.trader.store.imp.entities.ShipData; +import ru.trader.store.imp.entities.StationDataBase; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +public class EDDNStationData extends StationDataBase { + private final JsonNode node; + private final SUPPORT_VERSIONS version; + + + public EDDNStationData(JsonNode node, SUPPORT_VERSIONS version) { + this.node = node; + this.version = version; + } + + @Override + public Long getId() { + JsonNode n = node.get("id"); + if (n != null && n.isNumber()){ + return n.asLong(); + } + return null; + } + + @Override + public String getName() { + String name = null; + JsonNode n = node.get("stationName"); + if (n != null) name = n.asText(); + if (name == null){ + throw new ImportDataError("EDDN message don't have station name"); + } + return name; + } + + @Nullable + @Override + public Collection getCommodities() { + JsonNode n; + switch (version) { + case V1_SHIPYARD: + case V2_SHIPYARD: + return null; + case V1: + Collection items = new ArrayList<>(); + items.add(new EDDNItemData(node, version)); + return items; + default: + n = node.get("commodities"); + if (n != null && n.isArray()){ + items = new ArrayList<>(); + for (Iterator iterator = n.elements(); iterator.hasNext(); ) { + JsonNode itemNode = iterator.next(); + items.add(new EDDNItemData(itemNode, version)); + } + return items; + } else { + throw new ImportDataError("EDDN message don't have commodities field"); + } + } + } + + @Nullable + @Override + public Collection getShips() { + JsonNode n; + switch (version) { + case V1_SHIPYARD: + case V2_SHIPYARD: + n = node.get("ships"); + if (n != null && n.isArray()){ + Collection ships = new ArrayList<>(); + for (Iterator iterator = n.elements(); iterator.hasNext(); ) { + JsonNode shipNode = iterator.next(); + ships.add(new EDDNShipData(shipNode, version)); + } + return ships; + } else { + throw new ImportDataError("EDDN message don't have ships field"); + } + default: + return null; + } + } + + @Nullable + @Override + public LocalDateTime getModifiedTime() { + JsonNode n = node.get("timestamp"); + if (n != null && n.isTextual()){ + return LocalDateTime.parse(n.asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); + } else { + throw new ImportDataError("EDDN message don't have timestamp field"); + } + } +} diff --git a/utils/src/main/java/ru/trader/emdn/entities/EDDNSystemData.java b/utils/src/main/java/ru/trader/emdn/entities/EDDNSystemData.java new file mode 100644 index 0000000..e215b84 --- /dev/null +++ b/utils/src/main/java/ru/trader/emdn/entities/EDDNSystemData.java @@ -0,0 +1,48 @@ +package ru.trader.emdn.entities; + +import com.fasterxml.jackson.databind.JsonNode; +import org.jetbrains.annotations.Nullable; +import ru.trader.store.imp.ImportDataError; +import ru.trader.store.imp.entities.StarSystemDataBase; +import ru.trader.store.imp.entities.StationData; + +import java.util.ArrayList; +import java.util.Collection; + +public class EDDNSystemData extends StarSystemDataBase { + private final JsonNode node; + private final SUPPORT_VERSIONS version; + + public EDDNSystemData(JsonNode node, SUPPORT_VERSIONS version) { + this.node = node; + this.version = version; + } + + @Override + public Long getId() { + JsonNode n = node.get("id"); + if (n != null && n.isNumber()){ + return n.asLong(); + } + return null; + } + + @Override + public String getName() { + String name = null; + JsonNode n = node.get("systemName"); + if (n != null) name = n.asText(); + if (name == null){ + throw new ImportDataError("EDDN message don't have system name"); + } + return name; + } + + @Nullable + @Override + public Collection getStations() { + Collection stations = new ArrayList<>(); + stations.add(new EDDNStationData(node, version)); + return stations; + } +} diff --git a/utils/src/main/java/ru/trader/emdn/entities/Item.java b/utils/src/main/java/ru/trader/emdn/entities/Item.java deleted file mode 100644 index 273ce22..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/Item.java +++ /dev/null @@ -1,78 +0,0 @@ -package ru.trader.emdn.entities; - -public class Item { - private final String name; - private Long id; - private final long buyPrice; - private final long supply; - private LEVEL_TYPE supplyLevel; - private final long sellPrice; - private final long demand; - private LEVEL_TYPE demandLevel; - - public Item(String name, long buyPrice, long supply, long sellPrice, long demand) { - this.name = name; - this.buyPrice = buyPrice; - this.supply = supply; - this.sellPrice = sellPrice; - this.demand = demand; - } - - public String getName() { - return name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public long getBuyPrice() { - return buyPrice; - } - - public long getSupply() { - return supply; - } - - public LEVEL_TYPE getSupplyLevel() { - return supplyLevel; - } - - public void setSupplyLevel(LEVEL_TYPE supplyLevel) { - this.supplyLevel = supplyLevel; - } - - public long getSellPrice() { - return sellPrice; - } - - public long getDemand() { - return demand; - } - - public LEVEL_TYPE getDemandLevel() { - return demandLevel; - } - - public void setDemandLevel(LEVEL_TYPE demandLevel) { - this.demandLevel = demandLevel; - } - - @Override - public String toString() { - return "Item{" + - "name='" + name + '\'' + - ", id=" + id + - ", buyPrice=" + buyPrice + - ", supply=" + supply + - ", supplyLevel=" + supplyLevel + - ", sellPrice=" + sellPrice + - ", demand=" + demand + - ", demandLevel=" + demandLevel + - "} "; - } -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/LEVEL_TYPE.java b/utils/src/main/java/ru/trader/emdn/entities/LEVEL_TYPE.java deleted file mode 100644 index 2d952ef..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/LEVEL_TYPE.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.trader.emdn.entities; - -public enum LEVEL_TYPE { - LOW("Low"), MEDIUM("Med"), HIGH("High"); - - private final String jsonText; - - private LEVEL_TYPE(String jsonText) { - this.jsonText = jsonText; - } - - public String toJSON(){ - return jsonText; - } - - public static LEVEL_TYPE fromJSON(String text){ - for (LEVEL_TYPE level : LEVEL_TYPE.values()) { - if (level.jsonText.equals(text)) return level; - } - return null; - } - - -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/Message.java b/utils/src/main/java/ru/trader/emdn/entities/Message.java index 3011c0d..a80491b 100644 --- a/utils/src/main/java/ru/trader/emdn/entities/Message.java +++ b/utils/src/main/java/ru/trader/emdn/entities/Message.java @@ -1,37 +1,37 @@ package ru.trader.emdn.entities; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.JsonNode; +import ru.trader.store.imp.entities.StarSystemData; public class Message { - @JsonProperty("$schemaRef") - private String schemaRef; - + private SUPPORT_VERSIONS version; private Header header; + private JsonNode body; - private Body body; - - public Message(String schemaRef, Header header, Body body) { - this.schemaRef = schemaRef; + public Message(SUPPORT_VERSIONS version, Header header, JsonNode body) { + this.version = version; this.header = header; this.body = body; } - public String getSchemaRef() { - return schemaRef; + public SUPPORT_VERSIONS getVersion() { + return version; } public Header getHeader() { return header; } - public Body getBody() { - return body; + public StarSystemData getImportData(){ + return new EDDNSystemData(body, version); + } + @Override public String toString() { return "Message{" + - "schemaRef='" + schemaRef + '\'' + + "version='" + version + '\'' + ", header=" + header + ", body=" + body + "} "; diff --git a/utils/src/main/java/ru/trader/emdn/entities/SUPPORT_VERSIONS.java b/utils/src/main/java/ru/trader/emdn/entities/SUPPORT_VERSIONS.java new file mode 100644 index 0000000..d3fa4f5 --- /dev/null +++ b/utils/src/main/java/ru/trader/emdn/entities/SUPPORT_VERSIONS.java @@ -0,0 +1,22 @@ +package ru.trader.emdn.entities; + +public enum SUPPORT_VERSIONS { + V1_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/1"), + V2_SHIPYARD("http://schemas.elite-markets.net/eddn/shipyard/2"), + V1("http://schemas.elite-markets.net/eddn/commodity/1"), + V2("http://schemas.elite-markets.net/eddn/commodity/2"), + V3("http://schemas.elite-markets.net/eddn/commodity/3"); + + private final String schema; + + private SUPPORT_VERSIONS(String schema) { + this.schema = schema; + } + + public static SUPPORT_VERSIONS getVersion(String schema){ + for (SUPPORT_VERSIONS parser : SUPPORT_VERSIONS.values()) { + if (parser.schema.equals(schema)) return parser; + } + return null; + } + } diff --git a/utils/src/main/java/ru/trader/emdn/entities/Ship.java b/utils/src/main/java/ru/trader/emdn/entities/Ship.java deleted file mode 100644 index 711f027..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/Ship.java +++ /dev/null @@ -1,40 +0,0 @@ -package ru.trader.emdn.entities; - -public class Ship { - private final String name; - private Long price; - private Long id; - - public Ship(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getPrice() { - return price; - } - - public void setPrice(Long price) { - this.price = price; - } - - @Override - public String toString() { - return "Ship{" + - "name='" + name + '\'' + - ", id=" + id + - ", price=" + price + - "} "; - } -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/StarSystem.java b/utils/src/main/java/ru/trader/emdn/entities/StarSystem.java deleted file mode 100644 index 01c2308..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/StarSystem.java +++ /dev/null @@ -1,40 +0,0 @@ -package ru.trader.emdn.entities; - -public class StarSystem { - private final String name; - private Long id; - private Long address; - - public StarSystem(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getAddress() { - return address; - } - - public void setAddress(Long address) { - this.address = address; - } - - @Override - public String toString() { - return "StarSystem{" + - "name='" + name + '\'' + - ", id=" + id + - ", address=" + address + - "} "; - } -} diff --git a/utils/src/main/java/ru/trader/emdn/entities/Station.java b/utils/src/main/java/ru/trader/emdn/entities/Station.java deleted file mode 100644 index e766f8c..0000000 --- a/utils/src/main/java/ru/trader/emdn/entities/Station.java +++ /dev/null @@ -1,30 +0,0 @@ -package ru.trader.emdn.entities; - -public class Station { - private final String name; - private Long id; - - public Station(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - @Override - public String toString() { - return "Station{" + - "name='" + name + '\'' + - ", id=" + id + - "} "; - } -} diff --git a/utils/src/main/java/ru/trader/store/imp/AbstractImporter.java b/utils/src/main/java/ru/trader/store/imp/AbstractImporter.java new file mode 100644 index 0000000..037abf6 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/AbstractImporter.java @@ -0,0 +1,318 @@ +package ru.trader.store.imp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.core.*; +import ru.trader.store.imp.entities.*; + +import java.util.*; +import java.util.function.Predicate; + +public abstract class AbstractImporter implements Importer { + private final static Logger LOG = LoggerFactory.getLogger(AbstractImporter.class); + private final EnumSet flags; + + protected AbstractImporter() { + this.flags = EnumSet.copyOf(IMPORT_FLAG.ADD_AND_UPDATE); + } + + @Override + public void addFlag(IMPORT_FLAG flag) { + flags.add(flag); + } + + @Override + public void removeFlag(IMPORT_FLAG flag) { + flags.remove(flag); + } + + @Override + public void setFlags(EnumSet flags) { + this.flags.clear(); + this.flags.addAll(flags); + } + + @Override + public void imp(Market market){ + while (next()){ + StarSystemData systemData = getSystem(); + Place system = impSystem(market, systemData); + if (system != null){ + Collection stations = systemData.getStations(); + impStations(market, system, stations); + } else { + LOG.warn("System {} not found", systemData.getName()); + } + } + } + + protected Place impSystem(Market market, StarSystemData data){ + Place system = market.get(data.getName()); + if (system == null){ + if (flags.contains(IMPORT_FLAG.ADD_STARSYSTEMS)){ + LOG.debug("{} - is new system, adding", data.getName()); + system = market.addPlace(data.getName(), data.getX(), data.getY(), data.getZ()); + } + } + if (flags.contains(IMPORT_FLAG.STARSYSTEMS)){ + updateSystem(system, data); + } + return system; + } + + protected void updateSystem(Place system, StarSystemData data){ + if (!Double.isNaN(data.getX()) && !Double.isNaN(data.getY()) && !Double.isNaN(data.getZ()) && + (data.getX() != system.getX() || data.getY() != system.getY() || data.getZ() != system.getZ())){ + system.setPosition(data.getX(), data.getY(), data.getZ()); + } + if (data.getFaction() != null){ + system.setFaction(data.getFaction()); + } + if (data.getGovernment() != null){ + system.setGovernment(data.getGovernment()); + } + if (data.getPower() != null && data.getPowerState() != null){ + system.setPower(data.getPower(), data.getPowerState()); + } + } + + protected void impStations(Market market, Place system, Collection stations){ + if (stations == null) return; + Set stationsList = new HashSet<>(); + if (flags.contains(IMPORT_FLAG.REMOVE_STATIONS)){ + stationsList.addAll(system.getVendorNames()); + } + for (StationData s : stations) { + Vendor station = impStation(system, s); + if (station != null){ + stationsList.remove(station.getName()); + impItems(market, station, s.getCommodities(), s.getModules(), s.getShips()); + } else { + LOG.warn("Station {} not found", s.getName()); + } + } + if (flags.contains(IMPORT_FLAG.REMOVE_STATIONS)){ + for (String s : stationsList) { + LOG.debug("{} - is old station, remove", s); + Vendor station = system.get(s); + if (station != null){ + system.remove(station); + } + } + } + } + + protected Vendor impStation(Place system, StationData data){ + Vendor station = system.get(data.getName()); + if (station == null){ + if (flags.contains(IMPORT_FLAG.ADD_STATIONS)){ + LOG.debug("{} - is new station, adding", data.getName()); + station = system.addVendor(data.getName()); + } + } + if (flags.contains(IMPORT_FLAG.STATIONS)){ + updateStation(station, data); + } + return station; + } + + protected void updateStation(Vendor station, StationData data) { + if (!Double.isNaN(data.getDistance())){ + station.setDistance(data.getDistance()); + } + if (data.getType() != null){ + station.setType(data.getType()); + } + if (data.getFaction() != null){ + station.setFaction(data.getFaction()); + } + if (data.getGovernment() != null){ + station.setGovernment(data.getGovernment()); + } + if (data.getEconomic() != null){ + station.setEconomic(data.getEconomic()); + } + if (data.getSubEconomic() != null){ + station.setSubEconomic(data.getSubEconomic()); + } + if (data.getServices() != null){ + Collection services = new ArrayList<>(station.getServices()); + services.removeAll(data.getServices()); + services.forEach(station::remove); + data.getServices().forEach(station::add); + } + if (data.getModifiedTime() != null){ + station.setModifiedTime(data.getModifiedTime()); + } + } + + + protected void impItems(Market market, Vendor station, Collection commodities, Collection modules, Collection ships){ + Set itemsList = new HashSet<>(); + if (flags.contains(IMPORT_FLAG.REMOVE_COMMODITY) || flags.contains(IMPORT_FLAG.REMOVE_MODULE) || flags.contains(IMPORT_FLAG.REMOVE_SHIP)){ + Predicate isCanRemove = o -> + ships != null && o.getItem().getGroup().isShip() && flags.contains(IMPORT_FLAG.REMOVE_SHIP) || + modules != null && o.getItem().getGroup().isOutfit() && flags.contains(IMPORT_FLAG.REMOVE_MODULE) || + commodities != null && o.getItem().getGroup().isMarket() && flags.contains(IMPORT_FLAG.REMOVE_COMMODITY); + station.getAllSellOffers().stream().filter(isCanRemove).map(Offer::getItem).forEach(itemsList::add); + station.getAllBuyOffers().stream().filter(isCanRemove).map(Offer::getItem).forEach(itemsList::add); + } + if (commodities != null){ + for (ItemData c : commodities) { + Item item = impItem(market, station, c); + if (item != null){ + itemsList.remove(item); + } else { + LOG.warn("Item {}({}) not found", c.getId(), c.getName()); + } + } + } + if (modules != null){ + for (ModuleData m : modules) { + Item item = impModule(market, station, m); + if (item != null){ + itemsList.remove(item); + } else { + LOG.warn("Item {}({}) not found", m.getId(), m.getName()); + } + } + } + if (ships != null){ + for (ShipData s : ships) { + Item item = impShip(market, station, s); + if (item != null){ + itemsList.remove(item); + } else { + LOG.warn("Item {}({}) not found", s.getId(), s.getName()); + } + } + } + + if (flags.contains(IMPORT_FLAG.REMOVE_COMMODITY) || flags.contains(IMPORT_FLAG.REMOVE_MODULE) || flags.contains(IMPORT_FLAG.REMOVE_SHIP)){ + for (Item i : itemsList) { + Offer o = station.get(OFFER_TYPE.SELL, i); + if (o != null){ + LOG.debug("{} - is old offer, remove", o); + station.remove(o); + } + o = station.get(OFFER_TYPE.BUY, i); + if (o != null){ + LOG.debug("{} - is old offer, remove", o); + station.remove(o); + } + } + } + } + + protected Item impItem(Market market, Vendor station, ItemData data) { + Item item = market.getItem(data.getName()); + if (item == null){ + if (data.getGroup() != null && flags.contains(IMPORT_FLAG.ADD_COMMODITY)){ + LOG.debug("{} - is new commodity, adding", data.getName()); + Optional group = market.getGroups().stream().filter(g -> g.getName().equals(data.getGroup())).findAny(); + if (group.isPresent()) { + item = market.addItem(data.getName(), group.get()); + } else { + LOG.warn("Not found group, id={}, skip item", data.getGroup()); + } + } + } + if (item != null && flags.contains(IMPORT_FLAG.ITEMS)){ + updateOffers(station, item, data); + } + return item; + } + + protected void updateOffers(Vendor station, Item item, ItemData data){ + Offer sellOffer = station.getSell(item); + if (data.getBuyOfferPrice() > 0){ + if (sellOffer != null){ + sellOffer.setPrice(data.getSellOfferPrice()); + sellOffer.setCount(data.getSupply()); + } else { + station.addOffer(OFFER_TYPE.SELL, item, data.getBuyOfferPrice(), data.getSupply()); + } + } else { + if (sellOffer != null) station.remove(sellOffer); + } + Offer buyOffer = station.getBuy(item); + if (data.getSellOfferPrice() > 0){ + if (buyOffer != null){ + buyOffer.setPrice(data.getBuyOfferPrice()); + buyOffer.setCount(data.getDemand()); + } else { + station.addOffer(OFFER_TYPE.BUY, item, data.getSellOfferPrice(), data.getDemand()); + } + } else { + if (buyOffer != null) station.remove(buyOffer); + } + } + + protected Item impShip(Market market, Vendor station, ShipData data) { + Item item = market.getItem(data.getName()); + if (item == null){ + if (flags.contains(IMPORT_FLAG.ADD_SHIP)){ + LOG.debug("{} - is new ship, adding", data.getName()); + Optional group = market.getGroups().stream().filter(Group::isShip).findAny(); + if (group.isPresent()) { + item = market.addItem(data.getName(), group.get()); + } else { + LOG.warn("Not found ship group, skip"); + } + } + } + if (item != null && flags.contains(IMPORT_FLAG.ITEMS)){ + updateOffers(station, item, data); + } + return item; + } + + protected void updateOffers(Vendor station, Item item, ShipData data){ + if (data.getPrice() == null) return; + Offer sellOffer = station.getSell(item); + if (data.getPrice() > 0){ + if (sellOffer != null){ + sellOffer.setPrice(data.getPrice()); + sellOffer.setCount(1); + } else { + station.addOffer(OFFER_TYPE.SELL, item, data.getPrice(), 1); + } + } else { + if (sellOffer != null) station.remove(sellOffer); + } + } + + protected Item impModule(Market market, Vendor station, ModuleData data) { + Item item = market.getItem(data.getName()); + if (item == null){ + if (data.getGroup() != null && flags.contains(IMPORT_FLAG.ADD_MODULE)){ + LOG.debug("{} - is new module, adding", data.getName()); + Optional group = market.getGroups().stream().filter(g -> g.getName().equals(data.getGroup())).findAny(); + if (group.isPresent()) { + item = market.addItem(data.getName(), group.get()); + } else { + LOG.warn("Not found outfit group, id={}, skip item", data.getGroup()); + } + } + } + if (item != null && flags.contains(IMPORT_FLAG.ITEMS)){ + updateOffers(station, item, data); + } + return item; + } + + protected void updateOffers(Vendor station, Item item, ModuleData data){ + Offer sellOffer = station.getSell(item); + if (data.getPrice() > 0){ + if (sellOffer != null){ + sellOffer.setPrice(data.getPrice()); + sellOffer.setCount(1); + } else { + station.addOffer(OFFER_TYPE.SELL, item, data.getPrice(), 1); + } + } else { + if (sellOffer != null) station.remove(sellOffer); + } + } + +} diff --git a/utils/src/main/java/ru/trader/store/imp/IMPORT_FLAG.java b/utils/src/main/java/ru/trader/store/imp/IMPORT_FLAG.java new file mode 100644 index 0000000..5e1a6d6 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/IMPORT_FLAG.java @@ -0,0 +1,21 @@ +package ru.trader.store.imp; + +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +public enum IMPORT_FLAG { + STARSYSTEMS, STATIONS, ITEMS, + ADD_STARSYSTEMS, + REMOVE_STATIONS, ADD_STATIONS, + REMOVE_COMMODITY, ADD_COMMODITY, + REMOVE_MODULE, ADD_MODULE, + REMOVE_SHIP, ADD_SHIP; + + public static final Set UPDATE_ONLY = Collections.unmodifiableSet(EnumSet.of(STARSYSTEMS, STATIONS, ITEMS)); + public static final Set ADD_AND_UPDATE = Collections.unmodifiableSet(EnumSet.of(STARSYSTEMS, STATIONS, ITEMS, ADD_STARSYSTEMS, ADD_STATIONS, ADD_COMMODITY, ADD_SHIP, ADD_MODULE)); + public static final Set ADD_AND_REMOVE = Collections.unmodifiableSet(EnumSet.of(ADD_STATIONS, ADD_COMMODITY, ADD_SHIP, ADD_MODULE, REMOVE_STATIONS, REMOVE_COMMODITY, REMOVE_MODULE, REMOVE_SHIP)); + public static final Set UPDATE_MARKET = Collections.unmodifiableSet(EnumSet.of(ITEMS, ADD_STARSYSTEMS, ADD_STATIONS, ADD_COMMODITY, ADD_SHIP, ADD_MODULE)); + + +} diff --git a/utils/src/main/java/ru/trader/store/imp/ImportDataError.java b/utils/src/main/java/ru/trader/store/imp/ImportDataError.java new file mode 100644 index 0000000..6d3fcf0 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/ImportDataError.java @@ -0,0 +1,19 @@ +package ru.trader.store.imp; + +public class ImportDataError extends Error { + public ImportDataError() { + super(); + } + + public ImportDataError(String message) { + super(message); + } + + public ImportDataError(String message, Throwable cause) { + super(message, cause); + } + + public ImportDataError(Throwable cause) { + super(cause); + } +} diff --git a/utils/src/main/java/ru/trader/store/imp/Importer.java b/utils/src/main/java/ru/trader/store/imp/Importer.java new file mode 100644 index 0000000..54fc3b2 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/Importer.java @@ -0,0 +1,22 @@ +package ru.trader.store.imp; + +import ru.trader.core.Market; +import ru.trader.store.imp.entities.StarSystemData; + +import java.util.EnumSet; + +public interface Importer { + + + void addFlag(IMPORT_FLAG flag); + void removeFlag(IMPORT_FLAG flag); + void setFlags(EnumSet flags); + + + boolean next(); + StarSystemData getSystem(); + + void imp(Market market); + + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ItemData.java b/utils/src/main/java/ru/trader/store/imp/entities/ItemData.java new file mode 100644 index 0000000..799ab55 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ItemData.java @@ -0,0 +1,17 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; + +public interface ItemData { + + @Nullable + Long getId(); + String getName(); + @Nullable + String getGroup(); + long getBuyOfferPrice(); + long getSellOfferPrice(); + long getSupply(); + long getDemand(); + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ItemDataBase.java b/utils/src/main/java/ru/trader/store/imp/entities/ItemDataBase.java new file mode 100644 index 0000000..f858ec1 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ItemDataBase.java @@ -0,0 +1,16 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; + +public abstract class ItemDataBase implements ItemData { + @Override + public Long getId() { + return null; + } + + @Nullable + @Override + public String getGroup() { + return null; + } +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ModuleData.java b/utils/src/main/java/ru/trader/store/imp/entities/ModuleData.java new file mode 100644 index 0000000..08b7ede --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ModuleData.java @@ -0,0 +1,14 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; + +public interface ModuleData { + + @Nullable + Long getId(); + String getName(); + @Nullable + String getGroup(); + long getPrice(); + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ModuleDataBase.java b/utils/src/main/java/ru/trader/store/imp/entities/ModuleDataBase.java new file mode 100644 index 0000000..f4956e7 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ModuleDataBase.java @@ -0,0 +1,17 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; + +public abstract class ModuleDataBase implements ModuleData { + @Override + public Long getId() { + return null; + } + + @Nullable + @Override + public String getGroup() { + return null; + } + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ShipData.java b/utils/src/main/java/ru/trader/store/imp/entities/ShipData.java new file mode 100644 index 0000000..74dc32c --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ShipData.java @@ -0,0 +1,13 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; + +public interface ShipData { + + @Nullable + Long getId(); + String getName(); + @Nullable + Long getPrice(); + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/ShipDataBase.java b/utils/src/main/java/ru/trader/store/imp/entities/ShipDataBase.java new file mode 100644 index 0000000..4ad476c --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/ShipDataBase.java @@ -0,0 +1,13 @@ +package ru.trader.store.imp.entities; + +public abstract class ShipDataBase implements ShipData { + @Override + public Long getId() { + return null; + } + + @Override + public Long getPrice() { + return null; + } +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/StarSystemData.java b/utils/src/main/java/ru/trader/store/imp/entities/StarSystemData.java new file mode 100644 index 0000000..33c980c --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/StarSystemData.java @@ -0,0 +1,31 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; +import ru.trader.core.FACTION; +import ru.trader.core.GOVERNMENT; +import ru.trader.core.POWER; +import ru.trader.core.POWER_STATE; + +import java.util.Collection; + +public interface StarSystemData { + @Nullable + Long getId(); + String getName(); + + double getX(); + double getY(); + double getZ(); + + @Nullable + FACTION getFaction(); + @Nullable + GOVERNMENT getGovernment(); + @Nullable + POWER getPower(); + @Nullable + POWER_STATE getPowerState(); + + @Nullable + Collection getStations(); +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/StarSystemDataBase.java b/utils/src/main/java/ru/trader/store/imp/entities/StarSystemDataBase.java new file mode 100644 index 0000000..a665360 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/StarSystemDataBase.java @@ -0,0 +1,61 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; +import ru.trader.core.FACTION; +import ru.trader.core.GOVERNMENT; +import ru.trader.core.POWER; +import ru.trader.core.POWER_STATE; + +import java.util.Collection; + +public abstract class StarSystemDataBase implements StarSystemData { + @Override + public Long getId() { + return null; + } + + @Override + public double getX() { + return Double.NaN; + } + + @Override + public double getY() { + return Double.NaN; + } + + @Override + public double getZ() { + return Double.NaN; + } + + @Nullable + @Override + public FACTION getFaction() { + return null; + } + + @Nullable + @Override + public GOVERNMENT getGovernment() { + return null; + } + + @Nullable + @Override + public POWER getPower() { + return null; + } + + @Nullable + @Override + public POWER_STATE getPowerState() { + return null; + } + + @Nullable + @Override + public Collection getStations() { + return null; + } +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/StationData.java b/utils/src/main/java/ru/trader/store/imp/entities/StationData.java new file mode 100644 index 0000000..09f5105 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/StationData.java @@ -0,0 +1,39 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; +import ru.trader.core.*; + +import java.time.LocalDateTime; +import java.util.Collection; + +public interface StationData { + @Nullable + Long getId(); + String getName(); + + double getDistance(); + + @Nullable + STATION_TYPE getType(); + @Nullable + FACTION getFaction(); + @Nullable + GOVERNMENT getGovernment(); + @Nullable + ECONOMIC_TYPE getEconomic(); + @Nullable + ECONOMIC_TYPE getSubEconomic(); + @Nullable + Collection getServices(); + + @Nullable + Collection getCommodities(); + @Nullable + Collection getModules(); + @Nullable + Collection getShips(); + + @Nullable + LocalDateTime getModifiedTime(); + +} diff --git a/utils/src/main/java/ru/trader/store/imp/entities/StationDataBase.java b/utils/src/main/java/ru/trader/store/imp/entities/StationDataBase.java new file mode 100644 index 0000000..9e18156 --- /dev/null +++ b/utils/src/main/java/ru/trader/store/imp/entities/StationDataBase.java @@ -0,0 +1,79 @@ +package ru.trader.store.imp.entities; + +import org.jetbrains.annotations.Nullable; +import ru.trader.core.*; + +import java.time.LocalDateTime; +import java.util.Collection; + +public abstract class StationDataBase implements StationData { + @Override + public Long getId() { + return null; + } + + @Override + public double getDistance() { + return Double.NaN; + } + + @Nullable + @Override + public STATION_TYPE getType() { + return null; + } + + @Nullable + @Override + public FACTION getFaction() { + return null; + } + + @Nullable + @Override + public GOVERNMENT getGovernment() { + return null; + } + + @Nullable + @Override + public ECONOMIC_TYPE getEconomic() { + return null; + } + + @Nullable + @Override + public ECONOMIC_TYPE getSubEconomic() { + return null; + } + + @Nullable + @Override + public Collection getServices() { + return null; + } + + @Nullable + @Override + public Collection getCommodities() { + return null; + } + + @Nullable + @Override + public Collection getModules() { + return null; + } + + @Nullable + @Override + public Collection getShips() { + return null; + } + + @Nullable + @Override + public LocalDateTime getModifiedTime() { + return null; + } +} diff --git a/utils/src/test/java/ru/trader/emdn/ParserTest.java b/utils/src/test/java/ru/trader/emdn/ParserTest.java index da6a202..a181c44 100644 --- a/utils/src/test/java/ru/trader/emdn/ParserTest.java +++ b/utils/src/test/java/ru/trader/emdn/ParserTest.java @@ -9,6 +9,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ru.trader.TestUtils; import ru.trader.emdn.entities.*; +import ru.trader.store.imp.ImportDataError; +import ru.trader.store.imp.entities.ItemData; +import ru.trader.store.imp.entities.ShipData; +import ru.trader.store.imp.entities.StarSystemData; +import ru.trader.store.imp.entities.StationData; import java.io.InputStream; import java.time.LocalDateTime; @@ -28,38 +33,50 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/commodity/1", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V1, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("abcdef0123456789", header.getUploaderId()); assertEquals("My Awesome Market Uploader", header.getSoftwareName()); assertEquals("v3.14", header.getSoftwareVersion()); assertEquals(LocalDateTime.of(2014, 11, 17, 13, 35), header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Eranin", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Eranin", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Azeban Orbital", station.getName()); - assertNull(station.getId()); - Collection items = body.getCommodities(); + assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), station.getModifiedTime()); + Collection items = station.getCommodities(); assertNotNull(items); assertEquals(1, items.size()); - Item item = items.iterator().next(); + ItemData item = items.iterator().next(); assertNotNull(item); assertEquals("Gold", item.getName()); - assertNull(item.getId()); - assertEquals(1024, item.getBuyPrice()); + assertEquals(1024, item.getSellOfferPrice()); assertEquals(7, item.getSupply()); - assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel()); - assertEquals(1138, item.getSellPrice()); + assertEquals(1138, item.getBuyOfferPrice()); assertEquals(42, item.getDemand()); - assertEquals(LEVEL_TYPE.MEDIUM, item.getDemandLevel()); + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getShips()); + assertNull(station.getModules()); + assertNull(item.getId()); + assertNull(item.getGroup()); } } @@ -72,53 +89,64 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/commodity/2", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V2, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("abcdef0123456789", header.getUploaderId()); assertEquals("My Awesome Market Uploader", header.getSoftwareName()); assertEquals("v3.14", header.getSoftwareVersion()); assertEquals(LocalDateTime.of(2014, 11, 17, 13, 35), header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Eranin", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Eranin", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Azeban Orbital", station.getName()); - assertNull(station.getId()); - Collection items = body.getCommodities(); + assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), station.getModifiedTime()); + Collection items = station.getCommodities(); assertNotNull(items); assertEquals(2, items.size()); int found = 0; - for (Item item : items) { + for (ItemData item : items) { assertNotNull(item); if ("Gold".equals(item.getName())){ found++; - assertNull(item.getId()); - assertEquals(1024, item.getBuyPrice()); + assertEquals(1024, item.getSellOfferPrice()); assertEquals(7, item.getSupply()); - assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel()); - assertEquals(1138, item.getSellPrice()); + assertEquals(1138, item.getBuyOfferPrice()); assertEquals(42, item.getDemand()); - assertEquals(LEVEL_TYPE.MEDIUM, item.getDemandLevel()); + assertNull(item.getId()); + assertNull(item.getGroup()); } else if ("Explosives".equals(item.getName())){ found++; - assertNull(item.getId()); - assertEquals(999, item.getBuyPrice()); + assertEquals(999, item.getSellOfferPrice()); assertEquals(1500, item.getSupply()); - assertEquals(LEVEL_TYPE.LOW, item.getSupplyLevel()); - assertEquals(0, item.getSellPrice()); + assertEquals(0, item.getBuyOfferPrice()); assertEquals(0, item.getDemand()); - assertNull(item.getDemandLevel()); + assertNull(item.getId()); + assertNull(item.getGroup()); } } assertEquals("Expected items not found", 2, found); + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getShips()); + assertNull(station.getModules()); } } @@ -130,38 +158,51 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/commodity/1", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V1, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("abcdef0123456789", header.getUploaderId()); assertEquals("My Awesome Market Uploader", header.getSoftwareName()); assertEquals("v3.14", header.getSoftwareVersion()); assertNull(header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Eranin", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Eranin", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Azeban Orbital", station.getName()); - assertNull(station.getId()); - Collection items = body.getCommodities(); + assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), station.getModifiedTime()); + Collection items = station.getCommodities(); assertNotNull(items); assertEquals(1, items.size()); - Item item = items.iterator().next(); + ItemData item = items.iterator().next(); assertNotNull(item); assertEquals("Gold", item.getName()); assertNull(item.getId()); - assertEquals(1024, item.getBuyPrice()); + assertEquals(1024, item.getSellOfferPrice()); assertEquals(7, item.getSupply()); - assertNull(item.getSupplyLevel()); - assertEquals(1138, item.getSellPrice()); + assertEquals(1138, item.getBuyOfferPrice()); assertEquals(42, item.getDemand()); - assertNull(item.getDemandLevel()); + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getShips()); + assertNull(station.getModules()); + assertNull(item.getId()); + assertNull(item.getGroup()); } } @@ -174,39 +215,52 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/commodity/2", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V2, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("abcdef0123456789", header.getUploaderId()); assertEquals("My Awesome Market Uploader", header.getSoftwareName()); assertEquals("v3.14", header.getSoftwareVersion()); assertNull(header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Eranin", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Eranin", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Azeban Orbital", station.getName()); - assertNull(station.getId()); - Collection items = body.getCommodities(); + assertEquals(LocalDateTime.of(2014, 11, 17, 12, 34, 56), station.getModifiedTime()); + Collection items = station.getCommodities(); assertNotNull(items); assertEquals(1, items.size()); - for (Item item : items) { + for (ItemData item : items) { assertNotNull(item); assertEquals("Gold", item.getName()); assertNull(item.getId()); - assertEquals(1024, item.getBuyPrice()); + assertEquals(1024, item.getSellOfferPrice()); assertEquals(7, item.getSupply()); - assertNull(item.getSupplyLevel()); - assertEquals(1138, item.getSellPrice()); + assertEquals(1138, item.getBuyOfferPrice()); assertEquals(42, item.getDemand()); - assertNull(item.getDemandLevel()); + assertNull(item.getId()); + assertNull(item.getGroup()); } + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getShips()); + assertNull(station.getModules()); } } @@ -226,7 +280,18 @@ public class ParserTest extends Assert { try (InputStream is = getClass().getResourceAsStream("/emdn/v2e.json")) { String json = TestUtils.read(is); Message message = parser.parse(json); - assertNull(message); + assertNotNull(message); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Eranin", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); + assertNotNull(station); + assertEquals("Azeban Orbital", station.getName()); + exception.expect(ImportDataError.class); + station.getCommodities(); } } @@ -263,33 +328,28 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/shipyard/1", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V1_SHIPYARD, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("Marek Ce'ex", header.getUploaderId()); assertEquals("E:D Market Connector [Mac OS]", header.getSoftwareName()); assertEquals("2.1.6.1", header.getSoftwareVersion()); assertEquals(LocalDateTime.of(2016, 10, 5, 13, 53, 33, 930428000), header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2016, 10, 5, 13, 53, 25), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Venegana", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Venegana", data.getName()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Shull Ring", station.getName()); - assertNull(station.getId()); - Collection items = body.getCommodities(); - assertNotNull(items); - assertEquals(0, items.size()); - Collection ships = body.getShips(); + assertEquals(LocalDateTime.of(2016, 10, 5, 13, 53, 25), station.getModifiedTime()); + Collection ships = station.getShips(); assertNotNull(ships); assertEquals(8, ships.size()); int found = 0; - for (Ship ship : ships) { + for (ShipData ship : ships) { assertNotNull(ship); if ("Sidewinder".equals(ship.getName())){ found++; @@ -303,6 +363,21 @@ public class ParserTest extends Assert { } } assertEquals("Expected ships not found", 2, found); + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getCommodities()); + assertNull(station.getModules()); } } @@ -315,33 +390,30 @@ public class ParserTest extends Assert { String json = TestUtils.read(is); Message message = parser.parse(json); assertNotNull(message); - assertEquals("http://schemas.elite-markets.net/eddn/shipyard/2", message.getSchemaRef()); + assertEquals(SUPPORT_VERSIONS.V2_SHIPYARD, message.getVersion()); Header header = message.getHeader(); assertNotNull(header); assertEquals("Amadeus Sheperd", header.getUploaderId()); assertEquals("E:D Market Connector [Windows]", header.getSoftwareName()); assertEquals("2.1.7.2", header.getSoftwareVersion()); assertEquals(LocalDateTime.of(2016, 10, 5, 13, 16, 12, 490637000), header.getGatewayTimestamp()); - Body body = message.getBody(); - assertNotNull(body); - assertEquals(LocalDateTime.of(2016, 10, 5, 13, 15, 51), body.getTimestamp()); - StarSystem system = body.getSystem(); - assertNotNull(system); - assertEquals("Sothis", system.getName()); - assertNull(system.getId()); - assertNull(system.getAddress()); - Station station = body.getStation(); + StarSystemData data = message.getImportData(); + assertNotNull(data); + assertEquals("Sothis", data.getName()); + assertNull(data.getId()); + Collection stations = data.getStations(); + assertNotNull(stations); + assertEquals(1, stations.size()); + StationData station = stations.iterator().next(); assertNotNull(station); assertEquals("Newholm Station", station.getName()); assertNull(station.getId()); - Collection items = body.getCommodities(); - assertNotNull(items); - assertEquals(0, items.size()); - Collection ships = body.getShips(); + assertEquals(LocalDateTime.of(2016, 10, 5, 13, 15, 51), station.getModifiedTime()); + Collection ships = station.getShips(); assertNotNull(ships); assertEquals(5, ships.size()); int found = 0; - for (Ship ship : ships) { + for (ShipData ship : ships) { assertNotNull(ship); if ("SideWinder".equals(ship.getName())){ found++; @@ -355,6 +427,21 @@ public class ParserTest extends Assert { } } assertEquals("Expected ships not found", 2, found); + + assertNull(data.getId()); + assertNull(data.getFaction()); + assertNull(data.getGovernment()); + assertNull(data.getPower()); + assertNull(data.getPowerState()); + assertNull(station.getId()); + assertNull(station.getType()); + assertNull(station.getFaction()); + assertNull(station.getGovernment()); + assertNull(station.getEconomic()); + assertNull(station.getSubEconomic()); + assertNull(station.getServices()); + assertNull(station.getCommodities()); + assertNull(station.getModules()); } }