初始化server

This commit is contained in:
janing
2025-11-28 18:06:48 +08:00
commit 504423104b
5 changed files with 754 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
[build]
target = "wasm32-unknown-unknown"
[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"

17
server-rust/.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Spacetime ignore
/.spacetime

14
server-rust/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "server-rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
[dependencies]
spacetimedb = "1.10.*"
log = "0.4"
pinball-physics = { path = "../pinball-physics" }

102
server-rust/src/lib.rs Normal file
View File

@@ -0,0 +1,102 @@
use spacetimedb::{ReducerContext, Table};
use pinball_physics::PhysicsWorld;
use std::sync::{Arc, Mutex, OnceLock};
// Global physics world holder
static PHYSICS_WORLD: OnceLock<Arc<Mutex<Option<PhysicsWorld>>>> = OnceLock::new();
#[spacetimedb::table(name = person, public)]
pub struct Person {
name: String,
}
#[spacetimedb::table(name = physics_body, public)]
pub struct PhysicsBody {
body_id: u32,
world_id: u32,
x: f32,
y: f32,
}
#[spacetimedb::reducer(init)]
pub fn init(_ctx: &ReducerContext) {
// Initialize physics world
let world = PhysicsWorld::new(0.0, -9.81);
let world_mutex = Arc::new(Mutex::new(Some(world)));
PHYSICS_WORLD.set(world_mutex).ok();
log::info!("Physics world initialized successfully");
}
#[spacetimedb::reducer(client_connected)]
pub fn identity_connected(_ctx: &ReducerContext) {
// Called everytime a new client connects
}
#[spacetimedb::reducer(client_disconnected)]
pub fn identity_disconnected(_ctx: &ReducerContext) {
// Called everytime a client disconnects
}
#[spacetimedb::reducer]
pub fn add(ctx: &ReducerContext, name: String) {
ctx.db.person().insert(Person { name });
}
#[spacetimedb::reducer]
pub fn say_hello(ctx: &ReducerContext) {
for person in ctx.db.person().iter() {
log::info!("Hello, {}!", person.name);
}
log::info!("Hello, World!");
}
#[spacetimedb::reducer]
pub fn create_physics_body(ctx: &ReducerContext, x: f32, y: f32) {
if let Some(world_arc) = PHYSICS_WORLD.get() {
if let Ok(mut world_opt) = world_arc.lock() {
if let Some(ref mut world) = *world_opt {
let body_id = world.create_dynamic_body(x, y);
world.add_circle_collider(body_id, 0.5); // Add default circle collider
// Store in database table
ctx.db.physics_body().insert(PhysicsBody {
body_id,
world_id: 1, // Single world for now
x,
y,
});
log::info!("Created physics body with ID: {} at ({}, {})", body_id, x, y);
}
}
}
}
#[spacetimedb::reducer]
pub fn step_physics(ctx: &ReducerContext) {
if let Some(world_arc) = PHYSICS_WORLD.get() {
if let Ok(mut world_opt) = world_arc.lock() {
if let Some(ref mut world) = *world_opt {
world.step();
// Update positions in database
for body in ctx.db.physics_body().iter() {
if let Some((new_x, new_y)) = world.get_body_position(body.body_id) {
// Save data before moving
let body_id = body.body_id;
let world_id = body.world_id;
// Delete old record and insert updated one
ctx.db.physics_body().delete(body);
ctx.db.physics_body().insert(PhysicsBody {
body_id,
world_id,
x: new_x,
y: new_y,
});
}
}
log::info!("Physics step completed");
}
}
}
}