Skip to content

Commit

Permalink
Added the ability to create the necessary Sheets when the Sheets id i…
Browse files Browse the repository at this point in the history
…s set.
  • Loading branch information
Nlkomaru committed Feb 5, 2022
1 parent 3cdcf8d commit 27557f6
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ package dev.nikomaru.raceassist.bet.commands
import cloud.commandframework.annotations.Argument
import cloud.commandframework.annotations.CommandMethod
import com.github.shynixn.mccoroutine.launch
import com.google.api.services.sheets.v4.model.AddSheetRequest
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest
import com.google.api.services.sheets.v4.model.Request
import com.google.api.services.sheets.v4.model.SheetProperties
import dev.nikomaru.raceassist.RaceAssist
import dev.nikomaru.raceassist.api.sheet.SheetsServiceUtil.getSheetsService
import dev.nikomaru.raceassist.database.BetSetting
import dev.nikomaru.raceassist.utils.Lang
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -49,9 +54,36 @@ class BetSheetCommand {
it[spreadsheetId] = sheetId
}
}
createNewSheets(sheetId, raceID)
}
}

private suspend fun createNewSheets(sheetId: String, raceID: String) = withContext(Dispatchers.IO) {
val sheetsService = getSheetsService(sheetId)
val content = BatchUpdateSpreadsheetRequest()
val requests: ArrayList<Request> = ArrayList()
val request1 = Request()
val request2 = Request()
val addSheet1 = AddSheetRequest()
val addSheet2 = AddSheetRequest()
val properties1 = SheetProperties()
val properties2 = SheetProperties()
//賭けを表示するためのシート
properties1.title = "${raceID}_RaceAssist_Bet"
//将来の結果のため
properties2.title = "${raceID}_RaceAssist_Result"
addSheet1.properties = properties1
addSheet2.properties = properties2
request1.addSheet = addSheet1
request2.addSheet = addSheet2
requests.add(request1)
requests.add(request2)
content.requests = requests
sheetsService?.spreadsheets()
?.batchUpdate(sheetId, content)
?.execute()
}

private suspend fun getRaceCreator(raceID: String) =
newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class BetGuiClickEvent : Listener {
var i = 1
val data: ArrayList<ValueRange> = ArrayList()
data.add(
ValueRange().setRange("${raceID}_RaceAssist!A${i}").setValues(
ValueRange().setRange("${raceID}_RaceAssist_Bet!A${i}").setValues(
listOf(
listOf(
Lang.getText("sheet-timestamp", Locale.getDefault()),
Expand All @@ -403,7 +403,7 @@ class BetGuiClickEvent : Listener {
val betting = it[BetList.betting]
val timeStamp = it[BetList.timeStamp]
data.add(
ValueRange().setRange("${raceID}_RaceAssist!A${i + 1}").setValues(
ValueRange().setRange("${raceID}_RaceAssist_Bet!A${i + 1}").setValues(
listOf(
listOf(
timeStamp.toString(), player, jockey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ class RaceDebugCommand {

val score = objective.getScore(Lang.getText("first-ranking", player.locale()) + " " + "§b${player.name}")
score.score = 6
val data1 = objective.getScore("relativeNowX = $relativeNowX relativeNowY = $relativeNowY")
val data1 = objective.getScore("relativeNowX = $relativeNowX m relativeNowY = $relativeNowY m")
data1.score = 5
val data2 = objective.getScore("passBorders = $passBorders currentLap = $currentLap")
val data2 = objective.getScore("passBorders = $passBorders times currentLap = $currentLap times")
data2.score = 4
val data3 = objective.getScore("totalDegree = $totalDegree° currentDegree = $currentDegree°")
data3.score = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RaceDeleteCommand {
if (spreadsheetId != null) {
val sheetsService = SheetsServiceUtil.getSheetsService(spreadsheetId)

val range = "${raceID}_RaceAssist!A1:E"
val range = "${raceID}_RaceAssist_Bet!A1:E"
val requestBody = ClearValuesRequest()
val request = sheetsService!!.spreadsheets().values().clear("RaceAssist", range, requestBody)
request.execute()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Lang {

suspend fun load() {
withContext(Dispatchers.IO) {
val lang = listOf("en_US", "ja_JP", "zh_CN")
val lang = listOf("ja_JP")
val pluginDir = File(plugin.dataFolder, "lang")
if (!pluginDir.exists()) {
pluginDir.mkdir()
Expand All @@ -56,8 +56,8 @@ object Lang {
}

fun getText(key: String, locale: Locale): String {
val lang = langList[locale.toString()] ?: langList["ja_JP"]!!
return lang.getProperty(key) ?: "Please tell your server administrator that you got this message.(RaceAssist is broken)"
val lang = langList[locale.toString()] ?: langList["ja_JP"]
return lang?.getProperty(key) ?: "Please tell your server administrator that you got this message.(RaceAssist is broken)"
}

}
54 changes: 35 additions & 19 deletions src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package dev.nikomaru.raceassist.utils

import com.github.shynixn.mccoroutine.launch
import dev.nikomaru.raceassist.RaceAssist.Companion.plugin
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.title.Title
import net.kyori.adventure.title.Title.title
Expand All @@ -29,32 +33,44 @@ class RaceAudience {
private val audience: HashSet<UUID> = HashSet()

fun showTitleI18n(key: String, vararg args: String) {
audience.forEach {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.showTitle(title(text(MessageFormat.format(Lang.getText(key, player.locale()), *args)), text(" ")))
}
plugin.launch {
audience.map {
async {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.showTitle(title(text(MessageFormat.format(Lang.getText(key, player.locale()), *args)), text(" ")))
}
}
}.awaitAll()
}
}

fun showTitle(title: Title) {
audience.forEach {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.showTitle(title)
}
plugin.launch {
audience.map {
async {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.showTitle(title)
}
}
}.awaitAll()
}
}

fun sendMessageI18n(key: String, vararg args: Any) {
audience.forEach {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.sendMessage(text(MessageFormat.format(Lang.getText(key, player.locale()), *args)))
}
plugin.launch {
audience.map {
async {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
if (offlinePlayer.isOnline) {
val player = offlinePlayer.player!!
player.sendMessage(text(MessageFormat.format(Lang.getText(key, player.locale()), *args)))
}
}
}.awaitAll()
}
}

Expand All @@ -69,7 +85,7 @@ class RaceAudience {
}
}

fun getUUID(): Set<UUID> {
fun getUUID(): Collection<UUID> {
return audience
}

Expand Down
126 changes: 0 additions & 126 deletions src/main/resources/lang/en_US.properties

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/lang/ja_JP.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

finish-race=レースが終了しました
goal-degree-must-multiple-90=ゴール角度は90の倍数である必要があります
no-economy-plugin-found-disabling-vault=エコノミープラグインが見つかりません。Vaultを無効にしています
no-exist-central-point=中心点が存在しません
no-exist-goal-degree=ゴール角度が存在しません
no-exist-race=レースが存在しません
Expand Down
Loading

0 comments on commit 27557f6

Please sign in to comment.