can we call this rewrite 4.5

This commit is contained in:
Tao Tien 2026-02-23 16:18:21 -08:00
parent 2d3993452b
commit a0171b1de6
9 changed files with 128 additions and 120 deletions

View file

@ -16,7 +16,7 @@ jong-db.path = "../jong-db"
# bevy
bevy.workspace = true
bevy.features = ["default", "dynamic_linking"]
bevy.features = ["default", "dynamic_linking", "debug"]
bevy_ratatui.workspace = true
# spacetimedb

View file

@ -1,6 +1,7 @@
use bevy::prelude::*;
use bevy_spacetimedb::{
ReadInsertUpdateMessage, ReadStdbConnectedMessage, ReadStdbDisconnectedMessage, StdbPlugin,
ReadInsertUpdateMessage, ReadStdbConnectedMessage, ReadStdbDisconnectedMessage,
ReadUpdateMessage, StdbPlugin,
};
use jong_db::{
@ -9,6 +10,7 @@ use jong_db::{
};
use jong_db::{add_bot, set_ready};
use jong_types::*;
use spacetimedb_sdk::Table;
pub mod player;
use crate::riichi::player::*;
@ -25,10 +27,7 @@ impl Plugin for Riichi {
.add_table(RemoteTables::player)
.add_table(RemoteTables::lobby)
// TODO check bevy_spacetimedb PR status
.add_view_with_pk(RemoteTables::view_hand, |p| p.id)
// semicolon stopper
;
.add_view_with_pk(RemoteTables::view_hand, |p| p.id);
let plugins =
if let Some(token) = creds_store().load().expect("i/o error loading credentials") {
// FIXME patch plugin so this takes Option?
@ -40,27 +39,16 @@ impl Plugin for Riichi {
app.add_plugins(plugins)
.init_state::<jong_types::states::GameState>()
.add_sub_state::<jong_types::states::TurnState>()
// .init_resource::<round::MatchSettings>()
// .init_resource::<round::Compass>()
// .add_systems(Startup, tile::init_tiles)
// .add_systems(Update, hand::sort_hands.run_if(in_state(GameState::Play)))
// .add_systems(OnEnter(TurnState::Tsumo), round::tsumo)
// .add_systems(OnEnter(TurnState::Menzen), round::menzen)
// .add_systems(Update, round::riichi_kan.run_if(in_state(TurnState::RiichiKan)))
// .add_systems(Update, round::discard.run_if(in_state(TurnState::Discard)))
// .add_systems(OnEnter(TurnState::RonChiiPonKan), round::notify_callable)
// .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
.add_systems(Startup, subscriptions)
.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(OnEnter(GameState::Lobby), join_or_create_lobby)
// semicolon stopper
;
.add_systems(Update, on_player_insert_update)
.add_systems(
Update,
(on_view_hand_update)
.run_if(in_state(GameState::Play).or(in_state(GameState::Deal))),
);
}
}
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, _commands: Commands) {
@ -102,34 +90,51 @@ fn subscriptions(stdb: SpacetimeDB) {
// .subscribe_to_all_tables();
}
fn on_view_hand_insert_update(
fn on_view_hand_update(
stdb: SpacetimeDB,
mut messages: ReadInsertUpdateMessage<jong_db::PlayerHand>,
mut messages: ReadUpdateMessage<jong_db::PlayerHand>,
mut commands: Commands,
mut hand: Option<Single<&mut Children, With<Hand>>>,
mut hand: Option<Single<(Entity, &Children), With<Hand>>>,
drawn: Option<Single<Entity, With<Drawn>>>,
tiles: Query<&Tile>,
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
) {
for msg in messages.read() {
if hand.is_none() {
let hand_tiles: Vec<_> = msg
.new
.hand
.iter()
.map(|dbt| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
.collect();
commands.spawn(Hand).add_children(&hand_tiles);
}
// let mut hand = hand.and_then(|h| Some(*h));
// match msg.new.turn_state {
// jong_db::TurnState::None => todo!(),
// jong_db::TurnState::Tsumo => todo!(),
// jong_db::TurnState::Menzen => todo!(),
// jong_db::TurnState::RiichiKan => todo!(),
// jong_db::TurnState::RonChiiPonKan => todo!(),
// jong_db::TurnState::End => todo!(),
// }
if hand.is_none()
&& let Some(player_hand) = stdb.db().view_hand().iter().next()
{
let hand_tiles: Vec<_> = player_hand
.hand
.iter()
.map(|dbt| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
.collect();
let hand_ent = commands.spawn(Hand).add_children(&hand_tiles).id();
debug!("hand_tiles: {hand_tiles:?}");
// hand = Some(hand_ent);
}
for msg in messages.read() {
// trace!("new hand: {:?}", msg.new);
match msg.new.turn_state {
jong_db::TurnState::None => {
// TODO do we reconcile hand state here or in ::End?
}
jong_db::TurnState::Tsumo => {
let dbt = msg
.new
.working_tile
.as_ref()
.expect("entered tsumo without a drawn tile");
commands.spawn((Drawn, Tile::from(&dbt.tile), TileId(dbt.id)));
}
jong_db::TurnState::Menzen => todo!(),
jong_db::TurnState::RiichiKan => todo!(),
jong_db::TurnState::RonChiiPonKan => todo!(),
jong_db::TurnState::End => todo!(),
}
next_turnstate.set(msg.new.turn_state.into());
}

View file

@ -71,11 +71,11 @@ impl Plugin for TuiPlugin {
open: true,
})
.init_state::<states::TuiState>()
.add_message::<ConfirmSelect>()
.configure_sets(
Update,
(TuiSet::Input, TuiSet::Layout, TuiSet::Render).chain(),
)
.add_message::<ConfirmSelect>()
.add_systems(
Update,
(input::keyboard, input::mouse).in_set(TuiSet::Input),
@ -85,7 +85,11 @@ impl Plugin for TuiPlugin {
.add_systems(
Update,
(
(render::render_hand, render::render_pond).run_if(in_state(GameState::Play)),
(
render::render_hand,
render::render_pond,
)
.run_if(in_state(GameState::Play)),
render::render,
)
.chain()
@ -102,7 +106,8 @@ fn discard_tile(
drawn: Single<(Entity, &TileId), With<Drawn>>,
tiles: Query<&TileId>,
) {
while let Some(message) = selected.read().next() {
// FIXME why is this not consuming the messages?
while let Some(message) = selected.read().last() {
if let Ok(tile_id) = tiles.get(message.0) {
stdb.reducers().discard_tile(tile_id.0).unwrap();
commands.entity(drawn.0).remove::<Drawn>();

View file

@ -41,6 +41,7 @@ pub(crate) fn keyboard(
match curr_tuistate.get() {
TuiState::MainMenu => match key {
KeyCode::Char('p') => {
// TODO this should be a msg/signal and handle the correct lobby
stdb.reducers().join_or_create_lobby(0).unwrap();
next_tuistate.set(TuiState::InGame);
}

View file

@ -93,6 +93,21 @@ pub(crate) fn render(
Ok(())
}
pub(crate) fn query_tester(
mut commands: Commands,
mut tui: ResMut<RatatuiContext>,
hovered: Query<Entity, With<Hovered>>,
layouts: Res<HandLayouts>,
tiles: Query<&jong_types::Tile>,
// main_player: Single<(&Player, Entity /* , &Wind */), With<MainPlayer>>,
hand: Single<(&Children, Entity), With<Hand>>,
drawn_tile: Option<Single<Entity, With<Drawn>>>,
) {
trace!("owo")
}
// FIXME we don't care about other players atm
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub(crate) fn render_hand(