reconnect logic, and we're now jongin
This commit is contained in:
parent
a0171b1de6
commit
74bf702219
2 changed files with 102 additions and 23 deletions
|
|
@ -40,8 +40,8 @@ impl Plugin for Riichi {
|
||||||
.init_state::<jong_types::states::GameState>()
|
.init_state::<jong_types::states::GameState>()
|
||||||
.add_sub_state::<jong_types::states::TurnState>()
|
.add_sub_state::<jong_types::states::TurnState>()
|
||||||
.add_systems(Startup, subscriptions)
|
.add_systems(Startup, subscriptions)
|
||||||
.add_systems(Update, on_connect)
|
.add_observer(on_subscribed)
|
||||||
.add_systems(Update, on_disconnect)
|
.add_systems(Update, (on_connect, on_disconnect))
|
||||||
.add_systems(Update, on_lobby_insert_update)
|
.add_systems(Update, on_lobby_insert_update)
|
||||||
.add_systems(Update, on_player_insert_update)
|
.add_systems(Update, on_player_insert_update)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
|
|
@ -51,6 +51,7 @@ impl Plugin for Riichi {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, _commands: Commands) {
|
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, _commands: Commands) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
info!("you're now jongline");
|
info!("you're now jongline");
|
||||||
|
|
@ -72,9 +73,15 @@ fn on_disconnect(_stdb: SpacetimeDB, mut messages: ReadStdbDisconnectedMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscriptions(stdb: SpacetimeDB) {
|
// TODO we can make this hold more info in the future
|
||||||
|
#[derive(Event)]
|
||||||
|
struct Subscribed;
|
||||||
|
|
||||||
|
fn subscriptions(stdb: SpacetimeDB, mut commands: Commands) {
|
||||||
|
// commands.queue(command);
|
||||||
|
let (send, recv) = std::sync::mpsc::channel::<Subscribed>();
|
||||||
stdb.subscription_builder()
|
stdb.subscription_builder()
|
||||||
.on_applied(|_| trace!("made all subs!"))
|
.on_applied(move |_| send.send(Subscribed).unwrap())
|
||||||
.on_error(|_, err| error!("sub failed: {err}"))
|
.on_error(|_, err| error!("sub failed: {err}"))
|
||||||
.subscribe([
|
.subscribe([
|
||||||
// TODO change these to sub/unsub based on being in lobby and some such
|
// TODO change these to sub/unsub based on being in lobby and some such
|
||||||
|
|
@ -87,7 +94,51 @@ fn subscriptions(stdb: SpacetimeDB) {
|
||||||
"SELECT * FROM view_hand".to_string(),
|
"SELECT * FROM view_hand".to_string(),
|
||||||
"SELECT b.* FROM bot b JOIN lobby l ON l.id = b.lobby_id".to_string(),
|
"SELECT b.* FROM bot b JOIN lobby l ON l.id = b.lobby_id".to_string(),
|
||||||
]);
|
]);
|
||||||
// .subscribe_to_all_tables();
|
|
||||||
|
while let Ok(event) = recv.recv() {
|
||||||
|
commands.trigger(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// restores (spawns) entities to be consistent with server state
|
||||||
|
fn on_subscribed(
|
||||||
|
_event: On<Subscribed>,
|
||||||
|
|
||||||
|
stdb: SpacetimeDB,
|
||||||
|
|
||||||
|
mut commands: Commands,
|
||||||
|
mut next_gamestate: ResMut<NextState<jong_types::states::GameState>>,
|
||||||
|
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
|
||||||
|
) {
|
||||||
|
if let Some(player) = stdb.db().player().iter().next() {}
|
||||||
|
|
||||||
|
if let Some(lobby) = stdb.db().lobby().iter().next() {
|
||||||
|
next_gamestate.set(lobby.game_state.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let hand_ent = commands.spawn(Hand).id();
|
||||||
|
let pond_ent = commands.spawn(Pond).id();
|
||||||
|
if 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 pond_tiles: Vec<_> = player_hand
|
||||||
|
.pond
|
||||||
|
.iter()
|
||||||
|
.map(|dbt| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
||||||
|
.collect();
|
||||||
|
commands.entity(pond_ent).add_children(&pond_tiles);
|
||||||
|
commands.entity(hand_ent).add_children(&hand_tiles);
|
||||||
|
|
||||||
|
if player_hand.turn_state == jong_db::TurnState::Tsumo
|
||||||
|
&& let Some(drawn_dbt) = player_hand.working_tile
|
||||||
|
{
|
||||||
|
commands.spawn((Drawn, Tile::from(&drawn_dbt.tile), TileId(drawn_dbt.id)));
|
||||||
|
}
|
||||||
|
next_turnstate.set(player_hand.turn_state.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_view_hand_update(
|
fn on_view_hand_update(
|
||||||
|
|
@ -95,34 +146,51 @@ fn on_view_hand_update(
|
||||||
mut messages: ReadUpdateMessage<jong_db::PlayerHand>,
|
mut messages: ReadUpdateMessage<jong_db::PlayerHand>,
|
||||||
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut hand: Option<Single<(Entity, &Children), With<Hand>>>,
|
hand: Single<Entity, With<Hand>>,
|
||||||
drawn: Option<Single<Entity, With<Drawn>>>,
|
pond: Single<Entity, With<Pond>>,
|
||||||
tiles: Query<&Tile>,
|
// drawn: Option<Single<Entity, With<Drawn>>>,
|
||||||
|
tiles: Query<(Entity, &TileId)>,
|
||||||
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
|
mut next_turnstate: ResMut<NextState<jong_types::states::TurnState>>,
|
||||||
) {
|
) {
|
||||||
// let mut hand = hand.and_then(|h| Some(*h));
|
// trace!("on_view_hand_update");
|
||||||
|
|
||||||
if hand.is_none()
|
// TODO can this and similar run at startup or on play/reconnect?
|
||||||
&& let Some(player_hand) = stdb.db().view_hand().iter().next()
|
for msg in messages.read() {
|
||||||
{
|
trace!("new hand: {:?}", msg.new);
|
||||||
let hand_tiles: Vec<_> = player_hand
|
|
||||||
|
let hand_tiles: Vec<_> = msg
|
||||||
|
.new
|
||||||
.hand
|
.hand
|
||||||
.iter()
|
.iter()
|
||||||
.map(|dbt| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
.map(|dbt| {
|
||||||
|
tiles
|
||||||
|
.iter()
|
||||||
|
.find_map(|(e, t)| if *t == TileId(dbt.id) { Some(e) } else { None })
|
||||||
|
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let hand_ent = commands.spawn(Hand).add_children(&hand_tiles).id();
|
commands.entity(*hand).replace_children(&hand_tiles);
|
||||||
debug!("hand_tiles: {hand_tiles:?}");
|
|
||||||
// hand = Some(hand_ent);
|
|
||||||
}
|
|
||||||
|
|
||||||
for msg in messages.read() {
|
let pond_tiles: Vec<_> = msg
|
||||||
// trace!("new hand: {:?}", msg.new);
|
.new
|
||||||
|
.pond
|
||||||
|
.iter()
|
||||||
|
.map(|dbt| {
|
||||||
|
tiles
|
||||||
|
.iter()
|
||||||
|
.find_map(|(e, t)| if *t == TileId(dbt.id) { Some(e) } else { None })
|
||||||
|
.unwrap_or_else(|| commands.spawn((Tile::from(&dbt.tile), TileId(dbt.id))).id())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
commands.entity(*pond).replace_children(&pond_tiles);
|
||||||
|
|
||||||
match msg.new.turn_state {
|
match msg.new.turn_state {
|
||||||
jong_db::TurnState::None => {
|
jong_db::TurnState::None => {
|
||||||
|
trace!("turnstate none");
|
||||||
// TODO do we reconcile hand state here or in ::End?
|
// TODO do we reconcile hand state here or in ::End?
|
||||||
}
|
}
|
||||||
jong_db::TurnState::Tsumo => {
|
jong_db::TurnState::Tsumo => {
|
||||||
|
trace!("turnstate tsumo");
|
||||||
let dbt = msg
|
let dbt = msg
|
||||||
.new
|
.new
|
||||||
.working_tile
|
.working_tile
|
||||||
|
|
@ -145,7 +213,18 @@ fn on_player_insert_update(
|
||||||
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
|
mut messages: ReadInsertUpdateMessage<jong_db::Player>,
|
||||||
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|
||||||
|
hand: Option<Single<Entity, With<Hand>>>,
|
||||||
|
pond: Option<Single<Entity, With<Pond>>>,
|
||||||
) {
|
) {
|
||||||
|
// TODO this should be startup
|
||||||
|
if hand.is_none() {
|
||||||
|
commands.spawn(Hand);
|
||||||
|
}
|
||||||
|
if pond.is_none() {
|
||||||
|
commands.spawn(Pond);
|
||||||
|
}
|
||||||
|
|
||||||
for msg in messages.read() {}
|
for msg in messages.read() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,11 +232,11 @@ fn on_lobby_insert_update(
|
||||||
stdb: SpacetimeDB,
|
stdb: SpacetimeDB,
|
||||||
mut messages: ReadInsertUpdateMessage<jong_db::Lobby>,
|
mut messages: ReadInsertUpdateMessage<jong_db::Lobby>,
|
||||||
|
|
||||||
commands: Commands,
|
mut commands: Commands,
|
||||||
mut next_gamestate: ResMut<NextState<jong_types::states::GameState>>,
|
mut next_gamestate: ResMut<NextState<jong_types::states::GameState>>,
|
||||||
) {
|
) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
trace!("on_lobby_insert_update msg:\n{:#?}", msg.new);
|
// trace!("on_lobby_insert_update msg:\n{:#?}", msg.new);
|
||||||
|
|
||||||
let player = stdb
|
let player = stdb
|
||||||
.db()
|
.db()
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ fn discard_tile(
|
||||||
tiles: Query<&TileId>,
|
tiles: Query<&TileId>,
|
||||||
) {
|
) {
|
||||||
// FIXME why is this not consuming the messages?
|
// FIXME why is this not consuming the messages?
|
||||||
while let Some(message) = selected.read().last() {
|
while let Some(message) = selected.read().next() {
|
||||||
if let Ok(tile_id) = tiles.get(message.0) {
|
if let Ok(tile_id) = tiles.get(message.0) {
|
||||||
stdb.reducers().discard_tile(tile_id.0).unwrap();
|
stdb.reducers().discard_tile(tile_id.0).unwrap();
|
||||||
commands.entity(drawn.0).remove::<Drawn>();
|
commands.entity(drawn.0).remove::<Drawn>();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue