Difference between pages "Relay Override Scripting" and "Talk:Use familiar"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Bale
 
imported>Slyz
 
Line 1: Line 1:
{{TOCright}}
+
This always seems to return false for me, can anyone confirm?<br />
A relay script is a script that creates or modifies a web page in the relay browser. It is only in the relay browser that the effects of these scripts can be seen. There are two different kinds of relay override scripts. The first is simply known as an Relay Script. The second is a User Interface script.
+
--[[User:Slyz|Slyz]] 20:56, 9 April 2010 (UTC)
  
==Relay Script==
 
Basic relay scripts are used to change the appearance of a KoL page. You will find many examples of relay scripts that you may download from the [http://kolmafia.us/forumdisplay.php?16-Relay-Override-Scripts KoLmafia Forums] to your /relay directory.
 
  
===Writing a Relay Script===
+
> ash use_familiar($familiar[sandworm])
For a simple Relay Script, there are several basic rules:
 
#The name of the script must be the same as the page it is overriding, except with an ash extension instead of a php extension. For users who have enabled relay override scripts, mafia will automatically call xyz.ash every time the relay browser attempts to visit xyz.php.
 
#Unless the script will display entirely new content, it should first load the unmodified page from KoL using the command [[visit_url|visit_url()]] with no parameters.
 
#Then the contents of the page can be modified with [[replace_string|replace_string()]] or other string manipulation functions.
 
#Finally, the modified page is written with the [[write|write()]] command.
 
  
===Example of Relay Override===
+
Putting Mexicana the Hovering Sombrero back into terrarium...<br />
Here is an example of a relay script. Assuming it is named shore.ash, it will modify shore.php to reveal items that are needed for the level 6 tower monster.
+
Taking Hecho en Mexico the Baby Sandworm out of terrarium...<br />
<div style="margin-bottom: 1em; border: dashed 1px green; padding: 1em; margin:0px 20px;"><syntaxhighlight>
+
Returned: true<br />
// This script will only work if it is named shore.ash
+
I assume that the ash command should be enough to check return value; correct me if I'm wrong. --[[User:Heeheehee|Heeheehee]] 21:03, 9 April 2010 (UTC)
void main() {
 
  buffer results;
 
  // Get the basic shore page
 
  append(results, visit_url());
 
 
 
  // This will modify the Dude Ranch text if a stick of dynamite is needed
 
  void dynamite() {
 
      if(available_amount($item[stick of dynamite]) < 1)
 
        replace_string(results, "Distant Lands Dude Ranch Adventure",
 
          "<font color=\"green\"><b>Distant Lands Dude Ranch Adventure"
 
          +" (stick of dynamite needed)</b></font>");
 
  }
 
  
  // This will modify the Paradise Island text if a tropical orchid is needed
+
I'll have to recheck after RO, but here is what I was getting (doing things slightly differently):
  void orchid() {
 
      if(available_amount($item[tropical orchid]) < 1)
 
        replace_string(results, "Tropical Paradise Island Getaway",
 
          "<font color=\"green\"><b>Tropical Paradise Island Getaway"
 
          +" (tropical orchid needed)</b></font>");
 
  }
 
  
  // This will modify the Ski Resort text if a barbed-wire fence is needed
+
> ash print( use_familiar($familiar[slimeling]) )
  void fence() {
 
      if(available_amount($item[barbed-wire fence]) < 1)
 
        replace_string(results, "Large Donkey Mountain Ski Resort",
 
          "<font color=\"green\"><b>Large Donkey Mountain Ski Resort"
 
          +" (barbed-wire fence needed)</b></font>");
 
  }
 
 
 
  // Only do this if the character has no access to the mall
 
  if(!can_interact()) { 
 
      // If the character has a level 7 telescope, only check for the necessary item
 
      if(get_property("telescopeUpgrades") == "7")
 
        switch(get_property("telescope7")) {
 
        case "see a wooden beam":
 
            dynamite(); break;
 
        case "see a formidable stinger":
 
            orchid(); break;
 
        case "see a pair of horns":
 
            fence(); break;
 
        }
 
      // Check for all three items
 
      else {
 
        dynamite();
 
        orchid();
 
        fence();
 
      }
 
  }
 
 
 
  // Write the modified page to the web browser
 
  write(results);
 
}
 
</syntaxhighlight></div>
 
<br />
 
  
==User Interface Script==
+
Putting Dusty the Baby Sandworm back into terrarium...<br />
 
+
Taking Yuk the Slimeling out of terrarium...<br />
There are special kinds of relay scripts; scripts with user interfaces. As with a regular relay override script, they go in the "relay" directory. However, they don't override other pages; their entire content is created from scratch. These scripts can be accessed under the relay drop-down in the bottom-right of the top pane. Their names must start with "relay_" in order for them to show up there.
+
false<br />
 
+
Returned: void<br />
This can provide a pretty interface and automate tasks, but relay scripts aren't really suitable for long, ongoing processes such as autoadventuring, since nothing appears in the browser until the script exits. Also, writing them requires a rather different mindset than the sequential execution of a normal script: if the relay script presents options for the user, they are actually read by an entirely separate invocation of the script than the one that displayed the options. The areas in which relay scripts will be the most useful are user-friendly configuration of other scripts, and presentation of options for the user to carry out in the relay browser themselves since the script can simply link to any in-game activity.
+
--[[User:Slyz|Slyz]] 03:33, 10 April 2010 (UTC)
 
 
===Writing an Interface Script===
 
For writing interface scripts, several features were added to KoLmafia.
 
* ASH predefined string constant __FILE__, which is the filename of the current script. This allows relay scripts to link or submit a form to themselves, even if the user has renamed them.
 
* ASH function [[form_fields|form_fields()]], which returns a string[string] map with all the name/value pairs from the relay request being handled.
 
* ASH string functions [[entity_encode|entity_encode()]] and [[entity_decode|entity_decode()]], for converting text that might contain special characters such as ampersands and angle brackets so that it can safely be displayed in HTML, and retrieving such text from form fields.
 
 
 
Additionally, there are some excellent tools for writing form elements in interface scripts on the [http://kolmafia.us/showthread.php?3842-Form-of...HTML KoLmafia Forum]. That script is a toolbox of essential functions for any advanced interface script so make good use of it.
 
 
 
===Example of User Interface===
 
Here is an example of a User Interface script. This must be saved in /relay with a name starting with relay, such as '''relay_breakables.ash''' to appear in the relay drop-down in KoL's top menu. This was originally presented by Jason Harper [http://kolmafia.us/showthread.php?3769-Breakable-equipment-fine-tuning here] and makes an excellent example.
 
<div style="margin-bottom: 1em; font-size: 12px; border: dashed 1px green; padding: 1em; margin:0px 20px;"><syntaxhighlight>
 
// The following constant can be edited to add more breakable items.
 
// However, please note that adding items here does no good at all
 
// unless KoLmafia has been updated to recognize the specific breakage
 
// messages of those items.
 
boolean[item] breakable = $items[
 
  antique helmet, antique spear, antique shield, antique greaves,
 
  cyber-mattock, cheap studded belt,
 
  sugar chapeau, sugar shank, sugar shield, sugar shillelagh,
 
  sugar shirt, sugar shotgun, sugar shorts];
 
 
 
void write_option(string label, string value, string current) {
 
  write("<option value='");
 
  write(value);
 
  write("'");
 
  if (value == current)
 
      write(" selected");
 
  write(">");
 
  write(entity_encode(label));
 
  writeln("</option>");
 
}
 
 
 
void write_select(string label, string pref, boolean addDefault, string[string] fields) {
 
  if (fields contains pref)
 
      set_property(pref, fields[pref]);
 
 
 
  string current = get_property(pref);
 
  write("<tr><td align=right>");
 
  write(label);
 
  write("</td><td><select name='");
 
  write(pref);
 
  writeln("'>");
 
 
 
  if (addDefault)
 
      write_option("Use default", "", current);
 
  write_option("Abort on breakage", "1", current);
 
  write_option("Equip previous item", "2", current);
 
  write_option("Re-equip from inventory (if available), or abort", "3", current);
 
  write_option("Re-equip from inventory, or equip previous item", "4", current);
 
  write_option("Acquire new item & re-equip", "5", current);
 
  writeln("</select></td></tr>");
 
}
 
 
 
void main() {
 
  write("<html><body><form method=POST action='");
 
  write(__FILE__);
 
  writeln("'>");
 
  write("Breakable equipment fine tuning v1.0, by jasonharper@pobox.com");
 
  writeln(" (in-game: <a href='sendmessage.php?toid=363053'>Seventh</a>)");
 
  write("<p>");
 
  write("This script lets you override the default behavior when equipment breaks in combat,");
 
  write(" on an item-by-item basis.  The default setting (found in the Item section of Choice Advs)");
 
  writeln(" is duplicated here for your convenience.");
 
  writeln("<table border=0>");
 
  string[string] fields = form_fields();
 
  write_select("Default", "breakableHandling", false, fields);
 
  foreach itm in breakable
 
      write_select(to_string(itm), "breakableHandling" + to_int(itm), true, fields);
 
  writeln("</table>");
 
  writeln("<input type=submit value='Save changes'>");
 
  writeln("</form></body></html>");
 
}
 
</syntaxhighlight></div>
 
 
 
[[Category:Scripting]]
 

Revision as of 03:34, 10 April 2010

This always seems to return false for me, can anyone confirm?
--Slyz 20:56, 9 April 2010 (UTC)


> ash use_familiar($familiar[sandworm])

Putting Mexicana the Hovering Sombrero back into terrarium...
Taking Hecho en Mexico the Baby Sandworm out of terrarium...
Returned: true
I assume that the ash command should be enough to check return value; correct me if I'm wrong. --Heeheehee 21:03, 9 April 2010 (UTC)

I'll have to recheck after RO, but here is what I was getting (doing things slightly differently):

> ash print( use_familiar($familiar[slimeling]) )

Putting Dusty the Baby Sandworm back into terrarium...
Taking Yuk the Slimeling out of terrarium...
false
Returned: void
--Slyz 03:33, 10 April 2010 (UTC)