Skip to content

Commit

Permalink
Merge pull request #119 from y9san9/#118-duplicating-winners
Browse files Browse the repository at this point in the history
fix(#118): duplicating winners fixed
  • Loading branch information
y9san9 committed Jun 5, 2023
2 parents 57af9b4 + c649f0b commit 42781de
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
10 changes: 7 additions & 3 deletions bot/src/main/kotlin/me/y9san9/prizebot/Prizebot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ class Prizebot (
}
private val database = connectDatabase(databaseConfig)

private val raffleActor = RaffleActor(randomOrgApiKey)
private val autoRaffleActor = AutoRaffleActor(raffleActor)

private val di = PrizebotDI (
giveawaysStorage = GiveawaysStorage(database),
giveawaysStorage = GiveawaysStorage(database, autoRaffleActor = autoRaffleActor),
giveawaysActiveMessagesStorage = GiveawaysActiveMessagesStorage(database),
languageCodesStorage = LanguageCodesStorage(database),
linkedChannelsStorage = LinkedChannelsStorage(database),
userTitlesStorage = UserTitlesStorage(database),
raffleActor = RaffleActor(randomOrgApiKey),
raffleActor = raffleActor,
autoRaffleActor = autoRaffleActor,
conditionsClient = TelegramConditionsClient(scope, bot),
scope = scope
)
Expand Down Expand Up @@ -106,7 +110,7 @@ class Prizebot (
}

private fun scheduleRaffles() = scope.launch {
AutoRaffleActor(di.raffleActor).scheduleAll(bot, di)
autoRaffleActor.scheduleAll(bot, di)
}

private fun makeDatabaseMigrations() = MigrationsApplier.apply(database, databaseMigrations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class AutoRaffleActor(private val raffleActor: RaffleActor) : CoroutineScope {
.filterIsInstance<ActiveGiveaway>()
.forEach { schedule(bot, it, di) }

suspend fun cancelSchedulesRaffle(giveawayId: Long): Boolean {
return scheduledMutex.withLock {
scheduled[giveawayId]?.cancel()
scheduled.remove(giveawayId) != null
}
}

suspend fun schedule (
bot: TelegramBot,
giveaway: ActiveGiveaway,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object CreateGiveawayActor {
update.sendMessage(update.locale.giveawayCreated, replyMarkup = mainMarkup(update))
GiveawaySender.send(update, update.di, giveaway, demo = true)

AutoRaffleActor(update.di.raffleActor).schedule(update.bot, giveaway, update.di)
update.di.autoRaffleActor.schedule(update.bot, giveaway, update.di)

return stateResult(MainState)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.y9san9.prizebot.database.giveaways_storage

import kotlinx.serialization.Serializable
import me.y9san9.prizebot.actors.giveaway.AutoRaffleActor
import me.y9san9.prizebot.database.giveaways_storage.conditions_storage.ConditionsStorage
import me.y9san9.prizebot.database.giveaways_storage.giveaways_patch_storage.GiveawaysPatchStorage
import me.y9san9.prizebot.database.giveaways_storage.participants_storage.ParticipantsStorage
Expand Down Expand Up @@ -52,11 +52,16 @@ data class ActiveGiveaway internal constructor (
override val participantsStorage: ParticipantsStorage,
override val giveawaysPatchStorage: GiveawaysPatchStorage,
override val conditionsStorage: ConditionsStorage,
override val winnersSettings: WinnersSettings
override val winnersSettings: WinnersSettings,
val raffleActor: AutoRaffleActor
) : Giveaway() {
val winnersCount = winnersSettings.winnersCount
fun removeRaffleDate() = giveawaysPatchStorage.removeRaffleDate(id)
fun finish(winnerIds: List<Long>) = giveawaysPatchStorage.finishGiveaway(id, winnerIds)
suspend fun finish(winnerIds: List<Long>) {
giveawaysPatchStorage.finishGiveaway(id, winnerIds)
giveawaysPatchStorage.removeRaffleDate(id)
raffleActor.cancelSchedulesRaffle(id)
}
}

data class FinishedGiveaway internal constructor (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package me.y9san9.prizebot.database.giveaways_storage

import me.y9san9.prizebot.actors.giveaway.AutoRaffleActor
import me.y9san9.prizebot.database.giveaways_storage.conditions_storage.GiveawayConditions
import org.jetbrains.exposed.sql.Database
import java.time.OffsetDateTime


fun GiveawaysStorage(database: Database): GiveawaysStorage = TableGiveawaysStorage(database)
fun GiveawaysStorage(
database: Database,
autoRaffleActor: AutoRaffleActor
): GiveawaysStorage = TableGiveawaysStorage(database, autoRaffleActor)

interface GiveawaysStorage {
fun getGiveawayById(id: Long): Giveaway?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.y9san9.prizebot.database.giveaways_storage

import me.y9san9.prizebot.actors.giveaway.AutoRaffleActor
import me.y9san9.prizebot.database.giveaways_storage.Giveaways.GIVEAWAY_DISPLAY_WINNERS_WITH_EMOJIS
import me.y9san9.prizebot.database.giveaways_storage.Giveaways.GIVEAWAY_ID
import me.y9san9.prizebot.database.giveaways_storage.Giveaways.GIVEAWAY_LANGUAGE_CODE
Expand All @@ -19,7 +20,8 @@ import java.time.OffsetDateTime


internal class TableGiveawaysStorage (
private val database: Database
private val database: Database,
private val autoRaffleActor: AutoRaffleActor
) : GiveawaysStorage {

private val participantsStorage = ParticipantsStorage(database)
Expand Down Expand Up @@ -65,7 +67,8 @@ internal class TableGiveawaysStorage (
WinnersSettings.create (
WinnersCount.create(data[GIVEAWAY_WINNERS_COUNT]),
data[GIVEAWAY_DISPLAY_WINNERS_WITH_EMOJIS]
)
),
autoRaffleActor
)
}

Expand Down Expand Up @@ -93,7 +96,8 @@ internal class TableGiveawaysStorage (
WinnersSettings.create (
WinnersCount.create(this[GIVEAWAY_WINNERS_COUNT]),
this[GIVEAWAY_DISPLAY_WINNERS_WITH_EMOJIS]
)
),
autoRaffleActor
)
else
FinishedGiveaway (
Expand Down
2 changes: 2 additions & 0 deletions bot/src/main/kotlin/me/y9san9/prizebot/di/PrizebotDI.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.y9san9.prizebot.di

import kotlinx.coroutines.CoroutineScope
import me.y9san9.prizebot.actors.giveaway.AutoRaffleActor
import me.y9san9.prizebot.actors.giveaway.RaffleActor
import me.y9san9.prizebot.conditions.BaseConditionsClient
import me.y9san9.prizebot.database.giveaways_active_messages_storage.GiveawaysActiveMessagesStorage
Expand All @@ -16,6 +17,7 @@ class PrizebotDI (
linkedChannelsStorage: LinkedChannelsStorage,
userTitlesStorage: UserTitlesStorage,
val raffleActor: RaffleActor,
val autoRaffleActor: AutoRaffleActor,
val conditionsClient: BaseConditionsClient,
val scope: CoroutineScope
) : GiveawaysStorage by giveawaysStorage,
Expand Down

0 comments on commit 42781de

Please sign in to comment.