process TODO.md step8

This commit is contained in:
SepComet
2026-03-29 10:49:32 +08:00
parent ef01760924
commit c5fbd8e36d
26 changed files with 1005 additions and 20 deletions
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-03-29
@@ -0,0 +1,49 @@
## Context
The repository now has a concrete server runtime entry point and a shared `ServerNetworkHost` that already owns per-peer authoritative movement state and fixed-cadence `PlayerState` broadcast. Clients can send `ShootInput` and apply inbound `CombatEvent`, but no shared server path currently accepts `ShootInput` or mutates authoritative HP from combat. The networking layer also has an explicit split between high-frequency sync traffic and reliable gameplay events, so combat resolution needs to fit the existing message contracts instead of introducing another transport abstraction.
A second constraint is code placement: shared networking code under `Assets/Scripts/Network/` must remain Unity-free. That rules out designs that depend on scene physics or Unity colliders inside the shared server path. The MVP therefore needs a deterministic server combat loop that can be exercised by edit-mode tests and owned entirely by shared host/runtime code.
## Goals / Non-Goals
**Goals:**
- Add a shared server combat coordinator that registers `ShootInput` on `ServerNetworkHost` and validates each request against the sending peer.
- Keep combat ownership server-side: authoritative HP, damage, death, and shoot rejection are resolved in the server host path and never inferred from client presentation.
- Reuse existing message contracts by emitting `CombatEvent` on the reliable lane and folding resulting HP into later `PlayerState` snapshots.
- Preserve per-peer isolation so one sender's shoot tick history or invalid payload cannot corrupt another managed session's combat state.
- Keep the MVP implementation deterministic and easy to regression test from edit-mode tests.
**Non-Goals:**
- Full projectile simulation, lag compensation, or physics-based hit scanning.
- Unity-scene integration, VFX timing, or client cosmetic preplay logic.
- Advanced anti-cheat policy beyond sender identity validation, stale filtering, and basic target eligibility checks.
- Broader gameplay systems such as respawn, inventory, or weapon-specific balance rules.
## Decisions
### Decision: Use a dedicated server-authoritative combat coordinator beside movement authority
The server already centralizes movement authority inside `ServerAuthoritativeMovementCoordinator`. Combat should follow the same pattern with a focused coordinator that is constructed by `ServerNetworkHost`, owns per-peer combat bookkeeping, and exposes only the minimal inspection/update hooks needed by runtime code and tests.
This keeps combat logic out of `MessageManager` and avoids overloading `MultiSessionManager` with gameplay concerns. An alternative was to fold shooting directly into `ServerNetworkHost`, but that would make host orchestration responsible for validation rules, state mutation, and message emission simultaneously.
### Decision: Define the MVP hit model around sender-scoped validation plus explicit target lookup
The shared server path will treat `ShootInput` as a request to attack a specific managed peer identified by `targetId`. A request is accepted only when the sender maps to a managed authoritative player state, the `playerId` matches that peer, the tick is newer than the sender's last accepted shoot tick, the aim vector is finite and non-zero, and the target resolves to another managed peer that is still alive.
This deliberately favors a narrow target-based authority model over scene-geometry hit checks. The alternative was to leave hit validation abstract or physics-driven, but that would either make the spec untestable or force Unity-only dependencies into shared networking code.
### Decision: Emit one reliable `CombatEvent` per authoritative combat outcome and keep rejection explicit
When a valid shot is accepted, the server will apply deterministic combat resolution immediately and emit authoritative `CombatEvent` messages through the existing reliable-lane contract. Accepted shots may produce `Hit`, `DamageApplied`, and `Death` events as needed; invalid shots produce `ShootRejected` instead of being dropped silently.
Keeping rejection explicit improves observability and aligns with the current client-side `CombatEvent` handling path. An alternative was to let invalid fire requests disappear without a response, but that would make client/server divergence harder to diagnose.
### Decision: Keep authoritative HP in the shared player state model rather than a separate combat-only snapshot
The movement authority work already introduced server-owned per-peer `PlayerState` snapshots. Combat resolution should update the same server-owned state so later `PlayerState` broadcasts naturally include the current authoritative HP alongside position, rotation, velocity, and tick.
The alternative was a separate combat-state store plus ad hoc synchronization into `PlayerState`, but that creates two competing sources of truth for the same player's HP.
## Risks / Trade-offs
- [Risk] The target-id-based MVP hit model is less realistic than spatial hit detection. → Mitigation: document it as an MVP constraint and keep the coordinator boundary narrow so later geometry-aware validation can replace only the acceptance rule.
- [Risk] Emitting multiple `CombatEvent` messages for one accepted shot increases event volume on the reliable lane. → Mitigation: keep the event set minimal and reserve it for state-changing outcomes (`Hit`, `DamageApplied`, `Death`, `ShootRejected`).
- [Risk] HP updates now come from both movement snapshots and combat resolution paths. → Mitigation: make combat mutate the same authoritative player-state object that movement broadcast already reads.
- [Risk] Reliable ordered shooting requests do not need stale filtering as aggressively as sync traffic, but duplicate or out-of-order resends could still replay damage. → Mitigation: keep a per-peer last accepted shoot tick and reject non-increasing ticks for that sender.
@@ -0,0 +1,23 @@
## Why
The networking MVP still stops at authoritative movement. Clients can already send `ShootInput` and receive `CombatEvent`, but the shared server path does not yet validate shooting, resolve combat outcomes, or drive authoritative HP changes back into the replicated state model. Completing this closes the main gameplay-authority gap in the MVP and prevents combat truth from drifting back to client-side presentation code.
## What Changes
- Add a dedicated shared server-authoritative combat capability that accepts `ShootInput`, validates sender-scoped fire requests, and resolves hit, damage, death, and rejection outcomes on the server.
- Broadcast authoritative `CombatEvent` messages on the reliable lane and keep rejection results explicit instead of silently dropping invalid fire requests.
- Extend server-owned player state so authoritative HP changes produced by combat resolution are reflected in subsequent `PlayerState` snapshots.
- Preserve per-peer isolation for shoot validation and combat bookkeeping in the multi-session server host/runtime path.
## Capabilities
### New Capabilities
- `server-authoritative-combat`: Defines how the shared server path validates `ShootInput`, resolves authoritative combat outcomes, and emits `CombatEvent` results.
### Modified Capabilities
- `server-authoritative-movement`: Expand authoritative `PlayerState` broadcast requirements so combat-driven HP changes are reflected in later snapshots.
- `multi-session-lifecycle`: Clarify that sender-scoped authoritative input validation and combat bookkeeping remain isolated per managed peer.
## Impact
Affected areas include `Assets/Scripts/Network/NetworkHost/`, shared message-routing/runtime composition in `Assets/Scripts/Network/NetworkApplication/`, edit-mode regression tests under `Assets/Tests/EditMode/Network/`, and `TODO.md` / OpenSpec change tracking. No transport contract changes are expected; the work builds on the existing `ShootInput`, `CombatEvent`, and `PlayerState` message types.
@@ -0,0 +1,24 @@
## MODIFIED Requirements
### Requirement: Multi-session hosts can observe and evaluate each managed session
The multi-session lifecycle coordinator SHALL expose per-session lookup or enumeration and MUST evaluate timeout, heartbeat, login, reconnect, authoritative movement tick rules, and authoritative combat input rules for each managed session independently using the shared session lifecycle vocabulary. Server-side stale-input acceptance, shoot validation, and authoritative gameplay tracking MUST remain scoped to the peer that produced the traffic.
#### Scenario: Timeout affects only one managed session
- **WHEN** one managed session stops receiving liveness updates while another session continues receiving heartbeat or message activity
- **THEN** the timed-out session transitions through timeout or reconnect states according to policy
- **THEN** the active session remains in its current healthy state
#### Scenario: Host can inspect current managed sessions
- **WHEN** server-side code needs to inspect the current connection state of connected peers
- **THEN** it can look up or enumerate managed sessions through the multi-session coordinator
- **THEN** each entry exposes the shared session lifecycle state for that specific peer
#### Scenario: Movement tick filtering remains peer-scoped
- **WHEN** two managed peers send `MoveInput` traffic with different tick progress or ordering
- **THEN** stale-input acceptance is evaluated independently for each managed peer
- **THEN** one peer's late or advanced movement input does not overwrite or suppress the other's authoritative movement state
#### Scenario: Shoot validation remains peer-scoped
- **WHEN** two managed peers send `ShootInput` traffic with different tick progress, target choices, or validation failures
- **THEN** acceptance and rejection are evaluated independently for each sending peer
- **THEN** one peer's stale or invalid shoot request does not overwrite or suppress the other's authoritative combat state
@@ -0,0 +1,40 @@
## ADDED Requirements
### Requirement: Server registers and validates `ShootInput` per peer
The shared server networking path SHALL register `ShootInput` handling through the server host/runtime composition and SHALL validate each inbound shooting request against the sending managed peer before applying any authoritative combat result. Validation MUST reject malformed numeric input, missing or mismatched player identity, non-increasing shoot ticks for that sender, zero-direction fire requests, and targets that do not resolve to a living managed peer.
#### Scenario: Valid `ShootInput` is accepted for the sending peer
- **WHEN** a managed peer sends a well-formed `ShootInput` whose `playerId` maps to that sender, whose tick is newer than the sender's last accepted shoot tick, and whose `targetId` resolves to another living managed peer
- **THEN** the server accepts the request for that sender only
- **THEN** the sender's last accepted shoot tick is updated without changing other peers' combat bookkeeping
#### Scenario: Invalid `ShootInput` is rejected without mutating authoritative combat state
- **WHEN** a managed peer sends a `ShootInput` with malformed direction data, a stale tick, a mismatched `playerId`, or a target that is missing, self-targeted, or already dead
- **THEN** the server rejects that request
- **THEN** no authoritative damage or death state is applied to any peer from that rejected request
### Requirement: Server resolves authoritative combat outcomes
The shared server networking path SHALL own final combat resolution for accepted shots, including hit acceptance, damage application, authoritative HP mutation, and death determination. Combat resolution MUST update the authoritative server-owned state of both the attacker and target as needed without delegating gameplay truth to client-side prediction or presentation code.
#### Scenario: Accepted shot applies damage to the authoritative target state
- **WHEN** the server accepts a `ShootInput` that targets a living managed peer
- **THEN** it resolves the shot as an authoritative hit against that target
- **THEN** it reduces the target's authoritative HP according to the configured damage rule before later state snapshots are broadcast
#### Scenario: Lethal damage marks the authoritative target as dead
- **WHEN** an accepted shot reduces a target's authoritative HP to zero or below
- **THEN** the server clamps the target's authoritative HP to zero
- **THEN** subsequent combat and state broadcast treat that target as dead until a later server-owned lifecycle change resets it
### Requirement: Server emits reliable authoritative `CombatEvent` results
The shared server networking path SHALL emit authoritative combat outcomes through `CombatEvent` messages using the existing reliable-lane delivery contract. Accepted shots MUST produce reliable events for hit and damage application, and lethal results MUST also produce a death event. Rejected shots MUST produce a reliable `ShootRejected` event that identifies the attacker and rejected target context.
#### Scenario: Accepted shot produces authoritative hit and damage events
- **WHEN** the server resolves an accepted shot that damages a living target
- **THEN** it broadcasts `CombatEvent` messages on the reliable lane for the authoritative combat result
- **THEN** the emitted events identify the attacker, target, damage, and authoritative tick for client-side application
#### Scenario: Rejected shot produces an authoritative rejection event
- **WHEN** the server rejects a `ShootInput` during validation
- **THEN** it broadcasts a reliable `CombatEvent` with event type `ShootRejected`
- **THEN** clients can observe that rejection without inferring local authoritative damage or hit success
@@ -0,0 +1,19 @@
## MODIFIED Requirements
### Requirement: Server broadcasts authoritative `PlayerState` snapshots on the sync cadence
The shared server networking path SHALL emit authoritative `PlayerState` snapshots for managed peers at a fixed cadence using the existing sync-lane message contract. Each snapshot MUST be derived from the server-owned authoritative player state and include the authoritative tick for client reconciliation and interpolation. Authoritative HP changes produced by server-side combat resolution MUST be reflected in later snapshots for the affected peer.
#### Scenario: Authority update step emits sync-lane player snapshots
- **WHEN** the server reaches a configured authority broadcast cadence while one or more managed peers have authoritative player state
- **THEN** it sends `PlayerState` snapshots using the sync-lane delivery policy when a distinct sync transport exists
- **THEN** each snapshot includes the authoritative position, rotation, velocity, HP, and tick from server-owned state
#### Scenario: Combat-driven HP changes appear in later player snapshots
- **WHEN** the server applies authoritative combat damage or death to a managed peer
- **THEN** later `PlayerState` snapshots for that peer carry the updated authoritative HP value
- **THEN** clients do not need to invent or persist a separate HP truth outside authoritative server snapshots
#### Scenario: Reliable transport remains fallback when no sync transport exists
- **WHEN** the server broadcasts authoritative `PlayerState` snapshots without a dedicated sync transport
- **THEN** the shared routing path still emits `PlayerState` through the existing fallback lane behavior
- **THEN** the authoritative snapshot contract remains unchanged
@@ -0,0 +1,17 @@
## 1. Authoritative Combat Core
- [x] 1.1 Add a dedicated server-authoritative combat coordinator and per-peer combat state model under `Assets/Scripts/Network/NetworkHost/`.
- [x] 1.2 Register `ShootInput` handling through the server host/runtime composition path and validate sender-scoped shooting payloads before accepting them.
- [x] 1.3 Resolve accepted shots against managed peers, mutate authoritative HP/death state, and emit reliable `CombatEvent` results including explicit `ShootRejected` responses.
## 2. Runtime And State Integration
- [x] 2.1 Reuse or extend the server-owned authoritative player state model so combat resolution updates the same per-peer state consumed by `PlayerState` broadcast.
- [x] 2.2 Expose the minimal runtime/host surface needed for host processes and tests to inspect authoritative combat state and drive any required combat update hooks.
- [x] 2.3 Preserve per-peer isolation for shoot tick validation, target lookup, and removal/cleanup when sessions disconnect or the runtime stops.
## 3. Regression Coverage And Tracking
- [x] 3.1 Add edit-mode regression tests for accepted versus rejected `ShootInput` handling across multiple peers.
- [x] 3.2 Add edit-mode regression tests for authoritative damage/death resolution, reliable `CombatEvent` broadcast, and HP propagation into later `PlayerState` snapshots.
- [x] 3.3 Update `TODO.md` and related change tracking/docs to reflect the completed server-authoritative shooting/combat resolution work.