From acd2a76b5d64a207d7c07cb9c7036e7615886fc8 Mon Sep 17 00:00:00 2001 From: Handiwork Date: Mon, 4 Mar 2024 00:38:01 +0800 Subject: [PATCH] refactor: remove some not-null assertion --- .../maa/backend/common/aop/JsonSchemaAop.kt | 6 +++--- .../backend/controller/CopilotController.kt | 4 ++-- .../maa/backend/service/ArkGameDataService.kt | 14 ++++---------- .../maa/backend/service/ArkLevelService.kt | 19 +++++++++---------- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/plus/maa/backend/common/aop/JsonSchemaAop.kt b/src/main/kotlin/plus/maa/backend/common/aop/JsonSchemaAop.kt index 8782a51c..66fce403 100644 --- a/src/main/kotlin/plus/maa/backend/common/aop/JsonSchemaAop.kt +++ b/src/main/kotlin/plus/maa/backend/common/aop/JsonSchemaAop.kt @@ -21,7 +21,7 @@ import plus.maa.backend.controller.request.copilot.CopilotRatingReq import plus.maa.backend.controller.response.MaaResultException import java.io.IOException -private val log = KotlinLogging.logger { } +private val log = KotlinLogging.logger { } /** * @author LoMu @@ -61,12 +61,12 @@ class JsonSchemaAop( } } } - if (content == null) return + if (schemaJson == null || content == null) return //获取json schema json路径并验证 try { - ClassPathResource(schemaJson!!).inputStream.use { inputStream -> + ClassPathResource(schemaJson).inputStream.use { inputStream -> val json = JSONObject(content) val jsonObject = JSONObject(JSONTokener(inputStream)) val schema = SchemaLoader.load(jsonObject) diff --git a/src/main/kotlin/plus/maa/backend/controller/CopilotController.kt b/src/main/kotlin/plus/maa/backend/controller/CopilotController.kt index 6e0b88e4..b56c5322 100644 --- a/src/main/kotlin/plus/maa/backend/controller/CopilotController.kt +++ b/src/main/kotlin/plus/maa/backend/controller/CopilotController.kt @@ -51,8 +51,8 @@ class CopilotController( @ApiResponse(description = "删除作业结果") @RequireJwt @PostMapping("/delete") - fun deleteCopilot(@RequestBody request: CopilotCUDRequest?): MaaResult { - copilotService.delete(helper.requireUserId(), request!!) + fun deleteCopilot(@RequestBody request: CopilotCUDRequest): MaaResult { + copilotService.delete(helper.requireUserId(), request) return success() } diff --git a/src/main/kotlin/plus/maa/backend/service/ArkGameDataService.kt b/src/main/kotlin/plus/maa/backend/service/ArkGameDataService.kt index 2130d676..1de9285b 100644 --- a/src/main/kotlin/plus/maa/backend/service/ArkGameDataService.kt +++ b/src/main/kotlin/plus/maa/backend/service/ArkGameDataService.kt @@ -198,17 +198,11 @@ class ArkGameDataService(private val okHttpClient: OkHttpClient) { } val node = mapper.reader().readTree(body.string()) val characters = mapper.convertValue(node, object : TypeReference>() {}) - characters.forEach { (id, c) -> c.id = id } arkCharacterMap.clear() - characters.values.forEach { c -> - if (c.id.isNullOrBlank()) { - return@forEach - } - val ids = c.id!!.split("_") - if (ids.size != 3) { - // 不是干员 - return@forEach - } + characters.forEach { (id, c) -> + val ids = id.split("_") + if (ids.size != 3) return@forEach + c.id = id arkCharacterMap[ids[2]] = c } log.info { "[DATA]获取character数据成功, 共${arkCharacterMap.size}条" } diff --git a/src/main/kotlin/plus/maa/backend/service/ArkLevelService.kt b/src/main/kotlin/plus/maa/backend/service/ArkLevelService.kt index fbec4887..48294434 100644 --- a/src/main/kotlin/plus/maa/backend/service/ArkLevelService.kt +++ b/src/main/kotlin/plus/maa/backend/service/ArkLevelService.kt @@ -32,7 +32,7 @@ import java.time.ZoneId import java.util.* import java.util.concurrent.atomic.AtomicInteger -private val log = KotlinLogging.logger { } +private val log = KotlinLogging.logger { } /** * @author dragove @@ -164,7 +164,8 @@ class ArkLevelService( */ fun updateActivitiesOpenStatus() { log.info { "[ACTIVITIES-OPEN-STATUS]准备更新活动地图开放状态" } - val stages = githubRepo.getContents(githubToken, "resource").firstOrNull { content: GithubContent -> content.isFile && "stages.json" == content.name } + val stages = githubRepo.getContents(githubToken, "resource") + .firstOrNull { content: GithubContent -> content.isFile && "stages.json" == content.name } if (stages == null) { log.info { "[ACTIVITIES-OPEN-STATUS]活动地图开放状态数据不存在" } @@ -186,14 +187,12 @@ class ArkLevelService( okHttpClient .newCall(Request.Builder().url(stages.downloadUrl).build()) .execute().use { response -> - if (!response.isSuccessful || response.body == null) { + val body = response.body?.byteStream() + if (!response.isSuccessful || body == null) { log.error { "[ACTIVITIES-OPEN-STATUS]活动地图开放状态下载失败" } return } - val body = response.body!!.byteStream() - val stagesList: List = - mapper.readValue(body, object : TypeReference>() { - }) + val stagesList = mapper.readValue(body, object : TypeReference>() {}) val keyInfos = stagesList .map { it.stageId } // 提取地图系列的唯一标识 @@ -210,7 +209,7 @@ class ArkLevelService( val nowTime = LocalDateTime.now() while (arkLevelPage.hasContent()) { - arkLevelPage.forEach{ arkLevel: ArkLevel -> + arkLevelPage.forEach { arkLevel: ArkLevel -> // 只考虑地图系列的唯一标识 if (keyInfos.contains(ArkLevelUtil.getKeyInfoById(arkLevel.stageId))) { arkLevel.isOpen = true @@ -335,7 +334,7 @@ class ArkLevelService( private val fail: AtomicInteger = AtomicInteger(0), private val pass: AtomicInteger = AtomicInteger(0), val total: Int = 0, - private val finishCallback: ((DownloadTask) -> Unit)? = null + private val finishCallback: ((DownloadTask) -> Unit) ) { fun success() { success.incrementAndGet() @@ -365,7 +364,7 @@ class ArkLevelService( if (success.get() + fail.get() + pass.get() != total) { return } - finishCallback!!.invoke(this) + finishCallback.invoke(this) log.info { "[LEVEL]地图数据下载完成, 成功:${success.get()}, 失败:${fail.get()}, 跳过:${pass.get()} 总用时${duration}s" } } }