(bug) player now also advances game, but can double discard

This commit is contained in:
Tao Tien 2026-02-24 19:36:39 -08:00
parent e8dd782f59
commit 5ebf3f6c05
4 changed files with 12 additions and 6 deletions

View file

@ -19,9 +19,9 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
// checks every second (or more? when users make moves) on whether to advance the game's various states // checks every second (or more? when users make moves) on whether to advance the game's various states
// TODO this, or allow player/debug to call this? // TODO this, or allow player/debug to call this?
if !ctx.sender_auth().is_internal() { // if !ctx.sender_auth().is_internal() {
return Err("This reducer can only be called by the scheduler".to_string()); // return Err("This reducer can only be called by the scheduler".to_string());
} // }
if let Some(mut lobby) = ctx.db.lobby().id().find(game_timer.lobby_id) { if let Some(mut lobby) = ctx.db.lobby().id().find(game_timer.lobby_id) {
trace!("running schedule for lobby {}", lobby.id); trace!("running schedule for lobby {}", lobby.id);

View file

@ -108,7 +108,7 @@ pub struct Bot {
pub working_tile: Option<DbTile>, pub working_tile: Option<DbTile>,
} }
#[table(name = game_timer, scheduled(advance_game))] #[table(name = game_timer, scheduled(advance_game), public)]
pub struct GameTimer { pub struct GameTimer {
#[primary_key] #[primary_key]
#[auto_inc] #[auto_inc]

View file

@ -4,7 +4,7 @@ use bevy_spacetimedb::{
ReadUpdateMessage, StdbPlugin, ReadUpdateMessage, StdbPlugin,
}; };
use jong_db::{self, add_bot, set_ready}; use jong_db::{self, GameTimerTableAccess, add_bot, set_ready};
use jong_db::{ use jong_db::{
BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables, BotTableAccess, DbConnection, LobbyTableAccess, PlayerHand, PlayerTableAccess, RemoteTables,
ViewClosedHandsTableAccess, ViewHandTableAccess, ViewClosedHandsTableAccess, ViewHandTableAccess,
@ -26,6 +26,7 @@ impl Plugin for Riichi {
.with_run_fn(DbConnection::run_threaded) .with_run_fn(DbConnection::run_threaded)
.add_table(RemoteTables::player) .add_table(RemoteTables::player)
.add_table(RemoteTables::lobby) .add_table(RemoteTables::lobby)
.add_table(RemoteTables::game_timer)
// TODO check bevy_spacetimedb PR status // TODO check bevy_spacetimedb PR status
.add_view_with_pk(RemoteTables::view_hand, |p| p.id) .add_view_with_pk(RemoteTables::view_hand, |p| p.id)
.add_view_with_pk(RemoteTables::view_closed_hands, |p| { .add_view_with_pk(RemoteTables::view_closed_hands, |p| {
@ -102,6 +103,7 @@ fn subscriptions(stdb: SpacetimeDB, mut commands: Commands) {
"SELECT b.* FROM bot b JOIN lobby l ON l.id = b.lobby_id".to_string(), "SELECT b.* FROM bot b JOIN lobby l ON l.id = b.lobby_id".to_string(),
"SELECT * FROM view_hand".to_string(), "SELECT * FROM view_hand".to_string(),
"SELECT * FROM view_closed_hands".to_string(), "SELECT * FROM view_closed_hands".to_string(),
"SELECT g.* FROM game_timer g JOIN player p ON g.lobby_id = p.lobby_id".to_string(),
]); ]);
while let Ok(event) = recv.recv() { while let Ok(event) = recv.recv() {

View file

@ -4,10 +4,11 @@ use std::time::Duration;
use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin}; use bevy::{app::ScheduleRunnerPlugin, prelude::*, state::app::StatesPlugin};
use bevy_ratatui::RatatuiPlugins; use bevy_ratatui::RatatuiPlugins;
use spacetimedb_sdk::Table;
use tui_logger::TuiWidgetState; use tui_logger::TuiWidgetState;
use jong::{SpacetimeDB, riichi::player::*}; use jong::{SpacetimeDB, riichi::player::*};
use jong_db::{self, discard_tile as _}; use jong_db::{self, GameTimerTableAccess, advance_game, discard_tile as _};
use jong_types::states::{GameState, TurnState}; use jong_types::states::{GameState, TurnState};
mod input; mod input;
@ -109,6 +110,9 @@ fn discard_tile(
while let Some(message) = selected.read().next() { while let Some(message) = selected.read().next() {
if let Ok(tile_id) = tiles.get(message.0) { if let Ok(tile_id) = tiles.get(message.0) {
stdb.reducers().discard_tile(tile_id.0).unwrap(); stdb.reducers().discard_tile(tile_id.0).unwrap();
stdb.reducers()
.advance_game(stdb.db().game_timer().iter().next().unwrap())
.unwrap();
commands.entity(drawn.0).remove::<Drawn>(); commands.entity(drawn.0).remove::<Drawn>();
} }
} }