temporarily sub all, then react
This commit is contained in:
parent
d0c79377aa
commit
9f6a5b6423
1 changed files with 41 additions and 69 deletions
110
jong/src/game.rs
110
jong/src/game.rs
|
|
@ -66,7 +66,7 @@ impl Plugin for Riichi {
|
||||||
// .add_sub_state::<TurnState>()
|
// .add_sub_state::<TurnState>()
|
||||||
// .init_resource::<round::MatchSettings>()
|
// .init_resource::<round::MatchSettings>()
|
||||||
// .init_resource::<round::Compass>()
|
// .init_resource::<round::Compass>()
|
||||||
// .add_message::<GameMessage>()
|
.add_message::<GameMessage>()
|
||||||
.add_systems(Startup, tile::init_tiles)
|
.add_systems(Startup, tile::init_tiles)
|
||||||
// .add_systems(OnEnter(GameState::Setup), setup)
|
// .add_systems(OnEnter(GameState::Setup), setup)
|
||||||
// .add_systems(OnEnter(GameState::Deal), hand::shuffle_deal)
|
// .add_systems(OnEnter(GameState::Deal), hand::shuffle_deal)
|
||||||
|
|
@ -79,30 +79,24 @@ impl Plugin for Riichi {
|
||||||
// .add_systems(Update, round::ron_chi_pon_kan.run_if(in_state(TurnState::RonChiiPonKan)).after(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)
|
// .add_systems(OnEnter(TurnState::End), round::end)
|
||||||
// stdb
|
// stdb
|
||||||
.init_resource::<LobbyId>()
|
.add_systems(Startup, sub_to_all)
|
||||||
.add_systems(Update, on_connect)
|
.add_systems(Update, on_connect)
|
||||||
.add_systems(Update, on_disconnect)
|
.add_systems(Update, on_disconnect)
|
||||||
.add_systems(Update, on_player_insert_update)
|
.add_systems(Update, (on_player_insert_update, on_lobby_insert_update).chain())
|
||||||
.add_systems(Update, on_lobby_insert_update)
|
|
||||||
.add_systems(OnEnter(GameState::Setup), join_or_create_lobby)
|
.add_systems(OnEnter(GameState::Setup), join_or_create_lobby)
|
||||||
.add_systems(Update, (view_hand).run_if(in_state(GameState::Play)))
|
// .add_systems(Update, (view_hand).run_if(in_state(GameState::Play)))
|
||||||
// semicolon stopper
|
// semicolon stopper
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view_hand(stdb: SpacetimeDB) {
|
fn sub_to_all(stdb: SpacetimeDB) {
|
||||||
// stdb.db()
|
stdb.subscription_builder()
|
||||||
// .hand()
|
.on_applied(|_| trace!("made all subs!"))
|
||||||
// .player_identity()
|
.on_error(|_, err| error!("sub failed: {err}"))
|
||||||
// .find(&stdb.identity())
|
.subscribe_to_all_tables();
|
||||||
// .unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Deref)]
|
|
||||||
struct Player(stdb::Player);
|
|
||||||
|
|
||||||
// TODO or reconnect?
|
|
||||||
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, mut commands: Commands) {
|
fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, mut commands: Commands) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
info!("you're now jongline");
|
info!("you're now jongline");
|
||||||
|
|
@ -110,25 +104,19 @@ fn on_connect(stdb: SpacetimeDB, mut messages: ReadStdbConnectedMessage, mut com
|
||||||
creds_store()
|
creds_store()
|
||||||
.save(&msg.access_token)
|
.save(&msg.access_token)
|
||||||
.expect("i/o error saving token");
|
.expect("i/o error saving token");
|
||||||
|
|
||||||
stdb.subscription_builder()
|
|
||||||
.on_applied(|ctx| {
|
|
||||||
trace!("subbed to player table");
|
|
||||||
})
|
|
||||||
.on_error(|_, err| error!("sub to player failed: {err}"))
|
|
||||||
.subscribe(format!(
|
|
||||||
"SELECT * FROM player p WHERE p.identity = '{}'",
|
|
||||||
stdb.identity()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO how reconnect?
|
||||||
fn on_disconnect(stdb: SpacetimeDB, mut messages: ReadStdbDisconnectedMessage) {
|
fn on_disconnect(stdb: SpacetimeDB, mut messages: ReadStdbDisconnectedMessage) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
warn!("lost connection: {:#?}", msg.err);
|
warn!("lost connection: {:#?}", msg.err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Resource, Deref)]
|
||||||
|
struct Player(stdb::Player);
|
||||||
|
|
||||||
fn on_player_insert_update(
|
fn on_player_insert_update(
|
||||||
stdb: SpacetimeDB,
|
stdb: SpacetimeDB,
|
||||||
mut messages: ReadInsertUpdateMessage<stdb::Player>,
|
mut messages: ReadInsertUpdateMessage<stdb::Player>,
|
||||||
|
|
@ -154,47 +142,14 @@ fn on_player_insert_update(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn join_or_create_lobby(stdb: SpacetimeDB, player: Res<Player>, mut lobby_id: ResMut<LobbyId>) {
|
fn join_or_create_lobby(stdb: SpacetimeDB, player: Res<Player>) {
|
||||||
let sub = stdb
|
|
||||||
.subscription_builder()
|
|
||||||
.on_applied(|_| {
|
|
||||||
trace!("subbed to lobby table");
|
|
||||||
})
|
|
||||||
.on_error(|_, err| error!("sub to lobby table failed: {err}"))
|
|
||||||
.subscribe([format!(
|
|
||||||
"SELECT l.*
|
|
||||||
FROM lobby l
|
|
||||||
WHERE l.host_player_id = {}",
|
|
||||||
player.id
|
|
||||||
)]);
|
|
||||||
|
|
||||||
let mut player = player.clone();
|
let mut player = player.clone();
|
||||||
|
|
||||||
if player.lobby_id == 0 {
|
if player.lobby_id == 0 {
|
||||||
stdb.reducers().join_or_create_lobby(0).unwrap();
|
stdb.reducers().join_or_create_lobby(0).unwrap();
|
||||||
player.lobby_id = stdb
|
} else {
|
||||||
.db()
|
info!("in lobby: {}", player.lobby_id)
|
||||||
.lobby()
|
|
||||||
.host_player_id()
|
|
||||||
.find(&player.id)
|
|
||||||
.unwrap_or_else(|| panic!("can't find player with id {}", player.id))
|
|
||||||
.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(lobby) = stdb.db().lobby().id().find(&player.lobby_id)
|
|
||||||
&& lobby.host_player_id == player.id
|
|
||||||
&& matches!(lobby.game_state.into(), GameState::None)
|
|
||||||
{
|
|
||||||
debug!("setup_game({})", lobby.id);
|
|
||||||
stdb.reducers().setup_game(lobby.id).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
*lobby_id = LobbyId(player.lobby_id);
|
|
||||||
stdb.reducers().setup_game(**lobby_id).unwrap();
|
|
||||||
|
|
||||||
stdb.subscription_builder()
|
|
||||||
.on_applied(|_| trace!("subbed to view_hand"))
|
|
||||||
.subscribe("SELECT * FROM view_hand");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<stdb::GameState> for GameState {
|
impl From<stdb::GameState> for GameState {
|
||||||
|
|
@ -209,23 +164,40 @@ impl From<stdb::GameState> for GameState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default, Deref)]
|
|
||||||
struct LobbyId(u32);
|
|
||||||
|
|
||||||
fn on_lobby_insert_update(
|
fn on_lobby_insert_update(
|
||||||
stdb: SpacetimeDB,
|
stdb: SpacetimeDB,
|
||||||
mut messages: ReadInsertUpdateMessage<stdb::Lobby>,
|
mut messages: ReadInsertUpdateMessage<stdb::Lobby>,
|
||||||
|
|
||||||
|
player: Option<Res<Player>>,
|
||||||
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut next_gamestate: ResMut<NextState<GameState>>,
|
mut next_gamestate: ResMut<NextState<GameState>>,
|
||||||
|
|
||||||
lobby_id: Res<LobbyId>,
|
|
||||||
) {
|
) {
|
||||||
for msg in messages.read() {
|
for msg in messages.read() {
|
||||||
// TODO should this be an assert?
|
if let Some(player) = player.as_ref()
|
||||||
if msg.new.id == **lobby_id {
|
&& player.lobby_id == msg.new.id
|
||||||
debug!("lobby: {:?}", msg.new);
|
{
|
||||||
|
debug!("on_lobby_insert_update: {:#?}", msg.new);
|
||||||
next_gamestate.set(msg.new.game_state.into());
|
next_gamestate.set(msg.new.game_state.into());
|
||||||
|
|
||||||
|
match msg.new.game_state {
|
||||||
|
stdb::GameState::None => {
|
||||||
|
trace!("setup game");
|
||||||
|
stdb.reducers().setup_game(player.lobby_id).unwrap();
|
||||||
|
}
|
||||||
|
stdb::GameState::Setup => {
|
||||||
|
trace!("game entered setup");
|
||||||
|
}
|
||||||
|
stdb::GameState::Deal => {
|
||||||
|
trace!("game entered deal")
|
||||||
|
}
|
||||||
|
stdb::GameState::Play => {
|
||||||
|
trace!("game entered play")
|
||||||
|
}
|
||||||
|
stdb::GameState::Exit => {
|
||||||
|
trace!("game enetered exit")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue