session handling for players and hosts

This commit is contained in:
2025-05-17 12:23:19 -06:00
parent fee5576efd
commit b6a4936247
11 changed files with 221 additions and 28 deletions
+21
View File
@@ -0,0 +1,21 @@
import gleam/dynamic/decode
import gleam/json
pub type PlayerSession {
PlayerSession(id: Int, token_hash: String, username: String)
}
pub fn encode_player_session(player_session: PlayerSession) -> json.Json {
json.object([
#("id", json.int(player_session.id)),
#("token_hash", json.string(player_session.token_hash)),
#("username", json.string(player_session.username)),
])
}
pub fn player_session_decoder() -> decode.Decoder(PlayerSession) {
use id <- decode.field("id", decode.int)
use token_hash <- decode.field("token_hash", decode.string)
use username <- decode.field("username", decode.string)
decode.success(PlayerSession(id:, token_hash:, username:))
}
+3 -3
View File
@@ -4,12 +4,12 @@ import gleam/list
import player
pub type Session {
Session(id: Int, host_user_id: Int, players: List(player.Player))
Session(id: String, host_user_id: Int, players: List(player.Player))
}
pub fn serialize(session: Session) -> json.Json {
json.object([
#("id", json.int(session.id)),
#("id", json.string(session.id)),
#("host_user_id", json.int(session.host_user_id)),
#(
"players",
@@ -22,7 +22,7 @@ pub fn serialize(session: Session) -> json.Json {
}
pub fn decoder() -> decode.Decoder(Session) {
use id <- decode.field("id", decode.int)
use id <- decode.field("id", decode.string)
use host_user_id <- decode.field("host_user_id", decode.int)
use players <- decode.field("players", decode.list(player.decoder()))