hack for player-lobby and Res<Player> race?

This commit is contained in:
Tao Tien 2026-02-08 23:47:57 -08:00
parent 9f6a5b6423
commit 47735931b4
34 changed files with 536 additions and 1340 deletions

60
spacetimedb/src/game.rs Normal file
View file

@ -0,0 +1,60 @@
use log::info;
use spacetimedb::{ReducerContext, Table, rand::seq::SliceRandom, reducer};
use crate::tables::*;
use jong_types::*;
mod hand;
mod wall;
#[reducer]
pub fn join_or_create_lobby(ctx: &ReducerContext, mut lobby_id: u32) -> Result<(), String> {
let ok_or = ctx
.db
.player()
.identity()
.find(ctx.sender)
.ok_or(format!("cannot find player {}", ctx.sender))?;
let mut player = ok_or;
if lobby_id == 0 {
let lobby = ctx.db.lobby().insert(Lobby {
id: 0,
host_player_id: player.id,
players: vec![PlayerOrBot::Player { id: player.id }],
game_state: GameState::Setup,
});
info!("created lobby: {:?}", lobby);
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);
Ok(())
}
#[reducer]
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)
&& (ctx.db.player().lobby_id().filter(lobby_id).count()
+ ctx.db.bot().lobby_id().filter(lobby_id).count()
<= 4)
{
let bot = ctx.db.bot().insert(Bot {
id: 0,
lobby_id,
hand_id: 0,
pond_id: 0,
});
info!("added bot {} to lobby {}", bot.id, lobby_id);
Ok(())
} else {
Err("lobby doesn't exist".into())
}
}