diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt new file mode 100644 index 00000000..c776b04a --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanServletController.kt @@ -0,0 +1,117 @@ +package icu.samnyan.aqua.sega.chusan + +import ext.* +import icu.samnyan.aqua.sega.chunithm.handler.impl.GetGameIdlistHandler +import icu.samnyan.aqua.sega.chusan.handler.* +import icu.samnyan.aqua.sega.general.BaseHandler +import org.slf4j.LoggerFactory +import org.springframework.web.bind.annotation.* +import kotlin.reflect.full.declaredMemberProperties + + +/** + * @author samnyan (privateamusement@protonmail.com) + */ +@Suppress("unused") +@RestController +@API(value = ["/g/chu3/{version}/ChuniServlet", "/g/chu3/{version}"]) +class ChusanServletController( + val gameLogin: GameLoginHandler, + val gameLogout: GameLogoutHandler, + val getGameCharge: GetGameChargeHandler, + val getGameEvent: GetGameEventHandler, + val getGameIdlist: GetGameIdlistHandler, + val getGameRanking: GetGameRankingHandler, + val getGameSetting: GetGameSettingHandler, + val getTeamCourseRule: GetTeamCourseRuleHandler, + val getTeamCourseSetting: GetTeamCourseSettingHandler, + val getUserActivity: GetUserActivityHandler, + val getUserCharacter: GetUserCharacterHandler, + val getUserCharge: GetUserChargeHandler, + val getUserCourse: GetUserCourseHandler, + val getUserData: GetUserDataHandler, + val getUserDuel: GetUserDuelHandler, + val getUserFavoriteItem: GetUserFavoriteItemHandler, + val getUserItem: GetUserItemHandler, + val getUserLoginBonus: GetUserLoginBonusHandler, + val getUserMapArea: GetUserMapAreaHandler, + val getUserMusic: GetUserMusicHandler, + val getUserOption: GetUserOptionHandler, + val getUserPreview: GetUserPreviewHandler, + val getUserRecentRating: GetUserRecentRatingHandler, + val getUserRegion: GetUserRegionHandler, + val getUserRivalData: GetUserRivalDataHandler, + val getUserRivalMusic: GetUserRivalMusicHandler, + val getUserSymbolChatSetting: GetUserSymbolChatSettingHandler, + val getUserNetBattleData: GetUserNetBattleDataHandler, + val getUserTeam: GetUserTeamHandler, + val upsertUserAll: UpsertUserAllHandler, + val upsertUserChargelog: UpsertUserChargelogHandler, + val getGameGacha: GetGameGachaHandler, + val getGameGachaCardById: GetGameGachaCardByIdHandler, + val getUserCardPrintError: GetUserCardPrintErrorHandler, + val cmGetUserPreview: CMGetUserPreviewHandler, + val cmGetUserData: CMGetUserDataHandler, + val cmGetUserCharacter: CMGetUserCharacterHandler, + val getUserGacha: GetUserGachaHandler, + val getUserPrintedCard: GetUserPrintedCardHandler, + val cmGetUserItem: CMGetUserItemHandler, + val rollGacha: RollGachaHandler, + val cmUpsertUserGacha: CMUpsertUserGachaHandler, + val cmUpsertUserPrintSubtract: CMUpsertUserPrintSubtractHandler, + val cmUpsertUserPrintCancel: CMUpsertUserPrintCancelHandler, + val beginMatching: BeginMatchingHandler, + val endMatching: EndMatchingHandler, + val getMatchingState: GetMatchingStateHandler, + val removeMatchingMember: RemoveMatchingMemberHandler, +) { + val logger = LoggerFactory.getLogger(ChusanServletController::class.java) + + val cmUpsertUserPrint = BaseHandler { """{"returnCode":1,"orderId":"0","serialId":"FAKECARDIMAG12345678","apiName":"CMUpsertUserPrintApi"}""" } + val cmUpsertUserPrintlog = BaseHandler { """{"returnCode":1,"orderId":"0","serialId":"FAKECARDIMAG12345678","apiName":"CMUpsertUserPrintlogApi"}""" } + + val endpointList = mutableListOf( + "GameLoginApi", "GameLogoutApi", "GetGameChargeApi", "GetGameEventApi", "GetGameIdlistApi", + "GetGameRankingApi", "GetGameSettingApi", "GetTeamCourseRuleApi", "GetTeamCourseSettingApi", "GetUserActivityApi", + "GetUserCharacterApi", "GetUserChargeApi", "GetUserCourseApi", "GetUserDataApi", "GetUserDuelApi", + "GetUserFavoriteItemApi", "GetUserItemApi", "GetUserLoginBonusApi", "GetUserMapAreaApi", "GetUserMusicApi", + "GetUserOptionApi", "GetUserPreviewApi", "GetUserRecentRatingApi", "GetUserRegionApi", "GetUserRivalDataApi", + "GetUserRivalMusicApi", "GetUserTeamApi", "GetUserSymbolChatSettingApi", "GetUserNetBattleDataApi", + "UpsertClientBookkeepingApi", "UpsertClientDevelopApi", "UpsertClientErrorApi", "UpsertClientSettingApi", + "UpsertClientTestmodeApi", "UpsertUserAllApi", "UpsertUserChargelogApi", "CreateTokenApi", "RemoveTokenApi", + "UpsertClientUploadApi", "MatchingServer/Ping", "MatchingServer/BeginMatchingApi", "MatchingServer/EndMatchingApi", + "MatchingServer/RemoveMatchingMemberApi", "MatchingServer/GetMatchingStateApi", "GetGameGachaApi", + "GetGameGachaCardByIdApi", "GetUserCardPrintErrorApi", "CMGetUserCharacterApi", "CMGetUserDataApi", + "GetUserGachaApi", "CMGetUserItemApi", "CMGetUserPreviewApi", "GetUserPrintedCardApi", "PrinterLoginApi", + "PrinterLogoutApi", "RollGachaApi", "CMUpsertUserGachaApi", "CMUpsertUserPrintApi", "CMUpsertUserPrintCancelApi", + "CMUpsertUserPrintlogApi", "CMUpsertUserPrintSubtractApi") + + val noopEndpoint = endpointList.popAll("UpsertClientBookkeepingApi", "UpsertClientDevelopApi", "UpsertClientErrorApi", + "UpsertClientSettingApi", "UpsertClientTestmodeApi", "CreateTokenApi", "RemoveTokenApi", "UpsertClientUploadApi", + "MatchingServer/Ping", "PrinterLoginApi", "PrinterLogoutApi") + + val members = this::class.declaredMemberProperties + val handlers: Map = endpointList.associateWith { api -> + val name = api.replace("Api", "").lowercase() + (members.find { it.name.lowercase() == name } ?: members.find { it.name.lowercase() == name.replace("cm", "") }) + ?.let { it.call(this) as BaseHandler } + ?: throw IllegalArgumentException("Chu3: No handler found for $api") + } + + @API("/{endpoint}") + fun handle(@PV endpoint: Str, @RB request: MutableMap, @PV version: Str): Any { + var api = endpoint + request["version"] = version + + logger.info("Chu3 $api : $request") + + if (api in noopEndpoint) { + return """{"returnCode":"1"}""" + } + + return handlers[api]?.handle(request) ?: { + logger.warn("Chu3 $api not found") + """{"returnCode":"1","apiName":"$api"}""" + } + } +} diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletController.java b/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletController.java deleted file mode 100644 index f066ed9e..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletController.java +++ /dev/null @@ -1,380 +0,0 @@ -package icu.samnyan.aqua.sega.chusan.controller; - - -import com.fasterxml.jackson.core.JsonProcessingException; -import icu.samnyan.aqua.sega.chusan.handler.impl.*; -import lombok.AllArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import java.util.Map; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -@RestController -@RequestMapping({"/g/chu3/{version}/ChuniServlet", "/g/chu3/{version}"}) -@AllArgsConstructor -public class ChusanServletController { - - private final GameLoginHandler gameLoginHandler; - private final GameLogoutHandler gameLogoutHandler; - private final GetGameChargeHandler getGameChargeHandler; - private final GetGameEventHandler getGameEventHandler; - private final GetGameIdlistHandler getGameIdlistHandler; - private final GetGameRankingHandler getGameRankingHandler; - private final GetGameSettingHandler getGameSettingHandler; - private final GetTeamCourseRuleHandler getTeamCourseRuleHandler; - private final GetTeamCourseSettingHandler getTeamCourseSettingHandler; - private final GetUserActivityHandler getUserActivityHandler; - private final GetUserCharacterHandler getUserCharacterHandler; - private final GetUserChargeHandler getUserChargeHandler; - private final GetUserCourseHandler getUserCourseHandler; - private final GetUserDataHandler getUserDataHandler; - private final GetUserDuelHandler getUserDuelHandler; - private final GetUserFavoriteItemHandler getUserFavoriteItemHandler; - private final GetUserItemHandler getUserItemHandler; - private final GetUserLoginBonusHandler getUserLoginBonusHandler; - private final GetUserMapAreaHandler getUserMapAreaHandler; - private final GetUserMusicHandler getUserMusicHandler; - private final GetUserOptionHandler getUserOptionHandler; - private final GetUserPreviewHandler getUserPreviewHandler; - private final GetUserRecentRatingHandler getUserRecentRatingHandler; - private final GetUserRegionHandler getUserRegionHandler; - private final GetUserRivalDataHandler getUserRivalDataHandler; - private final GetUserRivalMusicHandler getUserRivalMusicHandler; - private final GetUserSymbolChatSettingHandler getUserSymbolChatSettingHandler; - private final GetUserNetBattleDataHandler getUserNetBattleDataHandler; - private final GetUserTeamHandler getUserTeamHandler; - private final UpsertUserAllHandler upsertUserAllHandler; - private final UpsertUserChargelogHandler upsertUserChargelogHandler; - private final GetGameGachaHandler getGameGachaHandler; - private final GetGameGachaCardByIdHandler getGameGachaCardByIdHandler; - private final GetUserCardPrintErrorHandler getUserCardPrintErrorHandler; - private final CMGetUserPreviewHandler cmGetUserPreviewHandler; - private final CMGetUserDataHandler cmGetUserDataHandler; - private final CMGetUserCharacterHandler cmGetUserCharacterHandler; - private final GetUserGachaHandler getUserGachaHandler; - private final GetUserPrintedCardHandler getUserPrintedCardHandler; - private final CMGetUserItemHandler cmGetUserItemHandler; - private final RollGachaHandler rollGachaHandler; - private final CMUpsertUserGachaHandler cmUpsertUserGachaHandler; - private final CMUpsertUserPrintSubtractHandler cmUpsertUserPrintSubtractHandler; - private final CMUpsertUserPrintCancelHandler cmUpsertUserPrintCancelHandler; - private final BeginMatchingHandler beginMatchingHandler; - private final EndMatchingHandler endMatchingHandler; - private final GetMatchingStateHandler getMatchingStateHandler; - private final RemoveMatchingMemberHandler removeMatchingMemberHandler; - - @PostMapping("GameLoginApi") - String gameLogin(@ModelAttribute Map request) throws JsonProcessingException { - return gameLoginHandler.handle(request); - } - - @PostMapping("GameLogoutApi") - String gameLogout(@ModelAttribute Map request) throws JsonProcessingException { - return gameLogoutHandler.handle(request); - } - - @PostMapping("GetGameChargeApi") - String getGameCharge(@ModelAttribute Map request) throws JsonProcessingException { - return getGameChargeHandler.handle(request); - } - - @PostMapping("GetGameEventApi") - String getGameEvent(@ModelAttribute Map request) throws JsonProcessingException { - return getGameEventHandler.handle(request); - } - - @PostMapping("GetGameIdlistApi") - String getGameIdList(@ModelAttribute Map request) throws JsonProcessingException { - return getGameIdlistHandler.handle(request); - } - - @PostMapping("GetGameRankingApi") - String getGameRanking(@ModelAttribute Map request) throws JsonProcessingException { - return getGameRankingHandler.handle(request); - } - - @PostMapping("GetGameSettingApi") - String getGameSetting(@ModelAttribute Map request) throws JsonProcessingException { - return getGameSettingHandler.handle(request); - } - - @PostMapping("GetTeamCourseRuleApi") - String getTeamCourseRule(@ModelAttribute Map request) throws JsonProcessingException { - return getTeamCourseRuleHandler.handle(request); - } - - @PostMapping("GetTeamCourseSettingApi") - String getTeamCourseSetting(@ModelAttribute Map request) throws JsonProcessingException { - return getTeamCourseSettingHandler.handle(request); - } - - @PostMapping("GetUserActivityApi") - String getUserActivity(@ModelAttribute Map request) throws JsonProcessingException { - return getUserActivityHandler.handle(request); - } - - @PostMapping("GetUserCharacterApi") - String getUserCharacter(@ModelAttribute Map request) throws JsonProcessingException { - return getUserCharacterHandler.handle(request); - } - - @PostMapping("GetUserChargeApi") - String getUserCharge(@ModelAttribute Map request) throws JsonProcessingException { - return getUserChargeHandler.handle(request); - } - - @PostMapping("GetUserCourseApi") - String getUserCourse(@ModelAttribute Map request) throws JsonProcessingException { - return getUserCourseHandler.handle(request); - } - - @PostMapping("GetUserDataApi") - String getUserData(@ModelAttribute Map request) throws JsonProcessingException { - return getUserDataHandler.handle(request); - } - - @PostMapping("GetUserDuelApi") - String getUserDuel(@ModelAttribute Map request) throws JsonProcessingException { - return getUserDuelHandler.handle(request); - } - - @PostMapping("GetUserFavoriteItemApi") - String getUserFavoriteItem(@ModelAttribute Map request) throws JsonProcessingException { - return getUserFavoriteItemHandler.handle(request); - } - - @PostMapping("GetUserItemApi") - String getUserItem(@ModelAttribute Map request) throws JsonProcessingException { - return getUserItemHandler.handle(request); - } - - @PostMapping("GetUserLoginBonusApi") - String getUserLoginBonus(@ModelAttribute Map request) throws JsonProcessingException { - return getUserLoginBonusHandler.handle(request); - } - - @PostMapping("GetUserMapAreaApi") - String getUserMap(@ModelAttribute Map request) throws JsonProcessingException { - return getUserMapAreaHandler.handle(request); - } - - @PostMapping("GetUserMusicApi") - String getUserMusic(@ModelAttribute Map request, @PathVariable String version) throws JsonProcessingException { - request.put("version", version); - return getUserMusicHandler.handle(request); - } - - @PostMapping("GetUserOptionApi") - String getUserOption(@ModelAttribute Map request) throws JsonProcessingException { - return getUserOptionHandler.handle(request); - } - - // Call when login. Return null if no profile exist - @PostMapping("GetUserPreviewApi") - String getUserPreview(@ModelAttribute Map request) throws JsonProcessingException { - return getUserPreviewHandler.handle(request); - } - - @PostMapping("GetUserRecentRatingApi") - String getUserRecentRating(@ModelAttribute Map request) throws JsonProcessingException { - return getUserRecentRatingHandler.handle(request); - } - - @PostMapping("GetUserRegionApi") - String getUserRegion(@ModelAttribute Map request) throws JsonProcessingException { - return getUserRegionHandler.handle(request); - } - - @PostMapping("GetUserRivalDataApi") - String getUserRivalData(@ModelAttribute Map request) throws JsonProcessingException { - return getUserRivalDataHandler.handle(request); - } - - @PostMapping("GetUserRivalMusicApi") - String getUserRivalMusic(@ModelAttribute Map request) throws JsonProcessingException { - return getUserRivalMusicHandler.handle(request); - } - - @PostMapping("GetUserTeamApi") - String getUserTeam(@ModelAttribute Map request) throws JsonProcessingException { - return getUserTeamHandler.handle(request); - } - - @PostMapping("GetUserSymbolChatSettingApi") - String getUserSymbolChatSetting(@ModelAttribute Map request) throws JsonProcessingException { - return getUserSymbolChatSettingHandler.handle(request); - } - - @PostMapping("GetUserNetBattleDataApi") - String getUserNetBattleData(@ModelAttribute Map request) throws JsonProcessingException { - return getUserNetBattleDataHandler.handle(request); - } - - @PostMapping("UpsertClientBookkeepingApi") - String upsertClientBookkeeping(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertClientDevelopApi") - String upsertClientDevelop(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertClientErrorApi") - String upsertClientError(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertClientSettingApi") - String upsertClientSetting(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertClientTestmodeApi") - String upsertClientTestmode(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertUserAllApi") - String upsertUserAll(@ModelAttribute Map request) throws JsonProcessingException { - return upsertUserAllHandler.handle(request); - } - - @PostMapping("UpsertUserChargelogApi") - String upsertUserChargelog(@ModelAttribute Map request) throws JsonProcessingException { - return upsertUserChargelogHandler.handle(request); - } - - @PostMapping("CreateTokenApi") - String createToken(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("RemoveTokenApi") - String removeToken(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("UpsertClientUploadApi") - String upsertClientUpload(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - // Matching endpoints - - @PostMapping("MatchingServer/Ping") - String ping(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("MatchingServer/BeginMatchingApi") - String beginMatching(@ModelAttribute Map request) throws JsonProcessingException { - return beginMatchingHandler.handle(request); - } - - @PostMapping("MatchingServer/EndMatchingApi") - String endMatching(@ModelAttribute Map request) throws JsonProcessingException { - return endMatchingHandler.handle(request); - } - - @PostMapping("MatchingServer/RemoveMatchingMemberApi") - String removeMatchingMember(@ModelAttribute Map request) throws JsonProcessingException { - return removeMatchingMemberHandler.handle(request); - } - - @PostMapping("MatchingServer/GetMatchingStateApi") - String getMatchingState(@ModelAttribute Map request) throws JsonProcessingException { - return getMatchingStateHandler.handle(request); - } - - // Cardmaker endpoints - - @PostMapping("GetGameGachaApi") - String getGameGacha(@ModelAttribute Map request) throws JsonProcessingException { - return getGameGachaHandler.handle(request); - } - - @PostMapping("GetGameGachaCardByIdApi") - String getGameGachaCardById(@ModelAttribute Map request) throws JsonProcessingException { - return getGameGachaCardByIdHandler.handle(request); - } - - @PostMapping("GetUserCardPrintErrorApi") - String getUserCardPrintError(@ModelAttribute Map request) throws JsonProcessingException { - return getUserCardPrintErrorHandler.handle(request); - } - - @PostMapping("CMGetUserCharacterApi") - String cmGetUserCharacter(@ModelAttribute Map request) throws JsonProcessingException { - return cmGetUserCharacterHandler.handle(request); - } - - @PostMapping("CMGetUserDataApi") - String cmGetUserData(@ModelAttribute Map request) throws JsonProcessingException { - return cmGetUserDataHandler.handle(request); - } - - @PostMapping("GetUserGachaApi") - String GetUserGacha(@ModelAttribute Map request) throws JsonProcessingException { - return getUserGachaHandler.handle(request); - } - - @PostMapping("CMGetUserItemApi") - String cmGetUserItem(@ModelAttribute Map request) throws JsonProcessingException { - return cmGetUserItemHandler.handle(request); - } - - @PostMapping("CMGetUserPreviewApi") - String cmGetUserPreview(@ModelAttribute Map request) throws JsonProcessingException { - return cmGetUserPreviewHandler.handle(request); - } - - @PostMapping("GetUserPrintedCardApi") - String getUserPrintedCard(@ModelAttribute Map request) throws JsonProcessingException { - return getUserPrintedCardHandler.handle(request); - } - - @PostMapping("PrinterLoginApi") - String printerLogin(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("PrinterLogoutApi") - String printerLogout(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\"}"; - } - - @PostMapping("RollGachaApi") - String rollGacha(@ModelAttribute Map request) throws JsonProcessingException { - return rollGachaHandler.handle(request); - } - - @PostMapping("CMUpsertUserGachaApi") - String cmUpsertUserGacha(@ModelAttribute Map request) throws JsonProcessingException { - return cmUpsertUserGachaHandler.handle(request); - } - - @PostMapping("CMUpsertUserPrintApi") - String cmUpsertUserPrint(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\", \"orderId\":\"0\", \"serialId\":\"FAKECARDIMAG12345678\", \"apiName\":\"CMUpsertUserPrintApi\"}"; - } - - @PostMapping("CMUpsertUserPrintCancelApi") - String cmUpsertUserPrintCancel(@ModelAttribute Map request) throws JsonProcessingException { - return cmUpsertUserPrintCancelHandler.handle(request); - } - - @PostMapping("CMUpsertUserPrintlogApi") - String cmUpsertUserPrintlog(@ModelAttribute Map request) { - return "{\"returnCode\":\"1\", \"orderId\":\"0\", \"serialId\":\"FAKECARDIMAG12345678\", \"apiName\":\"CMUpsertUserPrintlogApi\"}"; - } - - @PostMapping("CMUpsertUserPrintSubtractApi") - String cmUpsertUserPrintSubtract(@ModelAttribute Map request) throws JsonProcessingException { - return cmUpsertUserPrintSubtractHandler.handle(request); - } - -} diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletControllerAdvice.java b/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletControllerAdvice.java deleted file mode 100644 index 42463b3f..00000000 --- a/src/main/java/icu/samnyan/aqua/sega/chusan/controller/ChusanServletControllerAdvice.java +++ /dev/null @@ -1,38 +0,0 @@ -package icu.samnyan.aqua.sega.chusan.controller; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Map; - -/** - * @author samnyan (privateamusement@protonmail.com) - */ -@RestControllerAdvice(basePackages = "icu.samnyan.aqua.sega.chusan") -public class ChusanServletControllerAdvice { - - private static final Logger logger = LoggerFactory.getLogger(ChusanServletControllerAdvice.class); - - /** - * Get the map object from json string - * - * @param request HttpServletRequest - */ - @ModelAttribute - public Map preHandle(HttpServletRequest request) throws IOException { - byte[] src = request.getInputStream().readAllBytes(); - String outputString = new String(src, StandardCharsets.UTF_8).trim(); - logger.info("Request {} : {}", request.getRequestURI(), outputString); - ObjectMapper mapper = new ObjectMapper(); - - return mapper.readValue(outputString, new TypeReference<>() { - }); - } -}