Skip to content

Commit

Permalink
Improved application state saving
Browse files Browse the repository at this point in the history
Added champion list saving
Added suggestions generation mechanism
  • Loading branch information
st4s1k committed Apr 3, 2022
1 parent a30f864 commit af90b3f
Show file tree
Hide file tree
Showing 36 changed files with 2,245 additions and 539 deletions.
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<javafx.plugin.version>0.0.8</javafx.plugin.version>
<sortpom.plugin.version>3.0.1</sortpom.plugin.version>
<log4j.version>2.17.2</log4j.version>
<orianna.version>4.0.0-rc8</orianna.version>
<lol-client-java-api.version>1.2.5</lol-client-java-api.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -56,6 +58,17 @@
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Riot API -->
<dependency>
<groupId>com.github.stirante</groupId>
<artifactId>lol-client-java-api</artifactId>
<version>${lol-client-java-api.version}</version>
</dependency>
<dependency>
<groupId>com.merakianalytics.orianna</groupId>
<artifactId>orianna</artifactId>
<version>${orianna.version}</version>
</dependency>
<!-- Misc -->
<dependency>
<groupId>org.controlsfx</groupId>
Expand All @@ -74,9 +87,9 @@
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.github.stirante</groupId>
<artifactId>lol-client-java-api</artifactId>
<version>1.2.3</version>
<groupId>dev.failsafe</groupId>
<artifactId>failsafe</artifactId>
<version>3.2.3</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.st4s1k.leagueteamcomp;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.merakianalytics.orianna.Orianna;
import com.st4s1k.leagueteamcomp.controller.LTCExceptionController;
import com.st4s1k.leagueteamcomp.controller.LeagueTeamCompController;
import com.st4s1k.leagueteamcomp.exceptions.LTCException;
import com.st4s1k.leagueteamcomp.model.champion.ChampionsDTO;
import com.st4s1k.leagueteamcomp.model.champion.ChampionDTO;
import com.st4s1k.leagueteamcomp.repository.ChampionRepository;
import com.st4s1k.leagueteamcomp.utils.ResizeHelper;
import com.st4s1k.leagueteamcomp.utils.Resources;
import com.st4s1k.leagueteamcomp.utils.Utils;
import javafx.application.Application;
import javafx.application.Platform;
Expand All @@ -25,14 +27,15 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.text.MessageFormat;
import java.util.Map;
import java.util.stream.DoubleStream;

import static com.st4s1k.leagueteamcomp.utils.Resources.*;
import static com.merakianalytics.orianna.types.common.Region.EUROPE_WEST;
import static java.net.http.HttpResponse.BodyHandlers;
import static java.util.Objects.requireNonNull;

Expand All @@ -46,9 +49,11 @@ public static void main(String[] args) {
@Override
@SneakyThrows
public void start(Stage stage) {
Orianna.setRiotAPIKey(Resources.RIOT_API_KEY);
Orianna.setDefaultRegion(EUROPE_WEST);
Thread.setDefaultUncaughtExceptionHandler(LeagueTeamCompApplication::showError);
getChampionsFromUrl();
FXMLLoader loader = new FXMLLoader(getClass().getResource(FXML_FILE_PATH), LTC_VIEW_PROPERTIES);
FXMLLoader loader = new FXMLLoader(getClass().getResource(Resources.FXML_FILE_PATH), Resources.LTC_VIEW_PROPERTIES);
Parent root = loader.load();
LeagueTeamCompController controller = loader.getController();
controller.setStageAndSetupListeners(stage);
Expand All @@ -57,37 +62,41 @@ public void start(Stage stage) {
stage.close();
}));
controller.setMinimizeButtonAction(() -> stage.setIconified(true));
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
Scene scene = new Scene(root, Resources.WINDOW_WIDTH, Resources.WINDOW_HEIGHT);
stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.getIcons().add(new Image(requireNonNull(getClass().getResourceAsStream(ICON_FILE_PATH))));
stage.setTitle(WINDOW_TITLE);
stage.setResizable(WINDOW_IS_RESIZABLE);
stage.getIcons().add(new Image(requireNonNull(getClass().getResourceAsStream(Resources.ICON_FILE_PATH))));
stage.setTitle(Resources.WINDOW_TITLE);
stage.setResizable(Resources.WINDOW_IS_RESIZABLE);
stage.show();
}

@SneakyThrows
private void getChampionsFromUrl() {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(CHAMPIONS_URL))
.uri(new URI(Resources.CHAMPIONS_URL))
.build();
HttpClient.newHttpClient()
.sendAsync(request, BodyHandlers.ofString())
.thenApply(this::getChampions)
.thenAccept(ChampionRepository::init);
}

private ChampionsDTO getChampions(HttpResponse<String> response) {
String json = MessageFormat.format("'{'\"champions\":{0}'}'", response.body());
ChampionsDTO champions = new Gson().fromJson(json, ChampionsDTO.class);
champions.getChampions().values().forEach(champion ->
private Map<String, ChampionDTO> getChampions(HttpResponse<String> response) {
Map<String, ChampionDTO> champions = Resources.GSON.fromJson(response.body(), getChampionMapType());
champions.values().forEach(champion ->
Utils.setFieldValue(champion, "image", new Image(champion.getIconUrl(), true)));
return champions;
}

private Type getChampionMapType() {
return new TypeToken<Map<String, ChampionDTO>>() {
}.getType();
}

private static void showError(Thread t, Throwable e) {
log.error("***Default exception handler***");
log.error(e.getMessage(), e);
if (Platform.isFxApplicationThread()) {
showErrorDialog(e);
} else {
Expand Down Expand Up @@ -126,4 +135,9 @@ private static void showErrorDialog(Throwable e) {
ResizeHelper.addResizeListener(dialog, border);
dialog.show();
}

@Override
public void stop() {
Platform.exit();
}
}
Loading

0 comments on commit af90b3f

Please sign in to comment.