forked from boluokk/e7Helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
1668 lines (1486 loc) · 43 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
图片: img_开头
颜色比值:cmp_开头
颜色多图:mul_开头
识字:ocr_开头
]]
findNode = function(selector) return nodeLib.findOne(selector, true) end
findNodes = function(selector) return nodeLib.findAll(selector, true) end
findByIndex = function (selector) return nodeLib.findByIndex(selector) end
allClick = function (selector) nodeLib.matchAllAndClick(point[selector]) end
saveConfig = setStringConfig
loadConfig = function(k, v)
v = v or ''
local y = getStringConfig(k)
if not y or #y == 0 then y = v end
return y
end
local prefix = '国际服'
local prefixArr = {'cmp_', 'mul_', 'ocr_', 'img_'}
getTargetName = function (str)
for i=1,#prefixArr do
if point[prefixArr[i]..prefix..str] then return prefixArr[i]..prefix..str end
if point[prefixArr[i]..str] then return prefixArr[i]..str end
end
return str
end
findTapOnce = function (target, config)
local r,w = findOne(target, config)
if not config then config = {} end
if r then
if type(r[1]) == 'table' then
stap({r[1].l, r[1].t}, config.tapInterval)
else
stap(r, config.tapInterval)
end
return w
end
end
-- 重置等待时间
reWaitTime = function ()
log('re_wait_time')
-- 重置wait()超时时间
-- 默认超时时间
waitTimeout = time()
-- 设置的超时时间
if TIMEOUT then
TIMEOUT = time() + 1000 * TIMEOUTSECOND
end
end
findOne = function (target, config)
if not target then return end
if type(target) ~= "table" then target = {target} end
if not config then config = {} end
config.sim = config.sim or 0.95
config.rg = config.rg or {0, 0, 0, 0}
config.imgEnd = config.imgEnd or '.png'
config.dir = config.dir or 0
originTar = ''
if type(config.keyword) == 'string' then config.keyword = {config.keyword} end
-- 截图延迟
ssleep(capture_interval)
-- 间隔时间查看游戏是否在运行
if time() - app_is_run > check_game_status_interval then
if not sAppIsRunning(current_server) then
log("程序未运行.."..current_server)
open(server_pkg_name[current_server])
end
if not sAppIsFront(current_server) then
open(server_pkg_name[current_server])
end
app_is_run = time()
end
local tar
releaseCapture()
keepCapture()
-- 派遣任务
-- 循环 5s 未发现
if cmpColorEx(point['cmp_国服派遣任务重新进行'], 1) == 1 then
releaseCapture()
ssleep(1)
-- 取消掉 or 继续 ?
-- 642,578 关闭
-- 878,577 重新
local t = time()
local timeout = 5 * 1000
while 'qqGroup_206490280' do
if cmpColorEx(point['cmp_国服派遣任务重新进行'], 1) == 1 then
log('派遣任务')
tap(878,577)
ssleep(1.5)
t = time()
end
if time() - t > timeout then break end
end
reWaitTime()
return
end
-- 提交神经网络
if cmpColorEx(point['cmp_国服Connection'], 1) == 1 then
releaseCapture()
wait(function ()
log('connection..')
reWaitTime()
-- B服特殊判定
if cmpColorEx(point['cmp_B服退出判定'], 1) == 1 then tap(528,417) return end
if cmpColorEx(point['cmp_国服Connection'], 1) == 0 then keepCapture() return true end
end, 1, nil, true)
end
if cmpColorEx(point['cmp_网络断开连接'], .98) == 1 then
releaseCapture()
local disconnectCount = 0
-- 次数过多, 消息通知?
-- qq通知
-- 邮箱通知
wait(function ()
disconnectCount = disconnectCount + 1
log('已经断开连接..')
reWaitTime()
if cmpColorEx(point['cmp_网络断开连接'], .98) == 0 then keepCapture() return true end
if disconnectCount == 10 then sendCloudMessage('游戏可能已经断开连接了, 请上线检查!') end
tap(640,319)
end, 2, nil, true)
end
-- 维护公告关闭掉
local retX, retY = findMultiColor(310,67,972,227, point.国服维护公告[1], point.国服维护公告[2], 0, .95)
if retX ~= -1 then
tap(retX, retY)
ssleep(1)
releaseCapture()
if cmpColorEx(point['cmp_国服邮件领取确认蓝底'], .95) == 1 then
tap(743,442)
reWaitTime()
return
end
end
-- 账号被顶之类的 todo
for i=1,#target do
tar = target[i]
if tar == "" then return end
originTar = tar
tar = getTargetName(tar)
running = tar
if detail_log_message then log('[', table.unpack(target), ']') end
if tar:find('img_') then
ret,x,y = findPicEx(config.rg[1], config.rg[2], config.rg[3], config.rg[4], tar..config.imgEnd, config.sim)
if x ~= -1 then return {x, y}, originTar end
end
if tar:find('cmp_') then
local r = cmpColorEx(point[tar], config.sim)
if r == 1 then
local p = string.split(point[tar], '|')
return {tonumber(p[1]), tonumber(p[2])}, originTar
end
end
if tar:find('mul_') then
local x=-1 y=-1
x,y=findMultiColor(config.rg[1], config.rg[2], config.rg[3], config.rg[4], point[tar][1], point[tar][2], config.dir, config.sim)
if x~=-1 then return {x, y}, originTar end
end
if tar:find('ocr_') then
local res = ocr(tar)
if #res > 0 then
if config.keyword then
for i=1,#config.keyword do
for j=1,#res do
if res[j]['text']:find(config.keyword[i]) then return {res[j]}, originTar end
end
end
else
return res, originTar
end
end
end
if tar:find('|') then
local r = cmpColorEx(tar, config.sim)
if r == 1 then
local p = string.split(tar, '|')
return {tonumber(p[1]), tonumber(p[2])}, originTar
end
end
end
end
findTap = function (target, config)
local r,w = findOne(target, config)
if not config then config = {} end
if r then
return wait(function ()
if type(r[1]) == 'table' then
stap({r[1].l, r[1].t}, config.tapInterval)
else
stap(r, config.tapInterval)
end
ssleep(other_ssleep_interval)
if not findOne(w, config) then return w end
end)
end
end
findAll = function (target, config)
if not target then return end
if type(target) ~= "table" then target = {target} end
if not config then config = {} end
config.sim = config.sim or 0.95
config.rg = config.rg or {0, 0, 0, 0}
config.imgEnd = config.imgEnd or '.png'
config.dir = config.dir or 0
-- 检测异常
-- findOne({})
releaseCapture()
keepCapture()
local all = {}
local tar
-- log(config)
for i=1,#target do
tar = target[i]
if tar:find('img_') then
local res = findPicAllPoint(config.rg[1], config.rg[2], config.rg[3], config.rg[4], tar..config.imgEnd, config.sim)
if #res > 0 then all[tar] = res end
end
if tar:find('mul_') then
local res = findMultiColorAll(config.rg[1], config.rg[2], config.rg[3], config.rg[4], point[tar][1], point[tar][2], config.dir, config.sim)
if res ~= nil then all[tar] = res end
end
end
if not next(all) then return end
return all
end
-- 切割字符串
string.split = function (str, sStr)
local r = splitStr(str, sStr)
if r then return r end
end
-- 显示多久 持续性,会到timeout才会退出]
longAppearAndTap = function (target, config, pos, timeout)
local t = time()
local timeout = timeout or 1.5
timeout = timeout * 1000
local r
wait(function ()
if pos then stap(pos, 0) end
r = findOne(target, config)
if r and time() - t > timeout then return true end
if not r then t = time() end
end, .1)
end
longAppear = function (target, config, appearTime, timeout)
local t = time()
local appearTime = appearTime or 1
appearTime = appearTime * 1000
local timeout = timeout or 3
local r
return wait(function ()
r = findOne(target, config)
if r and time() - t > appearTime then return true end
if not r then t = time() end
end, .1, timeout)
end
-- 消失多久[持续性,会到timeout才会退出]
longDisappearTap = function (target, config, pos, timeout, waitTimeOut)
local t = time()
local timeout = timeout or 1.5
timeout = timeout * 1000
local r
return wait(function ()
if pos then stap(pos, 0) end
r = findOne(target, config)
if not r and time() - t > timeout then return true end
if r then t = time() end
end, .1, waitTimeOut)
end
-- 显示多久 [确保必须是appear页面]
-- 显示中间突然断掉 true
-- 显示到时间完 false
longAppearMomentDisAppear = function (target, config, pos, timeout)
local t = time()
local timeout = timeout or 1.5
timeout = timeout * 1000
local r
while true do
-- if pos then stap(pos, 0) end
r = findOne(target, config)
if r and time() - t > timeout then return false end
if not r then return true end
ssleep(.1)
end
end
-- 消失多久[非持续性]
longDisappearMomentTap = function (target, config, pos, timeout)
local t = time()
local timeout = timeout or 1.5
timeout = timeout * 1000
local r
while true do
if pos then stap(pos, 0) end
r = findOne(target, config)
if not r and time() - t > timeout then return true end
if r then return false end
ssleep(.1)
end
end
-- 单位秒
ssleep = function (time) time = time or 0 sleep(time * 1000) end
-- 脚本运行时间
appRunningTime = function () return tickCount() end
stap = function (pos, interval, disableTapCheck)
if not interval then interval = tap_interval end
-- 解决把挤下线点击了和4点重连接
-- 防止误点击
if not disableTapCheck then ssleep(interval) findOne('') end
-- log(pos)
if type(pos) == "table" then tap(pos[1], pos[2]) end
if type(pos) == "string" then pos = getTargetName(pos) local p = string.split(point[pos], '|') tap(tonumber(p[1]), tonumber(p[2])) end
end
-- 识别颜色时间超时
-- func 执行逻辑
-- interval 间隔
-- TIMEOUT 超时
-- disableRestartGame 关闭重启游戏
wait = function (func, interval, TIMEOUT, disableRestartGame)
interval = interval or wait_interval
if TIMEOUT then
-- 记录传入的TIMEOUT
TIMEOUTSECOND = TIMEOUT
TIMEOUT = time() + 1000 * TIMEOUT
end
waitTimeout = time()
local game_stop_check = function ()
local t,a = findOne({'国服主页Rank', '国服公告X', '国服登录第七史诗'})
if a and not longAppearMomentDisAppear(a, nil, nil, 5) then
restartGame('游戏崩溃或每日更新')
end
end
while "q:1352955539" do
local r = func(game_stop_check)
if r then TIMEOUTSECOND = nil return r end
ssleep(interval)
if TIMEOUT and time() + 0 > TIMEOUT then TIMEOUTSECOND = nil TIMEOUT = nil break end
-- wait 超时可能卡主了
-- 重启脚本 + 回退到首页
if not TIMEOUT and not disableRestartGame
and time() - waitTimeout > check_game_identify_timeout then
restartGame('超时重试')
end
end
end
restartGame = function (message)
log(message)
slog(message)
setNumberConfig("scriptStatus", 3)
sendCloudMessage(message..'截图')
path.游戏首页()
reScript()
end
-- log_history = {}
log = function(...)
local arg = {...}
local l = table.join({
map(tostring, running, ' ', table.unpack(map(table2string, arg))),
}, ' ')
local t = os.date('%Y.%m.%d %H:%M:%S')
if logger_display_left_bottom then stoast(table.unpack(arg)) else console.println(1, t..' '..l) end
end
slog = function (msg, level, clear)
local msg = msg or ''
local level = level or 1
if clear then console.clearLog() end
local a = os.date('%Y-%m-%d %H:%M:%S')
msg = a..': '..msg
console.println(level, msg)
end
sswipe = function (s, e)
touchDown(1, s[1], s[2])
ssleep(.05)
touchMoveEx(1, e[1], e[2], 100)
touchMoveEx(1, e[1], e[2], 50)
touchUp(1)
ssleep(.8)
end
doubleFingerSwiper = function (f, s, e, time)
touchDown(0, f[1], f[2])
touchDown(1, s[1], s[2])
sleep(50)
touchMoveEx(0, e[1], e[2], time or 150)
touchMoveEx(1, e[1], e[2], time or 150)
touchUp(0)
touchUp(1)
end
onceFingerSwiper = function (f, e, time)
touchDown(1, f[1], f[2])
ssleep(.05)
touchMoveEx(1, e[1], e[2], time or 150)
ssleep(.1)
touchUp(1)
ssleep(1)
end
-- 填写路径滑动
fingerSwiperPath = function (firstPoint, otherPoints, interval)
touchDown(1, firstPoint[1], firstPoint[2])
ssleep(.05)
for i=1,#otherPoints do
touchMoveEx(1, otherPoints[i][1], otherPoints[1][2], interval[i] * 1000 or 1000)
end
ssleep(1)
touchUp(1)
end
-- 滑动后暂停
swipeEndStop = function (start, dest, stopTime, swTime)
touchDown(1, start[1], start[2])
ssleep(.05)
touchMoveEx(1, dest[1], dest[2], swTime or 500)
ssleep(stopTime or 0)
touchUp(1)
end
-- https://stackoverflow.com/questions/10460126/how-to-remove-spaces-from-a-string-in-lua
getScreen = function()
local width, height = getDisplaySize()
if getDisplayRotate() % 2 == 1 then width, height = height, width end
return {width = width, height = height}
end
screen = getScreen()
-- gesture = function(fingers)
-- if #fingers == 0 then fingers = {fingers} end
-- local gesture = Gesture:new() -- 创建一个手势滑动对象
-- for _, finger in pairs(fingers) do
-- local path = Path:new()
-- for _, point in pairs(finger.point) do path:addPoint(point[1], point[2]) end
-- path:setDurTime(finger.duration or 1000)
-- path:setStartTime(finger.start or 100)
-- gesture:addPath(path)
-- end
-- gesture:dispatch()
-- end
-- Path = {}
-- function Path:new(o)
-- o = o or {startTime = 0, durTime = 0, point = {}}
-- setmetatable(o, self)
-- self.__index = self
-- return o
-- end
-- function Path:setStartTime(t) self.startTime = t end
-- function Path:setDurTime(t) self.durTime = t end
-- function Path:addPoint(x, y)
-- table.insert(self.point, x)
-- table.insert(self.point, y)
-- end
-- Gesture = {}
-- function Gesture:new(o)
-- o = o or {path = {}}
-- setmetatable(o, self)
-- self.__index = self
-- return o
-- end
-- function Gesture:addPath(path) table.insert(self.path, path) end
-- gestureDispatchOnePath = function(path, id)
-- local point = path.point
-- if #point < 2 then return end
-- local start_time = time()
-- local timeline = {}
-- local length = 0
-- local x, y, px, py
-- px = point[1]
-- py = point[2]
-- sleep(path.startTime)
-- for i = 2, #point / 2 do
-- x = point[i * 2]
-- y = point[i * 2 + 1]
-- length = length + math.sqrt((x - px) ^ 2 + (y - py) ^ 2)
-- table.insert(timeline, length)
-- px, py = x, y
-- end
-- touchDown(id, point[1], point[2])
-- for i = 1,#point do
-- x = point[i]
-- y = point[i+1]
-- -- print("x = "..x)
-- -- print("y = "..y)
-- timeline[i] = timeline[i] / length * path.durTime
-- touchMoveEx(id, x, y, timeline[i])
-- if time() - start_time > path.durTime then break end
-- end
-- sleep(max(0, time() - start_time - path.durTime))
-- ssleep(1)
-- touchUp(id)
-- end
-- function Gesture:dispatch()
-- for id, path in pairs(self.path) do
-- -- log(71, id, path)
-- beginThread(gestureDispatchOnePath, path, id)
-- end
-- end
read = function (path, needDecode)
local resource = readFile(root_path..path)
if resource and #resource == 0 then return {} end
if needDecode then
resource = jsonLib.decode(resource)
end
return resource
end
write = function (path, data, append)
local tAppend = false
if append then tAppend = append end
writeFile(path, jsonLib.encode(data), tAppend)
end
ocr = function(r)
-- releaseCapture()
r = point[r]
r = ocrEx(r[1], r[2], r[3], r[4]) or {}
return r
end
table.ssort = function (t , parma)
local res = {}
if #t == 0 then return res end
for i=1,#t do
if i == 1 then
table.insert(res, t[1])
else
-- 7 5 9
-- 5 7 9
for e=1,#res do
if #res == e then table.insert(res, t[i]) end
if res[e][parma] > t[i][parma] then table.insert(res, e, t[i]) break end
end
end
end
return res
end
startsWithX = function(x) return function(prefix) return x:startsWith(prefix) end end
string.padStart = function(str, len, char)
if char == nil then char = " " end
return string.rep(char, len - #str) .. str
end
string.padEnd = function(str, len, char)
if char == nil then char = " " end
return str .. string.rep(char, len - #str)
end
table.diff = function(a, b)
local ans = {}
for k, v in pairs(a) do if v ~= b[k] then ans[k] = v end end
return ans
end
table.index = function(t, idx)
local ans = {}
for _, i in pairs(idx) do table.insert(ans, t[i]) end
return ans
end
table.reduce = function(t, f, a)
a = a or 0
for _, c in pairs(t) do a = f(a, c) end
return a
end
table.sum = function(t)
local a = 0
for _, c in pairs(t) do a = a + c end
return a
end
-- 从t中选出长度为n的所有组合,结果在ans,
table.combination = function(t, n)
local ans = {}
local cur = {}
local k = 1
combination(t, n, ans, cur, k)
return ans
end
combination = function(t, n, ans, cur, k)
-- cur = cur or {}
-- k = k or 1
if n == 0 then
table.insert(ans, shallowCopy(cur))
elseif k <= #t then
table.insert(cur, t[k])
combination(t, n - 1, ans, cur, k + 1)
cur[#cur] = nil
combination(t, n, ans, cur, k + 1)
end
end
table.flatten = function(t)
local ans = {}
for _, v in pairs(t) do
if type(v) == 'table' then
table.extend(ans, table.flatten(v))
else
table.insert(ans, v)
end
end
return ans
end
table.remove_duplicate = function(t)
local ans = {}
local visited = {}
for _, v in pairs(t) do
if not visited[v] then
table.insert(ans, v)
visited[v] = 1
end
end
return ans
end
-- 出现n次的元素
table.appear_times = function(t, times)
local ans = {}
local visited = {}
for _, v in pairs(t) do visited[v] = (visited[v] or 0) + 1 end
-- log(visited)
-- exit()
for k, _ in pairs(visited) do
if visited[k] == times then table.insert(ans, k) end
end
return ans
end
-- table.rotate = function(t, idx)
-- return table.extend(table.slice(t, idx), table.slice(t, 1, idx - 1))
-- end
-- 交
table.intersect = function(a, b)
local ans = {}
if #b < #a then a, b = b, a end
b = table.value2key(b)
a = table.value2key(a)
for k, _ in pairs(a) do if b[k] then table.insert(ans, k) end end
return ans
end
-- 差
table.subtract = function(a, b)
local ans = {}
b = table.value2key(b or {})
a = table.value2key(a or {})
for k, _ in pairs(a) do if not b[k] then table.insert(ans, k) end end
return ans
end
table.slice = function(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do sliced[#sliced + 1] = tbl[i] end
return sliced
end
-- shallow table
table.contains = function(a, b)
for k, v in pairs(b) do if a[k] ~= v then return false end end
return true
end
table.value2key = function(x)
local ans = {}
for k, v in pairs(x) do ans[v] = k end
return ans
end
table.select = function(mask, reference)
local ans = {}
for i = 1, #reference do if mask[i] then table.insert(ans, reference[i]) end end
return ans
end
-- return true if there is an x s.t. f(x) is true
table.any = function(t, f)
for k, v in pairs(t) do if f(v) then return true end end
end
-- return true if f(x) is all true
table.all = function(t, f)
for _, v in pairs(t) do if not f(v) then return false end end
return true
end
table.findv = function(t, f)
for k, v in pairs(t) do if f(v) then return v end end
end
table.filter = function(t, f)
local a = {}
for _, v in pairs(t) do if f(v) then table.insert(a, v) end end
return a
end
table.div_filter = function(t, f)
for _, v in pairs(t) do
local res = f(v)
if res then return res end
end
end
table.filterKV = function(t, f)
local a = {}
for k, v in pairs(t) do if f(k, v) then a[k] = v end end
return a
end
table.keys = function(t)
local a = {}
t = t or a
for k, _ in pairs(t) do table.insert(a, k) end
return a
end
table.values = function(t)
local a = {}
t = t or a
for _, v in pairs(t) do table.insert(a, v) end
return a
end
-- a,a+1,...b
range = function(a, b, s)
local t = {}
if not b and not s then a, b = 1, a end
s = s or 1
for i = a, b, s do table.insert(t, i) end
return t
end
table.includes = function(t, e)
return table.any(t, function(x) return x == e end)
end
string.includes = function(s, t)
for _, v in pairs(t) do if s:find(v) then return true end end
end
table.extend = function(t, e)
for k, v in pairs(e) do table.insert(t, v) end
return t
end
table.cat = function(t)
local ans = {}
for _, v in pairs(t) do for _, n in pairs(v) do table.insert(ans, n) end end
return ans
end
-- return contains element
table.containsTable = function (old, new)
local ans = {}
for i=1,#new do
if table.includes(old, new[i]) then table.insert(ans, new[i]) end
end
return ans
end
-- 将多个table合并成一个table: 包括key和value
mergeTables = function(...)
local mergedTable = {}
local function mergeTable(table)
for key, value in pairs(table) do
if type(value) == "table" then
mergedTable[key] = mergeTable(value)
else
mergedTable[key] = value
end
end
end
local tables = {...}
for _, table in ipairs(tables) do
mergeTable(table)
end
return mergedTable
end
-- in = {
-- "A" = {1,4,5,7},
-- "B" = {1,2,5,6},
-- "C" = {3,4,6,7},
-- "D" = {2,3,6,7},
-- }
-- out = { {"A","B"},...}
-- n:key, m:value O(mmn)
table.reverseIndex = function(t)
local r = {}
local s = {}
for k, v in pairs(t) do for k2, v2 in pairs(v) do s[v2] = true end end
for k, v in pairs(s) do
r[k] = {}
for k2, v2 in pairs(t) do
if table.includes(v2, k) then table.insert(r[k], k2) end
end
end
for k, v in pairs(r) do table.sort(v) end
return r
end
table.find = function(t, f) for k, v in pairs(t) do if f(v) then return k end end end
table.shuffle = function(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
-- one depth compare, and key-value pairs all same
table.equal = function(a, b)
if type(a) ~= 'table' or type(b) ~= 'table' then return end
if #a ~= #b then return end
if #a == 0 and #table.keys(a) ~= #table.keys(b) then return end
for k, v in pairs(a) do if v ~= b[k] then return end end
return true
end
-- one depth compare, and key all same
table.equalKey = function(a, b)
if type(a) ~= 'table' or type(b) ~= 'table' then return end
if #a ~= #b then return end
if #a == 0 and #table.keys(a) ~= #table.keys(b) then return end
for k, _ in pairs(a) do if b[k] == nil then return end end
return true
end
map = function(...)
local a = {...}
local n = select("#", ...)
local r = {}
local f, x = a[1], a[2]
local p, ur
if n < 2 then return r end
if n == 2 then
n = #x
elseif n > 2 then
ur = true
x = {table.unpack(a, 2, n)}
n = n - 1
end
for i = 1, n do
p = x[i]
if type(f) == "function" then
p = f(p)
elseif type(f) == "table" then
p = f[p]
end
r[i] = p
end
if ur then return table.unpack(r, 1, n) end
return r
end
table.join = function(t, d)
t = t or {}
d = not d and ',' or d
local a = ''
for i = 1, #t do
a = a .. t[i]
if i ~= #t then a = a .. d end
end
return a
end
string.trim = function (s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
shallowCopy = function(x)
local y = {}
if x == nil then return y end
for k, v in pairs(x) do y[k] = v end
return y
end
max = math.max
min = math.min
math.round = function(x) return math.floor(x + 0.5) end
round = math.round
clip = function(x, minimum, maximum) return min(max(x, minimum), maximum) end
table.findInsert = function (t, n, v)
if not t[n] then t[n] = {} end
if type(v) == "table" then
for i=1,#v do table.insert(t[n], v[i]) end
else
table.insert(t[n], v[i])
end
end
-- 从table中获取随机数
table.randomWithTableIn = function (arr, many)
local t = {}
local tempIndex = {}
local cur
wait(function ()
cur = math.ceil(math.random(1, #arr))
if not table.includes(tempIndex, cur) then table.insert(tempIndex, cur) end
if #tempIndex == many then return true end
end, 0)
for i=1,#tempIndex do table.insert(t, arr[tempIndex[i]]) end
return t
end
-- 获取相差时间(结束时间, 开始时间)
getTime = function (startTime)
local sec = tonumber(startTime) or 0
sec = math.floor((startTime - time()) / 1000)
local day = math.floor(sec / 60 / 60 / 24)
local hour = math.floor(sec / 60 / 60 % 24)
local min = math.floor(sec / 60 % 60)
sec = sec % 60
return day.."天"..hour.."时"..min.."分"..sec.."秒"
end
-- 获得所有K
getAllKey = function (originData)
if not originData then return {} end
local r = {}
for _,v in pairs(originData) do table.insert(r, _) end
if #r > 0 then table.sort(r) end
return r
end
chineseUnicodeStringMatch = function(a, b)
local len = min(#a, #b) // 3
local score = 0
for i = 1, len do
if a:sub(i * 3 - 2, i * 3) == b:sub(i * 3 - 2, i * 3) then
score = score + 1
end
end
return score
end
--获取时间
getDate = function ()