Commit 22f2c508 authored by Him188's avatar Him188

Reformat code

parent aa7a3206
......@@ -9,7 +9,7 @@ import net.mamoe.mirai.message.MessagePacket
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
tailrec fun generateSessionKey():String{
tailrec fun generateSessionKey(): String {
fun generateRandomSessionKey(): String {
val all = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"
return buildString(capacity = 8) {
......@@ -20,27 +20,27 @@ tailrec fun generateSessionKey():String{
}
val key = generateRandomSessionKey()
if(!SessionManager.allSession.containsKey(key)){
if (!SessionManager.allSession.containsKey(key)) {
return key
}
return generateSessionKey()
}
object SessionManager {
internal object SessionManager {
val allSession:MutableMap<String,Session> = mutableMapOf()
val allSession: MutableMap<String, Session> = mutableMapOf()
lateinit var authKey:String
lateinit var authKey: String
fun createTempSession():TempSession = TempSession(EmptyCoroutineContext).also {newTempSession ->
fun createTempSession(): TempSession = TempSession(EmptyCoroutineContext).also { newTempSession ->
allSession[newTempSession.key] = newTempSession
//设置180000ms后检测并回收
newTempSession.launch{
newTempSession.launch {
delay(180000)
allSession[newTempSession.key]?.run {
if(this is TempSession)
if (this is TempSession)
closeSession(newTempSession.key)
}
}
......@@ -50,15 +50,13 @@ object SessionManager {
fun containSession(sessionKey: String): Boolean = allSession.containsKey(sessionKey)
fun closeSession(sessionKey: String) = allSession.remove(sessionKey)?.also {it.close() }
fun closeSession(sessionKey: String) = allSession.remove(sessionKey)?.also { it.close() }
fun closeSession(session: Session) = closeSession(session.key)
}
/**
* @author NaturalHG
* 这个用于管理不同Client与Mirai HTTP的会话
......@@ -68,20 +66,19 @@ object SessionManager {
*/
abstract class Session internal constructor(
coroutineContext: CoroutineContext
): CoroutineScope {
) : CoroutineScope {
val supervisorJob = SupervisorJob(coroutineContext[Job])
final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext
val key:String = generateSessionKey()
val key: String = generateSessionKey()
internal open fun close(){
internal open fun close() {
supervisorJob.complete()
}
}
/**
* 任何新链接建立后分配一个[TempSession]
*
......@@ -93,10 +90,10 @@ class TempSession internal constructor(coroutineContext: CoroutineContext) : Ses
* 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot
*/
class AuthedSession internal constructor(val bot: Bot, coroutineContext: CoroutineContext):Session(coroutineContext){
class AuthedSession internal constructor(val bot: Bot, coroutineContext: CoroutineContext) : Session(coroutineContext) {
val messageQueue = MessageQueue()
private val _listener : Listener<MessagePacket<*, *>>
private val _listener: Listener<MessagePacket<*, *>>
init {
bot.subscribeMessages {
......
......@@ -70,6 +70,7 @@ fun ByteReadPacket.debugPrintThis(name: String = ""): ByteReadPacket {
@MiraiDebugAPI("Low efficiency")
@UseExperimental(ExperimentalContracts::class)
inline fun <R> Input.debugIfFail(name: String = "", onFail: (ByteArray) -> ByteReadPacket = { it.toReadPacket() }, block: ByteReadPacket.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
callsInPlace(onFail, InvocationKind.UNKNOWN)
......
package demo.gentleman
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.uploadAsImage
import org.jsoup.Jsoup
import kotlin.random.Random
class GentleImage {
lateinit var contact: Contact
// `Deferred<Image?>` causes a runtime ClassCastException
class GentleImage(val contact: Contact, val keyword: String) {
val image: Deferred<Image> by lazy { getImage(0) }
......@@ -18,18 +17,21 @@ class GentleImage {
fun getImage(r18: Int): Deferred<Image> {
return GlobalScope.async {
withTimeoutOrNull(5 * 1000) {
withTimeoutOrNull(10 * 1000) {
withContext(Dispatchers.IO) {
val result =
JSON.parseObject(
Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18").ignoreContentType(true).timeout(
Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18" + if (keyword.isNotBlank()) "&keyword=$keyword&num=100" else "").ignoreContentType(
true
).timeout(
10_0000
).get().body().text()
)
val url: String
val pid: String
with(result.getJSONArray("data").getJSONObject(0)) {
val data = result.getJSONArray("data")
with(JSONObject(data.getJSONObject(Random.nextInt(0, data.size)))) {
url = this.getString("url")
pid = this.getString("pid")
}
......
......@@ -18,22 +18,20 @@ private const val IMAGE_BUFFER_CAPACITY: Int = 5
@ExperimentalUnsignedTypes
@ExperimentalCoroutinesApi
object Gentlemen : MutableMap<Long, Gentleman> by mutableMapOf() {
fun provide(key: Contact): Gentleman = this.getOrPut(key.id) { Gentleman(key) }
fun provide(key: Contact, keyword: String = ""): Gentleman = this.getOrPut(key.id) { Gentleman(key, keyword) }
}
/**
* 工作是缓存图片
*/
@ExperimentalCoroutinesApi
class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(IMAGE_BUFFER_CAPACITY) {
class Gentleman(private val contact: Contact, private val keyword: String) : Channel<GentleImage> by Channel(IMAGE_BUFFER_CAPACITY) {
init {
GlobalScope.launch {
while (!isClosedForSend) {
send(GentleImage().apply {
contact = this@Gentleman.contact
image// start downloading
send(GentleImage(contact, keyword).apply {
seImage// start downloading
})
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment