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;
}
}
}

View file

@ -1,61 +0,0 @@
use log::debug;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use crate::tables::*;
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();
if lobby.game_state == jong_types::states::GameState::Deal {
let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(DbWall {
// id: 0,
lobby_id,
tiles,
});
deal_hands(ctx, lobby_id);
lobby.game_state = jong_types::states::GameState::Play;
ctx.db.lobby().id().update(lobby);
}
}
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
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
}
pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
let players = ctx.db.player().lobby_id().filter(lobby_id);
let bots = ctx.db.bot().lobby_id().filter(lobby_id);
let mut wall = ctx.db.wall().lobby_id().find(lobby_id).unwrap();
// FIXME rectify deal orders
for player in 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);
ctx.db.player_hand().insert(PlayerHand {
id: 0,
player_id: player.id,
turn_state: jong_types::TurnState::None,
pond: vec![],
hand: tiles,
working_tile: None,
});
}
for mut bot in bots {
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);
bot.hand = tiles;
ctx.db.bot().id().update(bot);
}
}

View file

@ -11,6 +11,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
let mut hand = ctx.db.player_hand().player_id().find(player.id).unwrap();
// TODO we can probably remove a buncha these errors
let dealt_tile = if let Some(dealt) = ctx.db.tile().id().find(tile_id) {
if let Some(drawn) = hand.working_tile {
if drawn.id == dealt.id {
@ -49,6 +50,10 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
ctx.db.player_hand().id().update(hand);
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
lobby.next_player();
ctx.db.lobby().id().update(lobby);
debug!("player {} discarded tile {:?}", player.id, dealt_tile.tile);
Ok(())

View file

@ -14,7 +14,7 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
.find(ctx.sender)
.ok_or(format!("cannot find player {}", ctx.sender))?;
if lobby_id == 0 {
if lobby_id == 0 && player.lobby_id == 0 {
// TODO check first if player is already in a lobby
let lobby = ctx.db.lobby().insert(Lobby {
@ -27,10 +27,17 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
info!("created lobby: {}", lobby.id);
lobby_id = lobby.id;
player.lobby_id = lobby_id;
} else {
let lobby = ctx
.db
.lobby()
.id()
.find(player.lobby_id)
.ok_or(format!("can't find lobby {}", player.lobby_id))?;
lobby_id = lobby.id;
}
player.lobby_id = lobby_id;
let player = ctx.db.player().identity().update(player);
info!("player {} joined lobby {}", player.id, lobby_id);