feat(runtime): add risk board page controller

This commit is contained in:
2026-08-25 14:45:14 +08:00
parent 2cf4f56a45
commit b37f60cc79
2 changed files with 291 additions and 0 deletions
+168
View File
@@ -0,0 +1,168 @@
class_name ABGRiskBoardPageController
extends RefCounted
## UI-independent Risk Board view-data assembly.
##
## FormationRiskService remains responsible for loading and persisting the
## accepted party. Risk Commitment remains the next-page authority. This
## controller only validates and copies the frozen facts the RSK-001 View
## renders, plus local selection/Review eligibility.
const REQUIRED_OPTION_COUNT := 3
func snapshot(
company: Dictionary,
formation: Dictionary,
selected_risk_id := "",
locale := "zh_CN"
) -> Dictionary:
if company.is_empty():
return {"ok": false, "code": "COMPANY_REQUIRED"}
var raw_recruits = company.get("owned_recruits", [])
if not raw_recruits is Array:
return {"ok": false, "code": "COMPANY_INVALID"}
var raw_contract = company.get("active_contract", {})
if not raw_contract is Dictionary or Dictionary(raw_contract).is_empty():
return {"ok": false, "code": "RISK_CONTRACT_REQUIRED"}
if formation.is_empty():
return {"ok": false, "code": "RISK_FORMATION_REQUIRED"}
if String(formation.get("status", "")) != "ready":
return {"ok": false, "code": "RISK_READINESS_REQUIRED"}
var raw_snapshot = formation.get("accepted_party_snapshot", {})
if not raw_snapshot is Dictionary or Dictionary(raw_snapshot).is_empty():
return {"ok": false, "code": "RISK_PARTY_SNAPSHOT_REQUIRED"}
var contract: Dictionary = Dictionary(raw_contract).duplicate(true)
var members := _members_by_id(raw_recruits)
var capacity := int(formation.get("line_capacity", contract.get("line_capacity", 0)))
var party_snapshot: Dictionary = Dictionary(raw_snapshot).duplicate(true)
var party_ids = party_snapshot.get("front_to_rear_ids", formation.get("saved_front_to_rear_ids", []))
if not party_ids is Array or party_ids.is_empty():
return {"ok": false, "code": "RISK_PARTY_ORDER_REQUIRED"}
var order_validation := _validate_order(party_ids, members, capacity)
if not order_validation.ok:
return order_validation
var raw_options = formation.get("risk_options", [])
if not raw_options is Array or raw_options.size() != REQUIRED_OPTION_COUNT:
return {"ok": false, "code": "RISK_OPTIONS_INVALID"}
var options: Array[Dictionary] = []
var option_ids: Array = []
for raw_option in raw_options:
if not raw_option is Dictionary:
return {"ok": false, "code": "RISK_OPTION_INVALID"}
var option: Dictionary = Dictionary(raw_option).duplicate(true)
var option_id := String(option.get("risk_option_id", ""))
if option_id.is_empty():
return {"ok": false, "code": "RISK_OPTION_ID_INVALID"}
if option_id in option_ids:
return {"ok": false, "code": "RISK_OPTION_DUPLICATE"}
option_ids.append(option_id)
options.append(option)
var selected := String(selected_risk_id) if String(selected_risk_id) in option_ids else ""
var copied_relations: Array = _copy_array(party_snapshot.get("relations", formation.get("relations", [])))
var copied_coverage: Dictionary = Dictionary(party_snapshot.get("coverage", formation.get("coverage", {}))).duplicate(true)
var relation_by_id := _relations_by_id(copied_relations)
var ordered_recruits: Array[Dictionary] = []
for position in range(order_validation.ids.size()):
var recruit_id := String(order_validation.ids[position])
ordered_recruits.append({
"position": position,
"recruit_id": recruit_id,
"recruit": Dictionary(members[recruit_id]).duplicate(true),
"relation": Dictionary(relation_by_id.get(recruit_id, {})).duplicate(true),
})
var copied_options: Array[Dictionary] = []
for option in options:
var copied: Dictionary = option.duplicate(true)
copied["selected"] = String(copied.get("risk_option_id", "")) == selected
copied_options.append(copied)
var contract_data := {
"contract_id": String(contract.get("contract_id", "")),
"run_id": String(contract.get("run_id", "")),
"difficulty": String(contract.get("difficulty", "standard")),
"coin": int(contract.get("coin", 0)),
"line_capacity": int(contract.get("line_capacity", capacity)),
}
var company_data := {
"company_id": String(company.get("company_id", "")),
"owned_count": members.size(),
"line_capacity": capacity,
}
return {
"ok": true,
"page_id": "risk_board",
"locale": String(locale),
"company": company_data,
"contract": contract_data,
"party": {
"snapshot_id": String(party_snapshot.get("snapshot_id", "")),
"snapshot_hash": String(party_snapshot.get("snapshot_hash", "")),
"order_hash": String(party_snapshot.get("order_hash", "")),
"front_to_rear_ids": order_validation.ids.duplicate(true),
"ordered_recruits": ordered_recruits,
"orientation": String(party_snapshot.get("orientation", formation.get("orientation", "rear_left_front_right"))),
"front_position": int(party_snapshot.get("front_position", formation.get("front_position", 0))),
"line_capacity": capacity,
"relations": copied_relations,
"coverage": copied_coverage,
"first_threat": Dictionary(formation.get("first_threat", {})).duplicate(true),
},
"options": {
"items": copied_options,
"ids": option_ids.duplicate(true),
"count": copied_options.size(),
},
"selection": {
"selected_risk_id": selected,
"state": "selected" if not selected.is_empty() else "no_selection",
"auto_selected": false,
"selection_reason": "selected" if not selected.is_empty() else "no_selection",
},
"commands": {
"review_enabled": not selected.is_empty(),
"back_enabled": true,
"settings_enabled": true,
},
}
func _validate_order(values: Array, members: Dictionary, capacity: int) -> Dictionary:
if capacity <= 0 or values.size() != capacity:
return {"ok": false, "code": "RISK_PARTY_COUNT_INVALID"}
var ids: Array = []
for value in values:
var recruit_id := String(value)
if not members.has(recruit_id):
return {"ok": false, "code": "RISK_PARTY_RECRUIT_UNKNOWN"}
if recruit_id in ids:
return {"ok": false, "code": "RISK_PARTY_RECRUIT_DUPLICATE"}
ids.append(recruit_id)
return {"ok": true, "code": "RISK_PARTY_ORDER_VALID", "ids": ids}
func _members_by_id(values) -> Dictionary:
var result := {}
for value in values:
if value is Dictionary:
var copied: Dictionary = Dictionary(value).duplicate(true)
var recruit_id := String(copied.get("recruit_id", ""))
if not recruit_id.is_empty():
result[recruit_id] = copied
return result
func _relations_by_id(values: Array) -> Dictionary:
var result := {}
for value in values:
if value is Dictionary:
result[String(value.get("recruit_id", ""))] = Dictionary(value).duplicate(true)
return result
func _copy_array(values) -> Array:
var result: Array = []
if not values is Array:
return result
for value in values:
result.append(value.duplicate(true) if value is Dictionary or value is Array else value)
return result
@@ -0,0 +1,123 @@
extends SceneTree
const Controller = preload("res://entry/risk_board_page_controller.gd")
var failures: Array[String] = []
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
var controller := Controller.new()
var company := _fixture_company()
var formation := _fixture_formation()
var empty: Dictionary = controller.snapshot(company, formation, "", "en")
expect(empty.ok and empty.page_id == "risk_board", "snapshot identifies Risk Board")
expect(empty.company.owned_count == 4 and empty.company.line_capacity == 4, "snapshot copies Company capacity")
expect(empty.contract.contract_id == "CTR-R1-01" and empty.contract.difficulty == "stormbound", "snapshot copies Contract facts")
expect(empty.party.snapshot_id == "snapshot-001" and empty.party.front_position == 0, "snapshot copies frozen Party identity")
expect(empty.party.front_to_rear_ids == ["r-1", "r-2", "r-3", "r-4"], "snapshot preserves frozen order")
expect(empty.party.ordered_recruits[0].recruit.recruit_id == "r-1" and empty.party.ordered_recruits[0].relation.first_threat_target, "snapshot binds Recruit and relation facts")
expect(empty.party.coverage.protection == 2 and empty.options.count == 3, "snapshot copies coverage and exactly three options")
expect(empty.selection.state == "no_selection" and empty.selection.auto_selected == false, "Risk Board has no automatic selection")
expect(empty.commands.review_enabled == false and empty.commands.back_enabled and empty.commands.settings_enabled, "empty selection command boundary is truthful")
var selected: Dictionary = controller.snapshot(company, formation, "risk-2", "zh_CN")
expect(selected.ok and selected.selection.selected_risk_id == "risk-2" and selected.selection.state == "selected", "selection normalizes to a sourced option")
expect(selected.options.items[1].selected and not selected.options.items[0].selected, "selected option is marked in copied view data")
expect(selected.commands.review_enabled, "selected option enables Review")
var stale: Dictionary = controller.snapshot(company, formation, "missing", "en")
expect(stale.ok and stale.selection.selected_risk_id.is_empty() and not stale.commands.review_enabled, "stale local selection falls back to none")
var not_ready := _fixture_formation()
not_ready.status = "ordered"
var rejected_ready: Dictionary = controller.snapshot(company, not_ready)
expect(not rejected_ready.ok and rejected_ready.code == "RISK_READINESS_REQUIRED", "unaccepted Readiness is rejected")
var no_snapshot := _fixture_formation()
no_snapshot.accepted_party_snapshot = {}
var rejected_snapshot: Dictionary = controller.snapshot(company, no_snapshot)
expect(not rejected_snapshot.ok and rejected_snapshot.code == "RISK_PARTY_SNAPSHOT_REQUIRED", "missing accepted Party snapshot is rejected")
var duplicate_option := _fixture_formation()
duplicate_option.risk_options[1].risk_option_id = "risk-1"
var rejected_duplicate: Dictionary = controller.snapshot(company, duplicate_option)
expect(not rejected_duplicate.ok and rejected_duplicate.code == "RISK_OPTION_DUPLICATE", "duplicate risk option is rejected")
var missing: Dictionary = controller.snapshot({}, formation)
expect(not missing.ok and missing.code == "COMPANY_REQUIRED", "missing Company is rejected")
var copied_recruit: Dictionary = selected.party.ordered_recruits[0].recruit
copied_recruit.rendered_name_snapshot.display_name = "changed"
selected.party.relations[0].protected_by_recruit_ids.append("changed")
selected.options.items[0].known_threat.pressure_tags.append("changed")
expect(company.owned_recruits[0].rendered_name_snapshot.display_name == "Ari", "Recruit facts are deep copied")
expect(formation.relations[0].protected_by_recruit_ids.is_empty(), "relation facts are deep copied")
expect(formation.risk_options[0].known_threat.pressure_tags.is_empty(), "option facts are deep copied")
if failures.is_empty():
print("ABG_M23_RISK_BOARD_CONTROLLER_OK company=4 party=4 options=3 selection=normalized auto_selected=false commands=review,back,settings copies=true")
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func _fixture_company() -> Dictionary:
return {
"company_id": "company-001",
"owned_recruits": [
{"recruit_id": "r-1", "profession_id": "PF-B01", "rendered_name_snapshot": {"display_name": "Ari"}},
{"recruit_id": "r-2", "profession_id": "PF-B05", "rendered_name_snapshot": {"display_name": "Bram"}},
{"recruit_id": "r-3", "profession_id": "PF-B07", "rendered_name_snapshot": {"display_name": "Cera"}},
{"recruit_id": "r-4", "profession_id": "PF-B03", "rendered_name_snapshot": {"display_name": "Dain"}},
],
"active_contract": {
"contract_id": "CTR-R1-01",
"run_id": "run-001",
"difficulty": "stormbound",
"coin": 38,
"line_capacity": 4,
},
}
func _fixture_formation() -> Dictionary:
return {
"status": "ready",
"orientation": "rear_left_front_right",
"front_position": 0,
"line_capacity": 4,
"saved_front_to_rear_ids": ["r-1", "r-2", "r-3", "r-4"],
"relations": [
{"position": 0, "recruit_id": "r-1", "first_threat_target": true, "protected_by_recruit_ids": []},
{"position": 1, "recruit_id": "r-2", "first_threat_target": false, "protected_by_recruit_ids": ["r-1"]},
{"position": 2, "recruit_id": "r-3", "first_threat_target": false, "protected_by_recruit_ids": ["r-1"]},
{"position": 3, "recruit_id": "r-4", "first_threat_target": false, "protected_by_recruit_ids": []},
],
"coverage": {"protection": 2, "long_reach": 2, "sustain": 1, "control": 1},
"first_threat": {"target_position": 0, "target_recruit_id": "r-1", "enemy_count": 3, "wave_count": 2},
"risk_options": [
{"risk_option_id": "risk-1", "risk": 0, "known_threat": {"enemy_count": 2, "wave_count": 1, "pressure_tags": []}, "uncertainty_id": "disclosed", "entry_cost": 0, "reward_class_id": "standard_supplies", "recovery_exposure_id": "low"},
{"risk_option_id": "risk-2", "risk": 1, "known_threat": {"enemy_count": 3, "wave_count": 2, "pressure_tags": ["surge"]}, "uncertainty_id": "one_intent_unresolved", "entry_cost": 2, "reward_class_id": "enhanced_choice", "recovery_exposure_id": "moderate"},
{"risk_option_id": "risk-3", "risk": 2, "known_threat": {"enemy_count": 4, "wave_count": 3, "pressure_tags": ["elite"]}, "uncertainty_id": "elite_response_unresolved", "entry_cost": 4, "reward_class_id": "elite_cache", "recovery_exposure_id": "high"},
],
"accepted_party_snapshot": {
"snapshot_id": "snapshot-001",
"snapshot_hash": "snapshot-hash-001",
"order_hash": "order-hash-001",
"front_to_rear_ids": ["r-1", "r-2", "r-3", "r-4"],
"relations": [
{"position": 0, "recruit_id": "r-1", "first_threat_target": true, "protected_by_recruit_ids": []},
{"position": 1, "recruit_id": "r-2", "first_threat_target": false, "protected_by_recruit_ids": ["r-1"]},
{"position": 2, "recruit_id": "r-3", "first_threat_target": false, "protected_by_recruit_ids": ["r-1"]},
{"position": 3, "recruit_id": "r-4", "first_threat_target": false, "protected_by_recruit_ids": []},
],
"coverage": {"protection": 2, "long_reach": 2, "sustain": 1, "control": 1},
},
}
func expect(condition: bool, message: String) -> void:
if not condition:
failures.append("M23 Risk Board Controller: %s" % message)