(stash) render other players

This commit is contained in:
Tao Tien 2026-02-24 00:56:34 -08:00
parent 33cb9c7f97
commit cc07cc89c6
10 changed files with 249 additions and 14 deletions

View file

@ -0,0 +1,39 @@
// 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};
use super::player_or_bot_type::PlayerOrBot;
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)]
pub struct HandView {
pub player: PlayerOrBot,
pub hand_length: u8,
pub drawn: bool,
}
impl __sdk::InModule for HandView {
type Module = super::RemoteModule;
}
/// Column accessor struct for the table `HandView`.
///
/// Provides typed access to columns for query building.
pub struct HandViewCols {
pub player: __sdk::__query_builder::Col<HandView, PlayerOrBot>,
pub hand_length: __sdk::__query_builder::Col<HandView, u8>,
pub drawn: __sdk::__query_builder::Col<HandView, bool>,
}
impl __sdk::__query_builder::HasCols for HandView {
type Cols = HandViewCols;
fn cols(table_name: &'static str) -> Self::Cols {
HandViewCols {
player: __sdk::__query_builder::Col::new(table_name, "player"),
hand_length: __sdk::__query_builder::Col::new(table_name, "hand_length"),
drawn: __sdk::__query_builder::Col::new(table_name, "drawn"),
}
}
}

View file

