PtokaX forum

Development Section => Your Developing Problems => Topic started by: Stravides on 14 May, 2004, 05:31:46

Title: Pattern matching Problem
Post by: Stravides 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
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 ....
Title:
Post by: tezlo on 14 May, 2004, 06:12:28
would this help?
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
Title:
Post by: Stravides 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.

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