sorta discard a tile
This commit is contained in:
parent
63e21713ab
commit
8c4132e628
7 changed files with 199 additions and 26 deletions
|
|
@ -24,6 +24,7 @@ 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,
|
||||
});
|
||||
info!("created lobby: {:?}", lobby);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use log::debug;
|
||||
use log::{debug, trace};
|
||||
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
||||
|
||||
use super::hand::deal_hands;
|
||||
use crate::tables::*;
|
||||
use crate::tables::{player::player, *};
|
||||
use jong_types::*;
|
||||
|
||||
#[reducer]
|
||||
|
|
@ -40,3 +40,44 @@ 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ pub struct Lobby {
|
|||
|
||||
pub game_state: GameState,
|
||||
pub turn_state: TurnState,
|
||||
|
||||
// pub dealt_idx: i8,
|
||||
}
|
||||
|
||||
#[table(name = wall)]
|
||||
|
|
@ -29,7 +31,7 @@ pub struct DbWall {
|
|||
}
|
||||
|
||||
#[table(name = tile)]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct DbTile {
|
||||
#[primary_key]
|
||||
#[auto_inc]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue