Add preliminary support for multiple card games
CI / Build and push docker image (push) Successful in 3m5s

A game-type registry (server/src/tavolo/games.py) is now the single
source of truth for the games the platform can host; only scopone
scientifico is registered so far. GET /api/game-types exposes it for the
lobby's new game dropdown, and POST /api/games accepts a validated
game_type (default scopone_scientifico) which is carried on the live
GameState and onto each finished Match row (new indexed column,
migration 1_20260916235833_update), so statistics can be scoped per
game: /api/me/matches and /api/leaderboard take an optional game_type
filter and every serialized match includes its game_type.

Game states serialized before this change still load with the default
game type.
This commit is contained in:
2026-09-17 08:26:46 +08:00
parent 876b4abd8b
commit ab4130a4ca
15 changed files with 376 additions and 28 deletions
+15 -2
View File
@@ -22,9 +22,22 @@ pub async fn me() -> Result<Option<User>, String> {
resp.json().await.map(Some).map_err(|e| e.to_string())
}
pub async fn create_game(target_score: i32) -> Result<GameView, String> {
/// Fetch the card games the platform can host (for the creation dropdown).
pub async fn game_types() -> Result<Vec<GameTypeInfo>, String> {
let resp = Request::get("/api/game-types")
.send()
.await
.map_err(|e| e.to_string())?;
if !resp.ok() {
return Err(server_error(resp.status()));
}
let page: GameTypesPage = resp.json().await.map_err(|e| e.to_string())?;
Ok(page.results)
}
pub async fn create_game(game_type: &str, target_score: i32) -> Result<GameView, String> {
let resp = Request::post("/api/games")
.json(&serde_json::json!({ "target_score": target_score }))
.json(&serde_json::json!({ "game_type": game_type, "target_score": target_score }))
.map_err(|e| e.to_string())?
.send()
.await
+21
View File
@@ -101,6 +101,9 @@ pub struct GameView {
pub id: String,
#[serde(default)]
pub join_code: String,
/// Which card game this match is (id from /api/game-types).
#[serde(default)]
pub game_type: String,
pub phase: String,
#[serde(default)]
pub target_score: i32,
@@ -174,6 +177,8 @@ pub struct MatchPlayer {
#[allow(dead_code)]
pub struct MatchSummary {
pub id: String,
#[serde(default)]
pub game_type: String,
pub team_a_score: i32,
pub team_b_score: i32,
pub winner_team: String,
@@ -211,6 +216,22 @@ pub struct LeaderboardPage {
pub results: Vec<LeaderboardEntry>,
}
/// A card game the platform can host (GET /api/game-types).
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[allow(dead_code)]
pub struct GameTypeInfo {
pub id: String,
pub name: String,
#[serde(default)]
pub description: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct GameTypesPage {
#[serde(default)]
pub results: Vec<GameTypeInfo>,
}
/// Map a card code (e.g. `07D`) to its asset path.
pub fn card_asset(code: &str) -> String {
format!("/assets/cards/{code}.svg")
+32 -2
View File
@@ -4,7 +4,16 @@ use sycamore::prelude::*;
use sycamore_router::navigate;
use crate::api;
use crate::model::User;
use crate::model::{GameTypeInfo, User};
/// Used when the game-types fetch fails: match creation must still work.
fn fallback_game_types() -> Vec<GameTypeInfo> {
vec![GameTypeInfo {
id: "scopone_scientifico".to_string(),
name: "Scopone scientifico".to_string(),
description: String::new(),
}]
}
#[component]
pub fn LobbyPage() -> View {
@@ -12,6 +21,8 @@ pub fn LobbyPage() -> View {
let user = create_signal(Option::<Option<User>>::None);
let error = create_signal(Option::<String>::None);
let code = create_signal(String::new());
let game_types = create_signal(fallback_game_types());
let selected_game = create_signal("scopone_scientifico".to_string());
spawn_local(async move {
match api::me().await {
@@ -23,9 +34,20 @@ pub fn LobbyPage() -> View {
}
});
spawn_local(async move {
match api::game_types().await {
Ok(types) if !types.is_empty() => {
selected_game.set(types[0].id.clone());
game_types.set(types);
}
_ => {} // keep the scopone fallback
}
});
let on_create = move |target: i32| {
let game_type = selected_game.get_clone();
spawn_local(async move {
match api::create_game(target).await {
match api::create_game(&game_type, target).await {
Ok(game) => navigate(&format!("/game/{}", game.id)),
Err(e) => error.set(Some(e)),
}
@@ -70,6 +92,14 @@ pub fn LobbyPage() -> View {
}
div(class="panel") {
h2 { "New match" }
label(r#for="game-type") { "Game" }
select(id="game-type", bind:value=selected_game) {
Keyed(
list=game_types,
view=|g| view! { option(value=g.id.clone()) { (g.name) } },
key=|g| g.id.clone(),
)
}
p { "First team to reach the target score wins." }
div(class="target-buttons") {
button(class="button", on:click=move |_| on_create(11)) { "Target 11" }