@ -20,6 +20,7 @@ pub mod dragon_type;
pub mod game_state_type; pub mod game_state_type;
pub mod game_timer_table; pub mod game_timer_table;
pub mod game_timer_type; pub mod game_timer_type;
pub mod hand_view_type;
pub mod join_or_create_lobby_reducer; pub mod join_or_create_lobby_reducer;
pub mod lobby_table; pub mod lobby_table;
pub mod lobby_type; pub mod lobby_type;
@ -37,6 +38,7 @@ pub mod suit_type;
pub mod tile_table; pub mod tile_table;
pub mod tile_type; pub mod tile_type;
pub mod turn_state_type; pub mod turn_state_type;
pub mod view_closed_hands_table;
pub mod view_hand_table; pub mod view_hand_table;
pub mod wall_table; pub mod wall_table;
pub mod wind_type; pub mod wind_type;
@ -55,6 +57,7 @@ pub use dragon_type::Dragon;
pub use game_state_type::GameState; pub use game_state_type::GameState;
pub use game_timer_table::*; pub use game_timer_table::*;
pub use game_timer_type::GameTimer; pub use game_timer_type::GameTimer;
pub use hand_view_type::HandView;
pub use join_or_create_lobby_reducer::{ pub use join_or_create_lobby_reducer::{
join_or_create_lobby, set_flags_for_join_or_create_lobby, JoinOrCreateLobbyCallbackId, join_or_create_lobby, set_flags_for_join_or_create_lobby, JoinOrCreateLobbyCallbackId,
}; };
@ -74,6 +77,7 @@ pub use suit_type::Suit;
pub use tile_table::*; pub use tile_table::*;
pub use tile_type::Tile; pub use tile_type::Tile;
pub use turn_state_type::TurnState; pub use turn_state_type::TurnState;
pub use view_closed_hands_table::*;
pub use view_hand_table::*; pub use view_hand_table::*;
pub use wall_table::*; pub use wall_table::*;
pub use wind_type::Wind; pub use wind_type::Wind;
@ -190,6 +194,7 @@ pub struct DbUpdate {
player_clock: __sdk::TableUpdate<PlayerClock>, player_clock: __sdk::TableUpdate<PlayerClock>,
player_hand: __sdk::TableUpdate<PlayerHand>, player_hand: __sdk::TableUpdate<PlayerHand>,
tile: __sdk::TableUpdate<DbTile>, tile: __sdk::TableUpdate<DbTile>,
view_closed_hands: __sdk::TableUpdate<HandView>,
view_hand: __sdk::TableUpdate<PlayerHand>, view_hand: __sdk::TableUpdate<PlayerHand>,
wall: __sdk::TableUpdate<DbWall>, wall: __sdk::TableUpdate<DbWall>,
} }
@ -224,6 +229,9 @@ impl TryFrom<__ws::DatabaseUpdate<__ws::BsatnFormat>> for DbUpdate {
"tile" => db_update "tile" => db_update
.tile .tile
.append(tile_table::parse_table_update(table_update)?), .append(tile_table::parse_table_update(table_update)?),
"view_closed_hands" => db_update
.view_closed_hands
.append(view_closed_hands_table::parse_table_update(table_update)?),
"view_hand" => db_update "view_hand" => db_update
.view_hand .view_hand
.append(view_hand_table::parse_table_update(table_update)?), .append(view_hand_table::parse_table_update(table_update)?),
@ -283,6 +291,8 @@ impl __sdk::DbUpdate for DbUpdate {
diff.wall = cache diff.wall = cache
.apply_diff_to_table::<DbWall>("wall", &self.wall) .apply_diff_to_table::<DbWall>("wall", &self.wall)
.with_updates_by_pk(|row| &row.lobby_id); .with_updates_by_pk(|row| &row.lobby_id);
diff.view_closed_hands =
cache.apply_diff_to_table::<HandView>("view_closed_hands", &self.view_closed_hands);
diff.view_hand = cache.apply_diff_to_table::<PlayerHand>("view_hand", &self.view_hand); diff.view_hand = cache.apply_diff_to_table::<PlayerHand>("view_hand", &self.view_hand);
diff diff
@ -301,6 +311,7 @@ pub struct AppliedDiff<'r> {
player_clock: __sdk::TableAppliedDiff<'r, PlayerClock>, player_clock: __sdk::TableAppliedDiff<'r, PlayerClock>,
player_hand: __sdk::TableAppliedDiff<'r, PlayerHand>, player_hand: __sdk::TableAppliedDiff<'r, PlayerHand>,
tile: __sdk::TableAppliedDiff<'r, DbTile>, tile: __sdk::TableAppliedDiff<'r, DbTile>,
view_closed_hands: __sdk::TableAppliedDiff<'r, HandView>,
view_hand: __sdk::TableAppliedDiff<'r, PlayerHand>, view_hand: __sdk::TableAppliedDiff<'r, PlayerHand>,
wall: __sdk::TableAppliedDiff<'r, DbWall>, wall: __sdk::TableAppliedDiff<'r, DbWall>,
__unused: std::marker::PhantomData<&'r ()>, __unused: std::marker::PhantomData<&'r ()>,
@ -332,6 +343,11 @@ impl<'r> __sdk::AppliedDiff<'r> for AppliedDiff<'r> {
); );
callbacks.invoke_table_row_callbacks::<PlayerHand>("player_hand", &self.player_hand, event); callbacks.invoke_table_row_callbacks::<PlayerHand>("player_hand", &self.player_hand, event);
callbacks.invoke_table_row_callbacks::<DbTile>("tile", &self.tile, event); callbacks.invoke_table_row_callbacks::<DbTile>("tile", &self.tile, event);
callbacks.invoke_table_row_callbacks::<HandView>(
"view_closed_hands",
&self.view_closed_hands,
event,
);
callbacks.invoke_table_row_callbacks::<PlayerHand>("view_hand", &self.view_hand, event); callbacks.invoke_table_row_callbacks::<PlayerHand>("view_hand", &self.view_hand, event);
callbacks.invoke_table_row_callbacks::<DbWall>("wall", &self.wall, event); callbacks.invoke_table_row_callbacks::<DbWall>("wall", &self.wall, event);
} }
@ -1062,6 +1078,7 @@ impl __sdk::SpacetimeModule for RemoteModule {
player_clock_table::register_table(client_cache); player_clock_table::register_table(client_cache);
player_hand_table::register_table(client_cache); player_hand_table::register_table(client_cache);
tile_table::register_table(client_cache); tile_table::register_table(client_cache);
view_closed_hands_table::register_table(client_cache);
view_hand_table::register_table(client_cache); view_hand_table::register_table(client_cache);
wall_table::register_table(client_cache); wall_table::register_table(client_cache);
} }

