Difference between pages "Batch open" and "Extract items"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Bale
m (markup fix)
 
imported>Eliteofdelete
 
Line 1: Line 1:
 
{{
 
{{
#vardefine:name|batch_open}}{{
+
#vardefine:name|extract_items}}{{
#vardefine:return_type|void}}{{
+
#vardefine:return_type|int [item]}}{{
 +
#vardefine:aggregate|yes}}{{
  
 
FunctionPage|
 
FunctionPage|
Line 11: Line 12:
 
return_type={{#var:return_type}}|
 
return_type={{#var:return_type}}|
 
return_also={{#var:return_also}}|
 
return_also={{#var:return_also}}|
 +
parameter1={{Param|string|text}}|
 +
p1desc={{Pspan|text}} is the string to search|
 
}}|
 
}}|
  
function_description=Marks the start of a batch operation. For any meaningful results, it must eventually be followed by {{f|batch_close}} (see the preceding for information on using these two functions).|
+
function_description=This function searches your supplied {{pspan|text}}, searching for either the phrase "You acquire an item: <nowiki><b>THING</b></nowiki>" or "You acquire <nowiki><b># THINGS</b></nowiki>" and returns a map keyed by items ("THING"), with the quantity of each item as the value. Note that the phrases to search are case-sensitive, so it will not match on "You Acquire 10 broken skulls". This function can be used for parsing item acquisition from combat and kmails (though only the system messages are recommended from the latter, as the message text of a kmail can be set in a non-standard way).</p>
 +
<p>If a kmail properly contains the text for item acquisition, this function will believe that the item was acquired. Consequently it should not be relied on in a case where a message containing text such as "You acquire an item: <nowiki><b>Mr. Accessory<b></nowiki>" will cost you. For secure sales you'll need to use [[contains_text|contains_text()]] and a harder to forge string.</p>
 +
<p>Note that this function cannot identify items affected by the sword behind inappropriate prepositions (acquired via pickpocketing, for instance). This has been reported as a bug and is subject to change.|
  
needscode=yes|
+
code1={{CodeSample|
 +
title=Code Samples|
 +
description=Adventures once in the Castle of the Sky Top Floor and prints out which items were acquired from the fight.|
 +
code=
 +
<syntaxhighlight>
 +
int[item] items;
 +
buffer fight = visit_url(to_url($location[The Castle in the Clouds in the Sky (Top Floor)]));
 +
repeat {
 +
  if (contains_text(fight, "choice.php")) { //Check to see if in a choice adventure
 +
      cli_execute("choice-goal"); //Clear the choice adventure
 +
      fight = visit_url(to_url($location[The Castle in the Clouds in the Sky (Top Floor)]));
 +
  }
 +
} until (!contains_text(fight, "choice.php"));
 +
fight = run_combat();
 +
if (have_effect($effect[beaten up]) == 0) {
 +
  items = extract_items(fight);
 +
  print("You acquired the following items:", "green");
 +
  foreach it, number in items
 +
      print(""+number+", "+it, "blue");
 +
}
 +
else print("You lost the fight :(", "red");
  
see_also={{SeeAlso|batch_close}}|
+
</syntaxhighlight>|
 +
moreinfo=
 +
Does not work with the adventure() function unless it calls a consult script. You must use a visit_url() or a consult script.
 +
}}|
 +
 
 +
see_also={{SeeAlso|extract_meat}}|
 +
special=This function returns an empty map if it does not find any matches.
 
}}
 
}}
  
[[Category:Miscellaneous Functions]]
+
[[Category:String Handling Routines]]

Latest revision as of 08:51, 11 January 2015

Function Syntax

int [item] extract_items(string text )

  • text is the string to search

This function searches your supplied text, searching for either the phrase "You acquire an item: <b>THING</b>" or "You acquire <b># THINGS</b>" and returns a map keyed by items ("THING"), with the quantity of each item as the value. Note that the phrases to search are case-sensitive, so it will not match on "You Acquire 10 broken skulls". This function can be used for parsing item acquisition from combat and kmails (though only the system messages are recommended from the latter, as the message text of a kmail can be set in a non-standard way).

If a kmail properly contains the text for item acquisition, this function will believe that the item was acquired. Consequently it should not be relied on in a case where a message containing text such as "You acquire an item: <b>Mr. Accessory<b>" will cost you. For secure sales you'll need to use contains_text() and a harder to forge string.

Note that this function cannot identify items affected by the sword behind inappropriate prepositions (acquired via pickpocketing, for instance). This has been reported as a bug and is subject to change.

Code Samples

Adventures once in the Castle of the Sky Top Floor and prints out which items were acquired from the fight.

int[item] items;
buffer fight = visit_url(to_url($location[The Castle in the Clouds in the Sky (Top Floor)]));
repeat {
   if (contains_text(fight, "choice.php")) { //Check to see if in a choice adventure
      cli_execute("choice-goal"); //Clear the choice adventure
      fight = visit_url(to_url($location[The Castle in the Clouds in the Sky (Top Floor)])); 
   }
} until (!contains_text(fight, "choice.php"));
fight = run_combat();
if (have_effect($effect[beaten up]) == 0) {
   items = extract_items(fight);
   print("You acquired the following items:", "green");
   foreach it, number in items 
      print(""+number+", "+it, "blue");
}
else print("You lost the fight :(", "red");

Does not work with the adventure() function unless it calls a consult script. You must use a visit_url() or a consult script.

See Also

extract_meat()

Special

This function returns an empty map if it does not find any matches.