This commit is contained in:
InsanusMokrassar 2021-04-05 23:34:41 +06:00
parent 338e97770d
commit 6fe5f96e4e

View File

@ -31,6 +31,8 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.toList import kotlinx.coroutines.channels.toList
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import kotlin.random.Random import kotlin.random.Random
@ -256,7 +258,6 @@ data class ExpressionCaptchaProvider(
) { ) {
val userBanDateTime = eventDateTime + checkTimeSpan val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.mapNotNull { user -> newUsers.mapNotNull { user ->
safelyWithoutExceptions {
launch { launch {
doInSubContext { doInSubContext {
val callbackData = ExpressionBuilder.createExpression( val callbackData = ExpressionBuilder.createExpression(
@ -290,8 +291,11 @@ data class ExpressionCaptchaProvider(
} }
} }
var passed = true var passed: Boolean? = null
val passedMutex = Mutex()
val callback: suspend (Boolean) -> Unit = { val callback: suspend (Boolean) -> Unit = {
passedMutex.withLock {
if (passed == null) {
removeRedundantMessages() removeRedundantMessages()
passed = it passed = it
if (it) { if (it) {
@ -302,8 +306,18 @@ data class ExpressionCaptchaProvider(
} }
} }
} }
}
}
val banJob = launch {
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (passed == null) {
callback(false)
stop()
}
}
val job = parallel {
var leftAttempts = attempts var leftAttempts = attempts
waitDataCallbackQuery { waitDataCallbackQuery {
when { when {
@ -313,7 +327,7 @@ data class ExpressionCaptchaProvider(
if (leftAttempts < 1) { if (leftAttempts < 1) {
this this
} else { } else {
launch { answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts) } answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts)
null null
} }
} }
@ -321,15 +335,10 @@ data class ExpressionCaptchaProvider(
} }
}.first() }.first()
banJob.cancel()
callback(leftAttempts > 0) callback(leftAttempts > 0)
} }
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (job.isActive) job.cancel()
callback(passed)
}
}
} }
}.joinAll() }.joinAll()
} }