2026-02-07 00:18:47 -08:00
|
|
|
use spacetimedb::{Identity, ReducerContext, Table, rand::Rng, reducer, table};
|
|
|
|
|
|
|
|
|
|
use jong::tile::Tile;
|
2026-02-06 23:51:08 -08:00
|
|
|
|
|
|
|
|
#[table(name = player, public)]
|
|
|
|
|
pub struct Player {
|
|
|
|
|
#[primary_key]
|
|
|
|
|
identity: Identity,
|
|
|
|
|
name: Option<String>,
|
|
|
|
|
host: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[table(name = wall)]
|
|
|
|
|
pub struct Wall {
|
2026-02-07 00:18:47 -08:00
|
|
|
tiles: Vec<Tile>,
|
2026-02-06 23:51:08 -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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[reducer]
|
2026-02-07 00:18:47 -08:00
|
|
|
pub fn shuffle_wall(ctx: &ReducerContext) {
|
|
|
|
|
let mut rng = ctx.rng();
|
|
|
|
|
let mut tiles: Vec<Tile> = todo();
|
|
|
|
|
|
|
|
|
|
// rng.fill();
|
|
|
|
|
// let tiles = rng.sh;
|
|
|
|
|
|
|
|
|
|
ctx.db.wall().insert(Wall {tiles});
|
|
|
|
|
}
|
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!");
|
|
|
|
|
// }
|