Skip to content

Commit

Permalink
Merge pull request #26 from Nlkomaru/version-2.0
Browse files Browse the repository at this point in the history
Added functions such as saving race results for web app.
  • Loading branch information
Nlkomaru authored Aug 12, 2022
2 parents 9ece9b8 + 1a4e5f8 commit e6c5c07
Show file tree
Hide file tree
Showing 63 changed files with 1,036 additions and 1,397 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:

- uses: release-drafter/release-drafter@v5
id: release-drafter
with:
publish: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
69 changes: 38 additions & 31 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ dependencies {
implementation("cloud.commandframework", "cloud-kotlin-coroutines-annotations", cloudVersion)
implementation("cloud.commandframework", "cloud-kotlin-coroutines", cloudVersion)

implementation("com.github.stefvanschie.inventoryframework", "IF", "0.10.6")

implementation("org.jetbrains.exposed", "exposed-core", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-java-time", exposedVersion)

implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")

implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.6.2")

implementation("com.github.shynixn.mccoroutine", "mccoroutine-bukkit-api", "2.2.0")
Expand All @@ -58,6 +62,8 @@ dependencies {
implementation("com.google.oauth-client", "google-oauth-client-jetty", "1.34.1")
implementation("com.google.apis", "google-api-services-sheets", "v4-rev20220606-1.32.1")

implementation("com.squareup.okhttp3", "okhttp", "4.10.0")

bukkitLibrary("com.google.code.gson", "gson", "2.8.7")
}

Expand All @@ -76,12 +82,13 @@ tasks {
shadowJar {
relocate("cloud.commandframework", "dev.nikomaru.receassist.shaded.cloud")
relocate("io.leangen.geantyref", "dev.nikomaru.receassist.shaded.typetoken")
relocate("com.github.stefvanschie.inventoryframework", "dev.nikomaru.receassist.inventoryframework")
}
build {
dependsOn(shadowJar)
}
runServer {
minecraftVersion("1.19")
minecraftVersion("1.19.2")
}
}

Expand All @@ -101,39 +108,39 @@ bukkit {
permissions {
register("RaceAssist.admin") {
default = Default.OP
children = listOf("RaceAssist.commands.audience.join",
"RaceAssist.commands.audience.leave",
"RaceAssist.commands.audience.list",
"RaceAssist.commands.bet.can",
"RaceAssist.commands.bet.delete",
"RaceAssist.commands.bet.list",
"RaceAssist.commands.bet.open",
"RaceAssist.commands.bet.rate",
"RaceAssist.commands.bet.revert",
"RaceAssist.commands.bet.return",
"RaceAssist.commands.bet.remove",
"RaceAssist.commands.bet.sheet",
"RaceAssist.commands.place.reverse",
"RaceAssist.commands.place.central",
"RaceAssist.commands.place.degree",
"RaceAssist.commands.place.lap",
"RaceAssist.commands.place.set",
"RaceAssist.commands.place.finish",
"RaceAssist.commands.player.add",
"RaceAssist.commands.player.remove",
"RaceAssist.commands.player.delete",
"RaceAssist.commands.player.list",
"RaceAssist.commands.race.start",
"RaceAssist.commands.race.debug",
"RaceAssist.commands.race.stop",
"RaceAssist.commands.setting.create",
"RaceAssist.commands.setting.delete",
"RaceAssist.commands.setting.copy",
"RaceAssist.commands.setting.staff")
children = listOf("raceassist.commands.audience.join",
"raceassist.commands.audience.leave",
"raceassist.commands.audience.list",
"raceassist.commands.bet.can",
"raceassist.commands.bet.delete",
"raceassist.commands.bet.list",
"raceassist.commands.bet.open",
"raceassist.commands.bet.rate",
"raceassist.commands.bet.revert",
"raceassist.commands.bet.return",
"raceassist.commands.bet.remove",
"raceassist.commands.bet.sheet",
"raceassist.commands.place.reverse",
"raceassist.commands.place.central",
"raceassist.commands.place.degree",
"raceassist.commands.place.lap",
"raceassist.commands.place.set",
"raceassist.commands.place.finish",
"raceassist.commands.player.add",
"raceassist.commands.player.remove",
"raceassist.commands.player.delete",
"raceassist.commands.player.list",
"raceassist.commands.race.start",
"raceassist.commands.race.debug",
"raceassist.commands.race.stop",
"raceassist.commands.setting.create",
"raceassist.commands.setting.delete",
"raceassist.commands.setting.copy",
"raceassist.commands.setting.staff")
}
register("RaceAssist.user") {
default = Default.TRUE
children = listOf("RaceAssist.commands.audience.join", "RaceAssist.commands.audience.leave", "RaceAssist.commands.bet.open")
children = listOf("raceassist.commands.audience.join", "raceassist.commands.audience.leave", "raceassist.commands.bet.open")
}
}
}
86 changes: 46 additions & 40 deletions src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import dev.nikomaru.raceassist.files.Config
import dev.nikomaru.raceassist.horse.commands.OwnerDeleteCommand
import dev.nikomaru.raceassist.horse.events.HorseBreedEvent
import dev.nikomaru.raceassist.race.commands.HelpCommand
import dev.nikomaru.raceassist.race.commands.ReloadCommand
import dev.nikomaru.raceassist.race.commands.audience.*
import dev.nikomaru.raceassist.race.commands.place.*
import dev.nikomaru.raceassist.race.commands.player.*
Expand Down Expand Up @@ -91,46 +92,51 @@ class RaceAssist : SuspendingJavaPlugin() {
SimpleCommandMeta.empty()
}.installCoroutineSupport()

annotationParser.parse(CommandSuggestions())

annotationParser.parse(AudienceJoinCommand())
annotationParser.parse(AudienceLeaveCommand())
annotationParser.parse(AudienceListCommand())

annotationParser.parse(PlaceCentralCommand())
annotationParser.parse(PlaceDegreeCommand())
annotationParser.parse(PlaceFinishCommand())
annotationParser.parse(PlaceLapCommand())
annotationParser.parse(PlaceReverseCommand())
annotationParser.parse(PlaceSetCommand())

annotationParser.parse(PlayerAddCommand())
annotationParser.parse(PlayerDeleteCommand())
annotationParser.parse(PlayerListCommand())
annotationParser.parse(PlayerRemoveCommand())

annotationParser.parse(RaceStartCommand())
annotationParser.parse(RaceStopCommand())
annotationParser.parse(RaceDebugCommand())

annotationParser.parse(BetCanCommand())
annotationParser.parse(BetDeleteCommand())
annotationParser.parse(BetListCommand())
annotationParser.parse(BetOpenCommand())
annotationParser.parse(BetRateCommand())
annotationParser.parse(BetRevertCommand())
annotationParser.parse(BetSheetCommand())
annotationParser.parse(BetRemoveCommand())
annotationParser.parse(BetReturnCommand())

annotationParser.parse(SettingCreateCommand())
annotationParser.parse(SettingDeleteCommand())
annotationParser.parse(SettingCopyCommand())
annotationParser.parse(SettingStaffCommand())

annotationParser.parse(HelpCommand())

annotationParser.parse(OwnerDeleteCommand())
with(annotationParser) {
parse(CommandSuggestions())

parse(AudienceJoinCommand())
parse(AudienceLeaveCommand())
parse(AudienceListCommand())

parse(PlaceCentralCommand())
parse(PlaceDegreeCommand())
parse(PlaceFinishCommand())
parse(PlaceLapCommand())
parse(PlaceReverseCommand())
parse(PlaceSetCommand())

parse(PlayerAddCommand())
parse(PlayerDeleteCommand())
parse(PlayerListCommand())
parse(PlayerRemoveCommand())
parse(PlayerReplacementCommand())

parse(RaceStartCommand())
parse(RaceStopCommand())
parse(RaceDebugCommand())

parse(BetCanCommand())
parse(BetDeleteCommand())
parse(BetListCommand())
parse(BetOpenCommand())
parse(BetRateCommand())
parse(BetRevertCommand())
parse(BetSheetCommand())
parse(BetReturnCommand())
parse(BetUnitCommand())

parse(SettingCreateCommand())
parse(SettingDeleteCommand())
parse(SettingCopyCommand())
parse(SettingStaffCommand())
parse(SettingViewCommand())

parse(HelpCommand())
parse(ReloadCommand())

parse(OwnerDeleteCommand())
}
}

private fun registerEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.api.client.json.gson.GsonFactory
import com.google.api.client.util.store.FileDataStoreFactory
import com.google.api.services.sheets.v4.SheetsScopes
import dev.nikomaru.raceassist.RaceAssist.Companion.plugin
import dev.nikomaru.raceassist.files.Config
import java.io.*
import java.security.GeneralSecurityException

Expand All @@ -49,6 +50,6 @@ object GoogleAuthorizeUtil {
val flow: GoogleAuthorizationCodeFlow =
GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), clientSecrets, scopes)
.setDataStoreFactory(FileDataStoreFactory(tokensDirectoryPath)).setAccessType("offline").build()
return AuthorizationCodeInstalledApp(flow, LocalServerReceiver.Builder().setPort(8888).build()).authorize("user")
return AuthorizationCodeInstalledApp(flow, LocalServerReceiver.Builder().setPort(Config.config.spreadSheet.port).build()).authorize("user")
}
}
118 changes: 117 additions & 1 deletion src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,120 @@

