Skip to content

Commit

Permalink
feat: 支持共享cd/次数
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 17, 2023
1 parent 1e84805 commit b4f5f85
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
30 changes: 23 additions & 7 deletions app/src/main/java/li/songe/gkd/data/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ import li.songe.gkd.service.openAdOptimized
import li.songe.gkd.service.querySelector
import li.songe.selector.Selector

class Value<T>(var value: T)

data class Rule(
/**
* length>0
*/
val matches: List<Selector> = emptyList(),
val excludeMatches: List<Selector> = 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,
Expand All @@ -44,7 +45,7 @@ data class Rule(
) {

/**
* 优化: 切换 APP 后短时间内, 如果存在开屏广告的规则并且没有一次触发, 则不启用其它规则, 避免过多规则阻塞运行
* 优化: 切换 APP 后短时间内, 如果存在开屏广告的规则并且没有一次触发, 则尽量使开屏广告运行
*/
val isOpenAd = group.name.startsWith("开屏广告")

Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/li/songe/gkd/data/SubscriptionRaw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ data class SubscriptionRaw(
val key: Int?,
val preKeys: List<Int>?,
val action: String?,
val actionCdKey: Int?,
val actionMaximumKey: Int?,
override val actionCd: Long?,
override val actionDelay: Long?,
override val quickFind: Boolean?,
Expand Down Expand Up @@ -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"),
)
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/li/songe/gkd/service/AbState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 达到最大执行次数
}
}
Expand Down Expand Up @@ -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
// 上一个点击的规则不在当前需要点击的列表
Expand Down
24 changes: 16 additions & 8 deletions app/src/main/java/li/songe/gkd/util/SubsState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -117,7 +110,6 @@ val appIdToRulesFlow by lazy {
},
matchDelay = matchDelay,
matchTime = matchTime,
actionMaximum = actionMaximum,
appId = appRaw.id,
activityIds = activityIds,
excludeActivityIds = excludeActivityIds,
Expand All @@ -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)
}
Expand Down

0 comments on commit b4f5f85

Please sign in to comment.