Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce0203 committed Aug 19, 2023
1 parent a0a060c commit ddab1bf
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 214 deletions.
11 changes: 8 additions & 3 deletions client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ korge {
))
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "17"
}
}

kotlin {
jvm()
sourceSets {
val commonMain by getting {
kotlin.addSrcDir(File(project(":shared").projectDir, "src/commonMain/kotlin"))
// kotlin.addSrcDir(File(project(":shared").projectDir, "src/commonMain/kotlin"))
dependencies {
api("de.cketti.unicode:kotlin-codepoints-deluxe:0.6.1")
api(project(":deps"))
api(project(":shared"))
api(libs.kotlinx.uuid)
api(libs.kotlinx.serialization)
api(libs.koin)
api(libs.ktor.client.auth)
api(libs.ktor.client.content.negotation)
api(libs.ktor.serialization.kotlinx.protobuf)
Expand Down Expand Up @@ -88,7 +94,6 @@ kotlin {
).contains(it.name) }.forEach {
it.exclude(libs.kotlinx.uuid.asProvider())
it.exclude(libs.kotlinx.serialization)
it.exclude(libs.koin)
it.exclude(libs.ktor.client.auth)
it.exclude(libs.ktor.client.content.negotation)
it.exclude(libs.ktor.serialization.kotlinx.json)
Expand Down
34 changes: 0 additions & 34 deletions client/src/androidMain/AndroidManifest.xml

This file was deleted.

24 changes: 2 additions & 22 deletions client/src/androidMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import io.ktor.client.engine.cio.*
import korlibs.io.file.std.resourcesVfs
import korlibs.io.lang.readProperties
import network.ClientEngineFactory
import network.URLProvider
import org.koin.core.context.startKoin
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module
import org.koin.mp.KoinPlatform

class Main

Expand All @@ -16,19 +8,7 @@ suspend fun runMain() {
}
suspend fun main() {
val clientProps = resourcesVfs["client.properties"].readProperties()
val url = clientProps["server"]!!
val version = clientProps["version"]!!
startKoin {}
KoinPlatform.getKoin().loadModules(listOf(module {
single(named("version")) { version }
factory {
object : URLProvider {
override val url: String get() = url
}
} bind URLProvider::class
factory {
object : ClientEngineFactory { override fun getEngine() = CIO }
} bind ClientEngineFactory::class
}))
currentUrl = clientProps["server"]!!
version = clientProps["version"]!!
startMain()
}
6 changes: 2 additions & 4 deletions client/src/commonMain/kotlin/MainScene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import korlibs.korge.scene.Scene
import korlibs.korge.style.*
import korlibs.korge.view.*
import network.ServerClosedPacket
import org.koin.core.qualifier.named
import org.koin.mp.KoinPlatform.getKoin
import ui.loadingMenu
import ui.loginMenuView
import util.ColorPalette
import util.launchNow
import util.transform

val styler: ViewStyles.() -> Unit = {
textFont = getKoin().get<Font>()
textFont = font
textAlignment = TextAlignment.MIDDLE_CENTER
textSize = 100f
textColor = ColorPalette.text
Expand All @@ -38,7 +36,7 @@ class MainScene : Scene() {
}

screen.container {
text(getKoin().get<String>(named("version")), textSize = 30f) {
text(version, textSize = 30f) {
}.zIndex(100)
zIndex(100)
}.transform {
Expand Down
7 changes: 7 additions & 0 deletions client/src/commonMain/kotlin/const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import io.ktor.client.engine.*

typealias KtorEngine = HttpClientEngineFactory<HttpClientEngineConfig>

lateinit var currentUrl: String
lateinit var version: String
lateinit var engine: KtorEngine
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package network

import currentUrl
import engine
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.plugins.contentnegotiation.*
Expand All @@ -10,10 +12,9 @@ import io.ktor.serialization.kotlinx.*
import io.ktor.serialization.kotlinx.protobuf.*
import io.ktor.websocket.*
import kotlinx.serialization.protobuf.ProtoBuf
import org.koin.mp.KoinPlatform
import sessionId
import username

val currentUrl get() = KoinPlatform.getKoin().get<URLProvider>().url
val clientEngine get() = KoinPlatform.getKoin().get<ClientEngineFactory>().getEngine()

private var clientInst: HttpClient? = null
suspend fun websocket(): WebSocketSession = client().webSocketSession(currentUrl.httpToWs())
Expand Down Expand Up @@ -45,7 +46,7 @@ suspend fun client(): HttpClient = run {


suspend fun initializeClient() = run {
clientInst = HttpClient(clientEngine) {
clientInst = HttpClient(engine) {
install(io.ktor.client.plugins.websocket.WebSockets) {
contentConverter = converter
}
Expand Down
23 changes: 23 additions & 0 deletions client/src/commonMain/kotlin/network/login.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import io.ktor.client.call.*
import kotlinx.uuid.UUID
import kotlinx.uuid.generateUUID
import network.LoginRequest
import network.LoginResult
import network.LoginResultType
import network.sendHttp

var username: String = generateUsername()
lateinit var sessionId: String
lateinit var sessionUUID: UUID
val usernameRegex = Regex("[ㄱ-ㅎ가-힣a-zA-Z0-9._]")
private fun generateUsername() = UUID.generateUUID().toString().substring(0, 4)

suspend fun login(loginRequest: LoginRequest = LoginRequest(username)): LoginResultType {
val loginResult = runCatching { sendHttp("login", loginRequest, auth = false) }
.apply {println(this.exceptionOrNull()?.stackTraceToString())}.getOrNull()?.body<LoginResult>() ?: return LoginResultType.SERVER_IS_NOT_AVAILABLE
if (loginResult.result == LoginResultType.SUCCESS) {
sessionUUID = loginResult.uuid!!
sessionId = sessionUUID.toString()
}
return loginResult.result
}
23 changes: 23 additions & 0 deletions client/src/commonMain/kotlin/network/rooms.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package network

import currentUrl
import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.uuid.UUID
import sessionId
import username

suspend fun createRoom(createRoom: CreateRoom) = sendHttp("rooms/create", createRoom).body<CreateRoomResult>()
suspend fun getViewedRooms() = runCatching {
client().post("$currentUrl/rooms") {
basicAuth(username, sessionId)
}.body<List<ViewedRoom>>()
}.apply { this.exceptionOrNull()?.printStackTrace() }.getOrElse { listOf() }

suspend fun joinRoom(uuid: UUID) = sendHttp("rooms/join", uuid).status

suspend fun requestLeaveRoom(uuid: UUID) = sendHttp("rooms/leave", uuid).status

suspend fun getRoomName(uuid: UUID) = sendHttp("rooms/name", uuid).body<String>()

suspend fun listPlayer() = sendHttp("rooms/players").body<List<ViewedPlayer>>()
2 changes: 2 additions & 0 deletions client/src/commonMain/kotlin/network/websocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import kotlinx.serialization.encodeToByteArray
import kotlinx.serialization.protobuf.ProtoBuf
import network.ServerPacket.*
import MainScene
import currentUrl
import scene
import sessionUUID
import ui.loadingMenu
import util.launchNow

Expand Down
34 changes: 14 additions & 20 deletions client/src/commonMain/kotlin/start.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import korlibs.image.bitmap.Bitmap
import korlibs.image.bitmap.BmpSlice
import korlibs.image.font.Font
import korlibs.image.font.WoffFont
import korlibs.image.font.readWoffFont
import korlibs.image.format.readBitmap
import korlibs.image.format.readBitmapSlice
Expand All @@ -10,30 +13,24 @@ import korlibs.korge.view.*
import korlibs.math.geom.Anchor
import korlibs.math.geom.ScaleMode
import korlibs.math.geom.Size
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module
import org.koin.mp.KoinPlatform.getKoin
import util.ColorPalette
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext

lateinit var scene: SceneContainer
lateinit var screen: FixedSizeContainer
lateinit var globalCoroutineContext: CoroutineContext

lateinit var logo: BmpSlice
lateinit var font: WoffFont
lateinit var boldFont: WoffFont
lateinit var profile: Bitmap
suspend fun startMain() {
val coroutineContext = coroutineContext
val logo = resourcesVfs["images/logo.png"].readBitmapSlice()
val font = resourcesVfs["fonts/NanumSquareNeoTTF-dEb.woff"].readWoffFont()
val boldFont = resourcesVfs["fonts/NanumSquareNeoTTF-eHv.woff"].readWoffFont()
val profile = resourcesVfs["images/profile.png"].readBitmap()
getKoin().loadModules(listOf(module {
single { coroutineContext }
single(named("bold")) { boldFont } bind Font::class
single { font } bind Font::class
single(named("logo")) { logo }
single(named("profile")) { profile }

}))
globalCoroutineContext = coroutineContext
logo = resourcesVfs["images/logo.png"].readBitmapSlice()
font = resourcesVfs["fonts/NanumSquareNeoTTF-dEb.woff"].readWoffFont()
boldFont = resourcesVfs["fonts/NanumSquareNeoTTF-eHv.woff"].readWoffFont()
profile = resourcesVfs["images/profile.png"].readBitmap()
Korge(
windowSize = Size(512, 512),
title = "Skeep",
Expand All @@ -47,6 +44,3 @@ suspend fun startMain() {
scene.changeTo({ MainScene() })
}
}

inline fun <reified T : List<*>> onAdd(hook: List<T>) {
}
5 changes: 2 additions & 3 deletions client/src/commonMain/kotlin/ui/MainMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import korlibs.math.geom.*
import korlibs.time.DateTime
import korlibs.time.milliseconds
import korlibs.time.seconds
import logo
import network.*
import org.koin.core.qualifier.named
import org.koin.mp.KoinPlatform.getKoin
import scene
import styler
import screen
Expand Down Expand Up @@ -145,7 +144,7 @@ fun MainMenuState.room(room: ViewedRoom) {
rooms.customUiButton(size = buttonSize) button@{
transform { size(buttonSize) }
isRoom = true
val logo = getKoin().get<BmpSlice>(named("logo"))
val logo = logo
uiImage(size = logoSize, bitmap = logo, scaleMode = ScaleMode.FIT).transform {
size(logoSize)
.centerYOn(this@button)
Expand Down
1 change: 1 addition & 0 deletions client/src/commonMain/kotlin/ui/createRoomMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import styler
import screen
import ui.custom.*
import ui.custom.UITextInput
import username
import util.ColorPalette
import util.launchNow
import util.transform
Expand Down
4 changes: 3 additions & 1 deletion client/src/commonMain/kotlin/ui/newLoginMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ import korlibs.korge.ui.*
import korlibs.korge.view.Container
import korlibs.korge.view.align.*
import korlibs.korge.view.positionX
import korlibs.korge.view.solidRect
import korlibs.korge.view.zIndex
import korlibs.math.geom.RectCorners
import korlibs.math.geom.Size
import login
import network.*
import styler
import screen
import ui.custom.UITextInput
import ui.custom.customUiButton
import ui.custom.customUiTextInput
import username
import usernameRegex
import util.ColorPalette
import util.launchNow
import util.transform
Expand Down
6 changes: 3 additions & 3 deletions client/src/commonMain/kotlin/ui/waitingRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import korlibs.math.geom.Size
import korlibs.time.seconds
import kotlinx.uuid.UUID
import network.*
import org.koin.core.qualifier.named
import org.koin.mp.KoinPlatform.getKoin
import profile
import styler
import screen
import sessionUUID
import ui.custom.*
import ui.custom.UITextInput
import util.ColorPalette
Expand Down Expand Up @@ -177,7 +177,7 @@ fun WaitingRoomState.profileView(name: String, container: Container) {
transform { size(profileSize) }
// solidRect(size, color = Colors.WHITE)
setExtra("profile", name)
val imageBitmap = getKoin().get<Bitmap>(named("profile")).toBMP32().apply {
val imageBitmap = profile.toBMP32().apply {
updateColors {
if (it != Colors.TRANSPARENT) {
val t = ColorPalette.hover
Expand Down
7 changes: 5 additions & 2 deletions client/src/commonMain/kotlin/util/coroutineUtil.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package util

import globalCoroutineContext
import korlibs.io.async.launchImmediately
import kotlinx.coroutines.CoroutineScope
import org.koin.mp.KoinPlatform.getKoin
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlin.coroutines.CoroutineContext

fun launchNow(context: CoroutineContext = getKoin().get<CoroutineContext>(), callback: suspend () -> Unit) = CoroutineScope(context).launchImmediately(callback)
@DelicateCoroutinesApi
fun launchNow(context: CoroutineContext = GlobalScope.coroutineContext, callback: suspend () -> Unit) = CoroutineScope(context).launchImmediately(callback)
Loading

0 comments on commit ddab1bf

Please sign in to comment.