From 9609db941b22a3f2d5a985df1963ea14acd19a63 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Wed, 27 Mar 2024 23:10:41 -0400 Subject: [PATCH] [+] Wacca server handler (incomplete) --- .../samnyan/aqua/sega/wacca/WaccaServer.kt | 90 +++++++++++++++++++ .../aqua/sega/wacca/model/db/WaccaUser.kt | 8 +- 2 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 src/main/java/icu/samnyan/aqua/sega/wacca/WaccaServer.kt diff --git a/src/main/java/icu/samnyan/aqua/sega/wacca/WaccaServer.kt b/src/main/java/icu/samnyan/aqua/sega/wacca/WaccaServer.kt new file mode 100644 index 00000000..47e3269d --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/wacca/WaccaServer.kt @@ -0,0 +1,90 @@ +package icu.samnyan.aqua.sega.wacca + +import ext.* +import icu.samnyan.aqua.sega.wacca.WaccaOptionType.SET_ICON_ID +import icu.samnyan.aqua.sega.wacca.WaccaOptionType.SET_TITLE_ID +import icu.samnyan.aqua.sega.wacca.model.BaseRequest +import icu.samnyan.aqua.sega.wacca.model.db.WaccaRepos +import icu.samnyan.aqua.sega.wacca.model.db.WaccaUser +import io.ktor.client.utils.* +import jakarta.servlet.http.HttpServletRequest +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.RestController + +val empty = emptyList() + +@RestController +@API("/g/wacca/") +class WaccaServer(val rp: WaccaRepos) { + val handlerMap = mutableMapOf) -> List>() + val cacheMap = mutableMapOf() + + val log = logger() + + init { api() } + + // DSL Functions + fun options(u: WaccaUser) = rp.option.findByUser(u).associate { it.optId to it.value } + operator fun Map.get(type: WaccaOptionType) = getOrDefault(type.id, type.default) + + fun ls(vararg args: Any) = args.toList() + operator fun String.minus(value: Any) = value + + operator fun String.invoke(block: (BaseRequest, List) -> List) { handlerMap[this.lowercase()] = block } + infix fun String.cached(block: () -> Any) { cacheMap[this.lowercase()] = block().toJson() } + + /** Convert "3.07.01.JPN.26935.S" into "3.7.1" */ + fun String.shortVer() = split('.').let { "${it[0]}.${it[1].trimStart('0')}.${it[2].trimStart('0')}" } + + /** Generate response message */ + fun resp(paramsJson: String, status: Int = 0, message: String = ""): ResponseEntity { + val resp = """{"status":$status,"message":"${message.replace("\"", "\\\"")}","serverTime":${millis() / 1000},""" + + """"maintNoticeTime":0,"maintNotPlayableTime":0,"maintStartTime":0,"params":$paramsJson}""" + + return ResponseEntity.ok().headers( + "X-Wacca-Hash" to resp.md5(), + "Content-Type" to "application/json; charset=utf-8" + ).body(resp) + } + + /** Handle all requests */ + @API("/api/**") + fun handle(req: HttpServletRequest, @RB body: String): Any { + val path = req.requestURI.removePrefix("/g/wacca").removePrefix("/api").lowercase() + if (path in cacheMap) return resp(cacheMap[path]!!) + if (path !in handlerMap) return resp("[]", 1, "Not Found") + + val br = JACKSON.parse(body) + val resp = handlerMap[path]!!(br, br.params) + return resp(resp.toJson()) + } +} + +fun WaccaServer.api() { + "/housing/get" cached { ls("housingId" - 39, "isNewCab" - 0) } + "/housing/start" cached { ls("regionId" - 1, "recommendSongList" - ls(1269, 1007, 1270, 1002, 1020, 1003, 1008, + 1211, 1018, 1092, 1056, 32, 1260, 1230, 1258, 1251, 2212, 1264, 1125, 1037, 2001, 1272, 1126, 1119, 1104, 1070, + 1047, 1044, 1027, 1004, 1001, 24, 2068, 2062, 2021, 1275, 1249, 1207, 1203, 1107, 1021, 1009, 9, 4, 3, 23, 22, + 2014, 13, 1276, 1247, 1240, 1237, 1128, 1114, 1110, 1109, 1102, 1045, 1043, 1036, 1035, 1030, 1023, 1015)) + } + "/advertise/GetRanking" cached { empty } + "/advertise/GetNews" cached { ls("notices" - empty, "copyright" - empty, empty, empty, empty, empty, empty, empty, empty) } + "/user/info/GetMyRoom" cached { ls(0, 0, 0, 0, 0, empty, 0, 0, 0) } + "/user/status/Logout" cached { empty } + "/user/info/GetRanking" cached { ls("totalScore#" - 0, "highScoreBySong#" - 0, "cumulativeScore#" - 0, + "stateUpScore#" - 0, "otherScore#" - 0, "waccaPoints#" - 0) } + + "/user/status/get" { req, (uid) -> + val ru = rp.user.findById(uid as Long)() + val u = ru ?: WaccaUser() + val o = options(u) + u.run { ls( + ls(uid, username, "userType" - 1, xp, danLevel, danType, wp, "titlePartIds" - ls(0, 0, 0), + loginCount, loginCountDays, loginCountConsec, loginCountDaysConsec, vipExpireTime, loginCountToday, rating), + o[SET_TITLE_ID], o[SET_ICON_ID], + "status" - if (ru == null) 1 else 0, // 0 = GOOD, 1 = Register + // 0 = Version GOOD, 1 = Game is newer, 2 = Game is older + "version" - ls(req.appVersion.shortVer().compareTo(lastGameVer.shortVer()), lastGameVer.shortVer()) + ) } + } +} \ No newline at end of file diff --git a/src/main/java/icu/samnyan/aqua/sega/wacca/model/db/WaccaUser.kt b/src/main/java/icu/samnyan/aqua/sega/wacca/model/db/WaccaUser.kt index b161b506..88c2164c 100644 --- a/src/main/java/icu/samnyan/aqua/sega/wacca/model/db/WaccaUser.kt +++ b/src/main/java/icu/samnyan/aqua/sega/wacca/model/db/WaccaUser.kt @@ -2,11 +2,7 @@ package icu.samnyan.aqua.sega.wacca.model.db import icu.samnyan.aqua.net.games.BaseEntity import icu.samnyan.aqua.sega.general.model.Card -import jakarta.persistence.Column -import jakarta.persistence.Entity -import jakarta.persistence.JoinColumn -import jakarta.persistence.OneToOne -import jakarta.persistence.Table +import jakarta.persistence.* /** * General user information @@ -30,7 +26,7 @@ class WaccaUser : BaseEntity() { var title1 = 0 var title2 = 0 var rating = 0 - var vipExpireTime: String? = null + var vipExpireTime: String = "" var alwaysVip = false var loginCount = 0 var loginCountConsec = 0