-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
276 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ui | ||
|
||
import korlibs.korge.style.styles | ||
import korlibs.korge.ui.uiContainer | ||
import korlibs.korge.ui.uiText | ||
import korlibs.korge.view.align.alignX | ||
import korlibs.korge.view.align.alignY | ||
import kotlinx.uuid.UUID | ||
import network.ViewedRoom | ||
import network.getRoomName | ||
import scene.styler | ||
import sceneContainer | ||
|
||
suspend fun waitingRoom(room: UUID) { | ||
sceneContainer.uiContainer { | ||
styles(styler) | ||
uiText(getRoomName(room)) { | ||
|
||
} | ||
.alignY(root, 0.5, true) | ||
.alignX(root, 0.5, true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
package application | ||
|
||
import application.configuration.getPlayer | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.uuid.UUID | ||
import model.createRoom | ||
import model.listRoom | ||
import model.nameRoom | ||
|
||
fun Application.configureRooms() { | ||
routing { | ||
authenticate { | ||
route("rooms") { | ||
post { call.respond(listRoom()) } | ||
post("create") { | ||
call.respond(createRoom(call.getPlayer())) | ||
post("create") { call.respond(createRoom(call.getPlayer())) } | ||
post("join/{uuid}") { | ||
call.parameters["uuid"] | ||
//todo | ||
call.respond(HttpStatusCode.OK) | ||
} | ||
post("{uuid}/name") { | ||
call.respond(nameRoom(UUID(call.parameters["uuid"]!!))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,73 @@ | ||
package application | ||
|
||
import io.ktor.serialization.kotlinx.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.websocket.* | ||
import io.ktor.util.reflect.* | ||
import io.ktor.websocket.* | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.uuid.UUID | ||
import model.Room | ||
import model.Rooms | ||
import network.PacketController | ||
import network.PacketFrame | ||
import network.ServerPacket | ||
import network.ServerPacket.GET_ROOM_NUMBER | ||
import network.packet | ||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import java.time.Duration | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KTypeProjection | ||
import kotlin.reflect.full.allSupertypes | ||
import kotlin.reflect.full.createType | ||
import kotlin.reflect.javaType | ||
|
||
val serialFormat = Json | ||
|
||
fun Application.configureWebsocket() { | ||
install(WebSockets) { | ||
val _15sec = Duration.ofSeconds(15) | ||
pingPeriod = _15sec | ||
timeout = _15sec | ||
contentConverter = KotlinxWebsocketSerializationConverter(serialFormat) | ||
} | ||
routing { | ||
webSocket { | ||
for (frame in incoming) { | ||
frame.readBytes() | ||
val packet = receiveDeserialized<PacketFrame>() | ||
val serverPacketCT = serverPacket(ServerPacket.values()[packet.type]) | ||
serverPacketCT.invoke(serverPacketCT.decode(packet.data)!!) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@OptIn(InternalSerializationApi::class, ExperimentalStdlibApi::class, ExperimentalSerializationApi::class) | ||
private fun PacketController<*>.decode(data: String): Any? { | ||
val type = clazz.createType(clazz.allSupertypes.map { KTypeProjection(null, it) }) | ||
val typeInfo = typeInfoImpl(type.javaType, clazz, type) | ||
val serializer = serialFormat.serializersModule.serializerForTypeInfo(typeInfo) | ||
@Suppress("UNCHECKED_CAST") | ||
return serialFormat.decodeFromString(serializer as KSerializer<Any?>, data) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun serverPacket(serverPacket: ServerPacket): PacketController<Any> = when(serverPacket) { | ||
GET_ROOM_NUMBER -> packet<UUID> { | ||
transaction { Room.find(Rooms.id eq it).first().name } | ||
} | ||
ServerPacket.LEAVE_ROOM -> packet { | ||
|
||
} | ||
} as PacketController<Any> | ||
|
||
inline fun <reified T : Any> transactionPacket(crossinline code: (T) -> Any) = object : PacketController<T> { | ||
override fun invoke(t: T) = transaction { code(t) } | ||
override val clazz: KClass<T> = T::class | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package model | ||
|
||
import kotlinx.uuid.UUID | ||
import kotlinx.uuid.exposed.KotlinxUUIDEntity | ||
import kotlinx.uuid.exposed.KotlinxUUIDEntityClass | ||
import kotlinx.uuid.exposed.KotlinxUUIDTable | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
object JoinSessions : KotlinxUUIDTable() { | ||
val room = reference("room", Rooms) | ||
val player = reference("player", Players) | ||
init { let { transaction { SchemaUtils.create(it) } } } | ||
} | ||
|
||
class JoinSession(id: EntityID<UUID>) : KotlinxUUIDEntity(id) { | ||
companion object : KotlinxUUIDEntityClass<JoinSession>(JoinSessions) | ||
var room by JoinSessions.room | ||
var player by JoinSessions.player | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.