View file

@ -0,0 +1,112 @@
// 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 super::hand_view_type::HandView;
use super::player_or_bot_type::PlayerOrBot;
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
/// Table handle for the table `view_closed_hands`.
///
/// Obtain a handle from the [`ViewClosedHandsTableAccess::view_closed_hands`] method on [`super::RemoteTables`],
/// like `ctx.db.view_closed_hands()`.
///
/// Users are encouraged not to explicitly reference this type,
/// but to directly chain method calls,
/// like `ctx.db.view_closed_hands().on_insert(...)`.
pub struct ViewClosedHandsTableHandle<'ctx> {
imp: __sdk::TableHandle<HandView>,
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}
#[allow(non_camel_case_types)]
/// Extension trait for access to the table `view_closed_hands`.
///
/// Implemented for [`super::RemoteTables`].
pub trait ViewClosedHandsTableAccess {
#[allow(non_snake_case)]
/// Obtain a [`ViewClosedHandsTableHandle`], which mediates access to the table `view_closed_hands`.
fn view_closed_hands(&self) -> ViewClosedHandsTableHandle<'_>;
}
impl ViewClosedHandsTableAccess for super::RemoteTables {
fn view_closed_hands(&self) -> ViewClosedHandsTableHandle<'_> {
ViewClosedHandsTableHandle {
imp: self.imp.get_table::<HandView>("view_closed_hands"),
ctx: std::marker::PhantomData,
}
}
}
pub struct ViewClosedHandsInsertCallbackId(__sdk::CallbackId);
pub struct ViewClosedHandsDeleteCallbackId(__sdk::CallbackId);
impl<'ctx> __sdk::Table for ViewClosedHandsTableHandle<'ctx> {
type Row = HandView;
type EventContext = super::EventContext;
fn count(&self) -> u64 {
self.imp.count()
}
fn iter(&self) -> impl Iterator<Item = HandView> + '_ {
self.imp.iter()
}
type InsertCallbackId = ViewClosedHandsInsertCallbackId;
fn on_insert(
&self,
callback: impl FnMut(&Self::EventContext, &Self::Row) + Send + 'static,
) -> ViewClosedHandsInsertCallbackId {
ViewClosedHandsInsertCallbackId(self.imp.on_insert(Box::new(callback)))
}
fn remove_on_insert(&self, callback: ViewClosedHandsInsertCallbackId) {
self.imp.remove_on_insert(callback.0)
}
type DeleteCallbackId = ViewClosedHandsDeleteCallbackId;
fn on_delete(
&self,
callback: impl FnMut(&Self::EventContext, &Self::Row) + Send + 'static,
) -> ViewClosedHandsDeleteCallbackId {
ViewClosedHandsDeleteCallbackId(self.imp.on_delete(Box::new(callback)))
}
fn remove_on_delete(&self, callback: ViewClosedHandsDeleteCallbackId) {
self.imp.remove_on_delete(callback.0)
}
}
#[doc(hidden)]
pub(super) fn register_table(client_cache: &mut __sdk::ClientCache<super::RemoteModule>) {
let _table = client_cache.get_or_make_table::<HandView>("view_closed_hands");
}
#[doc(hidden)]
pub(super) fn parse_table_update(
raw_updates: __ws::TableUpdate<__ws::BsatnFormat>,
) -> __sdk::Result<__sdk::TableUpdate<HandView>> {
__sdk::TableUpdate::parse_table_update(raw_updates).map_err(|e| {
__sdk::InternalError::failed_parse("TableUpdate<HandView>", "TableUpdate")
.with_cause(e)
.into()
})
}
#[allow(non_camel_case_types)]
/// Extension trait for query builder access to the table `HandView`.
///
/// Implemented for [`__sdk::QueryTableAccessor`].
pub trait view_closed_handsQueryTableAccess {
#[allow(non_snake_case)]
/// Get a query builder for the table `HandView`.
fn view_closed_hands(&self) -> __sdk::__query_builder::Table<HandView>;
}
impl view_closed_handsQueryTableAccess for __sdk::QueryTableAccessor {
fn view_closed_hands(&self) -> __sdk::__query_builder::Table<HandView> {
__sdk::__query_builder::Table::new("view_closed_hands")
}
}

