aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/apis/Routes.kt
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-06-02 23:01:09 +0200
committernea <nea@nea.moe>2023-06-02 23:01:09 +0200
commitf2eb8c4dc898336f1ee44e62e18b4a7406464736 (patch)
tree50af6c0e91306e2d5882b8407ff2028d91863585 /src/main/kotlin/moe/nea/firmament/apis/Routes.kt
parentf249df8f67114b14e3d75a955ace1d28ba6cb937 (diff)
downloadfirmament-f2eb8c4dc898336f1ee44e62e18b4a7406464736.tar.gz
firmament-f2eb8c4dc898336f1ee44e62e18b4a7406464736.tar.bz2
firmament-f2eb8c4dc898336f1ee44e62e18b4a7406464736.zip
Add more caching and fix some models
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/apis/Routes.kt')
-rw-r--r--src/main/kotlin/moe/nea/firmament/apis/Routes.kt109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/apis/Routes.kt b/src/main/kotlin/moe/nea/firmament/apis/Routes.kt
new file mode 100644
index 0000000..7785d90
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/apis/Routes.kt
@@ -0,0 +1,109 @@
+package moe.nea.firmament.apis
+
+import io.ktor.client.call.body
+import io.ktor.client.request.get
+import io.ktor.client.request.parameter
+import io.ktor.http.URLProtocol
+import io.ktor.http.isSuccess
+import io.ktor.http.path
+import io.ktor.util.CaseInsensitiveMap
+import java.util.UUID
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.async
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.util.MinecraftDispatcher
+
+object Routes {
+ val apiKey = "e721a103-96e0-400f-af2a-73b2a91007b1"
+ private val nameToUUID: MutableMap<String, Deferred<UUID?>> = CaseInsensitiveMap()
+ private val profiles: MutableMap<UUID, Deferred<Profiles?>> = mutableMapOf()
+ private val accounts: MutableMap<UUID, Deferred<PlayerData?>> = mutableMapOf()
+ private val UUIDToName: MutableMap<UUID, Deferred<String?>> = mutableMapOf()
+
+ suspend fun getPlayerNameForUUID(uuid: UUID): String? {
+ return withContext(MinecraftDispatcher) {
+ UUIDToName.computeIfAbsent(uuid) {
+ async(Firmament.coroutineScope.coroutineContext) {
+ val response = Firmament.httpClient.get("https://api.ashcon.app/mojang/v2/user/$uuid")
+ if (!response.status.isSuccess()) return@async null
+ val data = response.body<AshconNameLookup>()
+ launch(MinecraftDispatcher) {
+ nameToUUID[data.username] = async { data.uuid }
+ }
+ data.username
+ }
+ }
+ }.await()
+ }
+
+ suspend fun getUUIDForPlayerName(name: String): UUID? {
+ return withContext(MinecraftDispatcher) {
+ nameToUUID.computeIfAbsent(name) {
+ async(Firmament.coroutineScope.coroutineContext) {
+ val response = Firmament.httpClient.get("https://api.ashcon.app/mojang/v2/user/$name")
+ if (!response.status.isSuccess()) return@async null
+ val data = response.body<AshconNameLookup>()
+ launch(MinecraftDispatcher) {
+ UUIDToName[data.uuid] = async { data.username }
+ }
+ data.uuid
+ }
+ }
+ }.await()
+ }
+
+ suspend fun getAccountData(uuid: UUID): PlayerData? {
+ return withContext(MinecraftDispatcher) {
+ accounts.computeIfAbsent(uuid) {
+ async(Firmament.coroutineScope.coroutineContext) {
+ val response = Firmament.httpClient.get {
+ url {
+ protocol = URLProtocol.HTTPS
+ host = "api.hypixel.net"
+ path("player")
+ parameter("key", apiKey)
+ parameter("uuid", uuid)
+ }
+ }
+ if (!response.status.isSuccess()) {
+ launch(MinecraftDispatcher) {
+ @Suppress("DeferredResultUnused")
+ accounts.remove(uuid)
+ }
+ return@async null
+ }
+ response.body<PlayerResponse>().player
+ }
+ }
+ }.await()
+ }
+
+ suspend fun getProfiles(uuid: UUID): Profiles? {
+ return withContext(MinecraftDispatcher) {
+ profiles.computeIfAbsent(uuid) {
+ async(Firmament.coroutineScope.coroutineContext) {
+ val response = Firmament.httpClient.get {
+ url {
+ protocol = URLProtocol.HTTPS
+ host = "api.hypixel.net"
+ path("skyblock", "profiles")
+ parameter("key", apiKey)
+ parameter("uuid", uuid)
+ }
+ }
+ if (!response.status.isSuccess()) {
+ launch(MinecraftDispatcher) {
+ @Suppress("DeferredResultUnused")
+ profiles.remove(uuid)
+ }
+ return@async null
+ }
+ response.body<Profiles>()
+ }
+ }
+ }.await()
+ }
+
+}