Skip to content

Commit

Permalink
Encounter manager tests (#583)
Browse files Browse the repository at this point in the history
* A few small changes during writing tests

* More work on Player utils

* Write tests for EncounterManager

* Cleanup after tests

* Replace series of if statements with switch

* Reset last adventure before every test in EncounterManager since it gets set by the adventure again link in the html

* That doesn't work, this does\?

* Add testing for rainman monster

* Cover the rest of the file that is reasonable to do

* Add Feeling Lost as Teleportitis source and DRY autostop encounter type check

* Increment RegisteredEncounter in place

* Simplify some loop functions with streams

* Use Encounter toString instead of exposing just the name String as an API response
  • Loading branch information
gausie committed Feb 6, 2022
1 parent d380b20 commit c73eab1
Show file tree
Hide file tree
Showing 12 changed files with 832 additions and 129 deletions.
10 changes: 5 additions & 5 deletions src/net/sourceforge/kolmafia/AreaCombatData.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,17 @@ public void getEncounterData(final StringBuffer buffer) {
}
}

String encounter = EncounterManager.findEncounterForLocation(this.zone, EncounterType.LUCKY);
var encounter = EncounterManager.findEncounterForLocation(this.zone, EncounterType.LUCKY);

if (null != encounter) {
if (encounter != null) {
buffer.append("<br>");
buffer.append("<b>Lucky:</b> ");
buffer.append(encounter);
}

encounter = EncounterManager.findEncounterForLocation(this.zone, EncounterType.GLYPH);

if (null != encounter) {
if (encounter != null) {
buffer.append("<br>");
buffer.append("<b>Hobo Glyph:</b> ");
buffer.append(encounter);
Expand All @@ -740,7 +740,7 @@ public void getEncounterData(final StringBuffer buffer) {
if (KoLCharacter.inAxecore()) {
encounter = EncounterManager.findEncounterForLocation(this.zone, EncounterType.BORIS);

if (null != encounter) {
if (encounter != null) {
buffer.append("<br>");
buffer.append("<b>Clancy:</b> ");
buffer.append(encounter);
Expand All @@ -750,7 +750,7 @@ public void getEncounterData(final StringBuffer buffer) {
if (KoLCharacter.inBadMoon()) {
encounter = EncounterManager.findEncounterForLocation(this.zone, EncounterType.BADMOON);

if (null != encounter) {
if (encounter != null) {
buffer.append("<br>");
buffer.append("<b>Badmoon:</b> ");
buffer.append(encounter);
Expand Down
1 change: 1 addition & 0 deletions src/net/sourceforge/kolmafia/objectpool/EffectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public class EffectPool {
public static final int CARTOGRAPHICALLY_CHARGED = 2600;
public static final int CARTOGRAPHICALLY_AWARE = 2601;
public static final int CARTOGRAPHICALLY_ROOTED = 2602;
public static final int FEELING_LOST = 2630;
public static final int EW_THE_HUMANITY = 2647;
public static final int A_BEASTLY_ODOR = 2648;
public static final int WINE_FORTIFIED = 2659;
Expand Down
2 changes: 1 addition & 1 deletion src/net/sourceforge/kolmafia/request/GenericRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ private static void checkItemRedirection(final String location) {
KoLAdventure adventure = AdventureDatabase.getAdventure(nextAdventure);
KoLAdventure.setLastAdventure(adventure);
KoLAdventure.setNextAdventure(adventure);
EncounterManager.registerAdventure(adventure.getAdventureName());
EncounterManager.registerAdventure(adventure);
}

String message = "[" + KoLAdventure.getAdventureCount() + "] " + itemName;
Expand Down
227 changes: 108 additions & 119 deletions src/net/sourceforge/kolmafia/session/EncounterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.kolmafia.AdventureResult;
Expand All @@ -25,22 +26,37 @@

public abstract class EncounterManager {
// Types of special encounters

public enum EncounterType {
NONE,
STOP,
STOP(true),
LUCKY,
GLYPH,
GLYPH(true),
TURTLE,
SEAL,
FIST,
BORIS,
BADMOON,
BORIS(true),
BADMOON(true),
BUGBEAR,
WANDERER,
SUPERLIKELY,
ULTRARARE,
FREE_COMBAT,
NOWANDER, // Don't start wandering monster counters
NOWANDER; // Don't start wandering monster counters

private final boolean autostop;

EncounterType(final boolean autostop) {
this.autostop = autostop;
}

EncounterType() {
this(false);
}

public final boolean isAutostop() {
return this.autostop;
}
}

public static class Encounter {
Expand All @@ -65,6 +81,11 @@ public EncounterType getEncounterType() {
public String getEncounter() {
return this.encounter;
}

@Override
public String toString() {
return this.encounter;
}
}

private static Encounter[] specialEncounters;
Expand Down Expand Up @@ -96,7 +117,7 @@ private static void resetEncounters() {
}

/** Utility method used to register a given adventure in the running adventure summary. */
public void registerAdventure(final KoLAdventure adventureLocation) {
public static void registerAdventure(final KoLAdventure adventureLocation) {
if (adventureLocation == null) {
return;
}
Expand All @@ -112,8 +133,7 @@ public static void registerAdventure(final String adventureName) {
LockableListFactory.lastElement(KoLConstants.adventureList);

if (previousAdventure != null && previousAdventure.name.equals(adventureName)) {
++previousAdventure.encounterCount;
KoLConstants.adventureList.set(KoLConstants.adventureList.size() - 1, previousAdventure);
previousAdventure.increment();
} else {
KoLConstants.adventureList.add(new RegisteredEncounter(null, adventureName));
}
Expand All @@ -131,18 +151,11 @@ public static final Encounter findEncounter(

public static final Encounter findEncounter(
final String locationName, final String encounterName) {
for (int i = 0; i < specialEncounters.length; ++i) {
Encounter encounter = specialEncounters[i];
if (locationName != null && !locationName.equalsIgnoreCase(encounter.location)) {
continue;
}
if (!encounterName.equalsIgnoreCase(encounter.encounter)) {
continue;
}
return encounter;
}

return null;
return Arrays.stream(specialEncounters)
.filter(e -> locationName == null || e.getLocation().equalsIgnoreCase(locationName))
.filter(e -> e.getEncounter().equalsIgnoreCase(encounterName))
.findAny()
.orElse(null);
}

private static EncounterType encounterType(final String encounterName) {
Expand All @@ -159,20 +172,13 @@ private static EncounterType encounterType(
: EncounterType.NONE;
}

public static final String findEncounterForLocation(
public static final Encounter findEncounterForLocation(
final String locationName, final EncounterType type) {
for (int i = 0; i < specialEncounters.length; ++i) {
Encounter encounter = specialEncounters[i];
if (!locationName.equalsIgnoreCase(encounter.location)) {
continue;
}
if (!type.equals(encounter.encounterType)) {
continue;
}
return encounter.encounter;
}

return null;
return Arrays.stream(specialEncounters)
.filter(e -> e.getLocation().equalsIgnoreCase(locationName))
.filter(e -> e.getEncounterType().equals(type))
.findAny()
.orElse(null);
}

public static final boolean isAutoStop(final String encounterName) {
Expand All @@ -181,11 +187,7 @@ public static final boolean isAutoStop(final String encounterName) {
return false;
}

EncounterType encounterType = encounterType(encounterName);
return encounterType == EncounterType.STOP
|| encounterType == EncounterType.BORIS
|| encounterType == EncounterType.GLYPH
|| encounterType == EncounterType.BADMOON;
return encounterType(encounterName).isAutostop();
}

public static boolean isRomanticEncounter(final String responseText, final boolean checkMonster) {
Expand Down Expand Up @@ -330,6 +332,7 @@ public static void ignoreSpecialMonsters() {
}

private static final AdventureResult TELEPORTITIS = EffectPool.get(EffectPool.TELEPORTITIS);
private static final AdventureResult FEELING_LOST = EffectPool.get(EffectPool.FEELING_LOST);

private static void recognizeEncounter(final String encounterName, final String responseText) {
Encounter encounter = EncounterManager.findEncounter(encounterName);
Expand Down Expand Up @@ -364,15 +367,13 @@ private static void recognizeEncounter(final String encounterName, final String
BadMoonManager.registerAdventure(encounterName);
}

if (encounterType == EncounterType.STOP
|| encounterType == EncounterType.BORIS
|| encounterType == EncounterType.GLYPH
|| encounterType == EncounterType.BADMOON) {
if (encounterType.isAutostop()) {
// Don't autostop if you have teleportisis
if (KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, EquipmentManager.ACCESSORY1)
|| KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, EquipmentManager.ACCESSORY2)
|| KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, EquipmentManager.ACCESSORY3)
|| KoLConstants.activeEffects.contains(EncounterManager.TELEPORTITIS)) {
|| KoLConstants.activeEffects.contains(TELEPORTITIS)
|| KoLConstants.activeEffects.contains(FEELING_LOST)) {
return;
}

Expand All @@ -382,89 +383,73 @@ private static void recognizeEncounter(final String encounterName, final String

/** Utility. The method used to register a given encounter in the running adventure summary. */
public static void registerEncounter(
String encounterName, final String encounterType, final String responseText) {
encounterName = encounterName.trim();
final String encounterName, final String encounterType, final String responseText) {
final String name = encounterName.trim();

handleSpecialEncounter(encounterName, responseText);
recognizeEncounter(encounterName, responseText);

RegisteredEncounter[] encounters = new RegisteredEncounter[KoLConstants.encounterList.size()];
KoLConstants.encounterList.toArray(encounters);

for (int i = 0; i < encounters.length; ++i) {
if (encounters[i].name.equals(encounterName)) {
++encounters[i].encounterCount;

// Manually set to force repainting in GUI
KoLConstants.encounterList.set(i, encounters[i]);
return;
}
}
handleSpecialEncounter(name, responseText);
recognizeEncounter(name, responseText);

KoLConstants.encounterList.add(new RegisteredEncounter(encounterType, encounterName));
KoLConstants.encounterList.stream()
.filter(e -> e.name.equalsIgnoreCase(name))
.findFirst()
.ifPresentOrElse(
RegisteredEncounter::increment,
() ->
KoLConstants.encounterList.add(
new RegisteredEncounter(encounterType, encounterName)));
}

public static void handleSpecialEncounter(final String encounterName, final String responseText) {
if (encounterName.equalsIgnoreCase("Step Up to the Table, Put the Ball in Play")) {
if (InventoryManager.hasItem(ItemPool.CARONCH_DENTURES)) {
ResultProcessor.processItem(ItemPool.CARONCH_DENTURES, -1);
QuestDatabase.setQuestIfBetter(Quest.PIRATE, "step4");
}

if (InventoryManager.hasItem(ItemPool.FRATHOUSE_BLUEPRINTS)) {
ResultProcessor.processItem(ItemPool.FRATHOUSE_BLUEPRINTS, -1);
}
return;
}

if (encounterName.equalsIgnoreCase("Granny, Does Your Dogfish Bite?")) {
if (InventoryManager.hasItem(ItemPool.GRANDMAS_MAP)) {
ResultProcessor.processItem(ItemPool.GRANDMAS_MAP, -1);
}
return;
}

if (encounterName.equalsIgnoreCase("Meat For Nothing and the Harem for Free")) {
Preferences.setBoolean("_treasuryEliteMeatCollected", true);
return;
}

if (encounterName.equalsIgnoreCase("Finally, the Payoff")) {
Preferences.setBoolean("_treasuryHaremMeatCollected", true);
return;
}

if (encounterName.equals("Faction Traction = Inaction")) {
Preferences.setInteger("booPeakProgress", 98);
return;
}

if (encounterName.equals("Daily Done, John.")) {
// Daily Dungeon Complete
Preferences.setBoolean("dailyDungeonDone", true);
Preferences.setInteger("_lastDailyDungeonRoom", 15);
return;
}

if (encounterName.equals("A hidden surprise!")) {
// Since this content is short-lived, create the patterns here every time
// the encounter is found instead of globally
Pattern GIFT_SENDER_PATTERN = Pattern.compile("nounder><b>(.*?)</b></a>");
Pattern NOTE_PATTERN = Pattern.compile("1px solid black;'>(.*?)</td></tr>", Pattern.DOTALL);
switch (encounterName.toLowerCase()) {
case "step up to the table, put the ball in play":
if (InventoryManager.hasItem(ItemPool.CARONCH_DENTURES)) {
ResultProcessor.processItem(ItemPool.CARONCH_DENTURES, -1);
QuestDatabase.setQuestIfBetter(Quest.PIRATE, "step4");
}

Matcher senderMatcher = GIFT_SENDER_PATTERN.matcher(responseText);
if (senderMatcher.find()) {
String sender = senderMatcher.group(1);
RequestLogger.printLine("Gift sender: " + sender);
RequestLogger.updateSessionLog("Gift sender: " + sender);
}
if (InventoryManager.hasItem(ItemPool.FRATHOUSE_BLUEPRINTS)) {
ResultProcessor.processItem(ItemPool.FRATHOUSE_BLUEPRINTS, -1);
}
return;
case "granny, does your dogfish bite?":
if (InventoryManager.hasItem(ItemPool.GRANDMAS_MAP)) {
ResultProcessor.processItem(ItemPool.GRANDMAS_MAP, -1);
}
return;
case "meat for nothing and the harem for free":
Preferences.setBoolean("_treasuryEliteMeatCollected", true);
return;
case "finally, the payoff":
Preferences.setBoolean("_treasuryHaremMeatCollected", true);
return;
case "faction traction = inaction":
Preferences.setInteger("booPeakProgress", 98);
return;
case "daily done, john.":
// Daily Dungeon Complete
Preferences.setBoolean("dailyDungeonDone", true);
Preferences.setInteger("_lastDailyDungeonRoom", 15);
return;
case "a hidden surprise!":
// Since this content is short-lived, create the patterns here every time
// the encounter is found instead of globally
Pattern GIFT_SENDER_PATTERN = Pattern.compile("nounder><b>(.*?)</b></a>");
Pattern NOTE_PATTERN = Pattern.compile("1px solid black;'>(.*?)</td></tr>", Pattern.DOTALL);

Matcher senderMatcher = GIFT_SENDER_PATTERN.matcher(responseText);
if (senderMatcher.find()) {
String sender = senderMatcher.group(1);
RequestLogger.printLine("Gift sender: " + sender);
RequestLogger.updateSessionLog("Gift sender: " + sender);
}

Matcher noteMatcher = NOTE_PATTERN.matcher(responseText);
if (noteMatcher.find()) {
String note = noteMatcher.group(1);
RequestLogger.printLine("Gift note: " + note);
RequestLogger.updateSessionLog("Gift note: " + note);
}
Matcher noteMatcher = NOTE_PATTERN.matcher(responseText);
if (noteMatcher.find()) {
String note = noteMatcher.group(1);
RequestLogger.printLine("Gift note: " + note);
RequestLogger.updateSessionLog("Gift note: " + note);
}
return;
}
}

Expand All @@ -489,6 +474,10 @@ public String toString() {
return this.stringform + " (" + this.encounterCount + ")";
}

public void increment() {
this.encounterCount++;
}

@Override
public int compareTo(final RegisteredEncounter o) {
if (o == null) {
Expand Down

0 comments on commit c73eab1

Please sign in to comment.