jong/spacetimedb/src/lib.rs

199 lines
4.3 KiB
Rust
Raw Normal View History

2026-02-08 00:10:10 -08:00
use log::info;
use spacetimedb::{Identity, ReducerContext, Table, rand::seq::SliceRandom, reducer, table};
2026-02-07 00:18:47 -08:00
2026-02-07 17:46:03 -08:00
use jong_types::*;
2026-02-06 23:51:08 -08:00
2026-02-08 00:10:10 -08:00
#[derive(Debug)]
#[table(name = player, public)]
2026-02-06 23:51:08 -08:00
pub struct Player {
#[primary_key]
identity: Identity,
2026-02-07 22:45:43 -08:00
2026-02-08 00:10:10 -08:00
#[auto_inc]
#[index(direct)]
#[unique]
id: u32,
2026-02-07 22:45:43 -08:00
name: Option<String>,
2026-02-08 00:10:10 -08:00
#[index(btree)]
2026-02-07 22:45:43 -08:00
lobby_id: u32,
}
2026-02-08 00:10:10 -08:00
#[table(name = bot)]
pub struct Bot {
#[primary_key]
#[auto_inc]
id: u32,
#[index(btree)]
lobby_id: u32,
}
#[derive(Debug)]
2026-02-07 22:45:43 -08:00
#[table(name = lobby, public)]
pub struct Lobby {
#[primary_key]
2026-02-07 17:46:03 -08:00
#[auto_inc]
id: u32,
2026-02-07 22:45:43 -08:00
2026-02-08 00:10:10 -08:00
#[index(direct)]
#[unique]
host_id: u32,
2026-02-08 18:15:09 -08:00
game_state: GameState,
2026-02-06 23:51:08 -08:00
}
#[table(name = wall)]
pub struct Wall {
2026-02-07 22:45:43 -08:00
#[primary_key]
#[auto_inc]
id: u32,
2026-02-08 00:10:10 -08:00
#[index(direct)]
#[unique]
lobby_id: u32,
2026-02-07 00:18:47 -08:00
tiles: Vec<Tile>,
2026-02-06 23:51:08 -08:00
}
2026-02-07 17:46:03 -08:00
#[table(name = hand)]
pub struct Hand {
2026-02-07 22:45:43 -08:00
#[primary_key]
player_ident: Identity,
tiles: Vec<Tile>,
}
#[table(name = pond, public)]
pub struct Pond {
2026-02-07 17:46:03 -08:00
tiles: Vec<Tile>,
2026-02-06 23:51:08 -08:00
}
2026-02-08 00:10:10 -08:00
#[reducer(client_connected)]
pub fn login_or_add_player(ctx: &ReducerContext) {
let identity = ctx.sender;
// TODO remove player on disconnect
if let Ok(player) = ctx.db.player().try_insert(Player {
2026-02-07 22:45:43 -08:00
identity,
2026-02-08 00:10:10 -08:00
id: 0,
name: None,
2026-02-07 22:45:43 -08:00
lobby_id: 0,
2026-02-08 00:10:10 -08:00
}) {
info!("added player: {:?}", player);
2026-02-06 23:51:08 -08:00
} else {
2026-02-08 00:10:10 -08:00
let player = ctx.db.player().identity().find(identity).unwrap();
info!("player {:?} has reconnected", player)
2026-02-06 23:51:08 -08:00
}
}
2026-02-07 22:45:43 -08:00
#[reducer]
pub fn join_or_create_lobby(ctx: &ReducerContext, lobby: Option<u32>) -> Result<(), String> {
2026-02-08 00:10:10 -08:00
let mut player = ctx
.db
.player()
.identity()
.find(ctx.sender)
.ok_or(format!("cannot find player {}", ctx.sender))?;
let lobby_id = lobby.unwrap_or_else(|| {
let lobby = ctx.db.lobby().insert(Lobby {
id: 0,
host_id: player.id,
2026-02-08 18:15:09 -08:00
game_state: GameState::None,
2026-02-08 00:10:10 -08:00
});
info!("created lobby: {:?}", lobby);
2026-02-07 22:45:43 -08:00
2026-02-08 00:10:10 -08:00
lobby.id
});
player.lobby_id = lobby_id;
let player = ctx.db.player().identity().update(player);
info!("player {} joined lobby {}", player.id, lobby_id);
Ok(())
2026-02-07 22:45:43 -08:00
}
#[reducer]
2026-02-08 00:10:10 -08:00
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 });
info!("added bot {} to lobby {}", bot.id, lobby_id);
Ok(())
} else {
Err("lobby doesn't exist".into())
}
2026-02-07 22:45:43 -08:00
}
2026-02-06 23:51:08 -08:00
#[reducer]
2026-02-07 00:18:47 -08:00
pub fn shuffle_wall(ctx: &ReducerContext) {
2026-02-08 00:10:10 -08:00
// if let Some(wall) =
// let mut rng = ctx.rng();
// let mut wall = tiles();
// wall.shuffle(&mut rng);
2026-02-07 22:45:43 -08:00
todo!()
}
#[reducer]
pub fn deal_hands(ctx: &ReducerContext) {
todo!()
2026-02-07 17:46:03 -08:00
}
#[reducer]
pub fn sort_hand(ctx: &ReducerContext) {
todo!()
2026-02-07 00:18:47 -08:00
}
2026-02-06 23:51:08 -08:00
2026-02-07 17:46:03 -08:00
// #[reducer(init)]
// pub fn init(_ctx: &ReducerContext) {
// // Called when the module is initially published
// }
// #[reducer(client_connected)]
// pub fn identity_connected(_ctx: &ReducerContext) {
// // Called everytime a new client connects
// }
// #[reducer(client_disconnected)]
// pub fn identity_disconnected(_ctx: &ReducerContext) {
// // Called everytime a client disconnects
// }
2026-02-06 23:51:08 -08:00
// #[reducer]
// pub fn add(ctx: &ReducerContext, name: String) {
// ctx.db.player().insert(Player { name });
// }
// #[reducer]
// pub fn say_hello(ctx: &ReducerContext) {
// for person in ctx.db.person().iter() {
// log::info!("Hello, {}!", person.name);
// }
// log::info!("Hello, World!");
// }
2026-02-08 00:10:10 -08:00
// #[reducer]
// pub fn set_name(ctx: &ReducerContext, name: String) -> Result<(), String> {
// if name.is_empty() {
// return Err("names must not be empty".into());
// }
// if let Some(player) = ctx.db.player().identity().find(ctx.sender) {
// ctx.db.player().identity().update(Player {
// name: Some(name),
// ..player
// });
// Ok(())
// } else {
// Err("cannot set name for unknown user".into())
// }
// }