diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/bridge/MindustryServerMessage.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/bridge/MindustryServerMessage.kt index 5d643f7f..4b7942d7 100644 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/bridge/MindustryServerMessage.kt +++ b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/bridge/MindustryServerMessage.kt @@ -22,4 +22,8 @@ import com.xpdustry.imperium.common.security.Identity import kotlinx.serialization.Serializable @Serializable -data class MindustryServerMessage(val server: Identity.Server, val message: String) : Message +data class MindustryServerMessage( + val server: Identity.Server, + val message: String, + val chat: Boolean +) : Message diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/content/MongoMindustryMapManager.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/content/MongoMindustryMapManager.kt index bcf16e45..3388aa93 100644 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/content/MongoMindustryMapManager.kt +++ b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/content/MongoMindustryMapManager.kt @@ -52,7 +52,7 @@ internal class MongoMindustryMapManager( maps = mongo.getCollection("maps", MindustryMap::class) ratings = mongo.getCollection("map_ratings", Rating::class) runBlocking { - maps.index(Indexes.descending("name")) { name("name_index").version(2).unique(true) } + maps.index(Indexes.ascending("name")) { name("name_index").version(3).unique(true) } } } @@ -70,7 +70,7 @@ internal class MongoMindustryMapManager( .firstOrNull() override suspend fun findAllMaps(): Flow = - maps.findAll().sort(Sorts.descending("name")) + maps.findAll().sort(Sorts.ascending("name")) override suspend fun computeAverageScoreByMap(map: ObjectId): Double { if (ratings.count(Filters.eq("map", map)) == 0L) { @@ -136,5 +136,5 @@ internal class MongoMindustryMapManager( } override suspend fun searchMap(query: String): Flow = - maps.find(Filters.text(query)).sort(Sorts.descending("name")) + maps.find(Filters.text(query)).sort(Sorts.ascending("name")) } diff --git a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/bridge/BridgeListener.kt b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/bridge/BridgeListener.kt index 66797b0a..e4ba1c25 100644 --- a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/bridge/BridgeListener.kt +++ b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/bridge/BridgeListener.kt @@ -77,11 +77,12 @@ class BridgeListener(instances: InstanceManager) : ImperiumApplication.Listener messenger.consumer { message -> val channel = getLiveChatChannel(message.server) ?: return@consumer - MessageBuilder() - .setAllowedMentions(NO_MENTIONS) - .setContent(":purple_square: ${message.message}") - .send(channel) - .await() + val text = buildString { + append(":purple_square: ") + if (message.chat) append("**${message.server.name}**: ") + append(message.message) + } + MessageBuilder().setAllowedMentions(NO_MENTIONS).setContent(text).send(channel).await() } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountCommand.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountCommand.kt index dbb020f4..60ee5ece 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountCommand.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountCommand.kt @@ -38,6 +38,7 @@ import com.xpdustry.imperium.mindustry.misc.Entities import com.xpdustry.imperium.mindustry.misc.identity import com.xpdustry.imperium.mindustry.misc.runMindustryThread import com.xpdustry.imperium.mindustry.misc.showInfoMessage +import com.xpdustry.imperium.mindustry.misc.tryGrantAdmin import com.xpdustry.imperium.mindustry.ui.Interface import com.xpdustry.imperium.mindustry.ui.View import com.xpdustry.imperium.mindustry.ui.action.BiAction @@ -164,10 +165,9 @@ class AccountCommand(instances: InstanceManager) : ImperiumApplication.Listener messenger.consumer { message -> if (message.response && verifications.getIfPresent(message.account) == message.code) { verifications.invalidate(message.account) - runMindustryThread { - Entities.PLAYERS.find { it.uuid() == message.uuid } - ?.showInfoMessage("You have been verified!") - } + val player = Entities.PLAYERS.find { it.uuid() == message.uuid } ?: return@consumer + player.showInfoMessage("You have been verified!") + player.tryGrantAdmin(manager) } } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountListener.kt index 6b66ee9c..13e4fb5d 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/account/AccountListener.kt @@ -18,10 +18,8 @@ package com.xpdustry.imperium.mindustry.account import com.xpdustry.imperium.common.account.AccountManager -import com.xpdustry.imperium.common.account.Role import com.xpdustry.imperium.common.account.User import com.xpdustry.imperium.common.account.UserManager -import com.xpdustry.imperium.common.account.containsRole import com.xpdustry.imperium.common.application.ImperiumApplication import com.xpdustry.imperium.common.async.ImperiumScope import com.xpdustry.imperium.common.inject.InstanceManager @@ -30,6 +28,7 @@ import com.xpdustry.imperium.common.misc.toInetAddress 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.misc.tryGrantAdmin import com.xpdustry.imperium.mindustry.security.GatekeeperPipeline import com.xpdustry.imperium.mindustry.security.GatekeeperResult import fr.xpdustry.distributor.api.event.EventHandler @@ -68,11 +67,7 @@ class AccountListener(instances: InstanceManager) : ImperiumApplication.Listener user.addresses += event.player.ip().toInetAddress() user.lastJoin = Instant.now() } - - // Grants admin to moderators - event.player.admin = - accounts.findByIdentity(event.player.identity)?.roles?.containsRole(Role.MODERATOR) - ?: false + event.player.tryGrantAdmin(accounts) } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/BridgeChatMessageListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/BridgeChatMessageListener.kt index a8baa9cd..3bb3e3cb 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/BridgeChatMessageListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/BridgeChatMessageListener.kt @@ -108,7 +108,7 @@ class BridgeChatMessageListener(instances: InstanceManager) : ImperiumApplicatio "Game over! Team ${event.winner.name} is victorious with ${Entities.PLAYERS.size} players online on map ${Vars.state.map.name()}." } ImperiumScope.MAIN.launch { - messenger.publish(MindustryServerMessage(config.identity, message)) + messenger.publish(MindustryServerMessage(config.identity, message, chat = false)) } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt index 594d9a64..1d73dfdb 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/chat/ChatMessageListener.kt @@ -22,11 +22,13 @@ import arc.util.Time import com.xpdustry.imperium.common.account.AccountManager import com.xpdustry.imperium.common.application.ImperiumApplication import com.xpdustry.imperium.common.async.ImperiumScope +import com.xpdustry.imperium.common.bridge.MindustryServerMessage import com.xpdustry.imperium.common.command.Command import com.xpdustry.imperium.common.command.annotation.Greedy 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.misc.MINDUSTRY_ORANGE_COLOR import com.xpdustry.imperium.common.misc.logger import com.xpdustry.imperium.common.misc.stripMindustryColors @@ -69,6 +71,7 @@ class ChatMessageListener(instances: InstanceManager) : ImperiumApplication.List private val accounts = instances.get() private val punishments = instances.get() private val config = instances.get() + private val messenger = instances.get() override fun onImperiumInit() { // Intercept chat messages, so they go through the async processing pipeline @@ -151,7 +154,7 @@ class ChatMessageListener(instances: InstanceManager) : ImperiumApplication.List ?.let { "#$it" } ?: MINDUSTRY_ORANGE_COLOR.toHexString() is Identity.Discord -> MINDUSTRY_ORANGE_COLOR.toHexString() - else -> Color.RED.toHexString() + else -> "[scarlet]" } } } @@ -224,6 +227,8 @@ class ChatMessageListener(instances: InstanceManager) : ImperiumApplication.List processed) if (target == null) { sender.sendMessage("&fi&lcServer: &fr&lw${processed.stripMindustryColors()}") + messenger.publish( + MindustryServerMessage(config.identity, processed, chat = true)) } } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/command/MindustryCommandRegistry.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/command/MindustryCommandRegistry.kt index 0aa4a54c..2b411b6d 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/command/MindustryCommandRegistry.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/command/MindustryCommandRegistry.kt @@ -137,7 +137,6 @@ class MindustryCommandRegistry( PredicatePermission.of(SimpleCloudKey.of("imperium:$command")) { sender -> role == Role.EVERYONE || sender.isConsole || - sender.player.admin || runBlocking { accounts.findByIdentity(sender.player.identity)?.roles?.containsRole(role) ?: false diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/history/SimpleBlockHistory.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/history/SimpleBlockHistory.kt index c42aba5e..337a9f5a 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/history/SimpleBlockHistory.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/history/SimpleBlockHistory.kt @@ -132,7 +132,7 @@ class SimpleBlockHistory(private val config: ServerConfig.Mindustry) : } @EventHandler(priority = Priority.HIGH) - fun onWorldLoadEvent(event: EventType.WorldLoadEvent) { + fun onResetEvent(event: EventType.ResetEvent) { positions.clear() players.clear() } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt index ee5355b4..41450068 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/misc/PlayerExtensions.kt @@ -17,6 +17,9 @@ */ package com.xpdustry.imperium.mindustry.misc +import com.xpdustry.imperium.common.account.AccountManager +import com.xpdustry.imperium.common.account.Role +import com.xpdustry.imperium.common.account.containsRole import com.xpdustry.imperium.common.misc.logger import com.xpdustry.imperium.common.misc.toInetAddress import com.xpdustry.imperium.common.security.Identity @@ -55,4 +58,9 @@ fun NetConnection.kick(reason: String, duration: Duration, silent: Boolean = fal kicked = true } +suspend fun Player.tryGrantAdmin(manager: AccountManager) { + val account = manager.findByIdentity(identity) ?: return + admin = account.roles.containsRole(Role.MODERATOR) || admin +} + private val logger = logger("ROOT") diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/LogicImageAnalysisListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/LogicImageAnalysisListener.kt index af8cdb48..3ae7f0e2 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/LogicImageAnalysisListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/security/LogicImageAnalysisListener.kt @@ -59,6 +59,7 @@ import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import mindustry.Vars +import mindustry.content.Blocks import mindustry.game.EventType import mindustry.gen.Building import mindustry.gen.Call @@ -69,9 +70,6 @@ import mindustry.world.blocks.logic.CanvasBlock import mindustry.world.blocks.logic.LogicBlock import mindustry.world.blocks.logic.LogicDisplay -// TODO -// - Index blocks on map load ? -// - "Less dramatic destruction" - .json probably class LogicImageAnalysisListener(instances: InstanceManager) : ImperiumApplication.Listener { private val analyzer = instances.get() private val history = instances.get() @@ -159,9 +157,11 @@ class LogicImageAnalysisListener(instances: InstanceManager) : ImperiumApplicati } @EventHandler - fun onWorldLoad(event: EventType.WorldLoadEvent) { + fun onResetEvent(event: EventType.ResetEvent) { displays.reset() + drawerQueue.clear() canvases.reset() + pixmapQueue.clear() } @EventHandler @@ -385,11 +385,13 @@ class LogicImageAnalysisListener(instances: InstanceManager) : ImperiumApplicati runMindustryThread { for (block in element.value.blocks) { - Vars.world.tile(block.x, block.y)?.build?.kill() + Vars.world.tile(block.x, block.y)?.setNet(Blocks.air) val data = block.data if (data is LogicImage.Drawer) { for (processor in data.processors) { - Vars.world.tile(processor.x, processor.y)?.build?.kill() + Vars.world + .tile(processor.x, processor.y) + ?.setNet(Blocks.air) } } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/CoreBlockListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/CoreBlockListener.kt index 554aec7b..68b7022c 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/CoreBlockListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/CoreBlockListener.kt @@ -123,36 +123,32 @@ class CoreBlockListener(instances: InstanceManager) : ImperiumApplication.Listen for (player in Groups.player) { if (player.team() == building.team) { player.sendMessage( - "[scarlet]The core cluster [orange]#$index[] at ([orange]${cluster.x}[], [orange]${cluster.x}[]) is under attack!") + "[scarlet]The core cluster [orange]#${index + 1}[] at ([orange]${cluster.x}[], [orange]${cluster.x}[]) is under attack!") } } } @EventHandler - fun onWorldLoadEvent(event: EventType.WorldLoadEvent) { + fun onResetEvent(event: EventType.ResetEvent) { managers.clear() - } - - @EventHandler - fun onPlayEvent(event: EventType.PlayEvent) { - Vars.world.tiles.eachTile { - val building = it.build - if (building is CoreBlock.CoreBuild) { - val manager = getManager(building.team) - if (manager.getElement(building.rx, building.ry) != null) { - return@eachTile - } - manager.addElement( - Cluster.Block( - building.rx, - building.ry, - building.block.size, - Unit, - ), - ) - logger.trace( - "Loaded {} core at ({}, {})", building.team.name, building.rx, building.ry) + Vars.world.tiles.eachTile { tile -> + val building = tile.build + if (building !is CoreBlock.CoreBuild) { + return@eachTile + } + val manager = getManager(building.team) + if (manager.getElement(building.rx, building.ry) != null) { + return@eachTile } + manager.addElement( + Cluster.Block( + building.rx, + building.ry, + building.block.size, + Unit, + ), + ) + logger.trace("Loaded {} core at ({}, {})", building.team.name, building.rx, building.ry) } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/HubListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/HubListener.kt index 0887010a..18cc024e 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/HubListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/world/HubListener.kt @@ -136,7 +136,7 @@ class HubListener(instances: InstanceManager) : ImperiumApplication.Listener { } @EventHandler - fun onPlayEvent(event: EventType.PlayEvent) { + fun onResetEvent(event: EventType.ResetEvent) { portals.clear() portals.putAll(loadPortals()) updatePortals()