From 667abf2131403ed69b0acaae4abe7719fd0a48e4 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sun, 29 Dec 2024 05:16:09 -0500 Subject: [PATCH] [O] Split matching apis --- .../samnyan/aqua/sega/chusan/ChusanApis.kt | 46 ++-------------- .../aqua/sega/chusan/ChusanMatchingApis.kt | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 src/main/java/icu/samnyan/aqua/sega/chusan/ChusanMatchingApis.kt diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt index 28144a97..b40d1d87 100644 --- a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt +++ b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanApis.kt @@ -2,8 +2,6 @@ package icu.samnyan.aqua.sega.chusan import ext.* import icu.samnyan.aqua.sega.chusan.model.request.UserCMissionResp -import icu.samnyan.aqua.sega.chusan.model.response.data.MatchingMemberInfo -import icu.samnyan.aqua.sega.chusan.model.response.data.MatchingWaitState import icu.samnyan.aqua.sega.chusan.model.response.data.UserEmoney import icu.samnyan.aqua.sega.chusan.model.userdata.UserCharge import icu.samnyan.aqua.sega.chusan.model.userdata.UserItem @@ -12,7 +10,9 @@ import icu.samnyan.aqua.sega.general.model.response.UserRecentRating import java.time.format.DateTimeFormatter @Suppress("UNCHECKED_CAST") -val chusanInit: ChusanController.() -> Unit = { +fun ChusanController.chusanInit() { + matchingApiInit() + // Stub handlers "GetGameRanking" { """{"type":"${data["type"]}","length":"0","gameRankingList":[]}""" } "GetGameIdlist" { """{"type":"${data["type"]}","length":"0","gameIdlistList":[]}""" } @@ -31,46 +31,6 @@ val chusanInit: ChusanController.() -> Unit = { "CMUpsertUserPrint" { """{"returnCode":1,"orderId":"0","serialId":"FAKECARDIMAG12345678","apiName":"CMUpsertUserPrintApi"}""" } "CMUpsertUserPrintlog" { """{"returnCode":1,"orderId":"0","serialId":"FAKECARDIMAG12345678","apiName":"CMUpsertUserPrintlogApi"}""" } - // Matching - data class MatchingRoom(val members: MutableList, val startTime: Long) - val matchingRooms = mutableMapOf() - var matchingLast = 0 - val matchingTime = 120 // Seconds - - "BeginMatching" { - val memberInfo = parsing { mapper.convert(data["matchingMemberInfo"] as JDict) } - - // Check if there are any room available with less than 4 members and not started - var id = matchingRooms.entries.find { it.value.members.size < 4 && it.value.startTime == 0L }?.key - if (id == null) { - matchingLast += 1 - id = matchingLast - matchingRooms[id] = MatchingRoom(mutableListOf(memberInfo), millis()) - } - - mapOf("roomId" to id, "matchingWaitState" to MatchingWaitState(listOf(memberInfo))) - } - - "GetMatchingState" api@ { - val roomId = parsing { data["roomId"]!!.int } - val room = matchingRooms[roomId] ?: return@api null - val dt = matchingTime - (millis() - room.startTime) / 1000 - val ended = room.members.size == 4 || dt <= 0 - - mapOf("roomId" to roomId, "matchingWaitState" to MatchingWaitState(room.members, ended, dt.int, 1)) - } - - "EndMatching" api@ { - val roomId = parsing { data["roomId"]!!.int } - val room = matchingRooms[roomId] ?: return@api null - mapOf( - "matchingMemberInfoList" to room.members, - "matchingMemberRoleList" to room.members.indices.map { mapOf("role" to it) }, - "matchingResult" to 1, - "reflectorUri" to "http://reflector.naominet.live:18080/" - ) - } - // User handlers "GetUserData" { db.userData.findByCard_ExtId(uid)()?.let{ u -> mapOf("userId" to uid, "userData" to u) } diff --git a/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanMatchingApis.kt b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanMatchingApis.kt new file mode 100644 index 00000000..1fd2f705 --- /dev/null +++ b/src/main/java/icu/samnyan/aqua/sega/chusan/ChusanMatchingApis.kt @@ -0,0 +1,52 @@ +package icu.samnyan.aqua.sega.chusan + +import ext.JDict +import ext.int +import ext.millis +import ext.parsing +import icu.samnyan.aqua.sega.chusan.model.response.data.MatchingMemberInfo +import icu.samnyan.aqua.sega.chusan.model.response.data.MatchingWaitState + + +@Suppress("UNCHECKED_CAST") +fun ChusanController.matchingApiInit() { + // Matching + data class MatchingRoom(val members: MutableList, val startTime: Long) + val matchingRooms = mutableMapOf() + var matchingLast = 0 + val matchingTime = 120 // Seconds + + "BeginMatching" { + val memberInfo = parsing { mapper.convert(data["matchingMemberInfo"] as JDict) } + + // Check if there are any room available with less than 4 members and not started + var id = matchingRooms.entries.find { it.value.members.size < 4 && it.value.startTime == 0L }?.key + if (id == null) { + matchingLast += 1 + id = matchingLast + matchingRooms[id] = MatchingRoom(mutableListOf(memberInfo), millis()) + } + + mapOf("roomId" to id, "matchingWaitState" to MatchingWaitState(listOf(memberInfo))) + } + + "GetMatchingState" api@ { + val roomId = parsing { data["roomId"]!!.int } + val room = matchingRooms[roomId] ?: return@api null + val dt = matchingTime - (millis() - room.startTime) / 1000 + val ended = room.members.size == 4 || dt <= 0 + + mapOf("roomId" to roomId, "matchingWaitState" to MatchingWaitState(room.members, ended, dt.int, 1)) + } + + "EndMatching" api@ { + val roomId = parsing { data["roomId"]!!.int } + val room = matchingRooms[roomId] ?: return@api null + mapOf( + "matchingMemberInfoList" to room.members, + "matchingMemberRoleList" to room.members.indices.map { mapOf("role" to it) }, + "matchingResult" to 1, + "reflectorUri" to "http://reflector.naominet.live:18080/" + ) + } +} \ No newline at end of file