Some questions on 3-parameter visit_url() & macros

StDoodle

Minion
Hi! (/me waves)

I've got a couple of more "show off my ignorance" questions.

1) When using the 3rd parameter of visit_url(), what exactly does that third parameter being set to "true" do (I assume this is what I want, and the other two versions default to "false", but I could be backwards)? Does it only prevent "&" from being treated as a form delimiter, does it override all encoding... I'm confused.

2) Mafia-submitted macros (made from your CCS) seem to include a couple of extra blank lines at the beginning; is this necessary for KoL to process them, or is it just an artifact of how they're parsed that has no real need or effect?

Every time I think I have a handle on how to use visit_url() to submit a combat macro, it turns out I'm wrong. For reference, if I avoid "&&" I can make things work, but I'm all manner of stupid when I try to use that. :(
 

xKiv

Active member
I think you should be using "andand" instead of "&&" anyway (and "oror" instead of "||", probably), if you are submitting this way.

also

re: 1) I think the third argument only matters for form fields of relay scripts and setting it to true is probably never what you want to do in ASH, unless you are somehow URL-encoding all values in your form fields yourself (extremely unlikely)
 
Last edited:

StDoodle

Minion
I think you should be using "andand" instead of "&&" anyway (and "oror" instead of "||", probably), if you are submitting this way.

Does this really work? I'm not sure the original report that it does is from a reliable source (me). ;)

1) I think the third argument only matters for form fields of relay scripts and setting it to true is probably never what you want to do in ASH, unless you are somehow URL-encoding all values in your form fields yourself (extremely unlikely)

Well, if I do need to use ampersands in some way, I might... depends on how they're handled. Which is very mysterious to me.
 

xKiv

Active member
Does this really work? I'm not sure the original report that it does is from a reliable source (me). ;)

You then went to test and confirm it. Link: http://forums.kingdomofloathing.com/vb/showpost.php?p=3801862&postcount=592

Well, if I do need to use ampersands in some way, I might... depends on how they're handled. Which is very mysterious to me.

Just use %26 (url-encoded &) instead? Whatever you pass to mafia in the first parameter of visit_url should be passed without any encoding, I think. Otherwise you couldn't use & as request parameter separator.
 

StDoodle

Minion
But I'm not sure I did a proper test for that, is what I'm saying.

So far, I've tried tons of combinations, and I'm still having problems. But I'm not sure how to track down exactly what the problem is.
 

StDoodle

