From d2dd3a9f11c205b7915fd7537d1828f8611bceaa Mon Sep 17 00:00:00 2001 From: phinner <62483793+phinner@users.noreply.github.com> Date: Sat, 2 Dec 2023 02:57:05 +0100 Subject: [PATCH] chore: Misc improvements and bugfixes --- .../imperium/common/account/AccountManager.kt | 1 + .../common/database/NonCloseableConnection.kt | 37 ------------------- .../imperium/common/database/SQLProvider.kt | 9 ++++- .../discord/account/RoleSyncListener.kt | 2 - .../discord/commands/PlayerCommand.kt | 5 +-- .../imperium/mindustry/ImperiumPlugin.kt | 15 ++++++++ 6 files changed, 26 insertions(+), 43 deletions(-) delete mode 100644 imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/NonCloseableConnection.kt diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/account/AccountManager.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/account/AccountManager.kt index 6cf0eac0..55aa5239 100644 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/account/AccountManager.kt +++ b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/account/AccountManager.kt @@ -446,6 +446,7 @@ class SimpleAccountManager( it[AccountAchievementTable.account] = account it[AccountAchievementTable.achievement] = achievement it[AccountAchievementTable.completed] = completed + it[progress] = progress.plus(value) } AccountResult.Success to completed diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/NonCloseableConnection.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/NonCloseableConnection.kt deleted file mode 100644 index d43cc7a1..00000000 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/NonCloseableConnection.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 . - */ -package com.xpdustry.imperium.common.database - -import java.sql.Connection -import java.sql.SQLException - -class NonCloseableConnection(private val delegate: Connection) : Connection by delegate { - - @Throws(SQLException::class) fun close0() = this.delegate.close() - - @Throws(SQLException::class) override fun close() = Unit - - @Throws(SQLException::class) - override fun isWrapperFor(iface: Class<*>): Boolean = - iface.isInstance(this.delegate) || this.delegate.isWrapperFor(iface) - - @Throws(SQLException::class) - @Suppress("UNCHECKED_CAST") - override fun unwrap(iface: Class): T = - if (iface.isInstance(this.delegate)) this.delegate as T else this.delegate.unwrap(iface) -} diff --git a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/SQLProvider.kt b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/SQLProvider.kt index 150ca738..dc829e16 100644 --- a/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/SQLProvider.kt +++ b/imperium-common/src/main/kotlin/com/xpdustry/imperium/common/database/SQLProvider.kt @@ -26,6 +26,9 @@ import java.nio.file.Path import java.sql.DriverManager import java.sql.SQLException import kotlin.io.path.absolutePathString +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.runBlocking import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.transactions.TransactionManager import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction @@ -40,6 +43,8 @@ interface SQLProvider { class SimpleSQLProvider(private val config: DatabaseConfig.SQL, private val directory: Path) : SQLProvider, ImperiumApplication.Listener { + private val parent = SupervisorJob() + private val scope = CoroutineScope(ImperiumScope.IO.coroutineContext + parent) private lateinit var database: Database private lateinit var source: HikariDataSource @@ -72,13 +77,15 @@ class SimpleSQLProvider(private val config: DatabaseConfig.SQL, private val dire } override fun onImperiumExit() { + parent.complete() + runBlocking { parent.join() } source.close() } override fun newTransaction(block: () -> T): T = transaction { block() } override suspend fun newSuspendTransaction(block: suspend () -> T): T = - newSuspendedTransaction(ImperiumScope.IO.coroutineContext, database) { block() } + newSuspendedTransaction(scope.coroutineContext, database) { block() } private fun unregisterDriver(name: String) { // Calling Class.forName("com.mysql.cj.jdbc.Driver") is enough to call the static diff --git a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/account/RoleSyncListener.kt b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/account/RoleSyncListener.kt index 15332d42..9ea60372 100644 --- a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/account/RoleSyncListener.kt +++ b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/account/RoleSyncListener.kt @@ -23,7 +23,6 @@ import com.xpdustry.imperium.common.account.RankChangeEvent import com.xpdustry.imperium.common.application.ImperiumApplication import com.xpdustry.imperium.common.async.ImperiumScope import com.xpdustry.imperium.common.command.Command -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 @@ -36,7 +35,6 @@ class RoleSyncListener(instances: InstanceManager) : ImperiumApplication.Listene private val discord = instances.get() private val accounts = instances.get() private val messenger = instances.get() - private val config = instances.get() override fun onImperiumInit() { discord.getMainServer().addServerMemberJoinListener { diff --git a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/commands/PlayerCommand.kt b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/commands/PlayerCommand.kt index 687b6d3e..87a7ad45 100644 --- a/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/commands/PlayerCommand.kt +++ b/imperium-discord/src/main/kotlin/com/xpdustry/imperium/discord/commands/PlayerCommand.kt @@ -29,7 +29,6 @@ import com.xpdustry.imperium.common.user.UserManager import com.xpdustry.imperium.discord.command.InteractionSender import com.xpdustry.imperium.discord.command.annotation.NonEphemeral import com.xpdustry.imperium.discord.service.DiscordService -import java.net.InetAddress import org.javacord.api.entity.message.embed.EmbedBuilder class PlayerCommand(instances: InstanceManager) : ImperiumApplication.Listener { @@ -61,10 +60,10 @@ class PlayerCommand(instances: InstanceManager) : ImperiumApplication.Listener { .apply { if (discord.isAllowed(actor.user, Rank.ADMIN)) { addField("Uuid", "`${user.uuid}`", true) - addField("Last Address", user.lastAddress.hostAddress, true) + addField("Last Address", "`${user.lastAddress.hostAddress}`", true) addField( "Addresses", - details.addresses.joinToString(transform = InetAddress::getHostAddress), + details.addresses.joinToString(transform = { "`${it.hostAddress}`" }), true) } }, diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt index adec694a..91515b37 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt @@ -29,6 +29,8 @@ import com.xpdustry.imperium.common.config.ServerConfig import com.xpdustry.imperium.common.content.MindustryGamemode import com.xpdustry.imperium.common.inject.get import com.xpdustry.imperium.common.version.MindustryVersion +import com.xpdustry.imperium.common.webhook.WebhookMessage +import com.xpdustry.imperium.common.webhook.WebhookMessageSender import com.xpdustry.imperium.mindustry.account.AccountCommand import com.xpdustry.imperium.mindustry.account.AccountListener import com.xpdustry.imperium.mindustry.account.UserSettingsCommand @@ -63,6 +65,7 @@ import fr.xpdustry.distributor.api.localization.LocalizationSourceRegistry import fr.xpdustry.distributor.api.plugin.AbstractMindustryPlugin import java.util.Locale import kotlin.system.exitProcess +import kotlinx.coroutines.runBlocking class ImperiumPlugin : AbstractMindustryPlugin() { private val application = MindustryImperiumApplication(this) @@ -137,6 +140,13 @@ class ImperiumPlugin : AbstractMindustryPlugin() { } } } + + // TODO This is not clean, find a better way + runBlocking { + application.instances + .get() + .send(WebhookMessage(content = "The server has started.")) + } } override fun onExit() { @@ -152,6 +162,11 @@ private class MindustryImperiumApplication(private val plugin: ImperiumPlugin) : if (exited) return exited = true super.exit(status) + runBlocking { + instances + .get() + .send(WebhookMessage(content = "The server has exit with $status code.")) + } when (status) { ExitStatus.EXIT, ExitStatus.INIT_FAILURE -> Core.app.exit()