Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce0203 committed Aug 4, 2023
1 parent 3cedea8 commit b42205d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 30 deletions.
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 @@ -72,7 +72,7 @@ suspend fun waitingRoom(room: UUID) {
scroll.scrollTopRatio = 1f
onEvent(ChatEvent) { event ->
println("chat")
val (username, message) = event.chat
val (username, message) = event.chatPacket
val chat = uiText("<$username> $message")
space.height = max(0f, space.height - chat.height)
scroll.scrollTopRatio = 1f
Expand Down
4 changes: 1 addition & 3 deletions client/src/commonMain/kotlin/websocket/websocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package websocket

import event.ChatEvent
import io.ktor.util.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.charsets.*
import korlibs.io.net.ws.WebSocketClient
import kotlinx.coroutines.Job
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -46,7 +44,7 @@ suspend fun startWebSocket(): Job {

@Suppress("UNCHECKED_CAST")
fun serverPacket(serverPacket: ServerPacket): PacketController<Any> = when(serverPacket) {
ServerPacket.CHAT -> packet<Chat> {
ServerPacket.CHAT -> packet<ChatPacket> {
sceneContainer.dispatch(ChatEvent(it))
}
} as PacketController<Any>
11 changes: 11 additions & 0 deletions server/src/main/kotlin/application/rooms.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.uuid.UUID
import model.*
import network.PlayerJoinPacket
import network.PlayerLeavePacket
import network.ServerPacket

fun Application.configureRooms() {
routing {
Expand All @@ -19,11 +22,19 @@ fun Application.configureRooms() {
post("create") { call.respond(createRoom(call.getPlayer())) }
post("join") {
joinRoom(call.getPlayer(), call.receive<UUID>())
val player = getPlayerBySession(call.getUserSession())
getPlayersByRoom(player.room!!.value).forEach {
it.websocket.sendToClient(ServerPacket.PLAYER_JOIN, PlayerJoinPacket(player.name))
}
call.respond(HttpStatusCode.OK)
}
post("name") { call.respond(nameRoom(call.receive<UUID>())) }
post("leave") {
leaveRoom(call.getUserSession())
val player = getPlayerBySession(call.getUserSession())
getPlayersByRoom(player.room!!.value).forEach {
it.websocket.sendToClient(ServerPacket.PLAYER_LEAVE, PlayerLeavePacket(player.name))
}
call.respond(HttpStatusCode.OK)
}
}
Expand Down
11 changes: 6 additions & 5 deletions server/src/main/kotlin/application/websocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import java.util.*
import kotlin.collections.LinkedHashSet

val serverUUID = UUID.generateUUID()
val connections = Collections.synchronizedSet<Connection?>(LinkedHashSet())
val connections: MutableSet<Connection> = Collections.synchronizedSet<Connection?>(LinkedHashSet())
fun getPlayersByRoom(room: UUID) =
connections.filter { runCatching { getPlayerBySession(it.session).room?.equals(room) }.getOrNull()?: false }

class Connection(val websocket: DefaultWebSocketSession, val session: UUID)
fun Application.configureWebsocket() {
Expand Down Expand Up @@ -64,10 +66,9 @@ suspend fun serverPacket(websocket: DefaultWebSocketSession, session: UUID, clie
}
ClientPacket.CHAT -> packet<String> { received ->
val player = getPlayerBySession(session)
connections.filter { runCatching { getPlayerBySession(it.session).room?.equals(player.room) }.getOrNull()?: false }
.forEach {
it.websocket.sendToClient(ServerPacket.CHAT, Chat(player.name, received))
}
getPlayersByRoom(player.room!!.value).forEach {
it.websocket.sendToClient(ServerPacket.CHAT, ChatPacket(player.name, received))
}
}
} as PacketController<Any>

Expand Down
6 changes: 2 additions & 4 deletions shared/src/commonMain/kotlin/event/AuditEvent.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package event

import korlibs.event.*
import korlibs.math.geom.*
import korlibs.time.*
import network.Chat
import network.ChatPacket

class ChatEvent(
val chat: Chat,
val chatPacket: ChatPacket,
) : Event(), TEvent<ChatEvent> {
companion object : EventType<ChatEvent>
override val type: EventType<ChatEvent> get() = ChatEvent
Expand Down
6 changes: 0 additions & 6 deletions shared/src/commonMain/kotlin/network/Chat.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package network

import io.ktor.serialization.kotlinx.*
import io.ktor.util.reflect.*
import io.ktor.websocket.*
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.uuid.UUID
import kotlin.reflect.KClass

val serialFormat = Json

Expand Down Expand Up @@ -37,12 +35,3 @@ data class PacketFrame(
val data: String
)

enum class ClientPacket {
GET_ROOM_NUMBER,
LEAVE_ROOM,
CHAT
}

enum class ServerPacket {
CHAT
}
23 changes: 23 additions & 0 deletions shared/src/commonMain/kotlin/network/Packets.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package network

import kotlinx.serialization.Serializable

enum class ClientPacket {
GET_ROOM_NUMBER,
LEAVE_ROOM,
CHAT
}

enum class ServerPacket {
CHAT,
PLAYER_JOIN, PLAYER_LEAVE
}

@Serializable
data class ChatPacket(val username: String, val message: String)

@Serializable
data class PlayerJoinPacket(val username: String)

@Serializable
data class PlayerLeavePacket(val username: String)
Empty file removed temp.sh
Empty file.

0 comments on commit b42205d

Please sign in to comment.