Difference between revisions of "Run choice"

From Kolmafia
Jump to navigation Jump to search
imported>Fredg1
m (CLI equivalent)
(Use Template:FunctionEmbed, add documentation for second form)
Line 1: Line 1:
{{
+
<onlyinclude>{{{{{format|Function2}}}
#vardefine:name|run_choice}}{{
+
|name=run_choice
#vardefine:return_type|buffer}}{{
+
|function1.return_type=buffer
 +
|function1.description=Run the current choice adventure by selecting a choice number.
 +
|function1.param1=choice
 +
|function1.param1.type=int
 +
|function1.param1.description=the number of the choice option you wish to take. Use <code>-1</code> to automate the rest of the choice using the current [[Choice Adventures|choice adventure preferences]].
 +
|function1.param2=automate_fights
 +
|function1.param2.type=boolean
 +
|function1.param2.optional=yes
 +
|function1.param2.default=true
 +
|function1.param2.description=If <code>true</code> and the choice resunts in a combat, KoLmafia will automate the fight. If <code>false</code>, KoLmafia will stop when the fight begins.
 +
|function2.return_type=buffer
 +
|function2.description=Run the current choice adventure by selecting a choice number and specifying additional URL parameters.
 +
|function2.param1=choice
 +
|function2.param1.type=int
 +
|function2.param1.description=the number of the choice option you wish to take. Use <code>-1</code> to automate the rest of the choice using the current [[Choice Adventures|choice adventure preferences]].
 +
|function2.param2=url_params
 +
|function2.param2.type=string
 +
|function2.param2.description=URL parameter string to append when submitting the choice request.
 +
|description=
 +
<p>Proceeds to run a choice by selecting the provided choice number. If the provided number is <code>-1</code>, KoLmafia will use your current [[Choice Adventures|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 {{f|visit_url}}.</p>
  
FunctionPage|
+
<p>The second form (with the {{pspan|url_params}} parameter) can be used to automate fights that require additional URL parameters. For example, if you are in choice adventure #1182 and call <kbd>run_choice(1, "piece=1936")</kbd>, KoLmafia will send a request to the server using <samp>choice.php?option=1&pwd=<pwd_hash>&whichchoice=1182&piece=1936</samp>. You can specify multiple parameters separated by ampersands (<code>&</code>).</p>
name={{#var:name}}|
 
  
function1={{Function|
+
<p>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 <kbd>ashq run_choice(choice)</kbd> into the gCLI will finish up the choice for you using your selected choice.  Note that <code>ashq</code> is necessary rather than <code>ash</code> to avoid spewing the entire choice results page into the gCLI.</p>
name={{#var:name}}|
+
|code1={{CodeSample
aggregate={{#var:aggregate}}|
+
  |title=Code Samples
return_type={{#var:return_type}}|
+
  |description=This is a personalized version of {{f|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 <code>page_text</code> variable. (Effectively the same as using <code>run_choice(-1)</code>). If a combat is encountered instead of a choice adventure, {{f|run_combat}} is used.
return_also={{#var:return_also}}|
+
  |code=
parameter1={{Param|int|choice}}|
+
{{{!}} class="wikitable"
p1desc={{Pspan|choice}} is the number of the choice option you wish to take. Use "-1" to automate the rest of the choice using existing choice adventure settings.|
+
! style="width: 50%" {{!}} ASH
 
+
! style="width: 50%" {{!}} JavaScript
}}|
+
{{!}}- style="vertical-align: top"
 
+
{{!}}
 
+
<syntaxhighlight lang="d" line highlight="14">
 
+
int run_adv( location place ) {
function_description=Proceeds to run a choice, selecting the provided choice number (or, if the provided choice number = -1, using your current KoLmafia settings). This function returns the HTML from the choice page (not just what is displayed, but all markup). Most commonly used to finish up choices started via [[visit_url|visit_url()]].</p><p>
+
  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 );
  
Another 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 CLI 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 CLI.|
+
      if ( choice_num == "0" ) abort( "Manual control for " + choiceAdventure );
 +
      if ( choice_num == "" ) abort( "Unsupported choice adventure!" );
  
code1={{CodeSample|
+
       page_text = run_choice( choice_num );
title=Code Sample|
 
description=This is a personalized version of [[adv1|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|run_combat()]] is used.|
 
code=
 
<syntaxhighlight>
 
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"))
+
   if ( page_text.contains_text( "Combat" ) )
 
       run_combat();
 
       run_combat();
 
   return choiceAdventure.to_int();
 
   return choiceAdventure.to_int();
 
}
 
}
</syntaxhighlight>}}|
+
</syntaxhighlight>
 +
{{!}}
 +
<syntaxhighlight lang="js" line highlight="14">
 +
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));
  
see_also={{SeeAlso|adventure|adv1|visit_url|run_combat|run_turn}}|
+
    if (choiceNum === 0) abort("Manual control for " + choiceAdventure);
cli_equiv=The CLI command "choice" works similarly.|
+
    if (Number.isNaN(choiceNum)) abort("Unsupported choice adventure!");
  
 +
    pageText = runChoice(choiceNum);
 +
  }
 +
  if (pageText.includes("Combat"))
 +
    runCombat();
 +
  return choiceAdventure;
 +
}
 +
</syntaxhighlight>
 +
{{!}}}
 +
  |moreinfo=
 
}}
 
}}
 
+
|see_also={{SeeAlso|adventure|adv1|visit_url|run_combat|run_turn}}
 +
|cli_equiv=The CLI command <kbd>choice</kbd> works similarly.
 +
|more_info=
 +
|special=
 +
|{{{1|}}}
 +
}}</onlyinclude>
 
[[Category:Adventuring]]
 
[[Category:Adventuring]]

Revision as of 19:42, 1 January 2021

Function Syntax

buffer run_choiceint choice, boolean? automate_fights = true )

Run the current choice adventure by selecting a choice number.
  • 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.

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().

The second form (with the url_params parameter) can be used to automate fights that require additional URL parameters. 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 (&).

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.

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
 1 int run_adv( location place ) {
 2    string page_text = to_url( place ).visit_url();
 3    string choiceAdventure = "-1";
 4    matcher m_choice = create_matcher( "whichchoice value=(\\d+)", page_text );
 5    while ( page_text.contains_text( "choice.php" ) ) {
 6       m_choice.reset( page_text );
 7       m_choice.find();
 8       choiceAdventure = m_choice.group( 1 );
 9       string choice_num = get_property( "choiceAdventure" + choiceAdventure );
10 
11       if ( choice_num == "0" ) abort( "Manual control for " + choiceAdventure );
12       if ( choice_num == "" ) abort( "Unsupported choice adventure!" );
13 
14       page_text = run_choice( choice_num );
15    }
16    if ( page_text.contains_text( "Combat" ) )
17       run_combat();
18    return choiceAdventure.to_int();
19 }
 1 const {abort, getProperty, toUrl, visitUrl} = require("kolmafia");
 2 
 3 function runAdv(place) {
 4   const pageText = visitUrl(toUrl(place));
 5   let choiceAdventure = -1;
 6   while (pageText.includes("choice.php")) {
 7     let match = pageText.match(/whichchoice value=(\d+)/);
 8     choiceAdventure = parseInt(match[1]);
 9     let choiceNum = Number(getProperty("choiceAdventure" + choiceAdventure));
10 
11     if (choiceNum === 0) abort("Manual control for " + choiceAdventure);
12     if (Number.isNaN(choiceNum)) abort("Unsupported choice adventure!");
13 
14     pageText = runChoice(choiceNum);
15   }
16   if (pageText.includes("Combat"))
17     runCombat();
18   return choiceAdventure;
19 }

CLI Equivalent

The CLI command choice works similarly.

See Also

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