Skip to content

Commit

Permalink
fix: api solo stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KatieUmbra committed Sep 12, 2023
1 parent d2dd773 commit f61f974
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:$okHttpVersion")
// File IO
implementation("com.squareup.okio:okio:$okioVersion")
testImplementation("io.ktor:ktor-server-test-host-jvm:2.3.4")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.9.10")
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ suspend inline fun <reified T : Stats> RequestBody.getLocalStats(
val element = profile.jsonObject["stats"]?.jsonObject?.get(gameModeKey) ?: JsonObject(emptyMap())
val mappedStats = buildJsonObject {
statsMapping.forEach { (original, mapped) ->
val originalValue = element.jsonObject.get(original)?.jsonPrimitive
put(mapped, originalValue ?: JsonNull)

val originalValue = element.jsonObject[mapped]?.jsonPrimitive
if (originalValue != null) {
put(mapped, originalValue)
}
}
}
mappedStats
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package studios.pinkcloud.celestial.api.routing.games.skywars

import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.server.testing.*
import studios.pinkcloud.celestial.api.module
import kotlin.test.Test

class SkywarsKtTest {

@Test
fun testGetPlayerSkywarsId() = testApplication {
application {
module()
}
client.get("/player/skywars/{id}").apply {
TODO("Please write your test here")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package studios.pinkcloud.celestial.api.routing.games.skywars

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import studios.pinkcloud.celestial.api.routing.games.Method
import studios.pinkcloud.celestial.api.routing.games.Stats
import studios.pinkcloud.celestial.api.routing.games.getLocalStats
Expand All @@ -22,18 +25,46 @@ data class GlobalSkywarsStats (
@SerialName("time_played") val timePlayed: Int,
@SerialName("opals") val opals: Int,
@SerialName("angel_of_death_level") val angelOfDeathLevel: Int,
@SerialName("games") val games: Int
@SerialName("games") val games: Int,
@SerialName("lab_win") val laboratoryWins: Int
): Stats()

private val gameModes = arrayOf(
"team_normal",
"team_insane",
"team",
"solo",
"solo_normal",
"solo_insane",
"mega",
"mega_normal",
"mega_insane",
"lucky_blocks_lab",
"lucky_blocks_lab_solo",
"lucky_blocks_lab_team",
"rush_lab",
"rush_lab_solo",
"rush_lab_team",
"slime_lab",
"slime_lab_solo",
"slime_lab_team",
"hunters_vs_beasts_lab",
"hunters_vs_beasts_lab_solo",
"hunters_vs_beasts_lab_team"
)

private val jsonIgnoreUnknownKeys = Json { ignoreUnknownKeys = true }

fun Application.skywarsStats() {
routing {
get("/player/skywars") {
getStats<GlobalSkywarsStats>("SkyWars") { return@get }
}
get("/player/skywars/solo") {
getLocalStats<GlobalSkywarsStats>("SkyWars", "solo", Method.SUFFIX) {}
get("/player/skywars/{id}") {
val param: String? = call.parameters["id"]
if (param == null) call.respondText("Invalid game mode id", status = HttpStatusCode.BadRequest)
if (param !in gameModes) call.respondText("Invalid game mode id", status = HttpStatusCode.BadRequest)
getLocalStats<GlobalSkywarsStats>("SkyWars", param!!, Method.SUFFIX) {};
}
}
}
54 changes: 53 additions & 1 deletion Api/src/test/skywars http.http
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,56 @@ Content-Type: application/json
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###
###

GET localhost:8080/player/skywars/solo_insane
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###

GET localhost:8080/player/skywars/solo_normal
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###

GET localhost:8080/player/skywars/team
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###
GET localhost:8080/player/skywars/team_normal
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###
GET localhost:8080/player/skywars/team_insane
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###

GET localhost:8080/player/skywars/mega
Content-Type: application/json

{
"uuid": "3a5321ea336043539f84fa018c5de50b"
}

###

0 comments on commit f61f974

Please sign in to comment.