Skip to content

Commit

Permalink
feat: Misc improvements and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 12, 2023
1 parent cbd1b31 commit 5c12937
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}

Expand All @@ -70,7 +70,7 @@ internal class MongoMindustryMapManager(
.firstOrNull()

override suspend fun findAllMaps(): Flow<MindustryMap> =
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) {
Expand Down Expand Up @@ -136,5 +136,5 @@ internal class MongoMindustryMapManager(
}

override suspend fun searchMap(query: String): Flow<MindustryMap> =
maps.find(Filters.text(query)).sort(Sorts.descending("name"))
maps.find(Filters.text(query)).sort(Sorts.ascending("name"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ class BridgeListener(instances: InstanceManager) : ImperiumApplication.Listener

messenger.consumer<MindustryServerMessage> { 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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,10 +165,9 @@ class AccountCommand(instances: InstanceManager) : ImperiumApplication.Listener
messenger.consumer<VerificationMessage> { 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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,6 +71,7 @@ class ChatMessageListener(instances: InstanceManager) : ImperiumApplication.List
private val accounts = instances.get<AccountManager>()
private val punishments = instances.get<PunishmentManager>()
private val config = instances.get<ServerConfig.Mindustry>()
private val messenger = instances.get<Messenger>()

override fun onImperiumInit() {
// Intercept chat messages, so they go through the async processing pipeline
Expand Down Expand Up @@ -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]"
}
}
}
Expand Down Expand Up @@ -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))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class MindustryCommandRegistry(
PredicatePermission.of<CommandSender>(SimpleCloudKey.of("imperium:$command")) { sender ->
role == Role.EVERYONE ||
sender.isConsole ||
sender.player.admin ||
runBlocking {
accounts.findByIdentity(sender.player.identity)?.roles?.containsRole(role)
?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<LogicImageAnalysis>()
private val history = instances.get<BlockHistory>()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5c12937

Please sign in to comment.