View file

@ -55,4 +55,13 @@ mod conversions {
Self::from_repr(value as usize).unwrap() Self::from_repr(value as usize).unwrap()
} }
} }
impl From<&crate::db::PlayerOrBot> for jong_types::PlayerOrBot {
fn from(value: &crate::db::PlayerOrBot) -> Self {
match value {
crate::PlayerOrBot::Player(id) => Self::Player { id: *id },
crate::PlayerOrBot::Bot(id) => Self::Bot { id: *id },
}
}
}
} }

View file

@ -5,12 +5,11 @@ use spacetimedb::{
ReducerContext, ScheduleAt::Interval, Table as _, rand::seq::SliceRandom, reducer, ReducerContext, ScheduleAt::Interval, Table as _, rand::seq::SliceRandom, reducer,
}; };
use jong_types::{GameState, TurnState};
use crate::tables::{ use crate::tables::{
DbTile, DbWall, GameTimer, Lobby, PlayerClock, PlayerHand, PlayerOrBot, bot, game_timer, DbTile, DbWall, GameTimer, Lobby, PlayerClock, PlayerHand, bot, game_timer, lobby as _, player,
lobby as _, player, player_clock, player_hand, tile as _, wall, player_clock, player_hand, tile as _, wall,
}; };
use jong_types::{GameState, PlayerOrBot, TurnState};
mod hand; mod hand;
mod lobby; mod lobby;

View file

@ -47,9 +47,12 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
hand.pond.push(dealt_tile); hand.pond.push(dealt_tile);
hand.working_tile = None; hand.working_tile = None;
hand.turn_state = TurnState::None; hand.turn_state = TurnState::None;
ctx.db.player_hand().id().update(hand); ctx.db.player_hand().id().update(hand);
let mut clock = ctx.db.player_clock().player_id().find(player.id).unwrap();
clock.renew();
ctx.db.player_clock().player_id().update(clock);
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap(); let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
lobby.next_player(); lobby.next_player();
ctx.db.lobby().id().update(lobby); ctx.db.lobby().id().update(lobby);

View file

@ -3,6 +3,8 @@ use std::time::Duration;
use log::info; use log::info;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer}; use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use jong_types::PlayerOrBot;
use crate::tables::*; use crate::tables::*;
#[reducer] #[reducer]

View file

