feat(utas-host): FIFA17 squad authority — PUT, /squad/list, userMassInfo overlay
Extend the UTAS migration host to own the squad slice, reusing the exact
production identity path (Fifa17CardCatalog + ExternalIdentityStore) that
/club uses, so /club, /squad/list, userMassInfo and PUT all agree on
wire<->owned identity.
Routing (classified ONCE, no try-Rust-then-Python):
PUT …/squad/<n> -> Rust (numeric id; …/squad/active stays Python)
GET …/squad/list -> Rust
GET …/userMassInfo-> Python proxy, ONLY .squad overlaid
everything else -> Python verbatim
CoreAccess gains read_squad_ext / replace_squad / all_owned (HTTP to the new
Core /squad/ext + /squad/replace routes).
PUT pipeline: parse -> build_squad_write (reverse-resolve every wire id;
refuse unresolved/duplicate) -> AUTHORIZE every resolved owned item against
the active club (identity resolution is NOT authorization; a valid wire id
owned by another profile is rejected before any mutation) -> Core atomic
replace+extension -> exactly {"id":0}. No Python fallback on failure; no
host-side second extension store; no fingerprint recomputation.
Read path assembles ONE projection input (one read_squad_ext + one batch
all_owned; no per-slot lookup) and runs the single adapter projector. Both
/squad/list and userMassInfo.squad derive from it. Fresh projects; Stale is
never applied; Missing is never fabricated — both are prominent integrity
failures, never served from Python (no split authority). The overlay
replaces only .squad and preserves userInfo/settings/userData/
pileSizeClientData, fixing Content-Length.
Tests: routing, full-replacement + exact ack, unknown/foreign/duplicate item
rejection with Core unchanged, idempotent repeat PUT, coupled read-after-
write (list + userMassInfo agree, real resourceIds + stable wire ids),
overlay field preservation, bounded no-N+1 reads, Stale/Missing integrity.
This commit is contained in:
+588
-20
@@ -46,6 +46,14 @@ use openfut_adapter_fifa17::fut::club_response::{
|
||||
};
|
||||
use openfut_adapter_fifa17::fut::entities::Fifa17Entities;
|
||||
use openfut_adapter_fifa17::fut::owned_query::{map_to_core, parse_club_query, MapError};
|
||||
use openfut_adapter_fifa17::fut::squad::{parse_squad_put, save_ack, SquadWireResolver};
|
||||
use openfut_adapter_fifa17::fut::squad_ext::{
|
||||
build_squad_write, Fifa17SquadExtensionV1, SquadBuildError, EXT_NAMESPACE, EXT_SCHEMA_VERSION,
|
||||
};
|
||||
use openfut_adapter_fifa17::fut::squad_projection::{
|
||||
project_squad, squad_list, user_mass_info_squad, ProjectionSlot, SquadExtInput,
|
||||
SquadProjection, SquadProjectionInput,
|
||||
};
|
||||
use openfut_identity::ExternalIdentityStore;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
@@ -56,30 +64,62 @@ use config::HostConfig;
|
||||
/// The route decision, taken once, before execution.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Route {
|
||||
/// The owned-player search, served from Core.
|
||||
/// `GET …/club` — the owned-player search, served from Core.
|
||||
Club,
|
||||
/// `PUT …/squad/<n>` — full squad replacement, committed to Core.
|
||||
SquadReplace,
|
||||
/// `GET …/squad/list` — the squad summary, projected from Core.
|
||||
SquadList,
|
||||
/// `GET …/userMassInfo` — proxied to Python, with only `.squad` overlaid.
|
||||
UserMassInfo,
|
||||
/// Anything else — proxied verbatim to the Python oracle.
|
||||
Passthrough,
|
||||
}
|
||||
|
||||
/// Classify a request. ONLY `GET /ut/game/<title>/club` (exact) is owned by Rust.
|
||||
/// `/club/stats/*`, `/clubUser`, mutations, auth, packs, market, squad, etc. all
|
||||
/// fall through to Python.
|
||||
/// Classify a request ONCE, before execution. Rust owns exactly:
|
||||
/// * `GET …/club`
|
||||
/// * `PUT …/squad/<n>` (numeric id; `…/squad/active` is NOT numeric → Python)
|
||||
/// * `GET …/squad/list`
|
||||
/// * `GET …/userMassInfo` (proxied, `.squad` overlaid)
|
||||
///
|
||||
/// Everything else — `GET …/squad/<n>`, `…/squad/active`, `/clubUser`, auth,
|
||||
/// packs, market, other mutations — falls through to Python. There is no
|
||||
/// "try Rust then Python", so a squad mutation can never be double-applied.
|
||||
pub fn classify(method: &str, path: &str) -> Route {
|
||||
if method.eq_ignore_ascii_case("GET") && is_exact_club_path(path) {
|
||||
Route::Club
|
||||
let get = method.eq_ignore_ascii_case("GET");
|
||||
let put = method.eq_ignore_ascii_case("PUT");
|
||||
if get && is_exact_club_path(path) {
|
||||
return Route::Club;
|
||||
}
|
||||
match ut_tail(path) {
|
||||
Some("squad/list") if get => Route::SquadList,
|
||||
Some("userMassInfo") if get => Route::UserMassInfo,
|
||||
Some(tail) if put && is_numeric_squad_tail(tail) => Route::SquadReplace,
|
||||
_ => Route::Passthrough,
|
||||
}
|
||||
}
|
||||
|
||||
/// The tail after `/ut/game/<title>/` (non-empty title), or `None`.
|
||||
fn ut_tail(path: &str) -> Option<&str> {
|
||||
let rest = path.strip_prefix("/ut/game/")?;
|
||||
let (title, tail) = rest.split_once('/')?;
|
||||
if title.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Route::Passthrough
|
||||
Some(tail)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_exact_club_path(path: &str) -> bool {
|
||||
// /ut/game/<seg>/club with nothing after and a non-empty title segment.
|
||||
match path.strip_prefix("/ut/game/") {
|
||||
Some(rest) => match rest.split_once('/') {
|
||||
Some((title, tail)) => !title.is_empty() && tail == "club",
|
||||
None => false,
|
||||
},
|
||||
ut_tail(path) == Some("club")
|
||||
}
|
||||
|
||||
/// `squad/<digits>` — the active/full squad save. `squad/active` (non-numeric) is
|
||||
/// deliberately excluded so the multi-squad flow stays with Python until there is
|
||||
/// retail evidence for it.
|
||||
fn is_numeric_squad_tail(tail: &str) -> bool {
|
||||
match tail.strip_prefix("squad/") {
|
||||
Some(id) => !id.is_empty() && id.bytes().all(|b| b.is_ascii_digit()),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
@@ -110,11 +150,83 @@ pub struct CorePage {
|
||||
pub total: i64,
|
||||
}
|
||||
|
||||
/// One canonical squad slot as read back from Core (game-independent).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CoreSquadSlot {
|
||||
pub owned_card_id: String,
|
||||
pub index: i64,
|
||||
pub is_captain: bool,
|
||||
pub is_on_bench: bool,
|
||||
}
|
||||
|
||||
/// Freshness of the stored opaque extension vs the current canonical squad, as
|
||||
/// Core reports it. Carries the stored payload for Fresh/Stale (never applied
|
||||
/// when Stale — the host decides policy).
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CoreExtState {
|
||||
Fresh { schema_version: i64, payload: String },
|
||||
Stale { schema_version: i64, payload: String },
|
||||
Missing,
|
||||
}
|
||||
|
||||
/// The active squad, its canonical slots, and its opaque extension state — the
|
||||
/// result of Core's `GET /squad/ext`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CoreSquadRead {
|
||||
pub name: String,
|
||||
/// FIFA formation token, verbatim (Core stores it opaquely).
|
||||
pub formation: String,
|
||||
pub slots: Vec<CoreSquadSlot>,
|
||||
pub ext: CoreExtState,
|
||||
}
|
||||
|
||||
/// A canonical + extension squad replacement the host asks Core to commit
|
||||
/// atomically (`PUT /squad/replace`).
|
||||
pub struct CoreReplaceRequest {
|
||||
pub name: Option<String>,
|
||||
pub formation: Option<String>,
|
||||
pub slots: Vec<CoreSquadSlot>,
|
||||
pub client_reported: CoreClientEval,
|
||||
pub ext_namespace: String,
|
||||
pub ext_schema_version: i64,
|
||||
pub ext_payload: String,
|
||||
}
|
||||
|
||||
/// Client-reported shadow evaluation carried through to Core (never Core's
|
||||
/// authoritative evaluation).
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CoreClientEval {
|
||||
pub chemistry: Option<i64>,
|
||||
pub rating: Option<i64>,
|
||||
pub star_rating: Option<i64>,
|
||||
}
|
||||
|
||||
/// Outcome of a committed replacement.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CoreReplaceResult {
|
||||
pub squad_id: String,
|
||||
pub canonical_fingerprint: String,
|
||||
pub slots_written: usize,
|
||||
}
|
||||
|
||||
/// How the host reaches Core. The adapter never sees this — the host owns the
|
||||
/// transport, mirroring the architecture rule. Tests inject a fake.
|
||||
pub trait CoreAccess: Send + Sync {
|
||||
/// Query the owned inventory with semantic `/collection` query params.
|
||||
fn query_owned(&self, params: &[(&str, String)]) -> Result<CorePage, CoreError>;
|
||||
|
||||
/// Every owned item for the active club, in one call (no pagination) — used
|
||||
/// to assemble a whole squad projection and to authorize squad writes. The
|
||||
/// default delegates to an unfiltered `query_owned`.
|
||||
fn all_owned(&self) -> Result<Vec<CoreOwnedItem>, CoreError> {
|
||||
Ok(self.query_owned(&[])?.items)
|
||||
}
|
||||
|
||||
/// Read the active squad + its opaque extension for `namespace`.
|
||||
fn read_squad_ext(&self, namespace: &str) -> Result<CoreSquadRead, CoreError>;
|
||||
|
||||
/// Replace the active squad's canonical slots + opaque extension atomically.
|
||||
fn replace_squad(&self, req: &CoreReplaceRequest) -> Result<CoreReplaceResult, CoreError>;
|
||||
}
|
||||
|
||||
/// Default HTTP implementation: `GET {core_url}/collection?…` (plain HTTP JSON,
|
||||
@@ -155,6 +267,118 @@ impl CoreAccess for HttpCoreClient {
|
||||
let v: Value = resp.json().map_err(|e| CoreError::Parse(e.to_string()))?;
|
||||
parse_core_page(&v)
|
||||
}
|
||||
fn read_squad_ext(&self, namespace: &str) -> Result<CoreSquadRead, CoreError> {
|
||||
let url = format!("{}/squad/ext", self.base_url);
|
||||
let resp = self
|
||||
.client
|
||||
.get(&url)
|
||||
.header("X-OpenFUT-Game", &self.game)
|
||||
.query(&[("namespace", namespace)])
|
||||
.send()
|
||||
.map_err(|e| CoreError::Http(e.to_string()))?;
|
||||
let status = resp.status().as_u16();
|
||||
if !(200..300).contains(&status) {
|
||||
return Err(CoreError::Status(status));
|
||||
}
|
||||
let v: Value = resp.json().map_err(|e| CoreError::Parse(e.to_string()))?;
|
||||
parse_core_squad_read(&v)
|
||||
}
|
||||
|
||||
fn replace_squad(&self, req: &CoreReplaceRequest) -> Result<CoreReplaceResult, CoreError> {
|
||||
let url = format!("{}/squad/replace", self.base_url);
|
||||
let resp = self
|
||||
.client
|
||||
.put(&url)
|
||||
.header("X-OpenFUT-Game", &self.game)
|
||||
.json(&replace_request_body(req))
|
||||
.send()
|
||||
.map_err(|e| CoreError::Http(e.to_string()))?;
|
||||
let status = resp.status().as_u16();
|
||||
if !(200..300).contains(&status) {
|
||||
return Err(CoreError::Status(status));
|
||||
}
|
||||
let v: Value = resp.json().map_err(|e| CoreError::Parse(e.to_string()))?;
|
||||
Ok(CoreReplaceResult {
|
||||
squad_id: v.get("squad_id").and_then(|x| x.as_str()).unwrap_or("").to_string(),
|
||||
canonical_fingerprint: v
|
||||
.get("canonical_fingerprint")
|
||||
.and_then(|x| x.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string(),
|
||||
slots_written: v.get("slots_written").and_then(|x| x.as_u64()).unwrap_or(0) as usize,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize a [`CoreReplaceRequest`] into the `PUT /squad/replace` JSON body.
|
||||
pub fn replace_request_body(req: &CoreReplaceRequest) -> Value {
|
||||
let slots: Vec<Value> = req
|
||||
.slots
|
||||
.iter()
|
||||
.map(|s| {
|
||||
json!({
|
||||
"owned_card_id": s.owned_card_id,
|
||||
"slot": s.index,
|
||||
"is_captain": s.is_captain,
|
||||
"is_on_bench": s.is_on_bench,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
json!({
|
||||
"name": req.name,
|
||||
"formation": req.formation,
|
||||
"slots": slots,
|
||||
"client_reported": {
|
||||
"client_reported_chemistry": req.client_reported.chemistry,
|
||||
"client_reported_rating": req.client_reported.rating,
|
||||
"client_reported_star_rating": req.client_reported.star_rating,
|
||||
},
|
||||
"extension": {
|
||||
"namespace": req.ext_namespace,
|
||||
"schema_version": req.ext_schema_version,
|
||||
"payload": req.ext_payload,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse Core's `GET /squad/ext` response into a [`CoreSquadRead`].
|
||||
pub fn parse_core_squad_read(v: &Value) -> Result<CoreSquadRead, CoreError> {
|
||||
let squad = v.get("squad").ok_or_else(|| CoreError::Parse("missing `squad`".into()))?;
|
||||
let name = squad.get("name").and_then(|x| x.as_str()).unwrap_or("").to_string();
|
||||
let formation = squad
|
||||
.get("formation")
|
||||
.and_then(|x| x.as_str())
|
||||
.ok_or_else(|| CoreError::Parse("missing squad.formation".into()))?
|
||||
.to_string();
|
||||
let players = v
|
||||
.get("players")
|
||||
.and_then(|p| p.as_array())
|
||||
.ok_or_else(|| CoreError::Parse("missing `players`".into()))?;
|
||||
let slots = players
|
||||
.iter()
|
||||
.filter_map(|p| {
|
||||
Some(CoreSquadSlot {
|
||||
owned_card_id: p.get("owned_card_id")?.as_str()?.to_string(),
|
||||
index: p.get("position_index")?.as_i64()?,
|
||||
is_captain: p.get("is_captain").and_then(|x| x.as_bool()).unwrap_or(false),
|
||||
is_on_bench: p.get("is_on_bench").and_then(|x| x.as_bool()).unwrap_or(false),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let ext_v = v.get("extension").ok_or_else(|| CoreError::Parse("missing `extension`".into()))?;
|
||||
let ext = match ext_v.get("state").and_then(|x| x.as_str()) {
|
||||
Some("fresh") => CoreExtState::Fresh {
|
||||
schema_version: ext_v.get("schema_version").and_then(|x| x.as_i64()).unwrap_or(0),
|
||||
payload: ext_v.get("payload").and_then(|x| x.as_str()).unwrap_or("").to_string(),
|
||||
},
|
||||
Some("stale") => CoreExtState::Stale {
|
||||
schema_version: ext_v.get("schema_version").and_then(|x| x.as_i64()).unwrap_or(0),
|
||||
payload: ext_v.get("payload").and_then(|x| x.as_str()).unwrap_or("").to_string(),
|
||||
},
|
||||
Some("missing") => CoreExtState::Missing,
|
||||
other => return Err(CoreError::Parse(format!("unknown extension state {other:?}"))),
|
||||
};
|
||||
Ok(CoreSquadRead { name, formation, slots, ext })
|
||||
}
|
||||
|
||||
/// Parse Core's `/collection` response `{ "collection": [...], "total": n }` into
|
||||
@@ -279,6 +503,16 @@ impl ItemIdentityResolver for Fifa17IdentityResolver {
|
||||
}
|
||||
}
|
||||
|
||||
/// The same production resolver reverses a wire id to a Core owned-instance id
|
||||
/// for the squad PUT path — reusing the identity store, so `/club`, `/squad`,
|
||||
/// and PUT all agree on wire↔owned. Identity ONLY; ownership is authorized
|
||||
/// separately (a resolvable id is not proof of ownership).
|
||||
impl SquadWireResolver for Fifa17IdentityResolver {
|
||||
fn owned_id_for_wire(&self, wire: i64) -> Option<String> {
|
||||
Fifa17IdentityResolver::owned_id_for_wire(self, wire)
|
||||
}
|
||||
}
|
||||
|
||||
// ───────────────────────────── /club handler ────────────────────────────────
|
||||
|
||||
/// Safe, structured summary of a handled `/club` request (no auth/session/device
|
||||
@@ -379,6 +613,301 @@ fn summarize(pairs: &[(&str, String)]) -> String {
|
||||
.join(",")
|
||||
}
|
||||
|
||||
// ───────────────────────────── Squad handlers ───────────────────────────────
|
||||
|
||||
/// FIFA wire id of the single active squad (`…/squad/0`).
|
||||
pub const ACTIVE_SQUAD_WIRE_ID: i64 = 0;
|
||||
|
||||
/// Dependencies for the Rust squad paths. `resolver` is the SAME production
|
||||
/// identity resolver `/club` uses (forward shape + reverse wire→owned), so every
|
||||
/// route agrees on wire↔owned identity.
|
||||
pub struct SquadDeps<'a> {
|
||||
pub core: &'a dyn CoreAccess,
|
||||
pub resolver: &'a Fifa17IdentityResolver,
|
||||
pub entities: &'a Fifa17Entities,
|
||||
}
|
||||
|
||||
/// Secret-free structured log line for a handled squad request.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SquadLog {
|
||||
pub outcome: &'static str,
|
||||
pub detail: String,
|
||||
}
|
||||
|
||||
/// The active squad projected from Core, with the freshness policy applied.
|
||||
enum HostProjection {
|
||||
/// Fresh: the projected FIFA squad object (before any endpoint envelope).
|
||||
Squad(Value),
|
||||
/// Stored extension is stale vs the canonical squad — NEVER applied.
|
||||
Stale,
|
||||
/// No extension stored — nothing fabricated.
|
||||
Missing,
|
||||
/// Core unreachable / response unreadable / projection failed.
|
||||
Error(String),
|
||||
}
|
||||
|
||||
/// Assemble the projection input from Core in a BOUNDED number of calls — one
|
||||
/// `read_squad_ext` + one batch `all_owned`, never per slot — then project. The
|
||||
/// Fresh/Stale/Missing policy is decided HERE, not buried in a default.
|
||||
fn project_active_squad(deps: &SquadDeps<'_>) -> HostProjection {
|
||||
let read = match deps.core.read_squad_ext(EXT_NAMESPACE) {
|
||||
Ok(r) => r,
|
||||
Err(e) => return HostProjection::Error(e.to_string()),
|
||||
};
|
||||
let ext = match &read.ext {
|
||||
CoreExtState::Fresh { schema_version, payload } => {
|
||||
match Fifa17SquadExtensionV1::from_payload(*schema_version, payload) {
|
||||
Ok(e) => e,
|
||||
// Fresh but the payload does not parse as our schema: corruption,
|
||||
// never coerced into a fabricated squad.
|
||||
Err(e) => return HostProjection::Error(format!("fresh extension unreadable: {e}")),
|
||||
}
|
||||
}
|
||||
CoreExtState::Stale { .. } => return HostProjection::Stale,
|
||||
CoreExtState::Missing => return HostProjection::Missing,
|
||||
};
|
||||
let owned = match deps.core.all_owned() {
|
||||
Ok(v) => v,
|
||||
Err(e) => return HostProjection::Error(e.to_string()),
|
||||
};
|
||||
let owned_by_id: std::collections::HashMap<String, CoreOwnedItem> =
|
||||
owned.into_iter().map(|i| (i.owned_card_id.clone(), i)).collect();
|
||||
let slots: Vec<ProjectionSlot> = read
|
||||
.slots
|
||||
.iter()
|
||||
.map(|s| ProjectionSlot {
|
||||
owned_card_id: s.owned_card_id.clone(),
|
||||
index: s.index,
|
||||
is_captain: s.is_captain,
|
||||
is_on_bench: s.is_on_bench,
|
||||
})
|
||||
.collect();
|
||||
let input = SquadProjectionInput {
|
||||
fifa_squad_id: ACTIVE_SQUAD_WIRE_ID,
|
||||
name: read.name,
|
||||
formation: read.formation,
|
||||
slots,
|
||||
ext: SquadExtInput::Fresh(ext),
|
||||
owned: &owned_by_id,
|
||||
};
|
||||
match project_squad(&input, deps.resolver, deps.entities) {
|
||||
Ok(SquadProjection::Projected(v)) => HostProjection::Squad(v),
|
||||
Ok(SquadProjection::Stale) => HostProjection::Stale,
|
||||
Ok(SquadProjection::Missing) => HostProjection::Missing,
|
||||
Err(e) => HostProjection::Error(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A small JSON error body (UTAS mutations that cannot be honoured fail loudly —
|
||||
/// they are NEVER retried against Python, which would risk a double mutation).
|
||||
fn error_response(status: u16, code: &str) -> WireResponse {
|
||||
WireResponse {
|
||||
status,
|
||||
headers: vec![("Content-Type".to_string(), "application/json".to_string())],
|
||||
body: format!("{{\"error\":\"{code}\"}}").into_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
/// `PUT …/squad/<n>` — parse the full replacement, reverse-resolve every wire id,
|
||||
/// AUTHORIZE every resolved item against the active club, then commit the
|
||||
/// canonical squad + FIFA extension to Core in one transaction. On any failure
|
||||
/// it returns an error and NEVER falls back to Python (no double mutation).
|
||||
pub fn handle_put_squad(body: &[u8], deps: &SquadDeps<'_>) -> (WireResponse, SquadLog) {
|
||||
let put = match parse_squad_put(body) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return (
|
||||
error_response(400, "parse_error"),
|
||||
SquadLog { outcome: "parse_error", detail: e.to_string() },
|
||||
)
|
||||
}
|
||||
};
|
||||
// Reverse-resolve wire→owned and shape canonical + extension. Refuses on an
|
||||
// unresolved wire id or the same owned item placed twice.
|
||||
let build = match build_squad_write(&put, deps.resolver) {
|
||||
Ok(b) => b,
|
||||
Err(SquadBuildError::UnresolvedWireIds(ids)) => {
|
||||
return (
|
||||
error_response(400, "unresolved_wire_ids"),
|
||||
SquadLog { outcome: "unresolved_wire_ids", detail: format!("{ids:?}") },
|
||||
)
|
||||
}
|
||||
Err(SquadBuildError::DuplicateOwnedItem(id)) => {
|
||||
return (
|
||||
error_response(400, "duplicate_owned_item"),
|
||||
SquadLog { outcome: "duplicate_owned_item", detail: id },
|
||||
)
|
||||
}
|
||||
};
|
||||
// AUTHORIZATION — identity resolution is NOT authorization. Every resolved
|
||||
// owned item MUST belong to the active club; a globally-valid wire id that
|
||||
// belongs to another profile is rejected BEFORE any canonical mutation.
|
||||
let owned_set: std::collections::HashSet<String> = match deps.core.all_owned() {
|
||||
Ok(v) => v.into_iter().map(|i| i.owned_card_id).collect(),
|
||||
Err(e) => {
|
||||
return (
|
||||
error_response(502, "core_error"),
|
||||
SquadLog { outcome: "core_error", detail: e.to_string() },
|
||||
)
|
||||
}
|
||||
};
|
||||
for slot in &build.canonical.slots {
|
||||
if !owned_set.contains(&slot.owned_card_id) {
|
||||
return (
|
||||
error_response(403, "not_owned"),
|
||||
SquadLog { outcome: "unauthorized_item", detail: slot.owned_card_id.clone() },
|
||||
);
|
||||
}
|
||||
}
|
||||
// Commit canonical + extension atomically. No Python fallback on failure.
|
||||
let req = CoreReplaceRequest {
|
||||
name: build.canonical.name.clone(),
|
||||
formation: build.canonical.formation.clone(),
|
||||
slots: build
|
||||
.canonical
|
||||
.slots
|
||||
.iter()
|
||||
.map(|s| CoreSquadSlot {
|
||||
owned_card_id: s.owned_card_id.clone(),
|
||||
index: s.index,
|
||||
is_captain: s.is_captain,
|
||||
is_on_bench: s.is_on_bench,
|
||||
})
|
||||
.collect(),
|
||||
client_reported: CoreClientEval {
|
||||
chemistry: build.extension.client_reported.chemistry,
|
||||
rating: build.extension.client_reported.rating,
|
||||
star_rating: build.extension.client_reported.star_rating,
|
||||
},
|
||||
ext_namespace: EXT_NAMESPACE.to_string(),
|
||||
ext_schema_version: EXT_SCHEMA_VERSION,
|
||||
ext_payload: build.extension.to_payload(),
|
||||
};
|
||||
match deps.core.replace_squad(&req) {
|
||||
Ok(_) => (
|
||||
json_response(&save_ack(put.id)),
|
||||
SquadLog { outcome: "ok", detail: String::new() },
|
||||
),
|
||||
Err(e) => (
|
||||
error_response(502, "core_error"),
|
||||
SquadLog { outcome: "core_error", detail: e.to_string() },
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// `GET …/squad/list` — served from Core + the ONE projector. Stale/Missing are
|
||||
/// integrity failures for the migrated dev profile: logged prominently, degraded
|
||||
/// to an empty list, NEVER served from Python and NEVER projected from stale ext.
|
||||
pub fn handle_squad_list(deps: &SquadDeps<'_>) -> (WireResponse, SquadLog) {
|
||||
match project_active_squad(deps) {
|
||||
HostProjection::Squad(v) => (
|
||||
json_response(&squad_list(&v)),
|
||||
SquadLog { outcome: "ok", detail: String::new() },
|
||||
),
|
||||
HostProjection::Stale => (
|
||||
json_response(&json!({ "squad": [] })),
|
||||
SquadLog { outcome: "stale_integrity", detail: "stale extension not applied".into() },
|
||||
),
|
||||
HostProjection::Missing => (
|
||||
json_response(&json!({ "squad": [] })),
|
||||
SquadLog { outcome: "missing_integrity", detail: "no extension stored".into() },
|
||||
),
|
||||
HostProjection::Error(e) => (
|
||||
json_response(&json!({ "squad": [] })),
|
||||
SquadLog { outcome: "core_error", detail: e },
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// An explicit empty active squad used only when Rust squad authority cannot
|
||||
/// produce a Fresh projection during a userMassInfo overlay. It is NOT Python's
|
||||
/// squad (that would reintroduce split authority) and NOT fabricated extension
|
||||
/// state — it is an honest "no squad available", surfaced with an ERROR log.
|
||||
fn empty_squad_overlay(persona_id: i64) -> Value {
|
||||
json!({
|
||||
"id": ACTIVE_SQUAD_WIRE_ID,
|
||||
"personaId": persona_id,
|
||||
"changed": 0,
|
||||
"actives": [],
|
||||
"players": [],
|
||||
})
|
||||
}
|
||||
|
||||
/// Replace `resp`'s body with `body`, fixing framing headers (Content-Length,
|
||||
/// Content-Type; drops any stale length/transfer-encoding).
|
||||
fn set_json_body(resp: &mut WireResponse, body: Vec<u8>) {
|
||||
resp.headers.retain(|(k, _)| {
|
||||
!k.eq_ignore_ascii_case("content-length")
|
||||
&& !k.eq_ignore_ascii_case("content-type")
|
||||
&& !k.eq_ignore_ascii_case("transfer-encoding")
|
||||
});
|
||||
resp.headers.push(("Content-Type".to_string(), "application/json".to_string()));
|
||||
resp.headers.push(("Content-Length".to_string(), body.len().to_string()));
|
||||
resp.body = body;
|
||||
}
|
||||
|
||||
/// `GET …/userMassInfo` — proxy the request to Python verbatim, then overlay ONLY
|
||||
/// `.squad` with the Rust/Core projection. Every unrelated field (`userInfo`,
|
||||
/// `settings`, `userData`, `pileSizeClientData`, …) is preserved exactly.
|
||||
pub fn handle_user_mass_info(
|
||||
method: &str,
|
||||
target: &str,
|
||||
headers: &[(String, String)],
|
||||
body: &[u8],
|
||||
deps: &SquadDeps<'_>,
|
||||
pass: &PassClient,
|
||||
) -> (WireResponse, SquadLog) {
|
||||
let mut resp = match pass.forward(method, target, headers, body) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return (
|
||||
error_response(502, "upstream_unavailable"),
|
||||
SquadLog { outcome: "python_unreachable", detail: e.to_string() },
|
||||
)
|
||||
}
|
||||
};
|
||||
// Only a successful JSON object carrying `.squad` is overlaid; anything else
|
||||
// is returned verbatim (we never invent a squad into an unrelated response).
|
||||
if !(200..300).contains(&resp.status) {
|
||||
return (resp, SquadLog { outcome: "python_non_2xx_passthrough", detail: String::new() });
|
||||
}
|
||||
let mut root: Value = match serde_json::from_slice::<Value>(&resp.body) {
|
||||
Ok(v) if v.is_object() => v,
|
||||
_ => return (resp, SquadLog { outcome: "python_body_unusable_passthrough", detail: String::new() }),
|
||||
};
|
||||
if root.get("squad").is_none() {
|
||||
return (resp, SquadLog { outcome: "python_no_squad_passthrough", detail: String::new() });
|
||||
}
|
||||
// Preserve the client's persona from Python's own response.
|
||||
let persona = root["squad"]
|
||||
.get("personaId")
|
||||
.and_then(|x| x.as_i64())
|
||||
.or_else(|| root.get("userInfo").and_then(|u| u.get("personaId")).and_then(|x| x.as_i64()))
|
||||
.unwrap_or(0);
|
||||
let (squad_val, log) = match project_active_squad(deps) {
|
||||
HostProjection::Squad(v) => (
|
||||
user_mass_info_squad(v, persona),
|
||||
SquadLog { outcome: "ok", detail: String::new() },
|
||||
),
|
||||
HostProjection::Stale => (
|
||||
empty_squad_overlay(persona),
|
||||
SquadLog { outcome: "stale_integrity", detail: "stale extension not applied".into() },
|
||||
),
|
||||
HostProjection::Missing => (
|
||||
empty_squad_overlay(persona),
|
||||
SquadLog { outcome: "missing_integrity", detail: "no extension stored".into() },
|
||||
),
|
||||
HostProjection::Error(e) => (
|
||||
empty_squad_overlay(persona),
|
||||
SquadLog { outcome: "core_error", detail: e },
|
||||
),
|
||||
};
|
||||
root["squad"] = squad_val;
|
||||
let new_body = serde_json::to_vec(&root).unwrap_or_else(|_| resp.body.clone());
|
||||
set_json_body(&mut resp, new_body);
|
||||
(resp, log)
|
||||
}
|
||||
|
||||
// ───────────────────────────── HTTP wire types ──────────────────────────────
|
||||
|
||||
/// A response ready to write: status, headers, body.
|
||||
@@ -479,7 +1008,9 @@ impl PassClient {
|
||||
pub struct Server {
|
||||
core: Arc<dyn CoreAccess>,
|
||||
entities: Arc<Fifa17Entities>,
|
||||
assets: Arc<dyn ItemIdentityResolver + Send + Sync>,
|
||||
/// The single production identity resolver, shared by `/club`, `/squad/*`
|
||||
/// and the userMassInfo overlay — one wire↔owned identity everywhere.
|
||||
resolver: Arc<Fifa17IdentityResolver>,
|
||||
pass: Arc<PassClient>,
|
||||
}
|
||||
|
||||
@@ -488,13 +1019,13 @@ impl Server {
|
||||
pub fn new(
|
||||
core: Arc<dyn CoreAccess>,
|
||||
entities: Arc<Fifa17Entities>,
|
||||
assets: Arc<dyn ItemIdentityResolver + Send + Sync>,
|
||||
resolver: Arc<Fifa17IdentityResolver>,
|
||||
pass: Arc<PassClient>,
|
||||
) -> Self {
|
||||
Server {
|
||||
core,
|
||||
entities,
|
||||
assets,
|
||||
resolver,
|
||||
pass,
|
||||
}
|
||||
}
|
||||
@@ -510,19 +1041,28 @@ impl Server {
|
||||
.map_err(|e| format!("loading card-definition catalog {}: {e}", cfg.catalog_path))?;
|
||||
let store = openfut_identity::JsonIdentityStore::open(&cfg.identity_store_path)
|
||||
.map_err(|e| format!("opening identity store {}: {e}", cfg.identity_store_path))?;
|
||||
let assets: Arc<dyn ItemIdentityResolver + Send + Sync> =
|
||||
Arc::new(Fifa17IdentityResolver::new(catalog, Arc::new(store)));
|
||||
let resolver = Arc::new(Fifa17IdentityResolver::new(catalog, Arc::new(store)));
|
||||
Ok(Server {
|
||||
core: Arc::new(HttpCoreClient::new(
|
||||
cfg.core_url.clone(),
|
||||
Fifa17WireItemIdPolicy::GAME,
|
||||
)),
|
||||
entities: Arc::new(entities),
|
||||
assets,
|
||||
resolver,
|
||||
pass: Arc::new(PassClient::new(cfg.python_upstream.clone())),
|
||||
})
|
||||
}
|
||||
|
||||
/// Assemble the shared squad dependencies (Core access + the one production
|
||||
/// resolver + entity tables).
|
||||
fn squad_deps(&self) -> SquadDeps<'_> {
|
||||
SquadDeps {
|
||||
core: self.core.as_ref(),
|
||||
resolver: self.resolver.as_ref(),
|
||||
entities: self.entities.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Route one request to a response. Classification happens here, once,
|
||||
/// before either branch runs.
|
||||
pub fn handle(
|
||||
@@ -539,7 +1079,7 @@ impl Server {
|
||||
let deps = ClubDeps {
|
||||
core: self.core.as_ref(),
|
||||
entities: self.entities.as_ref(),
|
||||
assets: self.assets.as_ref(),
|
||||
assets: self.resolver.as_ref(),
|
||||
};
|
||||
let (resp, log) = handle_club(query, &deps);
|
||||
eprintln!(
|
||||
@@ -548,6 +1088,34 @@ impl Server {
|
||||
);
|
||||
resp
|
||||
}
|
||||
Route::SquadReplace => {
|
||||
let deps = self.squad_deps();
|
||||
let (resp, log) = handle_put_squad(body, &deps);
|
||||
eprintln!(
|
||||
"utas-host owner=RUST route=squad-replace status={} outcome={} detail=[{}]",
|
||||
resp.status, log.outcome, log.detail
|
||||
);
|
||||
resp
|
||||
}
|
||||
Route::SquadList => {
|
||||
let deps = self.squad_deps();
|
||||
let (resp, log) = handle_squad_list(&deps);
|
||||
eprintln!(
|
||||
"utas-host owner=RUST route=squad-list status={} outcome={} detail=[{}]",
|
||||
resp.status, log.outcome, log.detail
|
||||
);
|
||||
resp
|
||||
}
|
||||
Route::UserMassInfo => {
|
||||
let deps = self.squad_deps();
|
||||
let (resp, log) =
|
||||
handle_user_mass_info(method, target, headers, body, &deps, self.pass.as_ref());
|
||||
eprintln!(
|
||||
"utas-host owner=RUST_OVERLAY route=userMassInfo status={} squad_outcome={} detail=[{}]",
|
||||
resp.status, log.outcome, log.detail
|
||||
);
|
||||
resp
|
||||
}
|
||||
Route::Passthrough => {
|
||||
let resp = match self.pass.forward(method, target, headers, body) {
|
||||
Ok(r) => r,
|
||||
|
||||
Reference in New Issue
Block a user