From a9972dc0305d58beaf8de9aa83b761abb5da4c0d Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 24 Mar 2020 15:28:24 +0800 Subject: [PATCH] Reduce code redundancy --- .../packet/chat/receive/MessageSvc.kt | 2 +- .../kotlin/net/mamoe/mirai/contact/Contact.kt | 1 - .../kotlin/net/mamoe/mirai/contact/Group.kt | 47 ++++--------------- .../kotlin/net.mamoe.mirai/contact/Contact.kt | 1 - .../kotlin/net.mamoe.mirai/contact/Group.kt | 45 ++++++++++++++++-- .../kotlin/net/mamoe/mirai/contact/Contact.kt | 1 - .../kotlin/net/mamoe/mirai/contact/Group.kt | 43 +++-------------- 7 files changed, 57 insertions(+), 83 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt index a711e56df..973dd853e 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt @@ -198,7 +198,7 @@ internal class MessageSvc { if (group.members.contains(msg.msgHead.authUin)) { return@mapNotNull null } - return@mapNotNull MemberJoinEvent(group.Member(object : MemberInfo { + return@mapNotNull MemberJoinEvent(group.newMember(object : MemberInfo { override val nameCard: String get() = "" override val permission: MemberPermission get() = MemberPermission.MEMBER override val specialTitle: String get() = "" diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Contact.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Contact.kt index bfae361d2..e39b3ff62 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Contact.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Contact.kt @@ -32,7 +32,6 @@ import net.mamoe.mirai.utils.WeakRefProperty * * @author Him188moe */ -@Suppress("INAPPLICABLE_JVM_NAME") @OptIn(MiraiInternalAPI::class, JavaHappyAPI::class) actual abstract class Contact : CoroutineScope, ContactJavaHappyAPI() { /** diff --git a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt index bd976661d..b35bb85a2 100644 --- a/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt +++ b/mirai-core/src/androidMain/kotlin/net/mamoe/mirai/contact/Group.kt @@ -25,7 +25,6 @@ import net.mamoe.mirai.utils.OverFileSizeMaxException /** * 群. 在 QQ Android 中叫做 "Troop" */ -@Suppress("INAPPLICABLE_JVM_NAME") actual abstract class Group : Contact(), CoroutineScope { /** * 群名称. @@ -101,7 +100,7 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract val owner: Member /** - * [Bot] 在群内的 [Member] 实例 + * [Bot] 在群内的 [newMember] 实例 */ @MiraiExperimentalAPI actual abstract val botAsMember: Member @@ -158,13 +157,12 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract suspend fun quit(): Boolean /** - * 构造一个 [Member]. + * 构造一个 [newMember]. * 非特殊情况请不要使用这个函数. 优先使用 [get]. */ - @JvmName("newMember") - @Suppress("INAPPLICABLE_JVM_NAME", "FunctionName") + @Suppress("FunctionName") @MiraiExperimentalAPI("dangerous") - actual abstract fun Member(memberInfo: MemberInfo): Member + actual abstract fun newMember(memberInfo: MemberInfo): Member /** * 向这个对象发送消息. @@ -193,40 +191,11 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract override suspend fun uploadImage(image: ExternalImage): OfflineGroupImage actual companion object { - /** - * by @kar98k - */ - actual fun calculateGroupUinByGroupCode(groupCode: Long): Long { - var left: Long = groupCode / 1000000L + actual fun calculateGroupUinByGroupCode(groupCode: Long): Long = + CommonGroupCalculations.calculateGroupUinByGroupCode(groupCode) - when (left) { - in 0..10 -> left += 202 - in 11..19 -> left += 480 - 11 - in 20..66 -> left += 2100 - 20 - in 67..156 -> left += 2010 - 67 - in 157..209 -> left += 2147 - 157 - in 210..309 -> left += 4100 - 210 - in 310..499 -> left += 3800 - 310 - } - - return left * 1000000L + groupCode % 1000000L - } - - actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long { - var left: Long = groupUin / 1000000L - - when (left) { - in 0 + 202..10 + 202 -> left -= 202 - in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11 - in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20 - in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67 - in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157 - in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210 - in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310 - } - - return left * 1000000L + groupUin % 1000000L - } + actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long = + CommonGroupCalculations.calculateGroupCodeByGroupUin(groupUin) } @MiraiExperimentalAPI diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt index a2d2a1e52..2eef49c68 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt @@ -36,7 +36,6 @@ import kotlin.jvm.JvmSynthetic * @author Him188moe */ // 不要删除多平台结构 !!! kotlin bug @OptIn(MiraiInternalAPI::class, JavaHappyAPI::class) -@Suppress("INAPPLICABLE_JVM_NAME") expect abstract class Contact() : CoroutineScope, ContactJavaHappyAPI { /** * 这个联系人所属 [Bot]. diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index 5cbc18928..762b8ab8d 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -99,7 +99,7 @@ expect abstract class Group() : Contact, CoroutineScope { abstract val owner: Member /** - * [Bot] 在群内的 [Member] 实例 + * [Bot] 在群内的 [newMember] 实例 */ @MiraiExperimentalAPI abstract val botAsMember: Member @@ -158,13 +158,13 @@ expect abstract class Group() : Contact, CoroutineScope { abstract suspend fun quit(): Boolean /** - * 构造一个 [Member]. + * 构造一个 [newMember]. * 非特殊情况请不要使用这个函数. 优先使用 [get]. */ @MiraiExperimentalAPI("dangerous") @Suppress("INAPPLICABLE_JVM_NAME", "FunctionName") @JvmName("newMember") - abstract fun Member(memberInfo: MemberInfo): Member + abstract fun newMember(memberInfo: MemberInfo): Member /** * 向这个对象发送消息. @@ -203,9 +203,46 @@ expect abstract class Group() : Contact, CoroutineScope { fun toFullString(): String } +internal object CommonGroupCalculations { + /** + * by @kar98k + */ + fun calculateGroupUinByGroupCode(groupCode: Long): Long { + var left: Long = groupCode / 1000000L + + when (left) { + in 0..10 -> left += 202 + in 11..19 -> left += 480 - 11 + in 20..66 -> left += 2100 - 20 + in 67..156 -> left += 2010 - 67 + in 157..209 -> left += 2147 - 157 + in 210..309 -> left += 4100 - 210 + in 310..499 -> left += 3800 - 310 + } + + return left * 1000000L + groupCode % 1000000L + } + + fun calculateGroupCodeByGroupUin(groupUin: Long): Long { + var left: Long = groupUin / 1000000L + + when (left) { + in 0 + 202..10 + 202 -> left -= 202 + in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11 + in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20 + in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67 + in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157 + in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210 + in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310 + } + + return left * 1000000L + groupUin % 1000000L + } +} + /** * 返回机器人是否正在被禁言 * * @see Group.botMuteRemaining 剩余禁言时间 */ -val Group.isBotMuted: Boolean get() = this.botMuteRemaining != 0 +inline val Group.isBotMuted: Boolean get() = this.botMuteRemaining != 0 diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt index c92d581c9..05f354eab 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Contact.kt @@ -31,7 +31,6 @@ import net.mamoe.mirai.utils.WeakRefProperty * * @author Him188moe */ -@Suppress("INAPPLICABLE_JVM_NAME") @OptIn(MiraiInternalAPI::class, JavaHappyAPI::class) actual abstract class Contact : CoroutineScope, ContactJavaHappyAPI() { /** diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt index 9473443c2..50ef5c1f0 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/contact/Group.kt @@ -101,7 +101,7 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract val owner: Member /** - * [Bot] 在群内的 [Member] 实例 + * [Bot] 在群内的 [newMember] 实例 */ @MiraiExperimentalAPI actual abstract val botAsMember: Member @@ -159,13 +159,13 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract suspend fun quit(): Boolean /** - * 构造一个 [Member]. + * 构造一个 [newMember]. * 非特殊情况请不要使用这个函数. 优先使用 [get]. */ @JvmName("newMember") @Suppress("INAPPLICABLE_JVM_NAME", "FunctionName") @MiraiExperimentalAPI("dangerous") - actual abstract fun Member(memberInfo: MemberInfo): Member + actual abstract fun newMember(memberInfo: MemberInfo): Member /** * 向这个对象发送消息. @@ -194,40 +194,11 @@ actual abstract class Group : Contact(), CoroutineScope { actual abstract override suspend fun uploadImage(image: ExternalImage): OfflineGroupImage actual companion object { - /** - * by @kar98k - */ - actual fun calculateGroupUinByGroupCode(groupCode: Long): Long { - var left: Long = groupCode / 1000000L + actual fun calculateGroupUinByGroupCode(groupCode: Long): Long = + CommonGroupCalculations.calculateGroupUinByGroupCode(groupCode) - when (left) { - in 0..10 -> left += 202 - in 11..19 -> left += 480 - 11 - in 20..66 -> left += 2100 - 20 - in 67..156 -> left += 2010 - 67 - in 157..209 -> left += 2147 - 157 - in 210..309 -> left += 4100 - 210 - in 310..499 -> left += 3800 - 310 - } - - return left * 1000000L + groupCode % 1000000L - } - - actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long { - var left: Long = groupUin / 1000000L - - when (left) { - in 0 + 202..10 + 202 -> left -= 202 - in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11 - in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20 - in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67 - in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157 - in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210 - in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310 - } - - return left * 1000000L + groupUin % 1000000L - } + actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long = + CommonGroupCalculations.calculateGroupCodeByGroupUin(groupUin) } @MiraiExperimentalAPI