(stash)
This commit is contained in:
parent
d7b4221727
commit
2d3993452b
33 changed files with 920 additions and 143 deletions
|
|
@ -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!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue