-
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.
v1.2-beta release
- Loading branch information
Showing
95 changed files
with
1,241 additions
and
278 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM openjdk:8-jre-alpine3.9 | ||
|
||
CMD ["./gradlew", "fatJar"] | ||
|
||
COPY build/libs/app.jar /app.jar | ||
|
||
CMD ["java", "-jar", "/app.jar"] |
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
82 changes: 82 additions & 0 deletions
82
bot/src/main/kotlin/me/y9san9/prizebot/actors/giveaway/AutoRaffleActor.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,82 @@ | ||
package me.y9san9.prizebot.actors.giveaway | ||
|
||
import dev.inmo.tgbotapi.bot.TelegramBot | ||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage | ||
import dev.inmo.tgbotapi.types.ChatId | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import me.y9san9.prizebot.actors.storage.giveaways_active_messages_storage.GiveawaysActiveMessagesStorage | ||
import me.y9san9.prizebot.actors.storage.giveaways_storage.ActiveGiveaway | ||
import me.y9san9.prizebot.actors.storage.giveaways_storage.Giveaway | ||
import me.y9san9.prizebot.actors.storage.giveaways_storage.GiveawaysStorage | ||
import me.y9san9.prizebot.actors.storage.language_codes_storage.LanguageCodesStorage | ||
import me.y9san9.prizebot.actors.storage.participants_storage.ParticipantsStorage | ||
import me.y9san9.prizebot.actors.telegram.updater.GiveawayActiveMessagesUpdater | ||
import me.y9san9.prizebot.resources.locales.Locale | ||
import me.y9san9.telegram.updates.hierarchies.DIBotUpdate | ||
import java.time.Instant | ||
|
||
|
||
object AutoRaffleActor : CoroutineScope { | ||
|
||
/** | ||
* Map of giveaway id to schedule job | ||
*/ | ||
private val scheduled = mutableMapOf<Long, Job>() | ||
|
||
private val scheduledMutex = Mutex() | ||
|
||
suspend fun <T> scheduleAll ( | ||
bot: TelegramBot, | ||
di: T | ||
) where T : ParticipantsStorage, T : GiveawaysActiveMessagesStorage, | ||
T : GiveawaysStorage, T : LanguageCodesStorage = | ||
di.getAllGiveaways() | ||
.filterIsInstance<ActiveGiveaway>() | ||
.forEach { schedule(bot, it, di) } | ||
|
||
suspend fun <T> schedule ( | ||
bot: TelegramBot, | ||
giveaway: ActiveGiveaway, | ||
di: T | ||
) where T : GiveawaysActiveMessagesStorage, T : ParticipantsStorage, | ||
T : GiveawaysStorage, T : LanguageCodesStorage = scheduledMutex.withLock { | ||
val scope = this | ||
|
||
if(giveaway.raffleDate != null && giveaway.id !in scheduled) { | ||
scheduled[giveaway.id] = scope.launch { | ||
val delay = giveaway.raffleDate.toInstant().toEpochMilli() - Instant.now().toEpochMilli() | ||
delay(delay.takeIf { it > 0 } ?: 0) | ||
|
||
scheduledMutex.withLock { | ||
if (giveaway.id in scheduled && di.getGiveawayById(giveaway.id) != null) { | ||
scheduled.remove(giveaway.id) | ||
handleRaffleResult(bot, di, giveaway, RaffleActor.raffle(giveaway.id, di)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun <T> handleRaffleResult ( | ||
bot: TelegramBot, di: T, giveaway: Giveaway, successRaffle: Boolean | ||
) where T : GiveawaysActiveMessagesStorage, T : ParticipantsStorage, | ||
T : GiveawaysStorage, T : LanguageCodesStorage { | ||
if (successRaffle) { | ||
GiveawayActiveMessagesUpdater.update(getEvent(bot, di), giveaway.id) | ||
} else { | ||
di.removeRaffleDate(giveaway.id) | ||
val locale = Locale.with(di.getLanguageCode(giveaway.ownerId)) | ||
bot.sendMessage(ChatId(giveaway.ownerId), locale.cannotRaffleGiveaway(giveaway.title)) | ||
} | ||
} | ||
|
||
|
||
private fun <T> getEvent(bot: TelegramBot, di: T) = object : DIBotUpdate<T> { | ||
override val bot = bot | ||
override val di = di | ||
} | ||
|
||
override val coroutineContext = GlobalScope.coroutineContext + Job() | ||
} |
31 changes: 31 additions & 0 deletions
31
bot/src/main/kotlin/me/y9san9/prizebot/actors/giveaway/CreateGiveawayActor.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,31 @@ | ||
package me.y9san9.prizebot.actors.giveaway | ||
|
||
import me.y9san9.fsm.FSMStateResult | ||
import me.y9san9.fsm.stateResult | ||
import me.y9san9.prizebot.actors.telegram.sender.GiveawayCreatedSender | ||
import me.y9san9.prizebot.extensions.telegram.PrizebotPrivateMessageUpdate | ||
import me.y9san9.prizebot.handlers.private_messages.fsm.states.MainState | ||
import java.time.OffsetDateTime | ||
|
||
|
||
object CreateGiveawayActor { | ||
suspend fun create ( | ||
update: PrizebotPrivateMessageUpdate, | ||
title: String, | ||
participateText: String, | ||
raffleDate: OffsetDateTime? | ||
): FSMStateResult<*> { | ||
|
||
val giveaway = update.di.saveGiveaway ( | ||
update.chatId, title, participateText, | ||
languageCode = update.di.getLanguageCode(update.chatId) ?: update.languageCode, | ||
raffleDate | ||
) | ||
|
||
GiveawayCreatedSender.send(update, giveaway) | ||
|
||
AutoRaffleActor.schedule(update.bot, giveaway, update.di) | ||
|
||
return stateResult(MainState) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package me.y9san9.prizebot.actors.giveaway | ||
|
||
import me.y9san9.prizebot.actors.storage.giveaways_storage.GiveawaysStorage | ||
import me.y9san9.prizebot.actors.storage.participants_storage.ParticipantsStorage | ||
import me.y9san9.random.extensions.shuffledRandomOrg | ||
|
||
|
||
object RaffleActor { | ||
suspend fun <T> raffle ( | ||
giveawayId: Long, | ||
di: T | ||
): Boolean where T : ParticipantsStorage, T : GiveawaysStorage { | ||
val winner = chooseWinner(giveawayId, di) ?: return false | ||
di.finishGiveaway(giveawayId, winner) | ||
return true | ||
} | ||
|
||
private suspend fun <T> chooseWinner ( | ||
giveawayId: Long, | ||
di: T | ||
) where T : ParticipantsStorage = | ||
di.getParticipantsIds(giveawayId) | ||
.shuffledRandomOrg() | ||
.firstOrNull() | ||
} |
16 changes: 0 additions & 16 deletions
16
bot/src/main/kotlin/me/y9san9/prizebot/actors/raffle/RaffleActor.kt
This file was deleted.
Oops, something went wrong.
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
11 changes: 10 additions & 1 deletion
11
bot/src/main/kotlin/me/y9san9/prizebot/actors/storage/giveaways_storage/GiveawayStorage.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
Oops, something went wrong.