From e90123389c5050e29ca2a329396be10b021f833a Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 25 Oct 2025 08:12:22 +0800 Subject: [PATCH] [O] Capsule --- .../java/icu/samnyan/aqua/sega/diva/DivaRepos.kt | 10 ++++++++-- .../diva/handler/card/CardProcedureHandler.kt | 2 +- .../sega/diva/handler/card/ChangeNameHandler.kt | 6 +----- .../sega/diva/handler/card/ChangePasswdHandler.kt | 8 ++------ .../diva/handler/ingame/BuyCstmzItmHandler.kt | 9 ++------- .../sega/diva/handler/ingame/BuyModuleHandler.kt | 13 ++----------- .../sega/diva/handler/ingame/ShopExitHandler.kt | 4 +--- .../diva/handler/ingame/StageResultHandler.kt | 15 ++++++--------- .../sega/diva/handler/ingame/StageStartHandler.kt | 11 ++--------- .../sega/diva/handler/ingame/StoreSsHandler.kt | 13 ++++--------- .../aqua/sega/diva/handler/user/EndHandler.kt | 8 ++------ .../sega/diva/handler/user/PdUnlockHandler.kt | 6 +----- .../sega/diva/handler/user/SpendCreditHandler.kt | 3 +-- .../aqua/sega/diva/handler/user/StartHandler.kt | 8 +------- 14 files changed, 34 insertions(+), 82 deletions(-) diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/DivaRepos.kt b/src/main/java/icu/samnyan/aqua/sega/diva/DivaRepos.kt index ad295e91..096ca78c 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/DivaRepos.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/DivaRepos.kt @@ -1,9 +1,12 @@ package icu.samnyan.aqua.sega.diva +import ext.invoke import icu.samnyan.aqua.sega.diva.model.common.Difficulty import icu.samnyan.aqua.sega.diva.model.common.Edition import icu.samnyan.aqua.sega.diva.model.gamedata.* import icu.samnyan.aqua.sega.diva.model.userdata.* +import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException +import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.query.Param @@ -34,8 +37,11 @@ class DivaRepos( val profile: PlayerProfileRepository, val pvCustomize: PlayerPvCustomizeRepository, val pvRecord: PlayerPvRecordRepository, - val screenShot: PlayerScreenShotRepository -) + val screenShot: PlayerScreenShotRepository, +) { + fun profile(id: Long) = profile.findByPdId(id)() ?: throw ProfileNotFoundException() + fun session(id: Long) = profile(id).let { it to (gameSession.findByPdId(it)() ?: throw SessionNotFoundException()) } +} @Repository interface ContestRepository : JpaRepository { diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/CardProcedureHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/CardProcedureHandler.kt index b8413606..30f8ff54 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/CardProcedureHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/CardProcedureHandler.kt @@ -56,7 +56,7 @@ class CardProcedureHandler(val db: DivaRepos) { profile.vocaloidPoints ) - db.gameSession.save(session) + db.gameSession.save(session) return CardProcedureResponse( request.cmd, request.req_id, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangeNameHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangeNameHandler.kt index 7a7b4d4d..b30e3120 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangeNameHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangeNameHandler.kt @@ -1,9 +1,6 @@ package icu.samnyan.aqua.sega.diva.handler.card -import ext.invoke import icu.samnyan.aqua.sega.diva.DivaRepos -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException -import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import icu.samnyan.aqua.sega.diva.model.common.Result import icu.samnyan.aqua.sega.diva.model.request.card.ChangeNameRequest import icu.samnyan.aqua.sega.diva.model.response.card.ChangeNameResponse @@ -15,8 +12,7 @@ import org.springframework.stereotype.Component @Component class ChangeNameHandler(val db: DivaRepos) { fun handle(request: ChangeNameRequest): Any { - val profile = db.profile.findByPdId(request.pd_id)() ?: throw ProfileNotFoundException() - val session = db.gameSession.findByPdId(profile)() ?: throw SessionNotFoundException() + val (profile, session) = db.session(request.pd_id) profile.playerName = request.player_name db.profile.save(profile) diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangePasswdHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangePasswdHandler.kt index 2db90813..f7ad48fe 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangePasswdHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/card/ChangePasswdHandler.kt @@ -1,12 +1,11 @@ package icu.samnyan.aqua.sega.diva.handler.card import icu.samnyan.aqua.sega.diva.DivaRepos -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException -import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import icu.samnyan.aqua.sega.diva.model.common.PassStat import icu.samnyan.aqua.sega.diva.model.common.Result import icu.samnyan.aqua.sega.diva.model.request.card.ChangePasswdRequest import icu.samnyan.aqua.sega.diva.model.response.card.ChangePasswdResponse +import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component import java.util.function.Supplier @@ -16,10 +15,7 @@ import java.util.function.Supplier @Component class ChangePasswdHandler(val db: DivaRepos) { fun handle(request: ChangePasswdRequest): Any { - val profile = db.profile.findByPdId(request.pd_id) - .orElseThrow(Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (profile, session) = db.session(request.pd_id) profile.passwordStatus = PassStat.SET profile.password = request.new_passwd diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyCstmzItmHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyCstmzItmHandler.kt index b33872fc..50a26ddf 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyCstmzItmHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyCstmzItmHandler.kt @@ -4,7 +4,6 @@ import icu.samnyan.aqua.sega.diva.DivaRepos import icu.samnyan.aqua.sega.diva.model.common.Result import icu.samnyan.aqua.sega.diva.model.request.ingame.BuyCstmzItmRequest import icu.samnyan.aqua.sega.diva.model.response.ingame.BuyCstmzItmResponse -import icu.samnyan.aqua.sega.diva.model.userdata.GameSession import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component @@ -16,11 +15,7 @@ import java.util.function.Supplier @Component class BuyCstmzItmHandler(val db: DivaRepos) { fun handle(request: BuyCstmzItmRequest): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) - - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (profile, session) = db.session(request.pd_id) val customizeOptional = db.g.customize.findById(request.cstmz_itm_id) @@ -42,7 +37,7 @@ class BuyCstmzItmHandler(val db: DivaRepos) { } db.s.customize.buy(profile, request.cstmz_itm_id) session.vp -= customizeOptional.get().price - db.gameSession.save(session) + db.gameSession.save(session) return BuyCstmzItmResponse( request.cmd, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyModuleHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyModuleHandler.kt index 750eadca..5ae46290 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyModuleHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/BuyModuleHandler.kt @@ -4,11 +4,7 @@ import icu.samnyan.aqua.sega.diva.DivaRepos import icu.samnyan.aqua.sega.diva.model.common.Result import icu.samnyan.aqua.sega.diva.model.request.ingame.BuyModuleRequest import icu.samnyan.aqua.sega.diva.model.response.ingame.BuyModuleResponse -import icu.samnyan.aqua.sega.diva.model.userdata.GameSession -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException -import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component -import java.util.function.Supplier /** * @author samnyan (privateamusement@protonmail.com) @@ -16,12 +12,7 @@ import java.util.function.Supplier @Component class BuyModuleHandler(val db: DivaRepos) { fun handle(request: BuyModuleRequest): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) - - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) - + val (profile, session) = db.session(request.pd_id) val moduleOptional = db.g.module.findById(request.mdl_id) if (moduleOptional.isEmpty) { @@ -42,7 +33,7 @@ class BuyModuleHandler(val db: DivaRepos) { } db.s.module.buy(profile, request.mdl_id) session.vp -= moduleOptional.get().price - db.gameSession.save(session) + db.gameSession.save(session) return BuyModuleResponse( request.cmd, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/ShopExitHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/ShopExitHandler.kt index 38480083..1dd22e50 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/ShopExitHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/ShopExitHandler.kt @@ -6,7 +6,6 @@ import icu.samnyan.aqua.sega.diva.model.common.Result import icu.samnyan.aqua.sega.diva.model.request.ingame.ShopExitRequest import icu.samnyan.aqua.sega.diva.model.response.ingame.ShopExitResponse import icu.samnyan.aqua.sega.diva.model.userdata.PlayerPvCustomize -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException import org.springframework.stereotype.Component import java.util.function.Supplier @@ -16,8 +15,7 @@ import java.util.function.Supplier @Component class ShopExitHandler(val db: DivaRepos) { fun handle(request: ShopExitRequest): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) + val profile = db.profile(request.pd_id) val customize = db.pvCustomize.findByPdIdAndPvId(profile, request.ply_pv_id) .orElseGet(Supplier { PlayerPvCustomize(profile, request.ply_pv_id) }) diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageResultHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageResultHandler.kt index 7e6d5bad..cc697375 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageResultHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageResultHandler.kt @@ -33,10 +33,7 @@ class StageResultHandler(val db: DivaRepos, val calc: DivaCalculator) { fun handle(request: StageResultRequest): Any { val response: StageResultResponse? if (request.getPd_id() != -1L) { - val profile = db.profile.findByPdId(request.getPd_id()).orElseThrow( - Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (profile, session) = db.session(request.pd_id) currentProfile = profile // Get the last played index @@ -192,9 +189,9 @@ class StageResultHandler(val db: DivaRepos, val calc: DivaCalculator) { } } - db.pvRecord.save(record) - db.playLog.save(log) - db.gameSession.save(session) + db.pvRecord.save(record) + db.playLog.save(log) + db.gameSession.save(session) return StageResultResponse( @@ -374,7 +371,7 @@ class StageResultHandler(val db: DivaRepos, val calc: DivaCalculator) { result["string1"] = "***" result["string2"] = "***" } else { - db.inventory.save( + db.inventory.save( PlayerInventory( null, currentProfile, @@ -398,7 +395,7 @@ class StageResultHandler(val db: DivaRepos, val calc: DivaCalculator) { result.put("string1", "***") result.put("string2", "***") } else { - db.inventory.save( + db.inventory.save( PlayerInventory( null, currentProfile, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageStartHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageStartHandler.kt index 8dbf7fd2..3e1f89ba 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageStartHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StageStartHandler.kt @@ -3,11 +3,7 @@ package icu.samnyan.aqua.sega.diva.handler.ingame import icu.samnyan.aqua.sega.diva.DivaRepos import icu.samnyan.aqua.sega.diva.model.request.ingame.StageStartRequest import icu.samnyan.aqua.sega.diva.model.response.BaseResponse -import icu.samnyan.aqua.sega.diva.model.userdata.GameSession -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException -import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component -import java.util.function.Supplier /** * @author samnyan (privateamusement@protonmail.com) @@ -16,10 +12,7 @@ import java.util.function.Supplier class StageStartHandler(val db: DivaRepos) { fun handle(request: StageStartRequest): Any { if (request.getPd_id() != -1L) { - val profile = db.profile.findByPdId(request.getPd_id()).orElseThrow( - Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (_, session) = db.session(request.pd_id) val stageArr = request.getStg_ply_pv_id() var stageIndex = 0 @@ -36,7 +29,7 @@ class StageStartHandler(val db: DivaRepos) { stageIndex = 3 } session.stageIndex = stageIndex - db.gameSession.save(session) + db.gameSession.save(session) } return BaseResponse( diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StoreSsHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StoreSsHandler.kt index 07788753..0337fb36 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StoreSsHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/ingame/StoreSsHandler.kt @@ -6,7 +6,6 @@ import icu.samnyan.aqua.sega.diva.DivaRepos import icu.samnyan.aqua.sega.diva.model.request.ingame.StoreSsRequest import icu.samnyan.aqua.sega.diva.model.response.BaseResponse import icu.samnyan.aqua.sega.diva.model.userdata.PlayerScreenShot -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException import org.springframework.stereotype.Component import org.springframework.web.multipart.MultipartFile import java.io.IOException @@ -14,7 +13,6 @@ import java.nio.file.Files import java.nio.file.Paths import java.time.LocalDateTime import java.time.ZoneOffset -import java.util.function.Supplier /** * @author samnyan (privateamusement@protonmail.com) @@ -23,14 +21,11 @@ import java.util.function.Supplier class StoreSsHandler(val db: DivaRepos) { val logger = logger() fun handle(request: StoreSsRequest, file: MultipartFile): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) + val profile = db.profile(request.pd_id) - var response: BaseResponse? try { - val filename = - request.pd_id.toString() + "-" + LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) + ".jpg" - Files.write(Paths.get("data/" + filename), file.bytes) + val filename = "${request.pd_id}-${LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)}.jpg" + Files.write(Paths.get("data/$filename"), file.bytes) val ss = PlayerScreenShot( profile, @@ -39,7 +34,7 @@ class StoreSsHandler(val db: DivaRepos) { request.ss_mdl_id.csv, request.ss_c_itm_id.csv ) - db.screenShot.save(ss) + db.screenShot.save(ss) return BaseResponse( request.cmd, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/EndHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/EndHandler.kt index e22458e8..072eb3e2 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/EndHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/EndHandler.kt @@ -26,11 +26,7 @@ import kotlin.math.max @Component class EndHandler(val db: DivaRepos) { fun handle(request: StageResultRequest): Any { - val profile = db.profile.findByPdId(request.getPd_id()).orElseThrow( - Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) - + val (profile, session) = db.session(request.pd_id) profile.headphoneVolume = request.getHp_vol() profile.isButtonSeOn = request.isBtn_se_vol @@ -65,7 +61,7 @@ class EndHandler(val db: DivaRepos) { ) currentResultRank else contestRecord.resultRank contestRecord.lastUpdateTime = LocalDateTime.now() - db.contest.save(contestRecord) + db.contest.save(contestRecord) profile.isContestNowPlayingEnable = false profile.contestNowPlayingId = -1 profile.contestNowPlayingResultRank = ContestBorder.NONE diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/PdUnlockHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/PdUnlockHandler.kt index 89151451..c2c37975 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/PdUnlockHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/PdUnlockHandler.kt @@ -3,7 +3,6 @@ package icu.samnyan.aqua.sega.diva.handler.user import icu.samnyan.aqua.sega.diva.DivaRepos import icu.samnyan.aqua.sega.diva.model.request.user.PdUnlockRequest import icu.samnyan.aqua.sega.diva.model.response.BaseResponse -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component import java.util.function.Supplier @@ -11,10 +10,7 @@ import java.util.function.Supplier @Component class PdUnlockHandler(val db: DivaRepos) { fun handle(request: PdUnlockRequest): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (profile, session) = db.session(request.pd_id) db.gameSession.delete(session) diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/SpendCreditHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/SpendCreditHandler.kt index ae1d3a63..40eeabf5 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/SpendCreditHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/SpendCreditHandler.kt @@ -14,8 +14,7 @@ import java.util.function.Supplier @Component class SpendCreditHandler(val db: DivaRepos) { fun handle(request: SpendCreditRequest): Any { - val profile = db.profile.findByPdId(request.pd_id).orElseThrow( - Supplier { ProfileNotFoundException() }) + val profile = db.profile(request.pd_id) return SpendCreditResponse( request.cmd, diff --git a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/StartHandler.kt b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/StartHandler.kt index f78e56af..3ad2f7a6 100644 --- a/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/StartHandler.kt +++ b/src/main/java/icu/samnyan/aqua/sega/diva/handler/user/StartHandler.kt @@ -9,14 +9,11 @@ import icu.samnyan.aqua.sega.diva.model.userdata.GameSession import icu.samnyan.aqua.sega.diva.model.userdata.PlayerContest import icu.samnyan.aqua.sega.diva.model.userdata.PlayerProfile import icu.samnyan.aqua.sega.diva.model.userdata.PlayerPvRecord -import icu.samnyan.aqua.sega.diva.util.ProfileNotFoundException import icu.samnyan.aqua.sega.diva.util.PvRecordDataException -import icu.samnyan.aqua.sega.diva.util.SessionNotFoundException import org.springframework.stereotype.Component import java.time.LocalDateTime import java.util.* import java.util.function.Consumer -import java.util.function.Supplier import java.util.stream.Collectors /** @@ -25,10 +22,7 @@ import java.util.stream.Collectors @Component class StartHandler(val db: DivaRepos) { fun handle(request: StartRequest): Any { - val profile = db.profile.findByPdId(request.getPd_id()).orElseThrow( - Supplier { ProfileNotFoundException() }) - val session = db.gameSession.findByPdId(profile) - .orElseThrow(Supplier { SessionNotFoundException() }) + val (profile, session) = db.session(request.pd_id) session.startMode = StartMode.START db.gameSession.save(session)