2026-02-15 09:09:04 -08:00
|
|
|
use log::debug;
|
|
|
|
|
use spacetimedb::{ReducerContext, Table, reducer};
|
2026-02-07 00:18:47 -08:00
|
|
|
|
2026-02-20 03:20:29 -08:00
|
|
|
use jong_types::TurnState;
|
|
|
|
|
|
|
|
|
|
use crate::tables::*;
|
2026-02-06 23:51:08 -08:00
|
|
|
|
2026-02-13 06:10:09 -08:00
|
|
|
mod reducers {
|
2026-02-16 00:05:57 -08:00
|
|
|
mod deal;
|
|
|
|
|
mod hand;
|
|
|
|
|
mod lobby;
|
2026-02-13 06:10:09 -08:00
|
|
|
}
|
2026-02-08 23:47:57 -08:00
|
|
|
mod tables;
|
2026-02-06 23:51:08 -08:00
|
|
|
|
2026-02-15 07:09:19 -08:00
|
|
|
#[reducer]
|
|
|
|
|
pub fn clear_all(ctx: &ReducerContext) {
|
|
|
|
|
for row in ctx.db.player().iter() {
|
|
|
|
|
ctx.db.player().delete(row);
|
|
|
|
|
}
|
2026-02-16 00:05:57 -08:00
|
|
|
for row in ctx.db.lobby().iter() {
|
|
|
|
|
ctx.db.lobby().delete(row);
|
|
|
|
|
}
|
|
|
|
|
for row in ctx.db.bot().iter() {
|
|
|
|
|
ctx.db.bot().delete(row);
|
|
|
|
|
}
|
|
|
|
|
for row in ctx.db.wall().iter() {
|
|
|
|
|
ctx.db.wall().delete(row);
|
|
|
|
|
}
|
|
|
|
|
for row in ctx.db.tile().iter() {
|
|
|
|
|
ctx.db.tile().delete(row);
|
|
|
|
|
}
|
2026-02-15 07:09:19 -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-10 19:38:41 -08:00
|
|
|
ready: false,
|
2026-02-11 10:16:21 -08:00
|
|
|
sort: true,
|
|
|
|
|
hand: vec![],
|
|
|
|
|
pond: vec![],
|
2026-02-13 06:10:09 -08:00
|
|
|
drawn_tile: None,
|
2026-02-20 03:20:29 -08:00
|
|
|
turn_state: TurnState::None,
|
2026-02-08 00:10:10 -08:00
|
|
|
}) {
|
2026-02-15 07:09:19 -08:00
|
|
|
debug!("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();
|
2026-02-15 07:09:19 -08:00
|
|
|
debug!("player {:?} has reconnected", player)
|
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())
|
|
|
|
|
// }
|
|
|
|
|
// }
|