From b4f5f85a0f0bdd252e8ea805f99292aad12e64ca Mon Sep 17 00:00:00 2001 From: lisonge Date: Fri, 17 Nov 2023 23:17:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=85=B1=E4=BA=ABcd/?= =?UTF-8?q?=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/li/songe/gkd/data/Rule.kt | 30 ++++++++++++++----- .../java/li/songe/gkd/data/SubscriptionRaw.kt | 4 +++ .../main/java/li/songe/gkd/service/AbState.kt | 8 ++--- .../main/java/li/songe/gkd/util/SubsState.kt | 24 ++++++++++----- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/li/songe/gkd/data/Rule.kt b/app/src/main/java/li/songe/gkd/data/Rule.kt index 571fd29d1..d27923ed9 100644 --- a/app/src/main/java/li/songe/gkd/data/Rule.kt +++ b/app/src/main/java/li/songe/gkd/data/Rule.kt @@ -14,19 +14,20 @@ import li.songe.gkd.service.openAdOptimized import li.songe.gkd.service.querySelector import li.songe.selector.Selector +class Value(var value: T) + data class Rule( /** * length>0 */ val matches: List = emptyList(), val excludeMatches: List = emptyList(), - val actionCd: Long = defaultMiniCd, + val actionDelay: Long = 0, val quickFind: Boolean = false, val matchDelay: Long?, val matchTime: Long?, - val actionMaximum: Int?, val resetMatch: String?, val appId: String, @@ -44,7 +45,7 @@ data class Rule( ) { /** - * 优化: 切换 APP 后短时间内, 如果存在开屏广告的规则并且没有一次触发, 则不启用其它规则, 避免过多规则阻塞运行 + * 优化: 切换 APP 后短时间内, 如果存在开屏广告的规则并且没有一次触发, 则尽量使开屏广告运行 */ val isOpenAd = group.name.startsWith("开屏广告") @@ -63,19 +64,34 @@ data class Rule( ) } - var actionTriggerTime = 0L + val actionCd = defaultMiniCd.coerceAtLeast( + ((if (rule.actionCdKey != null) { + group.rules.find { r -> r.key == rule.actionCdKey }?.actionCd ?: group.actionCd + ?: app.actionCd + } else { + null + }) ?: rule.actionCd ?: defaultMiniCd) + ) + var actionTriggerTime = Value(0L) fun trigger() { - actionTriggerTime = System.currentTimeMillis() + actionTriggerTime.value = System.currentTimeMillis() // 重置延迟点 actionDelayTriggerTime = 0L - actionCount++ + actionCount.value++ lastTriggerRule = this if (isOpenAd && openAdOptimized == true) { openAdOptimized = false } } - var actionCount = 0 + val actionMaximum = ((if (rule.actionMaximumKey != null) { + group.rules.find { r -> r.key == rule.actionMaximumKey }?.actionMaximum + ?: group.actionMaximum ?: app.actionMaximum + } else { + null + }) ?: rule.actionMaximum) + + var actionCount = Value(0) var matchChangeTime = 0L diff --git a/app/src/main/java/li/songe/gkd/data/SubscriptionRaw.kt b/app/src/main/java/li/songe/gkd/data/SubscriptionRaw.kt index 5c3ea1dd3..dfb462652 100644 --- a/app/src/main/java/li/songe/gkd/data/SubscriptionRaw.kt +++ b/app/src/main/java/li/songe/gkd/data/SubscriptionRaw.kt @@ -101,6 +101,8 @@ data class SubscriptionRaw( val key: Int?, val preKeys: List?, val action: String?, + val actionCdKey: Int?, + val actionMaximumKey: Int?, override val actionCd: Long?, override val actionDelay: Long?, override val quickFind: Boolean?, @@ -221,6 +223,8 @@ data class SubscriptionRaw( resetMatch = getString(rulesJson, "resetMatch"), snapshotUrls = getStringIArray(rulesJson, "snapshotUrls"), exampleUrls = getStringIArray(rulesJson, "exampleUrls"), + actionMaximumKey = getInt(rulesJson, "actionMaximumKey"), + actionCdKey = getInt(rulesJson, "actionCdKey"), ) } diff --git a/app/src/main/java/li/songe/gkd/service/AbState.kt b/app/src/main/java/li/songe/gkd/service/AbState.kt index 2369344a6..7a55a68ce 100644 --- a/app/src/main/java/li/songe/gkd/service/AbState.kt +++ b/app/src/main/java/li/songe/gkd/service/AbState.kt @@ -89,19 +89,19 @@ fun isAvailableRule(rule: Rule): Boolean { if (activityChangeTime != rule.matchChangeTime) { // 当 界面 更新时, 重置操作延迟点, 重置点击次数 rule.actionDelayTriggerTime = 0 - rule.actionCount = 0 + rule.actionCount.value = 0 rule.matchChangeTime = activityChangeTime } } else { if (appChangeTime != rule.matchChangeTime) { // 当 切换APP 时, 重置点击次数 rule.actionDelayTriggerTime = 0 - rule.actionCount = 0 + rule.actionCount.value = 0 rule.matchChangeTime = appChangeTime } } if (rule.actionMaximum != null) { - if (rule.actionCount >= rule.actionMaximum) { + if (rule.actionCount.value >= rule.actionMaximum) { return false // 达到最大执行次数 } } @@ -129,7 +129,7 @@ fun isAvailableRule(rule: Rule): Boolean { } } } - if (rule.actionTriggerTime + rule.actionCd > t) return false // 处于冷却时间 + if (rule.actionTriggerTime.value + rule.actionCd > t) return false // 处于冷却时间 if (rule.preRules.isNotEmpty()) { // 需要提前点击某个规则 lastTriggerRule ?: return false // 上一个点击的规则不在当前需要点击的列表 diff --git a/app/src/main/java/li/songe/gkd/util/SubsState.kt b/app/src/main/java/li/songe/gkd/util/SubsState.kt index c2b6ec0f7..139a5c736 100644 --- a/app/src/main/java/li/songe/gkd/util/SubsState.kt +++ b/app/src/main/java/li/songe/gkd/util/SubsState.kt @@ -92,21 +92,14 @@ val appIdToRulesFlow by lazy { val matchDelay = ruleRaw.matchDelay ?: groupRaw.matchDelay ?: appRaw.matchDelay val matchTime = ruleRaw.matchTime ?: groupRaw.matchTime ?: appRaw.matchTime - val actionMaximum = - ruleRaw.actionMaximum ?: groupRaw.actionMaximum ?: appRaw.actionMaximum val resetMatch = ruleRaw.resetMatch ?: groupRaw.resetMatch ?: appRaw.resetMatch - val actionCd = Rule.defaultMiniCd.coerceAtLeast( - ruleRaw.actionCd ?: groupRaw.actionCd ?: appRaw.actionCd - ?: Rule.defaultMiniCd - ) val actionDelay = ruleRaw.actionDelay ?: groupRaw.actionDelay ?: appRaw.actionDelay ?: 0 groupRuleList.add( Rule( quickFind = quickFind, - actionCd = actionCd, actionDelay = actionDelay, index = ruleIndex, matches = ruleRaw.matches.map { Selector.parse(it) }, @@ -117,7 +110,6 @@ val appIdToRulesFlow by lazy { }, matchDelay = matchDelay, matchTime = matchTime, - actionMaximum = actionMaximum, appId = appRaw.id, activityIds = activityIds, excludeActivityIds = excludeActivityIds, @@ -138,6 +130,22 @@ val appIdToRulesFlow by lazy { otherRule.key ) }.toSet() + // 共用次数 + val maxKey = ruleConfig.rule.actionMaximumKey + if (maxKey != null) { + val otherRule = groupRuleList.find { r -> r.key == maxKey } + if (otherRule != null) { + ruleConfig.actionCount = otherRule.actionCount + } + } + // 共用 cd + val cdKey = ruleConfig.rule.actionCdKey + if (cdKey != null) { + val otherRule = groupRuleList.find { r -> r.key == cdKey } + if (otherRule != null) { + ruleConfig.actionTriggerTime = otherRule.actionTriggerTime + } + } } rules.addAll(groupRuleList) }