Minion
Ok, here's a somewhat stripped-down version of the script I'm using. The "Shieldbutt" and "Attack" macros, which have no "&&" in them, work fine. The "Debug" macro, however, fails. Oddly, the failure message I see is "Macro Aborted, couldn't find mark named "__while__0end".You twiddle your thumbs." I've tried so many combinations of so many things, but I think I'm ready to give up. If anyone has suggestions, I'd love to hear it. And for the record, I was very much wrong about "ANDAND"; I simply hadn't tested it properly. :(

Edit to add: is there any version of visit_url() that won't url encode my string? It seems to do so no matter what; so I have no idea how to pass along what I need... "%26" is changed to "%2526" etc. There's probably a method I'm missing -- I'm really not strong on this particular subject -- so please let me know if there is.
 

Attachments

  • fight.ash
    8.7 KB · Views: 40
Last edited:

slyz

Developer
I got a debug log due to a replace_all() at the end of the fight, and got thrown back directly to the main map:
Code:
class java.lang.IllegalArgumentException: Illegal group reference
java.lang.IllegalArgumentException: Illegal group reference
	at java.util.regex.Matcher.appendReplacement(Unknown Source)
	at java.util.regex.Matcher.replaceAll(Unknown Source)

The problem seem to be this one in condense_fight_page()
PHP:
 matcher m1 = create_matcher(
  "(<font size=\"?2\"? color=\"?gray\"?>.+?</font></td></tr>(?:<tbody>)?</table>)(.+?)" +
  "(<!--WINWINWIN-->)(.+?)<a (?:href|name)=\"\#end\".+?</a>(.+?)"
  ,page);
 if (m1.find()) { if (m1.group_count() >= 5) {
  page = replace_all(m1,
   m1.group(1) +
   "</td></tr></tbody></table>" +
   "<div style=\"cursor: pointer; cursor: hand; font-family: Arial,Helvetica,sans-serif; font-size: 10pt; width: 110pt; border: solid 2px black; font-weight: bold;\" "+
   "onclick=\"switchDivDisplay('std_collapse','std_clnk');void(0);\" id=\"std_clnk\">Show Macro Actions</div><p>" +
   "<div style=\"display: none;\" id=\"std_collapse\">" + m1.group(2) + "</div>" + m1.group(3) +
   "<center><table><tbody><center><tr><td align=center>" + m1.group(4) +m1.group(5));
  } }

Other than that, it looks great.

EDIT: I ran another combat, and started a debug log before hitting the "shieldbutt" button. Here it is if you want to go through the HTLM to find out what group was missing in the matcher above.

EDIT2: removed the attachement.
 
Last edited:

StDoodle

Minion
Wow... that is... not a small debug log. I don't know where to begin...

Actually, googling the exception leads me to suspect it's a different problem altogether; after all, it shouldn't try to actually run the replacement without having enough groups. Unless I misunderstand how group_count() works. (Which is very possible.)

But anyway, that particular part isn't even necessary for what I'm currently struggling with, which is figuring out how to visit a url with ampersands. Bah.
 

holatuwol

Developer
Yes, they mean something special.

Actually, I just double-checked your code. You're overcomplicating it:

PHP:
matcher m1 = create_matcher( 
  "(<font size=\"?2\"? color=\"?gray\"?>.+?</font></td></tr>(?:<tbody>)?</table>)(.+?)" + 
  "(<!--WINWINWIN-->)(.+?)<a (?:href|name)=\"\#end\".+?</a>(.+?)" 
  ,page); 
 if (m1.find()) { if (m1.group_count() >= 5) { 
  page = replace_all(m1, "$1</td></tr></tbody></table>" + 
   "<div style=\"cursor: pointer; cursor: hand; font-family: Arial,Helvetica,sans-serif; font-size: 10pt; width: 110pt; border: solid 2px black; font-weight: bold;\" "+ 
   "onclick=\"switchDivDisplay('std_collapse','std_clnk');void(0);\" id=\"std_clnk\">Show Macro Actions</div><p>" + 
   "<div style=\"display: none;\" id=\"std_collapse\">$2</div>$3" +
   "<center><table><tbody><center><tr><td align=center>$4$5"); 
  } }
Just use the $ for what they were intended for and you're fine.
 
Last edited:

StDoodle

Minion
Hola; perhaps my bug report is a better place, but doesn't this screw things up on write() vs print()? Am I doing it wrong (entirely possible, I really don't understand all the ins-and-outs here)?
 

holatuwol

Developer
I ninja-edited my post after I re-read your script and I thought to myself, "Wait, why are you doing + m1.group(1) + when you could just do $1". Take a look at the edited post (and also at the bug report) and see if it makes sense to you?

Edit: It also should have no effect on write() vs. print(), though maybe it does, but I need an example to understand what you mean.
 
Last edited:

StDoodle

Minion
Hola; if I use $1... etc. then it doesn't matter, so no worries.

However, my main concern of this thread -- that I'm still completely unable to figure out how to submit a macro via visit_url() that contains double-ampersands, is still haunting me...

If anyone has any ideas, I would <3 hearing them.... please!

Edit to add; for reference, I've been able to occasionally get such macros to run, but they ignore everything after "&&" which to me suggests they become something else, and the rest of that line is ignored. (This was my issue with thinking that "ANDAND" worked, btw.)
 

holatuwol

Developer
Have you tried changing this:
PHP:
page = visit_url("fight.php?action=macro&macrotext=" + make_url(form_field("std_act")),true,true);

To this:
PHP:
page = visit_url("fight.php?action=macro&macrotext=" + url_encode(form_field("std_act")),true,true);

Based on the comments, I believe the 'encode' parameter refers to the form field, rather than the form value (the value will always be assumed to be encoded). So it's the difference between steps[] and whatever the escaped version of that is, and since in ASH you never really need to escape it (unescaped versions are far more legible), I think it's just an extra API access that you'd never use that KoLmafia needs internally for the relay browser to work across browsers.

Edit: Actually, I might be wrong. If you set it to 'false' it might double-encode the value. If that's the case, I'm not sure why we even have that parameter, since people can't provide maps of form data, and in that case, it should always be 'true'.
 
Last edited:

StDoodle

Minion
I've tried just about every conceivable combination of encoding and decoding and use of visit_url() parameters; make_url() was something I came up with after all of those. I've also tried dozens of combinations of "to encode or not to encode" in make_url().

I think what the third parameter does is that it just ignores splitting the string at ampersands, but still encodes it. But there are several big blocks in my knowledge; 1) What exactly is done to my string under various formats and 2) What, if anything, mafia may add to any macro submissions via visit_url().
 
Top