[关闭]
@chendbdb 2017-07-10T20:00:59.000000Z 字数 6306 阅读 3423

_Do 框架

XXTouch


_Do 简介

基于Lua与XXTouch,制作便于开发“游戏”、“app”等批操作的简单框架。
_Do拟人判断方式以“多点满足条件则”方法尽心构架,处理操作流程使用keep方式超快速进行遍历所有条件。
此框架并不一定适用于所有操作。


传入参数(_tab)

参数
Name 为框架名
Repeat 为重复检测到后处理方式
n(Repeat) 为重复检测到时常满足 n秒内 每次都能找到
Run(Repeat) 为重复检测到后的处理方式
timeout 超时任何东西未检测到后处理方式
n(timeout) 在这段时间内任何东西没有找到则按照超时处理
Run(timeout) 处理方式
Begin 开启层前处理
End 跳出层时处理
sleep 每次处理完一次循环逻辑 等待时常
csim 全局相似度

母表示例

  1. Test = {
  2. Name = '第一层',
  3. {
  4. Name = '检测1',
  5. Color = {{619,110,0x334455}},
  6. Run = (function(et) touch.tap(100,100) end)
  7. },
  8. {
  9. Name = '检测2',Color = {{619,110,0xffffff}},
  10. Run = (function(et) return true --[[跳出]] end),
  11. ---[[--------------- 当处理完测试2后激活第二层
  12. layer = {
  13. Name = '第二层',
  14. {
  15. Name = '检测3',
  16. Color = {{619,110,0x334455}},
  17. Run = (function(et) touch.tap(100,100) end),
  18. },
  19. Repeat = {
  20. n = 3,
  21. Run = (function(et) --[[重复]] end),
  22. },
  23. timeout = {
  24. n = 60,
  25. Run = (function(et) --[[超时]] end),
  26. },
  27. Begin = (function() end),
  28. End = (function() end),
  29. sleep = 800,
  30. csim = 90
  31. },
  32. ---]]---------------
  33. csim = 90,
  34. },
  35. Repeat = {
  36. n = 3,
  37. Run = (function(et) --[[重复]] end),
  38. },
  39. timeout = {
  40. n = 60,
  41. Run = (function(et) --[[超时]] end),
  42. },
  43. Begin = (function() end),
  44. End = (function() end),
  45. sleep = 800,
  46. csim = 90
  47. }

子表示例

参数
Name 子表名
Color 检测颜色表是否满足
Run 处理方式
et 当满足时传入字表的对象
  1. {
  2. Name = '检测1',
  3. Color = {{619,110,0x334455}},
  4. Run = (function(et) touch.tap(100,100) end)
  5. },

传入参数(b_debug)

可传入两种内容

  1. --传入函数功能
  2. _Do(Test,nLog)
  3. --或
  4. _Do(Test,function(t_name, message)
  5. sys.log(t_name .. " " .. message)
  6. end)
  7. _Do(Test,true) --默认使用nLog方式

