Skip to content

Commit

Permalink
Merge branch 'feat/player-and-chat-prefixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 8, 2023
2 parents 95227fe + 9174f3d commit 79a4e4e
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package com.xpdustry.imperium.common.bridge

import com.xpdustry.imperium.common.message.Message
import com.xpdustry.imperium.common.security.Identity
import kotlinx.serialization.Serializable

@Serializable
data class BridgeChatMessage(val serverName: String, val senderName: String, val message: String) :
data class BridgeChatMessage(val serverName: String, val sender: Identity, val message: String) :
Message
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ sealed interface ServerConfig {
val color: Color = Color.WHITE,
val world: World = World(),
val security: Security = Security(),
val templates: Templates = Templates(),
) : ServerConfig {
init {
require(name != "discord") { "Mindustry Server name cannot be discord" }
Expand All @@ -121,6 +122,12 @@ sealed interface ServerConfig {
val imageProcessingDelay: Duration = 3.seconds,
)

data class Templates(
val chatPrefix: String = "<%prefix%>",
val chatFormat: String =
"[cyan]<[white]%subject_playtime:chaotic%[cyan]> [%subject_color:hex%]%subject_name:display% [cyan]>[white]",
)

companion object {
private val NAME_REGEX = Regex("^[a-z0-9](-?[a-z0-9])+\$")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import java.awt.Color

val MINDUSTRY_ACCENT_COLOR = Color(0xffd37f)

val MINDUSTRY_ORANGE_COLOR = Color(0xffa108)

fun CharSequence.stripMindustryColors(): String {
val out = StringBuilder(length)
var index = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.common.message.Messenger
import com.xpdustry.imperium.common.message.consumer
import com.xpdustry.imperium.common.misc.LoggerDelegate
import com.xpdustry.imperium.discord.misc.identity
import com.xpdustry.imperium.discord.service.DiscordService
import kotlin.jvm.optionals.getOrNull
import kotlinx.coroutines.future.await
Expand All @@ -50,7 +51,9 @@ class BridgeListener(instances: InstanceManager) : ImperiumApplication.Listener
ImperiumScope.MAIN.launch {
messenger.publish(
BridgeChatMessage(
channel.name, event.message.author.name, event.message.content))
channel.name,
event.message.author.asUser().get().identity,
event.message.content))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import com.xpdustry.imperium.common.image.LogicImageAnalysis
import com.xpdustry.imperium.common.inject.InstanceManager
import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.common.misc.toInetAddress
import com.xpdustry.imperium.common.security.Identity
import com.xpdustry.imperium.common.security.Punishment
import com.xpdustry.imperium.common.security.PunishmentManager
import com.xpdustry.imperium.discord.command.InteractionSender
import com.xpdustry.imperium.discord.misc.identity
import java.time.Duration
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.take
Expand Down Expand Up @@ -137,8 +137,7 @@ class ModerationCommand(instances: InstanceManager) : ImperiumApplication.Listen
Punishment.Target(user.lastAddress!!, user._id)
}

punishments.punish(
Identity.Discord(actor.user.name, actor.user.id), lookup, reason, type, duration)
punishments.punish(actor.user.identity, lookup, reason, type, duration)
actor.respond("$verb user $target.")
}

Expand All @@ -161,7 +160,7 @@ class ModerationCommand(instances: InstanceManager) : ImperiumApplication.Listen
return
}

punishments.pardon(Identity.Discord(actor.user.name, actor.user.id), snowflake, reason)
punishments.pardon(actor.user.identity, snowflake, reason)
actor.respond("Pardoned user.")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Imperium, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.discord.misc

import com.xpdustry.imperium.common.security.Identity
import org.javacord.api.entity.user.User

val User.identity: Identity
get() = Identity.Discord(name, id)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.xpdustry.imperium.mindustry.history.BlockHistory
import com.xpdustry.imperium.mindustry.history.SimpleBlockHistory
import com.xpdustry.imperium.mindustry.misc.Entities
import com.xpdustry.imperium.mindustry.placeholder.PlaceholderPipeline
import com.xpdustry.imperium.mindustry.placeholder.SimplePlaceholderManager
import com.xpdustry.imperium.mindustry.placeholder.SimplePlaceholderPipeline
import com.xpdustry.imperium.mindustry.security.GatekeeperPipeline
import com.xpdustry.imperium.mindustry.security.SimpleGatekeeperPipeline
import fr.xpdustry.distributor.api.plugin.MindustryPlugin
Expand All @@ -63,7 +63,7 @@ fun MindustryModule(plugin: ImperiumPlugin) =

