can we call this rewrite 4.5

This commit is contained in:
Tao Tien 2026-02-23 16:18:21 -08:00
parent 2d3993452b
commit a0171b1de6
9 changed files with 128 additions and 120 deletions

View file

@ -1,20 +1,17 @@
use std::time::Duration;
use log::{debug, trace};
use spacetimedb::{
ReducerContext, ScheduleAt::Interval, Table as _, rand::seq::SliceRandom, reducer,
};
use jong_types::{GameState, TurnState};
use crate::{
reducers::deal::{deal_hands, new_shuffled_wall, shuffle_deal},
tables::{
DbTile, DbWall, GameTimer, PlayerClock, PlayerHand, PlayerOrBot, bot, game_timer,
lobby as _, player, player_clock, player_hand, tile as _, wall,
},
use crate::tables::{
DbTile, DbWall, GameTimer, Lobby, PlayerClock, PlayerHand, PlayerOrBot, bot, game_timer,
lobby as _, player, player_clock, player_hand, tile as _, wall,
};
mod deal;
mod hand;
mod lobby;
@ -28,10 +25,12 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
}
if let Some(mut lobby) = ctx.db.lobby().id().find(game_timer.lobby_id) {
trace!("running schedule for lobby {}", lobby.id);
match lobby.game_state {
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()
@ -51,6 +50,7 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
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);
@ -68,6 +68,12 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
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;
@ -77,16 +83,19 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
}
}
lobby.game_state = jong_types::states::GameState::Play;
trace!("dealt hands");
}
GameState::Play => {
trace!("in play");
let curr_player = lobby.players.get(lobby.current_idx as usize).unwrap();
match curr_player {
PlayerOrBot::Player { id: player_id } => {
trace!("current player is {player_id}");
let mut clock = ctx.db.player_clock().player_id().find(player_id).unwrap();
let mut hand = ctx.db.player_hand().player_id().find(player_id).unwrap();
match hand.turn_state {
TurnState::None => {
// TODO draw tile
trace!("draw a tile");
if let Some(mut wall) = ctx.db.wall().lobby_id().find(lobby.id)
&& let Some(tile) = wall.tiles.pop()
{
@ -100,6 +109,7 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
}
}
TurnState::Tsumo => {
trace!("wait for discard");
if clock.tick() {
ctx.db.player_clock().id().update(clock);
} else {
@ -113,7 +123,18 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
}
}
PlayerOrBot::Bot { id: bot_id } => {
let b = ctx.db.bot().id().find(bot_id).unwrap();
debug!("current bot is {bot_id}");
let bot = ctx.db.bot().id().find(bot_id).unwrap();
match bot.turn_state {
// TurnState::None => todo!(),
// TurnState::Tsumo => todo!(),
// TurnState::Menzen => todo!(),
// TurnState::RiichiKan => todo!(),
// TurnState::RonChiiPonKan => todo!(),
// TurnState::End => todo!(),
_ => {}
}
lobby.next_player();
}
}
}
@ -160,3 +181,13 @@ impl PlayerClock {
self.renewable = 5;
}
}
impl Lobby {
fn next_player(&mut self) {
if self.current_idx + 1 >= 4 {
self.current_idx = 0
} else {
self.current_idx += 1;
}
}
}