改进订阅工具

- 订阅表支持群内成员
- 调整读写函数
调度器、订阅集添加获取bot的函数
This commit is contained in:
dongRogen 2022-07-28 10:11:10 +08:00
parent af82b32d62
commit 7c2f51e849
2 changed files with 60 additions and 84 deletions

View File

@ -148,7 +148,7 @@ class ScheduleHandler(
// TODO 异步、限时 // TODO 异步、限时
for ((id, task) in this) { for ((id, task) in this) {
if (pause) { if (pause) {
logger?.info("调度器[$name]执行任务中暂停") logger?.info("调度器[$name]执行任务中暂停")
break break
} }
else task(id) else task(id)
@ -208,6 +208,11 @@ class ScheduleHandler(
// region -- 参数 // region -- 参数
/**
* 获取所属bot
*/
fun bot(): Bot = bot
/** /**
* 检查 id 是否已添加 * 检查 id 是否已添加
*/ */

View File

@ -4,6 +4,11 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import java.util.* import java.util.*
/**
* 用户个人订阅的 group
*/
private const val SINGLE: Long = 0
/** /**
* 订阅集 * 订阅集
* *
@ -22,9 +27,9 @@ class SubscriptionSet(
/** /**
* 订阅表 * 订阅表
* { mod : group/qq } * { mod : { group : qq } }
*/ */
private val receiverMap = mutableMapOf<Int, MutableSet<Long>>() private val receiverMap = mutableMapOf<Int, MutableMap<Long, MutableSet<Long>>>()
/** /**
* 订阅表线程锁 * 订阅表线程锁
@ -39,41 +44,25 @@ class SubscriptionSet(
// region -- 参数 // region -- 参数
/** /**
* mod被订阅的qq/群数量 * 获取所属bot
*/
fun bot(): Bot = bot
/**
* mod被订阅的个人+群数量
*/ */
infix fun countSub(mod: Int): Int = receiverLock.withLock { infix fun countSub(mod: Int): Int = receiverLock.withLock {
get(mod)?.size ?: 0 get(mod)?.let {
val single = it[SINGLE]?.size ?: 0
size - 1 + single
} ?: 0
} }
/** /**
* 清空订阅 * 清空订阅
*/ */
fun clear() = receiverLock.withLock { fun clear() = receiverLock.withLock {
clear() receiverMap.clear()
}
/**
* 记录订阅
*
* [id] 规则
* 等于 0 无效
* 大于 0 为qq
* 小于 0 为群号
*
* @param mod 模组id
* @param id qq/群号不等于0
*/
private fun sub(mod: Int, id: Long) {
if (id == 0L) return
receiverLock.withLock {
val set = get(mod) ?: mutableSetOf()
put(mod, set)
set += id
logger?.apply {
val type = if (id < 0) "group" else "qq"
info("添加订阅{$mod:${type}_$id}mod总订阅量${set.size}--订阅集[$name]")
}
}
} }
/** /**
@ -81,59 +70,49 @@ class SubscriptionSet(
* *
* @param mod 模组id * @param mod 模组id
* @param qq q号大于0 * @param qq q号大于0
*/
fun subQQ(mod: Int, qq: Long) = sub(mod, qq)
/**
* 记录订阅
*
* @param mod 模组id
* @param group 群号大于0 * @param group 群号大于0
*/ */
fun subGroup(mod: Int, group: Long) = sub(mod, 0 - group) fun sub(mod: Int, qq: Long, group: Long = SINGLE) = receiverLock.withLock {
get(mod)?.let {
val qs = it[group] ?: mutableSetOf()
it[group] = qs
if (qs.add(qq)) {
logger?.info("新增订阅{$mod:{$group:$qq}}--订阅集[$name]")
}
}
}
/** /**
* 取消订阅 * 取消订阅
* *
* [id] 规则
* 等于 0 无效
* 大于 0 为qq
* 小于 0 为群号
*
* @param mod 模组id
* @param id qq/群号不等于0
*/
private fun unSub(mod: Int, id: Long) {
if (id == 0L) return
receiverLock.withLock {
get(mod)?.let {
it -= id
logger?.apply {
val type = if (id < 0) "group" else "qq"
info("移除订阅{$mod:${type}_$id}mod总订阅量${it.size}--订阅集[$name]")
}
}
}
}
/**
* 撤销用户订阅
*
* @param mod 模组id * @param mod 模组id
* @param qq q号大于0 * @param qq q号大于0
* @param group 群号大于0
*/ */
fun unSubQQ(mod: Int, qq: Long) = unSub(mod, qq) fun unsub(mod: Int, qq: Long, group: Long = SINGLE) = receiverLock.withLock {
get(mod)?.let {
it[group]?.let { qs ->
if (qs.remove(qq)) {
logger?.info("取消订阅{$mod:{$group:$qq}}--订阅集[$name]")
}
}
}
}
/** /**
* 撤销群订阅 * 移除群订阅
* *
* @param mod 模组id * @param mod 模组id
* @param group 群号大于0 * @param group 群号大于0
*/ */
fun unsubGroup(mod: Int, group: Long) = unSub(mod, 0 - group) fun unsubGroup(mod: Int, group: Long) = receiverLock.withLock {
get(mod)?.remove(group)?.let {
logger?.info("移除群订阅{$mod:$group}--订阅集[$name]")
}
}
/** /**
* 撤销mod订阅 * 移除mod
* *
* @param mod 模组id * @param mod 模组id
*/ */
@ -145,9 +124,7 @@ class SubscriptionSet(
} }
/** /**
* 撤销mod订阅 * [rmMod]
*
* @param mod 模组id
*/ */
operator fun minusAssign(mod: Int) = rmMod(mod) operator fun minusAssign(mod: Int) = rmMod(mod)
@ -156,32 +133,26 @@ class SubscriptionSet(
// region -- 消费 // region -- 消费
/** /**
* 遍历订阅 * 遍历个人订阅
* *
* @param mod 模组id * @param mod 模组id
* @param action qq号消费操作 * @param action qq号消费操作
*/ */
fun eachQQ(mod: Int, action: (Long) -> Unit) = receiverLock.withLock { fun eachSingle(mod: Int, action: (qq: Long) -> Unit) = receiverLock.withLock {
get(mod)?.let { get(mod)?.get(SINGLE)?.forEach { action(it) }
for (qq in it) {
if (qq <= 0) continue
action(qq)
}
}
} }
/** /**
* 遍历订阅 * 遍历群订阅
* *
* @param mod 模组id * @param mod 模组id
* @param action 群号消费操作 * @param action 消费操作
*/ */
fun eachGroup(mod: Int, action: (Long) -> Unit) = receiverLock.withLock { fun eachGroup(mod: Int, action: (group: Long, member: List<Long>) -> Unit) = receiverLock.withLock {
get(mod)?.let { get(mod)?.filter {
for (group in it) { it.key != SINGLE
if (group >= 0) continue }?.forEach { (g, m) ->
action(0 - group) action(g, m.toList())
}
} }
} }