This commit is contained in:
Tao Tien 2026-02-22 00:39:51 -08:00
parent d7b4221727
commit 2d3993452b
33 changed files with 920 additions and 143 deletions

View file

@ -1,3 +1,5 @@
#![feature(if_let_guard)]
use log::debug;
use spacetimedb::{ReducerContext, Table, reducer};

View file

@ -1,14 +1,16 @@
use std::time::Duration;
use spacetimedb::{ReducerContext, ScheduleAt::Interval, reducer};
use spacetimedb::{
ReducerContext, ScheduleAt::Interval, Table as _, rand::seq::SliceRandom, reducer,
};
use jong_types::{GameState, TurnState};
use crate::{
reducers::deal::shuffle_deal,
reducers::deal::{deal_hands, new_shuffled_wall, shuffle_deal},
tables::{
GameTimer, PlayerClock, PlayerOrBot, bot, game_timer, lobby as _, player_clock,
player_hand, wall,
DbTile, DbWall, GameTimer, PlayerClock, PlayerHand, PlayerOrBot, bot, game_timer,
lobby as _, player, player_clock, player_hand, tile as _, wall,
},
};
@ -20,6 +22,7 @@ mod lobby;
pub fn advance_game(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
// TODO this, or allow player/debug to call this?
if !ctx.sender_auth().is_internal() {
return Err("This reducer can only be called by the scheduler".to_string());
}
@ -29,11 +32,51 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
GameState::Setup => {
// TODO reduce interval beforehand so we don't wait a second?
// TODO keep a count to clear stale lobbies
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;
}
GameState::Deal => {
// TODO reduce interval beforehand so this can animate?
shuffle_deal(ctx, lobby.id);
// TODO change loop to be per interval somehow?
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,
});
}
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;
}
GameState::Play => {
let curr_player = lobby.players.get(lobby.current_idx as usize).unwrap();
@ -87,7 +130,8 @@ pub fn advance_game(ctx: &ReducerContext, mut game_timer: GameTimer) -> Result<(
_ => Err(format!("lobby {} in impossible state", lobby.id))?,
}
ctx.db.game_timer().id().update(game_timer);
// ctx.db.game_timer().id().update(game_timer);
ctx.db.lobby().id().update(lobby);
} else {
ctx.db.game_timer().id().delete(game_timer.id);
Err(format!(

View file

@ -9,15 +9,12 @@ pub fn shuffle_deal(ctx: &ReducerContext, lobby_id: u32) {
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);
}

View file

@ -15,6 +15,8 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
.ok_or(format!("cannot find player {}", ctx.sender))?;
if lobby_id == 0 {
// TODO check first if player is already in a lobby
let lobby = ctx.db.lobby().insert(Lobby {
id: 0,
players: vec![PlayerOrBot::Player { id: player.id }],

View file

@ -1,6 +1,4 @@
use std::time::Instant;
use spacetimedb::{SpacetimeType, table};
use spacetimedb::{SpacetimeType, ViewContext, table, view};
use jong_types::{
states::{GameState, TurnState},
@ -47,7 +45,6 @@ pub enum PlayerOrBot {
Bot { id: u32 },
}
// FIXME this shant be public, use views
#[table(name = player, public)]
#[table(name = logged_out_player)]
#[derive(Debug)]
@ -98,7 +95,7 @@ pub struct PlayerHand {
pub working_tile: Option<DbTile>,
}
#[table(name = bot)]
#[table(name = bot, public)]
pub struct Bot {
#[primary_key]
#[auto_inc]
@ -126,3 +123,12 @@ pub struct GameTimer {
pub scheduled_at: spacetimedb::ScheduleAt,
}
#[view(name = view_hand, public)]
fn view_hand(ctx: &ViewContext) -> Option<PlayerHand> {
ctx.db
.player()
.identity()
.find(ctx.sender)
.and_then(|p| ctx.db.player_hand().player_id().find(p.id))
}