ge plugin: submit trades even when not logged in

This commit is contained in:
Adam
2020-05-29 13:45:37 -04:00
parent 2622cc2ac6
commit 6d46bb09c4
3 changed files with 61 additions and 27 deletions

View File

@@ -43,6 +43,7 @@ public class RuneLiteAPI
private static final Logger logger = LoggerFactory.getLogger(RuneLiteAPI.class);
public static final String RUNELITE_AUTH = "RUNELITE-AUTH";
public static final String RUNELITE_MACHINEID = "RUNELITE-MACHINEID";
public static final OkHttpClient CLIENT;
public static final Gson GSON = new Gson();

View File

@@ -27,7 +27,7 @@ package net.runelite.http.api.ge;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI;
import static net.runelite.http.api.RuneLiteAPI.JSON;
@@ -39,12 +39,14 @@ import okhttp3.RequestBody;
import okhttp3.Response;
@Slf4j
@AllArgsConstructor
public class GrandExchangeClient
{
private static final Gson GSON = RuneLiteAPI.GSON;
private final UUID uuid;
@Setter
private UUID uuid;
@Setter
private String machineId;
public void submit(GrandExchangeTrade grandExchangeTrade)
{
@@ -52,8 +54,17 @@ public class GrandExchangeClient
.addPathSegment("ge")
.build();
Request request = new Request.Builder()
.header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString())
Request.Builder builder = new Request.Builder();
if (uuid != null)
{
builder.header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString());
}
if (machineId != null)
{
builder.header(RuneLiteAPI.RUNELITE_MACHINEID, machineId);
}
Request request = builder
.post(RequestBody.create(JSON, GSON.toJson(grandExchangeTrade)))
.url(url)
.build();

View File

@@ -29,17 +29,21 @@
package net.runelite.client.plugins.grandexchange;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.primitives.Shorts;
import com.google.gson.Gson;
import com.google.inject.Provides;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.NetworkInterface;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ScheduledExecutorService;
@@ -80,7 +84,6 @@ import net.runelite.client.account.SessionManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.events.SessionClose;
import net.runelite.client.events.SessionOpen;
import net.runelite.client.game.ItemManager;
import net.runelite.client.input.KeyManager;
@@ -91,6 +94,7 @@ import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.ColorUtil;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.OSType;
import net.runelite.client.util.QuantityFormatter;
import net.runelite.client.util.Text;
import net.runelite.http.api.ge.GrandExchangeClient;
@@ -176,10 +180,42 @@ public class GrandExchangePlugin extends Plugin
private int osbItem;
private OSBGrandExchangeResult osbGrandExchangeResult;
@Inject
private GrandExchangeClient grandExchangeClient;
private static String machineUuid;
private boolean wasFuzzySearch;
static
{
try
{
Hasher hasher = Hashing.sha256().newHasher();
Runtime runtime = Runtime.getRuntime();
hasher.putByte((byte) OSType.getOSType().ordinal());
hasher.putByte((byte) runtime.availableProcessors());
hasher.putUnencodedChars(System.getProperty("os.arch", ""));
hasher.putUnencodedChars(System.getProperty("os.version", ""));
hasher.putUnencodedChars(System.getProperty("user.name", ""));
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = networkInterfaces.nextElement();
byte[] hardwareAddress = networkInterface.getHardwareAddress();
if (hardwareAddress != null)
{
hasher.putBytes(hardwareAddress);
}
}
machineUuid = hasher.hash().toString();
}
catch (Exception ex)
{
log.warn("unable to generate machine id", ex);
}
}
/**
* Logic from {@link org.apache.commons.text.similarity.FuzzyScore}
*/
@@ -274,8 +310,13 @@ public class GrandExchangePlugin extends Plugin
AccountSession accountSession = sessionManager.getAccountSession();
if (accountSession != null)
{
grandExchangeClient = new GrandExchangeClient(accountSession.getUuid());
grandExchangeClient.setUuid(accountSession.getUuid());
}
else
{
grandExchangeClient.setUuid(null);
}
grandExchangeClient.setMachineId(machineUuid);
osbItem = -1;
osbGrandExchangeResult = null;
@@ -289,27 +330,13 @@ public class GrandExchangePlugin extends Plugin
keyManager.unregisterKeyListener(inputListener);
grandExchangeText = null;
grandExchangeItem = null;
grandExchangeClient = null;
}
@Subscribe
public void onSessionOpen(SessionOpen sessionOpen)
{
AccountSession accountSession = sessionManager.getAccountSession();
if (accountSession.getUuid() != null)
{
grandExchangeClient = new GrandExchangeClient(accountSession.getUuid());
}
else
{
grandExchangeClient = null;
}
}
@Subscribe
public void onSessionClose(SessionClose sessionClose)
{
grandExchangeClient = null;
grandExchangeClient.setUuid(accountSession.getUuid());
}
@Subscribe
@@ -351,11 +378,6 @@ public class GrandExchangePlugin extends Plugin
private void submitTrades(int slot, GrandExchangeOffer offer)
{
if (grandExchangeClient == null)
{
return;
}
if (offer.getState() != GrandExchangeOfferState.BOUGHT && offer.getState() != GrandExchangeOfferState.SOLD &&
offer.getState() != GrandExchangeOfferState.CANCELLED_BUY && offer.getState() != GrandExchangeOfferState.CANCELLED_SELL)
{