-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright © 2021-2022 Nikomaru <[email protected]> | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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 <raceId> <betId>") | ||
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] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters