jong/spacetimedb/src/lib.rs

126 lines
2.6 KiB
Rust
Raw Normal View History

2026-02-07 17:46:03 -08:00
use spacetimedb::{rand::seq::SliceRandom, reducer, table, Identity, ReducerContext, 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-07 22:45:43 -08:00
#[table(name = player)]
2026-02-06 23:51:08 -08:00
pub struct Player {
#[primary_key]
identity: Identity,
2026-02-07 22:45:43 -08:00
name: Option<String>,
lobby_id: u32,
}
#[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
host: Identity,
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-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-07 17:46:03 -08:00
#[reducer]
2026-02-07 22:45:43 -08:00
pub fn add_player(ctx: &ReducerContext, name: Option<String>) {
let identity = ctx.identity();
ctx.db.player().insert(Player {
identity,
name,
lobby_id: 0,
});
}
2026-02-06 23:51:08 -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())
}
}
2026-02-07 22:45:43 -08:00
#[reducer]
pub fn join_or_create_lobby(ctx: &ReducerContext, lobby: Option<u32>) -> Result<(), String> {
let player = ctx.db.player().identity().find(ctx.sender).unwrap();
todo!()
}
#[reducer]
pub fn insert_wall(ctx: &ReducerContext, player_ids: Vec<u32>) {
let tiles: Vec<Tile> = tiles();
ctx.db.wall().insert(Wall { id: 0, tiles });
}
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-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!");
// }