can we call this rewrite 4.5
This commit is contained in:
parent
2d3993452b
commit
a0171b1de6
9 changed files with 128 additions and 120 deletions
|
|
@ -1,61 +0,0 @@
|
|||
use log::debug;
|
||||
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
|
||||
|
||||
use crate::tables::*;
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<DbTile> {
|
||||
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
|
||||
}
|
||||
|
||||
pub fn deal_hands(ctx: &ReducerContext, lobby_id: u32) {
|
||||
let players = ctx.db.player().lobby_id().filter(lobby_id);
|
||||
let bots = ctx.db.bot().lobby_id().filter(lobby_id);
|
||||
|
||||
let mut wall = ctx.db.wall().lobby_id().find(lobby_id).unwrap();
|
||||
|
||||
// FIXME rectify deal orders
|
||||
for player in 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);
|
||||
ctx.db.player_hand().insert(PlayerHand {
|
||||
id: 0,
|
||||
player_id: player.id,
|
||||
turn_state: jong_types::TurnState::None,
|
||||
pond: vec![],
|
||||
hand: tiles,
|
||||
working_tile: None,
|
||||
});
|
||||
}
|
||||
for mut bot in bots {
|
||||
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);
|
||||
bot.hand = tiles;
|
||||
ctx.db.bot().id().update(bot);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
|||
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
|
||||
let mut hand = ctx.db.player_hand().player_id().find(player.id).unwrap();
|
||||
|
||||
// TODO we can probably remove a buncha these errors
|
||||
let dealt_tile = if let Some(dealt) = ctx.db.tile().id().find(tile_id) {
|
||||
if let Some(drawn) = hand.working_tile {
|
||||
if drawn.id == dealt.id {
|
||||
|
|
@ -49,6 +50,10 @@ pub fn discard_tile(ctx: &ReducerContext, tile_id: u32) -> Result<(), String> {
|
|||
|
||||
ctx.db.player_hand().id().update(hand);
|
||||
|
||||
let mut lobby = ctx.db.lobby().id().find(player.lobby_id).unwrap();
|
||||
lobby.next_player();
|
||||
ctx.db.lobby().id().update(lobby);
|
||||
|
||||
debug!("player {} discarded tile {:?}", player.id, dealt_tile.tile);
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
.find(ctx.sender)
|
||||
.ok_or(format!("cannot find player {}", ctx.sender))?;
|
||||
|
||||
if lobby_id == 0 {
|
||||
if lobby_id == 0 && player.lobby_id == 0 {
|
||||
// TODO check first if player is already in a lobby
|
||||
|
||||
let lobby = ctx.db.lobby().insert(Lobby {
|
||||
|
|
@ -27,10 +27,17 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(
|
|||
info!("created lobby: {}", lobby.id);
|
||||
|
||||
lobby_id = lobby.id;
|
||||
player.lobby_id = lobby_id;
|
||||
} else {
|
||||
let lobby = ctx
|
||||
.db
|
||||
.lobby()
|
||||
.id()
|
||||
.find(player.lobby_id)
|
||||
.ok_or(format!("can't find lobby {}", player.lobby_id))?;
|
||||
lobby_id = lobby.id;
|
||||
}
|
||||
|
||||
player.lobby_id = lobby_id;
|
||||
|
||||
let player = ctx.db.player().identity().update(player);
|
||||
|
||||
info!("player {} joined lobby {}", player.id, lobby_id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue