actionUtil.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. -- 动作转化
  2. local root = {}
  3. function root:initFuncMap()
  4. self.operateFunMap = {
  5. ["kv"] = self.isTableMatch,
  6. ["=="] = self.isConditionEqual,
  7. [">"] = self.isConditionGreater,
  8. [">="] = self.isConditionEqualGreater,
  9. ["<"] = self.isConditionLess,
  10. ["<="] = self.isConditionEqualLess,
  11. ["="] = self.isConditionEqual,
  12. ["~="] = self.isConditionNotEqual,
  13. ["!="] = self.isConditionNotEqual,
  14. ["sam"] = self.isConditionSameMember,
  15. ["unsam"] = self.isConditionNotSameMember,
  16. ["!sam"] = self.isConditionNotSameMember,
  17. [">inc"] = self.isConditionLinclude,
  18. ["<inc"] = self.isConditionRinclude,
  19. [">uninc"] = self.isConditionNotLinclude,
  20. ["<uninc"] = self.isConditionNotRinclude,
  21. ["!>inc"] = self.isConditionNotLinclude,
  22. ["!<inc"] = self.isConditionNotRinclude,
  23. ["range"] = self.isConditionRange,
  24. ["unrange"] = self.isConditionNotRange,
  25. ["!range"] = self.isConditionNotRange,
  26. -- 支持中文
  27. ["等于"] = self.isConditionEqual,
  28. ["大于"] = self.isConditionGreater,
  29. ["大于等于"] = self.isConditionEqualGreater,
  30. ["等于大于"] = self.isConditionEqualGreater,
  31. ["小于"] = self.isConditionLess,
  32. ["小于等于"] = self.isConditionEqualLess,
  33. ["等于小于"] = self.isConditionEqualLess,
  34. ["不等于"] = self.isConditionNotEqual,
  35. ["包含范围"] = self.isConditionRange,
  36. ["不包含范围"] = self.isConditionNotRange,
  37. ["相同成员"] = self.isConditionSameMember,
  38. ["无相同成员"] = self.isConditionNotSameMember
  39. }
  40. end
  41. -- 操作条件
  42. function root:isActionCondition(val, operate, ...)
  43. if val == nil or operate == nil then
  44. return false
  45. end
  46. if self.operateFunMap == nil then
  47. self:initFuncMap()
  48. end
  49. if self.operateFunMap then
  50. for k, fun in pairs(self.operateFunMap) do
  51. if k == operate then
  52. return fun(self, val, ...)
  53. end
  54. end
  55. end
  56. return false
  57. end
  58. -- table 匹配
  59. function root:isTableMatch(val, tabKey, tabVal)
  60. if type(val) ~= "table" then
  61. return false
  62. end
  63. if not tabKey or not tabVal then
  64. return true
  65. end
  66. for key, value in ipairs(val) do
  67. if key == tabKey and tabVal >= value then
  68. return true
  69. end
  70. end
  71. return false
  72. end
  73. -- 等于
  74. function root:isConditionEqual(val, condValue)
  75. return val and val == condValue
  76. end
  77. -- 不等于
  78. function root:isConditionNotEqual(val, condValue)
  79. return val and val ~= condValue
  80. end
  81. -- 大于
  82. function root:isConditionGreater(val, condValue)
  83. return val and condValue and val > condValue
  84. end
  85. -- 大于等于
  86. function root:isConditionEqualGreater(val, condValue)
  87. return val and condValue and val >= condValue
  88. end
  89. -- 小于
  90. function root:isConditionLess(val, condValue)
  91. return val and condValue and val < condValue
  92. end
  93. -- 小于等于
  94. function root:isConditionEqualLess(val, condValue)
  95. return val and condValue and val <= condValue
  96. end
  97. -- 是否范围值
  98. function root:isRangeValue(v, range)
  99. if type(range) ~= "table" or #range ~= 2 then
  100. return false
  101. end
  102. local isMinLimit = false
  103. local isMaxLimit = false
  104. if string.lower(range[1]) == "z" then
  105. isMinLimit = true
  106. end
  107. if range[2] == -1 or string.lower(range[2]) == "z" then
  108. isMaxLimit = true
  109. end
  110. if not isMinLimit and not isMaxLimit then
  111. if v >= range[1] and v <= range[2] then
  112. return true
  113. end
  114. else
  115. if not isMinLimit then
  116. if v < range[1] then
  117. return false
  118. end
  119. end
  120. if not isMaxLimit then
  121. if v > range[2] then
  122. return false
  123. end
  124. end
  125. return true
  126. end
  127. return false
  128. end
  129. -- 包含范围
  130. function root:isConditionRange(val, condValue)
  131. if val == nil or condValue == nil then
  132. return false
  133. end
  134. if type(condValue) ~= "table" then
  135. return false
  136. end
  137. return root:isRangeValue(val, condValue)
  138. end
  139. -- 有共同项
  140. function root:isConditionSameMember(val, condValue)
  141. local valTable = type(val) == "table" and val or {val}
  142. local condTable = type(condValue) == "table" and condValue or {condValue}
  143. for k, v in ipairs(condTable) do
  144. if table.include(valTable, v) then
  145. return true
  146. end
  147. end
  148. return false
  149. end
  150. -- 无共同项
  151. function root:isConditionNotSameMember(val, condValue)
  152. return not self:isConditionSameMember(val, condValue)
  153. end
  154. -- 不包含范围
  155. function root:isConditionNotRange(val, condValue)
  156. if val == nil or condValue == nil then
  157. return false
  158. end
  159. if type(condValue) ~= "table" then
  160. return false
  161. end
  162. return not root:isRangeValue(val, condValue)
  163. end
  164. -- 左被包含
  165. function root:isConditionLinclude(val, condValue)
  166. if val == nil or condValue == nil then
  167. return false
  168. end
  169. -- 配置参数必须为table
  170. local tbConf = nil
  171. if type(condValue) == "table" then
  172. tbConf = condValue
  173. else
  174. tbConf = {condValue}
  175. end
  176. if type(val) ~= "table" then
  177. return table.include(tbConf, val)
  178. end
  179. for k, v in ipairs(val) do
  180. if not table.include(tbConf, v) then
  181. return false
  182. end
  183. end
  184. return true
  185. end
  186. -- 右被包含
  187. function root:isConditionRinclude(val, condValue)
  188. if val == nil or condValue == nil then
  189. return false
  190. end
  191. -- 入参必须为table
  192. local tbConf = nil
  193. if type(val) == "table" then
  194. tbConf = val
  195. else
  196. tbConf = {val}
  197. end
  198. if type(condValue) ~= "table" then
  199. return table.include(tbConf, condValue)
  200. end
  201. for k, v in ipairs(condValue) do
  202. if not table.include(tbConf, v) then
  203. return false
  204. end
  205. end
  206. return true
  207. end
  208. -- 左被不包含
  209. function root:isConditionNotLinclude(val, condValue)
  210. if val == nil or condValue == nil then
  211. return false
  212. end
  213. -- 入参必须为table
  214. local tbConf = nil
  215. if type(condValue) == "table" then
  216. tbConf = condValue
  217. else
  218. tbConf = {condValue}
  219. end
  220. if type(val) ~= "table" then
  221. return not table.include(tbConf, val)
  222. end
  223. for k, v in ipairs(val) do
  224. if table.include(tbConf, v) then
  225. return false
  226. end
  227. end
  228. return true
  229. end
  230. -- 右被不包含
  231. function root:isConditionNotRinclude(val, condValue)
  232. if val == nil or condValue == nil then
  233. return false
  234. end
  235. -- 配置参数必须为table
  236. local tbConf = nil
  237. if type(val) == "table" then
  238. tbConf = val
  239. else
  240. tbConf = {val}
  241. end
  242. if type(condValue) ~= "table" then
  243. return not table.include(tbConf, condValue)
  244. end
  245. for k, v in ipairs(condValue) do
  246. if table.include(tbConf, v) then
  247. return false
  248. end
  249. end
  250. return true
  251. end
  252. return root