Something sort of like:
Code:
int dice = 1;
matcher m = create_matcher( "roll [URL="file://\\d"]\\d[/URL]+ di([c]?)e", message.to_lower_case());
if( m.find() ) dice = m.group( 1 ).to_int();
else
{
m = create_matcher( "roll di([c]?)e \\d+", message.to_lower_case());
if( m.find() ) dice = m.group( 2 ).to_int();
}
That will detect either "roll <number> dice" or "roll dice <number" in the message, pull the number of dice, and set it as the amount to display...
Generally, you wouldn't do it both ways... but you might want to do "dice" and "die" (the proper singular for one dice) depending on how likely you think your gamblers will be aware of proper grammar.
Edit: Since it amuses me, here's a bit of the workings to generate the proper syntax for sending punctuated dice results:
Code:
int rolled = 0;
buffer sending.append(sender + " just rolled a ");
while (rolled < dice)
{
if (rolled == 1 && dice == 2)
sending.append(" and ");
else if (rolled > 0 && dice == rolled + 1)
sending.append(", and ");
else if (rolled > 0)
sending.append(", ");
sending.append((random(6) + 1);
rolled += 1;
}
sending.append(" on " + dice);
if (dice == 1) sending.append("die.");
else sending.append("dice.");
I also varied the matching code above... this should check for 0 or 1 'c' in the word dice/die. You'll notice that having added a new variable, it now matches on the second group on the lower item. If it's easier for you to read, instead doing "di([c]?)e" you should also be able to do "(dice|die)" to match either dice or die. I just prefer the first for some somewhat bizarre reason.
Edit2: In the case of complicated dice, you could do a matcher like:
Code:
int dice = 1;
int sides = 6;
matcher m = create_matcher( "roll [URL="file://\\d+d\\d"]\\d+d\\d[/URL]+ di([c]?)e", message.to_lower_case());
if( m.find() ){
dice = m.group( 1 ).to_int();
sides = m.group( 2 ).to_int();
}
else
{
m = create_matcher( "roll di([c]?)e \\d+d\\d+", message.to_lower_case());
if( m.find() )
{
dice = m.group( 2 ).to_int();
sides = m.group( 3 ).to_int();
}
}
With that, you'd want to run random(sides) instead of random(6) when rolling your dice... And when giving the message about how many dice they rolled, I'd change this line:
sending.append(" on " + dice);
to this:
Code:
sending.append(" on " + dice + " " + sides + "-sided ");
If it works properly, it should return a clan chat of:
Theraze just rolled a 2, 5, and 1 on 3 6-sided dice.
Edit3: Fixed singular/pluralization. Don't grammar tired, it's bad. Also, I'd probably throw a check aborting the rolls if sides < 2 (possibly with a mocking comment on throwing balls, as that would be a 1 sided object)... and possibly calling 2 sided dice coins. If sides > 50 or something else rather large, because it ceases to be useful in dice calculations... though it may be useful for your own random number considerations. Just depends on how useful you want to keep it. Same on the number of dice thrown... if they aren't throwing at least 1 die, abort. If they're throwing over 20 or so, they can do it in multiple throws. Just don't want it to overwhelm the single message length capacity...