jong/jong-line/src/tables.rs

135 lines
2.3 KiB
Rust
Raw Normal View History

2026-02-22 00:39:51 -08:00
use spacetimedb::{SpacetimeType, ViewContext, table, view};
use jong_types::{
states::{GameState, TurnState},
2026-02-20 03:20:29 -08:00
tiles::Tile,
};
2026-02-20 15:36:04 -08:00
use crate::reducers::advance_game;
#[derive(Debug, Clone)]
#[table(name = lobby, public)]
pub struct Lobby {
#[primary_key]
#[auto_inc]
pub id: u32,
pub players: Vec<PlayerOrBot>,
2026-02-13 08:16:41 -08:00
pub dealer_idx: u8,
pub current_idx: u8,
pub game_state: GameState,
}
#[table(name = wall)]
2026-02-12 17:06:28 -08:00
pub struct DbWall {
#[primary_key]
pub lobby_id: u32,
2026-02-12 17:06:28 -08:00
pub tiles: Vec<DbTile>,
}
#[table(name = tile)]
2026-02-13 07:49:09 -08:00
#[derive(Debug, Clone, Copy)]
2026-02-12 17:06:28 -08:00
pub struct DbTile {
#[primary_key]
#[auto_inc]
pub id: u32,
pub tile: Tile,
}
#[derive(Debug, Clone, SpacetimeType)]
pub enum PlayerOrBot {
Player { id: u32 },
Bot { id: u32 },
}
2026-02-20 03:20:29 -08:00
#[table(name = player, public)]
2026-02-20 15:36:04 -08:00
#[table(name = logged_out_player)]
2026-02-20 03:20:29 -08:00
#[derive(Debug)]
pub struct Player {
#[unique]
#[auto_inc]
pub id: u32,
2026-02-20 15:36:04 -08:00
#[primary_key]
pub identity: spacetimedb::Identity,
2026-02-20 03:20:29 -08:00
pub name: Option<String>,
#[index(btree)]
pub lobby_id: u32,
pub ready: bool,
pub sort: bool,
2026-02-20 15:36:04 -08:00
}
#[table(name = player_clock, public)]
pub struct PlayerClock {
#[primary_key]
pub id: u32,
#[unique]
pub player_id: u32,
pub renewable: u16,
pub total: u16,
}
#[table(name = player_hand)]
pub struct PlayerHand {
#[primary_key]
#[auto_inc]
pub id: u32,
#[unique]
pub player_id: u32,
pub turn_state: TurnState,
2026-02-20 03:20:29 -08:00
pub pond: Vec<DbTile>,
2026-02-20 15:36:04 -08:00
pub hand: Vec<DbTile>,
2026-02-20 03:20:29 -08:00
2026-02-20 15:36:04 -08:00
/// drawn or callable tile
pub working_tile: Option<DbTile>,
2026-02-20 03:20:29 -08:00
}
2026-02-22 00:39:51 -08:00
#[table(name = bot, public)]
2026-02-20 03:20:29 -08:00
pub struct Bot {
#[primary_key]
#[auto_inc]
pub id: u32,
#[index(btree)]
pub lobby_id: u32,
2026-02-20 15:36:04 -08:00
pub turn_state: TurnState,
2026-02-20 03:20:29 -08:00
pub hand: Vec<DbTile>,
pub pond: Vec<DbTile>,
2026-02-20 15:36:04 -08:00
pub working_tile: Option<DbTile>,
}
#[table(name = game_timer, scheduled(advance_game))]
pub struct GameTimer {
#[primary_key]
#[auto_inc]
pub id: u64,
#[unique]
pub lobby_id: u32,
pub scheduled_at: spacetimedb::ScheduleAt,
2026-02-20 03:20:29 -08:00
}
2026-02-22 00:39:51 -08:00
#[view(name = view_hand, public)]
fn view_hand(ctx: &ViewContext) -> Option<PlayerHand> {
ctx.db
.player()
.identity()
.find(ctx.sender)
.and_then(|p| ctx.db.player_hand().player_id().find(p.id))
}