-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- started #8(2); - updated some versions; - refactoring;
- Loading branch information
Showing
45 changed files
with
798 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
bot/src/main/kotlin/me/y9san9/prizebot/actors/giveaway/ConditionsChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package me.y9san9.prizebot.actors.giveaway | ||
|
||
import dev.inmo.tgbotapi.bot.TelegramBot | ||
import dev.inmo.tgbotapi.bot.exceptions.RequestException | ||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat | ||
import dev.inmo.tgbotapi.extensions.api.chat.members.getChatMember | ||
import dev.inmo.tgbotapi.types.ChatId | ||
import dev.inmo.tgbotapi.types.ChatMember.MemberChatMember | ||
import dev.inmo.tgbotapi.types.UserId | ||
import dev.inmo.tgbotapi.types.chat.abstracts.UsernameChat | ||
import me.y9san9.prizebot.database.giveaways_storage.ActiveGiveaway | ||
import me.y9san9.prizebot.database.giveaways_storage.conditions_storage.Condition | ||
import me.y9san9.prizebot.extensions.list.on | ||
|
||
|
||
sealed class CheckConditionsResult { | ||
object GiveawayInvalid : CheckConditionsResult() | ||
object NotSubscribedToConditions : CheckConditionsResult() | ||
class FriendsAreNotInvited(val invitedCount: Int, val requiredCount: Int) : CheckConditionsResult() | ||
object Success : CheckConditionsResult() | ||
} | ||
|
||
object ConditionsChecker { | ||
suspend fun check(bot: TelegramBot, participantId: Long, giveaway: ActiveGiveaway): CheckConditionsResult { | ||
giveaway.conditions.list | ||
.on { condition: Condition.Subscription -> | ||
val channel = try { | ||
bot.getChat(ChatId(condition.channelId)) as? UsernameChat | ||
} catch (_: RequestException) { null } | ||
?: return CheckConditionsResult.GiveawayInvalid | ||
|
||
if(channel.username?.username != condition.channelUsername) | ||
return CheckConditionsResult.GiveawayInvalid | ||
|
||
try { | ||
bot.getChatMember(channel.id, UserId(participantId)) | ||
.takeIf { it is MemberChatMember } | ||
} catch (_: RequestException) { null } | ||
?: return CheckConditionsResult.NotSubscribedToConditions | ||
|
||
}.on { _: Condition.Invitations -> TODO() } | ||
|
||
return CheckConditionsResult.Success | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletions
13
bot/src/main/kotlin/me/y9san9/prizebot/actors/giveaway/RaffleActor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package me.y9san9.prizebot.actors.giveaway | ||
|
||
import dev.inmo.tgbotapi.bot.TelegramBot | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.take | ||
import kotlinx.coroutines.flow.toList | ||
import me.y9san9.prizebot.database.giveaways_storage.ActiveGiveaway | ||
import me.y9san9.prizebot.database.giveaways_storage.GiveawaysStorage | ||
import me.y9san9.random.extensions.shuffledRandomOrg | ||
|
||
|
||
object RaffleActor { | ||
suspend fun raffle ( | ||
bot: TelegramBot, | ||
giveaway: ActiveGiveaway | ||
): Boolean { | ||
val winnerIds = chooseWinners(giveaway) ?: return false | ||
val winnerIds = chooseWinners(bot, giveaway) ?: return false | ||
giveaway.finish(winnerIds) | ||
return true | ||
} | ||
|
||
private suspend fun chooseWinners ( | ||
bot: TelegramBot, | ||
giveaway: ActiveGiveaway, | ||
) = giveaway.participants | ||
.shuffledRandomOrg() | ||
.asFlow() | ||
.filter { userId -> ConditionsChecker.check(bot, userId, giveaway) is CheckConditionsResult.Success } | ||
.take(giveaway.winnersCount.value) | ||
.toList() | ||
.takeIf { it.size == giveaway.winnersCount.value } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...main/kotlin/me/y9san9/prizebot/database/giveaways_storage/conditions_storage/Condition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@file:Suppress("MemberVisibilityCanBePrivate", "CanBeParameter") | ||
|
||
package me.y9san9.prizebot.database.giveaways_storage.conditions_storage | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
|
||
@Serializable | ||
inline class PositiveInt internal constructor(val int: Int) { | ||
init { | ||
require(int > 0) { "The value must be positive" } | ||
} | ||
} | ||
|
||
fun Int.wrapPositiveInt() = PositiveInt(this) | ||
|
||
|
||
@Serializable | ||
sealed class Condition { | ||
@Serializable | ||
@SerialName("subscription") | ||
data class Subscription( | ||
val channelId: Long, | ||
val channelUsername: String | ||
) : Condition() | ||
|
||
@Serializable | ||
@SerialName("invitations") | ||
data class Invitations ( | ||
val count: PositiveInt | ||
) : Condition() | ||
} | ||
|
||
|
||
@Serializable | ||
// It is not inline because of serialization bug :( | ||
/*inline*/ class GiveawayConditions internal constructor ( | ||
val list: List<Condition> | ||
) { | ||
init { | ||
require(list.count { it is Condition.Invitations } <= 1) { | ||
"Only one invitations condition allowed" | ||
} | ||
require(list.filterIsInstance<Condition.Invitations>().isEmpty() | ||
|| list.filterIsInstance<Condition.Subscription>().isNotEmpty()) { | ||
"You should add at least one channel in case you want to invite friends" | ||
} | ||
} | ||
} | ||
|
||
fun List<Condition>.wrapGiveawayConditions() = GiveawayConditions(list = this) |
11 changes: 11 additions & 0 deletions
11
...lin/me/y9san9/prizebot/database/giveaways_storage/conditions_storage/ConditionsStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.y9san9.prizebot.database.giveaways_storage.conditions_storage | ||
|
||
import org.jetbrains.exposed.sql.Database | ||
|
||
|
||
internal fun ConditionsStorage(database: Database): ConditionsStorage = TableConditionsStorage(database) | ||
|
||
internal interface ConditionsStorage { | ||
fun addConditions(giveawayId: Long, conditions: GiveawayConditions) | ||
fun loadConditions(giveawayId: Long): GiveawayConditions | ||
} |
Oops, something went wrong.