From c274f6b807aebbcda28449960ce88e9b0886dc7b Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Tue, 24 Feb 2026 16:46:48 -0800 Subject: [PATCH] while .next() on db cache is infinite --- docs/queries.rs | 6 ------ jong/src/riichi.rs | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) delete mode 100644 docs/queries.rs diff --git a/docs/queries.rs b/docs/queries.rs deleted file mode 100644 index 522720d..0000000 --- a/docs/queries.rs +++ /dev/null @@ -1,6 +0,0 @@ -commands: Commands - -tiles: Query<(&Tile, &TileId, Entity)>, -player: Option>, -hand_ent: Option>>, - diff --git a/jong/src/riichi.rs b/jong/src/riichi.rs index de16274..93de3f5 100644 --- a/jong/src/riichi.rs +++ b/jong/src/riichi.rs @@ -83,10 +83,19 @@ fn subscriptions(stdb: SpacetimeDB, mut commands: Commands) { // commands.queue(command); let (send, recv) = std::sync::mpsc::channel::(); stdb.subscription_builder() - .on_applied(move |_| send.send(Subscribed).unwrap()) - .on_error(|_, err| error!("sub failed: {err}")) + .on_applied(move |_| { + trace!("subs succeeded"); + send.send(Subscribed).unwrap(); + }) + .on_error(|_, err| { + error!("subs failed: {err}"); + }) .subscribe([ // TODO change these to sub/unsub based on being in lobby and some such + format!( + "SELECT p.* FROM player p WHERE p.identity = '{}'", + stdb.identity() + ), "SELECT p.* FROM player p JOIN lobby l ON p.lobby_id = l.id".to_string(), "SELECT l.* FROM lobby l JOIN player p ON l.id = p.lobby_id".to_string(), "SELECT c.* FROM player_clock c JOIN player p ON c.player_id = p.id".to_string(), @@ -111,14 +120,18 @@ fn on_subscribed( mut next_gamestate: ResMut>, mut next_turnstate: ResMut>, ) { - while let Some(player) = stdb.db().player().iter().next() { + trace!("on_subscribed"); + for player in stdb.db().player().iter() { if player.identity == stdb.identity() { + // trace!("spawn_main_player"); spawn_main_player(&stdb, &mut commands, &mut next_turnstate, &player); } else { + // trace!("spawn_other_player"); spawn_other_player(&stdb, &mut commands, &player); } } - while let Some(bot) = stdb.db().bot().iter().next() { + + for bot in stdb.db().bot().iter() { let id = PlayerOrBot::Bot { id: bot.id }; let hand_view = stdb .db() @@ -143,7 +156,7 @@ fn spawn_main_player( player: &jong_db::Player, ) { - trace!("spawn_main_player"); + // trace!("spawn_main_player"); let main_player = commands .spawn(( Player { @@ -176,9 +189,15 @@ fn spawn_main_hand( .collect(); let pond = commands.spawn(Hand).add_children(&pond_tiles).id(); let hand = commands.spawn(Pond).add_children(&hand_tiles).id(); - let children = commands.entity(main_player).add_children(&[pond, hand]).id(); + let children = commands + .entity(main_player) + .add_children(&[pond, hand]) + .id(); - debug!("main_hand: {:?}\n main_pond: {:?}, children: {:?}", hand_tiles, pond_tiles, children); + debug!( + "main_hand: {:?}\n main_pond: {:?}, children: {:?}", + hand_tiles, pond_tiles, children + ); if player_hand.turn_state == jong_db::TurnState::Tsumo && let Some(drawn_dbt) = &player_hand.working_tile @@ -216,8 +235,9 @@ fn on_player_insert_update( mut next_turnstate: ResMut>, ) { for msg in messages.read() { + debug!("on_player_insert_update: {:?}", msg.new); if main_player.is_none() && msg.new.identity == stdb.identity() { - trace!("spawn_main_player"); + // trace!("spawn_main_player"); spawn_main_player(&stdb, &mut commands, &mut next_turnstate, &msg.new); } else if other_players.iter().any(|p| { if let PlayerOrBot::Player { id } = &p.id { @@ -226,6 +246,7 @@ fn on_player_insert_update( false } }) { + trace!("spawn_other_player"); spawn_other_player(&stdb, &mut commands, &msg.new); } else { // TODO update case