Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Station reload button (#81)
Browse files Browse the repository at this point in the history
* Added a station reload button (without logic)

* Added logic to station update button

* Finished update logic
  • Loading branch information
Fi0x committed Feb 27, 2022
1 parent 81d41dd commit d19c9ae
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 31 deletions.
32 changes: 23 additions & 9 deletions src/main/java/com/fi0x/edct/gui/controller/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fi0x.edct.logic.helper.ConvertToString;
import com.fi0x.edct.logic.helper.ExternalProgram;
import com.fi0x.edct.logic.structures.TRADE;
import com.fi0x.edct.logic.websites.InaraStation;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
Expand All @@ -28,10 +29,13 @@ public class Station implements Initializable
private boolean isBuying;
public int stationID;
private String stationSystem;
private String stationName;

@FXML
private Label lblAction;
@FXML
private Button btnReloadStation;
@FXML
private Button btnBlacklist;
@FXML
private Label lblSystem;
Expand Down Expand Up @@ -67,6 +71,8 @@ public void initialize(URL url, ResourceBundle resourceBundle)
{
Image img = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/images/blacklist.png")), 20, 20, false, false);
btnBlacklist.setGraphic(new ImageView(img));
img = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/images/reload.png")), 20, 20, false, false);
btnReloadStation.setGraphic(new ImageView(img));

btnReddit.addEventHandler(MouseEvent.MOUSE_CLICKED, e ->
{
Expand Down Expand Up @@ -128,6 +134,13 @@ private void previousStation()
resultsController.displayResults();
}
@FXML
private void reloadStation()
{
TRADE currentTrade = isBuying ? resultsController.getCurrentBuyStation() : resultsController.getCurrentSellStation();
InaraStation.updateSingleStationTrades(stationName, stationSystem, currentTrade);
resultsController.displayResults();
}
@FXML
private void addToBlacklist()
{
TRADE s = isBuying ? resultsController.getCurrentBuyStation() : resultsController.getCurrentSellStation();
Expand Down Expand Up @@ -160,24 +173,25 @@ private void copySystemToClipboard()
ExternalProgram.copyToClipboard(stationSystem);
}

public void setStation(TRADE station, boolean hasPrev, boolean hasNext)
public void setStation(TRADE trade, boolean hasPrev, boolean hasNext)
{
stationSystem = station.STATION.SYSTEM;
stationSystem = trade.STATION.SYSTEM;
stationName = trade.STATION.NAME;

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(1);

lblSystem.setText("System:\t " + stationSystem);
lblStationName.setText("Station:\t " + station.STATION.NAME);
lblType.setText("Type:\t " + station.STATION.TYPE);
lblPad.setText("Pad:\t\t " + station.STATION.PAD);
lblPrice.setText("Price:\t " + df.format((isBuying ? station.BUY_PRICE : station.SELL_PRICE)) + " credits");
lblAmount.setText((isBuying ? "Demand:\t " : "Supply:\t ") + df.format((isBuying ? station.DEMAND : station.SUPPLY)) + " tons");
lblStationName.setText("Station:\t " + stationName);
lblType.setText("Type:\t " + trade.STATION.TYPE);
lblPad.setText("Pad:\t\t " + trade.STATION.PAD);
lblPrice.setText("Price:\t " + df.format((isBuying ? trade.BUY_PRICE : trade.SELL_PRICE)) + " credits");
lblAmount.setText((isBuying ? "Demand:\t " : "Supply:\t ") + df.format((isBuying ? trade.DEMAND : trade.SUPPLY)) + " tons");

df.setMaximumFractionDigits(0);

lblStarDistance.setText("Star Distance:\t " + (station.STATION.DISTANCE_TO_STAR < 0 ? "---" : df.format(station.STATION.DISTANCE_TO_STAR) + " Ls"));
lblAge.setText("Data age:\t " + station.getUpdateAge());
lblStarDistance.setText("Star Distance:\t " + (trade.STATION.DISTANCE_TO_STAR < 0 ? "---" : df.format(trade.STATION.DISTANCE_TO_STAR) + " Ls"));
lblAge.setText("Data age:\t " + trade.getUpdateAge());

btnPrevStation.setDisable(!hasPrev);
btnNextStation.setDisable(!hasNext);
Expand Down
59 changes: 48 additions & 11 deletions src/main/java/com/fi0x/edct/logic/cleanup/HTMLCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,59 @@
import org.jsoup.select.Elements;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Objects;

public class HTMLCleanup
{
@Nullable
public static Element getStationDetails(String inputHTML)
{
Elements mainconten1s = getStationMaincontent1(inputHTML);
if(mainconten1s == null || mainconten1s.size() == 0) return null;

Elements mainblocks = Objects.requireNonNull(mainconten1s.first()).getElementsByClass("mainblock");
if(mainblocks.size() == 0) return null;

for(Element block : mainblocks)
{
String blockText = block.toString().toLowerCase();
if(blockText.contains("class=\"mainblock\"") && (blockText.contains("landing pad") || blockText.contains("station type") || blockText.contains("href=\"/station/")))
return block;
}
return null;
}

@Nullable
public static ArrayList<Element> getStationTrades(String inputHTML)
{
ArrayList<Element> stationTrades = new ArrayList<>();

Elements mainconten1s = getStationMaincontent1(inputHTML);
if(mainconten1s == null || mainconten1s.size() == 0) return null;

Elements mainblocks = Objects.requireNonNull(mainconten1s.first()).getElementsByClass("mainblock maintable");
if(mainblocks.size() == 0) return null;

Elements tables = Objects.requireNonNull(mainblocks.first()).getElementsByTag("table");
if(tables.size() == 0) return null;

Elements bodies = Objects.requireNonNull(tables.first()).getElementsByTag("tbody");
if(bodies.size() == 0) return null;

Elements rows = Objects.requireNonNull(bodies.first()).getElementsByTag("tr");

for(Element row : rows)
{
if(row.hasClass("subheader")) continue;
stationTrades.add(row);
}

return stationTrades;
}

@Nullable
private static Elements getStationMaincontent1(String inputHTML)
{
Document doc = Jsoup.parse(inputHTML);

Expand All @@ -27,16 +74,6 @@ public static Element getStationDetails(String inputHTML)
Elements mainconten1s = Objects.requireNonNull(maincontentcontainers.first()).getElementsByClass("maincontent1");
if(mainconten1s.size() == 0) return null;

Elements mainblocks = Objects.requireNonNull(mainconten1s.first()).getElementsByClass("mainblock");
if(mainblocks.size() == 0) return null;

for(Element block : mainblocks)
{
String blockText = block.toString().toLowerCase();
if(blockText.contains("class=\"mainblock\"") && (blockText.contains("landing pad") || blockText.contains("station type") || blockText.contains("href=\"/station/")))
return block;

}
return null;
return mainconten1s;
}
}
39 changes: 35 additions & 4 deletions src/main/java/com/fi0x/edct/logic/cleanup/INARACleanup.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.fi0x.edct.logic.cleanup;

import com.fi0x.edct.logic.structures.PADSIZE;
import com.fi0x.edct.logic.structures.STATION;
import com.fi0x.edct.logic.structures.STATIONTYPE;
import com.fi0x.edct.logic.structures.TRADE;
import com.fi0x.edct.logic.database.DBHandler;
import com.fi0x.edct.logic.structures.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand Down Expand Up @@ -135,4 +133,37 @@ public static String getStationID(String inputHTML, String stationName, String s
}
return stationID;
}

public static ArrayList<TRADE> getCommodityTradesForStation(String inputHTML, String systemName, String stationName)
{
ArrayList<TRADE> results = new ArrayList<>();

ArrayList<Element> stationCommodities = HTMLCleanup.getStationTrades(inputHTML);
if(stationCommodities == null) return results;

long currentMillis = System.currentTimeMillis();
for(Element commodity : stationCommodities)
{
Elements cols = commodity.getElementsByTag("td");
int inaraID = getIntFromString(Objects.requireNonNull(cols.get(0).getElementsByTag("a").first()).attr("href"));
int importPrice = getIntFromString(Objects.requireNonNull(cols.get(1).getElementsByTag("span").first()).ownText());
int exportPrice = getIntFromString(Objects.requireNonNull(cols.get(2).getElementsByTag("span").first()).ownText());
int demand = getIntFromString(cols.get(3).ownText());
int supply = getIntFromString(cols.get(4).ownText());

STATION s = DBHandler.getStation(systemName, stationName);
results.add(new TRADE(s, inaraID, currentMillis, supply, demand, importPrice, exportPrice));
}

return results;
}

private static int getIntFromString(String input)
{
String cleanString = input.replace("commodity", "").replace("/", "");
cleanString = cleanString.replace("-", "0").replace(",", "");

if(cleanString.equals("")) return 0;
return Integer.parseInt(cleanString);
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/fi0x/edct/logic/structures/TRADE.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class TRADE
{
public final STATION STATION;
public final int INARA_ID;
public final long AGE;
public final long SUPPLY;
public final long DEMAND;
public final long BUY_PRICE;
public final long SELL_PRICE;
public long AGE;
public long SUPPLY;
public long DEMAND;
public long BUY_PRICE;
public long SELL_PRICE;

public TRADE(STATION station, int inaraID, long age, long supply, long demand, long buyPrice, long sellPrice)
{
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/fi0x/edct/logic/websites/InaraStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.fi0x.edct.logging.exceptions.HtmlConnectionException;
import com.fi0x.edct.logic.cleanup.INARACleanup;
import com.fi0x.edct.logic.database.DBHandler;
import com.fi0x.edct.logic.structures.ENDPOINTS;
import com.fi0x.edct.logic.structures.TRADE;
import com.fi0x.edct.logic.webrequests.RequestHandler;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -30,6 +33,36 @@ public static String getStationHtml(String stationID) throws InterruptedExceptio
return RequestHandler.sendHTTPRequest(ENDPOINTS.StationSearch.url + stationID, ENDPOINTS.StationSearch.type, parameters);
}

public static void updateSingleStationTrades(String stationName, String systemName, TRADE tradeToUpdate)
{
String stationHTML = null;
try
{
String inaraID = getInaraStationID(stationName, systemName);
stationHTML = getStationHtml(inaraID);
} catch(InterruptedException | HtmlConnectionException ignored)
{
}

if(stationHTML != null)
{
ArrayList<TRADE> trades = INARACleanup.getCommodityTradesForStation(stationHTML, systemName, stationName);
for(TRADE t : trades)
{
DBHandler.setTradeData(t);
if(t.INARA_ID == tradeToUpdate.INARA_ID)
{
tradeToUpdate.AGE = t.AGE;
tradeToUpdate.SUPPLY = t.SUPPLY;
tradeToUpdate.DEMAND = t.DEMAND;
tradeToUpdate.BUY_PRICE = t.BUY_PRICE;
tradeToUpdate.SELL_PRICE = t.SELL_PRICE;
}
}

}
}

private static Map<String, String> getRefinedParameters(String[] parameter, String stationName)
{
Map<String, String> parameters = new HashMap<>();
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/css/station.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#topic {
-fx-font-weight: bold;
}
#btnReloadStation {
-fx-padding: 0;
-fx-min-height: 0;
-fx-min-width: 0;
-fx-max-height: 25;
-fx-max-width: 25;
-fx-text-fill: #000000;
-fx-background-color: transparent;
}
#btnReloadStation:hover {
-fx-background-color: rgba(0, 0, 0, 0.2);
}
#btnBlacklist {
-fx-padding: 0;
-fx-min-height: 0;
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/fxml/station.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
<ColumnConstraints hgrow="ALWAYS"/>
<ColumnConstraints/>
<ColumnConstraints/>
<ColumnConstraints/>
</columnConstraints>
<Label id="topic" fx:id="lblAction" text="Buy at" GridPane.columnIndex="0"/>
<Button id="btnBlacklist" fx:id="btnBlacklist" text="Blacklist" onAction="#addToBlacklist" GridPane.columnIndex="2">
<Button id="btnReloadStation" fx:id="btnReloadStation" text="Update" onAction="#reloadStation" GridPane.columnIndex="2">
<tooltip>
<Tooltip text="Reload the trade information for this station"/>
</tooltip>
</Button>
<Button id="btnBlacklist" fx:id="btnBlacklist" text="Blacklist" onAction="#addToBlacklist" GridPane.columnIndex="3">
<tooltip>
<Tooltip text="Add the system name of this station to the blacklist"/>
</tooltip>
</Button>
<Button id="btnRemove" text="X" onAction="#removeStation" GridPane.columnIndex="3">
<Button id="btnRemove" text="X" onAction="#removeStation" GridPane.columnIndex="4">
<tooltip>
<Tooltip text="Remove this station temporarily from the local database"/>
</tooltip>
Expand Down
Binary file added src/main/resources/images/reload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d19c9ae

Please sign in to comment.