Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return acquired item from zap function #982

Merged
merged 4 commits into from Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/net/sourceforge/kolmafia/request/ZapRequest.java
Expand Up @@ -26,12 +26,15 @@

public class ZapRequest extends GenericRequest {
private static final Pattern ZAP_PATTERN = Pattern.compile("whichitem=(\\d+)");
private static final Pattern ACQUIRE_PATTERN =
Pattern.compile("You acquire an item: <b>(.*?)</b>");
private static final Pattern OPTION_PATTERN =
Pattern.compile("<option value=(\\d+) descid='.*?'>.*?</option>");

private static final Map<Integer, List<String>> zapGroups = new HashMap<>();

private AdventureResult item;
private AdventureResult acquired;

public ZapRequest(final AdventureResult item) {
super("wand.php");
Expand Down Expand Up @@ -131,8 +134,18 @@ public void processResults() {
return;
}

Matcher acquiresMatcher = ZapRequest.ACQUIRE_PATTERN.matcher(responseText);
String acquired = acquiresMatcher.find() ? acquiresMatcher.group(1) : null;
if (acquired != null) {
this.acquired = ItemPool.get(acquired, 1);
}

// Notify the user of success.
KoLmafia.updateDisplay(this.item.getName() + " has been transformed.");
KoLmafia.updateDisplay(
this.item.getName()
+ " has been transformed into "
+ (acquired != null ? acquired : "an unknown item")
+ ".");
}

public static void parseResponse(final String urlString, final String responseText) {
Expand Down Expand Up @@ -216,4 +229,8 @@ public static boolean registerRequest(final String urlString) {

return true;
}

public AdventureResult getAcquired() {
return acquired;
}
}
13 changes: 13 additions & 0 deletions src/net/sourceforge/kolmafia/textui/RuntimeLibrary.java
Expand Up @@ -2698,6 +2698,9 @@ public static FunctionList getFunctions() {
"absorbed_monsters",
new AggregateType(DataTypes.BOOLEAN_TYPE, DataTypes.MONSTER_TYPE),
params));

params = new Type[] {DataTypes.ITEM_TYPE};
functions.add(new LibraryFunction("zap", DataTypes.ITEM_TYPE, params));
}

public static Method findMethod(final String name, final Class<?>[] args)
Expand Down Expand Up @@ -9718,4 +9721,14 @@ public static Value absorbed_monsters(ScriptRuntime controller) {

return value;
}

public static Value zap(ScriptRuntime controller, Value item) {
var request = new ZapRequest(ItemPool.get((int) item.intValue()));
RequestThread.postRequest(request);

var acquired = request.getAcquired();
int itemId = acquired == null ? -1 : acquired.getItemId();

return DataTypes.makeItemValue(itemId, true);
}
}
27 changes: 27 additions & 0 deletions test/net/sourceforge/kolmafia/textui/RuntimeLibraryTest.java
Expand Up @@ -241,6 +241,33 @@ void canSeeGreyYouMonsterAbsorbs() {
}
}

@Nested
class Zap {
@Test
void noWandReturnsNone() {
var cleanups = withItem("Dreadsylvanian spooky pocket");

try (cleanups) {
String output = execute("zap($item[Dreadsylvanian spooky pocket])");
assertThat(output, containsString("Returned: none"));
}
}

@Test
void canZapItem() {
var cleanups =
new Cleanups(
withItem("hexagonal wand"),
withItem("Dreadsylvanian spooky pocket"),
withNextResponse(200, html("request/test_zap_pockets.html")));

try (cleanups) {
String output = execute("zap($item[Dreadsylvanian spooky pocket])");
assertThat(output, containsString("Returned: Dreadsylvanian hot pocket"));
}
}
}

