From 0d4d8916f0a6dcff5873e0caa2b0f0ea17be6365 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 10:42:04 +0300 Subject: [PATCH 01/14] split gui module --- .../build.gradle.kts | 21 +++++++ .../allratings-gui-bukkit/build.gradle.kts | 21 +++++++ modules/allratings-shared/build.gradle.kts | 21 +++++++ .../api/rating/api/impl/RatingDBApiImpl.kt | 61 ++++++++----------- .../api/rating/di/ApiRatingModule.kt | 3 - .../api-rating/src/test/java/AuctionsTests.kt | 15 +++-- .../astrarating/core/RatingPermission.kt | 2 - .../astrarating/core/cache/JCache.kt | 5 +- .../astrarating/db/rating/entity/UserTable.kt | 1 - modules/gui-core-bukkit/build.gradle.kts | 25 ++++++++ .../astrarating/gui/di/GuiDependencies.kt | 22 +++++++ .../astrarating/gui/di/GuiModule.kt | 30 +++++++++ .../gui/loading/LoadingIndicator.kt | 2 +- .../gui/playerratings/PlayerRatingsGUI.kt | 2 +- .../di/PlayerRatingGuiDependencies.kt | 2 +- .../astrarating/gui/ratings/RatingsGUI.kt | 2 +- .../gui/ratings/di/RatingsGUIDependencies.kt | 2 +- .../astrarating/gui/router/GuiRouter.kt | 0 .../astrarating/gui/router/GuiRouterImpl.kt | 27 +++++--- .../astrarating/gui/slot/NavigationSlots.kt | 2 +- .../astrarating/gui/slot/SlotContext.kt | 2 +- .../astrarating/gui/slot/SortSlots.kt | 2 +- .../astrarating/gui/util/PlayerHeadUtil.kt | 2 +- .../astrarating/gui/util/TimeUtility.kt | 2 +- .../astrarating/gui/util/TranslationDesc.kt | 2 +- .../astrarating/gui/util/ext.kt | 13 ++-- .../astrarating/gui/util/ext_domain.kt | 10 +-- settings.gradle.kts | 1 + spigot/build.gradle.kts | 1 + .../command/rating/RatingCommandParser.kt | 2 +- .../astrarating/di/impl/RootModuleImpl.kt | 9 ++- .../astrarating/gui/di/GuiDependencies.kt | 18 ------ .../astrarating/gui/di/GuiModule.kt | 18 ------ 33 files changed, 225 insertions(+), 123 deletions(-) create mode 100644 modules/allratings-command-bukkit/build.gradle.kts create mode 100644 modules/allratings-gui-bukkit/build.gradle.kts create mode 100644 modules/allratings-shared/build.gradle.kts create mode 100644 modules/gui-core-bukkit/build.gradle.kts create mode 100644 modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt create mode 100644 modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt (98%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt (99%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt (92%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt (99%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt (92%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouter.kt (100%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt (59%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt (99%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt (87%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt (98%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt (94%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt (88%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt (73%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt (59%) rename {spigot => modules/gui-core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt (87%) delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt diff --git a/modules/allratings-command-bukkit/build.gradle.kts b/modules/allratings-command-bukkit/build.gradle.kts new file mode 100644 index 0000000..281cd66 --- /dev/null +++ b/modules/allratings-command-bukkit/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + // klibs + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Test + testImplementation(libs.bundles.testing.kotlin) + testImplementation(libs.tests.kotlin.test) + // Local + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.core) +} diff --git a/modules/allratings-gui-bukkit/build.gradle.kts b/modules/allratings-gui-bukkit/build.gradle.kts new file mode 100644 index 0000000..281cd66 --- /dev/null +++ b/modules/allratings-gui-bukkit/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + // klibs + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Test + testImplementation(libs.bundles.testing.kotlin) + testImplementation(libs.tests.kotlin.test) + // Local + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.core) +} diff --git a/modules/allratings-shared/build.gradle.kts b/modules/allratings-shared/build.gradle.kts new file mode 100644 index 0000000..281cd66 --- /dev/null +++ b/modules/allratings-shared/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + // klibs + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Test + testImplementation(libs.bundles.testing.kotlin) + testImplementation(libs.tests.kotlin.test) + // Local + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.core) +} diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt index 9514196..d746818 100644 --- a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt @@ -5,7 +5,8 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insertAndGetId -import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.or +import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.astrarating.db.rating.entity.UserDAO @@ -19,51 +20,37 @@ import ru.astrainteractive.astrarating.dto.RatingType import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.dto.UserRatingDTO import ru.astrainteractive.astrarating.model.UserModel -import java.io.File -import java.text.SimpleDateFormat -import java.time.Instant -import java.util.Date internal class RatingDBApiImpl( private val database: Database, - private val pluginFolder: File ) : RatingDBApi { - private fun Result.logStackTrace(): Result { - return this.onFailure { - val logsFolder = File(pluginFolder, "logs") - if (!logsFolder.exists()) logsFolder.mkdirs() - val fileName = SimpleDateFormat("dd.MM.yyyy").format(Date.from(Instant.now())) - val logFile = File(logsFolder, "$fileName.log") - if (!logFile.exists()) logFile.createNewFile() - logFile.appendText(it.stackTraceToString() + "\n") - } - } override suspend fun selectUser(playerName: String): Result = kotlin.runCatching { transaction(database) { UserDAO.find { UserTable.minecraftName.eq(playerName.uppercase()) + .or { UserTable.minecraftName.eq(playerName) } }.first().let(UserMapper::toDTO) } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun updateUser(user: UserDTO) = kotlin.runCatching { transaction(database) { val userDao = UserDAO.findById(user.id) ?: error("User not found!") userDao.lastUpdated = System.currentTimeMillis() } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun insertUser(user: UserModel) = kotlin.runCatching { transaction(database) { UserTable.insertAndGetId { it[lastUpdated] = System.currentTimeMillis() it[minecraftUUID] = user.minecraftUUID.toString() - it[minecraftName] = user.minecraftName.uppercase() + it[minecraftName] = user.minecraftName it[discordID] = null }.value } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun insertUserRating( reporter: UserDTO?, @@ -82,7 +69,7 @@ internal class RatingDBApiImpl( it[ratingTypeIndex] = type.ordinal }.value } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun deleteUserRating(it: UserRatingDTO) = kotlin.runCatching { transaction(database) { @@ -90,7 +77,7 @@ internal class RatingDBApiImpl( UserRatingTable.id.eq(it.id) } } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun fetchUserRatings(playerName: String) = kotlin.runCatching { val reportedUser = selectUser(playerName).getOrThrow() @@ -99,7 +86,7 @@ internal class RatingDBApiImpl( UserRatingTable.reportedUser.eq(reportedUser.id) }.map(UserRatingMapper::toDTO) } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun fetchUsersTotalRating() = kotlin.runCatching { transaction(database) { @@ -111,28 +98,30 @@ internal class RatingDBApiImpl( ) } } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun countPlayerTotalDayRated(playerName: String) = kotlin.runCatching { val user = selectUser(playerName).getOrThrow() transaction(database) { - UserRatingTable.select { - UserRatingTable.userCreatedReport.eq(user.id).and { - UserRatingTable.time.greater(System.currentTimeMillis() - 24 * 60 * 60 * 1000) - } - }.count() + UserRatingTable.selectAll() + .where { + UserRatingTable.userCreatedReport.eq(user.id).and { + UserRatingTable.time.greater(System.currentTimeMillis() - 24 * 60 * 60 * 1000) + } + }.count() } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) override suspend fun countPlayerOnPlayerDayRated(playerName: String, ratedPlayerName: String) = kotlin.runCatching { val playerCreatedReport = selectUser(playerName).getOrThrow() val ratedPlayer = selectUser(ratedPlayerName).getOrThrow() transaction(database) { - UserRatingTable.select { - UserRatingTable.userCreatedReport - .eq(playerCreatedReport.id) - .and { UserRatingTable.reportedUser.eq(ratedPlayer.id) } - }.count() + UserRatingTable.selectAll() + .where { + UserRatingTable.userCreatedReport + .eq(playerCreatedReport.id) + .and { UserRatingTable.reportedUser.eq(ratedPlayer.id) } + }.count() } - }.logStackTrace() + }.onFailure(Throwable::printStackTrace) } diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt index 7b927c1..6590359 100644 --- a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt @@ -8,7 +8,6 @@ import ru.astrainteractive.astrarating.api.rating.api.impl.CachedApiImpl import ru.astrainteractive.astrarating.api.rating.api.impl.RatingDBApiImpl import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.getValue -import java.io.File interface ApiRatingModule { val ratingDBApi: RatingDBApi @@ -17,13 +16,11 @@ interface ApiRatingModule { class Default( database: Database, coroutineScope: CoroutineScope, - pluginFolder: File ) : ApiRatingModule { override val ratingDBApi: RatingDBApi by Provider { RatingDBApiImpl( database = database, - pluginFolder = pluginFolder ) } override val cachedApi: CachedApi by Provider { diff --git a/modules/api-rating/src/test/java/AuctionsTests.kt b/modules/api-rating/src/test/java/AuctionsTests.kt index f8fc858..dbd5956 100644 --- a/modules/api-rating/src/test/java/AuctionsTests.kt +++ b/modules/api-rating/src/test/java/AuctionsTests.kt @@ -1,4 +1,11 @@ +import java.io.File +import java.util.UUID +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.runBlocking import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi @@ -10,13 +17,6 @@ import ru.astrainteractive.astrarating.model.UserModel import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.Reloadable import ru.astrainteractive.klibs.kdi.getValue -import java.io.File -import java.util.UUID -import kotlin.test.AfterTest -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotNull class AuctionsTests { @@ -30,7 +30,6 @@ class AuctionsTests { ApiRatingModule.Default( database = database.value, coroutineScope = GlobalScope, - pluginFolder = File("./") ).ratingDBApi } diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/RatingPermission.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/RatingPermission.kt index 5c59130..9a8d511 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/RatingPermission.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/RatingPermission.kt @@ -7,7 +7,5 @@ sealed class RatingPermission(override val value: String) : Permission { data object MaxRatePerDay : RatingPermission("astra_rating.max_rate_per_day") data object SinglePlayerPerDay : RatingPermission("astra_rating.single_player_rate_per_day") data object Vote : RatingPermission("astra_rating.vote") - data object SeeRecords : RatingPermission("astra_rating.see_records") - data object MergeRecords : RatingPermission("astra_rating.merge_records") data object DeleteReport : RatingPermission("delete_report.vote") } diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt index b9f36b9..83dd676 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt @@ -1,12 +1,12 @@ package ru.astrainteractive.astrarating.core.cache import io.github.reactivecircus.cache4k.Cache +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import kotlin.time.Duration -import kotlin.time.Duration.Companion.milliseconds class JCache( expiresAfterAccess: Duration, @@ -23,7 +23,6 @@ class JCache( private class Data(val data: T) { val cachedAt: Long = System.currentTimeMillis() - val a: Any = 1 fun needUpdate(duration: Duration): Boolean { val timePassed = System.currentTimeMillis().minus(cachedAt).milliseconds diff --git a/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/db/rating/entity/UserTable.kt b/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/db/rating/entity/UserTable.kt index 64e60d1..aa324af 100644 --- a/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/db/rating/entity/UserTable.kt +++ b/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/db/rating/entity/UserTable.kt @@ -15,7 +15,6 @@ object UserTable : LongIdTable("users", "user_id") { class UserDAO(id: EntityID) : LongEntity(id) { val minecraftUUID by UserTable.minecraftUUID val minecraftName by UserTable.minecraftName - var discordID by UserTable.discordID var lastUpdated by UserTable.lastUpdated val rating by UserRatingDAO referrersOn UserRatingTable.reportedUser diff --git a/modules/gui-core-bukkit/build.gradle.kts b/modules/gui-core-bukkit/build.gradle.kts new file mode 100644 index 0000000..5de21d6 --- /dev/null +++ b/modules/gui-core-bukkit/build.gradle.kts @@ -0,0 +1,25 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} + +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // Spigot dependencies + compileOnly(libs.minecraft.paper.api) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + implementation(libs.minecraft.astralibs.menu.bukkit) + implementation(libs.minecraft.astralibs.core.bukkit) + implementation(libs.minecraft.astralibs.command) + implementation(libs.minecraft.astralibs.command.bukkit) + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Local + implementation(projects.modules.core) + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.shared) +} diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt new file mode 100644 index 0000000..2632327 --- /dev/null +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt @@ -0,0 +1,22 @@ +package ru.astrainteractive.astrarating.gui.di + +import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer +import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.gui.playerratings.di.PlayerRatingGuiDependencies +import ru.astrainteractive.astrarating.gui.ratings.di.RatingsGUIDependencies +import ru.astrainteractive.klibs.kdi.getValue + +internal interface GuiDependencies : PlayerRatingGuiDependencies, RatingsGUIDependencies { + class Default( + apiRatingModule: ApiRatingModule, + coreModule: CoreModule, + override val translationContext: KyoriComponentSerializer + ) : GuiDependencies { + + override val dbApi = apiRatingModule.ratingDBApi + override val dispatchers = coreModule.dispatchers + override val translation by coreModule.translation + override val config by coreModule.config + } +} diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt new file mode 100644 index 0000000..5ca14bc --- /dev/null +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt @@ -0,0 +1,30 @@ +package ru.astrainteractive.astrarating.gui.di + +import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer +import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.feature.di.SharedModule +import ru.astrainteractive.astrarating.gui.router.GuiRouter +import ru.astrainteractive.astrarating.gui.router.GuiRouterImpl +import ru.astrainteractive.klibs.kdi.Provider +import ru.astrainteractive.klibs.kdi.getValue + +interface GuiModule { + val router: GuiRouter + + class Default( + coreModule: CoreModule, + apiRatingModule: ApiRatingModule, + translationContext: KyoriComponentSerializer, + private val sharedModule: SharedModule + ) : GuiModule { + override val router: GuiRouter by Provider { + GuiRouterImpl( + coreModule = coreModule, + apiRatingModule = apiRatingModule, + translationContext = translationContext, + sharedModule = sharedModule + ) + } + } +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt similarity index 98% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt index d76951f..9a889b2 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/loading/LoadingIndicator.kt @@ -11,7 +11,7 @@ import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer import ru.astrainteractive.astralibs.menu.core.Menu import ru.astrainteractive.astrarating.core.PluginTranslation -class LoadingIndicator( +internal class LoadingIndicator( private val menu: Menu, private val translation: PluginTranslation, private val translationContext: KyoriComponentSerializer diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt similarity index 99% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt index 802f4fb..4014726 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt @@ -40,7 +40,7 @@ import ru.astrainteractive.astrarating.gui.util.normalName import ru.astrainteractive.astrarating.gui.util.offlinePlayer import ru.astrainteractive.astrarating.gui.util.subListFromString -class PlayerRatingsGUI( +internal class PlayerRatingsGUI( selectedPlayerName: String, private val player: Player, private val module: PlayerRatingGuiDependencies, diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt similarity index 92% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt index f0a842e..9d4b02e 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/di/PlayerRatingGuiDependencies.kt @@ -6,7 +6,7 @@ import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.core.PluginTranslation import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface PlayerRatingGuiDependencies { +internal interface PlayerRatingGuiDependencies { val dbApi: RatingDBApi val dispatchers: KotlinDispatchers val translation: PluginTranslation diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt similarity index 99% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt index 5de7063..fbad44d 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt @@ -37,7 +37,7 @@ import ru.astrainteractive.astrarating.gui.util.offlinePlayer import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.getValue -class RatingsGUI( +internal class RatingsGUI( player: Player, private val module: RatingsGUIDependencies, private val allRatingsComponent: AllRatingsComponent, diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt similarity index 92% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt index 5a81789..6447f0b 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/di/RatingsGUIDependencies.kt @@ -6,7 +6,7 @@ import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.core.PluginTranslation import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface RatingsGUIDependencies { +internal interface RatingsGUIDependencies { val dbApi: RatingDBApi val dispatchers: KotlinDispatchers val translation: PluginTranslation diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouter.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouter.kt similarity index 100% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouter.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouter.kt diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt similarity index 59% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt index fad3dd0..8dc8d28 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt @@ -2,28 +2,39 @@ package ru.astrainteractive.astrarating.gui.router import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import ru.astrainteractive.astrarating.di.RootModule +import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer +import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.feature.di.SharedModule import ru.astrainteractive.astrarating.gui.di.GuiDependencies import ru.astrainteractive.astrarating.gui.playerratings.PlayerRatingsGUI import ru.astrainteractive.astrarating.gui.ratings.RatingsGUI import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.getValue -class GuiRouterImpl( - private val rootModule: RootModule, +internal class GuiRouterImpl( + coreModule: CoreModule, + apiRatingModule: ApiRatingModule, + translationContext: KyoriComponentSerializer, + private val sharedModule: SharedModule ) : GuiRouter { - private val scope by rootModule.coreModule.scope - private val dispatchers = rootModule.coreModule.dispatchers + private val scope by coreModule.scope + private val dispatchers = coreModule.dispatchers private val guiDependencies by Provider { - GuiDependencies.Default(rootModule) + GuiDependencies.Default( + coreModule = coreModule, + apiRatingModule = apiRatingModule, + translationContext = translationContext + ) } + override fun navigate(route: GuiRouter.Route) { scope.launch(dispatchers.IO) { val gui = when (route) { is GuiRouter.Route.AllRatings -> RatingsGUI( player = route.executor, module = guiDependencies, - allRatingsComponent = rootModule.sharedModule.allRatingsComponentFactory().create(), + allRatingsComponent = sharedModule.allRatingsComponentFactory().create(), router = this@GuiRouterImpl ) @@ -31,7 +42,7 @@ class GuiRouterImpl( selectedPlayerName = route.selectedPlayerName, player = route.executor, module = guiDependencies, - playerRatingsComponent = rootModule.sharedModule.playerRatingsComponentFactory( + playerRatingsComponent = sharedModule.playerRatingsComponentFactory( playerName = route.selectedPlayerName ).create(), router = this@GuiRouterImpl diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt similarity index 99% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt index f13f581..9eed185 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/NavigationSlots.kt @@ -17,7 +17,7 @@ import ru.astrainteractive.astrarating.gui.util.normalName import ru.astrainteractive.astrarating.gui.util.offlinePlayer import ru.astrainteractive.astrarating.gui.util.toItemStack -class NavigationSlots( +internal class NavigationSlots( slotContext: SlotContext, private val menu: PaginatedInventoryMenu, private val translationContext: KyoriComponentSerializer diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt similarity index 87% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt index 2e3bc42..4246b05 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SlotContext.kt @@ -3,7 +3,7 @@ package ru.astrainteractive.astrarating.gui.slot import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.core.PluginTranslation -interface SlotContext { +internal interface SlotContext { val translation: PluginTranslation val config: EmpireConfig } diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt similarity index 98% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt index 74e0884..1da4a45 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/slot/SortSlots.kt @@ -12,7 +12,7 @@ import ru.astrainteractive.astrarating.gui.util.toItemStack import ru.astrainteractive.astrarating.model.UserRatingsSort import ru.astrainteractive.astrarating.model.UsersRatingsSort -class SortSlots( +internal class SortSlots( slotContext: SlotContext, private val translationContext: KyoriComponentSerializer ) : SlotContext by slotContext { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt similarity index 94% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt index b824dd8..758eab1 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/PlayerHeadUtil.kt @@ -6,7 +6,7 @@ import org.bukkit.OfflinePlayer import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.SkullMeta -object PlayerHeadUtil { +internal object PlayerHeadUtil { fun getHead(player: OfflinePlayer): ItemStack { val item = ItemStack(Material.PLAYER_HEAD) val meta: SkullMeta = item.itemMeta as SkullMeta diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt similarity index 88% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt index 3dadb9c..f22fd55 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TimeUtility.kt @@ -3,7 +3,7 @@ package ru.astrainteractive.astrarating.gui.util import java.text.SimpleDateFormat import java.util.Date -object TimeUtility { +internal object TimeUtility { fun formatToString(time: Long, format: String): String? { return SimpleDateFormat(format).format(Date(time)) } diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt similarity index 73% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt index dd8498a..c493edb 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/TranslationDesc.kt @@ -3,6 +3,6 @@ package ru.astrainteractive.astrarating.gui.util import ru.astrainteractive.astralibs.string.StringDesc import ru.astrainteractive.astrarating.core.PluginTranslation -class TranslationDesc(private val provider: (PluginTranslation) -> StringDesc.Raw) { +internal class TranslationDesc(private val provider: (PluginTranslation) -> StringDesc.Raw) { fun toString(pluginTranslation: PluginTranslation) = provider.invoke(pluginTranslation) } diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt similarity index 59% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt index c5968de..de1aaf5 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt @@ -2,12 +2,12 @@ package ru.astrainteractive.astrarating.gui.util +import kotlin.math.max import org.bukkit.Material import org.bukkit.inventory.ItemStack import ru.astrainteractive.astrarating.core.EmpireConfig -import kotlin.math.max -fun subListFromString(text: String, threshold: Int, cutWords: Boolean): List { +internal fun subListFromString(text: String, threshold: Int, cutWords: Boolean): List { val res = if (cutWords) { text.chunked(threshold) } else { @@ -18,10 +18,11 @@ fun subListFromString(text: String, threshold: Int, cutWords: Boolean): List meta.setCustomModelData(customModelData) - itemMeta = meta } + return itemStack } diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt similarity index 87% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt rename to modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt index b4325de..715c549 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt @@ -2,28 +2,28 @@ package ru.astrainteractive.astrarating.gui.util +import java.util.UUID import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import ru.astrainteractive.astrarating.core.PluginTranslation import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.model.UserRatingsSort import ru.astrainteractive.astrarating.model.UsersRatingsSort -import java.util.UUID -val UserDTO.offlinePlayer: OfflinePlayer +internal val UserDTO.offlinePlayer: OfflinePlayer get() = Bukkit.getOfflinePlayer(UUID.fromString(minecraftUUID)) -val UserDTO.normalName: String +internal val UserDTO.normalName: String get() = Bukkit.getOfflinePlayer(UUID.fromString(minecraftUUID)).name ?: Bukkit.getOfflinePlayer(minecraftName)?.name ?: minecraftName -val UsersRatingsSort.desc: TranslationDesc +internal val UsersRatingsSort.desc: TranslationDesc get() = when (this) { UsersRatingsSort.ASC -> TranslationDesc(PluginTranslation::sortASC) UsersRatingsSort.DESC -> TranslationDesc(PluginTranslation::sortDESC) } -val UserRatingsSort.desc: TranslationDesc +internal val UserRatingsSort.desc: TranslationDesc get() = when (this) { UserRatingsSort.PLAYER_ASC -> TranslationDesc(PluginTranslation::sortPlayerASC) UserRatingsSort.PLAYER_DESC -> TranslationDesc(PluginTranslation::sortPlayerDESC) diff --git a/settings.gradle.kts b/settings.gradle.kts index 67bddfd..ca5285c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -48,3 +48,4 @@ include("modules:api-rating") include("modules:integration-papi") include("modules:shared") include("modules:core") +include("modules:gui-core-bukkit") diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index 58f2e36..da01c82 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -40,6 +40,7 @@ dependencies { implementation(projects.modules.integrationPapi) implementation(projects.modules.shared) implementation(projects.modules.core) + implementation(projects.modules.guiCoreBukkit) } minecraftMultiplatform { dependencies { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt index fe36bb8..761329e 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt @@ -47,7 +47,7 @@ class RatingCommandParser : BukkitCommandParser { "rating" -> { if (sender !is Player) return RatingCommand.Result.NotPlayer commandContext.args.getOrNull(1)?.takeIf(String::isNotBlank)?.let { requestedPlayer -> - RatingCommand.Result.OpenPlayerRatingGui(sender, requestedPlayer.uppercase()) + RatingCommand.Result.OpenPlayerRatingGui(sender, requestedPlayer) } ?: RatingCommand.Result.OpenRatingsGui(sender) } diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt index 1b8adad..48a9d0f 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt @@ -37,12 +37,10 @@ class RootModuleImpl : RootModule { } override val apiRatingModule: ApiRatingModule by Provider { - val plugin by bukkitModule.plugin val scope by coreModule.scope ApiRatingModule.Default( database = dbRatingModule.database, coroutineScope = scope, - plugin.dataFolder ) } @@ -71,7 +69,12 @@ class RootModuleImpl : RootModule { } override val guiModule: GuiModule by lazy { - GuiModule.Default(this) + GuiModule.Default( + coreModule = coreModule, + apiRatingModule = apiRatingModule, + translationContext = bukkitModule.kyoriComponentSerializer.value, + sharedModule = sharedModule + ) } override val eventModule: EventModule by lazy { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt deleted file mode 100644 index 61c50ac..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiDependencies.kt +++ /dev/null @@ -1,18 +0,0 @@ -package ru.astrainteractive.astrarating.gui.di - -import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer -import ru.astrainteractive.astrarating.di.RootModule -import ru.astrainteractive.astrarating.gui.playerratings.di.PlayerRatingGuiDependencies -import ru.astrainteractive.astrarating.gui.ratings.di.RatingsGUIDependencies -import ru.astrainteractive.klibs.kdi.getValue - -interface GuiDependencies : PlayerRatingGuiDependencies, RatingsGUIDependencies { - class Default(rootModule: RootModule) : GuiDependencies { - - override val dbApi = rootModule.apiRatingModule.ratingDBApi - override val dispatchers = rootModule.coreModule.dispatchers - override val translation by rootModule.coreModule.translation - override val config by rootModule.coreModule.config - override val translationContext: KyoriComponentSerializer by rootModule.bukkitModule.kyoriComponentSerializer - } -} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt deleted file mode 100644 index a0f6ad3..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/gui/di/GuiModule.kt +++ /dev/null @@ -1,18 +0,0 @@ -package ru.astrainteractive.astrarating.gui.di - -import ru.astrainteractive.astrarating.di.RootModule -import ru.astrainteractive.astrarating.gui.router.GuiRouter -import ru.astrainteractive.astrarating.gui.router.GuiRouterImpl -import ru.astrainteractive.klibs.kdi.Provider -import ru.astrainteractive.klibs.kdi.getValue - -interface GuiModule { - val router: GuiRouter - class Default(rootModule: RootModule) : GuiModule { - override val router: GuiRouter by Provider { - GuiRouterImpl( - rootModule = rootModule - ) - } - } -} From 9ba6db2d76e1e6cbda1214e594df182d3d3d9246 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 10:55:43 +0300 Subject: [PATCH 02/14] move command to other module --- modules/command-bukkit/build.gradle.kts | 27 +++++++++++ .../astrarating/command/CommandManager.kt | 2 +- .../astrarating/command/EmpireTabCompleter.kt | 2 +- .../command/di/CommandsDependencies.kt | 48 +++++++++++++++++++ .../astrarating/command/di/CommandsModule.kt | 36 ++++++++++++++ .../command/rating/RatingCommand.kt | 2 +- .../command/rating/RatingCommandExecutor.kt | 2 +- .../command/rating/RatingCommandParser.kt | 2 +- .../command/rating/RatingCommandRegistry.kt | 2 +- .../command/reload/ReloadCommand.kt | 2 +- .../command/reload/ReloadCommandRegistry.kt | 9 ++-- modules/core-bukkit/build.gradle.kts | 19 ++++++++ .../astrarating/LifecyclePlugin.kt | 16 +++++++ .../astrarating/di/BukkitModule.kt | 6 +-- settings.gradle.kts | 2 + spigot/build.gradle.kts | 2 + .../astrarating/AstraRating.kt | 3 +- .../command/di/CommandsDependencies.kt | 40 ---------------- .../astrarating/command/di/CommandsModule.kt | 24 ---------- .../astrarating/di/impl/RootModuleImpl.kt | 7 ++- 20 files changed, 171 insertions(+), 82 deletions(-) create mode 100644 modules/command-bukkit/build.gradle.kts rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt (97%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt (83%) create mode 100644 modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt create mode 100644 modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt (93%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt (99%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt (96%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt (98%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt (87%) rename {spigot => modules/command-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt (90%) create mode 100644 modules/core-bukkit/build.gradle.kts create mode 100644 modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/LifecyclePlugin.kt rename {spigot => modules/core-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt (92%) delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt diff --git a/modules/command-bukkit/build.gradle.kts b/modules/command-bukkit/build.gradle.kts new file mode 100644 index 0000000..486d80a --- /dev/null +++ b/modules/command-bukkit/build.gradle.kts @@ -0,0 +1,27 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} + +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // Spigot dependencies + compileOnly(libs.minecraft.paper.api) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + implementation(libs.minecraft.astralibs.menu.bukkit) + implementation(libs.minecraft.astralibs.core.bukkit) + implementation(libs.minecraft.astralibs.command) + implementation(libs.minecraft.astralibs.command.bukkit) + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Local + implementation(projects.modules.core) + implementation(projects.modules.coreBukkit) + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.shared) + implementation(projects.modules.guiCoreBukkit) +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt similarity index 97% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt index 85fa556..53db93a 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/CommandManager.kt @@ -8,7 +8,7 @@ import ru.astrainteractive.astrarating.command.tabCompleter * It's better to create different executors for different commands * @see Reload */ -class CommandManager( +internal class CommandManager( dependencies: CommandsDependencies ) : CommandsDependencies by dependencies { /** diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt similarity index 83% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt index 8393098..dd11c36 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt @@ -6,7 +6,7 @@ import org.bukkit.Bukkit /** * Tab completer for your plugin which is called when player typing commands */ -fun CommandManager.tabCompleter() = plugin.getCommand("arating")?.setTabCompleter { sender, command, label, args -> +internal fun CommandManager.tabCompleter() = plugin.getCommand("arating")?.setTabCompleter { sender, command, label, args -> when { args.size == 1 -> listOf("reload", "like", "dislike", "rating") args.size == 2 && ( diff --git a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt new file mode 100644 index 0000000..4010a73 --- /dev/null +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt @@ -0,0 +1,48 @@ +package ru.astrainteractive.astrarating.command.di + +import kotlinx.coroutines.CoroutineScope +import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer +import ru.astrainteractive.astrarating.LifecyclePlugin +import ru.astrainteractive.astrarating.core.EmpireConfig +import ru.astrainteractive.astrarating.core.PluginTranslation +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.di.BukkitModule +import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase +import ru.astrainteractive.astrarating.feature.di.SharedModule +import ru.astrainteractive.astrarating.gui.di.GuiModule +import ru.astrainteractive.astrarating.gui.router.GuiRouter +import ru.astrainteractive.klibs.kdi.Provider +import ru.astrainteractive.klibs.kdi.getValue +import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers + +internal interface CommandsDependencies { + val plugin: LifecyclePlugin + val addRatingUseCase: AddRatingUseCase + val dispatchers: KotlinDispatchers + val scope: CoroutineScope + val translation: PluginTranslation + val config: EmpireConfig + val kyoriComponentSerializer: KyoriComponentSerializer + val router: GuiRouter + + class Default( + sharedModule: SharedModule, + bukkitModule: BukkitModule, + coreModule: CoreModule, + guiModule: GuiModule + ) : CommandsDependencies { + + override val plugin: LifecyclePlugin by bukkitModule.plugin + override val addRatingUseCase: AddRatingUseCase by Provider { + sharedModule.changeRatingModule.addRatingUseCase + } + override val dispatchers = coreModule.dispatchers + override val scope by coreModule.scope + override val translation by coreModule.translation + override val config by coreModule.config + + override val kyoriComponentSerializer by bukkitModule.kyoriComponentSerializer + + override val router: GuiRouter by Provider { guiModule.router } + } +} diff --git a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt new file mode 100644 index 0000000..66c4814 --- /dev/null +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt @@ -0,0 +1,36 @@ +package ru.astrainteractive.astrarating.command.di + +import CommandManager +import ru.astrainteractive.astralibs.lifecycle.Lifecycle +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.di.BukkitModule +import ru.astrainteractive.astrarating.feature.di.SharedModule +import ru.astrainteractive.astrarating.gui.di.GuiModule + +interface CommandsModule { + val lifecycle: Lifecycle + + class Default( + sharedModule: SharedModule, + bukkitModule: BukkitModule, + coreModule: CoreModule, + guiModule: GuiModule + ) : CommandsModule { + private val commandManager: CommandManager by lazy { + val dependencies = CommandsDependencies.Default( + sharedModule = sharedModule, + bukkitModule = bukkitModule, + coreModule = coreModule, + guiModule = guiModule + ) + CommandManager(dependencies) + } + override val lifecycle: Lifecycle by lazy { + Lifecycle.Lambda( + onEnable = { + commandManager + } + ) + } + } +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt similarity index 93% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt index edad9cf..437d5d2 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommand.kt @@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender import org.bukkit.entity.Player import ru.astrainteractive.astralibs.command.api.command.BukkitCommand -interface RatingCommand : BukkitCommand { +internal interface RatingCommand : BukkitCommand { sealed interface Result { data object WrongUsage : Result class ChangeRating( diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt similarity index 99% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt index 4bf0191..c173b9d 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt @@ -16,7 +16,7 @@ import ru.astrainteractive.astrarating.gui.router.GuiRouter import ru.astrainteractive.astrarating.model.PlayerModel import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -class RatingCommandExecutor( +internal class RatingCommandExecutor( private val addRatingUseCase: AddRatingUseCase, private val translation: PluginTranslation, private val coroutineScope: CoroutineScope, diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt similarity index 96% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt index 761329e..da5240f 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandParser.kt @@ -6,7 +6,7 @@ import org.bukkit.entity.Player import ru.astrainteractive.astralibs.command.api.context.BukkitCommandContext import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser -class RatingCommandParser : BukkitCommandParser { +internal class RatingCommandParser : BukkitCommandParser { private fun parseChangeRating( label: String, diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt similarity index 98% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt index 762d6c4..7f9b751 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandRegistry.kt @@ -12,7 +12,7 @@ import ru.astrainteractive.astrarating.gui.router.GuiRouter import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers @Suppress("LongParameterList") -class RatingCommandRegistry( +internal class RatingCommandRegistry( private val plugin: JavaPlugin, private val addRatingUseCase: AddRatingUseCase, private val translation: PluginTranslation, diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt similarity index 87% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt index 9d762de..7559c53 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommand.kt @@ -3,7 +3,7 @@ package ru.astrainteractive.astrarating.command.reload import org.bukkit.command.CommandSender import ru.astrainteractive.astralibs.command.api.command.BukkitCommand -interface ReloadCommand : BukkitCommand { +internal interface ReloadCommand : BukkitCommand { sealed interface Result { data object NoPermission : Result class Success(val sender: CommandSender) : Result diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt similarity index 90% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt rename to modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt index f24e96b..0287d55 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/reload/ReloadCommandRegistry.kt @@ -1,18 +1,17 @@ package ru.astrainteractive.astrarating.command.reload -import org.bukkit.plugin.java.JavaPlugin import ru.astrainteractive.astralibs.command.api.commandfactory.BukkitCommandFactory import ru.astrainteractive.astralibs.command.api.parser.BukkitCommandParser import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistry import ru.astrainteractive.astralibs.command.api.registry.BukkitCommandRegistryContext.Companion.toCommandRegistryContext import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible -import ru.astrainteractive.astrarating.AstraRating +import ru.astrainteractive.astrarating.LifecyclePlugin import ru.astrainteractive.astrarating.core.PluginTranslation import ru.astrainteractive.astrarating.core.RatingPermission -class ReloadCommandRegistry( - private val plugin: JavaPlugin, +internal class ReloadCommandRegistry( + private val plugin: LifecyclePlugin, private val translation: PluginTranslation, private val kyoriComponentSerializer: KyoriComponentSerializer ) : KyoriComponentSerializer by kyoriComponentSerializer { @@ -27,7 +26,7 @@ class ReloadCommandRegistry( }, commandExecutor = { it.sender.sendMessage(translation.reload.let(::toComponent)) - (plugin as AstraRating).reloadPlugin() + plugin.onReload() it.sender.sendMessage(translation.reloadComplete.let(::toComponent)) }, commandSideEffect = { context, result -> diff --git a/modules/core-bukkit/build.gradle.kts b/modules/core-bukkit/build.gradle.kts new file mode 100644 index 0000000..a42f5ac --- /dev/null +++ b/modules/core-bukkit/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} + +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + implementation(libs.minecraft.astralibs.core) + implementation(libs.minecraft.astralibs.menu.bukkit) + implementation(libs.minecraft.astralibs.core.bukkit) + implementation(libs.klibs.kdi) + implementation(libs.minecraft.bstats) + implementation(libs.klibs.mikro.core) + implementation("io.github.reactivecircus.cache4k:cache4k-jvm:0.13.0") + // Spigot dependencies + compileOnly(libs.minecraft.paper.api) +} diff --git a/modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/LifecyclePlugin.kt b/modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/LifecyclePlugin.kt new file mode 100644 index 0000000..5ac37fc --- /dev/null +++ b/modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/LifecyclePlugin.kt @@ -0,0 +1,16 @@ +package ru.astrainteractive.astrarating + +import org.bukkit.plugin.java.JavaPlugin +import ru.astrainteractive.astralibs.lifecycle.Lifecycle + +abstract class LifecyclePlugin : JavaPlugin(), Lifecycle { + override fun onEnable() { + super.onEnable() + super.onEnable() + } + + override fun onDisable() { + super.onDisable() + super.onDisable() + } +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt b/modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt similarity index 92% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt rename to modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt index 392ec49..b95ed67 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt +++ b/modules/core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/di/BukkitModule.kt @@ -5,7 +5,7 @@ import ru.astrainteractive.astralibs.event.EventListener import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer import ru.astrainteractive.astralibs.lifecycle.Lifecycle import ru.astrainteractive.astralibs.menu.event.DefaultInventoryClickEvent -import ru.astrainteractive.astrarating.AstraRating +import ru.astrainteractive.astrarating.LifecyclePlugin import ru.astrainteractive.klibs.kdi.Factory import ru.astrainteractive.klibs.kdi.Lateinit import ru.astrainteractive.klibs.kdi.Reloadable @@ -16,7 +16,7 @@ interface BukkitModule { val lifecycle: Lifecycle // Core - val plugin: Lateinit + val plugin: Lateinit val inventoryClickEvent: Single val kyoriComponentSerializer: Reloadable @@ -25,7 +25,7 @@ interface BukkitModule { val eventListener: Single class Default : BukkitModule { - override val plugin = Lateinit() + override val plugin = Lateinit() override val inventoryClickEvent: Single = Single { DefaultInventoryClickEvent() diff --git a/settings.gradle.kts b/settings.gradle.kts index ca5285c..50a5e71 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -48,4 +48,6 @@ include("modules:api-rating") include("modules:integration-papi") include("modules:shared") include("modules:core") +include("modules:core-bukkit") include("modules:gui-core-bukkit") +include("modules:command-bukkit") diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index da01c82..0a43428 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -40,7 +40,9 @@ dependencies { implementation(projects.modules.integrationPapi) implementation(projects.modules.shared) implementation(projects.modules.core) + implementation(projects.modules.coreBukkit) implementation(projects.modules.guiCoreBukkit) + implementation(projects.modules.commandBukkit) } minecraftMultiplatform { dependencies { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/AstraRating.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/AstraRating.kt index 6babfb8..07be920 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/AstraRating.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/AstraRating.kt @@ -1,13 +1,12 @@ package ru.astrainteractive.astrarating -import org.bukkit.plugin.java.JavaPlugin import ru.astrainteractive.astralibs.lifecycle.Lifecycle import ru.astrainteractive.astrarating.di.impl.RootModuleImpl /** * Initial class for your plugin */ -class AstraRating : JavaPlugin() { +class AstraRating : LifecyclePlugin() { private val rootModule = RootModuleImpl() private val lifecycles: List get() = listOfNotNull( diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt deleted file mode 100644 index 53b341c..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsDependencies.kt +++ /dev/null @@ -1,40 +0,0 @@ -package ru.astrainteractive.astrarating.command.di - -import kotlinx.coroutines.CoroutineScope -import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer -import ru.astrainteractive.astrarating.AstraRating -import ru.astrainteractive.astrarating.core.EmpireConfig -import ru.astrainteractive.astrarating.core.PluginTranslation -import ru.astrainteractive.astrarating.di.RootModule -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase -import ru.astrainteractive.astrarating.gui.router.GuiRouter -import ru.astrainteractive.klibs.kdi.Provider -import ru.astrainteractive.klibs.kdi.getValue -import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers - -interface CommandsDependencies { - val plugin: AstraRating - val addRatingUseCase: AddRatingUseCase - val dispatchers: KotlinDispatchers - val scope: CoroutineScope - val translation: PluginTranslation - val config: EmpireConfig - val kyoriComponentSerializer: KyoriComponentSerializer - val router: GuiRouter - - class Default(rootModule: RootModule) : CommandsDependencies { - - override val plugin: AstraRating by rootModule.bukkitModule.plugin - override val addRatingUseCase: AddRatingUseCase by Provider { - rootModule.sharedModule.changeRatingModule.addRatingUseCase - } - override val dispatchers = rootModule.coreModule.dispatchers - override val scope by rootModule.coreModule.scope - override val translation by rootModule.coreModule.translation - override val config by rootModule.coreModule.config - - override val kyoriComponentSerializer by rootModule.bukkitModule.kyoriComponentSerializer - - override val router: GuiRouter by Provider { rootModule.guiModule.router } - } -} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt deleted file mode 100644 index 55a67e3..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/command/di/CommandsModule.kt +++ /dev/null @@ -1,24 +0,0 @@ -package ru.astrainteractive.astrarating.command.di - -import CommandManager -import ru.astrainteractive.astralibs.lifecycle.Lifecycle -import ru.astrainteractive.astrarating.di.RootModule - -interface CommandsModule { - val lifecycle: Lifecycle - val commandManager: CommandManager - - class Default(rootModule: RootModule) : CommandsModule { - override val commandManager: CommandManager by lazy { - val dependencies = CommandsDependencies.Default(rootModule) - CommandManager(dependencies) - } - override val lifecycle: Lifecycle by lazy { - Lifecycle.Lambda( - onEnable = { - commandManager - } - ) - } - } -} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt index 48a9d0f..54b35fd 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt @@ -82,6 +82,11 @@ class RootModuleImpl : RootModule { } override val commandsModule: CommandsModule by lazy { - CommandsModule.Default(this) + CommandsModule.Default( + sharedModule = sharedModule, + bukkitModule = bukkitModule, + coreModule = coreModule, + guiModule = guiModule + ) } } From 61a0af7a3cef480dfd59d16b5f2aa0f19347d3e5 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 11:07:00 +0300 Subject: [PATCH 03/14] split event module --- modules/event-bukkit/build.gradle.kts | 27 ++++++++++++ .../astrarating/event/di/EventDependencies.kt | 28 +++++++----- .../astrarating/event/di/EventModule.kt | 43 +++++++++++++++++++ .../event/kill/KillEventListener.kt | 20 +++++---- settings.gradle.kts | 1 + spigot/build.gradle.kts | 1 + .../astrarating/di/impl/RootModuleImpl.kt | 6 ++- .../astrarating/event/EventManager.kt | 12 ------ .../astrarating/event/di/EventModule.kt | 30 ------------- 9 files changed, 105 insertions(+), 63 deletions(-) create mode 100644 modules/event-bukkit/build.gradle.kts rename {spigot => modules/event-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt (53%) create mode 100644 modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt rename {spigot => modules/event-bukkit}/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt (77%) delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/EventManager.kt delete mode 100644 spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt diff --git a/modules/event-bukkit/build.gradle.kts b/modules/event-bukkit/build.gradle.kts new file mode 100644 index 0000000..486d80a --- /dev/null +++ b/modules/event-bukkit/build.gradle.kts @@ -0,0 +1,27 @@ +plugins { + kotlin("jvm") + kotlin("plugin.serialization") + alias(libs.plugins.klibs.gradle.java.core) +} + +dependencies { + // Kotlin + implementation(libs.bundles.kotlin) + // Spigot dependencies + compileOnly(libs.minecraft.paper.api) + // AstraLibs + implementation(libs.minecraft.astralibs.core) + implementation(libs.minecraft.astralibs.menu.bukkit) + implementation(libs.minecraft.astralibs.core.bukkit) + implementation(libs.minecraft.astralibs.command) + implementation(libs.minecraft.astralibs.command.bukkit) + implementation(libs.klibs.kdi) + implementation(libs.klibs.mikro.core) + // Local + implementation(projects.modules.core) + implementation(projects.modules.coreBukkit) + implementation(projects.modules.apiRating) + implementation(projects.modules.dbRating) + implementation(projects.modules.shared) + implementation(projects.modules.guiCoreBukkit) +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt similarity index 53% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt rename to modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt index c14d80d..df94a18 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt +++ b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventDependencies.kt @@ -5,13 +5,15 @@ import org.bukkit.plugin.java.JavaPlugin import ru.astrainteractive.astralibs.event.EventListener import ru.astrainteractive.astralibs.kyori.KyoriComponentSerializer import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi +import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.core.PluginTranslation -import ru.astrainteractive.astrarating.di.RootModule +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.di.BukkitModule import ru.astrainteractive.klibs.kdi.getValue import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface EventDependencies { +internal interface EventDependencies { val configDependency: EmpireConfig val apiDependency: RatingDBApi val translationDependency: PluginTranslation @@ -21,15 +23,19 @@ interface EventDependencies { val dispatchers: KotlinDispatchers val translationContext: KyoriComponentSerializer - class Default(rootModule: RootModule) : EventDependencies { + class Default( + coreModule: CoreModule, + apiRatingModule: ApiRatingModule, + bukkitModule: BukkitModule + ) : EventDependencies { - override val configDependency by rootModule.coreModule.config - override val apiDependency = rootModule.apiRatingModule.ratingDBApi - override val translationDependency by rootModule.coreModule.translation - override val scope by rootModule.coreModule.scope - override val eventListener by rootModule.bukkitModule.eventListener - override val plugin by rootModule.bukkitModule.plugin - override val dispatchers = rootModule.coreModule.dispatchers - override val translationContext: KyoriComponentSerializer by rootModule.bukkitModule.kyoriComponentSerializer + override val configDependency by coreModule.config + override val apiDependency = apiRatingModule.ratingDBApi + override val translationDependency by coreModule.translation + override val scope by coreModule.scope + override val eventListener by bukkitModule.eventListener + override val plugin by bukkitModule.plugin + override val dispatchers = coreModule.dispatchers + override val translationContext: KyoriComponentSerializer by bukkitModule.kyoriComponentSerializer } } diff --git a/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt new file mode 100644 index 0000000..4b37f4a --- /dev/null +++ b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt @@ -0,0 +1,43 @@ +package ru.astrainteractive.astrarating.event.di + +import org.bukkit.event.HandlerList +import ru.astrainteractive.astralibs.event.EventListener +import ru.astrainteractive.astralibs.lifecycle.Lifecycle +import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule +import ru.astrainteractive.astrarating.core.di.CoreModule +import ru.astrainteractive.astrarating.di.BukkitModule +import ru.astrainteractive.astrarating.event.kill.KillEventListener + +interface EventModule { + val lifecycle: Lifecycle + + class Default( + coreModule: CoreModule, + apiRatingModule: ApiRatingModule, + bukkitModule: BukkitModule + ) : EventModule { + private val dependencies by lazy { + EventDependencies.Default( + coreModule = coreModule, + apiRatingModule = apiRatingModule, + bukkitModule = bukkitModule + ) + } + private val killEvent by lazy { + KillEventListener(dependencies) + } + private val events: List + get() = listOf(killEvent) + override val lifecycle: Lifecycle by lazy { + Lifecycle.Lambda( + onEnable = { + events.forEach { event -> event.onEnable(bukkitModule.plugin.value) } + }, + onDisable = { + events.forEach(EventListener::onDisable) + HandlerList.unregisterAll(bukkitModule.plugin.value) + } + ) + } + } +} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt similarity index 77% rename from spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt rename to modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt index 76c0832..b9ff62d 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt +++ b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt @@ -1,22 +1,24 @@ package ru.astrainteractive.astrarating.event.kill import kotlinx.coroutines.launch +import org.bukkit.event.EventHandler import org.bukkit.event.entity.PlayerDeathEvent -import ru.astrainteractive.astralibs.event.DSLEvent +import ru.astrainteractive.astralibs.event.EventListener import ru.astrainteractive.astrarating.dto.RatingType import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.event.di.EventDependencies import ru.astrainteractive.astrarating.model.UserModel -class KillEventListener( +internal class KillEventListener( module: EventDependencies -) : EventDependencies by module { - - val onPlayerKilledPlayer = DSLEvent(eventListener, plugin) { - if (!configDependency.events.killPlayer.enabled) return@DSLEvent - if (configDependency.events.killPlayer.changeBy == 0) return@DSLEvent - val killedPlayer = it.entity - val killerPlayer = killedPlayer.killer ?: return@DSLEvent +) : EventDependencies by module, + EventListener { + @EventHandler + fun onPlayerKilledPlayer(e: PlayerDeathEvent) { + if (!configDependency.events.killPlayer.enabled) return + if (configDependency.events.killPlayer.changeBy == 0) return + val killedPlayer = e.entity + val killerPlayer = killedPlayer.killer ?: return scope.launch(dispatchers.IO) { val playerDTO = apiDependency.selectUser(killerPlayer.name).getOrNull() ?: run { val user = UserModel( diff --git a/settings.gradle.kts b/settings.gradle.kts index 50a5e71..93db415 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -51,3 +51,4 @@ include("modules:core") include("modules:core-bukkit") include("modules:gui-core-bukkit") include("modules:command-bukkit") +include("modules:event-bukkit") diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index 0a43428..1edf1d1 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -43,6 +43,7 @@ dependencies { implementation(projects.modules.coreBukkit) implementation(projects.modules.guiCoreBukkit) implementation(projects.modules.commandBukkit) + implementation(projects.modules.eventBukkit) } minecraftMultiplatform { dependencies { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt index 54b35fd..d6da461 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt @@ -78,7 +78,11 @@ class RootModuleImpl : RootModule { } override val eventModule: EventModule by lazy { - EventModule.Default(this) + EventModule.Default( + coreModule = coreModule, + apiRatingModule = apiRatingModule, + bukkitModule = bukkitModule + ) } override val commandsModule: CommandsModule by lazy { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/EventManager.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/EventManager.kt deleted file mode 100644 index e184cc9..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/EventManager.kt +++ /dev/null @@ -1,12 +0,0 @@ -package ru.astrainteractive.astrarating.event - -import ru.astrainteractive.astrarating.event.di.EventDependencies -import ru.astrainteractive.astrarating.event.kill.KillEventListener - -class EventManager( - module: EventDependencies -) { - init { - KillEventListener(module) - } -} diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt deleted file mode 100644 index fea20be..0000000 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/event/di/EventModule.kt +++ /dev/null @@ -1,30 +0,0 @@ -package ru.astrainteractive.astrarating.event.di - -import org.bukkit.event.HandlerList -import ru.astrainteractive.astralibs.lifecycle.Lifecycle -import ru.astrainteractive.astrarating.di.RootModule -import ru.astrainteractive.astrarating.event.EventManager - -interface EventModule { - val lifecycle: Lifecycle - val eventManager: EventManager - - class Default(rootModule: RootModule) : EventModule { - private val dependencies by lazy { - EventDependencies.Default(rootModule) - } - override val eventManager: EventManager by lazy { - EventManager(dependencies) - } - override val lifecycle: Lifecycle by lazy { - Lifecycle.Lambda( - onEnable = { - eventManager - }, - onDisable = { - HandlerList.unregisterAll(rootModule.bukkitModule.plugin.value) - } - ) - } - } -} From 8ba66f5a755f5fb0182a0c47860818d514e1458d Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 11:07:19 +0300 Subject: [PATCH 04/14] fix detekt --- modules/api-rating/src/test/java/AuctionsTests.kt | 14 +++++++------- .../astrarating/command/EmpireTabCompleter.kt | 4 +++- .../astrarating/core/cache/JCache.kt | 4 ++-- .../astrainteractive/astrarating/gui/util/ext.kt | 2 +- .../astrarating/gui/util/ext_domain.kt | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/modules/api-rating/src/test/java/AuctionsTests.kt b/modules/api-rating/src/test/java/AuctionsTests.kt index dbd5956..0a2ce00 100644 --- a/modules/api-rating/src/test/java/AuctionsTests.kt +++ b/modules/api-rating/src/test/java/AuctionsTests.kt @@ -1,11 +1,4 @@ -import java.io.File -import java.util.UUID -import kotlin.test.AfterTest -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotNull import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.runBlocking import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi @@ -17,6 +10,13 @@ import ru.astrainteractive.astrarating.model.UserModel import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.Reloadable import ru.astrainteractive.klibs.kdi.getValue +import java.io.File +import java.util.UUID +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull class AuctionsTests { diff --git a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt index dd11c36..c224a40 100644 --- a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/EmpireTabCompleter.kt @@ -6,7 +6,9 @@ import org.bukkit.Bukkit /** * Tab completer for your plugin which is called when player typing commands */ -internal fun CommandManager.tabCompleter() = plugin.getCommand("arating")?.setTabCompleter { sender, command, label, args -> +internal fun CommandManager.tabCompleter() = plugin.getCommand( + "arating" +)?.setTabCompleter { sender, command, label, args -> when { args.size == 1 -> listOf("reload", "like", "dislike", "rating") args.size == 2 && ( diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt index 83dd676..0534ef1 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/cache/JCache.kt @@ -1,12 +1,12 @@ package ru.astrainteractive.astrarating.core.cache import io.github.reactivecircus.cache4k.Cache -import kotlin.time.Duration -import kotlin.time.Duration.Companion.milliseconds import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds class JCache( expiresAfterAccess: Duration, diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt index de1aaf5..98ac435 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext.kt @@ -2,10 +2,10 @@ package ru.astrainteractive.astrarating.gui.util -import kotlin.math.max import org.bukkit.Material import org.bukkit.inventory.ItemStack import ru.astrainteractive.astrarating.core.EmpireConfig +import kotlin.math.max internal fun subListFromString(text: String, threshold: Int, cutWords: Boolean): List { val res = if (cutWords) { diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt index 715c549..0e00597 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/util/ext_domain.kt @@ -2,13 +2,13 @@ package ru.astrainteractive.astrarating.gui.util -import java.util.UUID import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import ru.astrainteractive.astrarating.core.PluginTranslation import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.model.UserRatingsSort import ru.astrainteractive.astrarating.model.UsersRatingsSort +import java.util.UUID internal val UserDTO.offlinePlayer: OfflinePlayer get() = Bukkit.getOfflinePlayer(UUID.fromString(minecraftUUID)) From 7eb1f06526af462af8dec35c285fef7c88549271 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 23:11:38 +0300 Subject: [PATCH 05/14] better checker --- ....kt => PlayerOnPlayerCounterRepository.kt} | 6 +- ...tory.kt => PlayerTotalRatingRepository.kt} | 6 +- .../changerating/di/ChangeRatingModule.kt | 111 ++++-------------- .../changerating/domain/check/Check.kt | 13 ++ .../domain/check/CheckValidator.kt | 5 + .../domain/check/CheckValidatorImpl.kt | 66 +++++++++++ .../domain/usecase/AddRatingUseCase.kt | 60 ++++------ .../domain/usecase/CanVoteOnPlayerUseCase.kt | 39 ------ .../domain/usecase/CanVoteTodayUseCase.kt | 32 ----- .../domain/usecase/CanVoteUseCase.kt | 22 ---- .../domain/usecase/CheckEnoughTimeUseCase.kt | 24 ---- .../usecase/CheckPlayerExistsUseCase.kt | 24 ---- .../domain/usecase/InsertRatingUseCase.kt | 40 ------- .../domain/usecase/InsertUserUseCase.kt | 4 +- .../domain/usecase/ValidateMessageUseCase.kt | 26 ---- .../playerrating/domain/SortRatingUseCase.kt | 5 +- .../DefaultPlayerRatingsComponent.kt | 2 +- 17 files changed, 143 insertions(+), 342 deletions(-) rename modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/{CanVoteOnPlayerRepository.kt => PlayerOnPlayerCounterRepository.kt} (83%) rename modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/{CanVoteTodayRepository.kt => PlayerTotalRatingRepository.kt} (82%) create mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/Check.kt create mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidator.kt create mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteOnPlayerUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteTodayUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckEnoughTimeUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckPlayerExistsUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertRatingUseCase.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/ValidateMessageUseCase.kt diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteOnPlayerRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt similarity index 83% rename from modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteOnPlayerRepository.kt rename to modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt index 8c473ec..68402fb 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteOnPlayerRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt @@ -4,14 +4,14 @@ import kotlinx.coroutines.withContext import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface CanVoteOnPlayerRepository { +fun interface PlayerOnPlayerCounterRepository { suspend fun countPlayerOnPlayerDayRated(creatorName: String, ratedName: String): Long } -internal class CanVoteOnPlayerRepositoryImpl( +internal class PlayerOnPlayerCounterRepositoryImpl( private val dbApi: RatingDBApi, private val dispatchers: KotlinDispatchers -) : CanVoteOnPlayerRepository { +) : PlayerOnPlayerCounterRepository { override suspend fun countPlayerOnPlayerDayRated(creatorName: String, ratedName: String): Long { return withContext(dispatchers.IO) { dbApi.countPlayerOnPlayerDayRated( diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteTodayRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt similarity index 82% rename from modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteTodayRepository.kt rename to modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt index 24d97f3..53ea3a8 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/CanVoteTodayRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt @@ -4,14 +4,14 @@ import kotlinx.coroutines.withContext import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface CanVoteTodayRepository { +fun interface PlayerTotalRatingRepository { suspend fun countPlayerTotalDayRated(name: String): Long } -internal class CanVoteTodayRepositoryImpl( +internal class PlayerTotalRatingRepositoryImpl( private val dbApi: RatingDBApi, private val dispatchers: KotlinDispatchers -) : CanVoteTodayRepository { +) : PlayerTotalRatingRepository { override suspend fun countPlayerTotalDayRated(name: String): Long { return withContext(dispatchers.IO) { dbApi.countPlayerTotalDayRated(name).getOrNull() ?: 0 diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt index 73ab710..55c6fab 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt @@ -2,53 +2,22 @@ package ru.astrainteractive.astrarating.feature.changerating.di import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.astrarating.core.EmpireConfig -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteOnPlayerRepository -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteOnPlayerRepositoryImpl -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteTodayRepository -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteTodayRepositoryImpl -import ru.astrainteractive.astrarating.feature.changerating.data.InsertRatingRepository import ru.astrainteractive.astrarating.feature.changerating.data.InsertRatingRepositoryImpl -import ru.astrainteractive.astrarating.feature.changerating.data.InsertUserRepository import ru.astrainteractive.astrarating.feature.changerating.data.InsertUserRepositoryImpl import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge +import ru.astrainteractive.astrarating.feature.changerating.data.PlayerOnPlayerCounterRepositoryImpl +import ru.astrainteractive.astrarating.feature.changerating.data.PlayerTotalRatingRepositoryImpl +import ru.astrainteractive.astrarating.feature.changerating.domain.check.CheckValidatorImpl import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteOnPlayerUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteOnPlayerUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteTodayUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteTodayUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckEnoughTimeUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckEnoughTimeUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckPlayerExistsUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckPlayerExistsUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertRatingUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertRatingUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertUserUseCase import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertUserUseCaseImpl -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.ValidateMessageUseCase -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.ValidateMessageUseCaseImpl import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.Reloadable import ru.astrainteractive.klibs.kdi.getValue import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers interface ChangeRatingModule { - val canVoteOnPlayerRepository: CanVoteOnPlayerRepository - val canVoteTodayRepository: CanVoteTodayRepository - val insertRatingRepository: InsertRatingRepository - val insertUserRepository: InsertUserRepository - val addRatingUseCase: AddRatingUseCase - val canVoteOnPlayerUseCase: CanVoteOnPlayerUseCase - val canVoteTodayUseCase: CanVoteTodayUseCase - val canVoteUseCase: CanVoteUseCase - val checkEnoughTimeUseCase: CheckEnoughTimeUseCase - val checkPlayerExistsUseCase: CheckPlayerExistsUseCase - val insertRatingUseCase: InsertRatingUseCase - val insertUserUseCase: InsertUserUseCase - val validateMessageUseCase: ValidateMessageUseCase class Default( private val dbApi: RatingDBApi, @@ -56,63 +25,27 @@ interface ChangeRatingModule { private val dispatchers: KotlinDispatchers, private val platformBridge: Provider ) : ChangeRatingModule { - override val canVoteOnPlayerRepository: CanVoteOnPlayerRepository by Provider { - CanVoteOnPlayerRepositoryImpl(dbApi, dispatchers) - } - override val canVoteTodayRepository: CanVoteTodayRepository by Provider { - CanVoteTodayRepositoryImpl(dbApi, dispatchers) - } - override val insertRatingRepository: InsertRatingRepository by Provider { - InsertRatingRepositoryImpl(dbApi, dispatchers) - } - override val insertUserRepository: InsertUserRepository by Provider { - InsertUserRepositoryImpl(dbApi, dispatchers) - } override val addRatingUseCase: AddRatingUseCase by Provider { AddRatingUseCaseImpl( - canVoteOnPlayerUseCase = canVoteOnPlayerUseCase, - canVoteTodayUseCase = canVoteTodayUseCase, - canVoteUseCase = canVoteUseCase, - checkEnoughTimeUseCase = checkEnoughTimeUseCase, - checkPlayerExistsUseCase = checkPlayerExistsUseCase, - validateMessageUseCase = validateMessageUseCase, - insertRatingUseCase = insertRatingUseCase - ) - } - override val canVoteOnPlayerUseCase: CanVoteOnPlayerUseCase by Provider { - CanVoteOnPlayerUseCaseImpl( - maxRatingPerPlayer = empireConfig.value.maxRatingPerPlayer, - canVoteOnPlayerRepository = canVoteOnPlayerRepository - ) - } - override val canVoteTodayUseCase: CanVoteTodayUseCase by Provider { - CanVoteTodayUseCaseImpl( - maxRatingPerDay = empireConfig.value.maxRatingPerDay, - canVoteTodayRepository = canVoteTodayRepository - ) - } - override val canVoteUseCase: CanVoteUseCase by Provider { - CanVoteUseCaseImpl() - } - override val checkEnoughTimeUseCase: CheckEnoughTimeUseCase by Provider { - CheckEnoughTimeUseCaseImpl(platformBridge.provide()) - } - override val checkPlayerExistsUseCase: CheckPlayerExistsUseCase by Provider { - CheckPlayerExistsUseCaseImpl(platformBridge.provide()) - } - override val insertRatingUseCase: InsertRatingUseCase by Provider { - InsertRatingUseCaseImpl( - insertUserUseCase = insertUserUseCase, - insertRatingRepository = insertRatingRepository - ) - } - override val insertUserUseCase: InsertUserUseCase by Provider { - InsertUserUseCaseImpl(insertUserRepository = insertUserRepository) - } - override val validateMessageUseCase: ValidateMessageUseCase by Provider { - ValidateMessageUseCaseImpl( - minMessageLength = empireConfig.value.minMessageLength, - maxMessageLength = empireConfig.value.maxMessageLength + insertRatingRepository = InsertRatingRepositoryImpl(dbApi = dbApi, dispatchers = dispatchers), + insertUserUseCase = InsertUserUseCaseImpl( + insertUserRepository = InsertUserRepositoryImpl( + dbApi = dbApi, + dispatchers = dispatchers + ) + ), + checkValidator = CheckValidatorImpl( + platformBridge = platformBridge.provide(), + playerTotalRatingRepository = PlayerTotalRatingRepositoryImpl( + dbApi = dbApi, + dispatchers = dispatchers + ), + playerOnPlayerCounterRepository = PlayerOnPlayerCounterRepositoryImpl( + dbApi = dbApi, + dispatchers = dispatchers + ), + config = empireConfig.value, + ) ) } } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/Check.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/Check.kt new file mode 100644 index 0000000..365f65c --- /dev/null +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/Check.kt @@ -0,0 +1,13 @@ +package ru.astrainteractive.astrarating.feature.changerating.domain.check + +import ru.astrainteractive.astrarating.model.PlayerModel + +internal sealed interface Check { + class CanVoteOnPlayer(val creator: PlayerModel, val rated: PlayerModel?) : Check + class CanVoteToday(val playerModel: PlayerModel) : Check + class CanVote(val playerModel: PlayerModel) : Check + class EnoughTime(val playerModel: PlayerModel) : Check + class PlayerExists(val playerModel: PlayerModel) : Check + class MessageCorrect(val message: String) : Check + class NotSamePlayer(val first: PlayerModel, val second: PlayerModel) : Check +} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidator.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidator.kt new file mode 100644 index 0000000..6d32995 --- /dev/null +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidator.kt @@ -0,0 +1,5 @@ +package ru.astrainteractive.astrarating.feature.changerating.domain.check + +internal interface CheckValidator { + suspend fun isValid(check: Check): Boolean +} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt new file mode 100644 index 0000000..a2c0f3d --- /dev/null +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt @@ -0,0 +1,66 @@ +package ru.astrainteractive.astrarating.feature.changerating.domain.check + +import ru.astrainteractive.astrarating.core.EmpireConfig +import ru.astrainteractive.astrarating.core.RatingPermission +import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge +import ru.astrainteractive.astrarating.feature.changerating.data.PlayerOnPlayerCounterRepository +import ru.astrainteractive.astrarating.feature.changerating.data.PlayerTotalRatingRepository + +internal class CheckValidatorImpl( + private val platformBridge: PlatformBridge, + private val playerOnPlayerCounterRepository: PlayerOnPlayerCounterRepository, + private val playerTotalRatingRepository: PlayerTotalRatingRepository, + private val config: EmpireConfig +) : CheckValidator { + private suspend fun checkCanVoteOnPlayer(check: Check.CanVoteOnPlayer): Boolean { + val maxVotePerPlayer = check.creator.permissible + ?.maxPermissionSize(RatingPermission.SinglePlayerPerDay) + ?: config.maxRatingPerPlayer + val votedOnPlayerAmount = playerOnPlayerCounterRepository.countPlayerOnPlayerDayRated( + creatorName = check.creator.name, + ratedName = check.rated?.name ?: error("Rated player doesn't have a name!") + ) + return votedOnPlayerAmount <= maxVotePerPlayer + } + + private suspend fun checkCanVoteToday(check: Check.CanVoteToday): Boolean { + val maxVotesPerDay = check.playerModel.permissible + ?.maxPermissionSize(RatingPermission.MaxRatePerDay) + ?: config.maxRatingPerDay + val todayVotedAmount = playerTotalRatingRepository.countPlayerTotalDayRated(check.playerModel.name) + return todayVotedAmount <= maxVotesPerDay + } + + private fun checkCanVote(check: Check.CanVote): Boolean { + return check.playerModel.permissible?.hasPermission(RatingPermission.Vote) ?: false + } + + private fun checkEnoughTime(check: Check.EnoughTime): Boolean { + return platformBridge.hasEnoughTime(check.playerModel) + } + + private fun checkPlayerExists(check: Check.PlayerExists): Boolean { + return platformBridge.isPlayerExists(check.playerModel) + } + + private fun checkMessageCorrect(check: Check.MessageCorrect): Boolean { + val message = check.message + return !(message.length < config.minMessageLength || message.length > config.maxMessageLength) + } + + private fun checkNotSamePlayer(check: Check.NotSamePlayer): Boolean { + return check.first.uuid != check.second.uuid + } + + override suspend fun isValid(check: Check): Boolean { + return when (check) { + is Check.CanVote -> checkCanVote(check) + is Check.CanVoteOnPlayer -> checkCanVoteOnPlayer(check) + is Check.CanVoteToday -> checkCanVoteToday(check) + is Check.EnoughTime -> checkEnoughTime(check) + is Check.MessageCorrect -> checkMessageCorrect(check) + is Check.PlayerExists -> checkPlayerExists(check) + is Check.NotSamePlayer -> checkNotSamePlayer(check) + } + } +} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/AddRatingUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/AddRatingUseCase.kt index 44bf9aa..2505e5c 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/AddRatingUseCase.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/AddRatingUseCase.kt @@ -1,15 +1,17 @@ package ru.astrainteractive.astrarating.feature.changerating.domain.usecase import ru.astrainteractive.astrarating.dto.RatingType +import ru.astrainteractive.astrarating.feature.changerating.data.InsertRatingRepository +import ru.astrainteractive.astrarating.feature.changerating.domain.check.Check +import ru.astrainteractive.astrarating.feature.changerating.domain.check.CheckValidator import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase.Input import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.AddRatingUseCase.Output import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase /** * This UseCase will try to add rating from player to another player */ -interface AddRatingUseCase : UseCase.Suspended { +interface AddRatingUseCase { class Input( val creator: PlayerModel, val message: String, @@ -28,52 +30,40 @@ interface AddRatingUseCase : UseCase.Suspended { data object MessageNotCorrect : Output data object Success : Output } + + suspend fun invoke(input: Input): Output } @Suppress("LongParameterList") internal class AddRatingUseCaseImpl( - private val canVoteOnPlayerUseCase: CanVoteOnPlayerUseCase, - private val canVoteTodayUseCase: CanVoteTodayUseCase, - private val canVoteUseCase: CanVoteUseCase, - private val checkEnoughTimeUseCase: CheckEnoughTimeUseCase, - private val checkPlayerExistsUseCase: CheckPlayerExistsUseCase, - private val validateMessageUseCase: ValidateMessageUseCase, - private val insertRatingUseCase: InsertRatingUseCase + private val checkValidator: CheckValidator, + private val insertUserUseCase: InsertUserUseCase, + private val insertRatingRepository: InsertRatingRepository ) : AddRatingUseCase { override suspend fun invoke(input: Input): Output { input.ratedPlayerModel ?: return Output.PlayerNotExists - - val canVote = canVoteUseCase.invoke(input.creator) - if (!canVote) return Output.NoPermission - - val isPlayerExists = checkPlayerExistsUseCase.invoke(input.ratedPlayerModel) - if (!isPlayerExists) return Output.PlayerNotExists - - val isSamePlayer = (input.ratedPlayerModel.uuid == input.creator.uuid) - if (isSamePlayer) return Output.SamePlayer - - val isEnoughOnServer = checkEnoughTimeUseCase.invoke(input.creator) - if (!isEnoughOnServer) return Output.NotEnoughOnServer - - val canVoteToday = canVoteTodayUseCase.invoke(input.creator) - if (!canVoteToday) return Output.AlreadyMaxDayVotes - - val canVoteOnPlayer = canVoteOnPlayerUseCase.invoke( - creator = input.creator, - rated = input.ratedPlayerModel + val checkMap = listOf( + Check.CanVote(input.creator) to Output.NoPermission, + Check.PlayerExists(input.ratedPlayerModel) to Output.PlayerNotExists, + Check.NotSamePlayer(input.ratedPlayerModel, input.creator) to Output.SamePlayer, + Check.EnoughTime(input.creator) to Output.NotEnoughOnServer, + Check.CanVoteToday(input.creator) to Output.AlreadyMaxDayVotes, + Check.CanVoteOnPlayer(input.creator, input.ratedPlayerModel) to Output.AlreadyMaxVotesOnPlayer, + Check.MessageCorrect(input.message) to Output.MessageNotCorrect ) - if (!canVoteOnPlayer) return Output.AlreadyMaxVotesOnPlayer + val invalidCheck = checkMap.firstOrNull { (check, _) -> !checkValidator.isValid(check) } + if (invalidCheck != null) return invalidCheck.second - val isMessageCorrect = validateMessageUseCase.invoke(input.message) - if (!isMessageCorrect) return Output.MessageNotCorrect + val ratedPlayerDTO = insertUserUseCase.invoke(input.ratedPlayerModel) + val ratingCreatorDTO = insertUserUseCase.invoke(input.creator) - InsertRatingUseCase.Input( - ratingCreator = input.creator, - ratedPlayer = input.ratedPlayerModel, + insertRatingRepository.insertUserRating( + reporter = ratingCreatorDTO.playerDTO, + reported = ratedPlayerDTO.playerDTO, message = input.message, type = input.type, ratingValue = input.rating - ).let { input -> insertRatingUseCase.invoke(input) } + ) return Output.Success } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteOnPlayerUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteOnPlayerUseCase.kt deleted file mode 100644 index 34327cb..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteOnPlayerUseCase.kt +++ /dev/null @@ -1,39 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.core.RatingPermission -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteOnPlayerRepository -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteOnPlayerUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Checks whether [Input.creator] can vote today on [Input.rated] or not - * - * Player has limited votes on each player - */ -interface CanVoteOnPlayerUseCase : UseCase.Suspended { - class Input(val creator: PlayerModel, val rated: PlayerModel?) - - suspend operator fun invoke( - creator: PlayerModel, - rated: PlayerModel? - ) = invoke(Input(creator, rated)) -} - -internal class CanVoteOnPlayerUseCaseImpl( - private val maxRatingPerPlayer: Int, - private val canVoteOnPlayerRepository: CanVoteOnPlayerRepository -) : CanVoteOnPlayerUseCase { - override suspend fun invoke(input: Input): Boolean { - val creator = input.creator - val rated = input.rated - val maxVotePerPlayer = creator.permissible?.maxPermissionSize( - RatingPermission.SinglePlayerPerDay - ) ?: maxRatingPerPlayer - val votedOnPlayerAmount = canVoteOnPlayerRepository.countPlayerOnPlayerDayRated( - creator.name, - rated?.name ?: error("Rated player doesn't have a name!") - ) - return votedOnPlayerAmount <= maxVotePerPlayer - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteTodayUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteTodayUseCase.kt deleted file mode 100644 index f8451d7..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteTodayUseCase.kt +++ /dev/null @@ -1,32 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.core.RatingPermission -import ru.astrainteractive.astrarating.feature.changerating.data.CanVoteTodayRepository -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteTodayUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Checks whether [Input.playerModel] can vote today or not - * - * Each player has limited amount of votes per day - */ -interface CanVoteTodayUseCase : UseCase.Suspended { - class Input(val playerModel: PlayerModel) - - suspend operator fun invoke(playerModel: PlayerModel) = invoke(Input(playerModel)) -} - -internal class CanVoteTodayUseCaseImpl( - private val maxRatingPerDay: Int, - private val canVoteTodayRepository: CanVoteTodayRepository -) : CanVoteTodayUseCase { - override suspend fun invoke(input: Input): Boolean { - val playerModel = input.playerModel - val maxVotesPerDay = playerModel.permissible?.maxPermissionSize( - RatingPermission.MaxRatePerDay - ) ?: maxRatingPerDay - val todayVotedAmount = canVoteTodayRepository.countPlayerTotalDayRated(playerModel.name) - return todayVotedAmount <= maxVotesPerDay - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteUseCase.kt deleted file mode 100644 index 3d88ff4..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CanVoteUseCase.kt +++ /dev/null @@ -1,22 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.core.RatingPermission -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CanVoteUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Checks whether [Input.playerModel] has permission to vote or not - */ -interface CanVoteUseCase : UseCase.Suspended { - @JvmInline - value class Input(val playerModel: PlayerModel) - - suspend operator fun invoke(playerModel: PlayerModel) = invoke(Input(playerModel)) -} - -internal class CanVoteUseCaseImpl : CanVoteUseCase { - override suspend fun invoke(input: Input): Boolean { - return input.playerModel.permissible?.hasPermission(RatingPermission.Vote) ?: false - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckEnoughTimeUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckEnoughTimeUseCase.kt deleted file mode 100644 index 7809a5b..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckEnoughTimeUseCase.kt +++ /dev/null @@ -1,24 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckEnoughTimeUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Checks whether [Input.playerModel] has been enough time on server - */ -interface CheckEnoughTimeUseCase : UseCase.Suspended { - @JvmInline - value class Input(val playerModel: PlayerModel) - - suspend operator fun invoke(playerModel: PlayerModel) = invoke(Input(playerModel)) -} - -internal class CheckEnoughTimeUseCaseImpl( - private val platformBridge: PlatformBridge -) : CheckEnoughTimeUseCase { - override suspend fun invoke(input: Input): Boolean { - return platformBridge.hasEnoughTime(input.playerModel) - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckPlayerExistsUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckPlayerExistsUseCase.kt deleted file mode 100644 index df1b7c5..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/CheckPlayerExistsUseCase.kt +++ /dev/null @@ -1,24 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.CheckPlayerExistsUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Checks if [Input.playerModel] exists - */ -interface CheckPlayerExistsUseCase : UseCase.Suspended { - @JvmInline - value class Input(val playerModel: PlayerModel?) - - suspend operator fun invoke(playerModel: PlayerModel?) = invoke(Input(playerModel)) -} - -internal class CheckPlayerExistsUseCaseImpl( - private val platformBridge: PlatformBridge -) : CheckPlayerExistsUseCase { - override suspend fun invoke(input: Input): Boolean { - return platformBridge.isPlayerExists(input.playerModel) - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertRatingUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertRatingUseCase.kt deleted file mode 100644 index 5e7c715..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertRatingUseCase.kt +++ /dev/null @@ -1,40 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.dto.RatingType -import ru.astrainteractive.astrarating.feature.changerating.data.InsertRatingRepository -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertRatingUseCase.Input -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Inserts new player rating and both players into database - * - * Each player has limited amount of votes per day - */ -interface InsertRatingUseCase : UseCase.Suspended { - class Input( - val ratingCreator: PlayerModel, - val ratedPlayer: PlayerModel, - val message: String, - val type: RatingType, - val ratingValue: Int - ) -} - -internal class InsertRatingUseCaseImpl( - private val insertUserUseCase: InsertUserUseCase, - private val insertRatingRepository: InsertRatingRepository -) : InsertRatingUseCase { - override suspend fun invoke(input: Input) { - val ratedPlayerDTO = insertUserUseCase.invoke(input.ratedPlayer) - val ratingCreatorDTO = insertUserUseCase.invoke(input.ratingCreator) - - insertRatingRepository.insertUserRating( - reporter = ratingCreatorDTO.playerDTO, - reported = ratedPlayerDTO.playerDTO, - message = input.message, - type = input.type, - ratingValue = input.ratingValue - ) - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertUserUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertUserUseCase.kt index 3fc82be..f447d3c 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertUserUseCase.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/InsertUserUseCase.kt @@ -6,20 +6,20 @@ import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.Inser import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.InsertUserUseCase.Output import ru.astrainteractive.astrarating.model.PlayerModel import ru.astrainteractive.astrarating.model.UserModel -import ru.astrainteractive.klibs.mikro.core.domain.UseCase /** * @param _auction auction to remove * @param player owner of auction * @return boolean - true if succesfully removed */ -interface InsertUserUseCase : UseCase.Suspended { +interface InsertUserUseCase { @JvmInline value class Input(val playerModel: PlayerModel) @JvmInline value class Output(val playerDTO: UserDTO) + suspend operator fun invoke(input: Input): Output suspend operator fun invoke(playerModel: PlayerModel) = invoke(Input(playerModel)) } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/ValidateMessageUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/ValidateMessageUseCase.kt deleted file mode 100644 index d0c8ae2..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/usecase/ValidateMessageUseCase.kt +++ /dev/null @@ -1,26 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.domain.usecase - -import ru.astrainteractive.astrarating.feature.changerating.domain.usecase.ValidateMessageUseCase.Input -import ru.astrainteractive.klibs.mikro.core.domain.UseCase - -/** - * Validates [Input.message] - * - * It should be in range of min/max range - */ -interface ValidateMessageUseCase : UseCase.Suspended { - @JvmInline - value class Input(val message: String) - - suspend operator fun invoke(message: String) = invoke(Input(message)) -} - -internal class ValidateMessageUseCaseImpl( - private val minMessageLength: Int, - private val maxMessageLength: Int -) : ValidateMessageUseCase { - override suspend fun invoke(input: Input): Boolean { - val message = input.message - return !(message.length < minMessageLength || message.length > maxMessageLength) - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt index e5683b2..c377be1 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt @@ -4,11 +4,12 @@ import ru.astrainteractive.astrarating.dto.UserRatingDTO import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCase.Input import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCase.Output import ru.astrainteractive.astrarating.model.UserRatingsSort -import ru.astrainteractive.klibs.mikro.core.domain.UseCase -interface SortRatingUseCase : UseCase.Suspended { +interface SortRatingUseCase { class Input(val ratings: List, val sort: UserRatingsSort) class Output(val ratings: List) + + suspend fun invoke(input: Input): Output } class SortRatingUseCaseImpl : SortRatingUseCase { diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt index 2d4b5e6..f23f4f8 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt @@ -23,7 +23,7 @@ class DefaultPlayerRatingsComponent( override fun onSortClicked() { componentScope.launch(dispatchers.IO) { - val sort = model.value.sort.next(UserRatingsSort.values()) + val sort = model.value.sort.next(UserRatingsSort.entries.toTypedArray()) val input = SortRatingUseCase.Input( ratings = model.value.userRatings, sort = sort From 7c8e491a1bef5654f41e9927f343d8fbb8022bf1 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 23:28:56 +0300 Subject: [PATCH 06/14] remove platform bridge --- .../command/rating/RatingCommandExecutor.kt | 6 ++++-- .../astrarating/model/PlayerModel.kt | 3 ++- .../changerating/data/BukkitPlatformBridge.kt | 19 ------------------- .../changerating/data/PlatformBridge.kt | 10 ---------- .../changerating/di/ChangeRatingModule.kt | 3 --- .../domain/check/CheckValidatorImpl.kt | 10 ++++------ .../astrarating/feature/di/SharedModule.kt | 4 ---- .../astrarating/di/impl/RootModuleImpl.kt | 8 -------- 8 files changed, 10 insertions(+), 53 deletions(-) delete mode 100644 modules/shared/src/bukkitMain/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/BukkitPlatformBridge.kt delete mode 100644 modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlatformBridge.kt diff --git a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt index c173b9d..3fdf300 100644 --- a/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt +++ b/modules/command-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/command/rating/RatingCommandExecutor.kt @@ -30,7 +30,8 @@ internal class RatingCommandExecutor( return PlayerModel( uuid = this.uniqueId, name = name ?: return null, - permissible = null + permissible = player?.toPermissible(), + firstPlayed = firstPlayed ) } @@ -38,7 +39,8 @@ internal class RatingCommandExecutor( return PlayerModel( uuid = this.uniqueId, name = name, - permissible = this.toPermissible() + permissible = toPermissible(), + firstPlayed = firstPlayed ) } diff --git a/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/model/PlayerModel.kt b/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/model/PlayerModel.kt index 9f8d110..368f76e 100644 --- a/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/model/PlayerModel.kt +++ b/modules/db-rating/src/main/kotlin/ru/astrainteractive/astrarating/model/PlayerModel.kt @@ -6,5 +6,6 @@ import java.util.UUID data class PlayerModel( val uuid: UUID, val name: String, - val permissible: Permissible? + val permissible: Permissible?, + val firstPlayed: Long ) diff --git a/modules/shared/src/bukkitMain/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/BukkitPlatformBridge.kt b/modules/shared/src/bukkitMain/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/BukkitPlatformBridge.kt deleted file mode 100644 index b2a959c..0000000 --- a/modules/shared/src/bukkitMain/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/BukkitPlatformBridge.kt +++ /dev/null @@ -1,19 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.data - -import org.bukkit.Bukkit -import ru.astrainteractive.astrarating.model.PlayerModel -import ru.astrainteractive.klibs.kdi.Provider - -class BukkitPlatformBridge( - private val minTimeOnServer: Provider -) : PlatformBridge { - override fun isPlayerExists(playerModel: PlayerModel?): Boolean { - val player = playerModel?.uuid?.let(Bukkit::getOfflinePlayer) - return player != null && player.firstPlayed > 0L - } - - override fun hasEnoughTime(playerModel: PlayerModel): Boolean { - val player = playerModel.uuid.let(Bukkit::getOfflinePlayer) - return System.currentTimeMillis() - player.firstPlayed > minTimeOnServer.provide() - } -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlatformBridge.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlatformBridge.kt deleted file mode 100644 index 0256727..0000000 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlatformBridge.kt +++ /dev/null @@ -1,10 +0,0 @@ -package ru.astrainteractive.astrarating.feature.changerating.data - -import ru.astrainteractive.astrarating.model.PlayerModel - -interface PlatformBridge { - - fun isPlayerExists(playerModel: PlayerModel?): Boolean - - fun hasEnoughTime(playerModel: PlayerModel): Boolean -} diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt index 55c6fab..81efb5a 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/di/ChangeRatingModule.kt @@ -4,7 +4,6 @@ import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.feature.changerating.data.InsertRatingRepositoryImpl import ru.astrainteractive.astrarating.feature.changerating.data.InsertUserRepositoryImpl -import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge import ru.astrainteractive.astrarating.feature.changerating.data.PlayerOnPlayerCounterRepositoryImpl import ru.astrainteractive.astrarating.feature.changerating.data.PlayerTotalRatingRepositoryImpl import ru.astrainteractive.astrarating.feature.changerating.domain.check.CheckValidatorImpl @@ -23,7 +22,6 @@ interface ChangeRatingModule { private val dbApi: RatingDBApi, private val empireConfig: Reloadable, private val dispatchers: KotlinDispatchers, - private val platformBridge: Provider ) : ChangeRatingModule { override val addRatingUseCase: AddRatingUseCase by Provider { AddRatingUseCaseImpl( @@ -35,7 +33,6 @@ interface ChangeRatingModule { ) ), checkValidator = CheckValidatorImpl( - platformBridge = platformBridge.provide(), playerTotalRatingRepository = PlayerTotalRatingRepositoryImpl( dbApi = dbApi, dispatchers = dispatchers diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt index a2c0f3d..fff7d5a 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/domain/check/CheckValidatorImpl.kt @@ -2,12 +2,10 @@ package ru.astrainteractive.astrarating.feature.changerating.domain.check import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.core.RatingPermission -import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge import ru.astrainteractive.astrarating.feature.changerating.data.PlayerOnPlayerCounterRepository import ru.astrainteractive.astrarating.feature.changerating.data.PlayerTotalRatingRepository internal class CheckValidatorImpl( - private val platformBridge: PlatformBridge, private val playerOnPlayerCounterRepository: PlayerOnPlayerCounterRepository, private val playerTotalRatingRepository: PlayerTotalRatingRepository, private val config: EmpireConfig @@ -20,7 +18,7 @@ internal class CheckValidatorImpl( creatorName = check.creator.name, ratedName = check.rated?.name ?: error("Rated player doesn't have a name!") ) - return votedOnPlayerAmount <= maxVotePerPlayer + return votedOnPlayerAmount < maxVotePerPlayer } private suspend fun checkCanVoteToday(check: Check.CanVoteToday): Boolean { @@ -28,7 +26,7 @@ internal class CheckValidatorImpl( ?.maxPermissionSize(RatingPermission.MaxRatePerDay) ?: config.maxRatingPerDay val todayVotedAmount = playerTotalRatingRepository.countPlayerTotalDayRated(check.playerModel.name) - return todayVotedAmount <= maxVotesPerDay + return todayVotedAmount < maxVotesPerDay } private fun checkCanVote(check: Check.CanVote): Boolean { @@ -36,11 +34,11 @@ internal class CheckValidatorImpl( } private fun checkEnoughTime(check: Check.EnoughTime): Boolean { - return platformBridge.hasEnoughTime(check.playerModel) + return System.currentTimeMillis() - check.playerModel.firstPlayed > config.minTimeOnServer } private fun checkPlayerExists(check: Check.PlayerExists): Boolean { - return platformBridge.isPlayerExists(check.playerModel) + return check.playerModel.firstPlayed > 0L } private fun checkMessageCorrect(check: Check.MessageCorrect): Boolean { diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt index aae3153..5fcb8d2 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt @@ -7,14 +7,12 @@ import ru.astrainteractive.astrarating.feature.allrating.AllRatingsComponent import ru.astrainteractive.astrarating.feature.allrating.DefaultAllRatingsComponent import ru.astrainteractive.astrarating.feature.allrating.data.AllRatingsRepository import ru.astrainteractive.astrarating.feature.allrating.data.AllRatingsRepositoryImpl -import ru.astrainteractive.astrarating.feature.changerating.data.PlatformBridge import ru.astrainteractive.astrarating.feature.changerating.di.ChangeRatingModule import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCase import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCaseImpl import ru.astrainteractive.astrarating.feature.playerrating.presentation.DefaultPlayerRatingsComponent import ru.astrainteractive.astrarating.feature.playerrating.presentation.PlayerRatingsComponent import ru.astrainteractive.klibs.kdi.Factory -import ru.astrainteractive.klibs.kdi.Provider import ru.astrainteractive.klibs.kdi.Reloadable import ru.astrainteractive.klibs.kdi.Single import ru.astrainteractive.klibs.kdi.getValue @@ -33,14 +31,12 @@ interface SharedModule { private val dispatchers: KotlinDispatchers, private val coroutineScope: CoroutineScope, private val empireConfig: Reloadable, - private val platformBridge: Provider ) : SharedModule { override val changeRatingModule: ChangeRatingModule by Single { ChangeRatingModule.Default( dbApi = apiRatingModule.ratingDBApi, empireConfig = empireConfig, dispatchers = dispatchers, - platformBridge = platformBridge ) } override val allRatingsRepository by Single { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt index d6da461..07a38d0 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt @@ -8,7 +8,6 @@ import ru.astrainteractive.astrarating.db.rating.di.DBRatingModule import ru.astrainteractive.astrarating.di.BukkitModule import ru.astrainteractive.astrarating.di.RootModule import ru.astrainteractive.astrarating.event.di.EventModule -import ru.astrainteractive.astrarating.feature.changerating.data.BukkitPlatformBridge import ru.astrainteractive.astrarating.feature.di.SharedModule import ru.astrainteractive.astrarating.gui.di.GuiModule import ru.astrainteractive.astrarating.integration.papi.di.PapiModule @@ -58,13 +57,6 @@ class RootModuleImpl : RootModule { dispatchers = coreModule.dispatchers, coroutineScope = coreModule.scope.value, empireConfig = coreModule.config, - platformBridge = { - BukkitPlatformBridge( - minTimeOnServer = { - coreModule.config.value.minTimeOnServer - } - ) - } ) } From 63c90c3c787d770e176cde80b8b0d16d426a0fe7 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 23:32:43 +0300 Subject: [PATCH 07/14] fix internal packages --- .../astrarating/gui/router/GuiRouterImpl.kt | 6 +-- .../allrating/DefaultAllRatingsComponent.kt | 2 +- .../allrating/data/AllRatingsRepository.kt | 2 +- .../data/AllRatingsRepositoryImpl.kt | 2 +- .../data/InsertRatingRepository.kt | 2 +- .../changerating/data/InsertUserRepository.kt | 2 +- .../data/PlayerOnPlayerCounterRepository.kt | 2 +- .../data/PlayerTotalRatingRepository.kt | 2 +- .../astrarating/feature/di/SharedModule.kt | 38 ++++++++----------- .../playerrating/domain/SortRatingUseCase.kt | 4 +- .../DefaultPlayerRatingsComponent.kt | 2 +- 11 files changed, 28 insertions(+), 36 deletions(-) diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt index 8dc8d28..b9d373b 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/router/GuiRouterImpl.kt @@ -34,7 +34,7 @@ internal class GuiRouterImpl( is GuiRouter.Route.AllRatings -> RatingsGUI( player = route.executor, module = guiDependencies, - allRatingsComponent = sharedModule.allRatingsComponentFactory().create(), + allRatingsComponent = sharedModule.createAllRatingsComponent(), router = this@GuiRouterImpl ) @@ -42,9 +42,9 @@ internal class GuiRouterImpl( selectedPlayerName = route.selectedPlayerName, player = route.executor, module = guiDependencies, - playerRatingsComponent = sharedModule.playerRatingsComponentFactory( + playerRatingsComponent = sharedModule.createPlayerRatingsComponent( playerName = route.selectedPlayerName - ).create(), + ), router = this@GuiRouterImpl ) } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/DefaultAllRatingsComponent.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/DefaultAllRatingsComponent.kt index 6bc8012..52d3aa3 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/DefaultAllRatingsComponent.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/DefaultAllRatingsComponent.kt @@ -9,7 +9,7 @@ import ru.astrainteractive.astrarating.model.UsersRatingsSort import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers import ru.astrainteractive.klibs.mikro.core.util.next -class DefaultAllRatingsComponent( +internal class DefaultAllRatingsComponent( private val repository: AllRatingsRepository, dispatchers: KotlinDispatchers ) : AllRatingsComponent, AsyncComponent() { diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepository.kt index f33df70..5f7f187 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepository.kt @@ -2,6 +2,6 @@ package ru.astrainteractive.astrarating.feature.allrating.data import ru.astrainteractive.astrarating.dto.RatedUserDTO -interface AllRatingsRepository { +internal interface AllRatingsRepository { suspend fun fetchUsersTotalRating(): List } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepositoryImpl.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepositoryImpl.kt index 220f22d..cb621f1 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepositoryImpl.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/allrating/data/AllRatingsRepositoryImpl.kt @@ -8,7 +8,7 @@ import ru.astrainteractive.astrarating.dto.RatedUserDTO import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers import kotlin.time.Duration.Companion.seconds -class AllRatingsRepositoryImpl( +internal class AllRatingsRepositoryImpl( private val dbApi: RatingDBApi, private val coroutineScope: CoroutineScope, private val dispatchers: KotlinDispatchers diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertRatingRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertRatingRepository.kt index effe286..fb382b9 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertRatingRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertRatingRepository.kt @@ -6,7 +6,7 @@ import ru.astrainteractive.astrarating.dto.RatingType import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface InsertRatingRepository { +internal interface InsertRatingRepository { suspend fun insertUserRating( reporter: UserDTO?, reported: UserDTO, diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertUserRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertUserRepository.kt index 2d3961b..6c65d3e 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertUserRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/InsertUserRepository.kt @@ -6,7 +6,7 @@ import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.model.UserModel import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -interface InsertUserRepository { +internal interface InsertUserRepository { suspend fun selectUser(name: String): Result suspend fun insertUser(userModel: UserModel): Result } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt index 68402fb..d135ad3 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerOnPlayerCounterRepository.kt @@ -4,7 +4,7 @@ import kotlinx.coroutines.withContext import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -fun interface PlayerOnPlayerCounterRepository { +internal fun interface PlayerOnPlayerCounterRepository { suspend fun countPlayerOnPlayerDayRated(creatorName: String, ratedName: String): Long } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt index 53ea3a8..0c92717 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/changerating/data/PlayerTotalRatingRepository.kt @@ -4,7 +4,7 @@ import kotlinx.coroutines.withContext import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers -fun interface PlayerTotalRatingRepository { +internal fun interface PlayerTotalRatingRepository { suspend fun countPlayerTotalDayRated(name: String): Long } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt index 5fcb8d2..e82a5d9 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/di/SharedModule.kt @@ -5,14 +5,12 @@ import ru.astrainteractive.astrarating.api.rating.di.ApiRatingModule import ru.astrainteractive.astrarating.core.EmpireConfig import ru.astrainteractive.astrarating.feature.allrating.AllRatingsComponent import ru.astrainteractive.astrarating.feature.allrating.DefaultAllRatingsComponent -import ru.astrainteractive.astrarating.feature.allrating.data.AllRatingsRepository import ru.astrainteractive.astrarating.feature.allrating.data.AllRatingsRepositoryImpl import ru.astrainteractive.astrarating.feature.changerating.di.ChangeRatingModule import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCase import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCaseImpl import ru.astrainteractive.astrarating.feature.playerrating.presentation.DefaultPlayerRatingsComponent import ru.astrainteractive.astrarating.feature.playerrating.presentation.PlayerRatingsComponent -import ru.astrainteractive.klibs.kdi.Factory import ru.astrainteractive.klibs.kdi.Reloadable import ru.astrainteractive.klibs.kdi.Single import ru.astrainteractive.klibs.kdi.getValue @@ -21,10 +19,8 @@ import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers interface SharedModule { val changeRatingModule: ChangeRatingModule - val allRatingsRepository: AllRatingsRepository - - fun playerRatingsComponentFactory(playerName: String): Factory - fun allRatingsComponentFactory(): Factory + fun createPlayerRatingsComponent(playerName: String): PlayerRatingsComponent + fun createAllRatingsComponent(): AllRatingsComponent class Default( private val apiRatingModule: ApiRatingModule, @@ -39,7 +35,7 @@ interface SharedModule { dispatchers = dispatchers, ) } - override val allRatingsRepository by Single { + private val allRatingsRepository by Single { AllRatingsRepositoryImpl( dbApi = apiRatingModule.ratingDBApi, coroutineScope = coroutineScope, @@ -48,24 +44,20 @@ interface SharedModule { } private val sortRatingUseCase: SortRatingUseCase = SortRatingUseCaseImpl() - override fun playerRatingsComponentFactory(playerName: String): Factory { - return Factory { - DefaultPlayerRatingsComponent( - playerName = playerName, - dbApi = apiRatingModule.ratingDBApi, - dispatchers = dispatchers, - sortRatingUseCase = sortRatingUseCase - ) - } + override fun createPlayerRatingsComponent(playerName: String): PlayerRatingsComponent { + return DefaultPlayerRatingsComponent( + playerName = playerName, + dbApi = apiRatingModule.ratingDBApi, + dispatchers = dispatchers, + sortRatingUseCase = sortRatingUseCase + ) } - override fun allRatingsComponentFactory(): Factory { - return Factory { - DefaultAllRatingsComponent( - repository = allRatingsRepository, - dispatchers = dispatchers - ) - } + override fun createAllRatingsComponent(): AllRatingsComponent { + return DefaultAllRatingsComponent( + repository = allRatingsRepository, + dispatchers = dispatchers + ) } } } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt index c377be1..3094465 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/domain/SortRatingUseCase.kt @@ -5,14 +5,14 @@ import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUse import ru.astrainteractive.astrarating.feature.playerrating.domain.SortRatingUseCase.Output import ru.astrainteractive.astrarating.model.UserRatingsSort -interface SortRatingUseCase { +internal interface SortRatingUseCase { class Input(val ratings: List, val sort: UserRatingsSort) class Output(val ratings: List) suspend fun invoke(input: Input): Output } -class SortRatingUseCaseImpl : SortRatingUseCase { +internal class SortRatingUseCaseImpl : SortRatingUseCase { override suspend fun invoke(input: Input): Output { val ratings = input.ratings return when (input.sort) { diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt index f23f4f8..319e4c4 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt @@ -11,7 +11,7 @@ import ru.astrainteractive.astrarating.model.UserRatingsSort import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers import ru.astrainteractive.klibs.mikro.core.util.next -class DefaultPlayerRatingsComponent( +internal class DefaultPlayerRatingsComponent( playerName: String, private val dbApi: RatingDBApi, private val dispatchers: KotlinDispatchers, From edc29d372eb2b9a3032d7a585bb2605a669a93e9 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Jul 2024 23:47:34 +0300 Subject: [PATCH 08/14] up version 1.18.0 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 445b79b..c4b263f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ makeevrserg.java.ktarget=21 # Project makeevrserg.project.name=AstraRating makeevrserg.project.group=ru.astrainteractive.astrarating -makeevrserg.project.version.string=1.17.5 +makeevrserg.project.version.string=1.18.0 makeevrserg.project.description=Rating plugin for EmpireProjekt makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com makeevrserg.project.url=https://empireprojekt.ru From 507bdc438c42f2fa40d59cfe4fcbd3400add7bd4 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 00:25:04 +0300 Subject: [PATCH 09/14] update readme, fix kill event --- README.md | 16 ++++------ .../api/rating/api/RatingDBApiExt.kt | 18 ++++++++++++ .../astrarating/core/EmpireConfig.kt | 2 +- .../event/kill/KillEventListener.kt | 29 ++++++++++--------- .../integration/papi/RatingPAPILifecycle.kt | 14 ++++++--- .../integration/papi/di/PapiModule.kt | 11 +++++-- spigot/build.gradle.kts | 2 +- 7 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/RatingDBApiExt.kt diff --git a/README.md b/README.md index 080a176..1498d14 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,8 @@ ## No-Lag and free! -Advantages: - -- [x] Advanced sorting -- [x] Highly customizable -- [x] Fast and open source -- [x] Advanced permissions -- [x] Convenient GUI -- [x] Fully translatable +> [!CAUTION] +> Java 21 and Paper 1.21 only supported! Use other versions on your own risk! More plugins from [AstraInteractive](https://github.com/Astra-Interactive) @@ -83,12 +77,14 @@ coloring: ### Adding events -Section will be created by default if you installing plugin first time +Section will be created by default if you're installing plugin first time. + +When enabled, the plugin will decrease player rating when killed a player with positive rating ```yaml events: kill_player: - change_by: -2 + change_by: -1 enabled: false ``` diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/RatingDBApiExt.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/RatingDBApiExt.kt new file mode 100644 index 0000000..a40dc00 --- /dev/null +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/RatingDBApiExt.kt @@ -0,0 +1,18 @@ +package ru.astrainteractive.astrarating.api.rating.api + +import ru.astrainteractive.astrarating.dto.UserDTO +import ru.astrainteractive.astrarating.model.UserModel + +object RatingDBApiExt { + suspend fun RatingDBApi.upsertUser(userModel: UserModel): UserDTO { + val selectedUser = selectUser(userModel.minecraftName).getOrNull() + if (selectedUser != null) return selectedUser + return UserDTO( + id = insertUser(userModel) + .getOrNull() + ?: error("Could not insert user $userModel"), + minecraftName = userModel.minecraftName, + minecraftUUID = userModel.minecraftUUID.toString() + ) + } +} diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt index 9f671a0..00e7930 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt @@ -46,7 +46,7 @@ data class EmpireConfig( @Serializable data class Event( @SerialName("change_by") - val changeBy: Int = 0, + val changeBy: Int = -1, val enabled: Boolean = false ) } diff --git a/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt index b9ff62d..f9608e9 100644 --- a/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt +++ b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt @@ -4,8 +4,8 @@ import kotlinx.coroutines.launch import org.bukkit.event.EventHandler import org.bukkit.event.entity.PlayerDeathEvent import ru.astrainteractive.astralibs.event.EventListener +import ru.astrainteractive.astrarating.api.rating.api.RatingDBApiExt.upsertUser import ru.astrainteractive.astrarating.dto.RatingType -import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.event.di.EventDependencies import ru.astrainteractive.astrarating.model.UserModel @@ -13,28 +13,29 @@ internal class KillEventListener( module: EventDependencies ) : EventDependencies by module, EventListener { + @EventHandler fun onPlayerKilledPlayer(e: PlayerDeathEvent) { if (!configDependency.events.killPlayer.enabled) return if (configDependency.events.killPlayer.changeBy == 0) return val killedPlayer = e.entity val killerPlayer = killedPlayer.killer ?: return + scope.launch(dispatchers.IO) { - val playerDTO = apiDependency.selectUser(killerPlayer.name).getOrNull() ?: run { - val user = UserModel( - minecraftUUID = killerPlayer.uniqueId, - minecraftName = killerPlayer.name - ) - val id = apiDependency.insertUser(user).getOrNull() ?: return@launch - UserDTO( - id = id, - minecraftName = user.minecraftName, - minecraftUUID = user.minecraftUUID.toString() - ) - } + val killedPlayerRating = apiDependency.fetchUsersTotalRating().getOrNull().orEmpty() + .firstOrNull { it.userDTO.minecraftUUID == killedPlayer.uniqueId.toString() } + ?.rating + ?: error("Could not fetch rating of ${killedPlayer.name}") + if (killedPlayerRating <= 0) return@launch + apiDependency.insertUserRating( reporter = null, - reported = playerDTO, + reported = apiDependency.upsertUser( + userModel = UserModel( + minecraftUUID = killerPlayer.uniqueId, + minecraftName = killerPlayer.name + ) + ), message = translationDependency.killedPlayer(killedPlayer.name).raw, type = RatingType.PLAYER_KILL, ratingValue = configDependency.events.killPlayer.changeBy diff --git a/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/RatingPAPILifecycle.kt b/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/RatingPAPILifecycle.kt index 56ecdea..a3a3e9b 100644 --- a/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/RatingPAPILifecycle.kt +++ b/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/RatingPAPILifecycle.kt @@ -2,19 +2,25 @@ package ru.astrainteractive.astrarating.integration.papi import org.bukkit.Bukkit import ru.astrainteractive.astralibs.lifecycle.Lifecycle +import ru.astrainteractive.astralibs.logging.JUtiltLogger +import ru.astrainteractive.astralibs.logging.Logger import ru.astrainteractive.astrarating.integration.papi.di.PapiDependencies -internal class RatingPAPILifecycle(dependencies: PapiDependencies) : Lifecycle { - private val expansion = RatingPAPIExpansion(dependencies) +internal class RatingPAPILifecycle(dependencies: PapiDependencies) : + Lifecycle, + Logger by JUtiltLogger("RatingPAPILifecycle") { + private val expansion by lazy { + RatingPAPIExpansion(dependencies) + } private val isPapiExists: Boolean get() = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null override fun onEnable() { if (!isPapiExists) { - Bukkit.getLogger().warning("Could not find PlaceholderAPI. Placeholders disabled") + warn { "Could not find PlaceholderAPI. Placeholders disabled" } return } - Bukkit.getLogger().warning("Found PlaceholderAPI. Placeholders are now enabled") + warn { "Found PlaceholderAPI. Placeholders are now enabled" } if (expansion.isRegistered) { expansion.unregister() } diff --git a/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/di/PapiModule.kt b/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/di/PapiModule.kt index 06d284c..506366c 100644 --- a/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/di/PapiModule.kt +++ b/modules/integration-papi/src/main/kotlin/ru/astrainteractive/astrarating/integration/papi/di/PapiModule.kt @@ -1,6 +1,7 @@ package ru.astrainteractive.astrarating.integration.papi.di import kotlinx.coroutines.CoroutineScope +import org.bukkit.Bukkit import ru.astrainteractive.astralibs.lifecycle.Lifecycle import ru.astrainteractive.astrarating.api.rating.api.CachedApi import ru.astrainteractive.astrarating.core.EmpireConfig @@ -9,7 +10,7 @@ import ru.astrainteractive.klibs.kdi.Dependency import ru.astrainteractive.klibs.kdi.getValue interface PapiModule { - val lifecycle: Lifecycle + val lifecycle: Lifecycle? class Default( cachedApi: CachedApi, @@ -21,8 +22,12 @@ interface PapiModule { override val config by config override val scope: CoroutineScope = scope } - override val lifecycle: Lifecycle by lazy { - RatingPAPILifecycle(papiDependencies) + override val lifecycle: Lifecycle? by lazy { + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + RatingPAPILifecycle(papiDependencies) + } else { + null + } } } } diff --git a/spigot/build.gradle.kts b/spigot/build.gradle.kts index 1edf1d1..58213ce 100644 --- a/spigot/build.gradle.kts +++ b/spigot/build.gradle.kts @@ -50,7 +50,7 @@ minecraftMultiplatform { implementation(projects.modules.shared.bukkitMain) } } -val localFolder = File("D:\\Minecraft Servers\\Servers\\conf.smp\\smp\\plugins") +val localFolder = File("D:\\Minecraft Servers\\Servers\\esmp-configuration\\test\\plugins") .takeIf { it.exists() } ?: File(rootDir, "jars") From 207bec0ded09853d5b23578cadf76d9a567b8d2d Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 00:50:48 +0300 Subject: [PATCH 10/14] update kill event --- .../api/rating/api/impl/RatingDBApiImpl.kt | 2 +- .../astrarating/core/PluginTranslation.kt | 12 ++++++++++- .../event/kill/KillEventListener.kt | 3 +++ .../gui/playerratings/PlayerRatingsGUI.kt | 20 +++++++++++++++++++ .../astrarating/gui/ratings/RatingsGUI.kt | 13 +++++++----- .../DefaultPlayerRatingsComponent.kt | 6 +++--- .../presentation/PlayerRatingsComponent.kt | 13 +++++++++--- 7 files changed, 56 insertions(+), 13 deletions(-) diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt index d746818..a2522a5 100644 --- a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt @@ -30,7 +30,7 @@ internal class RatingDBApiImpl( UserDAO.find { UserTable.minecraftName.eq(playerName.uppercase()) .or { UserTable.minecraftName.eq(playerName) } - }.first().let(UserMapper::toDTO) + }.firstOrNull()?.let(UserMapper::toDTO) ?: error("Could not find $playerName") } }.onFailure(Throwable::printStackTrace) diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/PluginTranslation.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/PluginTranslation.kt index ccd8916..7b658fa 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/PluginTranslation.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/PluginTranslation.kt @@ -107,10 +107,20 @@ class PluginTranslation( val message: StringDesc.Raw = StringDesc.Raw("B76CAСообщение:"), @SerialName("menu.loading") val loading: StringDesc.Raw = StringDesc.Raw("B76CAЗагрузка..."), + @SerialName("events.title") + val eventsTitle: StringDesc.Raw = StringDesc.Raw("B76CAОстальное"), + @SerialName("events.kill.amount") + private val eventKillAmount: StringDesc.Raw = StringDesc.Raw(" c0303Количество убийств: %kills%"), @SerialName("events.kill_player") - private val killedPlayer: StringDesc.Raw = StringDesc.Raw(" c0303Убил игрока %killed_player%") + private val killedPlayer: StringDesc.Raw = StringDesc.Raw(" c0303Убил игрока %killed_player%"), + @SerialName("events.you_killed_player") + private val youKilledPlayer: StringDesc.Raw = StringDesc.Raw( + " c0303Вы убили игрока %killed_player%, ваш рейтинг был понижен" + ) ) { fun killedPlayer(playerName: String) = killedPlayer.replace("%killed_player%", playerName) + fun youKilledPlayer(playerName: String) = youKilledPlayer.replace("%killed_player%", playerName) fun likedUser(playerName: String) = likedUser.replace("%player%", playerName) fun dislikedUser(playerName: String) = dislikedUser.replace("%player%", playerName) + fun eventKillAmount(count: Int) = eventKillAmount.replace("%kills%", count.toString()) } diff --git a/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt index f9608e9..5943a35 100644 --- a/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt +++ b/modules/event-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/event/kill/KillEventListener.kt @@ -40,6 +40,9 @@ internal class KillEventListener( type = RatingType.PLAYER_KILL, ratingValue = configDependency.events.killPlayer.changeBy ) + translationDependency.youKilledPlayer(killedPlayer.name) + .let(translationContext::toComponent) + .run(killerPlayer::sendMessage) } } } diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt index 4014726..5b197d9 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import net.kyori.adventure.text.Component +import org.bukkit.Material import org.bukkit.entity.Player import org.bukkit.event.inventory.ClickType import org.bukkit.event.inventory.InventoryClickEvent @@ -18,9 +19,11 @@ import ru.astrainteractive.astralibs.menu.inventory.util.PageContextExt.getIndex import ru.astrainteractive.astralibs.menu.inventory.util.PageContextExt.isFirstPage import ru.astrainteractive.astralibs.menu.inventory.util.PageContextExt.isLastPage import ru.astrainteractive.astralibs.menu.slot.InventorySlot +import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.addLore import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.editMeta import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.setIndex import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.setItemStack +import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.setMaterial import ru.astrainteractive.astralibs.menu.slot.util.InventorySlotBuilderExt.setOnClickListener import ru.astrainteractive.astralibs.permission.BukkitPermissibleExt.toPermissible import ru.astrainteractive.astralibs.string.StringDescExt.replace @@ -98,6 +101,22 @@ internal class PlayerRatingsGUI( click = { playerRatingsComponent.onSortClicked() } ) + private val killEventSlot: InventorySlot + get() = InventorySlot.Builder() + .setIndex(46) + .setMaterial(Material.NETHERITE_SWORD) + .editMeta { + translationContext + .toComponent(translation.eventsTitle) + .run(::displayName) + } + .addLore( + translation.eventKillAmount( + playerRatingsComponent.model.value.killCounts + ).let(translationContext::toComponent) + ) + .build() + override var pageContext: PageContext = PageContext( page = 0, maxItemsPerPage = 45, @@ -143,6 +162,7 @@ internal class PlayerRatingsGUI( inventory.clear() setManageButtons() sortButton.setInventorySlot() + killEventSlot.setInventorySlot() val list = model.userRatings for (i in 0 until pageContext.maxItemsPerPage) { val index = pageContext.getIndex(i) diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt index fbad44d..1ada503 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/ratings/RatingsGUI.kt @@ -148,7 +148,10 @@ internal class RatingsGUI( .setIndex(i) .setItemStack(PlayerHeadUtil.getHead(userAndRating.userDTO.normalName)) .editMeta { - val color = if (userAndRating.rating > 0) translation.positiveColor else translation.negativeColor + val color = when { + userAndRating.rating > 0 -> translation.positiveColor.raw + else -> translation.negativeColor.raw + } displayName( translationContext.toComponent( translation.playerNameColor.raw + userAndRating.userDTO.normalName @@ -157,7 +160,7 @@ internal class RatingsGUI( buildList { if (config.gui.showFirstConnection) { val component = translationContext.toComponent( - "${translation.firstConnection} ${ + "${translation.firstConnection.raw} ${ TimeUtility.formatToString( time = userAndRating.userDTO.offlinePlayer.firstPlayed, format = config.gui.format @@ -168,7 +171,7 @@ internal class RatingsGUI( } if (config.gui.showLastConnection) { val component = translationContext.toComponent( - "${translation.lastConnection} ${ + "${translation.lastConnection.raw} ${ TimeUtility.formatToString( time = userAndRating.userDTO.offlinePlayer.lastPlayed, format = config.gui.format @@ -178,9 +181,9 @@ internal class RatingsGUI( add(component) } translationContext - .toComponent("${translation.rating}: ${color}${userAndRating.rating}") + .toComponent("${translation.rating.raw}: ${color}${userAndRating.rating}") .run(::add) - } + }.run(::lore) } .setOnClickListener { val route = GuiRouter.Route.PlayerRating( diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt index 319e4c4..9b3e41d 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/DefaultPlayerRatingsComponent.kt @@ -25,14 +25,14 @@ internal class DefaultPlayerRatingsComponent( componentScope.launch(dispatchers.IO) { val sort = model.value.sort.next(UserRatingsSort.entries.toTypedArray()) val input = SortRatingUseCase.Input( - ratings = model.value.userRatings, + ratings = model.value.allRatings, sort = sort ) val result = sortRatingUseCase.invoke(input) model.update { it.copy( sort = sort, - userRatings = result.ratings + allRatings = result.ratings ) } } @@ -55,7 +55,7 @@ internal class DefaultPlayerRatingsComponent( .onFailure(Throwable::printStackTrace) .getOrDefault(emptyList()) model.update { - it.copy(userRatings = userRatings) + it.copy(allRatings = userRatings) } onSortClicked() model.update { it.copy(isLoading = false) } diff --git a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/PlayerRatingsComponent.kt b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/PlayerRatingsComponent.kt index 5fdebee..c799619 100644 --- a/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/PlayerRatingsComponent.kt +++ b/modules/shared/src/main/kotlin/ru/astrainteractive/astrarating/feature/playerrating/presentation/PlayerRatingsComponent.kt @@ -2,6 +2,7 @@ package ru.astrainteractive.astrarating.feature.playerrating.presentation import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.StateFlow +import ru.astrainteractive.astrarating.dto.RatingType import ru.astrainteractive.astrarating.dto.UserRatingDTO import ru.astrainteractive.astrarating.model.UserRatingsSort @@ -12,8 +13,14 @@ interface PlayerRatingsComponent : CoroutineScope { fun close() data class Model( val playerName: String, - val userRatings: List = emptyList(), - val sort: UserRatingsSort = UserRatingsSort.values().first(), + val allRatings: List = emptyList(), + val sort: UserRatingsSort = UserRatingsSort.entries.first(), val isLoading: Boolean = false - ) + ) { + val userRatings: List + get() = allRatings.filter { it.userCreatedReport != null } + + val killCounts: Int + get() = allRatings.count { it.ratingType == RatingType.PLAYER_KILL } + } } From 9328f5ebb59abf2313f8df2f40c3fe945a9b6aff Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 00:54:05 +0300 Subject: [PATCH 11/14] display kills only when more than one kill --- .../astrarating/gui/playerratings/PlayerRatingsGUI.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt index 5b197d9..fa8e31d 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt @@ -101,7 +101,7 @@ internal class PlayerRatingsGUI( click = { playerRatingsComponent.onSortClicked() } ) - private val killEventSlot: InventorySlot + private val killEventSlot: InventorySlot? get() = InventorySlot.Builder() .setIndex(46) .setMaterial(Material.NETHERITE_SWORD) @@ -116,6 +116,7 @@ internal class PlayerRatingsGUI( ).let(translationContext::toComponent) ) .build() + .takeIf { playerRatingsComponent.model.value.killCounts > 0 } override var pageContext: PageContext = PageContext( page = 0, @@ -162,7 +163,7 @@ internal class PlayerRatingsGUI( inventory.clear() setManageButtons() sortButton.setInventorySlot() - killEventSlot.setInventorySlot() + killEventSlot?.setInventorySlot() val list = model.userRatings for (i in 0 until pageContext.maxItemsPerPage) { val index = pageContext.getIndex(i) @@ -175,9 +176,9 @@ internal class PlayerRatingsGUI( displayName( translationContext.toComponent( translation.playerNameColor.raw + ( - userAndRating.userCreatedReport?.normalName - ?: "-" - ) + userAndRating.userCreatedReport?.normalName + ?: "-" + ) ) ) buildList { From 3bddd89ccdb6e3a9566306c943c37b9bba52020b Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 00:54:43 +0300 Subject: [PATCH 12/14] set paper 1.20.6 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9fd7d11..2ac349f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,7 +16,7 @@ klibs-kdi = "1.4.5" # https://github.com/makeevrserg/klibs.kdi # Minecraft minecraft-velocity = "4.0.0-SNAPSHOT" # https://github.com/PaperMC/Velocity -minecraft-spigot = "1.21-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper +minecraft-spigot = "1.20.6-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper minecraft-papi = "2.11.6" # https://github.com/PlaceholderAPI/PlaceholderAPI minecraft-protocollib = "5.1.0" # https://github.com/dmulloy2/ProtocolLib minecraft-vault = "1.7.1" # https://github.com/MilkBowl/VaultAPI From c9ec7482dca77c1881b7d6e349c6cb825b97121c Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 01:10:35 +0300 Subject: [PATCH 13/14] fix log by adding debug tag --- gradle/libs.versions.toml | 2 +- modules/api-rating/build.gradle.kts | 1 + .../api/rating/api/impl/RatingDBApiImpl.kt | 30 ++++++++++++------- .../api/rating/di/ApiRatingModule.kt | 2 ++ .../api-rating/src/test/java/AuctionsTests.kt | 1 + .../astrarating/core/EmpireConfig.kt | 3 +- .../gui/playerratings/PlayerRatingsGUI.kt | 6 ++-- .../astrarating/di/impl/RootModuleImpl.kt | 1 + 8 files changed, 31 insertions(+), 15 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2ac349f..7108ef1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,7 +20,7 @@ minecraft-spigot = "1.20.6-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper minecraft-papi = "2.11.6" # https://github.com/PlaceholderAPI/PlaceholderAPI minecraft-protocollib = "5.1.0" # https://github.com/dmulloy2/ProtocolLib minecraft-vault = "1.7.1" # https://github.com/MilkBowl/VaultAPI -minecraft-astralibs = "3.9.0" # https://github.com/Astra-Interactive/AstraLibs +minecraft-astralibs = "3.9.3" # https://github.com/Astra-Interactive/AstraLibs minecraft-bstats = "3.0.2" # https://github.com/Bastian/bStats minecraft-mockbukkit = "v3.92.1" #https://github.com/MockBukkit/MockBukkit diff --git a/modules/api-rating/build.gradle.kts b/modules/api-rating/build.gradle.kts index 66cde7a..42ed7bc 100644 --- a/modules/api-rating/build.gradle.kts +++ b/modules/api-rating/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { // klibs implementation(libs.klibs.mikro.core) implementation(libs.klibs.kdi) + implementation(libs.minecraft.astralibs.core) // Exposed implementation(libs.exposed.core) implementation(libs.exposed.dao) diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt index a2522a5..e76fdd9 100644 --- a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/api/impl/RatingDBApiImpl.kt @@ -8,6 +8,8 @@ import org.jetbrains.exposed.sql.insertAndGetId import org.jetbrains.exposed.sql.or import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction +import ru.astrainteractive.astralibs.logging.JUtiltLogger +import ru.astrainteractive.astralibs.logging.Logger import ru.astrainteractive.astrarating.api.rating.api.RatingDBApi import ru.astrainteractive.astrarating.db.rating.entity.UserDAO import ru.astrainteractive.astrarating.db.rating.entity.UserRatingDAO @@ -20,10 +22,18 @@ import ru.astrainteractive.astrarating.dto.RatingType import ru.astrainteractive.astrarating.dto.UserDTO import ru.astrainteractive.astrarating.dto.UserRatingDTO import ru.astrainteractive.astrarating.model.UserModel +import ru.astrainteractive.klibs.kdi.Provider internal class RatingDBApiImpl( private val database: Database, -) : RatingDBApi { + private val isDebugProvider: Provider +) : RatingDBApi, Logger by JUtiltLogger("RatingDBApi") { + + private fun Result.logFailure(): Result { + if (!isDebugProvider.provide()) return this + onFailure { throwable -> error(throwable) { throwable.message ?: throwable.localizedMessage } } + return this + } override suspend fun selectUser(playerName: String): Result = kotlin.runCatching { transaction(database) { @@ -32,14 +42,14 @@ internal class RatingDBApiImpl( .or { UserTable.minecraftName.eq(playerName) } }.firstOrNull()?.let(UserMapper::toDTO) ?: error("Could not find $playerName") } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun updateUser(user: UserDTO) = kotlin.runCatching { transaction(database) { val userDao = UserDAO.findById(user.id) ?: error("User not found!") userDao.lastUpdated = System.currentTimeMillis() } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun insertUser(user: UserModel) = kotlin.runCatching { transaction(database) { @@ -50,7 +60,7 @@ internal class RatingDBApiImpl( it[discordID] = null }.value } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun insertUserRating( reporter: UserDTO?, @@ -69,7 +79,7 @@ internal class RatingDBApiImpl( it[ratingTypeIndex] = type.ordinal }.value } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun deleteUserRating(it: UserRatingDTO) = kotlin.runCatching { transaction(database) { @@ -77,7 +87,7 @@ internal class RatingDBApiImpl( UserRatingTable.id.eq(it.id) } } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun fetchUserRatings(playerName: String) = kotlin.runCatching { val reportedUser = selectUser(playerName).getOrThrow() @@ -86,7 +96,7 @@ internal class RatingDBApiImpl( UserRatingTable.reportedUser.eq(reportedUser.id) }.map(UserRatingMapper::toDTO) } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun fetchUsersTotalRating() = kotlin.runCatching { transaction(database) { @@ -98,7 +108,7 @@ internal class RatingDBApiImpl( ) } } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun countPlayerTotalDayRated(playerName: String) = kotlin.runCatching { val user = selectUser(playerName).getOrThrow() @@ -110,7 +120,7 @@ internal class RatingDBApiImpl( } }.count() } - }.onFailure(Throwable::printStackTrace) + }.logFailure() override suspend fun countPlayerOnPlayerDayRated(playerName: String, ratedPlayerName: String) = kotlin.runCatching { val playerCreatedReport = selectUser(playerName).getOrThrow() @@ -123,5 +133,5 @@ internal class RatingDBApiImpl( .and { UserRatingTable.reportedUser.eq(ratedPlayer.id) } }.count() } - }.onFailure(Throwable::printStackTrace) + }.logFailure() } diff --git a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt index 6590359..f22dd13 100644 --- a/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt +++ b/modules/api-rating/src/main/kotlin/ru/astrainteractive/astrarating/api/rating/di/ApiRatingModule.kt @@ -16,11 +16,13 @@ interface ApiRatingModule { class Default( database: Database, coroutineScope: CoroutineScope, + isDebugProvider: Provider ) : ApiRatingModule { override val ratingDBApi: RatingDBApi by Provider { RatingDBApiImpl( database = database, + isDebugProvider = isDebugProvider ) } override val cachedApi: CachedApi by Provider { diff --git a/modules/api-rating/src/test/java/AuctionsTests.kt b/modules/api-rating/src/test/java/AuctionsTests.kt index 0a2ce00..27751e1 100644 --- a/modules/api-rating/src/test/java/AuctionsTests.kt +++ b/modules/api-rating/src/test/java/AuctionsTests.kt @@ -30,6 +30,7 @@ class AuctionsTests { ApiRatingModule.Default( database = database.value, coroutineScope = GlobalScope, + isDebugProvider = { false } ).ratingDBApi } diff --git a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt index 00e7930..edea7b4 100644 --- a/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt +++ b/modules/core/src/main/kotlin/ru/astrainteractive/astrarating/core/EmpireConfig.kt @@ -28,7 +28,8 @@ data class EmpireConfig( val databaseConnection: ConnectionModel = ConnectionModel(), @SerialName("coloring") val coloring: List = emptyList(), - val events: Events = Events() + val events: Events = Events(), + val debug: Boolean = false ) { @Serializable data class Coloring( diff --git a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt index fa8e31d..c818dc1 100644 --- a/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt +++ b/modules/gui-core-bukkit/src/main/kotlin/ru/astrainteractive/astrarating/gui/playerratings/PlayerRatingsGUI.kt @@ -176,9 +176,9 @@ internal class PlayerRatingsGUI( displayName( translationContext.toComponent( translation.playerNameColor.raw + ( - userAndRating.userCreatedReport?.normalName - ?: "-" - ) + userAndRating.userCreatedReport?.normalName + ?: "-" + ) ) ) buildList { diff --git a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt index 07a38d0..1a5df74 100644 --- a/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt +++ b/spigot/src/main/kotlin/ru/astrainteractive/astrarating/di/impl/RootModuleImpl.kt @@ -40,6 +40,7 @@ class RootModuleImpl : RootModule { ApiRatingModule.Default( database = dbRatingModule.database, coroutineScope = scope, + isDebugProvider = { coreModule.config.value.debug } ) } From 85e0b5f96f71e610cdb69ef5724fd1a82604e1b1 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sun, 7 Jul 2024 01:15:28 +0300 Subject: [PATCH 14/14] fix versions --- gradle/libs.versions.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7108ef1..9fd7d11 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,11 +16,11 @@ klibs-kdi = "1.4.5" # https://github.com/makeevrserg/klibs.kdi # Minecraft minecraft-velocity = "4.0.0-SNAPSHOT" # https://github.com/PaperMC/Velocity -minecraft-spigot = "1.20.6-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper +minecraft-spigot = "1.21-R0.1-SNAPSHOT" # https://github.com/PaperMC/Paper minecraft-papi = "2.11.6" # https://github.com/PlaceholderAPI/PlaceholderAPI minecraft-protocollib = "5.1.0" # https://github.com/dmulloy2/ProtocolLib minecraft-vault = "1.7.1" # https://github.com/MilkBowl/VaultAPI -minecraft-astralibs = "3.9.3" # https://github.com/Astra-Interactive/AstraLibs +minecraft-astralibs = "3.9.0" # https://github.com/Astra-Interactive/AstraLibs minecraft-bstats = "3.0.2" # https://github.com/Bastian/bStats minecraft-mockbukkit = "v3.92.1" #https://github.com/MockBukkit/MockBukkit