more refactor, start using stdb for everything???

4.5th rewrite for tui
This commit is contained in:
Tao Tien 2026-02-16 00:05:57 -08:00
parent 034e543d40
commit c3686221aa
29 changed files with 478 additions and 744 deletions

View file

@ -0,0 +1,41 @@
use log::debug;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use super::hand::deal_hands;
use crate::tables::*;
#[reducer]
pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
debug!("lobby_id: {lobby_id}");
let mut lobby = ctx.db.lobby().id().find(lobby_id).unwrap();
if lobby.game_state == jong_types::states::GameState::Setup {
lobby.game_state = jong_types::states::GameState::Deal;
lobby = ctx.db.lobby().id().update(lobby);
let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(DbWall {
// id: 0,
lobby_id,
tiles,
});
deal_hands(ctx, lobby_id);
lobby.game_state = jong_types::states::GameState::Play;
lobby.turn_state = jong_types::states::TurnState::Tsumo;
ctx.db.lobby().id().update(lobby);
}
}
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
let mut rng = ctx.rng();
let mut wall: Vec<_> = jong_types::tiles::tiles()
.into_iter()
.map(|tile| ctx.db.tile().insert(DbTile { id: 0, tile }))
.collect();
wall.shuffle(&mut rng);
wall
}