Util.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. local Config = require("Config")
  2. local pi = math.acos(-1)
  3. local sqrt2 = math.sqrt(2)
  4. local os = _G.os
  5. local math = _G.math
  6. local floor = math.floor
  7. local cos = math.cos
  8. local sin = math.sin
  9. local atan2 = math.atan2
  10. function getTableCount(s)
  11. local count = 0
  12. if s == nil then return count end
  13. for _ in pairs(s) do
  14. count = count + 1
  15. end
  16. return count
  17. end
  18. function copyTableSimple(s, t, noNewTB)
  19. for k, v in pairs(s) do
  20. if type(v) == "table" then
  21. if not noNewTB then
  22. t[k] = {}
  23. end
  24. copyTableSimple(v, t[k])
  25. else
  26. t[k] = v
  27. end
  28. end
  29. end
  30. function copyTable(s)
  31. if not s then return end
  32. local t = {}
  33. copyTableSimple(s,t)
  34. return t
  35. end
  36. function initTable(tb)
  37. if not tb then return end
  38. for k in pairs(tb) do
  39. tb[k]= nil
  40. end
  41. end
  42. function cleanTable(s)
  43. for k,v in pairs(s) do
  44. s[k] = nil
  45. end
  46. end
  47. function getBit(n, index)
  48. local div = 2 ^ index
  49. return (n - n % div) / div % 2
  50. end
  51. function setBit(n, index)
  52. local bit = getBit(n, index)
  53. if bit ~= 0 then
  54. assert()
  55. end
  56. return n + 2 ^ index
  57. end
  58. function hypot(a, b)
  59. return math.sqrt(a * a + b * b)
  60. end
  61. local out = {}
  62. local out_len = 0
  63. function printTable(tb, step)
  64. if not step then
  65. step = 0
  66. out_len = 0
  67. end
  68. for k, v in pairs(tb) do
  69. for i = 1, step do
  70. out_len = out_len + 1
  71. out[out_len] = " "
  72. end
  73. if type(k) == "number" then
  74. out_len = out_len + 1
  75. out[out_len] = "["
  76. end
  77. out_len = out_len + 1
  78. out[out_len] = k
  79. if type(k) == "number" then
  80. out_len = out_len + 1
  81. out[out_len] = "]"
  82. end
  83. out_len = out_len + 1
  84. out[out_len] = "="
  85. if type(v) == "string" then
  86. out_len = out_len + 1
  87. out[out_len] = "\""
  88. end
  89. out_len = out_len + 1
  90. out[out_len] = type(v) == "table" and "" or v
  91. if type(v) == "boolean" then
  92. out[out_len] = tostring(v)
  93. end
  94. if type(v) == "string" then
  95. out_len = out_len + 1
  96. out[out_len] = "\""
  97. end
  98. out_len = out_len + 1
  99. out[out_len] = "\n"
  100. if type(v) == "table" then
  101. printTable(v, step + 1)
  102. end
  103. end
  104. if step == 0 then
  105. local str = table.concat(out, nil, 1, out_len)
  106. print(str)
  107. return str
  108. end
  109. end
  110. -- 判断是否同一天
  111. function isSameDay(time1)
  112. if not time1 then
  113. return
  114. end
  115. local now = os.time()
  116. local tToday = os.date("%Y%m%d", now)
  117. local t1 = os.date("%Y%m%d", time1)
  118. if tToday ~= t1 then
  119. return
  120. end
  121. return now
  122. end
  123. -- 判断传入的两个时间是否同一天
  124. function isSameDayByTimes(time1,time2)
  125. if not time1 or not time2 then
  126. return
  127. end
  128. local t1 = os.date("%Y%m%d", time1)
  129. local t2 = os.date("%Y%m%d", time2)
  130. if t1 ~= t2 then
  131. return
  132. end
  133. return true
  134. end
  135. -- 是否同一个月
  136. function isSameMonth(time)
  137. local now = os.date("*t")
  138. local d = os.date("*t",time)
  139. return now.year==d.year and now.month==d.month
  140. end
  141. -- 判断传入时间点与当前时间相差多少天
  142. local tb = {}
  143. function diffDay(time)
  144. local d = os.date("*t",time)
  145. tb.year = d.year
  146. tb.month = d.month
  147. tb.day = d.day
  148. time = os.time(tb)
  149. local nowDay = os.date("*t")
  150. tb.year = nowDay.year
  151. tb.month = nowDay.month
  152. tb.day = nowDay.day
  153. local nowTime = os.time(tb)
  154. return math.ceil(math.abs(nowTime-time)/86400)
  155. end
  156. -- 判断传入时间点与当前时间相差多少小时
  157. function diffHour(time)
  158. local d = os.date("*t",time)
  159. tb.year = d.year
  160. tb.month = d.month
  161. tb.day = d.day
  162. time = os.time(tb)
  163. local nowDay = os.date("*t")
  164. tb.year = nowDay.year
  165. tb.month = nowDay.month
  166. tb.day = nowDay.day
  167. local nowTime = os.time(tb)
  168. return math.ceil(math.abs(nowTime-time)/3600)
  169. end
  170. function diffDayByTimes(time1,time2)
  171. local t1 = os.date("*t",time1)
  172. tb.year = t1.year
  173. tb.month = t1.month
  174. tb.day = t1.day
  175. time1 = os.time(tb)
  176. local t2 = os.date("*t",time2)
  177. tb.year = t2.year
  178. tb.month = t2.month
  179. tb.day = t2.day
  180. time2 = os.time(tb)
  181. return math.ceil(math.abs(time1-time2)/86400)
  182. end
  183. -- 获取本月有多少天
  184. function getMonthDayNum()
  185. local currMonthDate = os.date("*t")
  186. tb.year = currMonthDate.year
  187. tb.month = currMonthDate.month
  188. tb.day = currMonthDate.day
  189. currMonthDate.hour = nil
  190. currMonthDate.min = nil
  191. currMonthDate.sec = nil
  192. currMonthDate.day = 1
  193. tb.day = 1
  194. if tb.month<12 then
  195. tb.month = tb.month + 1
  196. else
  197. tb.year = tb.year+1
  198. tb.month = 1
  199. end
  200. local nextMonthFirstDayTime = os.time(tb)
  201. local currMonthTime = os.time(currMonthDate)
  202. return math.floor(math.abs(nextMonthFirstDayTime-currMonthTime)/86400)
  203. end
  204. -- 取时间间隔 s
  205. function getInterval(time)
  206. if not time then
  207. return math.huge
  208. end
  209. local now = os.time()
  210. return now - time
  211. end
  212. --参数结构:year=****,month=****,day=****
  213. function isTime( startDate, endDate)
  214. local date_min = os.time(startDate) - 12 * 60 * 60
  215. local date_max = os.time(endDate) + 12 * 60 * 60
  216. local tmp_time = os.time()
  217. if not (date_min <= tmp_time and tmp_time < date_max) then
  218. return false
  219. end
  220. return true, date_max - tmp_time
  221. end
  222. -- check time
  223. function checkTime(startTimeConf,endTimeConf)
  224. local nowDate = os.date("*t")
  225. local nowMin = nowDate.hour * 60 + nowDate.min
  226. local startMin = startTimeConf.hour * 60 + startTimeConf.min
  227. local endMin = endTimeConf.hour * 60 + endTimeConf.min
  228. if nowMin >= startMin and nowMin < endMin then
  229. return true
  230. end
  231. end
  232. -- 检查传入时间,是否经过resetTime时间点;
  233. function isTimeOver(time,resetTime)
  234. if not time or not resetTime then
  235. return
  236. end
  237. local nowTime = os.time()
  238. local nowDay = os.date("%d", nowTime) + 0
  239. local nowHour = os.date("%H", nowTime) + 0
  240. local getDay = os.date("%d", time) + 0
  241. local getHour = os.date("%H", time) + 0
  242. local dirty = false
  243. if nowDay == getDay then
  244. if getHour < resetTime and nowHour >= resetTime then
  245. dirty = true
  246. end
  247. elseif nowDay == getDay + 1 then
  248. if nowHour >= resetTime then
  249. dirty = true
  250. end
  251. else
  252. dirty = true
  253. end
  254. return dirty
  255. end
  256. tmpDate = tmpDate or {}
  257. -- 取本月开始时间
  258. function getMonthStartTime(time)
  259. local d = os.date("*t",time)
  260. tmpDate.year = d.year
  261. tmpDate.month = d.month
  262. tmpDate.day = 1
  263. tmpDate.hour = 0
  264. tmpDate.min = 0
  265. tmpDate.sec = 0
  266. return os.time(tmpDate)
  267. end
  268. -- 取本周开始时间 周一凌晨
  269. function getWeekStartTime(time)
  270. local d = os.date("*t",time)
  271. tmpDate.year = d.year
  272. tmpDate.month = d.month
  273. tmpDate.hour = 0
  274. tmpDate.min = 0
  275. tmpDate.sec = 0
  276. local day = nil
  277. if d.wday == 1 then
  278. day = 6
  279. else
  280. day = d.wday - 2
  281. end
  282. tmpDate.day = d.day - day
  283. return os.time(tmpDate)
  284. end
  285. -- 取本日开始时间
  286. function getDayStartTime(time)
  287. local d = os.date("*t",time)
  288. tmpDate.year = d.year
  289. tmpDate.month = d.month
  290. tmpDate.day = d.day
  291. tmpDate.hour = 0
  292. tmpDate.min = 0
  293. tmpDate.sec = 0
  294. return os.time(tmpDate)
  295. end
  296. -- 取相差几天时间
  297. function getNextDayTime(time, day)
  298. local d = os.date("*t",time)
  299. tmpDate.year = d.year
  300. tmpDate.month = d.month
  301. tmpDate.day = d.day
  302. tmpDate.hour = 0
  303. tmpDate.min = 0
  304. tmpDate.sec = 0
  305. local ts = os.time(tmpDate)
  306. local dayTs = day * 86400
  307. return ts + dayTs
  308. end
  309. -- 获取前几天数据
  310. function getLastDayTime(time, day)
  311. local d = os.date("*t",time)
  312. tmpDate.year = d.year
  313. tmpDate.month = d.month
  314. tmpDate.day = d.day
  315. tmpDate.hour = 0
  316. tmpDate.min = 0
  317. tmpDate.sec = 0
  318. local ts = os.time(tmpDate)
  319. local dayTs = day * 86400
  320. return ts - dayTs
  321. end
  322. -- 取本日特定小时时间戳
  323. function getDayHourTime(time, hour)
  324. local d = os.date("*t",time)
  325. tmpDate.year = d.year
  326. tmpDate.month = d.month
  327. tmpDate.day = d.day
  328. tmpDate.hour = hour
  329. tmpDate.min = 0
  330. tmpDate.sec = 0
  331. return os.time(tmpDate)
  332. end
  333. -- 判断传入时间点,是否同周
  334. function isSameWeek(time1,time2)
  335. if not time1 or not time2 then
  336. return
  337. end
  338. return getWeekStartTime(time1) == getWeekStartTime(time2)
  339. end
  340. -- 星期中第几天的数字表示 1(表示星期天)到 7(表示星期六)
  341. function getWeekDay( time )
  342. local time = time or os.time()
  343. local d = os.date("*t",time)
  344. return d.wday
  345. end
  346. function split(str, split_char,isNumber)
  347. local t = {}
  348. while string.len(str)>0 do
  349. local pos = string.find(str, split_char);
  350. if (not pos) then
  351. if isNumber then
  352. t[#t + 1] = tonumber(str) or 0
  353. else
  354. t[#t + 1] = str
  355. end
  356. break
  357. end
  358. local sub_str = string.sub(str, 1, pos - 1)
  359. if isNumber then
  360. t[#t + 1] = tonumber(sub_str) or 0
  361. else
  362. t[#t + 1] = sub_str
  363. end
  364. str = string.sub(str, pos + 1, #str)
  365. end
  366. return t
  367. end
  368. -- 根据KEY从小到大排序
  369. function pairsByKeys(t)
  370. local a = {}
  371. for n in pairs(t) do
  372. a[#a+1] = n
  373. end
  374. table.sort(a)
  375. local i = 0
  376. return function()
  377. i = i + 1
  378. return a[i], t[a[i]]
  379. end
  380. end
  381. function cmptb(a, b)
  382. for k, v in pairs(a) do
  383. if type(v) == "table" then
  384. if type(b[k]) ~= "table" then
  385. return
  386. end
  387. if not cmptb(v, b[k]) then
  388. return
  389. end
  390. else
  391. if b[k] ~= v and k ~= "now" and k ~= "uid" then
  392. return
  393. end
  394. end
  395. end
  396. for k, v in pairs(b) do
  397. if type(v) == "table" then
  398. if type(a[k]) ~= "table" then
  399. return
  400. end
  401. if not cmptb(v, a[k]) then
  402. return
  403. end
  404. else
  405. if a[k] ~= v and k ~= "now" and k ~= "uid" then
  406. return
  407. end
  408. end
  409. end
  410. return true
  411. end
  412. function getWay(dx, dy)
  413. return ((floor(( atan2(dy, dx) + 11.388) / 1.57)) % 4) * 2;
  414. end
  415. function tableIsEmpty(tb)
  416. return _G.next(tb) == nil
  417. end
  418. -- 服务器是否符合
  419. function checkSvrIndex(svrIndexs, svrIndex)
  420. if #svrIndexs < 1 then return true end
  421. if not svrIndex then
  422. svrIndex = Config.SVR_INDEX
  423. end
  424. for i = 1, #svrIndexs do
  425. if svrIndexs[i][1] <= svrIndex and
  426. svrIndex <= svrIndexs[i][2] then
  427. return true
  428. end
  429. end
  430. end
  431. -- 含中文文本长度
  432. function utf8len(content)
  433. local len = string.len(content)
  434. local left = len
  435. local cnt = 0
  436. local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
  437. while left ~= 0 do
  438. local tmp = string.byte(content, -left)
  439. local i = #arr
  440. while arr[i] do
  441. if tmp >= arr[i] then
  442. left = left - i
  443. break
  444. end
  445. i = i - 1
  446. end
  447. cnt = cnt + 1
  448. end
  449. return cnt
  450. end
  451. function checkRandom(cmpValue)
  452. local r = math.random(0,10000)
  453. if r < cmpValue then
  454. return true
  455. end
  456. end
  457. SEPARATOR = "||"
  458. function format(...)
  459. local str = ""
  460. local t = {}
  461. for k,v in ipairs({...}) do
  462. if k == 1 then
  463. str = str .. v
  464. else
  465. t = split(v, SEPARATOR)
  466. if #t > 1 then
  467. t[1] = string.gsub(t[1],"({(.-)})",function(p1,p2)
  468. p2 = tonumber(p2)
  469. if p2 and t[p2+1] then
  470. return string.sub(t[p2+1], 2)
  471. else
  472. return p1
  473. end
  474. end)
  475. end
  476. if t[1] then
  477. str = str .. SEPARATOR .. t[1]
  478. end
  479. end
  480. end
  481. return str
  482. end
  483. --------------------------------------------------------
  484. --- 新增table.find函数
  485. ---@param1 array 数组
  486. ---@param2 elem 目标元素
  487. --------------------------------------------------------
  488. table.find = function(array,elem)
  489. for idx,e in ipairs(array) do
  490. if e == elem then
  491. return idx
  492. end
  493. end
  494. return #array + 1
  495. end
  496. --------------------------------------------------------
  497. --- 新增table.shuffle函数
  498. --- @param1 array 数组
  499. --------------------------------------------------------
  500. table.shuffle = function(array)
  501. for i = 1,#array do
  502. local r = math.random(1,#array)
  503. array[i],array[r] = array[r],array[i]
  504. end
  505. return array
  506. end
  507. local function print_lua_table(lua_table, indent)
  508. indent = indent or 0
  509. for k, v in pairs(lua_table) do
  510. if type(k) == "string" then
  511. k = string.format("%q", k)
  512. end
  513. local szSuffix = ""
  514. if type(v) == "table" then
  515. szSuffix = "{"
  516. end
  517. local szPrefix = string.rep(" ", indent)
  518. formatting = szPrefix.."["..k.."]".." = "..szSuffix
  519. if type(v) == "table" then
  520. print(formatting)
  521. print_lua_table(v, indent + 1)
  522. print(szPrefix.."},")
  523. else
  524. local szValue = ""
  525. if type(v) == "string" then
  526. szValue = string.format("%q", v)
  527. else
  528. szValue = tostring(v)
  529. end
  530. print(formatting..szValue..",")
  531. end
  532. end
  533. end
  534. table.print_lua_table = print_lua_table