skip_calls, discard drawn works
This commit is contained in:
parent
8c4132e628
commit
b6e7635122
16 changed files with 222 additions and 59 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use log::{info, trace};
|
||||
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
||||
|
||||
use crate::tables::{player::player, wall, *};
|
||||
use crate::tables::{player::player, *};
|
||||
use jong_types::*;
|
||||
|
||||
mod deal;
|
||||
|
|
@ -24,7 +24,8 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
players: vec![PlayerOrBot::Player { id: player.id }],
|
||||
game_state: GameState::Lobby,
|
||||
turn_state: TurnState::None,
|
||||
// dealt_idx: -1,
|
||||
dealer_idx: 0,
|
||||
current_idx: 0,
|
||||
});
|
||||
info!("created lobby: {:?}", lobby);
|
||||
|
||||
|
|
@ -53,6 +54,7 @@ pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
|
|||
lobby_id,
|
||||
hand: vec![],
|
||||
pond: vec![],
|
||||
drawn_tile: None,
|
||||
});
|
||||
lobby.players.push(PlayerOrBot::Bot { id: bot.id });
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
|
|
@ -83,6 +85,10 @@ pub fn start_game(ctx: &ReducerContext) {
|
|||
{
|
||||
lobby.game_state = GameState::Setup;
|
||||
lobby.players.shuffle(&mut ctx.rng());
|
||||
lobby.dealer_idx += 1;
|
||||
if lobby.dealer_idx > 3 {
|
||||
lobby.dealer_idx = 0;
|
||||
}
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
0
spacetimedb/src/reducers/game/bot.rs
Normal file
0
spacetimedb/src/reducers/game/bot.rs
Normal file
|
|
@ -40,44 +40,3 @@ pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
|
|||
|
||||
wall
|
||||
}
|
||||
|
||||
// TODO make sure this can't be called or just error here?
|
||||
#[reducer]
|
||||
pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
||||
trace!("discard_tile");
|
||||
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
|
||||
let tile = if let Some(drawn) = player.drawn_tile
|
||||
&& drawn.id == tile_id
|
||||
{
|
||||
player.hand.push(drawn);
|
||||
// lobby.dealt_idx = -1;
|
||||
|
||||
drawn
|
||||
} else if let Some((i, &hand)) = player
|
||||
.hand
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.id == tile_id)
|
||||
{
|
||||
player.hand.remove(i);
|
||||
// lobby.dealt_idx = 0;
|
||||
|
||||
hand
|
||||
} else {
|
||||
return Err(format!(
|
||||
"player {} attempted to deal nonexistant tile {}",
|
||||
player.id, tile_id
|
||||
));
|
||||
};
|
||||
|
||||
player.drawn_tile = None;
|
||||
lobby.turn_state = TurnState::Discard;
|
||||
|
||||
ctx.db.player().id().update(player);
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use log::{debug, info, trace};
|
||||
use spacetimedb::{ReducerContext, Table, ViewContext, reducer, view};
|
||||
|
||||
use crate::tables::{player::player, *};
|
||||
|
|
@ -39,6 +40,79 @@ pub fn draw_tile(ctx: &ReducerContext) {
|
|||
ctx.db.player().id().update(player);
|
||||
}
|
||||
|
||||
// TODO make sure this can't be called or just error here?
|
||||
#[reducer]
|
||||
pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
|
||||
let dealt_tile = if let Some(drawn) = player.drawn_tile
|
||||
&& drawn.id == tile_id
|
||||
{
|
||||
drawn
|
||||
} else if let Some((i, _)) = player
|
||||
.hand
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, t)| t.id == tile_id)
|
||||
{
|
||||
player.hand.remove(i)
|
||||
} else {
|
||||
return Err(format!(
|
||||
"player {} attempted to deal nonexistant tile {}",
|
||||
player.id, tile_id
|
||||
));
|
||||
};
|
||||
|
||||
player.pond.push(dealt_tile);
|
||||
player.drawn_tile = None;
|
||||
lobby.turn_state = TurnState::RonChiiPonKan;
|
||||
|
||||
let player = ctx.db.player().id().update(player);
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
|
||||
debug!("player {} discarded tile {:?}", player.id, dealt_tile.tile);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[reducer]
|
||||
pub fn skip_call(ctx: &ReducerContext) {
|
||||
trace!("skip_call");
|
||||
|
||||
let mut player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
lobby.current_idx += 1;
|
||||
if lobby.current_idx >= 3 {
|
||||
lobby.current_idx = 0;
|
||||
}
|
||||
|
||||
// FIXME where better can this go
|
||||
bot_moves(ctx, &mut lobby);
|
||||
|
||||
ctx.db.player().id().update(player);
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
}
|
||||
|
||||
fn bot_moves(ctx: &ReducerContext, lobby: &mut Lobby) {
|
||||
let mut wall = ctx.db.wall().lobby_id().find(lobby.id).unwrap();
|
||||
if let Some(PlayerOrBot::Bot { id }) = lobby.players.get(lobby.current_idx as usize + 1) {
|
||||
let mut bot = ctx.db.bot().id().find(id).unwrap();
|
||||
bot.pond.push(wall.tiles.pop().unwrap());
|
||||
ctx.db.bot().id().update(bot);
|
||||
lobby.turn_state = TurnState::RonChiiPonKan;
|
||||
} else {
|
||||
lobby.turn_state = TurnState::Tsumo;
|
||||
}
|
||||
|
||||
lobby.current_idx += 1;
|
||||
if lobby.current_idx >= 3 {
|
||||
lobby.current_idx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// #[view(name = view_player_hand, public)]
|
||||
// pub fn view_player_hand(ctx: &ViewContext) -> Option<Hand> {
|
||||
// ctx.db
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@ pub struct Lobby {
|
|||
#[unique]
|
||||
pub host_player_id: u32,
|
||||
pub players: Vec<player::PlayerOrBot>,
|
||||
pub dealer_idx: u8,
|
||||
pub current_idx: u8,
|
||||
|
||||
pub game_state: GameState,
|
||||
pub turn_state: TurnState,
|
||||
|
||||
// pub dealt_idx: i8,
|
||||
}
|
||||
|
||||
#[table(name = wall)]
|
||||
|
|
|
|||
|
|
@ -39,6 +39,8 @@ pub struct Bot {
|
|||
|
||||
pub hand: Vec<DbTile>,
|
||||
pub pond: Vec<DbTile>,
|
||||
|
||||
pub drawn_tile: Option<DbTile>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, SpacetimeType)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue