use log::{debug, info}; use spacetimedb::{ Identity, ReducerContext, Table, ViewContext, rand::seq::SliceRandom, reducer, table, view, }; 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, Clone, Copy)] #[table(name = lobby, public)] pub struct Lobby { #[primary_key] #[auto_inc] id: u32, #[index(direct)] #[unique] host_player_id: u32, game_state: GameState, } #[table(name = wall)] pub struct Wall { // #[auto_inc] // id: u32, #[primary_key] // #[index(direct)] // #[unique] lobby_id: u32, tiles: Vec, } #[table(name = hand)] pub struct Hand { #[primary_key] player_identity: Identity, tiles: Vec, } #[table(name = bothand)] pub struct BotHand { #[primary_key] bot_id: u32, 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, mut lobby_id: u32) -> Result<(), String> { let mut player = ctx .db .player() .identity() .find(ctx.sender) .ok_or(format!("cannot find player {}", ctx.sender))?; if lobby_id == 0 { let lobby = ctx.db.lobby().insert(Lobby { id: 0, host_player_id: player.id, game_state: GameState::None, }); info!("created lobby: {:?}", lobby); lobby_id = 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 setup_game(ctx: &ReducerContext, lobby_id: u32) { debug!("lobby_id: {lobby_id}"); let mut lobby = ctx.db.lobby().id().find(lobby_id).unwrap(); lobby.game_state = GameState::Setup; ctx.db.lobby().id().update(lobby); let tiles = new_shuffled_wall(ctx); ctx.db.wall().insert(Wall { // id: 0, lobby_id, tiles, }); lobby.game_state = GameState::Deal; ctx.db.lobby().id().update(lobby); deal_hands(ctx, lobby_id); lobby.game_state = GameState::Play; ctx.db.lobby().id().update(lobby); } pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec { let mut rng = ctx.rng(); let mut wall = tiles(); wall.shuffle(&mut rng); wall } pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) { let players = ctx.db.player().lobby_id().filter(lobby_id); let bots = ctx.db.bot().lobby_id().filter(lobby_id); let mut wall = ctx.db.wall().lobby_id().find(lobby_id).unwrap(); // FIXME rectify deal orders for player in players { let tiles = wall.tiles.split_off(wall.tiles.len() - 13); wall = ctx.db.wall().lobby_id().update(wall); ctx.db.hand().insert(Hand { player_identity: player.identity, tiles, }); } for bot in bots { let tiles = wall.tiles.split_off(wall.tiles.len() - 13); wall = ctx.db.wall().lobby_id().update(wall); ctx.db.bothand().insert(BotHand { bot_id: bot.id, tiles, }); } } #[view(name = view_player_hand, public)] pub fn view_player_hand(ctx: &ViewContext) -> Option { ctx.db.hand().player_identity().find(ctx.sender) } #[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()) // } // }