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

View file

@ -13,14 +13,14 @@ pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
for mut player in players {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort();
tiles.sort_by_key(|t| t.tile);
player.hand = tiles;
ctx.db.player().id().update(player);
}
for mut bot in bots {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort();
tiles.sort_by_key(|t| t.tile);
bot.hand = tiles;
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);
let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(Wall {
ctx.db.wall().insert(DbWall {
// id: 0,
lobby_id,
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 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

View file

@ -21,9 +21,19 @@ pub struct Lobby {
}
#[table(name = wall)]
pub struct Wall {
pub struct DbWall {
#[primary_key]
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::{SpacetimeType, table};
use jong_types::*;
use super::DbTile;
#[derive(Debug)]
#[table(name = player, public)]
#[derive(Debug)]
pub struct Player {
#[primary_key]
pub identity: Identity,
@ -21,8 +21,8 @@ pub struct Player {
pub sort: bool,
pub hand: Vec<Tile>,
pub pond: Vec<Tile>,
pub hand: Vec<DbTile>,
pub pond: Vec<DbTile>,
}
#[table(name = bot)]
@ -34,8 +34,8 @@ pub struct Bot {
#[index(btree)]
pub lobby_id: u32,
pub hand: Vec<Tile>,
pub pond: Vec<Tile>,
pub hand: Vec<DbTile>,
pub pond: Vec<DbTile>,
}
#[derive(Debug, Clone, SpacetimeType)]