Skip to content

Commit

Permalink
Merge pull request #28 from Astra-Interactive/refactor-modules
Browse files Browse the repository at this point in the history
- Refactor modules
- Improve kill event
- Fix #27 
- Fix error logging by adding debug tag
  • Loading branch information
makeevrserg committed Jul 6, 2024
2 parents f00d069 + 85e0b5f commit 6678ec5
Show file tree
Hide file tree
Showing 91 changed files with 814 additions and 743 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down
21 changes: 21 additions & 0 deletions modules/allratings-command-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions modules/allratings-gui-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions modules/allratings-shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions modules/api-rating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ 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.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
Expand All @@ -19,51 +22,45 @@ 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
import ru.astrainteractive.klibs.kdi.Provider

internal class RatingDBApiImpl(
private val database: Database,
private val pluginFolder: File
) : RatingDBApi {
private fun <T> Result<T>.logStackTrace(): Result<T> {
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")
}
private val isDebugProvider: Provider<Boolean>
) : RatingDBApi, Logger by JUtiltLogger("RatingDBApi") {

private fun <T> Result<T>.logFailure(): Result<T> {
if (!isDebugProvider.provide()) return this
onFailure { throwable -> error(throwable) { throwable.message ?: throwable.localizedMessage } }
return this
}

override suspend fun selectUser(playerName: String): Result<UserDTO> = kotlin.runCatching {
transaction(database) {
UserDAO.find {
UserTable.minecraftName.eq(playerName.uppercase())
}.first().let(UserMapper::toDTO)
.or { UserTable.minecraftName.eq(playerName) }
}.firstOrNull()?.let(UserMapper::toDTO) ?: error("Could not find $playerName")
}
}.logStackTrace()
}.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()
}
}.logStackTrace()
}.logFailure()

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()
}.logFailure()

override suspend fun insertUserRating(
reporter: UserDTO?,
Expand All @@ -82,15 +79,15 @@ internal class RatingDBApiImpl(
it[ratingTypeIndex] = type.ordinal
}.value
}
}.logStackTrace()
}.logFailure()

override suspend fun deleteUserRating(it: UserRatingDTO) = kotlin.runCatching {
transaction(database) {
UserRatingTable.deleteWhere { _ ->
UserRatingTable.id.eq(it.id)
}
}
}.logStackTrace()
}.logFailure()

override suspend fun fetchUserRatings(playerName: String) = kotlin.runCatching {
val reportedUser = selectUser(playerName).getOrThrow()
Expand All @@ -99,7 +96,7 @@ internal class RatingDBApiImpl(
UserRatingTable.reportedUser.eq(reportedUser.id)
}.map(UserRatingMapper::toDTO)
}
}.logStackTrace()
}.logFailure()

override suspend fun fetchUsersTotalRating() = kotlin.runCatching {
transaction(database) {
Expand All @@ -111,28 +108,30 @@ internal class RatingDBApiImpl(
)
}
}
}.logStackTrace()
}.logFailure()

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()
}.logFailure()

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()
}.logFailure()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,13 +16,13 @@ interface ApiRatingModule {
class Default(
database: Database,
coroutineScope: CoroutineScope,
pluginFolder: File
isDebugProvider: Provider<Boolean>
) : ApiRatingModule {

override val ratingDBApi: RatingDBApi by Provider {
RatingDBApiImpl(
database = database,
pluginFolder = pluginFolder
isDebugProvider = isDebugProvider
)
}
override val cachedApi: CachedApi by Provider {
Expand Down
2 changes: 1 addition & 1 deletion modules/api-rating/src/test/java/AuctionsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AuctionsTests {
ApiRatingModule.Default(
database = database.value,
coroutineScope = GlobalScope,
pluginFolder = File("./")
isDebugProvider = { false }
).ratingDBApi
}

Expand Down
27 changes: 27 additions & 0 deletions modules/command-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ 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 && (
Expand Down
Loading

0 comments on commit 6678ec5

Please sign in to comment.