jong/spacetimedb/src/reducers/game/deal.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2026-02-13 07:49:09 -08:00
use log::{debug, trace};
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use super::hand::deal_hands;
2026-02-13 07:49:09 -08:00
use crate::tables::{player::player, *};
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);
let tiles = new_shuffled_wall(ctx);
2026-02-12 17:06:28 -08:00
ctx.db.wall().insert(DbWall {
// 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-12 17:06:28 -08:00
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
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();
wall.shuffle(&mut rng);
wall
}