Skip to content

Commit

Permalink
Add test version of token crafter
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Sep 25, 2020
1 parent 5f75f3c commit dd8d2d4
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 21 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/stirante/runechanger/client/Loot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import generated.LolCollectionsCollectionsChampionMastery;
import generated.LolLootPlayerLoot;
import generated.LolLootRecipe;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -134,11 +135,11 @@ else if (mastery.get().championLevel == 6) {
}

/**
* Returns a map of loot Ids and their count
* Returns a map of loot Ids and pair of their name and count
*
* @return map of loot Id -> token count
* @return map of loot Id -> pair of loot name and count
*/
public Map<String, Integer> getEventTokens() {
public Map<String, Pair<String, Integer>> getEventTokens() {
try {
ApiResponse<LolLootPlayerLoot[]> loot =
getApi().executeGet("/lol-loot/v1/player-loot/", LolLootPlayerLoot[].class);
Expand All @@ -148,7 +149,7 @@ public Map<String, Integer> getEventTokens() {
!IGNORED_MATERIALS.contains(lolLootPlayerLoot.lootId))
.collect(Collectors.toMap(
lolLootPlayerLoot -> lolLootPlayerLoot.lootId,
lolLootPlayerLoot -> lolLootPlayerLoot.count)
lolLootPlayerLoot -> new Pair<>(lolLootPlayerLoot.localizedName, lolLootPlayerLoot.count))
);
} catch (IOException e) {
log.error("Exception occurred while getting map of event tokens", e);
Expand All @@ -157,24 +158,22 @@ public Map<String, Integer> getEventTokens() {
}

/**
* Returns a map of token recipe names and their costs
* Returns a map of token recipe names and pair of their name and cost
*
* @param lootId loot id
* @return map of token recipe name -> recipe cost
* @return map of token recipe name -> pair of recipe name and cost
*/
public Map<String, Integer> getRecipes(String lootId) {
public Map<String, Pair<String, Integer>> getRecipes(String lootId) {
try {
ApiResponse<LolLootRecipe[]> loot =
getApi().executeGet("/lol-loot/v1/recipes/initial-item/" + lootId, LolLootRecipe[].class);
return Arrays.stream(loot.getResponseObject())
.filter(lolLootRecipe -> lolLootRecipe.slots.stream()
.anyMatch(lolLootRecipeSlot -> lolLootRecipeSlot.lootIds.contains(lootId)))
.filter(lolLootRecipe -> lolLootRecipe.slots.size() == 1 &&
lolLootRecipe.slots.get(0).lootIds.size() == 1 &&
lolLootRecipe.slots.get(0).lootIds.get(0).equalsIgnoreCase(lootId))
.collect(Collectors.toMap(
lolLootPlayerLoot -> lolLootPlayerLoot.recipeName,
lolLootPlayerLoot -> lolLootPlayerLoot.slots.stream()
.filter(lolLootRecipeSlot -> lolLootRecipeSlot.lootIds.contains(lootId))
.findFirst()
.orElseThrow().quantity)
lolLootPlayerLoot -> new Pair<>(lolLootPlayerLoot.description, lolLootPlayerLoot.slots.get(0).quantity))
);
} catch (IOException e) {
log.error("Exception occurred while getting map of recipes", e);
Expand All @@ -184,9 +183,10 @@ public Map<String, Integer> getRecipes(String lootId) {

/**
* Crafts a recipe
*
* @param recipeName recipe name
* @param lootId material Id to be consumed
* @param repeat how many times it should be crafted
* @param lootId material Id to be consumed
* @param repeat how many times it should be crafted
* @return whether the crafting was successful
*/
public boolean craftRecipe(String recipeName, String lootId, int repeat) {
Expand Down
126 changes: 120 additions & 6 deletions src/main/java/com/stirante/runechanger/gui/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
import com.stirante.runechanger.client.ClientEventListener;
import com.stirante.runechanger.gui.controllers.*;
import com.stirante.runechanger.model.client.Champion;
import com.stirante.runechanger.model.client.GameData;
import com.stirante.runechanger.model.client.RunePage;
import com.stirante.runechanger.sourcestore.SourceStore;
import com.stirante.runechanger.util.*;
import generated.LolGameflowGameflowPhase;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.NodeOrientation;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Pair;
import javafx.util.StringConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -228,7 +234,8 @@ public void onPostExecute(Void result) {
PerformanceMonitor.start();
RuneChanger.getInstance().getGuiHandler().showInfoMessage("Performance monitor started");
}
} else if (event.isControlDown() && event.getCode() == KeyCode.L) {
}
else if (event.isControlDown() && event.getCode() == KeyCode.L) {
ProgressDialogController progressDialog = new ProgressDialogController();
progressDialog.setTitle("Debugging LoL client connection");
progressDialog.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
Expand All @@ -248,7 +255,9 @@ public void onPostExecute(String result) {
pw.flush();
} catch (IOException e) {
log.error("Exception occurred while saving logs", e);
RuneChanger.getInstance().getGuiHandler().showErrorMessage("Error, while saving log: " + e.getMessage());
RuneChanger.getInstance()
.getGuiHandler()
.showErrorMessage("Error, while saving log: " + e.getMessage());
}
progressDialog.close();
try {
Expand All @@ -259,6 +268,111 @@ public void onPostExecute(String result) {
}
}.execute();
}
else if (event.isControlDown() && event.getCode() == KeyCode.T) {
// I'm sorry, this is very experimental and POC and ugly
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Crafting (suuuper test version)");
dialog.setHeaderText(null);
dialog.setWidth(500);
ButtonType loginButtonType = new ButtonType("Craft", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);

GridPane grid = new GridPane();
grid.setMinWidth(500);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(10, 10, 10, 10));

ChoiceBox<Map.Entry<String, Pair<String, Integer>>> tokens = new ChoiceBox<>();
tokens.setConverter(new StringConverter<>() {
@Override
public String toString(Map.Entry<String, Pair<String, Integer>> object) {
return object.getValue().getKey();
}

@Override
public Map.Entry<String, Pair<String, Integer>> fromString(String string) {
return tokens.getItems()
.stream()
.filter(stringPairEntry -> stringPairEntry.getValue().getKey().equals(string))
.findFirst()
.orElse(null);
}
});
ChoiceBox<Map.Entry<String, Pair<String, Integer>>> recipes = new ChoiceBox<>();
recipes.setDisable(true);
recipes.setConverter(new StringConverter<>() {
@Override
public String toString(Map.Entry<String, Pair<String, Integer>> object) {
return object.getValue().getKey();
}

@Override
public Map.Entry<String, Pair<String, Integer>> fromString(String string) {
return recipes.getItems()
.stream()
.filter(stringPairEntry -> stringPairEntry.getValue().getKey().equals(string))
.findFirst()
.orElse(null);
}
});
tokens.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
recipes.setDisable(newValue == null);
if (newValue != null) {
recipes.setItems(FXCollections.observableList(new ArrayList<>(RuneChanger.getInstance()
.getLootModule()
.getRecipes(newValue.getKey())
.entrySet())));
}
else {
recipes.getItems().clear();
}
});
tokens.setItems(FXCollections.observableList(new ArrayList<>(RuneChanger.getInstance()
.getLootModule()
.getEventTokens()
.entrySet())));

grid.add(new Label("Event token:"), 0, 0);
grid.add(tokens, 1, 0);
grid.add(new Label("Recipe:"), 0, 1);
grid.add(recipes, 1, 1);

Node craftButton = dialog.getDialogPane().lookupButton(loginButtonType);
craftButton.setDisable(true);

recipes.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> craftButton.setDisable(newValue == null));

dialog.getDialogPane().setContent(grid);

Platform.runLater(tokens::requestFocus);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(tokens.getSelectionModel()
.getSelectedItem()
.getKey(), recipes.getSelectionModel().getSelectedItem().getKey());
}
return null;
});

