run_choice

From Kolmafia
Revision as of 19:57, 1 January 2021 by Philmasterplus (talk | contribs)
Jump to navigation Jump to search

Function Syntax

buffer run_choiceint choice, boolean? automate_fights = true, string? url_params )

Run the current choice adventure by selecting a choice number, optionally choosing to automate any resulting fight and specifying additional URL parameters.
  • choice: the number of the choice option you wish to take. Use -1 to automate the rest of the choice using the current choice adventure preferences.
  • automate_fights: If true and the choice resunts in a combat, KoLmafia will automate the fight. If false, KoLmafia will stop when the fight begins.
  • url_params: URL parameter string to append when submitting the choice request.

buffer run_choiceint choice, string url_params )

Run the current choice adventure by selecting a choice number and specifying additional URL parameters.
  • choice: the number of the choice option you wish to take. Use -1 to automate the rest of the choice using the current choice adventure preferences.
  • url_params: URL parameter string to append when submitting the choice request.

Proceeds to run a choice by selecting the provided choice number. If the provided number is -1, KoLmafia will use your current choice adventure preferences. This function returns the HTML of the choice result page (not just what is displayed, but all markup). This is most commonly used to finish up choices started via visit_url().

If url_params is provided, KoLmafia will append it to the URL used to submit the choice request. For example, if you are in choice adventure #1182 and call run_choice(1, "piece=1936"), KoLmafia will send a request to the server using choice.php?option=1&pwd=<pwd_hash>&whichchoice=1182&piece=1936. You can specify multiple parameters separated by ampersands (&).

The second form (without the automate_fights parameter) always automates the resulting fight (if any).

A handy use for this function is to finish up choices that were started in the relay browser, or were interrupted (i.e. mafia aborted for you to manually make a choice selection). Typing ashq run_choice(choice) into the gCLI will finish up the choice for you using your selected choice. Note that ashq is necessary rather than ash to avoid spewing the entire choice results page into the gCLI.

The url_params parameter (and the second form) was added in r19537.

Code Samples

This is a personalized version of adv1() that is capable of returning information about which adventure is encountered so that you can look for a specific choice adventure. If a choice is encountered, it selects the appropriate choice per your settings for that choice adventure, and assigns the results to the page_text variable. (Effectively the same as using run_choice(-1)). If a combat is encountered instead of a choice adventure, run_combat() is used.

ASH JavaScript
int run_adv( location place ) {
   string page_text = to_url( place ).visit_url();
   string choiceAdventure = "-1";
   matcher m_choice = create_matcher( "whichchoice value=(\\d+)", page_text );
   while ( page_text.contains_text( "choice.php" ) ) {
      m_choice.reset( page_text );
      m_choice.find();
      choiceAdventure = m_choice.group( 1 );
      string choice_num = get_property( "choiceAdventure" + choiceAdventure );

      if ( choice_num == "0" ) abort( "Manual control for " + choiceAdventure );
      if ( choice_num == "" ) abort( "Unsupported choice adventure!" );

      page_text = run_choice( choice_num );
   }
   if ( page_text.contains_text( "Combat" ) )
      run_combat();
   return choiceAdventure.to_int();
}
const {abort, getProperty, toUrl, visitUrl} = require("kolmafia");

function runAdv(place) {
  const pageText = visitUrl(toUrl(place));
  let choiceAdventure = -1;
  while (pageText.includes("choice.php")) {
    let match = pageText.match(/whichchoice value=(\d+)/);
    choiceAdventure = parseInt(match[1]);
    let choiceNum = Number(getProperty("choiceAdventure" + choiceAdventure));

    if (choiceNum === 0) abort("Manual control for " + choiceAdventure);
    if (Number.isNaN(choiceNum)) abort("Unsupported choice adventure!");

    pageText = runChoice(choiceNum);
  }
  if (pageText.includes("Combat"))
    runCombat();
  return choiceAdventure;
}

CLI Equivalent

The CLI command choice works similarly.

See Also

adventure() | adv1() | visit_url() | run_combat() | run_turn()