Skip to content

Commit

Permalink
feat: support rock garden in garden command (#1438)
Browse files Browse the repository at this point in the history
* feat: support rock garden in garden command

* tests: fix unidentified leak

* tests: fix garden leak in test

Co-authored-by: Veracity0 <83725298+Veracity0@users.noreply.github.com>
  • Loading branch information
midgleyc and Veracity0 committed Jan 15, 2023
1 parent 52c0073 commit fa0e420
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 22 deletions.
64 changes: 50 additions & 14 deletions src/net/sourceforge/kolmafia/request/CampgroundRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import net.sourceforge.kolmafia.AdventureResult;
import net.sourceforge.kolmafia.KoLAdventure;
import net.sourceforge.kolmafia.KoLCharacter;
Expand Down Expand Up @@ -505,6 +506,17 @@ public static AdventureResult getCrop() {
return null;
}

public static List<AdventureResult> getCrops() {
var list = new ArrayList<AdventureResult>();
for (AdventureResult crop : CampgroundRequest.CROPS) {
int index = KoLConstants.campground.indexOf(crop);
if (index != -1) {
list.add(KoLConstants.campground.get(index));
}
}
return list;
}

public static CropType getCropType() {
return CampgroundRequest.getCropType(getCrop());
}
Expand Down Expand Up @@ -603,13 +615,13 @@ public static void clearCrop() {
}

public static void harvestCrop() {
AdventureResult crop = CampgroundRequest.getCrop();
if (crop == null) {
List<AdventureResult> crops = CampgroundRequest.getCrops();
if (crops.isEmpty()) {
// No garden
return;
}

CropType cropType = CampgroundRequest.getCropType(crop);
CropType cropType = CampgroundRequest.getCropType(crops.get(0));

// Dealing with mushroom gardens is an Adventure
if (cropType == CropType.MUSHROOM) {
Expand All @@ -619,23 +631,47 @@ public static void harvestCrop() {

// Other garden types have zero or more things to pick.
// We learned the count by looking at the campground.
int count = crop.getCount();
int count = crops.stream().mapToInt(AdventureResult::getCount).sum();
if (count == 0) {
// Nothing to pick.
return;
}

// Grass plots are special: each cluster of tall grass is picked
// individually - except for Very Tall Grass (the 8th growth)
if (cropType == CropType.GRASS || count == 8) {
// Harvest the entire garden in one go
count = 1;
}
if (cropType == CropType.ROCK) {
// rock garden is actually 3 gardens
var itemIds =
crops.stream().map(AdventureResult::getItemId).collect(Collectors.toUnmodifiableSet());
if (itemIds.contains(ItemPool.GROVELING_GRAVEL)
|| itemIds.contains(ItemPool.FRUITY_PEBBLE)
|| itemIds.contains(ItemPool.LODESTONE)) {
CampgroundRequest request = new CampgroundRequest("rgarden1");
RequestThread.postRequest(request);
}
if (itemIds.contains(ItemPool.MILESTONE)
|| itemIds.contains(ItemPool.BOLDER_BOULDER)
|| itemIds.contains(ItemPool.MOLEHILL_MOUNTAIN)) {
CampgroundRequest request = new CampgroundRequest("rgarden2");
RequestThread.postRequest(request);
}
if (itemIds.contains(ItemPool.WHETSTONE)
|| itemIds.contains(ItemPool.HARD_ROCK)
|| itemIds.contains(ItemPool.STRANGE_STALAGMITE)) {
CampgroundRequest request = new CampgroundRequest("rgarden3");
RequestThread.postRequest(request);
}
} else {
// Grass plots are special: each cluster of tall grass is picked
// individually - except for Very Tall Grass (the 8th growth)
if (cropType != CropType.GRASS || count == 8) {
// Harvest the entire garden in one go
count = 1;
}

// Pick your crop (in multiple requests, if Tall Grass)
CampgroundRequest request = new CampgroundRequest("garden");
while (count-- > 0) {
RequestThread.postRequest(request);
// Pick your crop (in multiple requests, if Tall Grass)
CampgroundRequest request = new CampgroundRequest("garden");
while (count-- > 0) {
RequestThread.postRequest(request);
}
}
}

Expand Down
40 changes: 32 additions & 8 deletions src/net/sourceforge/kolmafia/textui/command/GardenCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sourceforge.kolmafia.textui.command;

import java.util.List;
import net.sourceforge.kolmafia.AdventureResult;
import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.KoLmafia;
Expand Down Expand Up @@ -41,20 +42,43 @@ public void run(final String cmd, String parameters) {
return;
}

AdventureResult crop = CampgroundRequest.getCrop();
CropType cropType = CampgroundRequest.getCropType(crop);
List<AdventureResult> crops = CampgroundRequest.getCrops();

if (crop == null) {
if (crops.isEmpty()) {
KoLmafia.updateDisplay("You don't have a garden.");
return;
}

CropType cropType = CampgroundRequest.getCropType(crops.get(0));

if (parameters.equals("")) {
int count = crop.getPluralCount();
String name = crop.getPluralName();
String gardenType = cropType.toString();
KoLmafia.updateDisplay(
"Your " + gardenType + " garden has " + count + " " + name + " in it.");
StringBuilder display = new StringBuilder();
display.append("Your ").append(gardenType).append(" garden has ");
if (cropType == CropType.ROCK) {
boolean first = true;
for (var crop : crops) {
if (crop.getCount() > 0) {
if (!first) {
display.append(", and ");
}
int count = crop.getPluralCount();
String name = crop.getPluralName();
display.append(count).append(" ").append(name);
first = false;
}
}
if (first) {
display.append("nothing");
}
} else {
var onlyCrop = crops.get(0);
int count = onlyCrop.getPluralCount();
String name = onlyCrop.getPluralName();
display.append(count).append(" ").append(name);
}
display.append(" in it.");
KoLmafia.updateDisplay(display.toString());
return;
}

Expand All @@ -73,7 +97,7 @@ public void run(final String cmd, String parameters) {
return;
}

int count = crop.getCount();
int count = crops.stream().mapToInt(AdventureResult::getCount).sum();
if (count == 0) {
KoLmafia.updateDisplay("There is nothing ready to pick in your garden.");
return;
Expand Down
10 changes: 10 additions & 0 deletions test/net/sourceforge/kolmafia/request/CampgroundRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static internal.helpers.Networking.html;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import net.sourceforge.kolmafia.KoLCharacter;
Expand Down Expand Up @@ -43,6 +45,14 @@ public void afterEach() {
CampgroundRequest.reset();
}

@Test
void canDetectNoGarden() {
String html = html("request/test_campground_no_garden.html");
CampgroundRequest.parseResponse("campground.php", html);
assertThat(CampgroundRequest.getCrop(), nullValue());
assertThat(CampgroundRequest.getCrops(), empty());
}

@Test
void canDetectNoRockGarden() {
String html = html("request/test_campground_rock_garden_0.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static internal.helpers.Networking.assertPostRequest;
import static internal.helpers.Networking.html;
import static internal.helpers.Player.withEffect;
import static internal.helpers.Player.withEmptyCampground;
import static internal.helpers.Player.withHttpClientBuilder;
import static internal.helpers.Player.withItem;
import static internal.helpers.Player.withWorkshedItem;
Expand Down Expand Up @@ -221,6 +222,7 @@ void fuelValidSendsRequest() {

var cleanups =
new Cleanups(
withEmptyCampground(),
withHttpClientBuilder(builder),
withWorkshedItem(ItemPool.ASDON_MARTIN),
withFuel(136),
Expand Down
124 changes: 124 additions & 0 deletions test/net/sourceforge/kolmafia/textui/command/GardenCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package net.sourceforge.kolmafia.textui.command;

import static internal.helpers.HttpClientWrapper.getRequests;
import static internal.helpers.Networking.assertPostRequest;
import static internal.helpers.Player.withCampgroundItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;

import internal.helpers.Cleanups;
import internal.helpers.HttpClientWrapper;
import net.sourceforge.kolmafia.KoLConstants.MafiaState;
import net.sourceforge.kolmafia.StaticEntity;
import net.sourceforge.kolmafia.objectpool.ItemPool;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class GardenCommandTest extends AbstractCommandTestBase {

public GardenCommandTest() {
this.command = "garden";
}

@BeforeEach
public void initializeState() {
HttpClientWrapper.setupFakeClient();
StaticEntity.setContinuationState(MafiaState.CONTINUE);
}

@Test
public void noGardenErrors() {
String output = execute("");
assertThat(output, containsString("You don't have a garden"));
}

@Test
public void inspectsThanksgarden() {
var cleanups = withCampgroundItem(ItemPool.CORNUCOPIA, 1);

try (cleanups) {
String output = execute("");
assertThat(output, containsString("Your thanksgarden garden has 1 cornucopia in it."));
}
}

@Test
public void inspectsThanksgardenPlural() {
var cleanups = withCampgroundItem(ItemPool.CORNUCOPIA, 2);

try (cleanups) {
String output = execute("");
assertThat(output, containsString("Your thanksgarden garden has 2 cornucopias in it."));
}
}

@Test
public void inspectsEmptyRockGarden() {
var cleanups = withCampgroundItem(ItemPool.GROVELING_GRAVEL, 0);

try (cleanups) {
String output = execute("");
assertThat(output, containsString("Your rock garden has nothing in it."));
}
}

@Test
public void inspectsPartialRockGarden() {
var cleanups = withCampgroundItem(ItemPool.GROVELING_GRAVEL, 1);

try (cleanups) {
String output = execute("");
assertThat(output, containsString("Your rock garden has 1 groveling gravel in it."));
}
}

@Test
public void inspectsFullRockGarden() {
var cleanups =
new Cleanups(
withCampgroundItem(ItemPool.FRUITY_PEBBLE, 2),
withCampgroundItem(ItemPool.BOLDER_BOULDER, 2),
withCampgroundItem(ItemPool.HARD_ROCK, 2));

try (cleanups) {
String output = execute("");
assertThat(
output,
containsString(
"Your rock garden has 2 fruity pebbles, and 2 bolder boulders, and 2 hard rocks in it."));
}
}

@Test
public void picksPartialRockGarden() {
var cleanups = withCampgroundItem(ItemPool.GROVELING_GRAVEL, 1);

try (cleanups) {
execute("pick");

var requests = getRequests();
assertThat(requests, hasSize(1));
assertPostRequest(requests.get(0), "/campground.php", "action=rgarden1");
}
}

@Test
public void picksFullRockGarden() {
var cleanups =
new Cleanups(
withCampgroundItem(ItemPool.FRUITY_PEBBLE, 2),
withCampgroundItem(ItemPool.BOLDER_BOULDER, 2),
withCampgroundItem(ItemPool.HARD_ROCK, 2));

try (cleanups) {
execute("pick");

var requests = getRequests();
assertThat(requests, hasSize(3));
assertPostRequest(requests.get(0), "/campground.php", "action=rgarden1");
assertPostRequest(requests.get(1), "/campground.php", "action=rgarden2");
assertPostRequest(requests.get(2), "/campground.php", "action=rgarden3");
}
}
}

0 comments on commit fa0e420

Please sign in to comment.