diff --git a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt index c6c5eb9..38813b9 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt @@ -127,6 +127,7 @@ class RaceAssist : SuspendingJavaPlugin() { annotationParser.parse(BetRevertCommand()) annotationParser.parse(BetSheetCommand()) annotationParser.parse(RaceCopyCommand()) + annotationParser.parse(BetRemoveCommand()) } private fun registerEvents() { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt new file mode 100644 index 0000000..72aaf43 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt @@ -0,0 +1,105 @@ +/* + * Copyright © 2021-2022 Nikomaru + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.bet.commands + +import cloud.commandframework.annotations.Argument +import cloud.commandframework.annotations.CommandMethod +import cloud.commandframework.annotations.CommandPermission +import com.github.shynixn.mccoroutine.launch +import dev.nikomaru.raceassist.RaceAssist +import dev.nikomaru.raceassist.api.VaultAPI +import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.database.BetSetting +import dev.nikomaru.raceassist.utils.Lang +import dev.nikomaru.raceassist.utils.coroutines.minecraft +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import net.milkbowl.vault.economy.Economy +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.deleteWhere +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction +import java.text.MessageFormat +import java.util.* + +@CommandMethod("ra|RaceAssist bet") +class BetRemoveCommand { + @CommandPermission("RaceAssist.commands.bet.revert") + @CommandMethod("remove ") + fun remove(player: Player, @Argument(value = "raceId", suggestions = "raceId") raceID: String, @Argument(value = "betId") betId: Int) { + val eco: Economy = VaultAPI.getEconomy() + RaceAssist.plugin.launch { + withContext(Dispatchers.IO) { + if (!raceExist(raceID)) { + player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) + return@withContext + } + if (getRaceCreator(raceID) != player.uniqueId.toString()) { + player.sendMessage(Lang.getText("only-race-creator-can-setting", player.locale())) + return@withContext + } + if (eco.getBalance(player) < getBetSum(raceID)) { + player.sendMessage(Lang.getText("no-have-money", player.locale())) + return@withContext + } + } + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { (BetList.rowNum eq betId) and (BetList.raceID eq raceID) }.forEach { + val receiver = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID].toString())) + withContext(Dispatchers.minecraft) { + eco.withdrawPlayer(player, it[BetList.betting].toDouble()) + eco.depositPlayer(receiver, it[BetList.betting].toDouble()) + } + player.sendMessage(MessageFormat.format(Lang.getText("bet-revert-return-message-owner", player.locale()), + receiver.name, + it[BetList.betting])) + + if (receiver.isOnline) { + (receiver as Player).sendMessage(MessageFormat.format(Lang.getText("bet-revert-return-message-player", receiver.locale()), + player.name, + it[BetList.raceID], + it[BetList.jockey], + it[BetList.betting])) + } + + } + + BetList.deleteWhere { (BetList.rowNum eq betId) and (BetList.raceID eq raceID) } + } + + } + } + + private suspend fun getRaceCreator(raceID: String) = + newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] } + + private suspend fun raceExist(raceID: String): Boolean { + var exist = false + newSuspendedTransaction(Dispatchers.IO) { + exist = BetSetting.select { BetSetting.raceID eq raceID }.count() > 0 + } + return exist + } + + private suspend fun getBetSum(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + BetList.select { BetList.raceID eq raceID }.sumOf { + it[BetList.betting] + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt index 092835d..7bf3839 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt @@ -25,12 +25,14 @@ import dev.nikomaru.raceassist.api.VaultAPI import dev.nikomaru.raceassist.database.BetList import dev.nikomaru.raceassist.database.BetSetting import dev.nikomaru.raceassist.utils.Lang +import dev.nikomaru.raceassist.utils.coroutines.minecraft import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.withContext import net.milkbowl.vault.economy.Economy import org.bukkit.Bukkit import org.bukkit.entity.Player +import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.text.MessageFormat @@ -60,14 +62,14 @@ class BetRevertCommand { if (canRevert[player.uniqueId] == true) { - newSuspendedTransaction(Dispatchers.Main) { + newSuspendedTransaction(Dispatchers.IO) { BetList.select { BetList.raceID eq raceID }.forEach { - val receiver = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID].toString())) - eco.withdrawPlayer(player, it[BetList.betting].toDouble()) - - eco.depositPlayer(receiver, it[BetList.betting].toDouble()) + withContext(Dispatchers.minecraft) { + eco.withdrawPlayer(player, it[BetList.betting].toDouble()) + eco.depositPlayer(receiver, it[BetList.betting].toDouble()) + } player.sendMessage(MessageFormat.format(Lang.getText("bet-revert-return-message-owner", player.locale()), receiver.name, it[BetList.betting])) @@ -80,6 +82,7 @@ class BetRevertCommand { it[BetList.betting])) } } + BetList.deleteWhere { BetList.raceID eq raceID } } player.sendMessage(MessageFormat.format(Lang.getText("bet-revert-complete-message", player.locale()), raceID)) } else { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt index ec7ed06..b433e44 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt @@ -24,6 +24,7 @@ import dev.nikomaru.raceassist.bet.GuiComponent import dev.nikomaru.raceassist.bet.commands.BetOpenCommand.Companion.TempBetDatas import dev.nikomaru.raceassist.bet.gui.BetChestGui.Companion.AllPlayers import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.database.BetList.rowNum import dev.nikomaru.raceassist.database.BetSetting import dev.nikomaru.raceassist.files.Config.betUnit import dev.nikomaru.raceassist.utils.Lang @@ -46,7 +47,6 @@ import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.inventory.ItemStack import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.text.MessageFormat import java.time.LocalDateTime @@ -266,7 +266,7 @@ class BetGuiClickEvent : Listener { newSuspendedTransaction(Dispatchers.Default) { - var row = BetList.selectAll().count().toInt() + var row = getMaxRow(raceID) val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val temp = iterator.next() @@ -304,6 +304,16 @@ class BetGuiClickEvent : Listener { event.inventory.setItem(44, accept) } + private suspend fun getMaxRow(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + var maxRow = 0 + BetList.select { BetList.raceID eq raceID }.forEach { + if (it[rowNum] > maxRow) { + maxRow = it[rowNum] + } + } + maxRow + } + private fun betProcess(player: Player, row: Int, bet: Int, jockey: UUID, eco: Economy, owner: OfflinePlayer) { player.sendMessage(MessageFormat.format(Lang.getText("bet-complete-message-player", player.locale()), row + 1, diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt index 64f753b..78629a9 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt @@ -73,7 +73,7 @@ class PlayerAddCommand { it[playerUUID] = jockey.uniqueId.toString() } } - player.sendMessage(MessageFormat.format(Lang.getText("player-add-to-race-group", player.locale()), player.name, raceID)) + player.sendMessage(MessageFormat.format(Lang.getText("player-add-to-race-group", player.locale()), jockey.name, raceID)) } } } \ No newline at end of file