iutils: grand exchange instant buying now increments if unsuccessful

This commit is contained in:
illumineawake 2021-08-15 22:39:46 +10:00
parent 9d7d915441
commit 319d8a5392
4 changed files with 105 additions and 65 deletions

View File

@ -23,7 +23,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
version = "4.4.2"
version = "4.4.3"
project.extra["PluginName"] = "iUtils"
project.extra["PluginDescription"] = "Illumine - Utils required for plugins to function with added automation"

View File

@ -3,15 +3,20 @@ package net.runelite.client.plugins.iutils.ui;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.GrandExchangeOffer;
import net.runelite.api.GrandExchangeOfferState;
import net.runelite.api.ItemID;
import net.runelite.client.plugins.iutils.CalculationUtils;
import net.runelite.client.plugins.iutils.api.GrandExchangePrices;
import net.runelite.client.plugins.iutils.game.Game;
import net.runelite.client.plugins.iutils.game.ItemQuantity;
import net.runelite.client.plugins.iutils.walking.BankLocations;
import javax.inject.Inject;
import java.util.List;
// TODO: selling, several offers at once, custom prices, collect to inventory
@Slf4j
public class GrandExchange {
private final Game game;
public GrandExchange(Game game) {
@ -43,14 +48,14 @@ public class GrandExchange {
/**
* Buys items from GE, if prices are over 8000GP it will progressively buy, otherwise attempts to buy instantly
* */
*/
public void buy(int item, int quantity) {
if (GrandExchangePrices.get(item).high * quantity > 1000) {
if (!buyProgressively(item, quantity,1.2, 30, 20)) {
if (!buyProgressively(item, quantity, 1.2, 30, CalculationUtils.random(15, 25))) {
throw new AssertionError("Failed to buy GE item progressively");
}
} else {
buyInstantly(item, quantity);
} else if (!buyInstantly(item, quantity)) {
throw new AssertionError("Failed to buy GE item progressively");
}
}
@ -62,7 +67,7 @@ public class GrandExchange {
/**
* Buys list of items at GE with a high max price to buy quickly
* */
*/
public void buyInstantly(List<ItemQuantity> items) {
for (ItemQuantity item : items) {
buyInstantly(item.id, item.quantity);
@ -71,71 +76,89 @@ public class GrandExchange {
/**
* Buys items at GE with a high max price to buy quickly
* */
public void buyInstantly(int item, int quantity) {
if (!isOpen()) {
log.info("Opening Grand Exchange");
game.npcs().withName("Grand Exchange Clerk").nearest().interact("Exchange");
game.waitUntil(this::isOpen);
game.tick(2, 4);
}
*/
public boolean buyInstantly(int item, int quantity) {
var lastprice = -1;
if (game.inventory().withId(995).first() == null) {
throw new IllegalStateException("you'll need some coins to buy stuff");
}
log.info("Buying: {} quantity: {}", item, quantity);
var slot = freeSlot();
for (int attempts = 0; attempts < 30; attempts++) {
if (!isOpen()) {
log.info("Opening Grand Exchange");
game.npcs().withName("Grand Exchange Clerk").nearest().interact("Exchange");
game.waitUntil(this::isOpen);
game.tick(2, 4);
}
startBuyOffer(slot);
if (game.inventory().withId(995).first() == null) {
throw new IllegalStateException("you'll need some coins to buy stuff");
}
log.info("Buying: {} quantity: {}", item, quantity);
var slot = freeSlot();
game.chooseItem(item);
game.waitUntil(() -> currentBuyItem() == item);
startBuyOffer(slot);
if (quantity != currentQuantity()) { // todo: use +/- buttons
game.widget(465, 24, 7).interact(0);
game.chooseItem(item);
game.waitUntil(() -> currentBuyItem() == item);
if (quantity != currentQuantity()) {
game.widget(465, 24, 7).interact(0);
game.tick(2, 4);
game.chooseNumber(quantity);
game.tick(2, 4);
}
var price = Math.min(
(int) Math.ceil((10 + (attempts * 5)) * currentPrice()),
game.inventory().withId(995).first().quantity() / quantity
);
if (price == lastprice) {
log.info("Price is same as last attempt to buy item: {}. Checking bank for more GP", item);
if (bank().quantity(ItemID.COINS_995) > 0) {
bank().withdraw(ItemID.COINS_995, Integer.MAX_VALUE, false);
continue;
} else {
log.info("Not enough coins to buy item: {} from GE", item);
return false;
}
}
lastprice = price;
if (price != currentPrice()) {
game.widget(465, 24, 12).interact(0);
game.tick(2, 4);
game.chooseNumber(price);
game.tick(2, 4);
}
game.tick();
game.widget(465, 27).interact(0);
game.waitUntil(() -> game.grandExchangeOffer(slot) != null);
game.tick(2, 4);
game.chooseNumber(quantity);
game.tick(2, 4);
}
var price = Math.min(
(int) Math.ceil(10 * currentPrice()),
game.inventory().withId(995).first().quantity() / quantity
);
if (price != currentPrice()) {
game.widget(465, 24, 12).interact(0);
game.tick(2, 4);
game.chooseNumber(price);
game.tick(2, 4);
}
game.tick();
game.widget(465, 27).interact(0);
game.waitUntil(() -> game.grandExchangeOffer(slot) != null);
game.tick(2, 4);
var ticks = 0;
while (game.grandExchangeOffer(slot).getQuantitySold() != quantity && ticks++ < 10) {
game.tick(1, 4);
}
if (game.grandExchangeOffer(slot).getQuantitySold() == quantity) {
collectToBank();
return;
}
if (game.grandExchangeOffer(slot).getQuantitySold() != quantity) {
game.widget(465, 7, 2).interact(1);
game.tick(4);
collectToInv();
throw new IllegalStateException("timed out waiting for offer to complete: " + game.grandExchangeOffer(slot).getQuantitySold() + " / " + quantity);
var ticks = 0;
while (game.grandExchangeOffer(slot).getQuantitySold() != quantity && ticks++ < 10) {
game.tick(1, 4);
}
if (game.grandExchangeOffer(slot).getQuantitySold() == quantity) {
collectToBank();
return true;
}
if (game.grandExchangeOffer(slot).getQuantitySold() != quantity) {
game.widget(465, 7, 2).interact(1);
game.tick(4);
collectToInv();
log.info("timed out waiting for offer to complete: " + game.grandExchangeOffer(slot).getQuantitySold() + " / " + quantity);
}
}
return false;
}
/**
@ -286,4 +309,21 @@ public class GrandExchange {
BUY_OFFER_CREATION,
SELL_OFFER_CREATION,
}
private Bank bank() {
var bank = new Bank(game);
if (!bank.isOpen()) {
BankLocations.walkToBank(game);
if (game.npcs().withName("Banker").withAction("Bank").exists()) {
game.npcs().withName("Banker").withAction("Bank").nearest().interact("Bank");
} else if (game.objects().withName("Bank booth").withAction("Bank").exists()) {
game.objects().withName("Bank booth").withAction("Bank").nearest().interact("Bank");
} else {
game.objects().withName("Bank chest").nearest().interact("Use");
}
game.waitUntil(bank::isOpen, 10);
}
return bank;
}
}

File diff suppressed because one or more lines are too long