Skip to content

Commit

Permalink
基于控件自动分析能量球识别区域
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Aug 17, 2020
1 parent 739b97c commit 92371aa
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 33 deletions.
36 changes: 20 additions & 16 deletions 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-08-06 19:48:46
* @Last Modified time: 2020-08-17 23:05:51
* @Description:
*/
'ui';
Expand Down Expand Up @@ -139,6 +139,7 @@ let default_config = {
rank_check_width: 550,
rank_check_height: 130,
// 收集能量球区域
auto_detect_tree_collect_region: true,
tree_collect_left: 150,
tree_collect_top: 550,
tree_collect_width: 800,
Expand Down Expand Up @@ -177,12 +178,6 @@ if (typeof config.collectable_energy_ball_content !== 'string') {

if (!isRunningMode) {
if (!currentEngine.endsWith('/config.js')) {
if (config.base_on_image && config.direct_use_img_collect_and_help) {
if (config.tree_collect_width + config.tree_collect_left > config.device_width) {
toastLog('请先运行config.js修改能量球识别区域,具体操作见README.md')
exit()
}
}
if (config.bottom_check_top > config.device_height) {
config.bottom_check_top = config.device_height - 50
config.bottom_check_width = config.device_width - 50
Expand All @@ -191,7 +186,7 @@ if (!isRunningMode) {
}

if (config.rank_check_left + config.rank_check_width > config.device_width) {
onfig.rank_check_left = 100
config.rank_check_left = 100
config.rank_check_width = 100
storageConfig.put('rank_check_left', config.rank_check_left)
storageConfig.put('rank_check_width', config.rank_check_width)
Expand Down Expand Up @@ -312,15 +307,19 @@ if (!isRunningMode) {
ui.tryCollectByMultiTouchChkBox.setChecked(config.try_collect_by_multi_touch)
ui.directUseImgCollectChkBox.setChecked(config.direct_use_img_collect_and_help)

ui.autoDetectTreeCollectRegionChkBox.setChecked(config.auto_detect_tree_collect_region)
ui.treeCollectRegionInpt.text(config.tree_collect_left + ',' + config.tree_collect_top + ',' + config.tree_collect_width + ',' + config.tree_collect_height)

if (config.direct_use_img_collect_and_help) {
ui.multiTouchContainer.setVisibility(View.GONE)
ui.collectRegionContainer.setVisibility(View.VISIBLE)
ui.autoDetectTreeCollectRegionChkBox.setVisibility(View.VISIBLE)
ui.collectRegionContainer.setVisibility(config.auto_detect_tree_collect_region ? View.GONE : View.VISIBLE)
config.try_collect_by_multi_touch = false
} else {
ui.collectRegionContainer.setVisibility(View.GONE)
ui.multiTouchContainer.setVisibility(View.VISIBLE)
ui.autoDetectTreeCollectRegionChkBox.setVisibility(View.GONE)
ui.collectRegionContainer.setVisibility(View.GONE)
ui.tryCollectByMultiTouchChkBox.setChecked(config.try_collect_by_multi_touch)
}

ui.useOcrParentContainer.setVisibility(config.base_on_image ? View.VISIBLE : View.GONE)
Expand Down Expand Up @@ -928,6 +927,7 @@ if (!isRunningMode) {
</horizontal>
<horizontal w="*" h="1sp" bg="#cccccc" margin="5 0"></horizontal>
<checkbox id="directUseImgCollectChkBox" text="是否直接基于图像分析收取和帮助好友" />
<checkbox id="autoDetectTreeCollectRegionChkBox" text="是否自动分析能量球识别区域" />
<vertical id="collectRegionContainer">
<horizontal gravity="center">
<text text="基于图像收集能量球范围:" layout_weight="20" />
Expand Down Expand Up @@ -1368,11 +1368,9 @@ if (!isRunningMode) {

ui.viewpager.setTitles(['基本配置', '进阶配置', '控件文本配置'])
ui.tabs.setupWithViewPager(ui.viewpager)
if (config.device_height <= 10 || config.device_width <= 10) {
inputDeviceSize().then(() => resetUiValues())
} else {
resetUiValues()
}

resetUiValues()

// 列表监听
ui.whiteList.on('item_bind', function (itemView, itemHolder) {
// 绑定删除事件
Expand Down Expand Up @@ -2058,14 +2056,20 @@ if (!isRunningMode) {
config.direct_use_img_collect_and_help = ui.directUseImgCollectChkBox.isChecked()
if (config.direct_use_img_collect_and_help) {
ui.multiTouchContainer.setVisibility(View.GONE)
ui.collectRegionContainer.setVisibility(View.VISIBLE)
ui.autoDetectTreeCollectRegionChkBox.setVisibility(View.VISIBLE)
ui.collectRegionContainer.setVisibility(config.auto_detect_tree_collect_region ? View.GONE : View.VISIBLE)
config.try_collect_by_multi_touch = false
} else {
ui.multiTouchContainer.setVisibility(View.VISIBLE)
ui.autoDetectTreeCollectRegionChkBox.setVisibility(View.GONE)
ui.collectRegionContainer.setVisibility(View.GONE)
ui.tryCollectByMultiTouchChkBox.setChecked(config.try_collect_by_multi_touch)
}
})
ui.autoDetectTreeCollectRegionChkBox.on('click', () => {
config.auto_detect_tree_collect_region = ui.autoDetectTreeCollectRegionChkBox.isChecked()
ui.collectRegionContainer.setVisibility(config.auto_detect_tree_collect_region ? View.GONE : View.VISIBLE)
})

ui.baseOnImageChkBox.on('click', () => {
config.base_on_image = ui.baseOnImageChkBox.isChecked()
Expand Down
36 changes: 32 additions & 4 deletions core/Ant_forest.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* @Author: NickHopps
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-08-04 21:09:40
* @Last Modified time: 2020-08-17 23:12:31
* @Description: 蚂蚁森林操作集
*/
let { config: _config } = require('../config.js')(runtime, this)
let { config: _config, storage_name: _storage_name } = require('../config.js')(runtime, this)
let singletonRequire = require('../lib/SingletonRequirer.js')(runtime, this)
let _widgetUtils = singletonRequire('WidgetUtils')
let automator = singletonRequire('Automator')
let _commonFunctions = singletonRequire('CommonFunction')
let _runningQueueDispatcher = singletonRequire('RunningQueueDispatcher')
let alipayUnlocker = singletonRequire('AlipayUnlocker')
let callStateListener = config.enable_call_state_control ? singletonRequire('CallStateListener')
let callStateListener = _config.enable_call_state_control ? singletonRequire('CallStateListener')
: { exitIfNotIdle: () => { }, enableListener: () => { }, disableListener: () => { } }
let FriendListScanner = require('./FriendListScanner.js')
let ImgBasedFriendListScanner = null
Expand Down Expand Up @@ -557,7 +557,33 @@ function Ant_forest () {
return _lost_someone
}


const autoDetectTreeCollectRegion = function () {
if (_config.auto_detect_tree_collect_region) {
let balls = _widgetUtils.widgetGetAll(/合种|看林区/)
if (balls && balls.length >= 2) {
balls = balls.sort((b1, b2) => b1.bounds().bottom > b2.bounds().bottom ? -1 : 1)
let bounds1 = balls[1].bounds()
let bounds2 = balls[0].bounds()
_config.tree_collect_left = bounds1.width()
_config.tree_collect_top = bounds1.top
_config.tree_collect_width = parseInt(_config.device_width - 2 * bounds1.width())
_config.tree_collect_height = bounds2.top - bounds1.top
detectRegion = [_config.tree_collect_left, _config.tree_collect_top, _config.tree_collect_width, _config.tree_collect_height]
infoLog('自动识别区域:' + JSON.stringify(detectRegion))
let configStorage = storages.create(_storage_name)
configStorage.put('tree_collect_left', _config.tree_collect_left)
configStorage.put('tree_collect_top', _config.tree_collect_top)
configStorage.put('tree_collect_width', _config.tree_collect_width)
configStorage.put('tree_collect_height', _config.tree_collect_height)
} else {
warnInfo('自动识别能量球识别区域失败,未识别到对象:' + (balls ? JSON.stringify(
balls.map(b => {
return { 'content': b.desc() || b.text(), 'bounds': b.bounds() }
})
) : ''))
}
}
}
/***********************
* 主要函数
***********************/
Expand Down Expand Up @@ -593,6 +619,8 @@ function Ant_forest () {
engines.myEngine().forceStop()
}
logInfo('进入个人首页成功')
// 自动识别能量球区域
autoDetectTreeCollectRegion()
clearPopup()
getPreEnergy()
}
Expand Down
8 changes: 4 additions & 4 deletions lib/prototype/CommonFunction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: TonyJiangWJ
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-08-04 11:36:39
* @Last Modified time: 2020-08-17 17:53:57
* @Description: 通用工具
*/
importClass(android.content.Context)
Expand Down Expand Up @@ -1242,7 +1242,7 @@ function CommonFunctions () {

this.printExceptionStack = function (e) {
if (e) {
errorInfo(['fileName:{} line:{} typeof e:{}', e.fileName, e.lineNumber, typeof e])
_logUtils.errorInfo(['fileName:{} line:{} typeof e:{}', e.fileName, e.lineNumber, typeof e])
let throwable = null
if (e.javaException) {
throwable = e.javaException
Expand All @@ -1260,7 +1260,7 @@ function CommonFunctions () {
while ((line = bufferedReader.readLine()) != null) {
scriptTrace.append("\n").append(line)
}
errorInfo(scriptTrace.toString())
_logUtils.errorInfo(scriptTrace.toString())
} else {
let funcs = Object.getOwnPropertyNames(e)
for (let idx in funcs) {
Expand All @@ -1280,7 +1280,7 @@ function CommonFunctions () {
let start = new Date().getTime()
let ax = 0, ay = 0, az = 0
//监听数据
sensors.register('gravity', sensors.delay.normal)
sensors.register('gravity', sensors.delay.fastest)
.on('change', (event, gx, gy, gz) => {
count++
_logUtils.debugInfo(util.format("[%d]重力加速度: %d, %d, %d", count, gx, gy, gz))
Expand Down
8 changes: 4 additions & 4 deletions lib/prototype/WidgetUtils.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-08-11 09:28:14
* @Last Modified time: 2020-08-17 12:51:28
* @Description:
*/
let formatDate = require('../DateUtil.js')
Expand Down Expand Up @@ -378,7 +378,7 @@ const friendListWaiting = function () {
const ensureRankListLoaded = function (checkTime) {
if (_config.base_on_image) {
let start = new Date().getTime()
let img = _commonFunctions.checkCaptureScreenPermission(3)
let img = _commonFunctions.checkCaptureScreenPermission(false, 3)
let color = '#969696'
let scaleRate = _config.device_width / 1080
let loaded = false
Expand All @@ -392,7 +392,7 @@ const ensureRankListLoaded = function (checkTime) {
} else {
debugInfo('未检测到排行榜加载完毕的检测点,等待1秒')
sleep(1000)
img = _commonFunctions.checkCaptureScreenPermission(3)
img = _commonFunctions.checkCaptureScreenPermission(false, 3)
}
}
debugInfo(['排行榜加载状态:{} 判断耗时:{}ms', loaded, (new Date().getTime() - start)])
Expand Down Expand Up @@ -813,7 +813,7 @@ const tryFindBottomRegion = function (grayImg) {
}
debugInfo(['尝试多点找色识别区域:「{}」点集合:「{}」', JSON.stringify(detectRegion), JSON.stringify(checkPoints)])
// 多点找色需要彩色原图
grayImg = _commonFunctions.checkCaptureScreenPermission(3)
grayImg = _commonFunctions.checkCaptureScreenPermission(false, 3)
point = images.findMultiColors(grayImg, color, checkPoints, { region: detectRegion })
if (point) {
_config.bottom_check_left = point.x - 5
Expand Down
10 changes: 7 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* @Author: NickHopps
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-08-12 12:51:17
* @Last Modified time: 2020-08-17 15:46:46
* @Description: 蚂蚁森林自动收能量
*/
let { config } = require('./config.js')(runtime, this)
let { config, storage_name } = require('./config.js')(runtime, this)
let singletonRequire = require('./lib/SingletonRequirer.js')(runtime, this)
const resolver = require('./lib/AutoJSRemoveDexResolver.js')

Expand Down Expand Up @@ -46,11 +46,12 @@ commonFunctions.registerOnEngineRemoved(function () {
runningQueueDispatcher.removeRunningTask(true, true,
() => {
// 重置自动亮度
if (_config.auto_set_brightness) {
if (config.auto_set_brightness) {
device.setBrightnessMode(1)
}
debugInfo('校验并移除已加载的dex')
resolver()
console.clear()
}
)
})
Expand Down Expand Up @@ -157,6 +158,9 @@ if (screen) {
config.device_height = height
config.device_width = width
warnInfo(['设备分辨率设置不正确,宽高已修正为:[{}, {}]', width, height])
let configStorage = storages.create(storage_name)
configStorage.put('device_height', height)
configStorage.put('device_width', width)
}
}
// 初始化悬浮窗
Expand Down
Loading

0 comments on commit 92371aa

Please sign in to comment.