Skip to content

Commit

Permalink
Merge pull request #12 from cssxsh/dev
Browse files Browse the repository at this point in the history
version: 1.2.1-dev-5
  • Loading branch information
cssxsh authored Jun 21, 2021
2 parents 7dbb500 + 912e92b commit b08c5f3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "xyz.cssxsh.mirai.plugin"
version = "1.2.1-dev-4"
version = "1.2.1-dev-5"

repositories {
mavenLocal()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/xyz/cssxsh/arknights/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ suspend fun <T : GameDataType> Iterable<T>.load(dir: File, flush: Boolean): List
dir.resolve(type.path).also { file ->
if (flush || file.exists().not()) {
file.parentFile.mkdirs()
file.writeText(client.get(type.url))
file.writeBytes(client.get<ByteArray>(type.url).apply { check(isNotEmpty()) })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/xyz/cssxsh/arknights/announce/Announce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data class AnnouncementMeta(
@SerialName("extra")
val extra: AnnouncementExtra,
@SerialName("focusAnnounceId")
val focusId: Int
val focusId: Int? = null
)

@Serializable
Expand Down
45 changes: 44 additions & 1 deletion src/main/kotlin/xyz/cssxsh/arknights/excel/Uitls.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.cssxsh.arknights.excel

import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -195,6 +196,7 @@ class ExcelData(override val dir: File): GameDataDownloader {
private val zone by lazy { dir.readZoneTable() }
val zones by lazy { ZoneMap(zone) }
val weeks by lazy { WeeklyMap(zone) }
val version by lazy { dir.readExcelDataVersion() }

override val types get() = ExcelDataType.values().asIterable()
}
Expand All @@ -209,7 +211,8 @@ enum class ExcelDataType(file: String) : GameDataType {
SKILL("skill_table.json"),
STORY("story_review_table.json"),
TEAM("handbook_team_table.json"),
ZONE("zone_table.json");
ZONE("zone_table.json"),
VERSION("data_version.txt");

override val path = "excel/${file}"

Expand All @@ -218,6 +221,46 @@ enum class ExcelDataType(file: String) : GameDataType {

private fun path(type: GameDataType): String = "${SERVER.locale}/gamedata/${type.path}"

data class ExcelDataVersion(
val stream: String,
val change: String,
val versionControl: String
)

internal fun File.readExcelDataVersion(): ExcelDataVersion {
return resolve(ExcelDataType.VERSION.path).readText().readExcelDataVersion()
}

internal suspend fun loadExcelDataVersion(): ExcelDataVersion {
return useHttpClient<String> { it.get(jsdelivr(ExcelDataType.VERSION)) }.readExcelDataVersion()
}

internal fun String.readExcelDataVersion(): ExcelDataVersion {
lateinit var stream: String
lateinit var change: String
lateinit var versionControl: String
lines().filter(String::isNotBlank).forEach {
val (name, value) = it.split(":")
when (name) {
"Stream" -> {
stream = value
}
"Change" -> {
change = value
}
"VersionControl" -> {
versionControl = value
}
else -> {}
}
}
return ExcelDataVersion(
stream = stream,
change = change,
versionControl = versionControl
)
}

private val github = { type: ExcelDataType -> Url("https://raw.githubusercontent.com/$GITHUB_REPO/master/${path(type)}") }

private val jsdelivr = { type: ExcelDataType -> Url("https://cdn.jsdelivr.net/gh/$GITHUB_REPO@master/${path(type)}") }
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object ArknightsHelperPlugin : KotlinPlugin(
) {

override fun onEnable() {
downloadExternalData()
downloadGameData()
ArknightsUserData.reload()
ArknightsPoolData.reload()
ArknightsMineData.reload()
Expand Down
16 changes: 13 additions & 3 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/ArknightsSubscriber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.http.*
import kotlinx.coroutines.*
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
import net.mamoe.mirai.console.util.SemVersion
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.events.BotJoinGroupEvent
import net.mamoe.mirai.event.events.FriendAddEvent
Expand All @@ -14,6 +15,7 @@ import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import net.mamoe.mirai.utils.*
import xyz.cssxsh.arknights.announce.*
import xyz.cssxsh.arknights.bilibili.*
import xyz.cssxsh.arknights.excel.loadExcelDataVersion
import xyz.cssxsh.arknights.useHttpClient
import xyz.cssxsh.arknights.weibo.*
import java.time.Duration
Expand Down Expand Up @@ -164,17 +166,25 @@ private suspend fun sendRecruitClock(id: Long, site: Int) {
}
}

internal fun downloadExternalData(): Unit = runBlocking {
internal fun downloadGameData(): Unit = runBlocking {
runCatching {
ExcelData.download(flush = false)
val old = SemVersion.invoke(ExcelData.version.versionControl)
val now = SemVersion.invoke(loadExcelDataVersion().versionControl)
if (now > old) {
ExcelData.download(flush = true)
now
} else {
old
}
}.onSuccess {
logger.info { "ExcelData 数据加载完毕" }
logger.info { "ExcelData 数据加载完毕, 版本 $it" }
}.onFailure {
logger.warning({ "ExcelData 数据加载失败" }, it)
}

runCatching {
PenguinData.download(flush = false)
PenguinData.download(flush = true)
}.onSuccess {
logger.info { "PenguinData 数据加载完毕" }
}.onFailure {
Expand Down

0 comments on commit b08c5f3

Please sign in to comment.