Skip to content

Commit

Permalink
优化能量球识别方式;加入可视化辅助工具
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Dec 26, 2020
1 parent afc7758 commit 9525385
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 112 deletions.
7 changes: 6 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-12-09 20:42:08
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-16 22:50:58
* @Last Modified time: 2020-12-25 21:29:44
* @Description:
*/
let currentEngine = engines.myEngine().getSource() + ''
Expand Down Expand Up @@ -52,6 +52,7 @@ let default_config = {
// 锁屏启动关闭提示框
dismiss_dialog_if_locked: true,
request_capture_permission: true,
capture_permission_button: 'START NOW|立即开始|允许',
// 是否保存日志文件,如果设置为保存,则日志文件会按时间分片备份在logback/文件夹下
save_log_file: true,
// 异步写入日志文件
Expand Down Expand Up @@ -157,6 +158,8 @@ let default_config = {
try_collect_by_multi_touch: false,
// 直接使用图像分析方式收取和帮助好友
direct_use_img_collect_and_help: true,
// 跳过好友浇水能量球
skip_own_watering_ball: false,
hough_param1: 30,
hough_param2: 30,
hough_min_radius: null,
Expand All @@ -168,13 +171,15 @@ let default_config = {
is_pro: is_pro,
// 尝试先逛一逛进行能量收取
try_collect_by_stroll: true,
collect_by_stroll_only: false,
stroll_button_regenerate: true,
auto_set_bang_offset: true,
bang_offset: 0,
limit_runnable_time_range: true,
// 当以下包正在前台运行时,延迟执行
skip_running_packages: [],
enable_visual_helper: false,
auto_restart_when_crashed: true,
// 更新后需要强制执行的标记v1.3.2.5
updated_temp_flag_1325: true,
updated_temp_flag_1326: true,
Expand Down
12 changes: 5 additions & 7 deletions core/Ant_forest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: NickHopps
* @Date: 2019-01-31 22:58:00
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-16 23:13:22
* @Last Modified time: 2020-12-26 08:35:51
* @Description:
*/
let { config: _config, storage_name: _storage_name } = require('../config.js')(runtime, this)
Expand Down Expand Up @@ -438,6 +438,8 @@ function Ant_forest () {
_commonFunctions.persistHistoryEnergy(currentEnergy)
logInfo('当前能量:' + currentEnergy)
}
// 初始化,避免关闭收取自己时统计成负值
_post_energy = currentEnergy
showCollectSummaryFloaty()
}

Expand Down Expand Up @@ -748,9 +750,7 @@ function Ant_forest () {
unlocker && unlocker.saveNeedRelock(true)
_runningQueueDispatcher.removeRunningTask()
engines.myEngine().forceStop()
if (_config.auto_set_brightness) {
device.setBrightnessMode(1)
}
_config.resetBrightness && _config.resetBrightness()
}
})
})
Expand Down Expand Up @@ -790,9 +790,7 @@ function Ant_forest () {
automator.lockScreen()
unlocker.saveNeedRelock(true)
}
if (_config.auto_set_brightness) {
device.setBrightnessMode(1)
}
_config.resetBrightness && _config.resetBrightness()
flushAllLogs()
_runningQueueDispatcher.removeRunningTask()
if (_base_scanner !== null) {
Expand Down
66 changes: 36 additions & 30 deletions core/BaseScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-12-18 14:17:09
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-16 22:50:07
* @Last Modified time: 2020-12-26 08:25:42
* @Description: 能量收集和扫描基类,负责通用方法和执行能量球收集
*/
importClass(java.util.concurrent.LinkedBlockingQueue)
Expand Down Expand Up @@ -93,7 +93,6 @@ const BaseScanner = function () {
this.threadPool = null
}
if (this.houghHelper !== null) {
this.houghHelper.persistence()
this.houghHelper = null
}
if (this.visualHelper !== null) {
Expand Down Expand Up @@ -316,49 +315,48 @@ const BaseScanner = function () {
this.threadPool.execute(function () {
try {
if (rgbImg.getMat().dims() >= 2) {
/**
* 能量球多维度采样,通过不同的数值来判断是否可收取、帮助、浇水球
*/
let startForColorValue = new Date().getTime()
let clipImg = images.clip(rgbImg, b.x - cvt(30), b.y + cvt(35), cvt(60), cvt(20))
let avgHsv = OpenCvUtil.getHistAverage(clipImg)
let median = OpenCvUtil.getMedian(clipImg)
clipImg = images.clip(rgbImg, b.x - cvt(40), b.y + cvt(80), cvt(80), cvt(20))
let clipGrayImg = images.clip(grayImgInfo, b.x - cvt(40), b.y + cvt(80), cvt(80), cvt(20))
let medianBottom = OpenCvUtil.getMedian(clipImg)
let stdDeviation = OpenCvUtil.getStandardDeviation(clipGrayImg)
let collectableBall = { ball: b, isHelp: false, medianBottom: medianBottom, avg: avgHsv, median: median, std: stdDeviation, isNight: isNight, isOwn: isOwn }
let medianBottomMin = isNight ? 80 : 180
let ballImage = images.clip(rgbImg, b.x - cvt(70), b.y - cvt(70), cvt(140), cvt(180))
let grayBallImage = images.grayscale(ballImage)
// 用于判定是否可收取
let intervalBallImage = images.inRange(grayBallImage, '#a1a1a1', '#b1b1b1')
let stdDeviation = OpenCvUtil.getStandardDeviation(intervalBallImage)
// 用于判定是否帮助收取
let intervalForHelpCheck = images.inRange(ballImage, '#ad8500', '#f4ddff')
let stdBottom = OpenCvUtil.getStandardDeviation(images.clip(intervalForHelpCheck, 0, cvt(160), intervalForHelpCheck.width, intervalForHelpCheck.height - cvt(160)))
// 判定是否为浇水球
let avgHsv = OpenCvUtil.getHistAverage(intervalForHelpCheck)
// 判定是不是真实的能量球,包括倒计时中的,因为帮收和倒计时中的颜色特征有几率一模一样
let intervalForCollatableRecheck = images.inRange(ballImage, '#77cc00', '#ffff91')
let collectableRecheckStd = OpenCvUtil.getStandardDeviation(intervalForCollatableRecheck)
let collectableBall = { ball: b, isHelp: false, isNight: isNight, isOwn: isOwn, avg: avgHsv, intervalStd: stdDeviation, stdBottom: stdBottom }

debugForDev(['取色耗时:{}ms', new Date().getTime() - startForColorValue])
if (median >= 235 && avgHsv >= 235) {
if (avgHsv >= 25) {
// 浇水能量球
collectableBall.isWatering = true
recheck = isOwn
} else if (!isOwn && medianBottom > medianBottomMin) {
} else if (!isOwn && stdBottom < 160) {
// 判定为帮收
collectableBall.isHelp = true
} else if (!isOwn && stdDeviation <= 30 || isOwn && Math.abs(212 - median) <= 3 && avgHsv > 200) {
// 判定为可收取
// collectableBall = collectableBall
} else {
// 非帮助或可收取
} else if (stdDeviation > 1400 || collectableRecheckStd > 1200 && !(isOwn && collectableBall.isWatering)) {
// 非帮助或可收取, 小于1400的则是 可收取的
collectableBall.invalid = true
}
lock.lock()
if (_config.develop_saving_mode) {
self.houghHelper.saveImage(ballImage, collectableBall)
}
if (!collectableBall.invalid) {
clickPoints.push(collectableBall)
self.visualHelper.addCircle(collectableBall.isHelp ? '帮助能量球' : collectableBall.isWatering ? '好友浇水能量球' : '可收取', collectableBall.ball)
if (_config.develop_saving_mode) {
if (collectableBall.isHelp) {
self.houghHelper.addHelpBall(collectableBall, isNight)
} else if (collectableBall.isWatering) {
self.houghHelper.addWaterBall(collectableBall, isNight)
} else {
self.houghHelper.addCollectBall(collectableBall, isNight)
}
}
} else {
self.visualHelper.addCircle('非有效能量球', collectableBall.ball)
invalidPoints.push(collectableBall)
findInvalidCallback(collectableBall)
_config.develop_saving_mode && self.houghHelper.addInvalidBall(collectableBall, isNight)
}
lock.unlock()
} else {
Expand Down Expand Up @@ -391,7 +389,13 @@ const BaseScanner = function () {
}
haveValidBalls = true
findPointCallback(point)
if (isOwn && point.isWatering && !_config.skip_own_watering || _config.help_friend && point.isHelp || !point.isHelp) {
if (
// 收取自身的好友浇水球
isOwn && point.isWatering && !_config.skip_own_watering_ball
// 帮助收取
|| point.isHelp && _config.help_friend
// 非帮收球且非浇水直接收取
|| !point.isHelp && !point.isWatering) {
self.collect_operated = true
automator.click(b.x, b.y)
if (idx < clickPoints.length - 1) {
Expand Down Expand Up @@ -421,7 +425,9 @@ const BaseScanner = function () {
rgbImg.recycle()
}
// 有浇水能量球且收自己时,进行二次校验 最多3次 || 非收取自己,且未找到可操作能量球,二次校验 仅一次 || 使用了双击卡,且点击过球
repeat = recheck && isOwn && --recheckLimit > 0 || !haveValidBalls && haveBalls && --recheckLimit >= 2 || _config.use_double_click_card && haveValidBalls
repeat = recheck && isOwn && --recheckLimit > 0
|| !haveValidBalls && haveBalls && --recheckLimit >= 2
|| _config.use_double_click_card && haveValidBalls && --recheckLimit > 0
if (repeat) {
debugInfo(['需要二次校验,等待{}ms', isOwn ? 200 : 500])
sleep(isOwn ? 200 : 500)
Expand Down
60 changes: 40 additions & 20 deletions core/ImgBasedFriendListScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-11-11 09:17:29
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-16 21:57:08
* @Last Modified time: 2020-12-25 21:18:19
* @Description: 基于图像识别控件信息
*/
importClass(com.tony.ColorCenterCalculatorWithInterval)
Expand Down Expand Up @@ -150,7 +150,7 @@ const ImgBasedFriendListScanner = function () {
if (this.last_check_point) {
this.last_check_color = grayImg.getBitmap().getPixel(this.last_check_point.x, this.last_check_point.y)
} else {
this.last_check_color
this.last_check_color = null
}


Expand All @@ -167,8 +167,10 @@ const ImgBasedFriendListScanner = function () {
colors.toString(this.last_check_color)
)
)
return true
} else {
countdown.summary('滑动校验 未找到校验点')
return false
}
}

Expand Down Expand Up @@ -218,6 +220,7 @@ const ImgBasedFriendListScanner = function () {
let count = 0
let hasNext = true
let that = this
let strollUpCheckCount = 0
do {
screen = _commonFunctions.checkCaptureScreenPermission(5)
// 重新复制一份
Expand Down Expand Up @@ -269,6 +272,9 @@ const ImgBasedFriendListScanner = function () {
this.threadPool.execute(function () {
let executeSuccess = false
try {
if (_config.collect_by_stroll_only) {
return
}
let calculator = new ColorCenterCalculatorWithInterval(
images.copy(intervalScreenForDetectHelp), _config.device_width - parseInt(200 * SCALE_RATE), pointData.point.x, pointData.point.y
)
Expand Down Expand Up @@ -307,6 +313,9 @@ const ImgBasedFriendListScanner = function () {
calculator.setScriptLogger(SCRIPT_LOGGER)
let point = calculator.getCenterPoint()
if (that.checkIsCanCollect(images.copy(intervalScreenForDetectCollect), point)) {
if (_config.collect_by_stroll_only) {
return
}
debugInfo('可收取位置:' + JSON.stringify(point))
if (_config.autoSetThreshold && _config.ocrThreshold > point.regionSame * 1.44) {
_config.ocrThreshold = parseInt(point.regionSame * 1.44)
Expand Down Expand Up @@ -419,27 +428,31 @@ const ImgBasedFriendListScanner = function () {
}
originScreen.recycle()
countdown.summary('分析所有可帮助和可收取的点')
if (collectOrHelpList && collectOrHelpList.length > 0) {
debugInfo(['开始收集和帮助收取,总数:{}', collectOrHelpList.length])
if (_config.develop_mode) {
collectOrHelpList.forEach(target => {
debugInfo(JSON.stringify(target))
})
}
let noError = true
collectOrHelpList.forEach(point => {
if (noError) {
if (false === that.collectTargetFriend(point)) {
noError = false
if (!_config.collect_by_stroll_only) {
if (collectOrHelpList && collectOrHelpList.length > 0) {
debugInfo(['开始收集和帮助收取,总数:{}', collectOrHelpList.length])
if (_config.develop_mode) {
collectOrHelpList.forEach(target => {
debugInfo(JSON.stringify(target))
})
}
let noError = true
collectOrHelpList.forEach(point => {
if (noError) {
if (false === that.collectTargetFriend(point)) {
noError = false
}
}
})
if (!noError) {
// true is error
return true
}
})
if (!noError) {
// true is error
return true
} else {
debugInfo('无可收取或帮助的内容')
}
} else {
debugInfo('无可收取或帮助的内容')
debugInfo('只通过逛一逛收集,跳过排行榜中的收取,仅识别倒计时')
}
}
self.visualHelper.displayAndClearAll()
Expand Down Expand Up @@ -475,7 +488,14 @@ const ImgBasedFriendListScanner = function () {
screen = _commonFunctions.checkCaptureScreenPermission()
grayScreen = images.grayscale(screen)
}
this.scrollUpIfNeeded(images.copy(grayScreen))
if (!this.scrollUpIfNeeded(images.copy(grayScreen))) {
if (++strollUpCheckCount >= 5) {
warnInfo('滑动校验失败达到5次,重新执行')
return true
}
} else {
strollUpCheckCount = 0
}
}
grayScreen && grayScreen.recycle()
} while (hasNext)
Expand Down
6 changes: 5 additions & 1 deletion core/StrollScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2020-09-07 13:06:32
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-16 22:50:36
* @Last Modified time: 2020-12-17 19:51:57
* @Description: 逛一逛收集器
*/
let { config: _config, storage_name: _storage_name } = require('../config.js')(runtime, this)
Expand Down Expand Up @@ -92,6 +92,8 @@ const StrollScanner = function () {
_config.stroll_button_height = region[3]
_config.stroll_button_regenerate = true
debugInfo(['重新生成逛一逛按钮区域:{}', JSON.stringify(region)])
this.visualHelper.addRectangle('自动识别逛一逛按钮', region)
this.visualHelper.displayAndClearAll()
_commonFunctions.ensureRegionInScreen(region)
} else {
warnInfo('自动识别逛一逛按钮失败', true)
Expand All @@ -106,6 +108,8 @@ const StrollScanner = function () {
continue
}
debugInfo(['逛下一个, click random region: [{}]', JSON.stringify(region)])
this.visualHelper.addRectangle('准备点击下一个', region)
this.visualHelper.displayAndClearAll()
automator.clickRandomRegion({ left: region[0], top: region[1], width: region[2], height: region[3] })
sleep(300)
hasNext = this.collectTargetFriend()
Expand Down
23 changes: 17 additions & 6 deletions lib/Unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-11-05 09:12:00
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-12-02 21:52:02
* @Last Modified time: 2020-12-24 21:37:27
* @Description:
*/
let { config: _config, storageName: _storageName } = require('../config.js')(runtime, this)
Expand Down Expand Up @@ -111,9 +111,7 @@ function Unlocker () {
this.reTry++
if (this.reTry > 3) {
logInfo('解锁失败达到三次,停止运行')
if (_config.auto_set_brightness) {
device.setBrightnessMode(1)
}
_config.resetBrightness && _config.resetBrightness()
_runningQueueDispatcher.removeRunningTask()
this.saveNeedRelock(true)
engines.myEngine().forceStop()
Expand Down Expand Up @@ -142,7 +140,20 @@ function Unlocker () {
// 唤醒设备
this.wakeup = function () {
if (this.relock && _config.auto_set_brightness) {
logInfo('设置显示亮度为最低,关闭自动亮度')
_config.last_brightness_mode = device.getBrightnessMode()
_config.last_brightness = device.getBrightness()
logInfo(['设置显示亮度为最低,关闭自动亮度 原始模式: {} 亮度: {}', _config.last_brightness_mode, _config.last_brightness])
_config.resetBrightness = () => {
debugInfo(['重置自动亮度 原始模式: {} 亮度: {}', _config.last_brightness_mode, _config.last_brightness])
if (!isNaN(_config.last_brightness_mode)) {
device.setBrightnessMode(_config.last_brightness_mode)
debugInfo('自动亮度模式调整完毕')
}
if (!isNaN(_config.last_brightness)) {
device.setBrightness(_config.last_brightness)
debugInfo('亮度值调整完毕')
}
}
// 设置最低亮度 同时关闭自动亮度
device.setBrightnessMode(0)
device.setBrightness(0)
Expand Down Expand Up @@ -216,7 +227,7 @@ function Unlocker () {
}
let storage = storages.create(_storageName)
debugInfo('保存是否需要重新锁屏:' + this.relock)
storage.put('needRelock', JSON.stringify({needRelock: this.relock && this.relock, timeout: new Date().getTime() + 30000 }))
storage.put('needRelock', JSON.stringify({needRelock: this.relock, timeout: new Date().getTime() + 30000 }))
}

this.getRelockInfo = function () {
Expand Down
Loading

0 comments on commit 9525385

Please sign in to comment.