|
| 1 | +package dev.schlaubi.mastermind.core |
| 2 | + |
| 3 | +import androidx.compose.runtime.getValue |
| 4 | +import androidx.compose.runtime.mutableStateOf |
| 5 | +import androidx.compose.runtime.setValue |
| 6 | +import dev.schlaubi.gtakiller.common.Event |
| 7 | +import dev.schlaubi.gtakiller.common.KillGtaEvent |
| 8 | +import dev.schlaubi.gtakiller.common.Route |
| 9 | +import dev.schlaubi.gtakiller.common.Status |
| 10 | +import dev.schlaubi.gtakiller.common.Username |
| 11 | +import dev.schlaubi.mastermind.core.settings.settings |
| 12 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 13 | +import io.ktor.client.* |
| 14 | +import io.ktor.client.call.* |
| 15 | +import io.ktor.client.plugins.* |
| 16 | +import io.ktor.client.plugins.contentnegotiation.* |
| 17 | +import io.ktor.client.plugins.resources.* |
| 18 | +import io.ktor.client.plugins.websocket.* |
| 19 | +import io.ktor.http.* |
| 20 | +import io.ktor.serialization.kotlinx.* |
| 21 | +import io.ktor.serialization.kotlinx.json.* |
| 22 | +import io.ktor.websocket.* |
| 23 | +import kotlinx.coroutines.* |
| 24 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 25 | +import kotlinx.coroutines.flow.asSharedFlow |
| 26 | +import kotlinx.serialization.json.Json |
| 27 | +import kotlin.coroutines.CoroutineContext |
| 28 | + |
| 29 | +var currentApi by mutableStateOf<APIClient?>(null) |
| 30 | + |
| 31 | +val safeApi get() = currentApi ?: error("No API client set") |
| 32 | + |
| 33 | +private val LOG = KotlinLogging.logger { } |
| 34 | + |
| 35 | +class APIClient(val url: Url) : CoroutineScope { |
| 36 | + override val coroutineContext: CoroutineContext = Dispatchers.IO + SupervisorJob() |
| 37 | + |
| 38 | + private val client = HttpClient { |
| 39 | + install(ContentNegotiation) { |
| 40 | + json() |
| 41 | + } |
| 42 | + install(WebSockets) { |
| 43 | + contentConverter = KotlinxWebsocketSerializationConverter(Json) |
| 44 | + } |
| 45 | + install(Resources) |
| 46 | + |
| 47 | + defaultRequest { |
| 48 | + url.takeFrom(this@APIClient.url) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private var webSocketSession: DefaultClientWebSocketSession? = null |
| 53 | + private val _events = MutableSharedFlow<Event>() |
| 54 | + val events = _events.asSharedFlow() |
| 55 | + |
| 56 | + suspend fun connectToWebSocket() { |
| 57 | + webSocketSession?.close() |
| 58 | + val session = client.webSocketSession { |
| 59 | + url { |
| 60 | + url.takeFrom(this@APIClient.url) |
| 61 | + protocol = if (url.protocol.isSecure()) { |
| 62 | + URLProtocol.WSS |
| 63 | + } else { |
| 64 | + URLProtocol.WS |
| 65 | + } |
| 66 | + |
| 67 | + client.href(Route.Events(), this) |
| 68 | + } |
| 69 | + |
| 70 | + headers.append(HttpHeaders.Username, settings.userName) |
| 71 | + } |
| 72 | + |
| 73 | + webSocketSession = session |
| 74 | + |
| 75 | + session.launch { |
| 76 | + launch { |
| 77 | + while (isActive) { |
| 78 | + val event = session.receiveDeserialized<Event>() |
| 79 | + LOG.debug { "Received event: $event" } |
| 80 | + _events.emit(event) |
| 81 | + handleEvent(event) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + val reason = session.closeReason.await() |
| 86 | + LOG.info { "Lost connection to websocket: ${reason?.message}" } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + suspend fun getCurrentStatus() = client.get(Route.Status()).body<Status>() |
| 91 | + |
| 92 | + suspend fun sendEvent(event: Event) { |
| 93 | + LOG.debug { "Sending event: $event" } |
| 94 | + webSocketSession?.sendSerialized(event) ?: error("Not connected") |
| 95 | + } |
| 96 | + |
| 97 | + fun disconnect() { |
| 98 | + client.close() |
| 99 | + webSocketSession?.cancel() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +suspend fun reportKillCommand() = safeApi.sendEvent(KillGtaEvent) |
| 104 | + |
| 105 | +private suspend fun handleEvent(event: Event) { |
| 106 | + when (event) { |
| 107 | + is KillGtaEvent -> killGta() |
| 108 | + else -> {} |
| 109 | + } |
| 110 | +} |
0 commit comments