Skip to content

Commit

Permalink
Merge branch 'js-ecosystem' of github.com:kolmafia/kolmafia into js-e…
Browse files Browse the repository at this point in the history
…cosystem
  • Loading branch information
gausie committed Apr 8, 2024
2 parents f518f72 + 195fa01 commit 8cdb247
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/net/sourceforge/kolmafia/KoLmafiaCLI.java
Expand Up @@ -514,6 +514,7 @@ public static void registerCommands() {
new AcquireCommand().register("acquire").register("find").register("retrieve");
new AdventureCommand().register("adv").register("adventure");
new AliasCommand().register("alias");
new AprilBandCommand().register("aprilband");
new AreaSummaryCommand().register("safe");
new AsdonMartinCommand().register("asdonmartin");
new AshMultiLineCommand().register("<inline-ash-script>");
Expand Down
1 change: 1 addition & 0 deletions src/net/sourceforge/kolmafia/objectpool/ItemPool.java
Expand Up @@ -3745,6 +3745,7 @@ public class ItemPool {
public static final int FOCUSED_MAGNETRON_PISTOL = 11559;
public static final int EVERFULL_DART_HOLSTER = 11561;
public static final int RESEARCH_FRAGMENT = 11562;
public static final int APRILING_BAND_HELMET = 11565;
public static final int APRIL_BAND_SAXOPHONE = 11566;
public static final int APRIL_BAND_TOM = 11567;
public static final int APRIL_BAND_TUBA = 11568;
Expand Down
3 changes: 2 additions & 1 deletion src/net/sourceforge/kolmafia/request/GenericRequest.java
Expand Up @@ -1201,7 +1201,8 @@ public void run() {
&& !this.redirectLocation.equals("place.php?whichplace=monorail")
&& !this.redirectLocation.equals("place.php?whichplace=edunder")
&& !this.redirectLocation.equals("place.php?whichplace=ioty2014_cindy")
&& !this.redirectLocation.equals("/shop.php?whichshop=fwshop")) {
&& !this.redirectLocation.equals("/shop.php?whichshop=fwshop")
&& !this.redirectLocation.equals("inventory.php")) {
// Informational debug message
KoLmafia.updateDisplay("Unhandled redirect to " + this.redirectLocation);
}
Expand Down
161 changes: 161 additions & 0 deletions src/net/sourceforge/kolmafia/textui/command/AprilBandCommand.java
@@ -0,0 +1,161 @@
package net.sourceforge.kolmafia.textui.command;

import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.KoLConstants.MafiaState;
import net.sourceforge.kolmafia.KoLmafia;
import net.sourceforge.kolmafia.RequestThread;
import net.sourceforge.kolmafia.objectpool.ItemPool;
import net.sourceforge.kolmafia.preferences.Preferences;
import net.sourceforge.kolmafia.request.GenericRequest;
import net.sourceforge.kolmafia.session.ChoiceManager;
import net.sourceforge.kolmafia.session.InventoryManager;

public class AprilBandCommand extends AbstractCommand {
public AprilBandCommand() {
this.usage =
"effect [nc|c|drop] | item [instrument] | play [instrument] - participate in the apriling band";
}

private boolean lacksHelmet() {
if (!InventoryManager.equippedOrInInventory(ItemPool.APRILING_BAND_HELMET)) {
KoLmafia.updateDisplay(MafiaState.ERROR, "You need an Apriling band helmet.");
return true;
}
return false;
}

@Override
public void run(final String cmd, String parameters) {
String[] params = parameters.split(" ", 2);

switch (params[0]) {
case "effect", "conduct" -> effect(params);
case "item" -> item(params);
case "play", "twirl" -> play(params);
default -> KoLmafia.updateDisplay(MafiaState.ERROR, "Usage: aprilband " + this.usage);
}
}

private int turnsToNextConduct() {
var nextConduct = Preferences.getInteger("nextAprilBandTurn");
return nextConduct - KoLCharacter.getTurnsPlayed();
}

private int parameterToInstrument(String parameter) {
if (parameter.startsWith("sax") || parameter.startsWith("luck")) {
return ItemPool.APRIL_BAND_SAXOPHONE;
} else if (parameter.startsWith("quad") || parameter.startsWith("tom")) {
return ItemPool.APRIL_BAND_TOM;
} else if (parameter.startsWith("tuba") || parameter.startsWith("nc")) {
return ItemPool.APRIL_BAND_TUBA;
} else if (parameter.startsWith("staff")) {
return ItemPool.APRIL_BAND_STAFF;
} else if (parameter.startsWith("picc") || parameter.startsWith("fam")) {
return ItemPool.APRIL_BAND_PICCOLO;
} else {
return -1;
}
}

private void conduct(int choice) {
KoLmafia.updateDisplay("Conducting!");
GenericRequest request = new GenericRequest("inventory.php?action=apriling");
RequestThread.postRequest(request);
ChoiceManager.processChoiceAdventure(choice, "", false);
ChoiceManager.processChoiceAdventure(9, "", true);
}

private void effect(String[] params) {
if (lacksHelmet()) return;

int turns = turnsToNextConduct();
if (turns > 0) {
KoLmafia.updateDisplay(
MafiaState.ERROR, "You cannot change your conduct (" + turns + " turns to go).");
return;
}

String parameter;
if (params.length < 2 || (parameter = params[1].trim()).isEmpty()) {
KoLmafia.updateDisplay(MafiaState.ERROR, "Which effect do you want?");
return;
}

int choice;
if (parameter.startsWith("nc") || parameter.startsWith("non")) {
choice = 1;
} else if (parameter.startsWith("c")) {
choice = 2;
} else if (parameter.startsWith("drop")) {
choice = 3;
} else {
KoLmafia.updateDisplay(
MafiaState.ERROR, "I don't understand what effect " + parameter + " is.");
return;
}

conduct(choice);
}

private void item(String[] params) {
if (lacksHelmet()) return;

int instruments = Preferences.getInteger("_aprilBandInstruments");
if (instruments == 2) {
KoLmafia.updateDisplay(MafiaState.ERROR, "You cannot get any more instruments.");
return;
}

String parameter;
if (params.length < 2 || (parameter = params[1].trim()).isEmpty()) {
KoLmafia.updateDisplay(MafiaState.ERROR, "Which instrument do you want?");
return;
}

int instrument = parameterToInstrument(parameter);
int choice;
switch (instrument) {
case ItemPool.APRIL_BAND_SAXOPHONE -> choice = 4;
case ItemPool.APRIL_BAND_TOM -> choice = 5;
case ItemPool.APRIL_BAND_TUBA -> choice = 6;
case ItemPool.APRIL_BAND_STAFF -> choice = 7;
case ItemPool.APRIL_BAND_PICCOLO -> choice = 8;
default -> {
KoLmafia.updateDisplay(
MafiaState.ERROR, "I don't understand what instrument " + parameter + " is.");
return;
}
}

conduct(choice);
}

private void play(String[] params) {
String parameter;
if (params.length < 2 || (parameter = params[1].trim()).isEmpty()) {
KoLmafia.updateDisplay(MafiaState.ERROR, "Which instrument do you want to play?");
return;
}

int instrument = parameterToInstrument(parameter);

var item = ItemPool.get(instrument);
if (!InventoryManager.equippedOrInInventory(instrument)) {
KoLmafia.updateDisplay(MafiaState.ERROR, "You don't have an " + item.getName() + ".");
return;
}

KoLmafia.updateDisplay("Playing " + item.getName());

var request =
new GenericRequest(
"inventory.php?action=aprilplay&iid="
+ instrument
+ "&pwd="
+ GenericRequest.passwordHash,
false);
RequestThread.postRequest(request);

KoLCharacter.updateStatus();
}
}
8 changes: 8 additions & 0 deletions test/internal/helpers/Networking.java
Expand Up @@ -75,4 +75,12 @@ public static void assertPostRequest(HttpRequest request, String path, String bo
assertThat(uri.getPath(), equalTo(path));
assertThat(getPostRequestBody(request), equalTo(body));
}

public static void assertPostRequest(
HttpRequest request, String path, Matcher<String> bodyMatcher) {
assertThat(request.method(), equalTo("POST"));
var uri = request.uri();
assertThat(uri.getPath(), equalTo(path));
assertThat(getPostRequestBody(request), bodyMatcher);
}
}

0 comments on commit 8cdb247

Please sign in to comment.