Compare commits

..

No commits in common. "d183f5d993f8b0e3b605fcdd45673dfdf53a86b2" and "47735931b452bed57c1ef2ad342e37387d1039a5" have entirely different histories.

24 changed files with 138 additions and 509 deletions

View file

@ -1 +1,2 @@
[editor]
workspace-lsp-roots = ["jongline"]

View file

@ -1,6 +0,0 @@
# [language-server.lspmux]
# command = "lspmux"
# [[language]]
# name = "rust"
# language-servers = ["lspmux"]

View file

@ -4,7 +4,6 @@
... ...
}: rec { }: rec {
# https://devenv.sh/processes/ # https://devenv.sh/processes/
# processes.lspmux.exec = "lspmux server";
processes.spacetimedb_start.exec = "spacetime start"; processes.spacetimedb_start.exec = "spacetime start";
processes.spacetimedb_generate_bindings = { processes.spacetimedb_generate_bindings = {
exec = "spacetime dev --module-bindings-path jong/src/stdb jongline --delete-data=always"; exec = "spacetime dev --module-bindings-path jong/src/stdb jongline --delete-data=always";
@ -18,7 +17,7 @@
# https://devenv.sh/packages/ # https://devenv.sh/packages/
packages = with pkgs; [ packages = with pkgs; [
# lspmux # process-compose
pkg-config pkg-config
# spacetimedb # spacetimedb

View file

@ -11,14 +11,13 @@ use derive_aliases::derive;
use bevy::prelude::*; use bevy::prelude::*;
use spacetimedb::SpacetimeType; use spacetimedb::SpacetimeType;
use strum::{EnumCount, FromRepr}; use strum::FromRepr;
#[derive(..Base, Hash, Default)] #[derive(..Base, Hash, Default)]
#[derive(States, SpacetimeType)] #[derive(States, SpacetimeType)]
pub enum GameState { pub enum GameState {
#[default] #[default]
None, None,
Lobby,
Setup, Setup,
Deal, Deal,
Play, Play,
@ -113,24 +112,3 @@ pub fn tiles() -> Vec<Tile> {
} }
tiles tiles
} }
#[derive(
Default,
..Copy,
PartialEq,
Eq,
Hash,
Debug,
)]
#[derive(SubStates, FromRepr, EnumCount, SpacetimeType)]
#[source(GameState = GameState::Play)]
pub enum TurnState {
#[default]
None,
Tsumo,
Menzen,
RiichiKan,
Discard,
RonChiiPonKan,
End,
}

View file

@ -8,18 +8,19 @@ use bevy_spacetimedb::{
use spacetimedb::Identity; use spacetimedb::Identity;
use spacetimedb_sdk::{DbContext, Table, credentials}; use spacetimedb_sdk::{DbContext, Table, credentials};
use crate::stdb::{ use crate::{
SpacetimeDB, creds_store, game,
stdb::{
self, DbConnection, HandTableAccess, LobbyTableAccess, PlayerTableAccess, RemoteTables, self, DbConnection, HandTableAccess, LobbyTableAccess, PlayerTableAccess, RemoteTables,
ViewPlayerHandTableAccess, add_bot, join_or_create_lobby, login_or_add_player, set_ready, ViewPlayerHandTableAccess, add_bot, join_or_create_lobby, login_or_add_player,
shuffle_deal, start_game, shuffle_deal,
},
}; };
use crate::{ use crate::{
SpacetimeDB, creds_store,
game::{ game::{
self,
hand::{Hand, Pond}, hand::{Hand, Pond},
player::{CurrentPlayer, MainPlayer, Player}, player::{CurrentPlayer, MainPlayer},
round::Wind, round::{TurnState, Wind},
wall::Wall, wall::Wall,
}, },
tile::{self}, tile::{self},
@ -63,7 +64,7 @@ impl Plugin for Riichi {
app.add_plugins(plugins) app.add_plugins(plugins)
.init_state::<GameState>() .init_state::<GameState>()
.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>()
@ -82,12 +83,24 @@ impl Plugin for Riichi {
.add_systems(Update, on_disconnect) .add_systems(Update, on_disconnect)
.add_systems(Update, on_player_insert_update) .add_systems(Update, on_player_insert_update)
.add_systems(Update, on_lobby_insert_update) .add_systems(Update, on_lobby_insert_update)
// .add_systems(OnEnter(GameState::Lobby), 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)))
.add_systems(Update, (view_hand).run_if(in_state(GameState::Play)))
// semicolon stopper // semicolon stopper
; ;
} }
} }
fn view_hand(
stdb: SpacetimeDB,
mut commands: Commands,
// hand: Populated<&mut Children, With<Hand>>,
) {
if let Some(view) = stdb.db().view_player_hand().iter().next() {
// hand.get_mut(entity)
}
}
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");
@ -125,47 +138,26 @@ fn on_player_insert_update(
mut messages: ReadInsertUpdateMessage<stdb::Player>, mut messages: ReadInsertUpdateMessage<stdb::Player>,
mut commands: Commands, mut commands: Commands,
mut player: Option<Single<&mut player::Player>>,
) { ) {
use player::*;
for msg in messages.read() { for msg in messages.read() {
debug!("player_insert_update msg:\n{:#?}", msg.new); debug!("player_insert_update msg:\n{:#?}", msg.new);
if let Some(ref player) = player {
// if msg.old.as_ref().is_some_and(|m| !m.ready) && msg.new.ready {
// trace!("entered ready");
// // TODO add a start game button in the future
// stdb.reducers().start_game().unwrap();
// }
} else {
let player = Player {
name: msg
.new
.name
.as_ref()
.unwrap_or(&"nameless".to_string())
.clone(),
};
let bundle = (player, Hand, Pond, MainPlayer, CurrentPlayer);
commands.spawn(bundle);
}
} }
} }
// fn join_or_create_lobby(stdb: SpacetimeDB) { fn join_or_create_lobby(stdb: SpacetimeDB) {
// let player = stdb let player = stdb
// .db() .db()
// .player() .player()
// .identity() .identity()
// .find(&stdb.identity()) .find(&stdb.identity())
// .unwrap(); .unwrap();
// 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();
// } else { } else {
// info!("in lobby: {}", player.lobby_id) info!("in lobby: {}", player.lobby_id)
// } }
// } }
fn on_lobby_insert_update( fn on_lobby_insert_update(
stdb: SpacetimeDB, stdb: SpacetimeDB,
@ -177,6 +169,14 @@ fn on_lobby_insert_update(
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);
next_gamestate.set(msg.new.game_state.into());
match msg.new.game_state {
stdb::GameState::None => {
trace!("game entered none");
}
stdb::GameState::Setup => {
trace!("game entered setup");
let player = stdb let player = stdb
.db() .db()
.player() .player()
@ -184,64 +184,38 @@ fn on_lobby_insert_update(
.find(&stdb.identity()) .find(&stdb.identity())
.unwrap(); .unwrap();
next_gamestate.set(msg.new.game_state.into()); if player.lobby_id == msg.new.id {
match msg.new.game_state { stdb.reducers().shuffle_deal(player.lobby_id).unwrap();
stdb::GameState::None => {
trace!("game entered none");
}
stdb::GameState::Lobby => {
trace!("game in lobby");
if !player.ready {
for _ in 0..3 { for _ in 0..3 {
stdb.reducers().add_bot(player.lobby_id).unwrap(); stdb.reducers().add_bot(player.lobby_id).unwrap();
} }
stdb.reducers().set_ready(true).unwrap(); } else {
stdb.reducers().start_game().unwrap(); error!("no player but game in setup")
} }
} }
stdb::GameState::Setup => {
trace!("game entered setup");
stdb.reducers().shuffle_deal(player.lobby_id).unwrap();
}
stdb::GameState::Deal => { stdb::GameState::Deal => {
trace!("game entered deal"); trace!("game entered deal");
} }
stdb::GameState::Play => { stdb::GameState::Play => {
trace!("game entered play"); trace!("game entered play");
// TODO this should run once to spawn the hand? or do elsewhere
if let Some(hand) = stdb.db().view_player_hand().iter().next() {
debug!("hand: {hand:?}");
let tiles = hand
.tiles
.iter()
.map(Into::into)
.map(|t: Tile| commands.spawn(t).id())
.collect::<Vec<_>>();
commands.spawn(Hand).add_children(&tiles);
} else {
error!("entered play without a hand")
}
} }
stdb::GameState::Exit => { stdb::GameState::Exit => {
trace!("game enetered exit"); trace!("game enetered exit");
} }
} }
next_gamestate.set(msg.new.game_state.into());
}
}
fn view_hand(
stdb: SpacetimeDB,
mut commands: Commands,
tiles: Query<(&Tile, Entity)>,
hand_ent: Single<Entity, With<Hand>>,
) {
// trace!("view_hand");
if let Some(view) = stdb.db().view_player_hand().iter().next() {
let mut view = view.tiles.iter().map(Tile::from).collect::<Vec<_>>();
let tiles = tiles
.iter()
.filter(|(tt, _)| {
if let Some((i, _)) = view.iter().enumerate().find(|(_, t)| t == tt) {
view.swap_remove(i);
true
} else {
false
}
})
.map(|(_, e)| e)
.collect::<Vec<_>>();
commands.entity(*hand_ent).replace_children(&tiles);
} }
} }

View file

@ -12,7 +12,6 @@ use crate::{
wall::Wall, wall::Wall,
}, },
}; };
use jong_types::TurnState;
// #[derive(Resource)] // #[derive(Resource)]
// pub struct CurrentPlayer(pub Entity); // pub struct CurrentPlayer(pub Entity);
@ -49,6 +48,18 @@ pub enum WindRelation {
Kamicha, Kamicha,
} }
#[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug, FromRepr, EnumCount)]
#[source(GameState = GameState::Play)]
pub(crate) enum TurnState {
#[default]
Tsumo,
Menzen,
RiichiKan,
Discard,
RonChiiPonKan,
End,
}
#[derive(Component, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Component, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum CallType { pub(crate) enum CallType {
Skip, Skip,

