private static final Pattern BODY_PATTERN = Pattern.compile( "<body>.*</body>", Pattern.DOTALL );
private static final Pattern LOCATION_PATTERN = Pattern.compile( "<[aA] (id=\"againlink\" )?href=[\"']?([^\"'>]*)", Pattern.DOTALL );
public static final String getAdventureAgainLocation( StringBuffer response )
{
// Get the "adventure again" link from the page.
// Search only in the body of the page
Matcher m = BODY_PATTERN.matcher( response );
if ( !m.find() )
{
// This will not happen
return "main.php";
}
boolean againLinkExists = response.indexOf( "id=againlink" ) != -1;
m = LOCATION_PATTERN.matcher( m.group(0) );
while ( m.find() )
{
// Skip Monster Manuel's link to a new factoid
// questlog.php?which=6&vl=p#mon1429
String again = m.group( 1 );
String link = m.group( 2 );
// If KoL says that this is the "adventure again" link, believe it
if ( again != null )
{
return link;
}
if ( againLinkExists && again == null )
{
continue;
}
if ( !link.contains( "questlog.php" ) &&
!link.contains( "desc_item.php" ) &&
!link.contains( "showplayer.php" ) )
{
return link;
}
}
// If there is none, perhaps we fought a monster as a result of
// using an item.
MonsterData monster = MonsterStatusTracker.getLastMonster();
if ( monster != null )
{
String monsterName = monster.getName();
if ( monsterName.equals( "giant sandworm" ) )
{
AdventureResult drumMachine = ItemPool.get( ItemPool.DRUM_MACHINE, 1 );
if ( KoLConstants.inventory.contains( drumMachine ) )
{
return "inv_use.php?pwd=" + GenericRequest.passwordHash + "&which=3&whichitem=" + ItemPool.DRUM_MACHINE;
}
// Look for more drum machines in the Oasis
return "adventure.php?snarfblat=122";
}
if ( monsterName.equals( "scary pirate" ) )
{
return "inv_use.php?pwd=" + GenericRequest.passwordHash +"&which=3&whichitem=" + ItemPool.CURSED_PIECE_OF_THIRTEEN;
}
}
return "main.php";
}