advance_game stuff

This commit is contained in:
Tao Tien 2026-02-28 20:40:55 -08:00
parent 151f7a3489
commit a39ad4cf7c
6 changed files with 95 additions and 79 deletions

View file

@ -15,10 +15,77 @@ mod hand;
mod lobby;
#[reducer]
pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(), String> {
pub fn advance_game(ctx: &ReducerContext) -> Result<(), String> {
let game_timer = ctx
.db
.game_timer()
.lobby_id()
.find(
ctx.db
.player()
.identity()
.find(ctx.sender())
.ok_or("player not in lobby")?
.lobby_id,
)
.ok_or("no such lobby")?;
advance_game_private(ctx, game_timer)
}
fn shuffle_wall(ctx: &ReducerContext, lobby: &mut Lobby) {
let tiles = {
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
};
ctx.db.wall().insert(DbWall {
// id: 0,
lobby_id: lobby.id,
tiles,
});
lobby.game_state = GameState::Deal;
}
fn deal_hands(ctx: &ReducerContext, lobby: &mut Lobby) -> Result<(), String> {
let mut wall = ctx.db.wall().lobby_id().find(lobby.id).unwrap();
for pob in &lobby.players {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort_by_key(|t| t.tile);
match pob {
PlayerOrBot::Player { id } if let Some(p) = ctx.db.player().id().find(id) => {
ctx.db.player_hand().insert(PlayerHand {
id: 0,
player_id: p.id,
turn_state: jong_types::TurnState::None,
pond: vec![],
hand: tiles,
working_tile: None,
});
ctx.db.player_clock().insert(PlayerClock {
id: 0,
player_id: p.id,
renewable: 5,
total: 30,
});
}
PlayerOrBot::Bot { id } if let Some(mut b) = ctx.db.bot().id().find(id) => {
b.hand = tiles;
ctx.db.bot().id().update(b);
}
_ => Err("couldn't find player or bot".to_string())?,
}
}
lobby.game_state = jong_types::states::GameState::Play;
Ok(())
}
#[reducer]
pub fn advance_game_private(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(), String> {
// checks every second (or more? when users make moves) on whether to advance the game's various states
@ -30,60 +97,19 @@ pub fn advance_game_private(ctx: &ReducerContext, mut game_timer: GameTimer) ->
GameState::Setup => {
// TODO reduce interval beforehand so we don't wait a second?
// TODO keep a count to clear stale lobbies
trace!("shuffle wall");
let tiles = {
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
};
ctx.db.wall().insert(DbWall {
// id: 0,
lobby_id: lobby.id,
tiles,
});
lobby.game_state = GameState::Deal;
// trace!("shuffle wall");
shuffle_wall(ctx, &mut lobby);
ctx.db.lobby().id().update(lobby);
advance_game_private(ctx, game_timer)?;
return Ok(());
}
GameState::Deal => {
// TODO reduce interval beforehand so this can animate?
// TODO change loop to be per interval somehow?
// trace!("deal hands");
let mut wall = ctx.db.wall().lobby_id().find(lobby.id).unwrap();
for pob in &lobby.players {
let mut tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
tiles.sort_by_key(|t| t.tile);
match pob {
PlayerOrBot::Player { id }
if let Some(p) = ctx.db.player().id().find(id) =>
{
ctx.db.player_hand().insert(PlayerHand {
id: 0,
player_id: p.id,
turn_state: jong_types::TurnState::None,
pond: vec![],
hand: tiles,
working_tile: None,
});
ctx.db.player_clock().insert(PlayerClock {
id: 0,
player_id: p.id,
renewable: 5,
total: 30,
});
}
PlayerOrBot::Bot { id } if let Some(mut b) = ctx.db.bot().id().find(id) => {
b.hand = tiles;
ctx.db.bot().id().update(b);
}
_ => Err("couldn't find player or bot".to_string())?,
}
}
lobby.game_state = jong_types::states::GameState::Play;
// trace!("dealt hands");
deal_hands(ctx, &mut lobby)?;
ctx.db.lobby().id().update(lobby);
advance_game_private(ctx, game_timer)?;
return Ok(());
}
GameState::Play => {
// trace!("in play");

View file

@ -5,7 +5,7 @@ use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use jong_types::PlayerOrBot;
use crate::tables::*;
use crate::{reducers::advance_game_private, tables::*};
#[reducer]
pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(), String> {
@ -87,11 +87,13 @@ pub fn set_ready(ctx: &ReducerContext, ready: bool) -> Result<(), String> {
let lobby = ctx.db.lobby().id().update(lobby);
// TODO should we schedule this outside so that we can clear out stale lobbies?
ctx.db.game_timer().insert(GameTimer {
let game_timer = ctx.db.game_timer().insert(GameTimer {
id: 0,
lobby_id: lobby.id,
scheduled_at: spacetimedb::ScheduleAt::Interval(Duration::from_secs(1).into()),
});
advance_game_private(ctx, game_timer)?;
} else {
// if lobby doesn't exist, reset player state
player.lobby_id = 0;