2026-02-08 23:47:57 -08:00
|
|
|
use log::debug;
|
|
|
|
|
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
|
|
|
|
|
|
|
|
|
use super::hand::deal_hands;
|
|
|
|
|
use crate::tables::*;
|
|
|
|
|
use jong_types::*;
|
|
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
|
2026-02-10 01:38:13 -08:00
|
|
|
if lobby.game_state == GameState::Setup {
|
|
|
|
|
lobby.game_state = GameState::Deal;
|
|
|
|
|
lobby = ctx.db.lobby().id().update(lobby);
|
|
|
|
|
|
2026-02-10 19:38:41 -08:00
|
|
|
let tiles = new_shuffled_wall(ctx);
|
2026-02-12 17:06:28 -08:00
|
|
|
|
|
|
|
|
ctx.db.wall().insert(DbWall {
|
2026-02-10 19:38:41 -08:00
|
|
|
// id: 0,
|
|
|
|
|
lobby_id,
|
|
|
|
|
tiles,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
deal_hands(ctx, lobby_id);
|
|
|
|
|
|
|
|
|
|
lobby.game_state = GameState::Play;
|
|
|
|
|
lobby.turn_state = TurnState::Tsumo;
|
|
|
|
|
ctx.db.lobby().id().update(lobby);
|
|
|
|
|
}
|
2026-02-08 23:47:57 -08:00
|
|
|
}
|
|
|
|
|
|
2026-02-12 17:06:28 -08:00
|
|
|
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
|
2026-02-08 23:47:57 -08:00
|
|
|
let mut rng = ctx.rng();
|
2026-02-12 17:06:28 -08:00
|
|
|
let mut wall: Vec<_> = tiles()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|tile| ctx.db.tile().insert(DbTile { id: 0, tile }))
|
|
|
|
|
.collect();
|
2026-02-08 23:47:57 -08:00
|
|
|
wall.shuffle(&mut rng);
|
|
|
|
|
|
|
|
|
|
wall
|
|
|
|
|
}
|