From 9f6a5b6423c767b3b90ce8d329e381476d284cad Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Sun, 8 Feb 2026 23:05:22 -0800 Subject: [PATCH] temporarily sub all, then react --- jong/src/game.rs | 110 ++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 69 deletions(-) diff --git a/jong/src/game.rs b/jong/src/game.rs index 6b6b470..d00e50c 100644 --- a/jong/src/game.rs +++ b/jong/src/game.rs @@ -66,7 +66,7 @@ impl Plugin for Riichi { // .add_sub_state::() // .init_resource::() // .init_resource::() - // .add_message::() + .add_message::() .add_systems(Startup, tile::init_tiles) // .add_systems(OnEnter(GameState::Setup), setup) // .add_systems(OnEnter(GameState::Deal), hand::shuffle_deal) @@ -79,30 +79,24 @@ impl Plugin for Riichi { // .add_systems(Update, round::ron_chi_pon_kan.run_if(in_state(TurnState::RonChiiPonKan)).after(round::notify_callable)) // .add_systems(OnEnter(TurnState::End), round::end) // stdb - .init_resource::() + .add_systems(Startup, sub_to_all) .add_systems(Update, on_connect) .add_systems(Update, on_disconnect) - .add_systems(Update, on_player_insert_update) - .add_systems(Update, on_lobby_insert_update) + .add_systems(Update, (on_player_insert_update, on_lobby_insert_update).chain()) .add_systems(OnEnter(GameState::Setup), join_or_create_lobby) - .add_systems(Update, (view_hand).run_if(in_state(GameState::Play))) + // .add_systems(Update, (view_hand).run_if(in_state(GameState::Play))) // semicolon stopper ; } } -fn view_hand(stdb: SpacetimeDB) { - // stdb.db() - // .hand() - // .player_identity() - // .find(&stdb.identity()) - // .unwrap(); +fn sub_to_all(stdb: SpacetimeDB) { + stdb.subscription_builder() + .on_applied(|_| trace!("made all subs!")) + .on_error(|_, err| error!("sub failed: {err}")) + .subscribe_to_all_tables(); } -#[derive(Resource, Deref)] -struct Player(stdb::Player); - -// TODO or reconnect? fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, mut commands: Commands) { for msg in messages.read() { info!("you're now jongline"); @@ -110,25 +104,19 @@ fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, mut com creds_store() .save(&msg.access_token) .expect("i/o error saving token"); - - stdb.subscription_builder() - .on_applied(|ctx| { - trace!("subbed to player table"); - }) - .on_error(|_, err| error!("sub to player failed: {err}")) - .subscribe(format!( - "SELECT * FROM player p WHERE p.identity = '{}'", - stdb.identity() - )); } } +// TODO how reconnect? fn on_disconnect(stdb: SpacetimeDB, mut messages: ReadStdbDisconnectedMessage) { for msg in messages.read() { warn!("lost connection: {:#?}", msg.err); } } +#[derive(Resource, Deref)] +struct Player(stdb::Player); + fn on_player_insert_update( stdb: SpacetimeDB, mut messages: ReadInsertUpdateMessage, @@ -154,47 +142,14 @@ fn on_player_insert_update( } } -fn join_or_create_lobby(stdb: SpacetimeDB, player: Res, mut lobby_id: ResMut) { - let sub = stdb - .subscription_builder() - .on_applied(|_| { - trace!("subbed to lobby table"); - }) - .on_error(|_, err| error!("sub to lobby table failed: {err}")) - .subscribe([format!( - "SELECT l.* - FROM lobby l - WHERE l.host_player_id = {}", - player.id - )]); - +fn join_or_create_lobby(stdb: SpacetimeDB, player: Res) { let mut player = player.clone(); if player.lobby_id == 0 { stdb.reducers().join_or_create_lobby(0).unwrap(); - player.lobby_id = stdb - .db() - .lobby() - .host_player_id() - .find(&player.id) - .unwrap_or_else(|| panic!("can't find player with id {}", player.id)) - .id; + } else { + info!("in lobby: {}", player.lobby_id) } - - if let Some(lobby) = stdb.db().lobby().id().find(&player.lobby_id) - && lobby.host_player_id == player.id - && matches!(lobby.game_state.into(), GameState::None) - { - debug!("setup_game({})", lobby.id); - stdb.reducers().setup_game(lobby.id).unwrap() - } - - *lobby_id = LobbyId(player.lobby_id); - stdb.reducers().setup_game(**lobby_id).unwrap(); - - stdb.subscription_builder() - .on_applied(|_| trace!("subbed to view_hand")) - .subscribe("SELECT * FROM view_hand"); } impl From for GameState { @@ -209,23 +164,40 @@ impl From for GameState { } } -#[derive(Resource, Default, Deref)] -struct LobbyId(u32); - fn on_lobby_insert_update( stdb: SpacetimeDB, mut messages: ReadInsertUpdateMessage, + player: Option>, + mut commands: Commands, mut next_gamestate: ResMut>, - - lobby_id: Res, ) { for msg in messages.read() { - // TODO should this be an assert? - if msg.new.id == **lobby_id { - debug!("lobby: {:?}", msg.new); + if let Some(player) = player.as_ref() + && player.lobby_id == msg.new.id + { + debug!("on_lobby_insert_update: {:#?}", msg.new); next_gamestate.set(msg.new.game_state.into()); + + match msg.new.game_state { + stdb::GameState::None => { + trace!("setup game"); + stdb.reducers().setup_game(player.lobby_id).unwrap(); + } + stdb::GameState::Setup => { + trace!("game entered setup"); + } + stdb::GameState::Deal => { + trace!("game entered deal") + } + stdb::GameState::Play => { + trace!("game entered play") + } + stdb::GameState::Exit => { + trace!("game enetered exit") + } + } } } }