local _,_,cmd=strfind(data, "%b<>%s+(%S+).+")
local User = curUser.sName
local _,_,junk=strfind(data, "%b<>%s+%S+%s+(%S+)")
if cmd == "!attack" then
local answer,victim = Attacks[random(getn(Attacks))], junk
answer = gsub( answer, "(%[CURUSER%])", User )
answer = gsub( answer, "(%[VIC%])", victim )
SendToAll( answer.."|" )
end
can any one make this code, so the user only can write a name that is online?
and that is sends a text to the user that he wrote wrong name... and the user is not online...
This is a nice opportunity to see how code can shrink ...
and how usefull or disastrous it can be
Here we go ..
part 1 : a very spread out thing ... local _,_,cmd = strfind(data, "%b<>%s+(%S+)")
local _,_,otherUser = strfind( data, "%b<>%s+%S+%s+(%S+)")
if cmd == "!attack" then
local isUser = GetItemByName(otherUser)
if (isUser) then
local answer = Attacks[random(getn(Attacks))]
answer = gsub( answer, "(%[CURUSER%])", curUser.sName )
answer = gsub( answer, "(%[VIC%])", otherUser )
SendToAll( answer )
end
end
part2 : lets cut some stuff ...
this is fairly ok .. but it can go further .. local _,_,cmd, otherUser = strfind(data, "%b<>%s+(%S+)%s+(%S+)")
if cmd == "!attack" then
local otherUser = GetItemByName(otherUser)
if (otherUser) then
local answer = Attacks[random(getn(Attacks))]
answer = gsub( answer, "(%[CURUSER%])", curUser.sName )
answer = gsub( answer, "(%[VIC%])", otherUser.sName )
SendToAll( answer )
end
end
part 3 : Lets cut some more ...
making it in effect readable by anyone with a little knowledge ... local _,_,cmd, otherUser = strfind(data, "%b<>%s+(%S+)%s+(%S+)")
if cmd == "!attack" then
local otherUser = GetItemByName(otherUser)
if (otherUser) then
local answer = Attacks[random(getn(Attacks))]
answer = gsub( answer, "(%[CURUSER%])", curUser.sName )
SendToAll( gsub( answer, "(%[VIC%])", otherUser.sName ))
end
end
part 4: now it is getting even shorter ..
this is comfy for most scripters .. although it is not read without effort as the above .. local _,_,cmd, otherUser = strfind(data, "%b<>%s+(%S+)%s+(%S+)")
if cmd == "!attack" then
local otherUser = GetItemByName(otherUser)
if (otherUser) then
local answer = gsub( Attacks[random(getn(Attacks))], "(%[CURUSER%])", curUser.sName )
SendToAll( gsub( answer, "(%[VIC%])", otherUser.sName ))
end
end
part 5 : this is not recommended unless you feel comfortable with it ...
it is not easy to maintain ... local _,_,cmd, otherUser = strfind(data, "%b<>%s+(%S+)%s+(%S+)")
if cmd == "!attack" then
local otherUser = GetItemByName(otherUser)
if (otherUser) then
SendToAll( gsub( gsub( Attacks[random(getn(Attacks))], "(%[CURUSER%])", curUser.sName ), "(%[VIC%])", otherUser.sName ))
end
end
Thanks, that helped alot =)