Author Topic: Pattern matching Problem  (Read 1407 times)

0 Members and 1 Guest are viewing this topic.

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
Pattern matching Problem
« on: 14 May, 2004, 05:31:46 »
I've taken a brief look at the pattern matching how to, and I'm still quite confused :S

I have a snippet of code here
Code: [Select]
if (cmd == prefix.."roll") then
local s,e,cmd,qty,type = strfind( data, "%b<>%s+(%S+)%s+(%d+)%s+(.*)" )
local subtot=0
local scoretable={}
if  tonumber(qty) > tonumber(maxdiceroll) then
SendToAll(BOTName,user.sName.." Is trying to crash the server rolling "..qty.." dice - Max "..maxdiceroll.." Please")
else
for i = 1, qty do
randomgen=dice(type)
subtot=subtot + randomgen
SendToAll(BOTName,user.sName.." Roll Number "..i.." was "..randomgen)
end
SendToAll(BOTName,user.sName.." Rolled "..qty.."d"..type.." and got "..subtot)
return 1
end

this basicall accepts the input !roll 3 6

What I want to search for is !roll 3d6
or even better !roll 3d6+1
or !roll 3d6-2

What should I be looking to use for strfind

Many thanks in advance
Strav ....
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

PtokaX forum

Pattern matching Problem
« on: 14 May, 2004, 05:31:46 »

Offline tezlo

  • Triple Ace
  • **
  • Posts: 225
  • Karma: +1/-0
(No subject)
« Reply #1 on: 14 May, 2004, 06:12:28 »
would this help?
Code: [Select]
local s, e, throws, faces, sign, n = strfind("2d12+4", "(%d+)d(%d+)([%+%-]?)(%d*)")

local value = 0
faces = tonumber(faces)
for i = 1, tonumber(throws) do
value = value + random(faces)
end

n = tonumber(n) or 0
if sign == "+" then
value = value + n
elseif sign == "-" then
value = value - n
end

Offline Stravides

  • Triple Ace
  • **
  • Posts: 171
  • Karma: +0/-0
(No subject)
« Reply #2 on: 14 May, 2004, 15:35:46 »
Thanks that was excellent, I managed to incorporate it into the initial code and have included it should it be required by other RPG or dice rolling requirements.

Code: [Select]
elseif (cmd == prefix.."roll") then
local s,e,cmd,qty,type,sign,n = strfind( data, "%b<>%s+(%S+)%s+(%d+)d(%d+)([%+%-]?)(%d*)" )
if  tonumber(qty) > tonumber(maxdiceroll) then
SendToAll(BOTName,user.sName.." Is trying to crash the server rolling "..qty.." dice - Max "..maxdiceroll.." Please")
else
local subtot=0
local scoretable={}
local value = 0
type = tonumber(type)
for i = 1, tonumber(qty) do
randomgen=dice(type)
subtot=subtot + randomgen
SendToAll(BOTName,user.sName.." Roll Number "..i.." was "..randomgen)
end
num = tonumber(n) or 0
if sign == "+" then
subtot = subtot + num
elseif sign == "-" then
subtot = subtot - num
end
SendToAll(BOTName,user.sName.." Rolled "..qty.."d"..type..sign..n.." and got "..subtot)
return 1
end


Output is

Stravides Roll Number 1 was 6
Stravides Roll Number 2 was 6
Stravides Rolled 2d8+7 and got 19

« Last Edit: 14 May, 2004, 15:36:57 by Stravides »
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

PtokaX forum

(No subject)
« Reply #2 on: 14 May, 2004, 15:35:46 »