Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flippingcopilot.controller;

import com.flippingcopilot.model.SuggestionManager;
import com.flippingcopilot.ui.UIUtilities;
import com.flippingcopilot.util.ProfitCalculator;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,11 +23,13 @@
public class TooltipController {
private static final int SCRIPT_TOOLTIP_GE = 526;
private static final int TOOLTIP_HEIGHT_WITH_PROFIT = 45;
private static final int TOOLTIP_LINE_HEIGHT = 13;

private static final int WIDTH_PADDING = 4;

private final Client client;
private final ProfitCalculator profitCalculator;
private final SuggestionManager suggestionManager;

public void tooltip(ScriptPostFired e) {
if(e.getScriptId() != SCRIPT_TOOLTIP_GE) {
Expand Down Expand Up @@ -58,8 +61,17 @@ public void tooltip(ScriptPostFired e) {

if(name != null) {
long profit = profitCalculator.getProfitByItemName(name);
text.setText(text.getText() + "<br>Profit: " + UIUtilities.quantityToRSDecimalStack(profit, false) + " gp");
tooltip.setOriginalHeight(TOOLTIP_HEIGHT_WITH_PROFIT);
String updatedText = text.getText() + "<br>Profit: " + UIUtilities.quantityToRSDecimalStack(profit, false) + " gp";
int height = TOOLTIP_HEIGHT_WITH_PROFIT;

Double expectedSeconds = suggestionManager.getExpectedDurationForItem(name);
if (expectedSeconds != null) {
updatedText += "<br>Est. time: " + UIUtilities.formatSuggestionDuration(expectedSeconds);
height += TOOLTIP_LINE_HEIGHT;
}

text.setText(updatedText);
tooltip.setOriginalHeight(height);

int width = calculateTooltipWidth(text.getFont(), text.getText());
tooltip.setOriginalWidth(width);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/flippingcopilot/model/SuggestionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import javax.inject.Singleton;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Singleton
@Getter
Expand All @@ -28,13 +30,24 @@ public class SuggestionManager {
private int suggestionItemIdOnOfferSubmitted = -1;
private OfferStatus suggestionOfferStatusOnOfferSubmitted = null;

// Most recent expected duration (seconds) Copilot predicted for each item, captured from each
// suggestion as it arrives. expectedDuration only lives on the live suggestion, so caching it by
// item lets the GE offer tooltip show an expected time for any item that has been suggested.
private final Map<String, Double> itemNameToExpectedDuration = new ConcurrentHashMap<>();


public volatile int suggestionsDelayedUntil = 0;

public void setSuggestion(Suggestion suggestion) {
this.suggestion = suggestion;
suggestionReceivedAt = Instant.now();
if (suggestion != null && suggestion.getExpectedDuration() != null && suggestion.getName() != null) {
itemNameToExpectedDuration.put(suggestion.getName(), suggestion.getExpectedDuration());
}
}

public Double getExpectedDurationForItem(String itemName) {
return itemNameToExpectedDuration.get(itemName);
}

public void setSuggestionError(HttpResponseException error) {
Expand All @@ -52,6 +65,7 @@ public void reset() {
suggestionError = null;
suggestionItemIdOnOfferSubmitted = -1;
suggestionOfferStatusOnOfferSubmitted = null;
itemNameToExpectedDuration.clear();
}

public boolean suggestionOutOfDate() {
Expand Down