98 lines
3.4 KiB
Markdown
98 lines
3.4 KiB
Markdown
|
|
# World Simulation
|
||
|
|
|
||
|
|
A browser-based world simulation with a Rust server and a Leptos/Wasm UI. The server runs the simulation and exposes its state over HTTP and WebSockets. You can edit terrain and resources, manage people and animals, and customize agent behavior with the included Rune script.
|
||
|
|
|
||
|
|
The UI is built automatically as part of `cargo run` and embedded in the server binary. There is no separate manual UI build step.
|
||
|
|
|
||
|
|
## Getting Started
|
||
|
|
|
||
|
|
### Local run with SQLite
|
||
|
|
|
||
|
|
Install Rust with `rustup`. The build script installs the `wasm32-unknown-unknown` target if it is missing. Then run:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
cargo run
|
||
|
|
```
|
||
|
|
|
||
|
|
Open [http://127.0.0.1:7878](http://127.0.0.1:7878). The default database is SQLite at `sim.db` in the current directory, and the initial seed is `42`.
|
||
|
|
|
||
|
|
To choose a seed on the first run:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
cargo run -- 123
|
||
|
|
```
|
||
|
|
|
||
|
|
An existing database snapshot takes precedence on later runs, including over a new command-line seed. Use the UI's **Reset** button to start a fresh world with its current seed, or **Regenerate** to choose a different seed.
|
||
|
|
|
||
|
|
### Run with Just
|
||
|
|
|
||
|
|
Install [Just](https://just.systems/), then list the project recipes:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
just
|
||
|
|
```
|
||
|
|
|
||
|
|
Useful recipes:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
just run # Run locally with the default SQLite database
|
||
|
|
just run-release # Run an optimized local build
|
||
|
|
just sqlite # Explicitly select sim.db for local testing
|
||
|
|
just docker-build # Build the application image
|
||
|
|
just compose-up # Build and start the app and Postgres in the background
|
||
|
|
just compose-down # Stop the Compose services; keep the database volume
|
||
|
|
```
|
||
|
|
|
||
|
|
### Run with Docker Compose and Postgres
|
||
|
|
|
||
|
|
Install Docker with the Compose plugin, then run:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
docker compose up --build -d
|
||
|
|
```
|
||
|
|
|
||
|
|
Open [http://localhost:7878](http://localhost:7878). Compose waits for Postgres to become healthy before starting the app. Postgres data is stored in the `postgres_data` volume and survives container restarts and `docker compose down`.
|
||
|
|
|
||
|
|
To remove the database volume and all saved simulation data as well:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
docker compose down -v
|
||
|
|
```
|
||
|
|
|
||
|
|
The Compose file uses development-only database credentials. Change them before exposing the service beyond a trusted local environment.
|
||
|
|
|
||
|
|
## Database Configuration
|
||
|
|
|
||
|
|
The server selects its database through `DATABASE_URL`:
|
||
|
|
|
||
|
|
| Backend | Example URL |
|
||
|
|
| ---------------- | --------------------------------------- |
|
||
|
|
| SQLite (default) | `sqlite://sim.db?mode=rwc` |
|
||
|
|
| Postgres | `postgres://sim:sim@localhost:5432/sim` |
|
||
|
|
|
||
|
|
For example, run locally against a reachable Postgres server with:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
DATABASE_URL=postgres://sim:sim@localhost:5432/sim cargo run
|
||
|
|
```
|
||
|
|
|
||
|
|
The simulation is saved as a snapshot about once per second and restored at startup. SQLite is convenient for local testing; Compose configures the app to use Postgres.
|
||
|
|
|
||
|
|
`SIM_ADDR` sets the listen address. It defaults to `127.0.0.1:7878`; containers set it to `0.0.0.0:7878`.
|
||
|
|
|
||
|
|
## Project Layout
|
||
|
|
|
||
|
|
- `src/world.rs` contains the simulation and snapshot serialization.
|
||
|
|
- `src/main.rs` runs the Axum server, simulation thread, and HTTP/WebSocket API.
|
||
|
|
- `src/storage.rs` provides SQLite and Postgres snapshot storage.
|
||
|
|
- `src/behavior.rn` is the default Rune behavior script.
|
||
|
|
- `ui/` contains the Leptos frontend, compiled to Wasm by `build.rs`.
|
||
|
|
- `Dockerfile`, `compose.yaml`, and `Justfile` provide container and developer workflows.
|
||
|
|
|
||
|
|
## Tests
|
||
|
|
|
||
|
|
Run the server tests with:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
cargo test --bin sim
|
||
|
|
```
|