|
楼主 |
发表于 2015-11-24 07:36:04
|
显示全部楼层
本帖最后由 oolong 于 2015-11-28 06:14 PM 编辑
回复 4# ffdm
--alias.lua 模仿zmud 命令解析器#alias
var={}--变量表var["xxx"]="yyy"
alias = alias or {}--alias 表
--1)执行客户端送来指令cmd
mb.client_aliases=function(cmd)
if var["debug"] and var["debug"]==1 then
echo("\n"..C.y.."输入指令:"..cmd)
end
excute(cmd)
return true
end
--2)增加alias
function add_alias(cmd,alias_function)
alias[cmd]=alias_function
end
--3)删除alias
function del_alias(cmd)
alias[cmd]=nil
end
--4)执行cmud
function excute_alias(cmd)
if string.match(cmd,"(%S+)") and string.match(cmd,"(%S+)")~="" and alias[string.match(cmd,"(%S+)")] then
local i=0
local params={"","","","","","","","","",""}
for match in string.gmatch(cmd,"(%S+)") do
params=expand(match)
i=i+1
end
if alias[params[0]] then
params[-1] = string.match(cmd,"^"..params[0].." (.+)") if params[-1] then params[-1]= expand(params[-1]) end
alias[params[0]](params)
end
else
if var["debug"] and var["debug"]==1 then
echo(C.y..cmd)
end
send(expand(cmd))
end
end
--5)分解解析alias指令by";"
function excute(cmd)
for match in string.gmatch(cmd..";","(.-);") do
if string.match(match,"(#%d+)%s-.+") then
local times=string.match(match,"#(%d+)%s.+")
local action=string.match(match,"#%d+%s(.+)")
for i=1,times do
excute_alias(action)
end
else
excute_alias(match)
end
end
end
--6)展开变量by @
function expand(cmd)
if string.match(cmd,"@(%S+)") then
cmd=string.gsub(cmd,"@(%S+)",var)
return cmd
elseif string.match(cmd,"%$(%S+)") then
cmd=string.gsub(cmd,"%$(%S+)",var)
return cmd
else
return cmd
end
end
----例子
--[[
add_alias("aa",function(params)
var["aa"]="xixi"
local test=params[-1] or "全部参数"
local test2=params[0]
local test3=params[1] or "参数1"
send("say 参数0 alias: "..test2)
send("say 参数-1全部: "..test)
send("say 参数1第一个: "..test3)
excute("e;@aa;w;look")
echo("\n"..C.x.."测试alias aa完毕")
end)
]]
--定义几个alias自己用 查看trigger var alias
--listt listv lista
add_alias("listt",function(params)
echo("\n"..C.x.."以下触发trigger")
for k,v in pairs(trig_rex) do
echo(C.g.."#trigger id: "..k.." == rex: "..v)
end
echo(C.x.."以上触发trigger")
end)
add_alias("listv",function(params)
echo("\n"..C.x.."以下变量var")
for k,v in pairs(var) do
echo(C.c..'#var "'..k..'" == "'..tostring(v)..'"')
end
echo(C.x.."以上变量var")
end)
add_alias("lista",function(params)
echo("\n"..C.x.."以下别名alias")
for k,v in pairs(alias) do
echo(C.y..'#alias "'..k..'" == "'..tostring(v)..'"')
end
echo(C.x.."以上别名alias")
end) |
|