add a tile table

This commit is contained in:
Tao Tien 2026-02-12 17:06:28 -08:00
parent 9b01f6b96a
commit a1c72b2c48
14 changed files with 232 additions and 42 deletions

1
Cargo.lock generated
View file

@ -1229,6 +1229,7 @@ dependencies = [
"bitflags 2.10.0", "bitflags 2.10.0",
"color-eyre", "color-eyre",
"ratatui", "ratatui",
"smol_str",
"tracing", "tracing",
] ]

View file

@ -3,7 +3,7 @@
#![allow(unused, clippy::all)] #![allow(unused, clippy::all)]
use super::bot_type::Bot; use super::bot_type::Bot;
use super::tile_type::Tile; use super::db_tile_type::DbTile;
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 `bot`. /// Table handle for the table `bot`.

View file

@ -4,15 +4,15 @@
#![allow(unused, clippy::all)] #![allow(unused, clippy::all)]
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
use super::tile_type::Tile; use super::db_tile_type::DbTile;
#[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)]
pub struct Bot { pub struct Bot {
pub id: u32, pub id: u32,
pub lobby_id: u32, pub lobby_id: u32,
pub hand: Vec<Tile>, pub hand: Vec<DbTile>,
pub pond: Vec<Tile>, pub pond: Vec<DbTile>,
} }
impl __sdk::InModule for Bot { impl __sdk::InModule for Bot {

View file

@ -8,11 +8,11 @@ use super::tile_type::Tile;
#[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)]
pub struct Wall { pub struct DbTile {
pub lobby_id: u32, pub id: u32,
pub tiles: Vec<Tile>, pub tile: Tile,
} }
impl __sdk::InModule for Wall { impl __sdk::InModule for DbTile {
type Module = super::RemoteModule; type Module = super::RemoteModule;
} }

View file

@ -0,0 +1,18 @@
// 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::db_tile_type::DbTile;
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
#[sats(crate = __lib)]
pub struct DbWall {
pub lobby_id: u32,
pub tiles: Vec<DbTile>,
}
impl __sdk::InModule for DbWall {
type Module = super::RemoteModule;
}

View file

@ -9,6 +9,8 @@ use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
pub mod add_bot_reducer; pub mod add_bot_reducer;
pub mod bot_table; pub mod bot_table;
pub mod bot_type; pub mod bot_type;
pub mod db_tile_type;
pub mod db_wall_type;
pub mod dragon_type; pub mod dragon_type;
pub mod game_state_type; pub mod game_state_type;
pub mod join_or_create_lobby_reducer; pub mod join_or_create_lobby_reducer;
@ -23,15 +25,17 @@ pub mod set_ready_reducer;
pub mod shuffle_deal_reducer; pub mod shuffle_deal_reducer;
pub mod start_game_reducer; pub mod start_game_reducer;
pub mod suit_type; pub mod suit_type;
pub mod tile_table;
pub mod tile_type; pub mod tile_type;
pub mod turn_state_type; pub mod turn_state_type;
pub mod wall_table; pub mod wall_table;
pub mod wall_type;
pub mod wind_type; pub mod wind_type;
pub use add_bot_reducer::{add_bot, set_flags_for_add_bot, AddBotCallbackId}; pub use add_bot_reducer::{add_bot, set_flags_for_add_bot, AddBotCallbackId};
pub use bot_table::*; pub use bot_table::*;
pub use bot_type::Bot; pub use bot_type::Bot;
pub use db_tile_type::DbTile;
pub use db_wall_type::DbWall;
pub use dragon_type::Dragon; pub use dragon_type::Dragon;
pub use game_state_type::GameState; pub use game_state_type::GameState;
pub use join_or_create_lobby_reducer::{ pub use join_or_create_lobby_reducer::{
@ -50,10 +54,10 @@ pub use set_ready_reducer::{set_flags_for_set_ready, set_ready, SetReadyCallback
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 start_game_reducer::{set_flags_for_start_game, start_game, StartGameCallbackId};
pub use suit_type::Suit; pub use suit_type::Suit;
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 wall_table::*; pub use wall_table::*;
pub use wall_type::Wall;
pub use wind_type::Wind; pub use wind_type::Wind;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
@ -144,7 +148,8 @@ pub struct DbUpdate {
bot: __sdk::TableUpdate<Bot>, bot: __sdk::TableUpdate<Bot>,
lobby: __sdk::TableUpdate<Lobby>, lobby: __sdk::TableUpdate<Lobby>,
player: __sdk::TableUpdate<Player>, player: __sdk::TableUpdate<Player>,
wall: __sdk::TableUpdate<Wall>, tile: __sdk::TableUpdate<DbTile>,
wall: __sdk::TableUpdate<DbWall>,
} }
impl TryFrom<__ws::DatabaseUpdate<__ws::BsatnFormat>> for DbUpdate { impl TryFrom<__ws::DatabaseUpdate<__ws::BsatnFormat>> for DbUpdate {
@ -162,6 +167,9 @@ impl TryFrom<__ws::DatabaseUpdate<__ws::BsatnFormat>> for DbUpdate {
"player" => db_update "player" => db_update
.player .player
.append(player_table::parse_table_update(table_update)?), .append(player_table::parse_table_update(table_update)?),
"tile" => db_update
.tile
.append(tile_table::parse_table_update(table_update)?),
"wall" => db_update "wall" => db_update
.wall .wall
.append(wall_table::parse_table_update(table_update)?), .append(wall_table::parse_table_update(table_update)?),
@ -200,8 +208,11 @@ impl __sdk::DbUpdate for DbUpdate {
diff.player = cache diff.player = cache
.apply_diff_to_table::<Player>("player", &self.player) .apply_diff_to_table::<Player>("player", &self.player)
.with_updates_by_pk(|row| &row.identity); .with_updates_by_pk(|row| &row.identity);
diff.tile = cache
.apply_diff_to_table::<DbTile>("tile", &self.tile)
.with_updates_by_pk(|row| &row.id);
diff.wall = cache diff.wall = cache
.apply_diff_to_table::<Wall>("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 diff
@ -215,7 +226,8 @@ pub struct AppliedDiff<'r> {
bot: __sdk::TableAppliedDiff<'r, Bot>, bot: __sdk::TableAppliedDiff<'r, Bot>,
lobby: __sdk::TableAppliedDiff<'r, Lobby>, lobby: __sdk::TableAppliedDiff<'r, Lobby>,
player: __sdk::TableAppliedDiff<'r, Player>, player: __sdk::TableAppliedDiff<'r, Player>,
wall: __sdk::TableAppliedDiff<'r, Wall>, tile: __sdk::TableAppliedDiff<'r, DbTile>,
wall: __sdk::TableAppliedDiff<'r, DbWall>,
__unused: std::marker::PhantomData<&'r ()>, __unused: std::marker::PhantomData<&'r ()>,
} }
@ -232,7 +244,8 @@ impl<'r> __sdk::AppliedDiff<'r> for AppliedDiff<'r> {
callbacks.invoke_table_row_callbacks::<Bot>("bot", &self.bot, event); callbacks.invoke_table_row_callbacks::<Bot>("bot", &self.bot, event);
callbacks.invoke_table_row_callbacks::<Lobby>("lobby", &self.lobby, event); callbacks.invoke_table_row_callbacks::<Lobby>("lobby", &self.lobby, event);
callbacks.invoke_table_row_callbacks::<Player>("player", &self.player, event); callbacks.invoke_table_row_callbacks::<Player>("player", &self.player, event);
callbacks.invoke_table_row_callbacks::<Wall>("wall", &self.wall, event); callbacks.invoke_table_row_callbacks::<DbTile>("tile", &self.tile, event);
callbacks.invoke_table_row_callbacks::<DbWall>("wall", &self.wall, event);
} }
} }
@ -955,6 +968,7 @@ impl __sdk::SpacetimeModule for RemoteModule {
bot_table::register_table(client_cache); bot_table::register_table(client_cache);
lobby_table::register_table(client_cache); lobby_table::register_table(client_cache);
player_table::register_table(client_cache); player_table::register_table(client_cache);
tile_table::register_table(client_cache);
wall_table::register_table(client_cache); wall_table::register_table(client_cache);
} }
} }

