Difference between pages "Relay Override Scripting" and "Help talk:Frequently Asked Questions"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>Bale
m
 
imported>Heeheehee
m
 
Line 1: Line 1:
{{TOCright}}
+
==Setting up symlinks for Dropbox on non-Windows systems==
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.
+
Open the command-line interface for your OS. For linux, type in:<br>
 +
ln -s ~/.kolmafia /path/to/dropbox/folder/KoLmafia
  
==Relay Script==
+
For a Mac, type in:<br>
Basic relay scripts are used to change the appearance of a KoL page. Note that relay scripts are disabled by default. In order to use relay override scripts, you must enable the relevant KoLmafia preference checked as shown:
+
ln -s /Library/Application\ Support/KoLmafia /path/to/dropbox/folder/KoLmafia
:[[File:Override browser preference.jpg]]
 
  
Then you may download relay scripts from the [http://kolmafia.us/forumdisplay.php?16-Relay-Override-Scripts KoLmafia Forums] to your /relay directory.
+
==Setting up symlinks for Dropbox on a Mac==
 +
If you're using a Mac, and the section above made no sense, here are step-by-step instructions that will get you there.
 +
*Open Terminal.app.  This is in /Applications/Utilities, or you can just use Spotlight to find it.
 +
*Open Finder, and navigate to the folder containing your Dropbox folder.
 +
*In Terminal, type "cd ", then drag the Dropbox folder onto the Terminal window, then press Enter.
 +
**Leave the quotes out for all of these commands and make sure to include the trailing space. Copy-pasting the commands is advised.
 +
*In Terminal, type "ln -s /Library/Application\ Support/KoLmafia KoLmafia", then press enter.
 +
*You now have a symbolic link, and Dropbox will back up your mafia settings files.
  
===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|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===
 
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.
 
<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
 
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
+
My suggestion; we just create a "Dropbox" page here on the wiki, link to that from the FAQ, add the external link to the text above, and call it a day. Any other input? --[[User:StDoodle|StDoodle (#1059825)]] 02:30, 19 June 2010 (UTC)--[[Special:Contributions/75.48.127.248|75.48.127.248]] 02:29, 19 June 2010 (UTC)
  void orchid() {
+
:That seems like a better solution than what we have right now -- I kinda added (rather sloppily) the question on a whim, and it looks like everyone else has just been building off of that foundation. --[[User:Heeheehee|Heeheehee]] 03:59, 19 June 2010 (UTC)
      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
 
  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==
 
 
 
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|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]]
 

Latest revision as of 03:59, 19 June 2010

Setting up symlinks for Dropbox on non-Windows systems

Open the command-line interface for your OS. For linux, type in:
ln -s ~/.kolmafia /path/to/dropbox/folder/KoLmafia

For a Mac, type in:
ln -s /Library/Application\ Support/KoLmafia /path/to/dropbox/folder/KoLmafia

Setting up symlinks for Dropbox on a Mac

If you're using a Mac, and the section above made no sense, here are step-by-step instructions that will get you there.

  • Open Terminal.app. This is in /Applications/Utilities, or you can just use Spotlight to find it.
  • Open Finder, and navigate to the folder containing your Dropbox folder.
  • In Terminal, type "cd ", then drag the Dropbox folder onto the Terminal window, then press Enter.
    • Leave the quotes out for all of these commands and make sure to include the trailing space. Copy-pasting the commands is advised.
  • In Terminal, type "ln -s /Library/Application\ Support/KoLmafia KoLmafia", then press enter.
  • You now have a symbolic link, and Dropbox will back up your mafia settings files.


My suggestion; we just create a "Dropbox" page here on the wiki, link to that from the FAQ, add the external link to the text above, and call it a day. Any other input? --StDoodle (#1059825) 02:30, 19 June 2010 (UTC)--75.48.127.248 02:29, 19 June 2010 (UTC)

That seems like a better solution than what we have right now -- I kinda added (rather sloppily) the question on a whim, and it looks like everyone else has just been building off of that foundation. --Heeheehee 03:59, 19 June 2010 (UTC)