PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: Corayzon on 19 June, 2004, 04:52:53

Title: My Art =]
Post by: Corayzon on 19 June, 2004, 04:52:53
cslave 1.0 DataArrival function

function DataArrival(tUser, sData)

local _,_, sCmd, sArgs = strfind(sData, "(%S+)%s*(.*)|")
-- SendToAll(tUser.sName .. " -> " .. (sCmd or "none") .. " - " .. (sArgs or "none") .. "|") -- debug line for testing

if tSysCommands[sCmd] ~= nil and strsub(sData, 1, strlen(sBot) + 5) ~= "$To: " .. sBot then
return tSysCommands[sCmd]["Func"](tUser, sArgs)

elseif strsub(sData, 1, strlen(tUser.sName) + 2) == "<" .. tUser.sName .. ">" or strsub(sData, 1, strlen(sBot) + 5) == "$To: " .. sBot then
local _,_, sMessage = strfind(sData, "%b<>%s+(.*)|")
if tPrefixs[strsub(sMessage, 1, 1)] == 1 then
sCmd = getCommand(sMessage)
if tCommands[sCmd] then
if getUserLevel(tUser) <= tCommands[sCmd].iLevel then
local tArgs = buildArgTable(sMessage, tCommands[sCmd].iArgs)
local iReturn = tCommands[sCmd]["Func"](tUser, tArgs)
if (tCommands[sCmd].bReturn or iReturn) and (tCommands[sCmd].bReturn ~= 0) then return 1 else return end
else
return 1
end
else
return 1 -- :: Hide incorrect commands
end
else
return  -- :: Let through hub chat
end
else
tUser:Disconnect() -- :: Kill user on bad send (hacked or spammer client)
end
end

noza
Title:
Post by: NotRabidWombat on 19 June, 2004, 08:57:09
"if sData == "<" .. tUser.sName .. "> !!|" then tUser:SendPM(sBot, "Welcome to cSlave 1.0") end"

Why can't "!!" be part of tCommands?

"SendToAll(tUser.sName .. " -> " .. (sCmd or "none") .. " - " .. (sArgs or "none") .. "|")"

Probably want to comment the debug line ;-)

"local iReturn = tSysCommands[sCmd]["Func"](tUser, sArgs)
if iReturn then return 1 else return end"

You can return another functions return, so this becomes:
return tSysCommands[sCmd]["Func"](tUser, sArgs);

Other than that this is an excellent example of how a DataArrival should look like :-)

-NotRabidWombat
Title:
Post by: Corayzon on 19 June, 2004, 09:46:02
*** points taken and updated to above
Title:
Post by: NotRabidWombat on 19 June, 2004, 15:36:37
If you change:
local _,_, sCmd, sArgs = strfind(sData, "(%S+) (.*)")
to
local _,_, sCmd, sArgs = strfind(sData, "(%S+)%s*(.*)|")

You *should* no longer need:
if sCmd == nil then _,_, sCmd = strfind(sData, "(.*)|") end

You could also use: (%S+)%s?(.*)|
? - means 0 or 1 occurences
Or: ^(%S+)%s*(.*)|$
^ - anchor to beginning of string
$ - anchor to end of string
These may or may not improve the efficiency of the regex since it will understand you are trying to break up the whole string.

-NotRabidWombat
Title:
Post by: Corayzon on 20 June, 2004, 03:19:23
NotRabidWombat, allways here to set my strait =], what would i do without ya, ehehe

*** updated to above

noza