_Do 实现结构

  1. local _Do = function(_tab, b_debug)
  2. -- 判断是否传入的是正确内容
  3. local check_table
  4. check_table = function(t)
  5. if type(t) ~= 'table' then error('传入非table内容',3) end
  6. if #t == 0 then error('传入table无数据',3) end
  7. if not (type(t.csim) == 'number' or type(t.csim) == 'nil') then error('相似度数值不正确',3) end
  8. if not (
  9. (
  10. type(t.Repeat) == 'table' and
  11. type(t.Repeat.n) == 'number' and
  12. type(t.Repeat.Run) == 'function'
  13. ) or
  14. type(t.Repeat) == 'nil'
  15. ) then
  16. error('Repeat 参数不正确',3)
  17. end
  18. if not (
  19. (
  20. type(t.timeout) == 'table' and
  21. type(t.timeout.n) == 'number' and
  22. type(t.timeout.Run) == 'function'
  23. ) or
  24. type(t.timeout) == 'nil'
  25. ) then
  26. error('timeout 参数不正确',3)
  27. end
  28. for n, v in ipairs(t) do
  29. if not (
  30. type(v.Name) == 'string' and
  31. type(v.Color) == 'table' and
  32. type(v.Run) == 'function' and
  33. (type(v.csim) == 'number' or type(v.csim) == 'nil') and
  34. (type(v.layer) == 'table' or type(v.layer) == 'nil')
  35. ) then
  36. error(string.format("传入table第%d项不正确",n),3)
  37. else
  38. for _n, _v in ipairs(v.Color) do
  39. if not (
  40. type(_v[1]) == 'number' and
  41. type(_v[2]) == 'number' and
  42. type(_v[3]) == 'number'
  43. ) then
  44. error(string.format("传入table第%d项内点色第%d项不正确",n,_n),3)
  45. end
  46. end
  47. if type(v.layer) == 'table' then
  48. check_table(v.layer)
  49. end
  50. end
  51. end
  52. end
  53. check_table(_tab)
  54. local _log = function() end
  55. if type(b_debug) == "function" then
  56. _log = b_debug
  57. elseif b_debug then
  58. _log = function(...) nLog(...) end
  59. else
  60. end
  61. -- 对全局 DoName 进行赋值 当前处理的 框架
  62. if type(_G.DoName) == "table" then
  63. table.insert(DoName,_tab.Name)
  64. else
  65. _G.DoName = {_tab.Name}
  66. end
  67. if type(_tab.Begin) == "function" then
  68. _tab.Begin()
  69. end
  70. local _now = os.time()
  71. local _break = false
  72. local _Repeat = false
  73. -- 初始化 重复 计时值
  74. for _key,_value in ipairs(_tab) do
  75. _value.RepeatTime = os.time()
  76. end
  77. while true do
  78. --满足状态的 key 会缓存至这里
  79. local _States = {}
  80. --寻找满足状态的项
  81. screen.keep()
  82. for _key,_value in ipairs(_tab) do
  83. if screen.is_colors(_value.Color, _value.csim or _tab.csim) then
  84. _log(_value.Name, "发现")
  85. table.insert(_States,_key)
  86. --检测重复
  87. if _tab.Repeat then
  88. if os.time() - _value.RepeatTime >= _tab.Repeat.n then
  89. _value.RepeatTime = os.time()
  90. if not _value.NoRepeat then
  91. _log(_value.Name, "重复")
  92. _Repeat = true
  93. end
  94. end
  95. end
  96. else
  97. _value.RepeatTime = os.time()
  98. end
  99. end
  100. screen.unkeep()
  101. --超时判断
  102. if #_States == 0 then
  103. if _tab.timeout then
  104. if os.time() - _now >= _tab.timeout.n then
  105. _log(_tab.Name, "超时")
  106. if _tab.timeout.Run(_tab.timeout) then
  107. break
  108. end
  109. if _tab.timeout.layer then
  110. _Do(_tab.timeout.layer)
  111. end
  112. _now = os.time()
  113. end
  114. end
  115. else
  116. --执行满足状态的项
  117. for _key,_value in ipairs(_States) do
  118. local _Handle = _tab[_value]
  119. _log(_Handle.Name, "执行")
  120. if _Handle.Run then
  121. if _Handle.Run(_Handle) then
  122. _break = true
  123. end
  124. end
  125. if _Handle.layer then
  126. _Do(_Handle.layer)
  127. end
  128. _now = os.time()
  129. end
  130. if _break then break end
  131. if _Repeat then
  132. if _tab.Repeat.Run(_tab.Repeat) then
  133. break
  134. end
  135. _Repeat=false
  136. end
  137. end
  138. sys.msleep(_tab.sleep or 500)
  139. end
  140. if type(_tab.End) == "function" then
  141. _tab.End()
  142. end
  143. -- 对全局 DoName 进行剔除 已处理的 框架
  144. if type(_G.DoName) == 'table' then
  145. if #(_G.DoName) > 0 then
  146. table.remove(_G.DoName,#DoName)
  147. end
  148. end
  149. end

压缩后

  1. 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
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注