all clients trigger ready, then advance gamestate on last ready
This commit is contained in:
parent
1e6a3ca84b
commit
d183f5d993
18 changed files with 358 additions and 77 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use log::info;
|
||||
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
||||
use log::{info, trace};
|
||||
use spacetimedb::{ReducerContext, Table, reducer};
|
||||
|
||||
use crate::tables::*;
|
||||
use jong_types::*;
|
||||
|
|
@ -22,7 +22,7 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
id: 0,
|
||||
host_player_id: player.id,
|
||||
players: vec![PlayerOrBot::Player { id: player.id }],
|
||||
game_state: GameState::Setup,
|
||||
game_state: GameState::Lobby,
|
||||
turn_state: TurnState::None,
|
||||
});
|
||||
info!("created lobby: {:?}", lobby);
|
||||
|
|
@ -42,10 +42,10 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
|
||||
if lobby_id == 0 {
|
||||
Err("cannot add a bot without a lobby".into())
|
||||
} else if let Some(lobby) = ctx.db.lobby().id().find(lobby_id)
|
||||
} else if let Some(mut lobby) = ctx.db.lobby().id().find(lobby_id)
|
||||
&& (ctx.db.player().lobby_id().filter(lobby_id).count()
|
||||
+ ctx.db.bot().lobby_id().filter(lobby_id).count()
|
||||
<= 4)
|
||||
< 4)
|
||||
{
|
||||
let bot = ctx.db.bot().insert(Bot {
|
||||
id: 0,
|
||||
|
|
@ -53,9 +53,34 @@ pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
|
|||
hand_id: 0,
|
||||
pond_id: 0,
|
||||
});
|
||||
lobby.players.push(PlayerOrBot::Bot { id: bot.id });
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
info!("added bot {} to lobby {}", bot.id, lobby_id);
|
||||
Ok(())
|
||||
} else {
|
||||
Err("lobby doesn't exist".into())
|
||||
}
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn set_ready(ctx: &ReducerContext, ready: bool) {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
player.ready = ready;
|
||||
|
||||
let player = ctx.db.player().identity().update(player);
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn start_game(ctx: &ReducerContext) {
|
||||
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
if let Some(mut lobby) = ctx.db.lobby().host_player_id().find(player.id)
|
||||
&& lobby.players.len() == 4
|
||||
&& lobby.players.iter().all(|p| match p {
|
||||
PlayerOrBot::Player { id } => ctx.db.player().id().find(id).is_some_and(|p| p.ready),
|
||||
PlayerOrBot::Bot { id } => ctx.db.bot().id().find(id).is_some(),
|
||||
})
|
||||
{
|
||||
lobby.game_state = GameState::Deal;
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,21 +10,20 @@ 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();
|
||||
|
||||
lobby.game_state = GameState::Deal;
|
||||
let mut lobby = ctx.db.lobby().id().update(lobby);
|
||||
if lobby.game_state == GameState::Deal {
|
||||
let tiles = new_shuffled_wall(ctx);
|
||||
ctx.db.wall().insert(Wall {
|
||||
// id: 0,
|
||||
lobby_id,
|
||||
tiles,
|
||||
});
|
||||
|
||||
let tiles = new_shuffled_wall(ctx);
|
||||
ctx.db.wall().insert(Wall {
|
||||
// id: 0,
|
||||
lobby_id,
|
||||
tiles,
|
||||
});
|
||||
deal_hands(ctx, lobby_id);
|
||||
|
||||
deal_hands(ctx, lobby_id);
|
||||
|
||||
lobby.game_state = GameState::Play;
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
lobby.game_state = GameState::Play;
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
use log::{debug, info};
|
||||
use spacetimedb::{
|
||||
ReducerContext, Table, ViewContext, rand::seq::SliceRandom, reducer, table, view,
|
||||
};
|
||||
use log::info;
|
||||
use spacetimedb::{ReducerContext, Table, reducer};
|
||||
|
||||
use crate::tables::*;
|
||||
use jong_types::*;
|
||||
|
||||
mod game;
|
||||
mod tables;
|
||||
|
|
@ -21,6 +18,7 @@ pub fn login_or_add_player(ctx: &ReducerContext) {
|
|||
lobby_id: 0,
|
||||
hand_id: 0,
|
||||
pond_id: 0,
|
||||
ready: false,
|
||||
}) {
|
||||
info!("added player: {:?}", player);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ pub struct Player {
|
|||
pub lobby_id: u32,
|
||||
pub hand_id: u32,
|
||||
pub pond_id: u32,
|
||||
|
||||
pub ready: bool,
|
||||
}
|
||||
|
||||
#[table(name = bot)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue