extends SceneTree const EntryShell = preload("res://entry/entry_shell.gd") const SettingsStore = preload("res://entry/settings_store.gd") 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_live_capture_draft.json" const COMPANY_PATH := "user://dev5_battle_live_capture_company.json" const SEED := "2468ace013579bdf2468ace013579bdf" const CONTRACT_OP := "abcdef0123456789abcdef0123456789" const HIRE_OP_1 := "11111111222222223333333344444444" const HIRE_OP_2 := "55555555666666667777777788888888" const ORDER_OP := "99999999aaaaaaaabbbbbbbbcccccccc" const READY_OP := "ddddddddeeeeeeeeffffffff00000000" const BATTLE_OP := "1234567890abcdef1234567890abcdef" const INTERRUPT_BATTLE_OP := "fedcba0987654321fedcba0987654321" var failures: Array[String] = [] var capture_count := 0 var output_root := "" var viewport_size := Vector2i(1280, 720) var fixture_index := 0 var locale := "en" func _initialize() -> void: call_deferred("run_capture") func run_capture() -> void: fixture_index = int(OS.get_environment("ABG_CAPTURE_FIXTURE_INDEX")) var fixtures := [ {"size": Vector2i(760, 360), "locale": "zh_CN"}, {"size": Vector2i(844, 390), "locale": "en"}, {"size": Vector2i(1280, 720), "locale": "zh_CN"}, {"size": Vector2i(1280, 720), "locale": "en"}, ] if fixture_index < 0 or fixture_index >= fixtures.size(): push_error("unknown BAT-002 live capture fixture %d" % fixture_index) quit(1) return viewport_size = fixtures[fixture_index].size locale = fixtures[fixture_index].locale root.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS root.content_scale_aspect = Window.CONTENT_SCALE_ASPECT_EXPAND root.size = viewport_size output_root = OS.get_environment("ABG_EVIDENCE_DIR") if output_root.is_empty(): output_root = "/tmp/abg-bat002-live-candidate" output_root = output_root.path_join("captures") DirAccess.make_dir_recursive_absolute(output_root) _cleanup() var prepared := _create_prepared_company() var started: Dictionary = _battle_service().begin_battle(BATTLE_OP, String(prepared.active_contract.formation.risk_options[1].risk_option_id)) if not prepared.is_empty() else {} _expect(bool(started.get("ok", false)), "capture fixture creates one battle receipt") if started.is_empty() or not bool(started.get("ok", false)): _finish() return var viewport := SubViewport.new() viewport.name = "BattleLiveTimelineCaptureViewport" viewport.size = viewport_size viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS root.add_child(viewport) var shell := await _new_shell(viewport) shell.show_battle(started.company, false) await _frames(5) _expect(shell.battle_live_state == "waiting_for_next_event", "capture opens at waiting boundary") await _capture(viewport, shell, "01-waiting") shell.toggle_battle_pause() shell.advance_battle_phase() await _frames(2) await _capture(viewport, shell, "02-forecast") shell.advance_battle_phase() await _frames(2) await _capture(viewport, shell, "03-committed") shell.advance_battle_phase() await _frames(2) await _capture(viewport, shell, "04-resolving") shell.advance_battle_phase() await _frames(2) _expect(shell.battle_live_state == "event_resolved", "capture reaches resolved event boundary") await _capture(viewport, shell, "05-resolved") shell.toggle_battle_pause() await _frames(2) _expect(shell.battle_live_state == "paused" and shell.battle_paused, "capture freezes paused state") await _capture(viewport, shell, "06-paused") shell.toggle_battle_pause() while shell.battle_live_state != "event_resolving": shell.advance_battle_phase() await _frames(1) if shell.battle_live_state == "outcome_locked": break _expect(shell.battle_live_state == "event_resolving", "capture reaches an authoritative resolving event") if shell.battle_live_state == "event_resolving": shell.battle_live_event.event_hash = "0".repeat(64) shell.advance_battle_phase() await _frames(2) _expect(shell.battle_live_state == "invalid_event_data" and shell.battle_paused, "capture exposes invalid-event pause") await _capture(viewport, shell, "07-invalid") shell._battle_live_retry_snapshot() await _frames(1) _expect(shell.battle_live_state == "loading_resume", "capture exposes loading-resume validation") await _capture(viewport, shell, "08-loading") await _frames(3) _expect(shell.battle_live_state == "event_resolved" and shell.battle_paused, "capture retry returns to the last verified event boundary") shell.toggle_battle_pause() var guard := 0 while shell.battle_live_state != "outcome_locked" and guard < 90: if shell.battle_paused: shell.toggle_battle_pause() shell.advance_battle_phase() await _frames(1) guard += 1 _expect(shell.battle_live_state == "outcome_locked", "capture reaches immutable outcome") await _capture(viewport, shell, "10-outcome") shell.queue_free() viewport.queue_free() await _frames(12) _cleanup() var interrupted := _create_prepared_company() var interrupted_start: Dictionary = _battle_service().begin_battle(INTERRUPT_BATTLE_OP, String(interrupted.active_contract.formation.risk_options[1].risk_option_id)) if not interrupted.is_empty() else {} _expect(bool(interrupted_start.get("ok", false)), "interruption fixture creates a second isolated receipt") if bool(interrupted_start.get("ok", false)): var interrupt_viewport := SubViewport.new() interrupt_viewport.name = "BattleLiveInterruptCaptureViewport" interrupt_viewport.size = viewport_size interrupt_viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS root.add_child(interrupt_viewport) var interrupt_shell := await _new_shell(interrupt_viewport) interrupt_shell.show_battle(interrupted_start.company, false) await _frames(4) interrupt_shell._battle_live_interrupt("capture_background") await _frames(2) _expect(interrupt_shell.battle_live_state == "interrupted_background" and interrupt_shell.battle_paused, "capture exposes BAT-006 interruption handoff") await _capture(interrupt_viewport, interrupt_shell, "09-interrupted") interrupt_shell.queue_free() interrupt_viewport.queue_free() await _frames(12) _finish() func _finish() -> void: _cleanup() if failures.is_empty(): print("ABG_DEV5_BATTLE_LIVE_CAPTURE_OK fixture=%d viewport=%dx%d locale=%s text_100=true states=waiting,forecast,committed,resolving,resolved,paused,invalid,loading,interrupted,outcome captures=%d source=runtime" % [fixture_index, viewport_size.x, viewport_size.y, locale, capture_count]) quit(0) else: for failure in failures: push_error(failure) quit(1) func _new_shell(parent: Node) -> Control: var shell := EntryShell.new() shell.auto_start = false shell.initial_company_service.path = DRAFT_PATH shell.initial_company_service.company_path = COMPANY_PATH parent.add_child(shell) await _frames(3) var settings := SettingsStore.new().defaults() settings.setup_completed = true settings.locale = locale settings.text_scale = 1.0 shell.settings = settings shell.setup_draft = settings.duplicate(true) return shell func _capture(viewport: SubViewport, shell: Control, state_name: String) -> void: _verify_layout(shell, state_name) await RenderingServer.frame_post_draw var image := viewport.get_texture().get_image() var path := output_root.path_join("battle-live-timeline-%dx%d-%s-100-%s.png" % [viewport_size.x, viewport_size.y, locale, state_name]) if image.is_empty() or image.get_width() != viewport_size.x or image.get_height() != viewport_size.y: failures.append("invalid capture image for %s" % state_name) return if image.save_png(path) != OK: failures.append("capture failed for %s" % state_name) return capture_count += 1 func _verify_layout(shell: Control, state_name: String) -> void: var canvas := shell.find_child("BattleLiveCanvas", true, false) as Control if canvas == null: failures.append("%s has no contained live canvas" % state_name) return var actors := shell.find_child("BattleActors", true, false) as Control var enemies := shell.find_child("BattleEnemies", true, false) as Control if actors == null or actors.get_child_count() != 4: failures.append("%s loses the complete Party line" % state_name) if enemies == null or enemies.get_child_count() != 3: failures.append("%s loses the complete enemy line" % state_name) _verify_buttons(shell, state_name) func _verify_buttons(node: Node, state_name: String) -> void: for child in node.get_children(): if child is BaseButton and child.is_visible_in_tree(): var rect := (child as Control).get_global_rect() if rect.size.x < 47.9 or rect.size.y < 47.9: failures.append("%s button %s below 48px" % [state_name, child.name]) if rect.position.x < -1.0 or rect.position.y < -1.0 or rect.end.x > viewport_size.x + 1.0 or rect.end.y > viewport_size.y + 1.0: failures.append("%s button %s outside viewport" % [state_name, child.name]) _verify_buttons(child, state_name) func _create_prepared_company() -> Dictionary: var draft: Dictionary = _draft_service().begin_or_load(SEED) if not draft.ok: return {} _draft_service().set_selection([String(draft.draft.offers[0].offer_id), String(draft.draft.offers[2].offer_id)]) if not _draft_service().commit_company().ok: return {} var created: Dictionary = _contract_service().create_contract(CONTRACT_OP, "CTR-R1-01", "standard") if not created.ok: 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: return {} var begun: Dictionary = _formation_service().begin_formation() var saved: Dictionary = _formation_service().save_order(ORDER_OP, begun.formation.base_front_to_rear_ids) 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 _frames(count: int) -> void: for _index in range(count): await process_frame func _expect(condition: bool, message: String) -> void: if not condition: failures.append(message) 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)