Pattern matching Problem
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Pattern matching Problem

Started by Stravides, 14 May, 2004, 05:31:46

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Stravides

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 ....
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

tezlo

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

Stravides

#2
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
Stravides
For RPG Books, Mp3 & Videos
We host trivia  and the ever failing Smeagolbot

SMF spam blocked by CleanTalk