Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/stickers/fail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/stickers/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.boolean
import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.int
import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.seconds
import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.string
import com.github.pool_party.resistance_bot.sticker.AssetSticker

object Configuration {

Expand Down Expand Up @@ -50,6 +51,15 @@ object Configuration {

val PLAYERS_GAME_MAXIMUM = BOARDS.keys.maxOrNull()!!

val STICKER_SET_UPDATE by boolean()

object Stickers {

val FAIL = AssetSticker("fail", """❌""")

val SUCCESS = AssetSticker("success", """✔️""")
}

init {
check((PLAYERS_GAME_MINIMUM..PLAYERS_GAME_MAXIMUM).all { it in BOARDS })
if (DEBUG) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/github/pool_party/resistance_bot/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.pool_party.resistance_bot

import com.elbekD.bot.Bot
import com.elbekD.bot.server
import com.github.pool_party.resistance_bot.sticker.updateSet
import com.github.pool_party.resistance_bot.utils.initHandlers

fun main() {
Expand All @@ -22,5 +23,6 @@ fun main() {
}

bot.initHandlers()
if (Configuration.STICKER_SET_UPDATE) bot.updateSet()
bot.start()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package com.github.pool_party.resistance_bot.callback

import com.elbekD.bot.Bot
import com.github.pool_party.resistance_bot.Configuration
import com.github.pool_party.resistance_bot.Configuration.Stickers
import com.github.pool_party.resistance_bot.state.GameState
import com.github.pool_party.resistance_bot.state.RoundResult
import com.github.pool_party.resistance_bot.state.StateStorage
import com.github.pool_party.resistance_bot.state.Vote
import com.github.pool_party.resistance_bot.utils.sendMessageLogging
import com.github.pool_party.resistance_bot.utils.sendStickerLogging
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -29,6 +31,7 @@ class MissionVoteCallback(stateStorage: StateStorage) : AbstractVoteCallback(sta
val result = votes.asSequence().map { it.second }.all { it }

if (result) {
sendStickerLogging(chatId, Stickers.SUCCESS)
sendMessageLogging(chatId, "TODO: mission has completed successfully").join()

if (++state.resistancePoints >= Configuration.WIN_NUMBER) {
Expand All @@ -37,6 +40,7 @@ class MissionVoteCallback(stateStorage: StateStorage) : AbstractVoteCallback(sta
return
}
} else {
sendStickerLogging(chatId, Stickers.FAIL)
sendMessageLogging(chatId, "TODO: mission has failed").join()

if (++state.spyPoints >= Configuration.WIN_NUMBER) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.pool_party.resistance_bot.sticker

import com.elbekD.bot.Bot
import com.elbekD.bot.types.Message
import com.github.pool_party.resistance_bot.utils.resolveName
import java.util.concurrent.CompletableFuture

data class AssetSticker(val fileName: String, val emojis: String) {

private var fileId: String? = null

fun Bot.send(chatId: Long): CompletableFuture<out Message>? {
if (fileId == null) {
val setName = resolveName(StickerSetConfiguration.STICKER_SET_NAME)

val stickerSet = getStickerSet(setName)
.handle { value, _ -> value }
.join()
?: return null

fileId = stickerSet.stickers[StickerSetConfiguration.STICKERS.indexOf(this@AssetSticker)].file_id
}

return fileId?.let { sendSticker(chatId, it) }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.pool_party.resistance_bot.sticker

import com.elbekD.bot.Bot
import com.elbekD.bot.types.StickerSet
import com.github.pool_party.resistance_bot.sticker.StickerSetConfiguration.file
import com.github.pool_party.resistance_bot.utils.resolveName
import mu.KotlinLogging
import kotlin.system.exitProcess

private val logger = KotlinLogging.logger {}

fun Bot.updateSet() {
val setName = resolveName(StickerSetConfiguration.STICKER_SET_NAME)
val stickers = StickerSetConfiguration.STICKERS
if (stickers.isEmpty()) return
val firstSticker = stickers.first()

val stickerSet: StickerSet? = getStickerSet(setName)
.handle { value, _ -> value }
.join()

if (stickerSet != null) {
logger.info("Using existing sticker set, deleting all stickers")
stickerSet.stickers.forEach { deleteStickerFromSet(it.file_id).join() }
} else {
logger.info("Creating new sticker set")
val result = createNewStickerSet(
StickerSetConfiguration.DEV_USER_ID,
setName,
StickerSetConfiguration.TITLE,
firstSticker.emojis,
firstSticker.file,
).join()

if (!result) {
println("Failed")
exitProcess(1)
}
}

logger.info("Adding the rest of the stickers")
stickers.asSequence()
.drop(if (stickerSet != null) 0 else 1)
.forEach {
addStickerToSet(StickerSetConfiguration.DEV_USER_ID, setName, it.emojis, it.file).join()
}
logger.info("Sticker pack successfully updated")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.pool_party.resistance_bot.sticker

import com.github.pool_party.resistance_bot.Configuration.Stickers
import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.long
import com.github.pool_party.resistance_bot.utils.ConfigurationUtils.string
import java.io.File
import kotlin.reflect.full.memberProperties

object StickerSetConfiguration {

val STICKERS = Stickers::class.memberProperties.mapNotNull { it.get(Stickers) as? AssetSticker }

const val TITLE = "@ResistanceOnlineBot Stickers"

val DEV_USER_ID by long()

val STICKER_SET_NAME by string()

val AssetSticker.file
get() = File("assets/stickers/$fileName.png")
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.pool_party.resistance_bot.utils

import com.elbekD.bot.Bot
import com.elbekD.bot.types.InlineKeyboardButton
import com.elbekD.bot.types.InlineKeyboardMarkup
import com.elbekD.bot.types.Message
Expand Down Expand Up @@ -45,3 +46,5 @@ fun makeStopVoteMarkup(chatId: Long, votes: Map<Int, Boolean>): InlineKeyboardMa
makeButton(""" Continue 😎""", false),
).toMarkUp()
}

fun Bot.resolveName(stickerSetName: String) = "${stickerSetName}_by_${getMe().join().username!!}"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.natpryce.konfig.Key
import com.natpryce.konfig.PropertyLocation
import com.natpryce.konfig.booleanType
import com.natpryce.konfig.intType
import com.natpryce.konfig.longType
import com.natpryce.konfig.overriding
import com.natpryce.konfig.stringType
import kotlin.reflect.KProperty
Expand All @@ -26,6 +27,8 @@ object ConfigurationUtils {

fun <K> int() = Configured<Int, K>(intType)

fun <K> long() = Configured<Long, K>(longType)

fun <K> string() = Configured<String, K>(stringType)

fun <K> seconds() = ConfiguredDuration<K>("seconds", Duration::seconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.elbekD.bot.Bot
import com.elbekD.bot.types.InlineKeyboardMarkup
import com.elbekD.bot.types.Message
import com.elbekD.bot.types.ReplyKeyboard
import com.github.pool_party.resistance_bot.sticker.AssetSticker
import mu.KotlinLogging
import java.util.concurrent.CompletableFuture

Expand Down Expand Up @@ -55,3 +56,5 @@ fun Bot.editMessageReplyMarkupLogging(
return editMessageReplyMarkup(chatId, messageId, markup = markup)
.logging("Failed to edit reply markup \"$markup\"")
}

fun Bot.sendStickerLogging(chatId: Long, sticker: AssetSticker) = with(sticker) { send(chatId)?.logging() }
2 changes: 2 additions & 0 deletions src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ registration.extend.seconds=30
stop.game.voting.seconds=30
rejections.number=5
debug=false
sticker.set.name=ResistanceOnlineBotStickers
sticker.set.update=false