37 lines
837 B
Rust
37 lines
837 B
Rust
|
|
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();
|
||
|
|
|
||
|
|
lobby.game_state = GameState::Deal;
|
||
|
|
let mut lobby = ctx.db.lobby().id().update(lobby);
|
||
|
|
|
||
|
|
|
||
|
|
let tiles = new_shuffled_wall(ctx);
|
||
|
|
ctx.db.wall().insert(Wall {
|
||
|
|
// id: 0,
|
||
|
|
lobby_id,
|
||
|
|
tiles,
|
||
|
|
});
|
||
|
|
|
||
|
|
deal_hands(ctx, lobby_id);
|
||
|
|
|
||
|
|
lobby.game_state = GameState::Play;
|
||
|
|
ctx.db.lobby().id().update(lobby);
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> {
|
||
|
|
let mut rng = ctx.rng();
|
||
|
|
let mut wall = tiles();
|
||
|
|
wall.shuffle(&mut rng);
|
||
|
|
|
||
|
|
wall
|
||
|
|
}
|