Skip to content

Commit

Permalink
Fix Bet revert and add Bet remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Nlkomaru committed Feb 12, 2022
1 parent 9c6a03d commit 1ee4466
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class RaceAssist : SuspendingJavaPlugin() {
annotationParser.parse(BetRevertCommand())
annotationParser.parse(BetSheetCommand())
annotationParser.parse(RaceCopyCommand())
annotationParser.parse(BetRemoveCommand())
}

private fun registerEvents() {
Expand Down
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]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit 1ee4466

Please sign in to comment.