Skip to content

Commit

Permalink
👍 Change database access to work with IO dispatcher as much as possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nlkomaru committed Jan 27, 2022
1 parent a76cc41 commit 2196fd7
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 512 deletions.
7 changes: 3 additions & 4 deletions src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import dev.nikomaru.raceassist.race.event.SetCentralPointEvent
import dev.nikomaru.raceassist.race.event.SetInsideCircuitEvent
import dev.nikomaru.raceassist.race.event.SetOutsideCircuitEvent
import dev.nikomaru.raceassist.utils.Lang
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.selectAll
Expand Down Expand Up @@ -91,9 +90,9 @@ class RaceAssist : SuspendingJavaPlugin() {
}

private fun registerEvents() {
Bukkit.getPluginManager().registerEvents(SetInsideCircuitEvent(), this)
Bukkit.getPluginManager().registerEvents(SetOutsideCircuitEvent(), this)
Bukkit.getPluginManager().registerEvents(SetCentralPointEvent(), this)
server.pluginManager.registerSuspendingEvents(SetInsideCircuitEvent(), this)
server.pluginManager.registerSuspendingEvents(SetOutsideCircuitEvent(), this)
server.pluginManager.registerSuspendingEvents(SetCentralPointEvent(), this)
server.pluginManager.registerSuspendingEvents(BetGuiClickEvent(), this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,60 @@ import co.aikar.commands.annotation.CommandAlias
import co.aikar.commands.annotation.CommandCompletion
import co.aikar.commands.annotation.Single
import co.aikar.commands.annotation.Subcommand
import com.github.shynixn.mccoroutine.launch
import dev.nikomaru.raceassist.RaceAssist.Companion.plugin
import dev.nikomaru.raceassist.bet.gui.BetChestGui
import dev.nikomaru.raceassist.bet.gui.BetChestGui.Companion.AllPlayers
import dev.nikomaru.raceassist.database.BetSetting
import dev.nikomaru.raceassist.database.TempBetData
import dev.nikomaru.raceassist.utils.Lang
import kotlinx.coroutines.Dispatchers
import org.bukkit.entity.Player
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction

@CommandAlias("ra|RaceAssist")
class OpenBetGuiCommand : BaseCommand() {

@Subcommand("bet open")
@CommandCompletion("@RaceID")
fun openVending(player: Player, @Single raceID: String) {
if (!raceExist(raceID)) {
player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale()))
return
}
val vending = BetChestGui()
val canBet = transaction { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.canBet] }
if (!canBet) {
player.sendMessage(Lang.getText("now-cannot-bet-race", player.locale()))
return
}

transaction {
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
player.openInventory(vending.getGUI(player, raceID))
plugin!!.launch {
if (!raceExist(raceID)) {
player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale()))
return@launch
}
val vending = BetChestGui()
val canBet = newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.canBet] }
if (!canBet) {
player.sendMessage(Lang.getText("now-cannot-bet-race", player.locale()))
return@launch
}

}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
player.openInventory(vending.getGUI(player, raceID))
}

transaction {
AllPlayers[raceID]?.forEach { jockey ->
TempBetData.insert {
it[TempBetData.raceID] = raceID
it[playerUUID] = player.uniqueId.toString()
it[TempBetData.jockey] = jockey.toString()
it[bet] = 0
newSuspendedTransaction(Dispatchers.IO) {
AllPlayers[raceID]?.forEach { jockey ->
TempBetData.insert {
it[TempBetData.raceID] = raceID
it[playerUUID] = player.uniqueId.toString()
it[TempBetData.jockey] = jockey.toString()
it[bet] = 0
}
}
}
}

}
}

private fun raceExist(raceID: String): Boolean {
var exist = false
transaction {
exist = BetSetting.select { BetSetting.raceID eq raceID }.count() > 0
}
return exist
private suspend fun raceExist(raceID: String) = newSuspendedTransaction(Dispatchers.IO) {
BetSetting.select { BetSetting.raceID eq raceID }.count() > 0
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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 org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import java.text.MessageFormat
import java.util.*
Expand Down Expand Up @@ -151,7 +150,7 @@ class SetBetCommand : BaseCommand() {

if (canRevert[player.uniqueId] == true) {

transaction {
newSuspendedTransaction(Dispatchers.Main) {

BetList.select { BetList.raceID eq raceID }.forEach {

Expand Down Expand Up @@ -250,7 +249,8 @@ class SetBetCommand : BaseCommand() {
}
}

private fun getRaceCreator(raceID: String) = transaction { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] }
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
Expand Down
130 changes: 60 additions & 70 deletions src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.ItemStack
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import java.text.MessageFormat
import java.time.LocalDateTime
import java.util.*
Expand Down Expand Up @@ -90,18 +89,17 @@ class BetGuiClickEvent : Listener {
val selectedNowBet: Int = getNowBet(raceID, player, slot)
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 1.0f)

withContext(Dispatchers.IO) {
transaction {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot
).toString())
}) {
it[bet] = selectedNowBet + 10
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot
).toString())
}) {
it[bet] = selectedNowBet + 10
}
}


