News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Dice Roller

Started by Colin, July 30, 2009, 09:52:06 PM

Previous topic - Next topic

markem

I have a mod for your script:

Search for:

function dicemod($string) {
    global $txt;
    $output = '[blockquote]';


After the $output line put:

    $string[1] = preg_replace( "/\s*/", "", $string[1] );


This handles miscellaneous spaces being put into the string.

Before - if you put in "1 d 8 + 5" it would say it is an invalid dice string.

After - it just does the die roll.


Arantor

Thanks but considering the support for other things, I can't add this. From the examples:

[roll]{Uberdamage} 15d100+10^10v90<80>20[/roll]

This would be adversely affected by the suggested change.

markem

Er.....I just tried it and it worked without any problems.

The only thing that mod does is to remove any whitespace.


Rolled 5d8+3*5 : 8, 12, 8, 13, 7 + 3, total 51


Uberdamage 15d100+10^10v90<80>20 : 92 98 8 95 35, 69, 92 96 72, 45, 59, 16, 40, 45, 100 57, 57, 8 31, 51, 2 58, 13, 77 + 10, total 706


markem

I just finished a new MOD.  I added in the star(*) mod.  What the star mod does is to globally add/subtract from the dice roll AFTER everything else is done.  If you would like to see the mod I can post it.

Arantor

Except there's whitespace between the } and the start of the diceroll itself, yes?

*shrug* I'm not maintaining this mod any longer, it's been years since I originally wrote it so I don't really remember how it works any more, you'd have to ask the current maintainer if they were interested in adding it.

markem

Here is: "{uberdamage}                         15                       d             100          +          10            ^      10         v     90       <            80       >    20"

Hmmmmm......  I got:

'{uberdamage}            15           d      100     +     10      ^   10    v  90   <      80   >  20' is not a valid dice string!

This means that there is somewhere else I need to look because everything from the 15 on should have had the whitespace removed.

markem

Since I have your attention - a quick (offtopic) question - why does SMF knock you completely out of the posting area if you put in a new post?  Can I make it stop doing that easily?  (I run a new board)  TIA!

Arantor

QuoteThis means that there is somewhere else I need to look because everything from the 15 on should have had the whitespace removed.

Everyone loves regular expressions.

You might have had more luck simply using str_replace with an array of ("\t", " ") being replaced to "" but that's always a bit iffy too given SMF's preparser if you want to start playing with things like \n as well because they have special meaning in SMF and get preparsed out into something else.

As for your other question, that's covered in the wiki, forget exactly where.

markem

The "\s" covers any type of whitespace so it would grab spaces, tabs, newlines, linefeeds, and all other items between 00 and 32 as well as 128+ characters that are whitespace as well.  That's why I used it.  The fault doesn't line in the usage of the preg_replace() function.  I have put in some code to dump out the $thisdiceroll variable so I know what is going where.  The thing is - with the {uberdamage} the dicemod function doesn't go into the same area as before.  With {uberdamage} at the front the count returned zero.  Still looking.....

Arantor

Yes, I know what it does. (Amongst other things, I am a Zend Certified Engineer as well as a former SMF core developer. I do know what \s in a regex means. I also know the difference between using single and double quotes in this context means, and it's more surprising than you might think. Single quotes recommended for regex work with metacharacters.) Except it doesn't work the way you think it does. The * will match empty strings too and the greedy preferences will mean it won't work the way it does, and will only replace some of them. \s+ would probably be recommended because if nothing else the PCRE engine has to do less backtracking in that situation.

It also will NOT be touching most characters above 128 since that's where all extended UTF-8 characters are and you generally need to be passing the u parameter to it for that in the first place to be binary safe bearing in mind that most installations are ISO-8859-1 (or maybe -2 but usually -1) which means almost everything after 128 has a direct meaning, especially 160 which is a non breaking space in ISO encoding.

markem

Yes - and I found out what was wrong with the script.

The preg_replace should really be:

    $string[1] = preg_replace( "/\s*|&nbsp;/", "", $string[1] );

I did a hex dump and was like "Say what?" on the resultant hex string.  SMF puts in &nbsp;.  But the {uberdamage} still doesn't have a problem with there not being a space after the trailing brace.

Example:

String[0] = [roll]{uberdamage}                         15                       d             100          +          10            ^      10         v     90       <            80       >    20[/roll]

String[1] = {uberdamage}15d100+10^10v90<80>20

String[0] = 5b726f6c6c5d7b7562657264616d6167657d266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20203135266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b202064266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b2020313030266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b202b266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b203130266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b205e266e6273703b20266e6273703b20266e6273703b203130266e6273703b20266e6273703b20266e6273703b20266e6273703b202076266e6273703b20266e6273703b20203930266e6273703b20266e6273703b20266e6273703b2020266c743b266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b20266e6273703b203830266e6273703b20266e6273703b20266e6273703b20202667743b266e6273703b20266e6273703b2032305b2f726f6c6c5d

String[1] = 7b7562657264616d6167657d3135643130302b31305e3130763930266c743b38302667743b3230

Count = 1

uberdamage 15d100+10^10v90<80>20 : thisdiceroll[0][0] = {uberdamage}15d100+10^10v90<80>20

thisdiceroll[1][0] = {uberdamage}

thisdiceroll[2][0] = uberdamage

thisdiceroll[3][0] = 15

thisdiceroll[4][0] = 100

thisdiceroll[5][0] = +10

thisdiceroll[6][0] = ^10

thisdiceroll[7][0] = v90

thisdiceroll[8][0] = <80

thisdiceroll[9][0] = >20

thisdiceroll[10][0] =

thisdiceroll[11][0] =

thisdiceroll[12][0] =

44, 19, 31, 56, 7 63, 91 3 30, 100 26, 49, 77, 37, 57, 67, 69, 10 25, 14 + 10, total 641

So now the mod works for all occurances - unless you know of another one that might not work?  :-)

markem

Yes - you are right.  I probably should have used the one or more and not the zero or more.  :-)

Arantor

Ah, I see, I wasn't sure quite where inside parsing this would occur in which case the &nbsp is indeed inserted by SMF to preserve spacing. It's one of the more interesting parts of SMF. I'd assumed this was prior to that part of preparsecode though (in which case there are still situations where * won't match everything)

In fact your example is the *very* one I was alluding to. There's now no space after Uberdamage in the output.

markem

Correct - but it still worked.  You did see the last line did you not?  Also, it would be easy (if the space were needed) to re-insert it after the brace.

Arantor

*shrug* It's literally been 5 years since I looked at the code and I feel like I shouldn't have said anything anyway.

markem

You must have a different version than what I have.  After looking through the code in Subs-Post.php - there is no test of uberdamage or anything else in braces.  My version is 2.0.8.

Btw : Why not?  If you hadn't said anything I would not have thought to look for the &nbsp; in the string.  Ya know - so thanks!  :-)

Teh Lizzeh

In version 2.0.9 the mod changes from [roll] to [blockquote] whenever a roll is previewed, this did not happen before. I realise that 2.0.9 is so new and that it hasn't been modified to fit that version yet but I was wondering if there's either an easy fix or if the mod will be updated soon?

Colin

Yep, I'll look into this.
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Teh Lizzeh


Arantor

Quotethe mod changes from [roll] to [blockquote] whenever a roll is previewed, this did not happen before.

This *always* happened. This was always absolutely intentional and has done this for the last five years ever since it was written as an anti-tamper mechanism.

Advertisement: