Relay Override Scripting: Difference between revisions
imported>Bale No edit summary |
imported>Bale →Relay Script: Fix example to work since both KoL and KoLmafia have changed since then |
||
Line 11: | Line 11: | ||
#Then the contents of the page can be modified with [[replace_string|replace_string()]] or other string manipulation functions. | #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. | #Finally, the modified page is written with the [[write|write()]] command. | ||
===Whichplace, Whichshop and Whichstore Overrides=== | |||
For overrides to place.php, shop.php and store.php it is possible to override based on the whichplace, whichshop and whichstore parameters. This is quite helpful so that you can choose to override only specific places and shops without needing to make an override for all of them. | |||
An example is that to override the plains, you'll note that the URL is <code> place.php?whichplace=plains </code> so you could override all place.php pages with a place.ash override, or you could choose to override only the plains with a script named place.plains.ash. | |||
===Example of Relay Override=== | ===Example of Relay Override=== | ||
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. | Here is an example of a relay script. Assuming it is named shop.shore.ash, it will modify "The Shore, Inc." Shop at <code> shop.php?whichshop=shore.php </code> to reveal items that are needed for the level 6 tower monster. | ||
<div style="margin-bottom: 1em; border: dashed 1px green; padding: 1em; margin:0px 20px;"><syntaxhighlight> | <div style="margin-bottom: 1em; border: dashed 1px green; padding: 1em; margin:0px 20px;"><syntaxhighlight> | ||
// This script will only work if it is named shore.ash | // This script will only work if it is named shop.shore.ash | ||
buffer telescope(buffer page) { | |||
void dynamite() { | void dynamite() { | ||
if(available_amount($item[stick of dynamite]) < 1) | if(available_amount($item[stick of dynamite]) < 1) page.replace_string("<b>dude ranch souvenir crate</b>", | ||
"<font color=\"green\"><b>dude ranch souvenir crate<br />(stick of dynamite needed)</b></font>"); | |||
} | } | ||
void orchid() { | void orchid() { | ||
if(available_amount($item[tropical orchid]) < 1) | if(available_amount($item[tropical orchid]) < 1 && available_amount($item[packet of orchid seeds]) < 1) page.replace_string("<b>tropical island souvenir crate</b>", | ||
"<font color=\"green\"><b>tropical island souvenir crate<br />(tropical orchid needed)</b></font>"); | |||
} | } | ||
void fence() { | void fence() { | ||
if(available_amount($item[barbed-wire fence]) < 1) | if(available_amount($item[barbed-wire fence]) < 1) page.replace_string("<b>ski resort souvenir crate</b>", | ||
"<font color=\"green\"><b>ski resort souvenir crate<br />(barbed-wire fence needed)</b></font>"); | |||
} | } | ||
if(!can_interact() && my_path() != "Bugbear Invasion") { | |||
if(!can_interact()) { | if(my_path() == "Bees Hate You") { | ||
orchid(); | |||
if(get_property("telescopeUpgrades") == "7") | } else if(get_property("telescopeUpgrades") == "7" && !in_bad_moon()) { | ||
switch(get_property("telescope7")) { | switch(get_property("telescope7")) { | ||
case "see a wooden beam": | case "see a wooden beam": | ||
Line 57: | Line 51: | ||
fence(); break; | fence(); break; | ||
} | } | ||
} else { | |||
dynamite(); | dynamite(); | ||
orchid(); | orchid(); | ||
Line 65: | Line 58: | ||
} | } | ||
// | // If dinghy plans are needed, you will always need to buy them! Otherwise it is time to look into that UV-resistant compass | ||
write( | if(get_property("lastIslandUnlock").to_int() < my_ascensions() && page.contains_text("dinghy plans")) | ||
page.replace_string("<b>dinghy plans</b>", "<font color=\"green\"><b>dinghy plans</b></font>"); | |||
else if(available_amount($item[UV-resistant compass]) + available_amount($item[ornate dowsing rod]) == 0) | |||
page.replace_string("UV-resistant compass", "<font color=\"green\"><b>UV-resistant compass</b></font>"); | |||
return page; | |||
} | |||
void main() { | |||
visit_url().telescope().ShopWTF().write(); | |||
} | } | ||
</syntaxhighlight></div> | </syntaxhighlight></div> |
Revision as of 09:42, 14 November 2014
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.
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 KoLmafia Forums to your /relay directory.
Writing a Relay Script
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() with no parameters.
- Then the contents of the page can be modified with replace_string() or other string manipulation functions.
- Finally, the modified page is written with the write() command.
Whichplace, Whichshop and Whichstore Overrides
For overrides to place.php, shop.php and store.php it is possible to override based on the whichplace, whichshop and whichstore parameters. This is quite helpful so that you can choose to override only specific places and shops without needing to make an override for all of them.
An example is that to override the plains, you'll note that the URL is place.php?whichplace=plains
so you could override all place.php pages with a place.ash override, or you could choose to override only the plains with a script named place.plains.ash.
Example of Relay Override
Here is an example of a relay script. Assuming it is named shop.shore.ash, it will modify "The Shore, Inc." Shop at shop.php?whichshop=shore.php
to reveal items that are needed for the level 6 tower monster.
// This script will only work if it is named shop.shore.ash
buffer telescope(buffer page) {
void dynamite() {
if(available_amount($item[stick of dynamite]) < 1) page.replace_string("<b>dude ranch souvenir crate</b>",
"<font color=\"green\"><b>dude ranch souvenir crate<br />(stick of dynamite needed)</b></font>");
}
void orchid() {
if(available_amount($item[tropical orchid]) < 1 && available_amount($item[packet of orchid seeds]) < 1) page.replace_string("<b>tropical island souvenir crate</b>",
"<font color=\"green\"><b>tropical island souvenir crate<br />(tropical orchid needed)</b></font>");
}
void fence() {
if(available_amount($item[barbed-wire fence]) < 1) page.replace_string("<b>ski resort souvenir crate</b>",
"<font color=\"green\"><b>ski resort souvenir crate<br />(barbed-wire fence needed)</b></font>");
}
if(!can_interact() && my_path() != "Bugbear Invasion") {
if(my_path() == "Bees Hate You") {
orchid();
} else if(get_property("telescopeUpgrades") == "7" && !in_bad_moon()) {
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;
}
} else {
dynamite();
orchid();
fence();
}
}
// If dinghy plans are needed, you will always need to buy them! Otherwise it is time to look into that UV-resistant compass
if(get_property("lastIslandUnlock").to_int() < my_ascensions() && page.contains_text("dinghy plans"))
page.replace_string("<b>dinghy plans</b>", "<font color=\"green\"><b>dinghy plans</b></font>");
else if(available_amount($item[UV-resistant compass]) + available_amount($item[ornate dowsing rod]) == 0)
page.replace_string("UV-resistant compass", "<font color=\"green\"><b>UV-resistant compass</b></font>");
return page;
}
void main() {
visit_url().telescope().ShopWTF().write();
}
User Interface Script
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.
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.
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(), which returns a string[string] map with all the name/value pairs from the relay request being handled.
- ASH string functions entity_encode() and 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 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 here and makes an excellent example.
// 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>");
}