Skip to content

Commit

Permalink
fix: Fixed some issues that prevented uploads (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Rainy authored Nov 23, 2024
1 parent 889ab03 commit b0479e5
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ class ChuniData {
data class SongInfo(
val id: Int, val title: String, val artist: String, val genre: String,
val bpm: Int, val version: Int, val difficulties: List<SongDifficulty>,
val disable: Boolean = false
val disabled: Boolean = false
)

@Serializable
data class MusicDetail(
val id: Int,
val name: String, val level: Float,
val score: Int, val rating: Float,
val version: Int, val playTime: String = "",
Expand Down Expand Up @@ -80,9 +81,5 @@ class ChuniData {
.bufferedReader().use { it.readText() }
).songs
}

fun getSongIdFromTitle(title: String): Int {
return CHUNI_SONG_LIST.find { it.title == title }?.id ?: -1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ChuniEnums {
EXPERT("Expert", 2),
MASTER("Master", 3),
ULTIMA("Ultima", 4),
WORLDSEND("World's End", 5),
WORLDSEND("World's End", 0),
RECENT("Recent", 6);

companion object {
Expand All @@ -27,7 +27,7 @@ class ChuniEnums {
@JvmStatic
fun getDifficultyWithIndex(diffIndex: Int): Difficulty {
for (difficulty in entries) {
if (difficulty.diffIndex == diffIndex) {
if (difficulty.ordinal == diffIndex) {
return difficulty
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MaimaiData {
data class SongInfo(
val id: Int, val title: String, val artist: String, val genre: String,
val bpm: Int, val version: Int, val difficulties: SongDifficulties,
val disable: Boolean = false
val disabled: Boolean = false
)

@Serializable
Expand Down Expand Up @@ -95,7 +95,8 @@ class MaimaiData {
}

fun getSongIdFromTitle(title: String): Int {
return MAIMAI_SONG_LIST.find { it.title == title }?.id ?: -1
val id = MAIMAI_SONG_LIST.find { it.title == title }?.id ?: -1
return id
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ class DivingFishProberUtil : IProberUtil {
)
}

val postResponse = client.post("$baseApiUrl/maimaidxprober/player/update_records") {
setBody(Json.encodeToString(postScores))
headers {
append("Import-Token", importToken)
append(HttpHeaders.ContentType, "application/json")
val postResponse = try {
client.post("$baseApiUrl/maimaidxprober/player/update_records") {
setBody(Json.encodeToString(postScores))
headers {
append("Import-Token", importToken)
append(HttpHeaders.ContentType, "application/json")
}
}
} catch (e: Exception) {
Log.e("DivingFishProberUtil", "上传失败: $e")
sendMessageToUi("上传失败: $e")
return
}

if (postResponse.status.value == 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package io.github.skydynamic.maiproberplus.core.prober

import android.util.Log
import io.github.skydynamic.maiproberplus.GlobalViewModel
import io.github.skydynamic.maiproberplus.core.data.chuni.ChuniData
import io.github.skydynamic.maiproberplus.core.data.maimai.MaimaiData
import io.ktor.client.call.body
import io.ktor.client.plugins.HttpRequestTimeoutException
import io.ktor.client.request.header
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.HttpHeaders
import io.ktor.http.headers
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
Expand All @@ -28,16 +28,16 @@ class LxnsProberUtil : IProberUtil {

@Serializable
data class LxnsMaimaiResponse(
val data: List<LxnsMaimaiScoreBody> = listOf()
val data: List<LxnsMaimaiUploadReturnScoreBody> = listOf()
) : LxnsResponse()

@Serializable
data class LxnsChuniResponse(
val data: List<LxnsChuniScoreBody> = listOf()
val data: List<LxnsChuniUploadReturnScoreBody> = listOf()
) : LxnsResponse()

@Serializable
data class LxnsChuniScoreBody(
data class LxnsChuniUploadScoreBody(
val id: Int,
@SerialName("song_name") val songName: String = "",
val level: String = "",
Expand All @@ -54,7 +54,24 @@ class LxnsProberUtil : IProberUtil {
)

@Serializable
data class LxnsMaimaiScoreBody(
data class LxnsChuniUploadReturnScoreBody(
val id: Int,
@SerialName("song_name") val songName: String = "",
val level: String = "",
@SerialName("level_index") val levelIndex: Int,
val score: LxnsUploadDiff<Int> = LxnsUploadDiff(),
val rating: LxnsUploadDiff<Float> = LxnsUploadDiff(),
@SerialName("over_power") val overPower: LxnsUploadDiff<Float> = LxnsUploadDiff(),
val clear: LxnsUploadDiff<String> = LxnsUploadDiff(),
@SerialName("full_combo") val fullCombo: LxnsUploadDiff<String> = LxnsUploadDiff(),
@SerialName("full_chain") val fullChain: LxnsUploadDiff<String> = LxnsUploadDiff(),
val rank: String = "",
@SerialName("play_time") val playTime: String = "",
@SerialName("upload_time") val uploadTime: String = ""
)

@Serializable
data class LxnsMaimaiUploadScoreBody(
val id: Int,
@SerialName("song_name") val songName: String = "",
val level: String = "",
Expand All @@ -71,10 +88,33 @@ class LxnsProberUtil : IProberUtil {
)

@Serializable
data class LxnsMaimaiRequestBody(val scores: List<LxnsMaimaiScoreBody>)
data class LxnsMaimaiUploadReturnScoreBody(
val id: Int,
@SerialName("song_name") val songName: String = "",
val level: String = "",
@SerialName("level_index") val levelIndex: Int,
val achievements: LxnsUploadDiff<Float> = LxnsUploadDiff(),
val fc: LxnsUploadDiff<String> = LxnsUploadDiff(),
val fs: LxnsUploadDiff<String> = LxnsUploadDiff(),
@SerialName("dx_score") val dxScore: LxnsUploadDiff<Int>,
@SerialName("dx_rating") val dxRating: LxnsUploadDiff<Float> = LxnsUploadDiff(),
val rate: String = "",
val type: String,
@SerialName("play_time") val playTime: String = "",
@SerialName("upload_time") val uploadTime: String = ""
)

@Serializable
data class LxnsUploadDiff<T>(
val old: T? = null,
val new: T? = null
)

@Serializable
data class LxnsMaimaiRequestBody(val scores: List<LxnsMaimaiUploadScoreBody>)

@Serializable
data class LxnsChuniRequestBody(val scores: List<LxnsChuniScoreBody>)
data class LxnsChuniRequestBody(val scores: List<LxnsChuniUploadScoreBody>)

override suspend fun uploadMaimaiProberData(
importToken: String,
Expand All @@ -88,7 +128,7 @@ class LxnsProberUtil : IProberUtil {
}

val postScores = scores.map {
LxnsMaimaiScoreBody(
LxnsMaimaiUploadScoreBody(
id = MaimaiData.getSongIdFromTitle(it.name),
levelIndex = it.diff.diffIndex,
achievements = it.score,
Expand All @@ -101,12 +141,18 @@ class LxnsProberUtil : IProberUtil {

val body = Json.encodeToString(LxnsMaimaiRequestBody(postScores))

val postResponse = client.post("$baseApiUrl/api/v0/user/maimai/player/scores") {
setBody(body)
header("X-User-Token", importToken)
headers {
append(HttpHeaders.ContentType, "application/json")
val postResponse = try {
client.post("$baseApiUrl/api/v0/user/maimai/player/scores") {
setBody(body)
header("X-User-Token", importToken)
headers {
append(HttpHeaders.ContentType, "application/json")
}
}
} catch (e: Exception) {
Log.e("LxnsProberUtil", "上传失败: $e")
sendMessageToUi("上传失败: $e")
return
}

val postScoreResponseBody = postResponse.body<LxnsMaimaiResponse>()
Expand All @@ -129,8 +175,8 @@ class LxnsProberUtil : IProberUtil {
}

val postScores = scores.map {
LxnsChuniScoreBody(
id = ChuniData.getSongIdFromTitle(it.name),
LxnsChuniUploadScoreBody(
id = it.id,
levelIndex = it.diff.diffIndex,
score = it.score,
clear = it.clearType.type,
Expand All @@ -141,12 +187,18 @@ class LxnsProberUtil : IProberUtil {

val body = Json.encodeToString(LxnsChuniRequestBody(postScores))

val postResponse = client.post("$baseApiUrl/api/v0/user/chunithm/player/scores") {
setBody(body)
headers {
append("X-User-Token", importToken)
append(HttpHeaders.ContentType, "application/json")
val postResponse = try {
client.post("$baseApiUrl/api/v0/user/chunithm/player/scores") {
setBody(body)
headers {
append("X-User-Token", importToken)
append(HttpHeaders.ContentType, "application/json")
}
}
} catch (e: HttpRequestTimeoutException) {
Log.e("LxnsProberUtil", "上传失败: $e")
sendMessageToUi("上传失败: $e")
return
}

val postScoreResponseBody = postResponse.body<LxnsChuniResponse>()
Expand Down
Loading

0 comments on commit b0479e5

Please sign in to comment.