@chendbdb
2017-07-10T20:00:59.000000Z
字数 6306
阅读 3908
XXTouch
基于Lua与XXTouch,制作便于开发“游戏”、“app”等批操作的简单框架。
_Do拟人判断方式以“多点满足条件则”方法尽心构架,处理操作流程使用keep方式超快速进行遍历所有条件。
此框架并不一定适用于所有操作。
| 名 | 参数 |
|---|---|
| Name | 为框架名 |
| Repeat | 为重复检测到后处理方式 |
| n(Repeat) | 为重复检测到时常满足 n秒内 每次都能找到 |
| Run(Repeat) | 为重复检测到后的处理方式 |
| timeout | 超时任何东西未检测到后处理方式 |
| n(timeout) | 在这段时间内任何东西没有找到则按照超时处理 |
| Run(timeout) | 处理方式 |
| Begin | 开启层前处理 |
| End | 跳出层时处理 |
| sleep | 每次处理完一次循环逻辑 等待时常 |
| csim | 全局相似度 |
Test = {Name = '第一层',{Name = '检测1',Color = {{619,110,0x334455}},Run = (function(et) touch.tap(100,100) end)},{Name = '检测2',Color = {{619,110,0xffffff}},Run = (function(et) return true --[[跳出]] end),---[[--------------- 当处理完测试2后激活第二层layer = {Name = '第二层',{Name = '检测3',Color = {{619,110,0x334455}},Run = (function(et) touch.tap(100,100) end),},Repeat = {n = 3,Run = (function(et) --[[重复]] end),},timeout = {n = 60,Run = (function(et) --[[超时]] end),},Begin = (function() end),End = (function() end),sleep = 800,csim = 90},---]]---------------csim = 90,},Repeat = {n = 3,Run = (function(et) --[[重复]] end),},timeout = {n = 60,Run = (function(et) --[[超时]] end),},Begin = (function() end),End = (function() end),sleep = 800,csim = 90}
| 名 | 参数 |
|---|---|
| Name | 子表名 |
| Color | 检测颜色表是否满足 |
| Run | 处理方式 |
| et | 当满足时传入字表的对象 |
{Name = '检测1',Color = {{619,110,0x334455}},Run = (function(et) touch.tap(100,100) end)},
可传入两种内容
--传入函数功能_Do(Test,nLog)--或_Do(Test,function(t_name, message)sys.log(t_name .. " " .. message)end)_Do(Test,true) --默认使用nLog方式
local _Do = function(_tab, b_debug)-- 判断是否传入的是正确内容local check_tablecheck_table = function(t)if type(t) ~= 'table' then error('传入非table内容',3) endif #t == 0 then error('传入table无数据',3) endif not (type(t.csim) == 'number' or type(t.csim) == 'nil') then error('相似度数值不正确',3) endif not ((type(t.Repeat) == 'table' andtype(t.Repeat.n) == 'number' andtype(t.Repeat.Run) == 'function') ortype(t.Repeat) == 'nil') thenerror('Repeat 参数不正确',3)endif not ((type(t.timeout) == 'table' andtype(t.timeout.n) == 'number' andtype(t.timeout.Run) == 'function') ortype(t.timeout) == 'nil') thenerror('timeout 参数不正确',3)endfor n, v in ipairs(t) doif not (type(v.Name) == 'string' andtype(v.Color) == 'table' andtype(v.Run) == 'function' and(type(v.csim) == 'number' or type(v.csim) == 'nil') and(type(v.layer) == 'table' or type(v.layer) == 'nil')) thenerror(string.format("传入table第%d项不正确",n),3)elsefor _n, _v in ipairs(v.Color) doif not (type(_v[1]) == 'number' andtype(_v[2]) == 'number' andtype(_v[3]) == 'number') thenerror(string.format("传入table第%d项内点色第%d项不正确",n,_n),3)endendif type(v.layer) == 'table' thencheck_table(v.layer)endendendendcheck_table(_tab)local _log = function() endif type(b_debug) == "function" then_log = b_debugelseif b_debug then_log = function(...) nLog(...) endelseend-- 对全局 DoName 进行赋值 当前处理的 框架if type(_G.DoName) == "table" thentable.insert(DoName,_tab.Name)else_G.DoName = {_tab.Name}endif type(_tab.Begin) == "function" then_tab.Begin()endlocal _now = os.time()local _break = falselocal _Repeat = false-- 初始化 重复 计时值for _key,_value in ipairs(_tab) do_value.RepeatTime = os.time()endwhile true do--满足状态的 key 会缓存至这里local _States = {}--寻找满足状态的项screen.keep()for _key,_value in ipairs(_tab) doif screen.is_colors(_value.Color, _value.csim or _tab.csim) then_log(_value.Name, "发现")table.insert(_States,_key)--检测重复if _tab.Repeat thenif os.time() - _value.RepeatTime >= _tab.Repeat.n then_value.RepeatTime = os.time()if not _value.NoRepeat then_log(_value.Name, "重复")_Repeat = trueendendendelse_value.RepeatTime = os.time()endendscreen.unkeep()--超时判断if #_States == 0 thenif _tab.timeout thenif os.time() - _now >= _tab.timeout.n then_log(_tab.Name, "超时")if _tab.timeout.Run(_tab.timeout) thenbreakendif _tab.timeout.layer then_Do(_tab.timeout.layer)end_now = os.time()endendelse--执行满足状态的项for _key,_value in ipairs(_States) dolocal _Handle = _tab[_value]_log(_Handle.Name, "执行")if _Handle.Run thenif _Handle.Run(_Handle) then_break = trueendendif _Handle.layer then_Do(_Handle.layer)end_now = os.time()endif _break then break endif _Repeat thenif _tab.Repeat.Run(_tab.Repeat) thenbreakend_Repeat=falseendendsys.msleep(_tab.sleep or 500)endif type(_tab.End) == "function" then_tab.End()end-- 对全局 DoName 进行剔除 已处理的 框架if type(_G.DoName) == 'table' thenif #(_G.DoName) > 0 thentable.remove(_G.DoName,#DoName)endendend
local _Do = function(_tab, b_debug) local check_table check_table = function(t) if type(t) ~= 'table' then error('传入非table内容',3) end if #t == 0 then error('传入table无数据',3) end if not (type(t.csim) == 'number' or type(t.csim) == 'nil') then error('相似度数值不正确',3) end if not ( ( type(t.Repeat) == 'table' and type(t.Repeat.n) == 'number' and type(t.Repeat.Run) == 'function' ) or type(t.Repeat) == 'nil' ) then error('Repeat 参数不正确',3) end if not ( ( type(t.timeout) == 'table' and type(t.timeout.n) == 'number' and type(t.timeout.Run) == 'function' ) or type(t.timeout) == 'nil' ) then error('timeout 参数不正确',3) end for n, v in ipairs(t) do if not ( type(v.Name) == 'string' and type(v.Color) == 'table' and type(v.Run) == 'function' and (type(v.csim) == 'number' or type(v.csim) == 'nil') and (type(v.layer) == 'table' or type(v.layer) == 'nil') ) then error(string.format("传入table第%d项不正确",n),3) else for _n, _v in ipairs(v.Color) do if not ( type(_v[1]) == 'number' and type(_v[2]) == 'number' and type(_v[3]) == 'number' ) then error(string.format("传入table第%d项内点色第%d项不正确",n,_n),3) end end if type(v.layer) == 'table' then check_table(v.layer) end end end end check_table(_tab) local _log = function() end if type(b_debug) == "function" then _log = b_debug elseif b_debug then _log = function(...) nLog(...) end else end if type(_G.DoName) == "table" then table.insert(DoName,_tab.Name) else _G.DoName = {_tab.Name} end if type(_tab.Begin) == "function" then _tab.Begin() end local _now = os.time() local _break = false local _Repeat = false for _key,_value in ipairs(_tab) do _value.RepeatTime = os.time() end while true do local _States = {} screen.keep() for _key,_value in ipairs(_tab) do if screen.is_colors(_value.Color, _value.csim or _tab.csim) then _log(_value.Name, "发现") table.insert(_States,_key) if _tab.Repeat then if os.time() - _value.RepeatTime >= _tab.Repeat.n then _value.RepeatTime = os.time() if not _value.NoRepeat then _log(_value.Name, "重复") _Repeat = true end end end else _value.RepeatTime = os.time() end end screen.unkeep() if #_States == 0 then if _tab.timeout then if os.time() - _now >= _tab.timeout.n then _log(_tab.Name, "超时") if _tab.timeout.Run(_tab.timeout) then break end if _tab.timeout.layer then _Do(_tab.timeout.layer) end _now = os.time() end end else for _key,_value in ipairs(_States) do local _Handle = _tab[_value] _log(_Handle.Name, "执行") if _Handle.Run then if _Handle.Run(_Handle) then _break = true end end if _Handle.layer then _Do(_Handle.layer) end _now = os.time() end if _break then break end if _Repeat then if _tab.Repeat.Run(_tab.Repeat) then break end _Repeat=false end end sys.msleep(_tab.sleep or 500) end if type(_tab.End) == "function" then _tab.End() end if type(_G.DoName) == 'table' then if #(_G.DoName) > 0 then table.remove(_G.DoName,#DoName) end end end