Skip to content

Commit

Permalink
Added CompletableFuture to fetch async data from database.
Browse files Browse the repository at this point in the history
Fixed self operations... money actually not added and synchronized into account if account is same as name.
  • Loading branch information
EncryptSL committed May 14, 2024
1 parent 50ac6ae commit d535dee
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 36 deletions.
6 changes: 1 addition & 5 deletions src/main/kotlin/com/github/encryptsl/lite/eco/LiteEco.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ class LiteEco : JavaPlugin() {
}

private fun createAnnotationParser(commandManager: PaperCommandManager<CommandSender>): AnnotationParser<CommandSender> {
val annotationParser: AnnotationParser<CommandSender> = AnnotationParser(
commandManager,
CommandSender::class.java
)
return annotationParser
return AnnotationParser<CommandSender>(commandManager, CommandSender::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider {
}

override fun hasAccount(player: OfflinePlayer): Boolean {
return databaseEcoModel.getExistPlayerAccount(player.uniqueId)
return databaseEcoModel.getExistPlayerAccount(player.uniqueId).join()
}

override fun has(player: OfflinePlayer, amount: Double): Boolean {
Expand All @@ -50,7 +50,7 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider {
return if (PlayerAccount.isPlayerOnline(player.uniqueId) || PlayerAccount.isAccountCached(player.uniqueId))
PlayerAccount.getBalance(player.uniqueId)
else
databaseEcoModel.getBalance(player.uniqueId)
databaseEcoModel.getBalance(player.uniqueId).join()
}

override fun getCheckBalanceLimit(amount: Double): Boolean {
Expand Down Expand Up @@ -85,8 +85,12 @@ class LiteEcoEconomyAPI : LiteEconomyAPIProvider {
override fun setMoney(player: OfflinePlayer, amount: Double) {
if (PlayerAccount.isPlayerOnline(player.uniqueId)) {
cacheAccount(player, amount)
println(amount)
println(getBalance(player))
} else {
databaseEcoModel.setMoney(player.uniqueId, amount)
println(amount)
println(getBalance(player))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.encryptsl.lite.eco.api.interfaces

import com.github.encryptsl.lite.eco.common.database.entity.EconomyLog
import java.util.concurrent.CompletableFuture

interface AdapterLogger {
fun error(message: String)
fun warning(message: String)
fun info(message: String)
fun clearLogs()
fun getLog(): List<EconomyLog>
fun getLog(): CompletableFuture<List<EconomyLog>>
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.encryptsl.lite.eco.api.interfaces

import java.util.*
import java.util.concurrent.CompletableFuture

interface PlayerSQLProvider {
fun createPlayerAccount(username: String, uuid: UUID, money: Double)
fun deletePlayerAccount(uuid: UUID)
fun getExistPlayerAccount(uuid: UUID): Boolean
fun getExistPlayerAccount(uuid: UUID): CompletableFuture<Boolean>
fun getTopBalance(top: Int): MutableMap<String, Double>
fun getTopBalance(): MutableMap<String, Double>
fun getPlayersIds(): MutableCollection<UUID>
fun getBalance(uuid: UUID): Double
fun getBalance(uuid: UUID): CompletableFuture<Double>
fun depositMoney(uuid: UUID, money: Double)
fun withdrawMoney(uuid: UUID, money: Double)
fun setMoney(uuid: UUID, money: Double)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class EcoCMD(private val liteEco: LiteEco) {
commandSender.sendMessage(liteEco.locale.translation("messages.admin.purge_default_accounts"))
}
PurgeKey.MONO_LOG -> {
val logs = liteEco.loggerModel.getLog()
val logs = liteEco.loggerModel.getLog().join()

if (logs.isEmpty())
return commandSender.sendMessage(liteEco.locale.translation("messages.error.purge_monolog_fail"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.minus
import org.jetbrains.exposed.sql.SqlExpressionBuilder.notInList
import org.jetbrains.exposed.sql.SqlExpressionBuilder.plus
import java.util.*
import java.util.concurrent.CompletableFuture

class DatabaseEcoModel : PlayerSQLProvider {

Expand All @@ -29,8 +30,11 @@ class DatabaseEcoModel : PlayerSQLProvider {
}
}

override fun getExistPlayerAccount(uuid: UUID): Boolean = loggedTransaction {
!Account.select(Account.uuid).where(Account.uuid eq uuid.toString()).empty()
override fun getExistPlayerAccount(uuid: UUID): CompletableFuture<Boolean> {
val future = CompletableFuture<Boolean>()
val boolean = loggedTransaction { !Account.select(Account.uuid).where(Account.uuid eq uuid.toString()).empty() }
future.completeAsync { boolean }
return future
}

override fun getTopBalance(top: Int): MutableMap<String, Double> = loggedTransaction {
Expand All @@ -49,8 +53,13 @@ class DatabaseEcoModel : PlayerSQLProvider {
Account.selectAll().map { UUID.fromString(it[Account.uuid]) }.toMutableList()
}

override fun getBalance(uuid: UUID): Double = loggedTransaction {
Account.select(Account.uuid, Account.money).where(Account.uuid eq uuid.toString()).first()[Account.money]
override fun getBalance(uuid: UUID): CompletableFuture<Double> {
val future = CompletableFuture<Double>()
val balance = loggedTransaction {
Account.select(Account.uuid, Account.money).where(Account.uuid eq uuid.toString()).first()[Account.money]
}
future.completeAsync { balance }
return future
}

override fun depositMoney(uuid: UUID, money: Double) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import java.util.concurrent.CompletableFuture
import java.util.logging.Level

class DatabaseMonologModel(val plugin: Plugin) : AdapterLogger {
Expand All @@ -28,10 +29,15 @@ class DatabaseMonologModel(val plugin: Plugin) : AdapterLogger {
loggedTransaction { MonologTable.deleteAll() }
}

override fun getLog(): List<EconomyLog> {
return loggedTransaction { MonologTable.selectAll().orderBy(MonologTable.timestamp, SortOrder.DESC).mapNotNull {
EconomyLog(it[MonologTable.level], it[MonologTable.log], it[MonologTable.timestamp])
} }
override fun getLog(): CompletableFuture<List<EconomyLog>> {
val future = CompletableFuture<List<EconomyLog>>()
val log = loggedTransaction {
MonologTable.selectAll().orderBy(MonologTable.timestamp, SortOrder.DESC).mapNotNull {
EconomyLog(it[MonologTable.level], it[MonologTable.log], it[MonologTable.timestamp])
}
}
future.completeAsync { log }
return future
}

private fun log(level: Level, message: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccountManageListener(private val liteEco: LiteEco) : Listener {

when (event.operationType) {
OperationType.CREATE_ACCOUNT -> liteEco.api.createAccount(player, liteEco.config.getDouble("economy.starting_balance"))
OperationType.CACHING_ACCOUNT -> liteEco.api.cacheAccount(player, liteEco.databaseEcoModel.getBalance(player.uniqueId))
OperationType.CACHING_ACCOUNT -> liteEco.api.cacheAccount(player, liteEco.databaseEcoModel.getBalance(player.uniqueId).join())
OperationType.SYNC_ACCOUNT -> liteEco.api.syncAccount(player)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ class EconomyMoneyDepositListener(private val liteEco: LiteEco) : Listener {
Placeholder.parsed("account", target.name.toString())
))

if (sender.name == target.name && !target.isOp) {
return sender.sendMessage(
liteEco.locale.translation("messages.error.self_pay", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))
)
}

liteEco.increaseTransactions(1)
liteEco.api.depositMoney(target, money)
liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.deposit")
Expand All @@ -44,6 +38,12 @@ class EconomyMoneyDepositListener(private val liteEco: LiteEco) : Listener {
.replace("<money>", liteEco.api.fullFormatting(money))
)

if (sender.name == target.name && !target.isOp) {
return sender.sendMessage(
liteEco.locale.translation("messages.error.self_pay", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))
)
}

sender.sendMessage(liteEco.locale.translation("messages.sender.add_money",
TagResolver.resolver(
Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class EconomyMoneySetListener(private val liteEco: LiteEco) : Listener {

liteEco.increaseTransactions(1)

if (sender.name == target.name)
return sender.sendMessage(liteEco.locale.translation("messages.self.set_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money))))

liteEco.api.setMoney(target, money)
liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.set")
.replace("<sender>", sender.name)
.replace("<target>", target.name.toString())
.replace("<money>", liteEco.api.fullFormatting(money))
)

if (sender.name == target.name)
return sender.sendMessage(liteEco.locale.translation("messages.self.set_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money))))

sender.sendMessage(
liteEco.locale.translation("messages.sender.set_money",
TagResolver.resolver(Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money)))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ class EconomyMoneyWithdrawListener(private val liteEco: LiteEco) : Listener {
if (!liteEco.api.has(target, money))
return sender.sendMessage(liteEco.locale.translation("messages.error.insufficient_funds"))

if (sender.name == target.name)
return sender.sendMessage(
liteEco.locale.translation("messages.self.withdraw_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))
)

liteEco.increaseTransactions(1)
liteEco.api.withDrawMoney(target, money)
liteEco.loggerModel.info(liteEco.locale.getMessage("messages.monolog.admin.normal.withdraw")
Expand All @@ -38,9 +33,15 @@ class EconomyMoneyWithdrawListener(private val liteEco: LiteEco) : Listener {
.replace("<money>", liteEco.api.fullFormatting(money))
)

if (sender.name == target.name)
return sender.sendMessage(
liteEco.locale.translation("messages.self.withdraw_money", Placeholder.parsed("money", liteEco.api.fullFormatting(money)))
)

sender.sendMessage(
liteEco.locale.translation("messages.sender.withdraw_money",
TagResolver.resolver(Placeholder.parsed("target", target.name.toString()), Placeholder.parsed("money", liteEco.api.fullFormatting(money)))))

if (target.isOnline && liteEco.config.getBoolean("messages.target.notify_withdraw")) {
if (silent) {
target.player?.sendMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class Helper(private val liteEco: LiteEco) {
}

fun validateLog(player: String?): List<EconomyLog> {
val log = liteEco.loggerModel.getLog()

val log = liteEco.loggerModel.getLog().join()
if (player != null) {
return log.filter { l -> l.log.contains(player, true) }
}
Expand Down

0 comments on commit d535dee

Please sign in to comment.