single<ChatMessagePipeline> { SimpleChatMessagePipeline() }

single<PlaceholderPipeline> { SimplePlaceholderManager() }
single<PlaceholderPipeline> { SimplePlaceholderPipeline() }

single<Path>("directory") { plugin.directory }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,49 @@ import com.xpdustry.imperium.common.application.ImperiumApplication
import com.xpdustry.imperium.common.async.ImperiumScope
import com.xpdustry.imperium.common.bridge.BridgeChatMessage
import com.xpdustry.imperium.common.bridge.MindustryPlayerMessage
import com.xpdustry.imperium.common.config.ImperiumConfig
import com.xpdustry.imperium.common.config.ServerConfig
import com.xpdustry.imperium.common.inject.InstanceManager
import com.xpdustry.imperium.common.inject.get
import com.xpdustry.imperium.common.message.Messenger
import com.xpdustry.imperium.common.message.consumer
import com.xpdustry.imperium.common.misc.logger
import com.xpdustry.imperium.common.misc.stripMindustryColors
import com.xpdustry.imperium.common.misc.toHexString
import com.xpdustry.imperium.common.security.Identity
import com.xpdustry.imperium.mindustry.misc.Entities
import com.xpdustry.imperium.mindustry.misc.identity
import com.xpdustry.imperium.mindustry.placeholder.PlaceholderContext
import com.xpdustry.imperium.mindustry.placeholder.PlaceholderPipeline
import fr.xpdustry.distributor.api.event.EventHandler
import java.awt.Color
import kotlinx.coroutines.launch
import mindustry.game.EventType
import mindustry.gen.Iconc

private val BLURPLE = Color(0x5865F2)
private val logger = logger("ROOT")

class BridgeChatMessageListener(instances: InstanceManager) : ImperiumApplication.Listener {
private val config = instances.get<ImperiumConfig>()
private val config = instances.get<ServerConfig.Mindustry>()
private val messenger = instances.get<Messenger>()
private val pipeline = instances.get<ChatMessagePipeline>()
private val chatMessagePipeline = instances.get<ChatMessagePipeline>()
private val placeholderPipeline = instances.get<PlaceholderPipeline>()

override fun onImperiumInit() {
messenger.consumer<BridgeChatMessage> {
if (it.serverName != config.server.name) return@consumer
if (it.serverName != config.name) return@consumer
// The null target represents the server, for logging purposes
(Entities.PLAYERS + listOf(null)).forEach { target ->
ImperiumScope.MAIN.launch {
val processed = pipeline.pump(ChatMessageContext(null, target, it.message))
val processed =
chatMessagePipeline.pump(ChatMessageContext(null, target, it.message))
if (processed.isBlank()) return@launch
target?.sendMessage(
"[coral][[[white]${Iconc.discord}[]][[[orange]${it.senderName}[coral]]:[white] $processed")
"[${BLURPLE.toHexString()}]${getDiscordChatPrefix()} ${formatChatMessage(it.sender, processed)}")
if (target == null) {
logger.info(
"&fi&lcDiscord ({}): &fr&lw${processed.stripMindustryColors()}",
it.senderName)
it.sender.name)
}
}
}
Expand All @@ -68,25 +76,35 @@ class BridgeChatMessageListener(instances: InstanceManager) : ImperiumApplicatio
ImperiumScope.MAIN.launch {
messenger.publish(
MindustryPlayerMessage(
config.server.name, event.player.identity, MindustryPlayerMessage.Action.Join))
config.name, event.player.identity, MindustryPlayerMessage.Action.Join))
}

@EventHandler
fun onPlayerQuit(event: EventType.PlayerLeave) =
ImperiumScope.MAIN.launch {
messenger.publish(
MindustryPlayerMessage(
config.server.name, event.player.identity, MindustryPlayerMessage.Action.Quit))
config.name, event.player.identity, MindustryPlayerMessage.Action.Quit))
}

@EventHandler
fun onPlayerChat(event: ProcessedPlayerChatEvent) =
ImperiumScope.MAIN.launch {
messenger.publish(
MindustryPlayerMessage(
config.server.name,
config.name,
event.player.identity,
MindustryPlayerMessage.Action.Chat(Strings.stripColors(event.message))),
)
}

private suspend fun formatChatMessage(subject: Identity, message: String): String {
return placeholderPipeline.pump(PlaceholderContext(subject, config.templates.chatFormat)) +
" " +
message
}

private fun getDiscordChatPrefix(): String {
return config.templates.chatPrefix.replace("%prefix%", Iconc.discord.toString())
}
}
Loading

0 comments on commit 79a4e4e

Please sign in to comment.