View file

@ -2,8 +2,8 @@
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
#![allow(unused, clippy::all)] #![allow(unused, clippy::all)]
use super::db_tile_type::DbTile;
use super::player_type::Player; use super::player_type::Player;
use super::tile_type::Tile;
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 `player`. /// Table handle for the table `player`.

View file

@ -4,7 +4,7 @@
#![allow(unused, clippy::all)] #![allow(unused, clippy::all)]
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
use super::tile_type::Tile; use super::db_tile_type::DbTile;
#[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,8 +15,8 @@ pub struct Player {
pub lobby_id: u32, pub lobby_id: u32,
pub ready: bool, pub ready: bool,
pub sort: bool, pub sort: bool,
pub hand: Vec<Tile>, pub hand: Vec<DbTile>,
pub pond: Vec<Tile>, pub pond: Vec<DbTile>,
} }
impl __sdk::InModule for Player { impl __sdk::InModule for Player {

143
jong/src/stdb/tile_table.rs Normal file
View file

@ -0,0 +1,143 @@
// 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::db_tile_type::DbTile;
use super::tile_type::Tile;
use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
/// Table handle for the table `tile`.
///
/// Obtain a handle from the [`TileTableAccess::tile`] method on [`super::RemoteTables`],
/// like `ctx.db.tile()`.
///
/// Users are encouraged not to explicitly reference this type,
/// but to directly chain method calls,
/// like `ctx.db.tile().on_insert(...)`.
pub struct TileTableHandle<'ctx> {
imp: __sdk::TableHandle<DbTile>,
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
}
#[allow(non_camel_case_types)]
/// Extension trait for access to the table `tile`.
///
/// Implemented for [`super::RemoteTables`].
pub trait TileTableAccess {
#[allow(non_snake_case)]
/// Obtain a [`TileTableHandle`], which mediates access to the table `tile`.
fn tile(&self) -> TileTableHandle<'_>;
}
impl TileTableAccess for super::RemoteTables {
fn tile(&self) -> TileTableHandle<'_> {
TileTableHandle {
imp: self.imp.get_table::<DbTile>("tile"),
ctx: std::marker::PhantomData,
}
}
}
pub struct TileInsertCallbackId(__sdk::CallbackId);
pub struct TileDeleteCallbackId(__sdk::CallbackId);
impl<'ctx> __sdk::Table for TileTableHandle<'ctx> {
type Row = DbTile;
type EventContext = super::EventContext;
fn count(&self) -> u64 {
self.imp.count()
}
fn iter(&self) -> impl Iterator<Item = DbTile> + '_ {
self.imp.iter()
}
type InsertCallbackId = TileInsertCallbackId;
fn on_insert(
&self,
callback: impl FnMut(&Self::EventContext, &Self::Row) + Send + 'static,
) -> TileInsertCallbackId {
TileInsertCallbackId(self.imp.on_insert(Box::new(callback)))
}
fn remove_on_insert(&self, callback: TileInsertCallbackId) {
self.imp.remove_on_insert(callback.0)
}
type DeleteCallbackId = TileDeleteCallbackId;
fn on_delete(
&self,
callback: impl FnMut(&Self::EventContext, &Self::Row) + Send + 'static,
) -> TileDeleteCallbackId {
TileDeleteCallbackId(self.imp.on_delete(Box::new(callback)))
}
fn remove_on_delete(&self, callback: TileDeleteCallbackId) {
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::<DbTile>("tile");
_table.add_unique_constraint::<u32>("id", |row| &row.id);
}
pub struct TileUpdateCallbackId(__sdk::CallbackId);
impl<'ctx> __sdk::TableWithPrimaryKey for TileTableHandle<'ctx> {
type UpdateCallbackId = TileUpdateCallbackId;
fn on_update(
&self,
callback: impl FnMut(&Self::EventContext, &Self::Row, &Self::Row) + Send + 'static,
) -> TileUpdateCallbackId {
TileUpdateCallbackId(self.imp.on_update(Box::new(callback)))
}
fn remove_on_update(&self, callback: TileUpdateCallbackId) {
self.imp.remove_on_update(callback.0)
}
}
#[doc(hidden)]
pub(super) fn parse_table_update(
raw_updates: __ws::TableUpdate<__ws::BsatnFormat>,
) -> __sdk::Result<__sdk::TableUpdate<DbTile>> {
__sdk::TableUpdate::parse_table_update(raw_updates).map_err(|e| {
__sdk::InternalError::failed_parse("TableUpdate<DbTile>", "TableUpdate")
.with_cause(e)
.into()
})
}
/// Access to the `id` unique index on the table `tile`,
/// which allows point queries on the field of the same name
/// via the [`TileIdUnique::find`] method.
///
/// Users are encouraged not to explicitly reference this type,
/// but to directly chain method calls,
/// like `ctx.db.tile().id().find(...)`.
pub struct TileIdUnique<'ctx> {
imp: __sdk::UniqueConstraintHandle<DbTile, u32>,
phantom: std::marker::PhantomData<&'ctx super::RemoteTables>,
}
impl<'ctx> TileTableHandle<'ctx> {
/// Get a handle on the `id` unique index on the table `tile`.
pub fn id(&self) -> TileIdUnique<'ctx> {
TileIdUnique {
imp: self.imp.get_unique_constraint::<u32>("id"),
phantom: std::marker::PhantomData,
}
}
}
impl<'ctx> TileIdUnique<'ctx> {
/// Find the subscribed row whose `id` column value is equal to `col_val`,
/// if such a row is present in the client cache.
pub fn find(&self, col_val: &u32) -> Option<DbTile> {
self.imp.find(col_val)
}
}

