Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce0203 committed Aug 9, 2023
1 parent ec9c72f commit e834ddf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
21 changes: 17 additions & 4 deletions client/src/commonMain/kotlin/ui/createRoomMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import korlibs.korge.view.Container
import korlibs.korge.view.addTo
import korlibs.korge.view.align.*
import korlibs.korge.view.positionX
import korlibs.korge.view.solidRect
import korlibs.math.geom.Size
import network.*
import network.CreateRoomResultType.*
import scene.styler
import sceneContainer
import ui.custom.*
Expand All @@ -37,7 +37,6 @@ fun createRoomMenu(container: Container) {
val padding = 15.75f
val minAmount = 2
val maxAmount = 12
val recommendedAmount = 6
val rangeSize = maxAmount - minAmount + 1
val blockHeight = sceneContainer.width / 22
val blockSize = Size(blockHeight * rangeSize, blockHeight)
Expand Down Expand Up @@ -77,7 +76,7 @@ fun createRoomMenu(container: Container) {
}
}
roomSize = customUiSlider(
value = recommendedAmount,
value = CreateRoom.defaultRoomMaxPlayers,
min = minAmount,
max = maxAmount,
size = size,
Expand Down Expand Up @@ -123,7 +122,21 @@ fun createRoomMenu(container: Container) {
createRoomMenu.removeFromParent()
val createRoom = CreateRoom(roomName.text,
roomSize.index + minAmount, RoomMode.NORMAL)
val room = createRoom(createRoom).uuid
val createRoomResult = createRoom(createRoom)
when (createRoomResult.type) {
NOT_ALlOWED_NAME -> {
warningText.text = "방 이름은 한글, 영문, 숫자만 가능합니다"
warningText.styles.textColor = Colors.PALEVIOLETRED
return
}
NOT_ALLOWED_MAX_PLAYERS_AMOUNT -> {
warningText.text = "방 이름은 3글자 이상 16글자 이하여야 합니다"
warningText.styles.textColor = Colors.PALEVIOLETRED
return
}
else -> {}
}
val room = createRoomResult.createdRoom!!.uuid
joinRoom(room)
WaitingRoomState().waitingRoom(room)
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/commonMain/kotlin/ui/waitingRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ suspend fun WaitingRoomState.waitingRoom(room: UUID) {
onUp up@{
if (isDone) return@up
isDone = true
leaveRoom(sessionUUID)
requestLeaveRoom(sessionUUID)
waitingRoom.removeFromParent()
MainMenuState().mainMenu()
}
Expand Down
15 changes: 9 additions & 6 deletions server/src/main/kotlin/model/Rooms.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import kotlinx.uuid.UUID
import kotlinx.uuid.exposed.KotlinxUUIDEntity
import kotlinx.uuid.exposed.KotlinxUUIDEntityClass
import kotlinx.uuid.exposed.KotlinxUUIDTable
import network.CreateRoom
import network.RoomMode
import network.ViewedRoom
import network.*
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
Expand Down Expand Up @@ -45,12 +43,17 @@ fun getJoinedPlayersAmount(room: UUID) = transaction {


fun createRoom(creator: UUID, createRoom: CreateRoom) = transaction {
if (createRoom.test().not()) throw AssertionError()
Room.new {
if (createRoom.testLength().not()) {
CreateRoomResult(CreateRoomResultType.NOT_ALlOWED_NAME)
} else if (createRoom.testMaxPlayers().not()) {
CreateRoomResult(CreateRoomResultType.NOT_ALLOWED_MAX_PLAYERS_AMOUNT)
} else Room.new {
// val onlinePlayer = OnlinePlayer.find(OnlinePlayers.id eq creator).first()
name = createRoom.name
maxPlayers = createRoom.maxPlayers
}.run { ViewedRoom(id.value, name, maxPlayers, 0, createRoom.roomMode) }
}
.run { ViewedRoom(id.value, name, maxPlayers, 0, createRoom.roomMode) }
.run { CreateRoomResult(CreateRoomResultType.CREATED, this) }
}

fun nameRoom(room: UUID) = transaction {
Expand Down
9 changes: 7 additions & 2 deletions shared/src/commonMain/kotlin/network/Requesets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ data class LoginRequest(val username: String)

@Serializable
data class CreateRoom(val name: String, val maxPlayers: Int, val roomMode: RoomMode) {
fun test() = name.length <= defaultRoomNameLength && maxPlayers <= defaultRoomMaxPlayers
fun testLength() = name.length in AtLeastRoomNameLength..AtMostRoomNameLength
fun testMaxPlayers() = maxPlayers in AtLeastRoomPlayers..AtMostRoomPlayers
companion object {
const val AtLeastRoomPlayers = 2
const val AtMostRoomPlayers = 12
const val AtLeastRoomNameLength = 3
const val AtMostRoomNameLength = 16
const val defaultRoomMaxPlayers = 6
const val defaultRoomNameLength = 8
fun defaultRoomName(username: String) = "${username}의 방"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package network

import io.ktor.client.call.*
import io.ktor.client.request.*
import kotlinx.serialization.Serializable
import kotlinx.uuid.UUID

suspend fun createRoom(createRoom: CreateRoom) = sendHttp("rooms/create", createRoom).body<ViewedRoom>()
@Serializable
enum class CreateRoomResultType { CREATED, NOT_ALLOWED_MAX_PLAYERS_AMOUNT, NOT_ALlOWED_NAME }
@Serializable
data class CreateRoomResult(val type: CreateRoomResultType, val createdRoom: ViewedRoom? = null)
suspend fun createRoom(createRoom: CreateRoom) = sendHttp("rooms/create", createRoom).body<CreateRoomResult>()
suspend fun getViewedRooms() = runCatching {
client().post("$currentUrl/rooms") {
basicAuth(username, sessionId)
Expand All @@ -13,7 +18,7 @@ suspend fun getViewedRooms() = runCatching {

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

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

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

Expand Down

0 comments on commit e834ddf

Please sign in to comment.