discarding proper now

This commit is contained in:
Tao Tien 2026-02-19 00:00:26 -08:00
parent c3686221aa
commit 875c3fb5bb
7 changed files with 45 additions and 40 deletions

1
.helix/ignore Normal file
View file

@ -0,0 +1 @@
jong-db

View file

@ -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 {}",

View file

@ -101,29 +101,33 @@ fn on_player_insert_update(
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
mut commands: Commands,
player: Option<Single<Entity, With<MainPlayer>>>,
hand: Option<Single<Entity, With<Hand>>>,
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));

View file

@ -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)]

View file

@ -101,20 +101,9 @@ fn discard_tile(
mut commands: Commands,
drawn: Single<(Entity, &TileId), With<Drawn>>,
// curr_player: Single<Entity, With<CurrentPlayer>>,
// player_hands: Populated<(&Player, &Children), With<Hand>>,
// hands: Populated<&Children, (With<Hand>, Without<Player>)>,
main_player: Single<(&Player, Entity), With<MainPlayer>>,
hands: Query<(&Children, Entity), With<Hand>>,
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();

View file

@ -105,7 +105,7 @@ pub(crate) fn render_hand(
layouts: Res<HandLayouts>,
tiles: Query<&jong_types::Tile>,
main_player: Single<(&Player, Entity /* , &Wind */), With<MainPlayer>>,
// main_player: Single<(&Player, Entity /* , &Wind */), With<MainPlayer>>,
hand: Single<(&Children, Entity), With<Hand>>,
drawn_tile: Option<Single<Entity, With<Drawn>>>,
) -> Result {
@ -124,7 +124,7 @@ pub(crate) fn render_hand(
})
.collect::<Result<_>>()?;
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 };

View file

@ -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