Optional<Pair<String, String>> result = dialog.showAndWait();

result.ifPresent(tokenRecipePair -> {
Map.Entry<String, Pair<String, Integer>> token =
tokens.getSelectionModel().selectedItemProperty().get();
Map.Entry<String, Pair<String, Integer>> recipe =
recipes.getSelectionModel().selectedItemProperty().get();
int repeat = token.getValue().getValue() / recipe.getValue().getValue();
if (repeat == 0) {
runeChanger.getGuiHandler().showWarningMessage("You don't have enough tokens!");
} else {
runeChanger.getGuiHandler().showInfoMessage("Trying to craft the recipe " + repeat + " times");
runeChanger.getLootModule().craftRecipe(recipe.getKey(), token.getKey(), repeat);
}
});
}
});

Platform.setImplicitExit(false);
Expand Down Expand Up @@ -406,7 +520,7 @@ public void onPostExecute(List<RunePage> result) {
runeChanger.getRunesModule().getOwnedPageCount());
// Split list into runepages, that are both in runebook and in client and those, that are only in runebook
@SuppressWarnings("unchecked") Map<Boolean, List<RunePage>> results =
((List<RunePage>)SimplePreferences.getRuneBookValues().clone())
((List<RunePage>) SimplePreferences.getRuneBookValues().clone())
.stream()
.collect(Collectors.partitioningBy(runePage -> result
.stream().noneMatch(runePage1 -> runePage1.equals(runePage))));
Expand Down

0 comments on commit dd8d2d4

Please sign in to comment.