View file

@ -2,8 +2,8 @@
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
#![allow(unused, clippy::all)] #![allow(unused, clippy::all)]
use super::tile_type::Tile; use super::db_tile_type::DbTile;
use super::wall_type::Wall; use super::db_wall_type::DbWall;
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 `wall`. /// Table handle for the table `wall`.
@ -15,7 +15,7 @@ use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws};
/// but to directly chain method calls, /// but to directly chain method calls,
/// like `ctx.db.wall().on_insert(...)`. /// like `ctx.db.wall().on_insert(...)`.
pub struct WallTableHandle<'ctx> { pub struct WallTableHandle<'ctx> {
imp: __sdk::TableHandle<Wall>, imp: __sdk::TableHandle<DbWall>,
ctx: std::marker::PhantomData<&'ctx super::RemoteTables>, ctx: std::marker::PhantomData<&'ctx super::RemoteTables>,
} }
@ -32,7 +32,7 @@ pub trait WallTableAccess {
impl WallTableAccess for super::RemoteTables { impl WallTableAccess for super::RemoteTables {
fn wall(&self) -> WallTableHandle<'_> { fn wall(&self) -> WallTableHandle<'_> {
WallTableHandle { WallTableHandle {
imp: self.imp.get_table::<Wall>("wall"), imp: self.imp.get_table::<DbWall>("wall"),
ctx: std::marker::PhantomData, ctx: std::marker::PhantomData,
} }
} }
@ -42,13 +42,13 @@ pub struct WallInsertCallbackId(__sdk::CallbackId);
pub struct WallDeleteCallbackId(__sdk::CallbackId); pub struct WallDeleteCallbackId(__sdk::CallbackId);
impl<'ctx> __sdk::Table for WallTableHandle<'ctx> { impl<'ctx> __sdk::Table for WallTableHandle<'ctx> {
type Row = Wall; type Row = DbWall;
type EventContext = super::EventContext; type EventContext = super::EventContext;
fn count(&self) -> u64 { fn count(&self) -> u64 {
self.imp.count() self.imp.count()
} }
fn iter(&self) -> impl Iterator<Item = Wall> + '_ { fn iter(&self) -> impl Iterator<Item = DbWall> + '_ {
self.imp.iter() self.imp.iter()
} }
@ -81,7 +81,7 @@ impl<'ctx> __sdk::Table for WallTableHandle<'ctx> {
#[doc(hidden)] #[doc(hidden)]
pub(super) fn register_table(client_cache: &mut __sdk::ClientCache<super::RemoteModule>) { pub(super) fn register_table(client_cache: &mut __sdk::ClientCache<super::RemoteModule>) {
let _table = client_cache.get_or_make_table::<Wall>("wall"); let _table = client_cache.get_or_make_table::<DbWall>("wall");
_table.add_unique_constraint::<u32>("lobby_id", |row| &row.lobby_id); _table.add_unique_constraint::<u32>("lobby_id", |row| &row.lobby_id);
} }
pub struct WallUpdateCallbackId(__sdk::CallbackId); pub struct WallUpdateCallbackId(__sdk::CallbackId);
@ -104,9 +104,9 @@ impl<'ctx> __sdk::TableWithPrimaryKey for WallTableHandle<'ctx> {
#[doc(hidden)] #[doc(hidden)]
pub(super) fn parse_table_update( pub(super) fn parse_table_update(
raw_updates: __ws::TableUpdate<__ws::BsatnFormat>, raw_updates: __ws::TableUpdate<__ws::BsatnFormat>,
) -> __sdk::Result<__sdk::TableUpdate<Wall>> { ) -> __sdk::Result<__sdk::TableUpdate<DbWall>> {
__sdk::TableUpdate::parse_table_update(raw_updates).map_err(|e| { __sdk::TableUpdate::parse_table_update(raw_updates).map_err(|e| {
__sdk::InternalError::failed_parse("TableUpdate<Wall>", "TableUpdate") __sdk::InternalError::failed_parse("TableUpdate<DbWall>", "TableUpdate")
.with_cause(e) .with_cause(e)
.into() .into()
}) })
@ -120,7 +120,7 @@ pub(super) fn parse_table_update(
/// but to directly chain method calls, /// but to directly chain method calls,
/// like `ctx.db.wall().lobby_id().find(...)`. /// like `ctx.db.wall().lobby_id().find(...)`.
pub struct WallLobbyIdUnique<'ctx> { pub struct WallLobbyIdUnique<'ctx> {
imp: __sdk::UniqueConstraintHandle<Wall, u32>, imp: __sdk::UniqueConstraintHandle<DbWall, u32>,
phantom: std::marker::PhantomData<&'ctx super::RemoteTables>, phantom: std::marker::PhantomData<&'ctx super::RemoteTables>,
} }
@ -137,7 +137,7 @@ impl<'ctx> WallTableHandle<'ctx> {
impl<'ctx> WallLobbyIdUnique<'ctx> { impl<'ctx> WallLobbyIdUnique<'ctx> {
/// Find the subscribed row whose `lobby_id` column value is equal to `col_val`, /// Find the subscribed row whose `lobby_id` column value is equal to `col_val`,
/// if such a row is present in the client cache. /// if such a row is present in the client cache.
pub fn find(&self, col_val: &u32) -> Option<Wall> { pub fn find(&self, col_val: &u32) -> Option<DbWall> {
self.imp.find(col_val) self.imp.find(col_val)
} }
} }

View file

@ -13,14 +13,14 @@ pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
for mut player in players { for mut player in players {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13); let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall); wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort(); tiles.sort_by_key(|t| t.tile);
player.hand = tiles; player.hand = tiles;
ctx.db.player().id().update(player); ctx.db.player().id().update(player);
} }
for mut bot in bots { for mut bot in bots {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13); let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall); wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort(); tiles.sort_by_key(|t| t.tile);
bot.hand = tiles; bot.hand = tiles;
ctx.db.bot().id().update(bot); ctx.db.bot().id().update(bot);
} }

View file

@ -15,7 +15,8 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
lobby = ctx.db.lobby().id().update(lobby); 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(DbWall {
// id: 0, // id: 0,
lobby_id, lobby_id,
tiles, tiles,
@ -29,9 +30,12 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
} }
} }
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> { pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
let mut rng = ctx.rng(); let mut rng = ctx.rng();
let mut wall = tiles(); let mut wall: Vec<_> = tiles()
.into_iter()
.map(|tile| ctx.db.tile().insert(DbTile { id: 0, tile }))
.collect();
wall.shuffle(&mut rng); wall.shuffle(&mut rng);
wall wall

View file

@ -21,9 +21,19 @@ pub struct Lobby {
} }
#[table(name = wall)] #[table(name = wall)]
pub struct Wall { pub struct DbWall {
#[primary_key] #[primary_key]
pub lobby_id: u32, pub lobby_id: u32,
pub tiles: Vec<Tile>, pub tiles: Vec<DbTile>,
}
#[table(name = tile)]
#[derive(Debug)]
pub struct DbTile {
#[primary_key]
#[auto_inc]
pub id: u32,
pub tile: jong_types::Tile,
} }

View file

@ -1,10 +1,10 @@
use spacetimedb::Identity; use spacetimedb::Identity;
use spacetimedb::{SpacetimeType, table}; use spacetimedb::{SpacetimeType, table};
use jong_types::*; use super::DbTile;
#[derive(Debug)]
#[table(name = player, public)] #[table(name = player, public)]
#[derive(Debug)]
pub struct Player { pub struct Player {
#[primary_key] #[primary_key]
pub identity: Identity, pub identity: Identity,
@ -21,8 +21,8 @@ pub struct Player {
pub sort: bool, pub sort: bool,
pub hand: Vec<Tile>, pub hand: Vec<DbTile>,
pub pond: Vec<Tile>, pub pond: Vec<DbTile>,
} }
#[table(name = bot)] #[table(name = bot)]
@ -34,8 +34,8 @@ pub struct Bot {
#[index(btree)] #[index(btree)]
pub lobby_id: u32, pub lobby_id: u32,
pub hand: Vec<Tile>, pub hand: Vec<DbTile>,
pub pond: Vec<Tile>, pub pond: Vec<DbTile>,
} }
#[derive(Debug, Clone, SpacetimeType)] #[derive(Debug, Clone, SpacetimeType)]