feat(bat002): add non-settling observation service

This commit is contained in:
2026-08-24 12:43:36 +08:00
parent e3d3ac3d1c
commit 2d4d5a76e3
2 changed files with 630 additions and 0 deletions
+387
View File
@@ -5,6 +5,7 @@ const DraftService = preload("res://entry/initial_company_draft_service.gd")
const DEFAULT_COMPANY_PATH := "user://aetherbound_expedition_company.json"
const TOTAL_PHASES := 9
const OBSERVATION_VERSION := 1
var company_path := DEFAULT_COMPANY_PATH
@@ -104,6 +105,8 @@ func advance_battle(operation_id: String) -> Dictionary:
var battle: Dictionary = contract.get("battle_state", {})
if battle.is_empty() or String(battle.get("operation_id", "")) != operation_id:
return {"ok": false, "code": "BATTLE_NOT_FOUND"}
if String(battle.status) == "outcome_locked":
return {"ok": false, "code": "BATTLE_OUTCOME_HANDOFF_REQUIRED"}
if String(battle.status) == "result" or String(battle.status) == "recovered":
return {"ok": true, "code": "battle_already_settled", "company": company, "battle": battle}
var phase: int = int(battle.phase_index)
@@ -127,6 +130,190 @@ func advance_battle(operation_id: String) -> Dictionary:
return {"ok": true, "code": "battle_advanced", "company": saved.company, "battle": saved.company.active_contract.battle_state, "event": event}
func begin_observation(operation_id: String, expected_snapshot_hash := "") -> Dictionary:
var context := _load_observation_context(operation_id, false)
if not context.ok:
return context
var company: Dictionary = context.company
var contract: Dictionary = context.contract
var battle: Dictionary = context.battle
var receipt: Dictionary = context.receipt
if not expected_snapshot_hash.is_empty():
if not _is_hex(expected_snapshot_hash, 64) or expected_snapshot_hash != String(battle.snapshot_hash):
return {"ok": false, "code": "OBSERVATION_SNAPSHOT_MISMATCH"}
if battle.has("observation") and not Dictionary(battle.observation).is_empty():
return {
"ok": true,
"code": "observation_outcome_locked" if String(battle.observation.status) == "outcome_locked" else "observation_already_started",
"company": company,
"battle": battle,
"observation": Dictionary(battle.observation).duplicate(true),
"receipt": receipt,
}
if String(battle.status) != "active" or int(battle.phase_index) != 0 or not Array(battle.phase_events).is_empty():
return {"ok": false, "code": "OBSERVATION_OPENING_REQUIRED"}
var observation := {
"observation_version": OBSERVATION_VERSION,
"status": "waiting_for_next_event",
"operation_id": operation_id,
"snapshot_id": String(battle.snapshot_id),
"snapshot_hash": String(battle.snapshot_hash),
"receipt_hash": String(receipt.receipt_hash),
"next_phase_index": 0,
"last_event_id": "",
"last_event_hash": "",
"event_journal_hash": _hash([]),
"outcome_lock": {},
}
_refresh_hash(observation, "observation_hash")
battle.observation = observation
var saved := _save_battle_state(company, contract, battle)
if not saved.ok:
return saved
var saved_battle: Dictionary = saved.company.active_contract.battle_state
return {
"ok": true,
"code": "observation_started",
"company": saved.company,
"battle": saved_battle,
"observation": Dictionary(saved_battle.observation).duplicate(true),
"receipt": receipt,
}
func preview_observation_event(operation_id: String) -> Dictionary:
var context := _load_observation_context(operation_id, true)
if not context.ok:
return context
var battle: Dictionary = context.battle
var observation: Dictionary = context.observation
if String(observation.status) == "outcome_locked":
return {
"ok": true,
"code": "observation_outcome_locked",
"company": context.company,
"battle": battle,
"observation": observation,
"outcome_lock": Dictionary(observation.outcome_lock).duplicate(true),
}
var phase := int(observation.next_phase_index)
if phase < 0 or phase >= TOTAL_PHASES:
return {"ok": false, "code": "OBSERVATION_EVENT_ORDER_INVALID"}
var event := _observation_event_for_phase(context.company, battle, phase)
return {
"ok": true,
"code": "observation_event_ready",
"company": context.company,
"battle": battle,
"observation": observation,
"event": event,
}
func commit_observation_event(operation_id: String, event_id: String, event_hash: String) -> Dictionary:
if not _is_hex(event_id, 32) or not _is_hex(event_hash, 64):
return {"ok": false, "code": "OBSERVATION_EVENT_ID_INVALID"}
var context := _load_observation_context(operation_id, true)
if not context.ok:
return context
var company: Dictionary = context.company
var contract: Dictionary = context.contract
var battle: Dictionary = context.battle
var observation: Dictionary = context.observation
for committed_event in battle.phase_events:
if String(committed_event.get("event_id", "")) != event_id:
continue
if String(committed_event.get("event_hash", "")) != event_hash:
return {"ok": false, "code": "OBSERVATION_EVENT_HASH_MISMATCH"}
return {
"ok": true,
"code": "observation_event_already_committed",
"company": company,
"battle": battle,
"observation": observation,
"event": Dictionary(committed_event).duplicate(true),
}
if String(observation.status) == "outcome_locked":
return {"ok": false, "code": "OBSERVATION_OUTCOME_LOCKED"}
var phase := int(observation.next_phase_index)
if phase < 0 or phase >= TOTAL_PHASES:
return {"ok": false, "code": "OBSERVATION_EVENT_ORDER_INVALID"}
var expected_event := _observation_event_for_phase(company, battle, phase)
if String(expected_event.event_id) != event_id:
return {"ok": false, "code": "OBSERVATION_EVENT_ORDER_MISMATCH"}
if String(expected_event.event_hash) != event_hash:
return {"ok": false, "code": "OBSERVATION_EVENT_HASH_MISMATCH"}
battle.phase_events.append(expected_event.duplicate(true))
battle.phase_index = phase + 1
if String(expected_event.kind) == "knockout" and not String(expected_event.target_id).is_empty():
battle.knocked_out_ids = [String(expected_event.target_id)]
observation.next_phase_index = int(battle.phase_index)
observation.last_event_id = event_id
observation.last_event_hash = event_hash
observation.event_journal_hash = _hash(battle.phase_events)
var code := "observation_event_committed"
if String(expected_event.kind) == "result":
var decisive := _observation_decisive_event(battle.phase_events)
var outcome_lock := {
"outcome_id": ("%s|outcome|%s" % [operation_id, event_hash]).sha256_text().substr(0, 32),
"operation_id": operation_id,
"snapshot_hash": String(battle.snapshot_hash),
"outcome": String(expected_event.outcome),
"outcome_cause": "enemy_line_broken",
"outcome_tick": int(expected_event.event_tick),
"final_snapshot_hash": _observation_final_snapshot_hash(battle, String(observation.event_journal_hash)),
"event_journal_hash": String(observation.event_journal_hash),
"decisive_event_id": String(decisive.get("event_id", "")),
"decisive_event_hash": String(decisive.get("event_hash", "")),
"out001_is_next": true,
}
_refresh_hash(outcome_lock, "outcome_hash")
observation.status = "outcome_locked"
observation.outcome_lock = outcome_lock
battle.status = "outcome_locked"
code = "observation_outcome_locked"
else:
observation.status = "waiting_for_next_event"
_refresh_hash(observation, "observation_hash")
battle.observation = observation
var saved := _save_battle_state(company, contract, battle)
if not saved.ok:
return saved
var saved_battle: Dictionary = saved.company.active_contract.battle_state
return {
"ok": true,
"code": code,
"company": saved.company,
"battle": saved_battle,
"observation": Dictionary(saved_battle.observation).duplicate(true),
"event": expected_event,
"outcome_lock": Dictionary(saved_battle.observation.outcome_lock).duplicate(true),
}
func retry_observation_snapshot(operation_id: String, expected_snapshot_hash: String, expected_journal_hash := "") -> Dictionary:
if not _is_hex(expected_snapshot_hash, 64):
return {"ok": false, "code": "OBSERVATION_SNAPSHOT_ID_INVALID"}
if not expected_journal_hash.is_empty() and not _is_hex(expected_journal_hash, 64):
return {"ok": false, "code": "OBSERVATION_JOURNAL_ID_INVALID"}
var context := _load_observation_context(operation_id, true)
if not context.ok:
return context
var observation: Dictionary = context.observation
if expected_snapshot_hash != String(observation.snapshot_hash):
return {"ok": false, "code": "OBSERVATION_SNAPSHOT_MISMATCH"}
if not expected_journal_hash.is_empty() and expected_journal_hash != String(observation.event_journal_hash):
return {"ok": false, "code": "OBSERVATION_JOURNAL_MISMATCH"}
return {
"ok": true,
"code": "observation_snapshot_valid",
"company": context.company,
"battle": context.battle,
"observation": observation,
"receipt": context.receipt,
}
func recover_next_stage(operation_id: String) -> Dictionary:
if not _is_hex(operation_id, 32):
return {"ok": false, "code": "RECOVERY_OPERATION_ID_INVALID"}
@@ -272,6 +459,206 @@ func _settle_battle(company: Dictionary, battle: Dictionary) -> Dictionary:
return {"ok": true, "code": "battle_settled", "company": saved.company, "battle": saved.company.active_contract.battle_state, "event": result_event}
func _load_observation_context(operation_id: String, require_observation: bool) -> Dictionary:
if not _is_hex(operation_id, 32):
return {"ok": false, "code": "BATTLE_OPERATION_ID_INVALID"}
var loaded := load_company()
if not loaded.ok:
return loaded
var company: Dictionary = loaded.company.duplicate(true)
var contract: Dictionary = company.get("active_contract", {})
var battle: Dictionary = contract.get("battle_state", {})
if battle.is_empty() or String(battle.get("operation_id", "")) != operation_id:
return {"ok": false, "code": "BATTLE_NOT_FOUND"}
if String(battle.get("status", "")) not in ["active", "outcome_locked"]:
return {"ok": false, "code": "OBSERVATION_NOT_ACTIVE"}
if not _is_hex(String(battle.get("battle_hash", "")), 64) or _hash_without(battle, "battle_hash") != String(battle.battle_hash):
return {"ok": false, "code": "OBSERVATION_BATTLE_HASH_INVALID"}
var receipts: Array = contract.get("battle_receipts", [])
if receipts.size() != 1:
return {"ok": false, "code": "OBSERVATION_RECEIPT_COUNT_INVALID"}
var receipt: Dictionary = Dictionary(receipts[0])
if (
String(receipt.get("operation_id", "")) != operation_id
or String(receipt.get("snapshot_id", "")) != String(battle.get("snapshot_id", ""))
or String(receipt.get("snapshot_hash", "")) != String(battle.get("snapshot_hash", ""))
or String(receipt.get("company_id", "")) != String(battle.get("company_id", ""))
or String(receipt.get("run_id", "")) != String(battle.get("run_id", ""))
):
return {"ok": false, "code": "OBSERVATION_RECEIPT_SOURCE_INVALID"}
if not _is_hex(String(receipt.get("receipt_hash", "")), 64) or _hash_without(receipt, "receipt_hash") != String(receipt.receipt_hash):
return {"ok": false, "code": "OBSERVATION_RECEIPT_HASH_INVALID"}
var observation: Dictionary = battle.get("observation", {})
if require_observation and observation.is_empty():
return {"ok": false, "code": "OBSERVATION_NOT_STARTED"}
if not observation.is_empty():
var validation := _validate_observation_state(battle, receipt, observation)
if not validation.ok:
return validation
return {
"ok": true,
"code": "observation_context_valid",
"company": company,
"contract": contract,
"battle": battle,
"receipt": receipt.duplicate(true),
"observation": observation.duplicate(true),
}
func _validate_observation_state(battle: Dictionary, receipt: Dictionary, observation: Dictionary) -> Dictionary:
var required := [
"observation_version", "status", "operation_id", "snapshot_id",
"snapshot_hash", "receipt_hash", "next_phase_index", "last_event_id",
"last_event_hash", "event_journal_hash", "outcome_lock",
"observation_hash",
]
for field in required:
if not observation.has(field):
return {"ok": false, "code": "OBSERVATION_FIELD_MISSING", "field": field}
if (
int(observation.observation_version) != OBSERVATION_VERSION
or String(observation.status) not in ["waiting_for_next_event", "outcome_locked"]
or String(observation.operation_id) != String(battle.operation_id)
or String(observation.snapshot_id) != String(battle.snapshot_id)
or String(observation.snapshot_hash) != String(battle.snapshot_hash)
or String(observation.receipt_hash) != String(receipt.receipt_hash)
):
return {"ok": false, "code": "OBSERVATION_IDENTITY_INVALID"}
var events: Array = battle.get("phase_events", [])
if int(observation.next_phase_index) != int(battle.phase_index) or events.size() != int(battle.phase_index) or events.size() > TOTAL_PHASES:
return {"ok": false, "code": "OBSERVATION_EVENT_ORDER_INVALID"}
var prior_events: Array = []
for index in range(events.size()):
var event: Dictionary = events[index]
if (
int(event.get("phase", -1)) != index
or String(event.get("event_id", "")) != _observation_event_id(battle, index)
or String(event.get("previous_journal_hash", "")) != _hash(prior_events)
or not _is_hex(String(event.get("event_hash", "")), 64)
or _hash_without(event, "event_hash") != String(event.event_hash)
):
return {"ok": false, "code": "OBSERVATION_EVENT_HASH_INVALID"}
prior_events.append(event.duplicate(true))
if String(observation.event_journal_hash) != _hash(events):
return {"ok": false, "code": "OBSERVATION_JOURNAL_HASH_INVALID"}
if events.is_empty():
if not String(observation.last_event_id).is_empty() or not String(observation.last_event_hash).is_empty():
return {"ok": false, "code": "OBSERVATION_LAST_EVENT_INVALID"}
else:
var last: Dictionary = events.back()
if String(observation.last_event_id) != String(last.event_id) or String(observation.last_event_hash) != String(last.event_hash):
return {"ok": false, "code": "OBSERVATION_LAST_EVENT_INVALID"}
if String(observation.status) == "outcome_locked":
var outcome_lock: Dictionary = observation.outcome_lock
if (
String(battle.status) != "outcome_locked"
or events.size() != TOTAL_PHASES
or outcome_lock.is_empty()
or String(outcome_lock.get("event_journal_hash", "")) != String(observation.event_journal_hash)
or not _is_hex(String(outcome_lock.get("outcome_hash", "")), 64)
or _hash_without(outcome_lock, "outcome_hash") != String(outcome_lock.outcome_hash)
):
return {"ok": false, "code": "OBSERVATION_OUTCOME_INVALID"}
elif String(battle.status) != "active" or not Dictionary(observation.outcome_lock).is_empty():
return {"ok": false, "code": "OBSERVATION_STATUS_INVALID"}
if not _is_hex(String(observation.observation_hash), 64) or _hash_without(observation, "observation_hash") != String(observation.observation_hash):
return {"ok": false, "code": "OBSERVATION_HASH_INVALID"}
return {"ok": true, "code": "observation_valid"}
func _save_battle_state(company: Dictionary, contract: Dictionary, battle: Dictionary) -> Dictionary:
_refresh_hash(battle, "battle_hash")
contract.battle_state = battle
_refresh_hash(contract, "contract_hash")
company.active_contract = contract
_refresh_hash(company, "company_hash")
return _store().save_company(company)
func _observation_event_for_phase(company: Dictionary, battle: Dictionary, phase: int) -> Dictionary:
var event := _event_for_phase(company, battle, phase)
event.erase("event_hash")
if String(event.kind) in ["telegraph", "knockout"]:
var allied_ids: Array = battle.get("front_to_rear_ids", [])
var enemy_ids: Array = battle.get("enemy_ids", [])
event.actor_id = String(enemy_ids[0]) if not enemy_ids.is_empty() else "enemy-front"
event.target_id = String(allied_ids[0]) if not allied_ids.is_empty() else ""
var source_id := String(event.get("actor_id", ""))
var target_id := String(event.get("target_id", ""))
var previous_event_id := _observation_event_id(battle, phase - 1) if phase > 0 else ""
var next_event_id := _observation_event_id(battle, phase + 1) if phase + 1 < TOTAL_PHASES else ""
event.event_id = _observation_event_id(battle, phase)
event.event_tick = phase
event.event_priority = phase
event.event_kind = "outcome_lock" if String(event.kind) == "result" else String(event.kind)
event.event_families = _observation_event_families(String(event.kind))
event.action_id = String(event.get("caption_id", ""))
event.source_actor_id = source_id
event.forecast_target_ids = [target_id] if not target_id.is_empty() else []
event.reserved_target_id = target_id
event.actual_recipient_ids = [target_id] if not target_id.is_empty() else []
event.outcome_disposition = String(event.get("outcome", "observed"))
event.state_delta = {
"kind": String(event.kind),
"amount": int(event.get("amount", 0)),
"direction": _observation_delta_direction(String(event.kind)),
}
event.cause_event_ids = [previous_event_id] if not previous_event_id.is_empty() else []
event.previous_major_event_id = previous_event_id
event.next_major_event_id = next_event_id
event.previous_journal_hash = _hash(Array(battle.get("phase_events", [])))
_refresh_hash(event, "event_hash")
return event
func _observation_event_families(kind: String) -> Array[String]:
match kind:
"opening": return ["battle_opening"]
"telegraph": return ["enemy_action_and_target_relation"]
"guard": return ["guard_or_protection_intercept"]
"ranged": return ["ranged_release_and_contact", "damage_source_target_and_value"]
"magic": return ["magic_cast_and_resolution", "damage_source_target_and_value"]
"healing": return ["healing_source_and_recipient_change"]
"damage": return ["melee_contact", "damage_source_target_and_value"]
"knockout": return ["enemy_action_and_target_relation", "damage_source_target_and_value", "knockout_and_line_collapse"]
"guarded_recovery": return ["guard_or_protection_intercept"]
"result": return ["outcome_lock"]
return []
func _observation_delta_direction(kind: String) -> String:
if kind in ["ranged", "magic", "damage", "knockout"]:
return "loss"
if kind in ["guard", "healing", "guarded_recovery"]:
return "gain"
return "none"
func _observation_event_id(battle: Dictionary, phase: int) -> String:
return ("%s|%s|observation|%d" % [String(battle.get("operation_id", "")), String(battle.get("snapshot_hash", "")), phase]).sha256_text().substr(0, 32)
func _observation_decisive_event(events: Array) -> Dictionary:
for event in events:
if String(event.get("kind", "")) == "damage":
return Dictionary(event).duplicate(true)
return Dictionary(events.back()).duplicate(true) if not events.is_empty() else {}
func _observation_final_snapshot_hash(battle: Dictionary, journal_hash: String) -> String:
return _hash({
"operation_id": String(battle.operation_id),
"snapshot_id": String(battle.snapshot_id),
"snapshot_hash": String(battle.snapshot_hash),
"front_to_rear_ids": Array(battle.front_to_rear_ids).duplicate(),
"enemy_ids": Array(battle.enemy_ids).duplicate(),
"phase_index": int(battle.phase_index),
"knocked_out_ids": Array(battle.knocked_out_ids).duplicate(),
"event_journal_hash": journal_hash,
})
func _apply_xp(recruit: Dictionary, amount: int) -> void:
var level := int(recruit.level)
var xp := int(recruit.xp) + amount
@@ -0,0 +1,243 @@
extends SceneTree
const DraftService = preload("res://entry/initial_company_draft_service.gd")
const ContractService = preload("res://entry/guild_contract_service.gd")
const FormationService = preload("res://entry/formation_risk_service.gd")
const BattleService = preload("res://entry/battle_service.gd")
const DRAFT_PATH := "user://dev5_battle_observation_draft.json"
const COMPANY_PATH := "user://dev5_battle_observation_company.json"
const SEED := "13579bdf2468ace013579bdf2468ace0"
const CONTRACT_OP := "1029384756abcdef1029384756abcdef"
const HIRE_OP_1 := "11112222333344445555666677778888"
const HIRE_OP_2 := "88887777666655554444333322221111"
const ORDER_OP := "abcdefabcdefabcdefabcdefabcdefab"
const READY_OP := "1234567890abcdef1234567890abcdef"
const BATTLE_OP := "fedcba0987654321fedcba0987654321"
const WRONG_HASH := "0000000000000000000000000000000000000000000000000000000000000000"
const WRONG_EVENT_ID := "00000000000000000000000000000000"
var failures: Array[String] = []
func _initialize() -> void:
call_deferred("run_test")
func run_test() -> void:
_cleanup()
var prepared := _create_prepared_company()
if not prepared.is_empty():
_verify_non_settling_observation(prepared)
_cleanup()
prepared = _create_prepared_company()
if not prepared.is_empty():
_verify_legacy_settlement(prepared)
_cleanup()
if failures.is_empty():
print("ABG_DEV5_BATTLE_OBSERVATION_SERVICE_OK begin=no_tick receipt=one preview=stable commit=idempotent events=9 families=melee,ranged,magic,healing,guard,damage,knockout,enemy,outcome outcome=locked settlement=false reward=false recovery=false retry=validated legacy_settlement=true")
quit(0)
else:
for failure in failures:
push_error(failure)
quit(1)
func _verify_non_settling_observation(prepared: Dictionary) -> void:
var service := _battle_service()
var option_id := String(prepared.active_contract.formation.risk_options[1].risk_option_id)
var started: Dictionary = service.begin_battle(BATTLE_OP, option_id)
_expect(started.ok, "battle fixture starts")
if not started.ok:
return
var snapshot_hash := String(started.battle.snapshot_hash)
var receipt_before: Dictionary = Dictionary(started.company.active_contract.battle_receipts[0]).duplicate(true)
var coin_before := int(started.company.active_contract.coin)
var recruits_before := JSON.stringify(started.company.owned_recruits)
var rejected_snapshot: Dictionary = service.begin_observation(BATTLE_OP, WRONG_HASH)
_expect(not rejected_snapshot.ok and rejected_snapshot.code == "OBSERVATION_SNAPSHOT_MISMATCH", "wrong opening snapshot is rejected")
var begun: Dictionary = service.begin_observation(BATTLE_OP, snapshot_hash)
_expect(begun.ok and begun.code == "observation_started", "the original receipt starts observation")
if not begun.ok:
return
_expect(int(begun.battle.phase_index) == 0 and begun.battle.phase_events.is_empty(), "begin observation advances no hidden tick")
_expect(begun.company.active_contract.battle_receipts.size() == 1 and _same_receipt(begun.company.active_contract.battle_receipts[0], receipt_before), "begin observation preserves the one original receipt byte-for-byte")
var duplicate_begin: Dictionary = service.begin_observation(BATTLE_OP, snapshot_hash)
_expect(duplicate_begin.ok and duplicate_begin.code == "observation_already_started" and int(duplicate_begin.battle.phase_index) == 0, "duplicate observation begin is idempotent")
var first_preview: Dictionary = service.preview_observation_event(BATTLE_OP)
_expect(first_preview.ok and first_preview.code == "observation_event_ready", "first event can be previewed")
if not first_preview.ok:
return
var wrong_order: Dictionary = service.commit_observation_event(BATTLE_OP, WRONG_EVENT_ID, String(first_preview.event.event_hash))
_expect(not wrong_order.ok and wrong_order.code == "OBSERVATION_EVENT_ORDER_MISMATCH", "wrong event identity cannot create an order gap")
var kinds: Array[String] = []
var families: Array[String] = []
var last: Dictionary = begun
for phase in range(9):
var preview_a: Dictionary = service.preview_observation_event(BATTLE_OP)
var preview_b: Dictionary = service.preview_observation_event(BATTLE_OP)
_expect(preview_a.ok and preview_b.ok, "phase %d previews without mutation" % phase)
if not preview_a.ok or not preview_b.ok:
return
_expect(preview_a.event == preview_b.event, "phase %d preview identity and hash are stable" % phase)
_expect(int(preview_a.event.phase) == phase and int(preview_a.event.event_tick) == phase, "phase %d has an ordered tick" % phase)
if phase > 0 and phase < 8:
_expect(not String(preview_a.event.source_actor_id).is_empty() and not Array(preview_a.event.actual_recipient_ids).is_empty(), "phase %d binds a source and actual recipient" % phase)
kinds.append(String(preview_a.event.kind))
for family in preview_a.event.event_families:
if String(family) not in families:
families.append(String(family))
var wrong_hash: Dictionary = service.commit_observation_event(BATTLE_OP, String(preview_a.event.event_id), WRONG_HASH)
_expect(not wrong_hash.ok and wrong_hash.code == "OBSERVATION_EVENT_HASH_MISMATCH", "phase %d rejects a mismatched event hash" % phase)
var still_same: Dictionary = service.preview_observation_event(BATTLE_OP)
_expect(still_same.ok and still_same.event == preview_a.event, "phase %d rejection leaves the same event ready" % phase)
last = service.commit_observation_event(BATTLE_OP, String(preview_a.event.event_id), String(preview_a.event.event_hash))
_expect(last.ok, "phase %d commits" % phase)
if not last.ok:
return
var duplicate: Dictionary = service.commit_observation_event(BATTLE_OP, String(preview_a.event.event_id), String(preview_a.event.event_hash))
_expect(duplicate.ok and duplicate.code == "observation_event_already_committed", "phase %d commit retry is idempotent" % phase)
_expect(kinds == ["opening", "telegraph", "guard", "ranged", "magic", "healing", "damage", "knockout", "result"], "observation preserves the authored first-battle event order")
for required_family in [
"melee_contact", "ranged_release_and_contact", "magic_cast_and_resolution",
"healing_source_and_recipient_change", "guard_or_protection_intercept",
"damage_source_target_and_value", "knockout_and_line_collapse",
"enemy_action_and_target_relation", "outcome_lock",
]:
_expect(required_family in families, "required event family %s is bound" % required_family)
_expect(last.code == "observation_outcome_locked" and String(last.battle.status) == "outcome_locked", "the final event locks an immutable outcome")
var outcome: Dictionary = last.observation.outcome_lock
_expect(not outcome.is_empty() and outcome.outcome == "victory" and outcome.out001_is_next, "outcome lock names victory and the OUT-001 boundary")
_expect(int(last.company.active_contract.coin) == coin_before, "observation awards no Coin")
_expect(last.company.active_contract.status == "battle" and last.company.active_contract.battle_settlement_receipts.is_empty(), "observation creates no settlement receipt or result phase")
_expect(last.battle.result.is_empty() and JSON.stringify(last.company.owned_recruits) == recruits_before, "observation mutates no result, XP or Recruit history")
_expect(last.company.active_contract.battle_receipts.size() == 1 and _same_receipt(last.company.active_contract.battle_receipts[0], receipt_before), "outcome lock preserves the original receipt byte-for-byte")
var retry: Dictionary = service.retry_observation_snapshot(BATTLE_OP, snapshot_hash, String(last.observation.event_journal_hash))
_expect(retry.ok and retry.code == "observation_snapshot_valid", "snapshot retry validates the same receipt and journal")
var wrong_journal: Dictionary = service.retry_observation_snapshot(BATTLE_OP, snapshot_hash, WRONG_HASH)
_expect(not wrong_journal.ok and wrong_journal.code == "OBSERVATION_JOURNAL_MISMATCH", "snapshot retry rejects another journal")
var blocked_legacy: Dictionary = service.advance_battle(BATTLE_OP)
_expect(not blocked_legacy.ok and blocked_legacy.code == "BATTLE_OUTCOME_HANDOFF_REQUIRED", "legacy settlement cannot bypass the OUT-001 handoff")
var blocked_recovery: Dictionary = service.recover_next_stage("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_expect(not blocked_recovery.ok and blocked_recovery.code == "RECOVERY_RESULT_REQUIRED", "recovery cannot run before OUT-001 settlement")
var resumed: Dictionary = service.begin_observation(BATTLE_OP, snapshot_hash)
_expect(resumed.ok and resumed.code == "observation_outcome_locked", "restart resolves the persisted outcome lock without reseeding")
var loaded: Dictionary = service.load_company()
_expect(loaded.ok and _draft_service().validate_company(loaded.company).ok, "outcome-locked Company remains valid after restart")
if loaded.ok:
var corrupt_company: Dictionary = loaded.company.duplicate(true)
var corrupt_contract: Dictionary = corrupt_company.active_contract
var corrupt_battle: Dictionary = corrupt_contract.battle_state
corrupt_battle.phase_events[3].amount = int(corrupt_battle.phase_events[3].amount) + 1
service._refresh_hash(corrupt_battle, "battle_hash")
corrupt_contract.battle_state = corrupt_battle
service._refresh_hash(corrupt_contract, "contract_hash")
corrupt_company.active_contract = corrupt_contract
service._refresh_hash(corrupt_company, "company_hash")
var corrupt_save: Dictionary = _draft_service().save_company(corrupt_company)
_expect(corrupt_save.ok, "fixture persists a valid outer envelope around a corrupt event")
var rejected_corruption: Dictionary = service.retry_observation_snapshot(BATTLE_OP, snapshot_hash)
_expect(not rejected_corruption.ok and rejected_corruption.code == "OBSERVATION_EVENT_HASH_INVALID", "event-content corruption pauses before resume")
func _verify_legacy_settlement(prepared: Dictionary) -> void:
var service := _battle_service()
var option_id := String(prepared.active_contract.formation.risk_options[1].risk_option_id)
var started: Dictionary = service.begin_battle(BATTLE_OP, option_id)
_expect(started.ok, "legacy fixture starts")
if not started.ok:
return
var last: Dictionary = started
for _phase in range(9):
last = service.advance_battle(BATTLE_OP)
_expect(last.ok, "legacy phase still advances")
if not last.ok:
return
_expect(String(last.battle.status) == "result" and int(last.company.active_contract.coin) == 51, "legacy advance still settles and awards the historical reward")
_expect(last.company.active_contract.battle_settlement_receipts.size() == 1, "legacy settlement still writes exactly one receipt")
func _create_prepared_company() -> Dictionary:
var draft: Dictionary = _draft_service().begin_or_load(SEED)
if not draft.ok:
_expect(false, "fixture creates opening draft")
return {}
var selected: Dictionary = _draft_service().set_selection([String(draft.draft.offers[0].offer_id), String(draft.draft.offers[3].offer_id)])
var committed: Dictionary = _draft_service().commit_company()
if not selected.ok or not committed.ok:
_expect(false, "fixture commits opening Company")
return {}
var created: Dictionary = _contract_service().create_contract(CONTRACT_OP, "CTR-R1-01", "standard")
if not created.ok:
_expect(false, "fixture creates first Contract")
return {}
var offers: Array = created.company.active_contract.market_board.recruit_offers
_contract_service().hire_recruit(HIRE_OP_1, String(offers[0].source_offer_id), 14)
var hired: Dictionary = _contract_service().hire_recruit(HIRE_OP_2, String(offers[1].source_offer_id), 14)
if not hired.ok:
_expect(false, "fixture completes opening hires")
return {}
var begun: Dictionary = _formation_service().begin_formation()
if not begun.ok:
_expect(false, "fixture creates formation")
return {}
var saved: Dictionary = _formation_service().save_order(ORDER_OP, begun.formation.base_front_to_rear_ids)
if not saved.ok:
_expect(false, "fixture saves formation")
return {}
var ready: Dictionary = _formation_service().accept_readiness(READY_OP, String(saved.receipt.order_hash))
return ready.company if ready.ok else {}
func _draft_service() -> RefCounted:
var service := DraftService.new()
service.path = DRAFT_PATH
service.company_path = COMPANY_PATH
return service
func _contract_service() -> RefCounted:
var service := ContractService.new()
service.company_path = COMPANY_PATH
return service
func _formation_service() -> RefCounted:
var service := FormationService.new()
service.company_path = COMPANY_PATH
return service
func _battle_service() -> RefCounted:
var service := BattleService.new()
service.company_path = COMPANY_PATH
return service
func _same_receipt(left_value, right_value) -> bool:
if not left_value is Dictionary or not right_value is Dictionary:
return false
var left: Dictionary = left_value
var right: Dictionary = right_value
if left.size() != right.size():
return false
for key in right:
if not left.has(key) or left[key] != right[key]:
return false
return true
func _cleanup() -> void:
for path in [
DRAFT_PATH, "%s.bak" % DRAFT_PATH, "%s.candidate" % DRAFT_PATH,
"%s.bak.candidate" % DRAFT_PATH, COMPANY_PATH, "%s.bak" % COMPANY_PATH,
"%s.candidate" % COMPANY_PATH, "%s.bak.candidate" % COMPANY_PATH,
"%s.corrupt" % COMPANY_PATH,
]:
if FileAccess.file_exists(path):
DirAccess.remove_absolute(path)
func _expect(condition: bool, message: String) -> void:
if not condition:
failures.append("BAT2-4A observation service: %s" % message)