(wip) crash in render_hand, hand doesn't seem to be spawned

This commit is contained in:
Tao Tien 2026-02-24 15:02:32 -08:00
parent e2d554f321
commit b512e38b26
3 changed files with 65 additions and 57 deletions

View file

@ -1,15 +1,14 @@
use bevy::platform::collections::HashMap;
use bevy::prelude::*;
use bevy_spacetimedb::{
ReadInsertUpdateMessage, ReadStdbConnectedMessage, ReadStdbDisconnectedMessage,
ReadUpdateMessage, StdbPlugin,
};
use jong_db::{self, add_bot, set_ready};
use jong_db::{
self, BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess,
RemoteTables, ViewClosedHandsTableAccess, ViewHandTableAccess as _,
BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables,
ViewClosedHandsTableAccess, ViewHandTableAccess,
};
use jong_db::{add_bot, set_ready};
use jong_types::*;
use spacetimedb_sdk::Table;
@ -46,8 +45,7 @@ impl Plugin for Riichi {
.add_systems(Startup, subscriptions)
.add_observer(on_subscribed)
.add_systems(Update, (on_connect, on_disconnect))
.add_systems(Update, on_lobby_insert_update)
.add_systems(Update, on_player_insert_update)
.add_systems(Update, (on_lobby_insert_update, on_player_insert_update))
.add_systems(
Update,
(on_view_hand_update)
@ -56,7 +54,7 @@ impl Plugin for Riichi {
}
}
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, _commands: Commands) {
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage) {
for msg in messages.read() {
info!("you're now jongline");
@ -139,10 +137,13 @@ fn on_subscribed(
fn spawn_main_player(
stdb: &SpacetimeDB,
commands: &mut Commands,
next_turnstate: &mut ResMut<NextState<TurnState>>,
player: &jong_db::Player,
) {
trace!("spawn_main_player");
let main_player = commands
.spawn((
Player {
@ -152,6 +153,17 @@ fn spawn_main_player(
))
.id();
if let Some(player_hand) = stdb.db().view_hand().iter().next() {
spawn_main_hand(commands, next_turnstate, main_player, &player_hand);
}
}
fn spawn_main_hand(
commands: &mut Commands,
next_turnstate: &mut ResMut<NextState<TurnState>>,
main_player: Entity,
player_hand: &PlayerHand,
) {
let hand_tiles: Vec<_> = player_hand
.hand
.iter()
@ -164,10 +176,12 @@ fn spawn_main_player(
.collect();
let pond = commands.spawn(Hand).add_children(&pond_tiles).id();
let hand = commands.spawn(Pond).add_children(&hand_tiles).id();
commands.entity(main_player).add_children(&[pond, hand]);
let children = commands.entity(main_player).add_children(&[pond, hand]).id();
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
&& let Some(drawn_dbt) = &player_hand.working_tile
{
let drawn = commands
.spawn((Drawn, Tile::from(&drawn_dbt.tile), TileId(drawn_dbt.id)))
@ -176,7 +190,6 @@ fn spawn_main_player(
}
next_turnstate.set(player_hand.turn_state.into());
}
}
fn spawn_other_player(stdb: &SpacetimeDB, commands: &mut Commands, player: &jong_db::Player) {
let id = PlayerOrBot::Player { id: player.id };
@ -204,6 +217,7 @@ fn on_player_insert_update(
) {
for msg in messages.read() {
if main_player.is_none() && msg.new.identity == stdb.identity() {
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 {
@ -275,19 +289,23 @@ fn on_view_hand_update(
mut commands: Commands,
tiles: Query<(Entity, &TileId)>,
main_player: Single<&Children, With<MainPlayer>>,
main_player: Single<(Entity, Option<&Children>), With<MainPlayer>>,
hand: Query<Entity, With<Hand>>,
pond: Query<Entity, With<Pond>>,
// drawn: Option<Single<Entity, With<Drawn>>>,
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
) {
trace!("on_view_hand_update");
// TODO can this and similar run at startup or on play/reconnect?
for msg in messages.read() {
trace!("new hand: {:?}", msg.new);
if main_player.1.is_none() {
trace!("spawn_main_hand, {:?}", *main_player);
spawn_main_hand(&mut commands, &mut next_turnstate, main_player.0, &msg.new);
continue;
}
let hand_tiles: Vec<_> = msg
.new
.hand
@ -299,10 +317,6 @@ fn on_view_hand_update(
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
})
.collect();
commands
.entity(hand.iter().find(|e| main_player.contains(e)).unwrap())
.replace_children(&hand_tiles);
let pond_tiles: Vec<_> = msg
.new
.pond
@ -314,8 +328,20 @@ fn on_view_hand_update(
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
})
.collect();
commands
.entity(pond.iter().find(|e| main_player.contains(e)).unwrap())
.entity(
hand.iter()
.find(|e| main_player.1.is_some_and(|mp| mp.contains(e)))
.unwrap(),
)
.replace_children(&hand_tiles);
commands
.entity(
pond.iter()
.find(|e| main_player.1.is_some_and(|mp| mp.contains(e)))
.unwrap(),
)
.replace_children(&pond_tiles);
match msg.new.turn_state {

View file

@ -88,7 +88,6 @@ impl Plugin for TuiPlugin {
(render::render_main_hand, render::render_main_pond)
.run_if(in_state(GameState::Play)),
render::render,
query_tester,
)
.chain()
.in_set(TuiSet::Render),
@ -96,24 +95,6 @@ impl Plugin for TuiPlugin {
}
}
fn query_tester(
main_player: Single<&Children, With<MainPlayer>>,
// hand: Query<(&Children, Entity), With<Hand>>,
) {
trace!("owo");
// let hand = hand
// .iter()
// .find_map(|(c, e)| {
// if main_player.contains(&e) {
// Some(c)
// } else {
// None
// }
// })
// .unwrap();
// debug!("{hand:?}");
}
fn discard_tile(
stdb: SpacetimeDB,

View file

@ -115,6 +115,7 @@ pub(crate) fn render_main_hand(
let hand: Vec<_> = hand
.iter()
.find_map(|(c, e)| {
debug!("main_player children: {:?}", *main_player);
if main_player.contains(&e) {
Some(c)
} else {