diff --git a/auto-cream-api.iml b/auto-cream-api.iml index f79f6a4..e634122 100644 --- a/auto-cream-api.iml +++ b/auto-cream-api.iml @@ -34,5 +34,6 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5b15b08..956008e 100644 --- a/pom.xml +++ b/pom.xml @@ -131,5 +131,10 @@ unirest-java 3.1.02 + + me.xdrop + fuzzywuzzy + 1.3.1 + \ No newline at end of file diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index 6e92a30..f3d69b8 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -13,16 +13,16 @@ * . */ -import com.jfoenix.controls.*; import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.fxml.FXML; -import javafx.scene.control.Alert; -import javafx.scene.control.Label; -import javafx.scene.control.Tooltip; -import javafx.stage.FileChooser; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.stage.*; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.configuration2.ex.ConfigurationException; import org.jsoup.HttpStatusException; @@ -38,6 +38,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -64,31 +65,31 @@ public class Controller { @FXML public Label state_label; @FXML - public JFXTextField path_textfield; + public TextField path_textfield; @FXML - public JFXTextField appId_textfield; + public TextField appId_textfield; @FXML - public JFXTextField game_name_textfield; + public TextField game_name_textfield; @FXML - public JFXComboBox language_combobox; + public ComboBox language_combobox; @FXML - public JFXTextArea dlc_textarea; + public TextArea dlc_textarea; @FXML - public JFXCheckBox extra_protection_checkbox; + public CheckBox extra_protection_checkbox; @FXML - public JFXCheckBox offline_checkbox; + public CheckBox offline_checkbox; @FXML - public JFXCheckBox unlock_all_checkbox; + public CheckBox unlock_all_checkbox; @FXML - public JFXButton reset_button; + public Button reset_button; @FXML - public JFXButton save_button; + public Button save_button; @FXML - public JFXButton getAppId_button; + public Button getAppId_button; @FXML - public JFXButton path_button; + public Button path_button; @FXML - public JFXButton retrieveDlcList_button; + public Button retrieveDlcList_button; private String steamApiPathString; public Controller() { @@ -207,11 +208,33 @@ public class Controller { } public void getAppId() { - final App game = cache.findGame(game_name_textfield.getText()); - if (game == null) { + //final App game = cache.findGame(game_name_textfield.getText()); + final List games = cache.findListOfGames(game_name_textfield.getText()); + if (games.isEmpty()) { appId_textfield.setText("-1"); - } else { + } else if (games.size() == 1) { + App game = games.get(0); + game_name_textfield.setText(game.getName()); appId_textfield.setText(String.valueOf(game.getAppId())); + } else { + try { + FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("searchResultWindow.fxml")); + Parent root1 = fxmlLoader.load(); + + SearchResultWindowController c = fxmlLoader.getController(); + c.initMe(games); + c.setParentController(this); + + Stage stage = new Stage(); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setResizable(false); + //stage.initStyle(StageStyle.UNDECORATED); + stage.setTitle("Choose game..."); + stage.setScene(new Scene(root1)); + stage.show(); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/SearchResultWindowController.java b/src/main/java/SearchResultWindowController.java new file mode 100644 index 0000000..ded05bd --- /dev/null +++ b/src/main/java/SearchResultWindowController.java @@ -0,0 +1,96 @@ +/* + * Auto-CreamAPI + * Copyright (C) 2020 Jeddunk + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see + * . + */ + +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.scene.control.cell.TreeItemPropertyValueFactory; +import javafx.scene.input.MouseButton; +import javafx.scene.input.MouseEvent; +import javafx.stage.Stage; +import pojo.App; + +import java.util.List; + +public class SearchResultWindowController { + + @FXML + public Button okButton; + @FXML + public Button cancelButton; + @FXML + public TreeTableView gameTable; + @FXML + public TreeTableColumn appIdCol; + @FXML + public TreeTableColumn nameCol; + + public Controller parent; + + private long lastTime; + private boolean isDblClicked; + + public SearchResultWindowController() { + } + + @FXML + public void initialize() { + } + + public void initMe(List games) { + TreeItem root = new TreeItem<>(); + appIdCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("appId")); + nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("name")); + games.forEach(game -> root.getChildren().add(new TreeItem<>(game))); + gameTable.setRoot(root); + } + + public void confirm() { + App app = gameTable.getSelectionModel().getSelectedItem().getValue(); + parent.game_name_textfield.setText(app.getName()); + parent.appId_textfield.setText(String.valueOf(app.getAppId())); + Stage current = (Stage) okButton.getScene().getWindow(); + current.close(); + } + + public void cancel() { + parent.game_name_textfield.setText(""); + parent.appId_textfield.setText("-1"); + Stage current = (Stage) cancelButton.getScene().getWindow(); + current.close(); + } + + public void setParentController(Controller controller) { + parent = controller; + } + + public void doubleclickConfirm(MouseEvent mouseEvent) { + if (mouseEvent.getButton() == MouseButton.PRIMARY) { + long diff; + long currentTime = System.currentTimeMillis(); + if (lastTime != 0 && currentTime != 0) { + diff = currentTime - lastTime; + isDblClicked = (diff <= 215); + } + lastTime = currentTime; + if (isDblClicked) { + App app = gameTable.getSelectionModel().getSelectedItem().getValue(); + parent.game_name_textfield.setText(app.getName()); + parent.appId_textfield.setText(String.valueOf(app.getAppId())); + Stage current = (Stage) gameTable.getScene().getWindow(); + current.close(); + } + } + } +} diff --git a/src/main/java/util/SteamAppsListCache.java b/src/main/java/util/SteamAppsListCache.java index eae4bc8..591c5bb 100644 --- a/src/main/java/util/SteamAppsListCache.java +++ b/src/main/java/util/SteamAppsListCache.java @@ -20,6 +20,8 @@ import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import kong.unirest.HttpResponse; import kong.unirest.Unirest; +import me.xdrop.fuzzywuzzy.FuzzySearch; +import me.xdrop.fuzzywuzzy.model.BoundExtractedResult; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -32,10 +34,7 @@ import java.io.*; import java.lang.reflect.Type; import java.time.Duration; import java.time.Instant; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; public class SteamAppsListCache { @@ -93,20 +92,19 @@ public class SteamAppsListCache { .filter(app -> app.getName().equalsIgnoreCase(name)).findFirst().orElse(null); } - public App findGame(String name) { - for (App app : list.getSteamAppsList()) { - if (app.getName().toLowerCase().replaceAll("[^a-zA-Z0-9\\s+]", "") - .startsWith(name.toLowerCase().replaceAll("[^a-zA-Z0-9\\s+]", ""))) { - return app; + public List findListOfGames(String name) { + List> match = FuzzySearch.extractSorted(name, list.getSteamAppsList(), app -> + app.getName().toLowerCase().replaceAll("[^a-zA-Z0-9\\s+]", ""), 80); + List collect = match.stream().map(BoundExtractedResult::getReferent).collect(Collectors.toList()); + for (App app : collect) { + if (app.getName().replaceAll("[^a-zA-Z0-9\\s+]", "") + .equalsIgnoreCase(name.replaceAll("[^a-zA-Z0-9\\s+]", ""))) { + collect = new ArrayList<>(); + collect.add(app); + break; } } - for (App app : list.getSteamAppsList()) { - if (app.getName().toLowerCase().replaceAll("[^a-zA-Z0-9\\s+]", "") - .contains(name.toLowerCase().replaceAll("[^a-zA-Z0-9\\s+]", ""))) { - return app; - } - } - return null; + return collect; } private void getListFromApi() { diff --git a/src/main/resources/mainWindow.fxml b/src/main/resources/mainWindow.fxml index e4c0317..ebb2d7c 100644 --- a/src/main/resources/mainWindow.fxml +++ b/src/main/resources/mainWindow.fxml @@ -27,46 +27,68 @@ ~ . --> - + + - - - + + + - - - - - - - - - + + + + + + + + + - + - - - - - - - - + + - + - - - - - - - - - - diff --git a/src/main/resources/searchResultWindow.fxml b/src/main/resources/searchResultWindow.fxml new file mode 100644 index 0000000..600c0fd --- /dev/null +++ b/src/main/resources/searchResultWindow.fxml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/stylesheet.css b/src/main/resources/stylesheet.css new file mode 100644 index 0000000..c84e7e4 --- /dev/null +++ b/src/main/resources/stylesheet.css @@ -0,0 +1,18 @@ +/* + * Auto-CreamAPI + * Copyright (C) 2020 Jeddunk + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see + * . + */ + +JFXButton { + -fx-background-color: #ddd; +}