package dev.nikomaru.raceassist.bet

object BetUtils {}
import dev.nikomaru.raceassist.api.VaultAPI
import dev.nikomaru.raceassist.bet.data.TempBetData
import dev.nikomaru.raceassist.bet.gui.BetChestGui
import dev.nikomaru.raceassist.data.database.BetList
import dev.nikomaru.raceassist.data.database.BetListData
import dev.nikomaru.raceassist.data.files.BetSettingData
import dev.nikomaru.raceassist.data.files.RaceSettingData
import dev.nikomaru.raceassist.utils.Lang
import dev.nikomaru.raceassist.utils.Utils.locale
import dev.nikomaru.raceassist.utils.Utils.toUUID
import dev.nikomaru.raceassist.utils.coroutines.minecraft
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import java.math.RoundingMode
import java.util.*

object BetUtils {

val TempBetDatas = ArrayList<TempBetData>()

suspend fun deleteBetData(raceId: String) {
newSuspendedTransaction(Dispatchers.IO) {
BetList.deleteWhere { BetList.raceId eq raceId }
}
}

fun removeTempBetData(player: Player) {
val removeList = arrayListOf<TempBetData>()
TempBetDatas.stream().filter { it.player == player }.forEach { removeList.add(it) }
removeList.forEach { TempBetDatas.remove(it) }
}

fun initializeTempBetData(raceId: String, sender: Player) {
BetChestGui.AllPlayers[raceId]?.forEach { jockey ->
TempBetDatas.add(TempBetData(raceId, sender, jockey, 0))
}
}

suspend fun listBetData(raceId: String): ArrayList<BetListData> {
val list = arrayListOf<BetListData>()
newSuspendedTransaction(Dispatchers.IO) {
BetList.select { BetList.raceId eq raceId }.forEach {
list.add(BetListData(it[BetList.rowNum],
it[BetList.timeStamp],
it[BetList.playerUUID].toUUID(),
it[BetList.jockeyUUID].toUUID(),
it[BetList.betting]))
}
}
return list
}

suspend fun getBetSum(raceId: String) = newSuspendedTransaction(Dispatchers.IO) {
BetList.select { BetList.raceId eq raceId }.sumOf {
it[BetList.betting]
}
}

suspend fun getJockeyBetSum(raceId: String, jockey: OfflinePlayer) = newSuspendedTransaction(Dispatchers.IO) {
BetList.select { (BetList.jockeyUUID eq jockey.uniqueId.toString()) and (BetList.raceId eq raceId) }.sumOf { it[BetList.betting] }
}

suspend fun getRowBet(raceId: String, row: Int) = newSuspendedTransaction(Dispatchers.IO) {
BetList.select { (BetList.rowNum eq row) and (BetList.raceId eq raceId) }.sumOf { it[BetList.betting] }
}

suspend fun playerCanPay(raceId: String, amount: Int, executor: CommandSender): Boolean {
val eco = VaultAPI.getEconomy()
val owner = RaceSettingData.getOwner(raceId)
val can = eco.getBalance(owner) >= amount
if (!can) {
val locale = executor.locale()
executor.sendMessage(Lang.getComponent("no-have-money", locale))
if (owner is Player) {
owner.sendMessage(Lang.getComponent("no-have-money-owner", owner.locale()))
}
}
return can
}

//払い戻し
suspend fun returnBet(jockey: OfflinePlayer, raceId: String, sender: CommandSender, locale: Locale) {
val odds = getOdds(raceId, jockey)
val eco = VaultAPI.getEconomy()

newSuspendedTransaction(Dispatchers.IO) {
BetList.select { (BetList.jockey eq jockey.name.toString()) and (BetList.raceId eq raceId) }.forEach {

val returnAmount = it[BetList.betting] * odds
val retunrPlayer = Bukkit.getOfflinePlayer(it[BetList.playerUUID].toUUID())

withContext(minecraft) {

eco.depositPlayer(retunrPlayer, returnAmount)
eco.withdrawPlayer(RaceSettingData.getOwner(raceId), returnAmount)
}
sender.sendMessage(Lang.getComponent("paid-bet-creator", locale, retunrPlayer.name, returnAmount))
retunrPlayer.player?.sendMessage(Lang.getComponent("paid-bet-player", locale, raceId, retunrPlayer.name, jockey.name, returnAmount))
}
BetList.deleteWhere { BetList.raceId eq raceId }
}
}

suspend fun getOdds(raceId: String, jockey: OfflinePlayer): Double {
val sum = getBetSum(raceId)
val jockeySum = if (getJockeyBetSum(raceId, jockey) == 0) 0.0001 else getJockeyBetSum(raceId, jockey).toDouble()
val rate = BetSettingData.getReturnPercent(raceId).toDouble() / 100
return ((sum * rate) / jockeySum).toBigDecimal().setScale(2, RoundingMode.DOWN).toDouble()
}

}
5 changes: 4 additions & 1 deletion src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package dev.nikomaru.raceassist.bet

import com.github.stefvanschie.inventoryframework.gui.GuiItem
import dev.nikomaru.raceassist.data.files.BetSettingData
import dev.nikomaru.raceassist.utils.Lang
import net.kyori.adventure.text.Component
Expand Down Expand Up @@ -90,7 +91,7 @@ object GuiComponent {
fun noBet(locale: Locale): ItemStack {
val noBet = ItemStack(Material.BARRIER)
val noBetMeta: ItemMeta = noBet.itemMeta
noBetMeta.displayName(Lang.getComponent("gui-no-one-betting", locale))
noBetMeta.displayName(Lang.getComponent("no-one-betting", locale))
noBet.itemMeta = noBetMeta
return noBet
}
Expand All @@ -111,5 +112,7 @@ object GuiComponent {
return noHaveMoney
}

val noDataGrass = GuiItem(ItemStack(Material.GRAY_STAINED_GLASS_PANE))

}

Loading

0 comments on commit e6c5c07

Please sign in to comment.