This commit is contained in:
Tao Tien 2026-02-08 20:21:23 -08:00
parent 6cd10329df
commit d0c79377aa
16 changed files with 824 additions and 149 deletions

View file

@ -1,5 +1,7 @@
use log::info;
use spacetimedb::{Identity, ReducerContext, Table, rand::seq::SliceRandom, reducer, table};
use log::{debug, info};
use spacetimedb::{
Identity, ReducerContext, Table, ViewContext, rand::seq::SliceRandom, reducer, table, view,
};
use jong_types::*;
@ -30,7 +32,7 @@ pub struct Bot {
lobby_id: u32,
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
#[table(name = lobby, public)]
pub struct Lobby {
#[primary_key]
@ -39,19 +41,18 @@ pub struct Lobby {
#[index(direct)]
#[unique]
host_id: u32,
host_player_id: u32,
game_state: GameState,
}
#[table(name = wall)]
pub struct Wall {
// #[auto_inc]
// id: u32,
#[primary_key]
#[auto_inc]
id: u32,
#[index(direct)]
#[unique]
// #[index(direct)]
// #[unique]
lobby_id: u32,
tiles: Vec<Tile>,
@ -60,7 +61,15 @@ pub struct Wall {
#[table(name = hand)]
pub struct Hand {
#[primary_key]
player_ident: Identity,
player_identity: Identity,
tiles: Vec<Tile>,
}
#[table(name = bothand)]
pub struct BotHand {
#[primary_key]
bot_id: u32,
tiles: Vec<Tile>,
}
@ -89,7 +98,7 @@ pub fn login_or_add_player(ctx: &ReducerContext) {
}
#[reducer]
pub fn join_or_create_lobby(ctx: &ReducerContext, lobby: Option<u32>) -> Result<(), String> {
pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(), String> {
let mut player = ctx
.db
.player()
@ -97,16 +106,16 @@ pub fn join_or_create_lobby(ctx: &ReducerContext, lobby: Option<u32>) -> Result<
.find(ctx.sender)
.ok_or(format!("cannot find player {}", ctx.sender))?;
let lobby_id = lobby.unwrap_or_else(|| {
if lobby_id == 0 {
let lobby = ctx.db.lobby().insert(Lobby {
id: 0,
host_id: player.id,
host_player_id: player.id,
game_state: GameState::None,
});
info!("created lobby: {:?}", lobby);
lobby.id
});
lobby_id = lobby.id;
}
player.lobby_id = lobby_id;
@ -134,18 +143,64 @@ pub fn add_bot(ctx: &ReducerContext, lobby_id: u32) -> Result<(), String> {
}
#[reducer]
pub fn shuffle_wall(ctx: &ReducerContext) {
// if let Some(wall) =
pub fn setup_game(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::Setup;
// let mut rng = ctx.rng();
// let mut wall = tiles();
// wall.shuffle(&mut rng);
todo!()
ctx.db.lobby().id().update(lobby);
let tiles = new_shuffled_wall(ctx);
ctx.db.wall().insert(Wall {
// id: 0,
lobby_id,
tiles,
});
lobby.game_state = GameState::Deal;
ctx.db.lobby().id().update(lobby);
deal_hands(ctx, lobby_id);
lobby.game_state = GameState::Play;
ctx.db.lobby().id().update(lobby);
}
#[reducer]
pub fn deal_hands(ctx: &ReducerContext) {
todo!()
pub fn new_shuffled_wall(ctx: &ReducerContext) -> Vec<Tile> {
let mut rng = ctx.rng();
let mut wall = tiles();
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 tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
ctx.db.hand().insert(Hand {
player_identity: player.identity,
tiles,
});
}
for bot in bots {
let tiles = wall.tiles.split_off(wall.tiles.len() - 13);
wall = ctx.db.wall().lobby_id().update(wall);
ctx.db.bothand().insert(BotHand {
bot_id: bot.id,
tiles,
});
}
}
#[view(name = view_player_hand, public)]
pub fn view_player_hand(ctx: &ViewContext) -> Option<Hand> {
ctx.db.hand().player_identity().find(ctx.sender)
}
#[reducer]