Implementation:4thfever Cultivation world simulator Public Command API
| Knowledge Sources | |
|---|---|
| Domains | API_Design, Server |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete tool for exposing state-mutating game commands through a stable `/api/v1/command/*` REST API with Pydantic-validated requests and mutation serialization, provided by the Cultivation World Simulator external control API layer.
Description
The create_public_command_router factory function creates a FastAPI `APIRouter` with all v1 command endpoints. Each endpoint delegates to a `command_service` object (preferred) or individual handler callables (legacy). All mutations are serialized through the runtime's mutation lock via the command service.
The router exposes POST/DELETE endpoints for: game start/load/reinit/reset/pause/resume/shutdown, avatar create/delete/adjust/portrait/objective, custom content generate/create, world phenomenon, event cleanup, save/delete-save/load, and roleplay start/stop/submit-decision/submit-choice/conversation.
All request bodies are Pydantic `BaseModel` subclasses (e.g., `GameStartRequest`, `CreateAvatarRequest`, `RoleplayStartRequest`).
Usage
Import and mount this router when:
- Server setup: `main.py` creates the router with a `command_service` and mounts it on the FastAPI app
- Testing: create the router with handler callables to test individual commands
Code Reference
Source Location
- Repository: cultivation-world-simulator
- File: src/server/api/public_v1/command.py
Signature
def create_public_command_router(
*,
command_service: Any | None = None,
trigger_process_shutdown: Callable[[], dict],
run_start_game: Callable[[BaseModel], object] | None = None,
run_reinit_game: Callable[[], object] | None = None,
run_reset_game: Callable[[], object] | None = None,
run_pause_game: Callable[[], object] | None = None,
# ... 20+ more handler callables ...
) -> APIRouter:
# Request models:
class GameStartRequest(RunConfig): pass
class CreateAvatarRequest(BaseModel): ...
class SetPhenomenonRequest(BaseModel): id: int
class SaveGameRequest(BaseModel): custom_name: Optional[str] = None
class RoleplayStartRequest(BaseModel): avatar_id: str
class RoleplaySubmitDecisionRequest(BaseModel): avatar_id: str; request_id: str; command_text: str
Import
from src.server.api.public_v1.command import create_public_command_router
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| command_service | None | No | GameCommandService instance (preferred) |
| trigger_process_shutdown | Callable | Yes | Shutdown handler |
| run_* callables | None | No | Individual handlers (legacy fallback) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | APIRouter | FastAPI router with all POST/DELETE /api/v1/command/* endpoints |
Key Endpoints
| Endpoint | Method | Request Body | Description |
|---|---|---|---|
| /api/v1/command/game/start | POST | GameStartRequest | Start new game with RunConfig |
| /api/v1/command/game/load | POST | LoadGameRequest | Load save file |
| /api/v1/command/game/pause | POST | — | Pause simulation |
| /api/v1/command/avatar/create | POST | CreateAvatarRequest | Create new avatar |
| /api/v1/command/world/set-phenomenon | POST | SetPhenomenonRequest | Change celestial phenomenon |
| /api/v1/command/game/save | POST | SaveGameRequest | Save current game |
| /api/v1/command/roleplay/start | POST | RoleplayStartRequest | Start roleplay for avatar |
Usage Examples
Mount Command Router with Service
from fastapi import FastAPI
from src.server.api.public_v1.command import create_public_command_router
app = FastAPI()
command_router = create_public_command_router(
command_service=command_service,
trigger_process_shutdown=trigger_shutdown,
)
app.include_router(command_router)
Create Avatar via API (External Agent)
import requests
resp = requests.post("http://localhost:8000/api/v1/command/avatar/create", json={
"surname": "李",
"given_name": "逍遥",
"gender": "male",
"age": 18,
"level": 1,
"sect_id": 1,
"race": "human",
})
data = resp.json()
# {"ok": true, "data": {"avatar": {...}, "world_revision": 5}}
Related Pages
Implements Principle
- Principle:4thfever_Cultivation_world_simulator_Query_Command_Separation
- Principle:4thfever_Cultivation_world_simulator_Mutation_Serialization
Required Environments
- Environment:4thfever_Cultivation_world_simulator_Python_Runtime_Environment
- Environment:4thfever_Cultivation_world_simulator_Docker_Deployment_Environment
- Environment:4thfever_Cultivation_world_simulator_Frontend_Build_Environment