EDCE checker fixes
This commit is contained in:
@@ -15,6 +15,8 @@ import ru.trader.model.*;
|
||||
import ru.trader.model.support.StationUpdater;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -26,6 +28,9 @@ public class EDCE {
|
||||
private static ScheduledExecutorService executor;
|
||||
private static ScheduledFuture<?> checker;
|
||||
|
||||
private final static int CACHE_LIMIT = 8;
|
||||
private List<EDPacket> cache = new LinkedList<>();
|
||||
|
||||
private final ProfileModel profile;
|
||||
private final MarketModel world;
|
||||
private final StationUpdater updater;
|
||||
@@ -38,18 +43,37 @@ public class EDCE {
|
||||
this.world = world;
|
||||
this.session = new EDSession();
|
||||
this.updater = new StationUpdater(world);
|
||||
interval = 5;
|
||||
forceUpdate = true;
|
||||
interval = 20;
|
||||
}
|
||||
|
||||
public void setForceUpdate(boolean forceUpdate) {
|
||||
this.forceUpdate = forceUpdate;
|
||||
}
|
||||
|
||||
private void parseAndCheck(String json) {
|
||||
try {
|
||||
EDPacket packet = EDCEParser.parseJSON(json);
|
||||
if (cache.contains(packet)){
|
||||
if (!forceUpdate) {
|
||||
LOG.debug("Is old packet, skip");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
cache.add(packet);
|
||||
}
|
||||
if (cache.size() > CACHE_LIMIT){
|
||||
cache.remove(0);
|
||||
}
|
||||
checkCmd(packet.getCommander());
|
||||
if (checkSystem(packet.getLastSystem())){
|
||||
checkStarport(packet.getLastStarport());
|
||||
if (profile.isDocked()) {
|
||||
checkStarport(packet.getLastStarport());
|
||||
} else {
|
||||
profile.setSystem(ModelFabric.NONE_SYSTEM);
|
||||
}
|
||||
}
|
||||
checkShip(packet.getShip());
|
||||
forceUpdate = false;
|
||||
} catch (IOException e) {
|
||||
LOG.warn("Error on parse json:");
|
||||
LOG.warn("{}", json);
|
||||
@@ -75,9 +99,10 @@ public class EDCE {
|
||||
boolean found = sModel != ModelFabric.NONE_SYSTEM;
|
||||
if (!found){
|
||||
LOG.warn("Not found system {}", system.getName());
|
||||
sModel = world.add(system.getName(), 0,0,0);
|
||||
}
|
||||
profile.setSystem(sModel);
|
||||
return found;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void checkStarport(Starport starport){
|
||||
@@ -89,17 +114,13 @@ public class EDCE {
|
||||
StationModel station = sModel.get(starport.getName());
|
||||
boolean found = station != ModelFabric.NONE_STATION;
|
||||
if (!found){
|
||||
forceUpdate = false;
|
||||
LOG.info("Not found station {}, adding", starport.getName());
|
||||
updater.create(sModel);
|
||||
updater.setName(starport.getName());
|
||||
station = updateStation(starport);
|
||||
} else {
|
||||
if (!profile.getStation().equals(station) || forceUpdate){
|
||||
forceUpdate = false;
|
||||
updater.edit(station);
|
||||
updateStation(starport);
|
||||
}
|
||||
updater.edit(station);
|
||||
updateStation(starport);
|
||||
}
|
||||
profile.setStation(station);
|
||||
}
|
||||
@@ -107,7 +128,12 @@ public class EDCE {
|
||||
private StationModel updateStation(Starport starport) {
|
||||
updater.setName(starport.getName());
|
||||
for (Commodity commodity : starport.getCommodities()) {
|
||||
Optional<ItemModel> item = world.getItem(Converter.getItemId(commodity.getId()));
|
||||
String id = Converter.getItemId(commodity);
|
||||
if (id.isEmpty()){
|
||||
LOG.debug("{} is ignored, skip", commodity.getName());
|
||||
continue;
|
||||
}
|
||||
Optional<ItemModel> item = world.getItem(id);
|
||||
if (item.isPresent()){
|
||||
Optional<StationUpdater.FakeOffer> offer = updater.getOffer(item.get());
|
||||
if (offer.isPresent()){
|
||||
@@ -116,7 +142,7 @@ public class EDCE {
|
||||
LOG.error("Not found offer in updater, item: {}", item.get());
|
||||
}
|
||||
} else {
|
||||
LOG.warn("Not found item id: {}, name: {}, group: {}", commodity.getId(), commodity.getName(), commodity.getCategoryname());
|
||||
LOG.warn("Not found {}, id={}", commodity, id);
|
||||
}
|
||||
}
|
||||
StationModel res = updater.commit();
|
||||
@@ -125,8 +151,8 @@ public class EDCE {
|
||||
}
|
||||
|
||||
private void fillOffers(StationUpdater.FakeOffer offer, Commodity commodity){
|
||||
offer.setBprice(commodity.getBuyPrice());
|
||||
offer.setSprice(commodity.getSellPrice());
|
||||
offer.setSprice(commodity.getBuyPrice());
|
||||
offer.setBprice(commodity.getSellPrice());
|
||||
offer.setDemand(commodity.getDemand());
|
||||
offer.setSupply(commodity.getStock());
|
||||
}
|
||||
@@ -142,8 +168,8 @@ public class EDCE {
|
||||
|
||||
public void run(){
|
||||
if (executor == null) executor = Executors.newSingleThreadScheduledExecutor();
|
||||
LOG.debug("Start EDCE checker each {} sec", interval);
|
||||
checker = executor.scheduleAtFixedRate(new EDCEChecker(), interval, interval, TimeUnit.SECONDS);
|
||||
LOG.info("Start EDCE checker each {} sec", interval);
|
||||
checker = executor.scheduleAtFixedRate(new EDCEChecker(), 1, interval, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void shutdown() throws IOException {
|
||||
@@ -169,7 +195,7 @@ public class EDCE {
|
||||
LOG.trace("Read profile from ED");
|
||||
session.readProfile(EDCE.this::parseAndCheck);
|
||||
}
|
||||
if (session.getLastStatus() == ED_SESSION_STATUS.LOGIN_REQUIRED) {
|
||||
if (session.getLastStatus() == ED_SESSION_STATUS.LOGIN_REQUIRED || session.getLastStatus() == ED_SESSION_STATUS.LOGIN_FAILED) {
|
||||
waiting = true;
|
||||
Platform.runLater(() -> {
|
||||
Optional<Pair<String, String>> login = Screeners.showLogin();
|
||||
@@ -194,6 +220,17 @@ public class EDCE {
|
||||
}
|
||||
);
|
||||
}
|
||||
if (session.getLastStatus() == ED_SESSION_STATUS.VERIFICATION_REQUIRED){
|
||||
waiting = true;
|
||||
Platform.runLater(() -> {
|
||||
LOG.trace("Verification required, send request");
|
||||
Optional<String> code = Screeners.showVerifyCodeDialog();
|
||||
if (code.isPresent()) {
|
||||
session.submitVerifyCode(code.get());
|
||||
}
|
||||
waiting = false;
|
||||
});
|
||||
}
|
||||
} catch (Exception ex){
|
||||
LOG.error("",ex);
|
||||
}
|
||||
|
||||
@@ -3,17 +3,21 @@ package ru.trader.controllers;
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.input.MouseButton;
|
||||
import org.controlsfx.control.SegmentedButton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import javafx.fxml.FXML;
|
||||
import ru.trader.core.SERVICE_TYPE;
|
||||
import ru.trader.model.*;
|
||||
import ru.trader.model.MarketModel;
|
||||
import ru.trader.model.OfferModel;
|
||||
import ru.trader.model.StationModel;
|
||||
import ru.trader.model.SystemModel;
|
||||
import ru.trader.model.support.BindingsHelper;
|
||||
import ru.trader.model.support.ChangeMarketListener;
|
||||
import ru.trader.view.support.ViewUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -220,37 +224,41 @@ public class OffersController {
|
||||
@Override
|
||||
public void priceChange(OfferModel offer, double oldPrice, double newPrice) {
|
||||
if (station.hasBuy(offer.getItem()) || station.hasSell(offer.getItem())){
|
||||
sort();
|
||||
ViewUtils.doFX(OffersController.this::sort);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(OfferModel offer) {
|
||||
if (offer.getStation().equals(station)){
|
||||
addOffer(offer);
|
||||
ViewUtils.doFX(()-> addOffer(offer));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(StationModel station) {
|
||||
stationsBar.getButtons().add(buildStationNode(station));
|
||||
refresh();
|
||||
sort();
|
||||
ViewUtils.doFX(() -> {
|
||||
stationsBar.getButtons().add(buildStationNode(station));
|
||||
refresh();
|
||||
sort();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(OfferModel offer) {
|
||||
if (offer.getStation().equals(station)){
|
||||
removeOffer(offer);
|
||||
ViewUtils.doFX(() -> removeOffer(offer));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(StationModel station) {
|
||||
stationsBar.getToggleGroup().getSelectedToggle().setSelected(false);
|
||||
stationsBar.getButtons().removeIf(b -> b.getUserData().equals(station));
|
||||
refresh();
|
||||
sort();
|
||||
ViewUtils.doFX(() -> {
|
||||
stationsBar.getToggleGroup().getSelectedToggle().setSelected(false);
|
||||
stationsBar.getButtons().removeIf(b -> b.getUserData().equals(station));
|
||||
refresh();
|
||||
sort();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public class ProfileModel {
|
||||
this.station.set(station);
|
||||
}
|
||||
|
||||
public boolean getDocked() {
|
||||
public boolean isDocked() {
|
||||
return docked.get();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce;
|
||||
|
||||
import ru.trader.edce.entities.Commodity;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -112,8 +114,12 @@ public class Converter {
|
||||
|
||||
}
|
||||
|
||||
public static String getItemId(long edId){
|
||||
return ITEM_ID.get(edId);
|
||||
public static String getItemId(Commodity commodity){
|
||||
String id = ITEM_ID.get(commodity.getId());
|
||||
if (id == null){
|
||||
id = commodity.getName().toLowerCase().replace(" ","_");
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public static String getGroupId(String edName){
|
||||
|
||||
@@ -115,7 +115,7 @@ public class EDSession {
|
||||
}
|
||||
|
||||
public void readProfile(Consumer<String> contentConsumer){
|
||||
LOG.info("Submit profile request to {}", COMPANION_DOMAIN);
|
||||
LOG.debug("Submit profile request to {}", COMPANION_DOMAIN);
|
||||
HttpUriRequest submitRequest = RequestBuilder.get(PROFILE_URL).build();
|
||||
EDResponseHandler handler = new EDResponseHandler(content -> {
|
||||
if (lastStatus != ED_SESSION_STATUS.OK){
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Cargo {
|
||||
private int capacity;
|
||||
private int qty;
|
||||
@@ -19,4 +21,18 @@ public class Cargo {
|
||||
public void setQty(int qty) {
|
||||
this.qty = qty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Cargo cargo = (Cargo) o;
|
||||
return Objects.equals(capacity, cargo.capacity) &&
|
||||
Objects.equals(qty, cargo.qty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Commander {
|
||||
private String name;
|
||||
private long credits;
|
||||
@@ -29,4 +31,18 @@ public class Commander {
|
||||
this.docked = docked;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Commander commander = (Commander) o;
|
||||
return Objects.equals(credits, commander.credits) &&
|
||||
Objects.equals(docked, commander.docked) &&
|
||||
Objects.equals(name, commander.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(credits, docked);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Commodity {
|
||||
private long id;
|
||||
private String name;
|
||||
@@ -83,4 +85,30 @@ public class Commodity {
|
||||
public void setCategoryname(String categoryname) {
|
||||
this.categoryname = categoryname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Commodity commodity = (Commodity) o;
|
||||
return Objects.equals(id, commodity.id) &&
|
||||
Objects.equals(buyPrice, commodity.buyPrice) &&
|
||||
Objects.equals(sellPrice, commodity.sellPrice) &&
|
||||
Objects.equals(stock, commodity.stock) &&
|
||||
Objects.equals(demand, commodity.demand);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Commodity{" +
|
||||
"id:" + id +
|
||||
", name:'" + name + '\'' +
|
||||
", group:'" + categoryname + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class EDPacket {
|
||||
private Commander commander;
|
||||
private System lastSystem;
|
||||
@@ -37,4 +39,20 @@ public class EDPacket {
|
||||
public void setShip(Ship ship) {
|
||||
this.ship = ship;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
EDPacket edPacket = (EDPacket) o;
|
||||
return Objects.equals(commander, edPacket.commander) &&
|
||||
Objects.equals(lastSystem, edPacket.lastSystem) &&
|
||||
Objects.equals(lastStarport, edPacket.lastStarport) &&
|
||||
Objects.equals(ship, edPacket.ship);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(commander, lastSystem, lastStarport, ship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Fuel {
|
||||
private double capacity;
|
||||
private double lvl;
|
||||
@@ -19,4 +21,18 @@ public class Fuel {
|
||||
public void setLvl(double lvl) {
|
||||
this.lvl = lvl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Fuel fuel = (Fuel) o;
|
||||
return Objects.equals(capacity, fuel.capacity) &&
|
||||
Objects.equals(lvl, fuel.lvl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Module {
|
||||
private long id;
|
||||
private String category;
|
||||
@@ -37,4 +39,18 @@ public class Module {
|
||||
public void setCost(long cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Module module = (Module) o;
|
||||
return Objects.equals(id, module.id) &&
|
||||
Objects.equals(cost, module.cost);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Ship {
|
||||
private String name;
|
||||
@@ -70,4 +71,19 @@ public class Ship {
|
||||
return fsd != null ? fsd.getModule() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Ship ship = (Ship) o;
|
||||
return Objects.equals(name, ship.name) &&
|
||||
Objects.equals(fuel, ship.fuel) &&
|
||||
Objects.equals(cargo, ship.cargo) &&
|
||||
Objects.equals(modules, ship.modules);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Slot {
|
||||
private Module module;
|
||||
|
||||
@@ -10,4 +12,17 @@ public class Slot {
|
||||
public void setModule(Module module) {
|
||||
this.module = module;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Slot slot = (Slot) o;
|
||||
return Objects.equals(module, slot.module);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(module);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class Starport {
|
||||
private long id;
|
||||
@@ -51,4 +48,18 @@ public class Starport {
|
||||
public void setModules(Map<String, Module> modules) {
|
||||
this.modules = modules;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Starport starport = (Starport) o;
|
||||
return Objects.equals(id, starport.id) &&
|
||||
Objects.equals(commodities, starport.commodities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.trader.edce.entities;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class System {
|
||||
private long id;
|
||||
private String name;
|
||||
@@ -28,4 +30,17 @@ public class System {
|
||||
public void setFaction(String faction) {
|
||||
this.faction = faction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
System system = (System) o;
|
||||
return Objects.equals(id, system.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user