From 875c3fb5bb11352a4715a89c45de6dd6d4aa1ed1 Mon Sep 17 00:00:00 2001 From: Tao Tien <29749622+taotien@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:00:26 -0800 Subject: [PATCH] discarding proper now --- .helix/ignore | 1 + jong-line/src/reducers/hand.rs | 38 ++++++++++++++++++++++++---------- jong/src/riichi.rs | 18 +++++++++------- jong/src/riichi/player.rs | 2 +- jong/src/tui.rs | 13 +----------- jong/src/tui/render.rs | 4 ++-- justfile | 9 ++------ 7 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 .helix/ignore diff --git a/.helix/ignore b/.helix/ignore new file mode 100644 index 0000000..25da40d --- /dev/null +++ b/.helix/ignore @@ -0,0 +1 @@ +jong-db diff --git a/jong-line/src/reducers/hand.rs b/jong-line/src/reducers/hand.rs index 06257e7..1e3f18a 100644 --- a/jong-line/src/reducers/hand.rs +++ b/jong-line/src/reducers/hand.rs @@ -46,17 +46,33 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> { let mut player = ctx.db.player().identity().find(ctx.sender).unwrap(); let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap(); - let dealt_tile = if let Some(drawn) = player.drawn_tile - && drawn.id == tile_id - { - drawn - } else if let Some((i, _)) = player - .hand - .iter() - .enumerate() - .find(|(_, t)| t.id == tile_id) - { - player.hand.remove(i) + let dealt_tile = if let Some(dealt) = ctx.db.tile().id().find(tile_id) { + if let Some(drawn) = player.drawn_tile { + if drawn.id == dealt.id { + dealt + } else if let Some((i, _)) = player + .hand + .iter() + .enumerate() + .find(|(_, t)| t.id == tile_id) + { + let dealt = player.hand.remove(i); + player.hand.push(drawn); + player.hand.sort_by_key(|t| t.tile); + + dealt + } else { + return Err(format!( + "player {} attempted to deal tile {} not in hand or drawn", + player.id, tile_id + )); + } + } else { + return Err(format!( + "player {} attempted to deal tile {} without having drawn", + player.id, tile_id + )); + } } else { return Err(format!( "player {} attempted to deal nonexistant tile {}", diff --git a/jong/src/riichi.rs b/jong/src/riichi.rs index cde1938..0cd9eb9 100644 --- a/jong/src/riichi.rs +++ b/jong/src/riichi.rs @@ -101,29 +101,33 @@ fn on_player_insert_update( mut messages: ReadInsertUpdateMessage, mut commands: Commands, - player: Option>>, hand: Option>>, + tiles: Query<(Entity, &TileId)>, ) { - let hand = if player.is_none() && hand.is_none() { + let hand = if hand.is_none() { let hand = commands.spawn(Hand).id(); - commands.spawn((Player, MainPlayer)).add_child(hand); hand } else { *hand.unwrap() }; for msg in messages.read() { - let tiles: Vec<_> = msg + let hand_tiles: Vec<_> = msg .new .hand .iter() .map(|dbt| { - // drawn tiles will always be new entities to us, until wall isn't fake - commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id() + tiles + .iter() + .find_map(|(e, t)| if *t == TileId(dbt.id) { Some(e) } else { None }) + .or_else(|| Some(commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())) + .unwrap() }) .collect(); - commands.entity(hand).replace_children(&tiles); + debug!("hand_tiles: {hand_tiles:?}"); + + commands.entity(hand).replace_children(&hand_tiles); if let Some(dbt) = &msg.new.drawn_tile { commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id), Drawn)); diff --git a/jong/src/riichi/player.rs b/jong/src/riichi/player.rs index da22ed3..8c7ee4c 100644 --- a/jong/src/riichi/player.rs +++ b/jong/src/riichi/player.rs @@ -9,7 +9,7 @@ pub struct MainPlayer; #[derive(Component)] pub struct CurrentPlayer; -#[derive(Component)] +#[derive(Component, PartialEq, Eq)] pub struct TileId(pub u32); #[derive(Component)] diff --git a/jong/src/tui.rs b/jong/src/tui.rs index 7c619e7..18b994c 100644 --- a/jong/src/tui.rs +++ b/jong/src/tui.rs @@ -101,20 +101,9 @@ fn discard_tile( mut commands: Commands, drawn: Single<(Entity, &TileId), With>, - // curr_player: Single>, - // player_hands: Populated<(&Player, &Children), With>, - // hands: Populated<&Children, (With, Without)>, - main_player: Single<(&Player, Entity), With>, - hands: Query<(&Children, Entity), With>, tiles: Query<&TileId>, ) { - // trace!("discard_tile"); - - let (hand, hand_ent) = hands.iter().next().unwrap(); - - while let Some(message) = selected.read().next() - // && (message.0 == drawn.0 || hand.contains(&message.0)) - { + while let Some(message) = selected.read().next() { if let Ok(tile_id) = tiles.get(message.0) { stdb.reducers().discard_tile(tile_id.0).unwrap(); commands.get_entity(drawn.0).unwrap().despawn(); diff --git a/jong/src/tui/render.rs b/jong/src/tui/render.rs index ff96ac2..f1a243c 100644 --- a/jong/src/tui/render.rs +++ b/jong/src/tui/render.rs @@ -105,7 +105,7 @@ pub(crate) fn render_hand( layouts: Res, tiles: Query<&jong_types::Tile>, - main_player: Single<(&Player, Entity /* , &Wind */), With>, + // main_player: Single<(&Player, Entity /* , &Wind */), With>, hand: Single<(&Children, Entity), With>, drawn_tile: Option>>, ) -> Result { @@ -124,7 +124,7 @@ pub(crate) fn render_hand( }) .collect::>()?; - let (player, player_ent) = *main_player; + // let (player, player_ent) = *main_player; // split main box into thirds let mut this_hand = layouts.this_hand; let tile_drawn = if drawn_tile.is_some() { 7 } else { 0 }; diff --git a/justfile b/justfile index 6b3823b..8f51801 100644 --- a/justfile +++ b/justfile @@ -8,7 +8,8 @@ default: just --list run-tui: - spacetime call jong-line clear_all + just spacetime_restart_dev + sleep 3sec cargo run -- run-tui update: @@ -26,9 +27,3 @@ spacetime_generate-bindings: spacetime_restart_dev: mprocs -s localhost:4050 --ctl $"({c: restart-proc, name: spacetimedb_dev} | to yaml)" - -rrt: - just spacetime_restart_dev - sleep 3sec - just run-tui -