View file

@ -5,10 +5,11 @@ use bevy_spacetimedb::StdbConnection;
use spacetimedb_sdk::credentials; use spacetimedb_sdk::credentials;
pub mod game; pub mod game;
pub mod stdb;
pub mod tile; pub mod tile;
pub mod yakus; pub mod yakus;
mod stdb;
trait EnumNextCycle { trait EnumNextCycle {
fn next(&self) -> Self; fn next(&self) -> Self;
} }
@ -23,7 +24,6 @@ impl From<stdb::GameState> for jong_types::GameState {
fn from(value: stdb::GameState) -> Self { fn from(value: stdb::GameState) -> Self {
match value { match value {
stdb::GameState::None => Self::None, stdb::GameState::None => Self::None,
stdb::GameState::Lobby => Self::Lobby,
stdb::GameState::Setup => Self::Setup, stdb::GameState::Setup => Self::Setup,
stdb::GameState::Deal => Self::Deal, stdb::GameState::Deal => Self::Deal,
stdb::GameState::Play => Self::Play, stdb::GameState::Play => Self::Play,

View file

@ -10,8 +10,6 @@ use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
pub enum GameState { pub enum GameState {
None, None,
Lobby,
Setup, Setup,
Deal, Deal,

View file

@ -5,7 +5,6 @@
use super::game_state_type::GameState; use super::game_state_type::GameState;
use super::lobby_type::Lobby; use super::lobby_type::Lobby;
use super::player_or_bot_type::PlayerOrBot; use super::player_or_bot_type::PlayerOrBot;
use super::turn_state_type::TurnState;
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
/// Table handle for the table `lobby`. /// Table handle for the table `lobby`.

View file

@ -6,7 +6,6 @@ use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
use super::game_state_type::GameState; use super::game_state_type::GameState;
use super::player_or_bot_type::PlayerOrBot; use super::player_or_bot_type::PlayerOrBot;
use super::turn_state_type::TurnState;
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)] #[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)] #[sats(crate = __lib)]
@ -15,7 +14,6 @@ pub struct Lobby {
pub host_player_id: u32, pub host_player_id: u32,
pub players: Vec<PlayerOrBot>, pub players: Vec<PlayerOrBot>,
pub game_state: GameState, pub game_state: GameState,
pub turn_state: TurnState,
} }
impl __sdk::InModule for Lobby { impl __sdk::InModule for Lobby {

View file

@ -23,12 +23,9 @@ pub mod player_type;
pub mod pond_table; pub mod pond_table;
pub mod pond_type; pub mod pond_type;
pub mod rank_type; pub mod rank_type;
pub mod set_ready_reducer;
pub mod shuffle_deal_reducer; pub mod shuffle_deal_reducer;
pub mod start_game_reducer;
pub mod suit_type; pub mod suit_type;
pub mod tile_type; pub mod tile_type;
pub mod turn_state_type;
pub mod view_player_hand_table; pub mod view_player_hand_table;
pub mod wall_table; pub mod wall_table;
pub mod wall_type; pub mod wall_type;
@ -55,12 +52,9 @@ pub use player_type::Player;
pub use pond_table::*; pub use pond_table::*;
pub use pond_type::Pond; pub use pond_type::Pond;
pub use rank_type::Rank; pub use rank_type::Rank;
pub use set_ready_reducer::{set_flags_for_set_ready, set_ready, SetReadyCallbackId};
pub use shuffle_deal_reducer::{set_flags_for_shuffle_deal, shuffle_deal, ShuffleDealCallbackId}; pub use shuffle_deal_reducer::{set_flags_for_shuffle_deal, shuffle_deal, ShuffleDealCallbackId};
pub use start_game_reducer::{set_flags_for_start_game, start_game, StartGameCallbackId};
pub use suit_type::Suit; pub use suit_type::Suit;
pub use tile_type::Tile; pub use tile_type::Tile;
pub use turn_state_type::TurnState;
pub use view_player_hand_table::*; pub use view_player_hand_table::*;
pub use wall_table::*; pub use wall_table::*;
pub use wall_type::Wall; pub use wall_type::Wall;
@ -77,9 +71,7 @@ pub enum Reducer {
AddBot { lobby_id: u32 }, AddBot { lobby_id: u32 },
JoinOrCreateLobby { lobby_id: u32 }, JoinOrCreateLobby { lobby_id: u32 },
LoginOrAddPlayer, LoginOrAddPlayer,
SetReady { ready: bool },
ShuffleDeal { lobby_id: u32 }, ShuffleDeal { lobby_id: u32 },
StartGame,
} }
impl __sdk::InModule for Reducer { impl __sdk::InModule for Reducer {
@ -92,9 +84,7 @@ impl __sdk::Reducer for Reducer {
Reducer::AddBot { .. } => "add_bot", Reducer::AddBot { .. } => "add_bot",
Reducer::JoinOrCreateLobby { .. } => "join_or_create_lobby", Reducer::JoinOrCreateLobby { .. } => "join_or_create_lobby",
Reducer::LoginOrAddPlayer => "login_or_add_player", Reducer::LoginOrAddPlayer => "login_or_add_player",
Reducer::SetReady { .. } => "set_ready",
Reducer::ShuffleDeal { .. } => "shuffle_deal", Reducer::ShuffleDeal { .. } => "shuffle_deal",
Reducer::StartGame => "start_game",
_ => unreachable!(), _ => unreachable!(),
} }
} }
@ -116,13 +106,6 @@ impl TryFrom<__ws::ReducerCallInfo<__ws::BsatnFormat>> for Reducer {
login_or_add_player_reducer::LoginOrAddPlayerArgs, login_or_add_player_reducer::LoginOrAddPlayerArgs,
>("login_or_add_player", &value.args)? >("login_or_add_player", &value.args)?
.into()), .into()),
"set_ready" => Ok(
__sdk::parse_reducer_args::<set_ready_reducer::SetReadyArgs>(
"set_ready",
&value.args,
)?
.into(),
),
"shuffle_deal" => Ok( "shuffle_deal" => Ok(
__sdk::parse_reducer_args::<shuffle_deal_reducer::ShuffleDealArgs>( __sdk::parse_reducer_args::<shuffle_deal_reducer::ShuffleDealArgs>(
"shuffle_deal", "shuffle_deal",
@ -130,13 +113,6 @@ impl TryFrom<__ws::ReducerCallInfo<__ws::BsatnFormat>> for Reducer {
)? )?
.into(), .into(),
), ),
"start_game" => Ok(
__sdk::parse_reducer_args::<start_game_reducer::StartGameArgs>(
"start_game",
&value.args,
)?
.into(),
),
unknown => { unknown => {
Err( Err(
__sdk::InternalError::unknown_name("reducer", unknown, "ReducerCallInfo") __sdk::InternalError::unknown_name("reducer", unknown, "ReducerCallInfo")

View file

@ -13,7 +13,6 @@ pub struct Player {
pub lobby_id: u32, pub lobby_id: u32,
pub hand_id: u32, pub hand_id: u32,
pub pond_id: u32, pub pond_id: u32,
pub ready: bool,
} }
impl __sdk::InModule for Player { impl __sdk::InModule for Player {

View file

@ -1,102 +0,0 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
#![allow(unused, clippy::all)]
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)]
pub(super) struct SetReadyArgs {
pub ready: bool,
}
impl From<SetReadyArgs> for super::Reducer {
fn from(args: SetReadyArgs) -> Self {
Self::SetReady { ready: args.ready }
}
}
impl __sdk::InModule for SetReadyArgs {
type Module = super::RemoteModule;
}
pub struct SetReadyCallbackId(__sdk::CallbackId);
#[allow(non_camel_case_types)]
/// Extension trait for access to the reducer `set_ready`.
///
/// Implemented for [`super::RemoteReducers`].
pub trait set_ready {
/// Request that the remote module invoke the reducer `set_ready` to run as soon as possible.
///
/// This method returns immediately, and errors only if we are unable to send the request.
/// The reducer will run asynchronously in the future,
/// and its status can be observed by listening for [`Self::on_set_ready`] callbacks.
fn set_ready(&self, ready: bool) -> __sdk::Result<()>;
/// Register a callback to run whenever we are notified of an invocation of the reducer `set_ready`.
///
/// Callbacks should inspect the [`__sdk::ReducerEvent`] contained in the [`super::ReducerEventContext`]
/// to determine the reducer's status.
///
/// The returned [`SetReadyCallbackId`] can be passed to [`Self::remove_on_set_ready`]
/// to cancel the callback.
fn on_set_ready(
&self,
callback: impl FnMut(&super::ReducerEventContext, &bool) + Send + 'static,
) -> SetReadyCallbackId;
/// Cancel a callback previously registered by [`Self::on_set_ready`],
/// causing it not to run in the future.
fn remove_on_set_ready(&self, callback: SetReadyCallbackId);
}
impl set_ready for super::RemoteReducers {
fn set_ready(&self, ready: bool) -> __sdk::Result<()> {
self.imp.call_reducer("set_ready", SetReadyArgs { ready })
}
fn on_set_ready(
&self,
mut callback: impl FnMut(&super::ReducerEventContext, &bool) + Send + 'static,
) -> SetReadyCallbackId {
SetReadyCallbackId(self.imp.on_reducer(
"set_ready",
Box::new(move |ctx: &super::ReducerEventContext| {
#[allow(irrefutable_let_patterns)]
let super::ReducerEventContext {
event:
__sdk::ReducerEvent {
reducer: super::Reducer::SetReady { ready },
..
},
..
} = ctx
else {
unreachable!()
};
callback(ctx, ready)
}),
))
}
fn remove_on_set_ready(&self, callback: SetReadyCallbackId) {
self.imp.remove_on_reducer("set_ready", callback.0)
}
}
#[allow(non_camel_case_types)]
#[doc(hidden)]
/// Extension trait for setting the call-flags for the reducer `set_ready`.
///
/// Implemented for [`super::SetReducerFlags`].
///
/// This type is currently unstable and may be removed without a major version bump.
pub trait set_flags_for_set_ready {
/// Set the call-reducer flags for the reducer `set_ready` to `flags`.
///
/// This type is currently unstable and may be removed without a major version bump.
fn set_ready(&self, flags: __ws::CallReducerFlags);
}
impl set_flags_for_set_ready for super::SetReducerFlags {
fn set_ready(&self, flags: __ws::CallReducerFlags) {
self.imp.set_call_reducer_flags("set_ready", flags);
}
}

View file

@ -1,100 +0,0 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
#![allow(unused, clippy::all)]
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)]
pub(super) struct StartGameArgs {}
impl From<StartGameArgs> for super::Reducer {
fn from(args: StartGameArgs) -> Self {
Self::StartGame
}
}
impl __sdk::InModule for StartGameArgs {
type Module = super::RemoteModule;
}
pub struct StartGameCallbackId(__sdk::CallbackId);
#[allow(non_camel_case_types)]
/// Extension trait for access to the reducer `start_game`.
///
/// Implemented for [`super::RemoteReducers`].
pub trait start_game {
/// Request that the remote module invoke the reducer `start_game` to run as soon as possible.
///
/// This method returns immediately, and errors only if we are unable to send the request.
/// The reducer will run asynchronously in the future,
/// and its status can be observed by listening for [`Self::on_start_game`] callbacks.
fn start_game(&self) -> __sdk::Result<()>;
/// Register a callback to run whenever we are notified of an invocation of the reducer `start_game`.
///
/// Callbacks should inspect the [`__sdk::ReducerEvent`] contained in the [`super::ReducerEventContext`]
/// to determine the reducer's status.
///
/// The returned [`StartGameCallbackId`] can be passed to [`Self::remove_on_start_game`]
/// to cancel the callback.
fn on_start_game(
&self,
callback: impl FnMut(&super::ReducerEventContext) + Send + 'static,
) -> StartGameCallbackId;
/// Cancel a callback previously registered by [`Self::on_start_game`],
/// causing it not to run in the future.
fn remove_on_start_game(&self, callback: StartGameCallbackId);
}
impl start_game for super::RemoteReducers {
fn start_game(&self) -> __sdk::Result<()> {
self.imp.call_reducer("start_game", StartGameArgs {})
}
fn on_start_game(
&self,
mut callback: impl FnMut(&super::ReducerEventContext) + Send + 'static,
) -> StartGameCallbackId {
StartGameCallbackId(self.imp.on_reducer(
"start_game",
Box::new(move |ctx: &super::ReducerEventContext| {
#[allow(irrefutable_let_patterns)]
let super::ReducerEventContext {
event:
__sdk::ReducerEvent {
reducer: super::Reducer::StartGame {},
..
},
..
} = ctx
else {
unreachable!()
};
callback(ctx)
}),
))
}
fn remove_on_start_game(&self, callback: StartGameCallbackId) {
self.imp.remove_on_reducer("start_game", callback.0)
}
}
#[allow(non_camel_case_types)]
#[doc(hidden)]
/// Extension trait for setting the call-flags for the reducer `start_game`.
///
/// Implemented for [`super::SetReducerFlags`].
///
/// This type is currently unstable and may be removed without a major version bump.
pub trait set_flags_for_start_game {
/// Set the call-reducer flags for the reducer `start_game` to `flags`.
///
/// This type is currently unstable and may be removed without a major version bump.
fn start_game(&self, flags: __ws::CallReducerFlags);
}
impl set_flags_for_start_game for super::SetReducerFlags {
fn start_game(&self, flags: __ws::CallReducerFlags) {
self.imp.set_call_reducer_flags("start_game", flags);
}
}

View file

@ -1,28 +0,0 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
#![allow(unused, clippy::all)]
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)]
#[derive(Copy, Eq, Hash)]
pub enum TurnState {
None,
Tsumo,
Menzen,
RiichiKan,
Discard,
RonChiiPonKan,
End,
}
impl __sdk::InModule for TurnState {
type Module = super::RemoteModule;
}

View file

@ -26,32 +26,6 @@ pub enum TuiSet {
Render, Render,
} }
mod states {
use bevy::prelude::*;
use tui_logger::TuiWidgetState;
#[derive(Resource)]
pub(crate) struct ConsoleWidget {
pub(crate) state: TuiWidgetState,
pub(crate) open: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
pub(crate) enum TuiState {
#[default]
MainMenu,
InGame,
}
// #[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug)]
// #[source(TuiState = TuiState::MainMenu)]
// pub(crate) enum ZenState {
// #[default]
// Menu,
// Zen,
// }
}
impl Plugin for TuiPlugin { impl Plugin for TuiPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(( app.add_plugins((
@ -86,7 +60,7 @@ impl Plugin for TuiPlugin {
.add_systems( .add_systems(
Update, Update,
( (
(render::render_hands, render::render_arg_check).run_if(in_state(GameState::Play)), render::render_hands.run_if(in_state(GameState::Play)),
render::render, render::render,
) )
.chain() .chain()
@ -121,3 +95,29 @@ fn discard_tile(
writer.write(GameMessage::Discarded(message.0)); writer.write(GameMessage::Discarded(message.0));
} }
} }
mod states {
use bevy::prelude::*;
use tui_logger::TuiWidgetState;
#[derive(Resource)]
pub(crate) struct ConsoleWidget {
pub(crate) state: TuiWidgetState,
pub(crate) open: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States, Default)]
pub(crate) enum TuiState {
#[default]
MainMenu,
InGame,
}
// #[derive(SubStates, Default, Clone, Copy, PartialEq, Eq, Hash, Debug)]
// #[source(TuiState = TuiState::MainMenu)]
// pub(crate) enum ZenState {
// #[default]
// Menu,
// Zen,
// }
}

View file

@ -1,21 +1,15 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_ratatui::crossterm::event::KeyCode; use bevy_ratatui::crossterm::event::KeyCode;
use bevy_ratatui::event::KeyMessage; use bevy_ratatui::event::KeyMessage;
use jong::stdb::PlayerTableAccess; // use ratatui::crossterm::event::KeyCode;
use jong::stdb::join_or_create_lobby;
use jong::stdb::start_game;
use tui_logger::TuiWidgetEvent; use tui_logger::TuiWidgetEvent;
use jong::SpacetimeDB;
use jong_types::GameState;
use crate::tui::layout::Overlays; use crate::tui::layout::Overlays;
use crate::tui::states::ConsoleWidget; use crate::tui::states::ConsoleWidget;
use crate::tui::states::TuiState; use crate::tui::states::TuiState;
use jong_types::GameState;
pub(crate) fn keyboard( pub(crate) fn keyboard(
stdb: SpacetimeDB,
mut messages: MessageReader<KeyMessage>, mut messages: MessageReader<KeyMessage>,
mut overlays: ResMut<Overlays>, mut overlays: ResMut<Overlays>,
mut consolewidget: ResMut<ConsoleWidget>, mut consolewidget: ResMut<ConsoleWidget>,
@ -23,6 +17,7 @@ pub(crate) fn keyboard(
curr_gamestate: Res<State<GameState>>, curr_gamestate: Res<State<GameState>>,
curr_tuistate: Res<State<TuiState>>, curr_tuistate: Res<State<TuiState>>,
mut next_gamestate: ResMut<NextState<GameState>>, mut next_gamestate: ResMut<NextState<GameState>>,
mut next_tuistate: ResMut<NextState<TuiState>>, mut next_tuistate: ResMut<NextState<TuiState>>,
) { ) {
@ -43,8 +38,8 @@ pub(crate) fn keyboard(
match curr_tuistate.get() { match curr_tuistate.get() {
TuiState::MainMenu => match key { TuiState::MainMenu => match key {
KeyCode::Char('p') => { KeyCode::Char('p') => {
stdb.reducers().join_or_create_lobby(0).unwrap();
next_tuistate.set(TuiState::InGame); next_tuistate.set(TuiState::InGame);
next_gamestate.set(GameState::Setup);
} }
KeyCode::Char('z') => { KeyCode::Char('z') => {
// if let Some(ref curr_zenstate) = curr_zenstate { // if let Some(ref curr_zenstate) = curr_zenstate {

View file

@ -95,23 +95,6 @@ pub(crate) fn render(
Ok(()) Ok(())
} }
pub(crate) fn render_arg_check(
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>>,
curr_player: Single<Entity, With<CurrentPlayer>>,
players: Query<(&Player, Entity, &Children)>,
hands: Query<(&Children, Entity), (With<Hand>, Without<Player>)>,
// drawn_tile: Single<Entity, With<Drawn>>,
) {
// trace!("arg!");
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)] #[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub(crate) fn render_hands( pub(crate) fn render_hands(
mut commands: Commands, mut commands: Commands,
@ -127,8 +110,6 @@ pub(crate) fn render_hands(
hands: Query<(&Children, Entity), (With<Hand>, Without<Player>)>, hands: Query<(&Children, Entity), (With<Hand>, Without<Player>)>,
drawn_tile: Single<Entity, With<Drawn>>, drawn_tile: Single<Entity, With<Drawn>>,
) -> Result { ) -> Result {
trace!("render_hands");
let mut frame = tui.get_frame(); let mut frame = tui.get_frame();
debug_blocks(*layouts, &mut frame); debug_blocks(*layouts, &mut frame);

View file

@ -8,8 +8,8 @@ default:
just --list just --list
run-tui: run-tui:
# mprocs -s localhost:4050 --ctl $"({c: restart-proc, name: spacetimedb_generate_bindings} | to yaml)" mprocs -s localhost:4050 --ctl $"({c: restart-proc, name: spacetimedb_generate_bindings} | to yaml)"
# sleep 3sec sleep 3sec
cargo run -- run-tui cargo run -- run-tui
update: update:

View file

@ -1,5 +1,5 @@
use log::{info, trace}; use log::info;
use spacetimedb::{ReducerContext, Table, reducer}; use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use crate::tables::*; use crate::tables::*;
use jong_types::*; use jong_types::*;
@ -22,8 +22,7 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
id: 0, id: 0,
host_player_id: player.id, host_player_id: player.id,
players: vec![PlayerOrBot::Player { id: player.id }], players: vec![PlayerOrBot::Player { id: player.id }],
game_state: GameState::Lobby, game_state: GameState::Setup,
turn_state: TurnState::None,
}); });
info!("created lobby: {:?}", lobby); info!("created lobby: {:?}", lobby);
@ -42,10 +41,10 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> { pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
if lobby_id == 0 { if lobby_id == 0 {
Err("cannot add a bot without a lobby".into()) Err("cannot add a bot without a lobby".into())
} else if let Some(mut lobby) = ctx.db.lobby().id().find(lobby_id) } else if let Some(lobby) = ctx.db.lobby().id().find(lobby_id)
&& (ctx.db.player().lobby_id().filter(lobby_id).count() && (ctx.db.player().lobby_id().filter(lobby_id).count()
+ ctx.db.bot().lobby_id().filter(lobby_id).count() + ctx.db.bot().lobby_id().filter(lobby_id).count()
< 4) <= 4)
{ {
let bot = ctx.db.bot().insert(Bot { let bot = ctx.db.bot().insert(Bot {
id: 0, id: 0,
@ -53,34 +52,9 @@ pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
hand_id: 0, hand_id: 0,
pond_id: 0, pond_id: 0,
}); });
lobby.players.push(PlayerOrBot::Bot { id: bot.id });
ctx.db.lobby().id().update(lobby);
info!("added bot {} to lobby {}", bot.id, lobby_id); info!("added bot {} to lobby {}", bot.id, lobby_id);
Ok(()) Ok(())
} else { } else {
Err("lobby doesn't exist".into()) Err("lobby doesn't exist".into())
} }
} }
#[reducer]
pub fn set_ready(ctx: &ReducerContext, ready: bool) {
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
player.ready = ready;
let player = ctx.db.player().identity().update(player);
}
#[reducer]
pub fn start_game(ctx: &ReducerContext) {
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
if let Some(mut lobby) = ctx.db.lobby().host_player_id().find(player.id)
&& lobby.players.len() == 4
&& lobby.players.iter().all(|p| match p {
PlayerOrBot::Player { id } => ctx.db.player().id().find(id).is_some_and(|p| p.ready),
PlayerOrBot::Bot { id } => ctx.db.bot().id().find(id).is_some(),
})
{
lobby.game_state = GameState::Deal;
ctx.db.lobby().id().update(lobby);
}
}

View file

@ -10,7 +10,10 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
debug!("lobby_id: {lobby_id}"); debug!("lobby_id: {lobby_id}");
let mut lobby = ctx.db.lobby().id().find(lobby_id).unwrap(); let mut lobby = ctx.db.lobby().id().find(lobby_id).unwrap();
if lobby.game_state == GameState::Deal { lobby.game_state = GameState::Deal;
let mut lobby = ctx.db.lobby().id().update(lobby);
let tiles = new_shuffled_wall(ctx); let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(Wall { ctx.db.wall().insert(Wall {
// id: 0, // id: 0,
@ -21,9 +24,7 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
deal_hands(ctx, lobby_id); deal_hands(ctx, lobby_id);
lobby.game_state = GameState::Play; lobby.game_state = GameState::Play;
lobby.turn_state = TurnState::Tsumo;
ctx.db.lobby().id().update(lobby); ctx.db.lobby().id().update(lobby);
}
} }
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> { pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> {

View file

@ -1,7 +1,10 @@
use log::info; use log::{debug, info};
use spacetimedb::{ReducerContext, Table, reducer}; use spacetimedb::{
ReducerContext, Table, ViewContext, rand::seq::SliceRandom, reducer, table, view,
};
use crate::tables::*; use crate::tables::*;
use jong_types::*;
mod game; mod game;
mod tables; mod tables;
@ -18,7 +21,6 @@ pub fn login_or_add_player(ctx: &ReducerContext) {
lobby_id: 0, lobby_id: 0,
hand_id: 0, hand_id: 0,
pond_id: 0, pond_id: 0,
ready: false,
}) { }) {
info!("added player: {:?}", player); info!("added player: {:?}", player);
} else { } else {

View file

@ -18,8 +18,6 @@ pub struct Player {
pub lobby_id: u32, pub lobby_id: u32,
pub hand_id: u32, pub hand_id: u32,
pub pond_id: u32, pub pond_id: u32,
pub ready: bool,
} }
#[table(name = bot)] #[table(name = bot)]
@ -52,7 +50,6 @@ pub struct Lobby {
pub players: Vec<PlayerOrBot>, pub players: Vec<PlayerOrBot>,
pub game_state: GameState, pub game_state: GameState,
pub turn_state: TurnState,
} }
#[table(name = wall)] #[table(name = wall)]

18
states
View file

@ -1,18 +0,0 @@
gamestate
none
join_or_create_lobby()
lobby
set_ready()
add_bot()
start_game()
setup
shuffle_deal()
deal
animations??
play
discards??