use log::info; use spacetimedb::{Identity, ReducerContext, Table, rand::seq::SliceRandom, reducer, table}; use jong_types::*; #[derive(Debug)] #[table(name = player, public)] pub struct Player { #[primary_key] identity: Identity, #[auto_inc] #[index(direct)] #[unique] id: u32, name: Option, #[index(btree)] lobby_id: u32, } #[table(name = bot)] pub struct Bot { #[primary_key] #[auto_inc] id: u32, #[index(btree)] lobby_id: u32, } #[derive(Debug)] #[table(name = lobby, public)] pub struct Lobby { #[primary_key] #[auto_inc] id: u32, #[index(direct)] #[unique] host_id: u32, game_state: GameState, } #[table(name = wall)] pub struct Wall { #[primary_key] #[auto_inc] id: u32, #[index(direct)] #[unique] lobby_id: u32, tiles: Vec, } #[table(name = hand)] pub struct Hand { #[primary_key] player_ident: Identity, tiles: Vec, } #[table(name = pond, public)] pub struct Pond { tiles: Vec, } #[reducer(client_connected)] pub fn login_or_add_player(ctx: &ReducerContext) { let identity = ctx.sender; // TODO remove player on disconnect if let Ok(player) = ctx.db.player().try_insert(Player { identity, id: 0, name: None, lobby_id: 0, }) { info!("added player: {:?}", player); } else { let player = ctx.db.player().identity().find(identity).unwrap(); info!("player {:?} has reconnected", player) } } #[reducer] pub fn join_or_create_lobby(ctx: &ReducerContext, lobby: Option) -> Result<(), String> { let mut player = ctx .db .player() .identity() .find(ctx.sender) .ok_or(format!("cannot find player {}", ctx.sender))?; let lobby_id = lobby.unwrap_or_else(|| { let lobby = ctx.db.lobby().insert(Lobby { id: 0, host_id: player.id, game_state: GameState::None, }); info!("created lobby: {:?}", lobby); lobby.id }); player.lobby_id = lobby_id; let player = ctx.db.player().identity().update(player); info!("player {} joined lobby {}", player.id, lobby_id); Ok(()) } #[reducer] pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> { if lobby_id == 0 { Err("cannot add a bot without a lobby".into()) } else if let Some(lobby) = ctx.db.lobby().id().find(lobby_id) && (ctx.db.player().lobby_id().filter(lobby_id).count() + ctx.db.bot().lobby_id().filter(lobby_id).count() <= 4) { let bot = ctx.db.bot().insert(Bot { id: 0, lobby_id }); info!("added bot {} to lobby {}", bot.id, lobby_id); Ok(()) } else { Err("lobby doesn't exist".into()) } } #[reducer] pub fn shuffle_wall(ctx: &ReducerContext) { // if let Some(wall) = // let mut rng = ctx.rng(); // let mut wall = tiles(); // wall.shuffle(&mut rng); todo!() } #[reducer] pub fn deal_hands(ctx: &ReducerContext) { todo!() } #[reducer] pub fn sort_hand(ctx: &ReducerContext) { todo!() } // #[reducer(init)] // pub fn init(_ctx: &ReducerContext) { // // Called when the module is initially published // } // #[reducer(client_connected)] // pub fn identity_connected(_ctx: &ReducerContext) { // // Called everytime a new client connects // } // #[reducer(client_disconnected)] // pub fn identity_disconnected(_ctx: &ReducerContext) { // // Called everytime a client disconnects // } // #[reducer] // pub fn add(ctx: &ReducerContext, name: String) { // ctx.db.player().insert(Player { name }); // } // #[reducer] // pub fn say_hello(ctx: &ReducerContext) { // for person in ctx.db.person().iter() { // log::info!("Hello, {}!", person.name); // } // log::info!("Hello, World!"); // } // #[reducer] // pub fn set_name(ctx: &ReducerContext, name: String) -> Result<(), String> { // if name.is_empty() { // return Err("names must not be empty".into()); // } // if let Some(player) = ctx.db.player().identity().find(ctx.sender) { // ctx.db.player().identity().update(Player { // name: Some(name), // ..player // }); // Ok(()) // } else { // Err("cannot set name for unknown user".into()) // } // }