Player picking

This commit is contained in:
Lett Osprey 2026-04-05 17:12:08 +02:00
parent 5663b61906
commit 987e2b5576
3 changed files with 226 additions and 80 deletions

View file

@ -28,8 +28,8 @@ pub fn initialize(state_handler: Started(Subject(StateControl))) {
state.rooms |> list.key_find(id)
{
Error(_) -> {
// Prevent overflowing server with rooms, set max 200
case list.length(state.rooms) < 200 {
// Prevent overflowing server with rooms, set max 50
case list.length(state.rooms) < 50 {
True -> {
// Room not found (not really an error case), create it.
let name = process.new_name("quiz-registry" <> id)

View file

@ -4,7 +4,9 @@ import gleam/dynamic/decode
import gleam/erlang/process.{type Subject}
import gleam/http
import gleam/int
import gleam/json
import gleam/list
import gleam/option
import gleam/otp/actor.{type Started}
import shared/message.{type ClientsServer, type RoomControl, type StateControl}
import web/handlers/serve.{board, main_html, room, slow, status_head}
@ -17,7 +19,8 @@ pub fn handle_request(
) -> Response {
use req <- middleware(req)
case wisp.path_segments(req) {
["api", ..path] -> handle_api(state_handler, req, path)
["api", "room", ..path] -> handle_room_api(room_handler, req, path)
["api", ..path] -> handle_admin_api(state_handler, req, path)
_ -> handle_html(room_handler, req)
}
}
@ -38,7 +41,21 @@ fn handle_html(
|> main_html
}
fn handle_api(
fn handle_room_api(
room_handler: Started(Subject(RoomControl(ClientsServer))),
req: Request,
path: List(String),
) {
use json <- wisp.require_json(req)
case req.method, path {
http.Post, ["players"] -> fetch_players(room_handler, json)
_, _ -> #(404, "bad api path", "Resource not found")
}
|> serve.create_json_response
}
fn handle_admin_api(
actor: Started(Subject(StateControl)),
req: Request,
path: List(String),
@ -58,15 +75,15 @@ fn handle_api(
decode_index_to_text(actor, json, message.SetQuestion)
http.Post, ["answers"] ->
decode_index_to_text(actor, json, message.SetAnswer)
_, _ -> #(404, "bad api apth","Resource not found")
_, _ -> #(404, "bad api path", "Resource not found")
}
False -> {
#(401, "invalid api key","unauthorized")
#(401, "invalid api key", "unauthorized")
}
}
}
Error(_) -> {
#(401, "missing api key","unauthorized")
#(401, "missing api key", "unauthorized")
}
}
|> serve.create_json_response
@ -85,7 +102,35 @@ fn decode_info(
actor.send(actor.data, info)
#(200, "Updated info", "Updated info")
}
Error(_) -> #(400, "Unable to update info","bad request")
Error(_) -> #(400, "Unable to update info", "bad request")
}
}
fn fetch_players(
room_handler: Started(Subject(RoomControl(ClientsServer))),
json_string: decode.Dynamic,
) {
let decode_uri = {
use id <- decode.field("id", decode.string)
//use key <- decode.field("key", decode.string)
decode.success(message.FetchRoom(id, _))
}
case decode.run(json_string, decode_uri) {
Ok(room) -> {
case actor.call(room_handler.data, 1000, room) {
option.Some(#(_, player_handler)) -> #(
200,
json.to_string(json.object([#("id", json.string("10"))])),
json.to_string(json.object([#("id", json.string("10"))])),
)
option.None -> #(
404,
"Room not found, or key invalid",
"resource not found",
)
}
}
Error(fault) -> #(400, "Unable to fetch players", "bad request")
}
}
@ -105,9 +150,13 @@ fn decode_index_to_text(
list.each(answers, fn(answer_question) {
actor.send(actor.data, answer_question)
})
#(200, "imported " <> int.to_string(list.length(answers)) <> " items.","imported " <> int.to_string(list.length(answers)) <> " items.")
#(
200,
"imported " <> int.to_string(list.length(answers)) <> " items.",
"imported " <> int.to_string(list.length(answers)) <> " items.",
)
}
Error(_) -> #(400, "Failed to import","bad request")
Error(_) -> #(400, "Failed to import", "bad request")
}
}