Skip to content

Commit

Permalink
Update starter/RobotSession
Browse files Browse the repository at this point in the history
  • Loading branch information
kosaka-bun committed Jan 8, 2025
1 parent 9054df7 commit 787e627
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.util.concurrent.TimeUnit
/**
* 会话类,用于记录处于会话状态的qq号和群号,以及这些号码在进入会话状态以后发送的信息
*/
class RobotSession internal constructor(
class RobotSession private constructor(
val group: Long?,
val qq: Long,
/**
Expand All @@ -16,6 +16,14 @@ class RobotSession internal constructor(
private val sessionManager: SessionManager
) : Closeable {

companion object {

@JvmSynthetic
internal fun of(group: Long?, qq: Long, sessionManager: SessionManager): RobotSession = run {
RobotSession(group, qq, sessionManager)
}
}

data class Action(

/**
Expand All @@ -32,9 +40,12 @@ class RobotSession internal constructor(
class TimeoutException : Exception()

@set:JvmName("setReply")
@get:JvmSynthetic
@Volatile
internal var reply: RobotMultipartMessage? = null

@set:JvmSynthetic
@get:JvmSynthetic
internal lateinit var action: Action

/**
Expand All @@ -43,7 +54,7 @@ class RobotSession internal constructor(
* @return 回复
*/
@Throws(TimeoutException::class)
fun waitingForReply(timeout: Int): RobotMultipartMessage {
fun waitForReply(timeout: Int): RobotMultipartMessage {
//等待回复前,先忽略已有的回复
reply = null
var i = 0
Expand All @@ -55,6 +66,28 @@ class RobotSession internal constructor(
return reply!!
}

inline fun waitForReply(
prompt: String? = null,
promptOnInvalidValue: String = "提供的参数有误,请重新输入",
resultPredicate: (String) -> Boolean = { true },
timeout: Int = 60
): String {
reply(prompt!!)
var result: String
while(true) {
try {
result = waitForReply(timeout).contentToString()
val isValid = runCatching { resultPredicate(result) }.getOrDefault(false)
if(isValid) break
reply(promptOnInvalidValue)
} catch(t: Throwable) {
if(t is TimeoutException) throw t
reply(promptOnInvalidValue)
}
}
return result
}

fun reply(message: RobotMultipartMessage) {
sessionManager.framework.reply(group, qq, message)
}
Expand All @@ -63,6 +96,7 @@ class RobotSession internal constructor(
sessionManager.framework.reply(group, qq, message)
}

@JvmSynthetic
internal fun run() {
try {
action.action!!(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import org.springframework.stereotype.Component
* 系统会话列表的管理器
*/
@Component
class SessionManager(@Lazy internal val framework: RobotFramework) {
class SessionManager(
@Lazy
@get:JvmSynthetic
internal val framework: RobotFramework
) {

/**
* 当前会话列表
Expand All @@ -27,7 +31,7 @@ class SessionManager(@Lazy internal val framework: RobotFramework) {
fun openSession(group: Long?, qq: Long, configurer: RobotSession.Action.() -> Unit) {
//检查是否有存在的会话
getCurrentSession(group, qq)?.let { return }
val session = RobotSession(group, qq, this).apply {
val session = RobotSession.of(group, qq, this).apply {
action = RobotSession.Action()
action.configurer()
}
Expand Down Expand Up @@ -59,6 +63,7 @@ class SessionManager(@Lazy internal val framework: RobotFramework) {
}
}

@JvmSynthetic
internal fun closeSession(session: RobotSession) {
sessions.remove(session)
}
Expand Down

0 comments on commit 787e627

Please sign in to comment.