@Nested
class Equip {
@Test
Expand Down
Expand Up @@ -2,7 +2,9 @@

import static internal.helpers.HttpClientWrapper.getRequests;
import static internal.helpers.Networking.assertPostRequest;
import static internal.helpers.Networking.html;
import static internal.helpers.Player.withItem;
import static internal.helpers.Player.withNextResponse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
Expand Down Expand Up @@ -102,4 +104,21 @@ public void zapManyItems() {
var requests = getRequests();
assertThat(requests.size(), equalTo(3));
}

@Test
public void showAcquiredItem() {
String output;
var cleanups =
new Cleanups(
withItem("hexagonal wand"),
withItem("Dreadsylvanian spooky pocket"),
withNextResponse(200, html("request/test_zap_pockets.html")));

try (cleanups) {
output = execute("Dreadsylvanian spooky pocket");
}

assertThat(output, containsString("Dreadsylvanian spooky pocket has been transformed"));
assertThat(output, containsString("transformed into Dreadsylvanian hot pocket"));
}
}
1 change: 1 addition & 0 deletions test/root/request/test_zap_pockets.html
@@ -0,0 +1 @@
<html><head><script language=Javascript><!--if (parent.frames.length == 0) location.href="game.php";top.charpane.location.href="charpane.php";//--></script><script language=Javascript src="https://d2uyhvukfffg5a.cloudfront.net/scripts/keybinds.min.2.js"></script><script language=Javascript src="https://d2uyhvukfffg5a.cloudfront.net/scripts/window.20111231.js"></script><script language="javascript">function chatFocus(){if(top.chatpane.document.chatform.graf) top.chatpane.document.chatform.graf.focus();}if (typeof defaultBind != 'undefined') { defaultBind(47, 2, chatFocus); defaultBind(190, 2, chatFocus);defaultBind(191, 2, chatFocus); defaultBind(47, 8, chatFocus);defaultBind(190, 8, chatFocus); defaultBind(191, 8, chatFocus); }</script><script language=Javascript src="https://d2uyhvukfffg5a.cloudfront.net/scripts/jquery-1.3.1.min.js"></script><link rel="stylesheet" type="text/css" href="https://d2uyhvukfffg5a.cloudfront.net/styles.20151006.css"><style type='text/css'>.faded {zoom: 1;filter: alpha(opacity=35);opacity: 0.35;-khtml-opacity: 0.35;-moz-opacity: 0.35;}</style></head><body><centeR><table width=95% cellspacing=0 cellpadding=0><tr><td style="color: white;" align=center bgcolor=blue><b>Results:</b></td></tr><tr><td style="padding: 5px; border: 1px solid blue;"><center><table><tr><td>You zap the Dreadsylvanian spooky pocket with your hexagonal wand.<center><table class="item" style="float: none" rel="id=6564&s=0&q=0&d=0&g=0&t=1&n=1&m=0&p=0&u=e"><tr><td><img src="https://d2uyhvukfffg5a.cloudfront.net/itemimages/hotpocket.gif" alt="Dreadsylvanian hot pocket" title="Dreadsylvanian hot pocket" class=hand onClick='descitem(365191722)' ></td><td valign=center class=effect>You acquire an item: <b>Dreadsylvanian hot pocket</b></td></tr></table></center><p><Center><img src="https://d2uyhvukfffg5a.cloudfront.net/adventureimages/explosion.gif"></center><p>The hexagonal wand abruptly explodes, burning your hand pretty badly in the process.<center><table><tr><td><img src="https://d2uyhvukfffg5a.cloudfront.net/itemimages/hp.gif" height=30 width=30></td><td valign=center class=effect>You lose 30 hit points.</td></tr></table></center></td></tr></table></center></td></tr><tr><td height=4></td></tr></table><table width=95% cellspacing=0 cellpadding=0><tr><td style="color: white;" align=center bgcolor=blue><b>Zap Something</b></td></tr><tr><td style="padding: 5px; border: 1px solid blue;"><center><table><tr><td><center>You don't have a hexagonal wand anymore... How sad.<p><a href="inventory.php">Back to your Inventory</a></center></td></tr></table></center></td></tr><tr><td height=4></td></tr></table></center></body></html>