@ -1,6 +1,7 @@
use spacetimedb::{SpacetimeType, ViewContext, table, view}; use spacetimedb::{SpacetimeType, ViewContext, table, view};
use jong_types::{ use jong_types::{
PlayerOrBot,
states::{GameState, TurnState}, states::{GameState, TurnState},
tiles::Tile, tiles::Tile,
}; };
@ -19,6 +20,7 @@ pub struct Lobby {
pub current_idx: u8, pub current_idx: u8,
pub game_state: GameState, pub game_state: GameState,
// pub open_hands: bool,
} }
#[table(name = wall)] #[table(name = wall)]
@ -39,12 +41,6 @@ pub struct DbTile {
pub tile: Tile, pub tile: Tile,
} }
#[derive(Debug, Clone, SpacetimeType)]
pub enum PlayerOrBot {
Player { id: u32 },
Bot { id: u32 },
}
#[table(name = player, public)] #[table(name = player, public)]
#[table(name = logged_out_player)] #[table(name = logged_out_player)]
#[derive(Debug)] #[derive(Debug)]
@ -132,3 +128,48 @@ fn view_hand(ctx: &ViewContext) -> Option<PlayerHand> {
.find(ctx.sender) .find(ctx.sender)
.and_then(|p| ctx.db.player_hand().player_id().find(p.id)) .and_then(|p| ctx.db.player_hand().player_id().find(p.id))
} }
#[derive(SpacetimeType, Clone, Copy)]
pub struct HandView {
pub player: PlayerOrBot,
pub hand_length: u8,
// pub melds: u8,
pub drawn: bool,
}
#[view(name = view_closed_hands, public)]
fn view_closed_hands(ctx: &ViewContext) -> Vec<HandView> {
let this_player = ctx.db.player().identity().find(ctx.sender).unwrap();
ctx.db
.lobby()
.id()
.find(this_player.lobby_id)
.unwrap()
.players
.iter()
.filter_map(|&player| {
let (hand_length, drawn) = match player {
PlayerOrBot::Player { id } => {
let player_hand = ctx.db.player_hand().player_id().find(id).unwrap();
(
player_hand.hand.len() as u8,
player_hand.turn_state == TurnState::Tsumo
&& player_hand.working_tile.is_some(),
)
}
PlayerOrBot::Bot { id } => {
let bot = ctx.db.bot().id().find(id).unwrap();
(
bot.hand.len() as u8,
bot.turn_state == TurnState::Tsumo && bot.working_tile.is_some(),
)
}
};
Some(HandView {
player,
hand_length,
drawn,
})
})
.collect()
}

View file

@ -9,8 +9,17 @@ mod derive_alias {
} }
use derive_aliases::derive; use derive_aliases::derive;
use spacetimedb::SpacetimeType;
pub mod states; pub mod states;
pub mod tiles; pub mod tiles;
pub use states::*; pub use states::*;
pub use tiles::*; pub use tiles::*;
#[derive(Debug, ..Copy, ..Eq, Hash)]
#[derive(SpacetimeType)]
pub enum PlayerOrBot {
Player { id: u32 },
Bot { id: u32 },
}

View file

@ -6,7 +6,7 @@ use bevy_spacetimedb::{
use jong_db::{ use jong_db::{
self, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables, self, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables,
ViewHandTableAccess as _, ViewClosedHandsTableAccess, ViewHandTableAccess as _,
}; };
use jong_db::{add_bot, set_ready}; use jong_db::{add_bot, set_ready};
use jong_types::*; use jong_types::*;
@ -27,7 +27,10 @@ impl Plugin for Riichi {
.add_table(RemoteTables::player) .add_table(RemoteTables::player)
.add_table(RemoteTables::lobby) .add_table(RemoteTables::lobby)
// TODO check bevy_spacetimedb PR status // TODO check bevy_spacetimedb PR status
.add_view_with_pk(RemoteTables::view_hand, |p| p.id); .add_view_with_pk(RemoteTables::view_hand, |p| p.id)
.add_view_with_pk(RemoteTables::view_closed_hands, |p| {
PlayerOrBot::from(&p.player)
});
let plugins = let plugins =
if let Some(token) = creds_store().load().expect("i/o error loading credentials") { if let Some(token) = creds_store().load().expect("i/o error loading credentials") {
// FIXME patch plugin so this takes Option? // FIXME patch plugin so this takes Option?
@ -91,8 +94,9 @@ fn subscriptions(stdb: SpacetimeDB, mut commands: Commands) {
), ),
"SELECT l.* FROM lobby l JOIN player p ON l.id = p.lobby_id".to_string(), "SELECT l.* FROM lobby l JOIN player p ON l.id = p.lobby_id".to_string(),
"SELECT c.* FROM player_clock c JOIN player p ON c.player_id = p.id".to_string(), "SELECT c.* FROM player_clock c JOIN player p ON c.player_id = p.id".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(),
"SELECT * FROM view_hand".to_string(),
"SELECT * FROM view_closed_hands".to_string(),
]); ]);
while let Ok(event) = recv.recv() { while let Ok(event) = recv.recv() {