aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/apis/Routes.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-08-28 19:04:24 +0200
committerLinnea Gräf <nea@nea.moe>2024-08-28 19:04:24 +0200
commitd2f240ff0ca0d27f417f837e706c781a98c31311 (patch)
tree0db7aff6cc14deaf36eed83889d59fd6b3a6f599 /src/main/kotlin/apis/Routes.kt
parenta6906308163aa3b2d18fa1dc1aa71ac9bbcc83ab (diff)
downloadfirmament-d2f240ff0ca0d27f417f837e706c781a98c31311.tar.gz
firmament-d2f240ff0ca0d27f417f837e706c781a98c31311.tar.bz2
firmament-d2f240ff0ca0d27f417f837e706c781a98c31311.zip
Refactor source layout
Introduce compat source sets and move all kotlin sources to the main directory [no changelog]
Diffstat (limited to 'src/main/kotlin/apis/Routes.kt')
-rw-r--r--src/main/kotlin/apis/Routes.kt95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main/kotlin/apis/Routes.kt b/src/main/kotlin/apis/Routes.kt
new file mode 100644
index 0000000..bf55a2d
--- /dev/null
+++ b/src/main/kotlin/apis/Routes.kt
@@ -0,0 +1,95 @@
+
+
+package moe.nea.firmament.apis
+
+import io.ktor.client.call.*
+import io.ktor.client.request.*
+import io.ktor.http.*
+import io.ktor.util.*
+import java.util.*
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.async
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import kotlin.collections.MutableMap
+import kotlin.collections.listOf
+import kotlin.collections.mutableMapOf
+import kotlin.collections.set
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.util.MinecraftDispatcher
+
+object Routes {
+ 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 = UrsaManager.request(listOf("v1", "hypixel","player", uuid.toString()))
+ 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 = UrsaManager.request(listOf("v1", "hypixel","profiles", uuid.toString()))
+ if (!response.status.isSuccess()) {
+ launch(MinecraftDispatcher) {
+ @Suppress("DeferredResultUnused")
+ profiles.remove(uuid)
+ }
+ return@async null
+ }
+ response.body<Profiles>()
+ }
+ }
+ }.await()
+ }
+
+}