val selectedAfterBet: Int = getNowBet(raceID, player, (slot))
val item = event.inventory.getItem(slot + 18)!!
val itemMeta = item.itemMeta
Expand All @@ -125,17 +123,16 @@ class BetGuiClickEvent : Listener {
val selectedNowBet: Int = getNowBet(raceID, player, (slot - 9))
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f)

withContext(Dispatchers.IO) {
transaction {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 9
).toString())
}) {
it[bet] = selectedNowBet + 1
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 9
).toString())
}) {
it[bet] = selectedNowBet + 1
}
}

val selectedAfterBet: Int = getNowBet(raceID, player, (slot - 9))
val item = event.inventory.getItem(slot + 9)!!
val itemMeta = item.itemMeta
Expand Down Expand Up @@ -168,17 +165,16 @@ class BetGuiClickEvent : Listener {
}
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 0.7f)
val uuid = player.uniqueId.toString()
withContext(Dispatchers.IO) {
transaction {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 27
).toString())
}) {
it[bet] = selectedNowBet - 1
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 27
).toString())
}) {
it[bet] = selectedNowBet - 1
}
}

val selectedAfterBet: Int = getNowBet(raceID, player, (slot - 27))
val item = event.inventory.getItem(slot - 9)!!
val itemMeta = item.itemMeta
Expand Down Expand Up @@ -211,15 +207,13 @@ class BetGuiClickEvent : Listener {
}
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 0.7f)
val uuid = player.uniqueId.toString()
withContext(Dispatchers.IO) {
transaction {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 36
).toString())
}) {
it[bet] = selectedNowBet - 10
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.update({
(TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get(
slot - 36
).toString())
}) {
it[bet] = selectedNowBet - 10
}
}

Expand All @@ -242,19 +236,18 @@ class BetGuiClickEvent : Listener {
//clear
player.playSound(player.location, Sound.UI_BUTTON_CLICK, 1f, 1f)
val uuid = player.uniqueId.toString()
withContext(Dispatchers.IO) {
transaction {
TempBetData.deleteWhere { (TempBetData.playerUUID eq uuid) and (TempBetData.raceID eq raceID) }
AllPlayers[raceID]?.forEach { jockey ->
TempBetData.insert {
it[TempBetData.raceID] = raceID
it[playerUUID] = uuid
it[TempBetData.jockey] = jockey.toString()
it[bet] = 0
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.deleteWhere { (TempBetData.playerUUID eq uuid) and (TempBetData.raceID eq raceID) }
AllPlayers[raceID]?.forEach { jockey ->
TempBetData.insert {
it[TempBetData.raceID] = raceID
it[playerUUID] = uuid
it[TempBetData.jockey] = jockey.toString()
it[bet] = 0
}
}
}

for (i in 0 until limit + 1) {
val item = event.inventory.getItem(i + 18)!!
val itemMeta = item.itemMeta
Expand All @@ -272,10 +265,8 @@ class BetGuiClickEvent : Listener {
player.closeInventory()
player.playSound(player.location, Sound.UI_BUTTON_CLICK, 0.5f, 1f)

withContext(Dispatchers.IO) {
transaction {
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
}
newSuspendedTransaction(Dispatchers.IO) {
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
}

}
Expand All @@ -299,27 +290,26 @@ class BetGuiClickEvent : Listener {
BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator]
}))

withContext(Dispatchers.Default) {
transaction {
var row = BetList.selectAll().count().toInt()
TempBetData.select { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
.forEach { temp ->
if (temp[TempBetData.bet] != 0) {
BetList.insert { bet ->
bet[BetList.raceID] = raceID
bet[playerName] = player.name
bet[playerUUID] = player.uniqueId.toString()
bet[jockey] = Bukkit.getOfflinePlayer(UUID.fromString(temp[TempBetData.jockey])).name.toString()
bet[betting] = temp[TempBetData.bet] * betUnit
bet[timeStamp] = LocalDateTime.now()
bet[rowNum] = row + 1
}
betProcess(player, row, temp, eco, owner)
row++

newSuspendedTransaction(Dispatchers.Default) {
var row = BetList.selectAll().count().toInt()
TempBetData.select { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
.forEach { temp ->
if (temp[TempBetData.bet] != 0) {
BetList.insert { bet ->
bet[BetList.raceID] = raceID
bet[playerName] = player.name
bet[playerUUID] = player.uniqueId.toString()
bet[jockey] = Bukkit.getOfflinePlayer(UUID.fromString(temp[TempBetData.jockey])).name.toString()
bet[betting] = temp[TempBetData.bet] * betUnit
bet[timeStamp] = LocalDateTime.now()
bet[rowNum] = row + 1
}
betProcess(player, row, temp, eco, owner)
row++
}
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
}
}
TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) }
}
putSheetsData(raceID)
}
Expand Down
Loading

0 comments on commit 2196fd7

Please sign in to comment.