From 3e12969e39fe3de13895603eccda616cdeed6d75 Mon Sep 17 00:00:00 2001 From: wusumac <736139669@qq.com> Date: Tue, 25 Aug 2026 06:36:51 +0800 Subject: [PATCH] feat(m12): wire outcome recovery runtime route Co-Authored-By: Codex --- runtime/entry/entry_shell.gd | 226 ++++++++++++++++- runtime/tests/dev5/m12_runtime_test.gd | 324 +++++++++++++++++++++++++ 2 files changed, 546 insertions(+), 4 deletions(-) create mode 100644 runtime/tests/dev5/m12_runtime_test.gd diff --git a/runtime/entry/entry_shell.gd b/runtime/entry/entry_shell.gd index a8411fb1..1b334e84 100644 --- a/runtime/entry/entry_shell.gd +++ b/runtime/entry/entry_shell.gd @@ -164,6 +164,8 @@ var battle_notice := "" var battle_playback_generation := 0 var battle_playback_timer: Timer var battle_result_diagnosis_open := false +var battle_reward_selected_id := "" +var battle_reward_notice := "" var battle_opening_mode := false var battle_opening_state := "ready_to_observe" var battle_opening_notice := "" @@ -4975,7 +4977,7 @@ func _build_battle_live_bottom(parent: Control, company: Dictionary, battle: Dic match battle_live_state: "outcome_locked": next_text = "OUT-001" - next_action = Callable(self, "_battle_live_outcome_handoff") + next_action = Callable(self, "_battle_live_commit_outcome") next_disabled = false next_tooltip = _tr("OUT-001 负责结果诊断", "OUT-001 owns result diagnosis") "inspect_handoff", "pause_controls_handoff": @@ -5570,6 +5572,22 @@ func _battle_live_outcome_handoff() -> void: _render_battle_live() +func _battle_live_commit_outcome() -> void: + if battle_live_state != "outcome_locked": + return + battle_service.company_path = initial_company_service.company_path + var settled: Dictionary = battle_service.settle_observation_outcome(battle_operation_id) + if not settled.ok: + battle_live_notice = _battle_error_text(String(settled.get("code", "OUTCOME_SETTLEMENT_FAILED"))) + _render_battle_live() + return + guild_company = settled.company.duplicate(true) + battle_result_diagnosis_open = false + battle_reward_selected_id = "" + battle_reward_notice = "" + show_battle_result(guild_company) + + func _battle_live_open_bat006() -> void: if battle_live_state != "interrupted_background": return @@ -5991,7 +6009,7 @@ func show_battle_result(company := {}) -> void: context.add_child(detail) var footer := HBoxContainer.new() footer.name = "BattleResultFooter" - footer.custom_minimum_size = Vector2(0, 46 if short_result else 52) + footer.custom_minimum_size = Vector2(0, 48 if short_result else 52) footer.add_theme_constant_override("separation", 8) layout.add_child(footer) var inspect := _company_button("BattleResultDiagnosis", _tr("因果详情", "Cause Details"), toggle_battle_result_diagnosis, "company_secondary", "inspect") @@ -6013,7 +6031,21 @@ func toggle_battle_result_diagnosis() -> void: func continue_from_battle_result() -> void: - show_battle_knockout_review(guild_company) + var battle: Dictionary = guild_company.get("active_contract", {}).get("battle_state", {}) + var reward_draft: Dictionary = battle.get("reward_draft", {}) + var knocked: Array = battle.get("result", {}).get("knocked_out_ids", []) + if not reward_draft.is_empty() and String(reward_draft.get("status", "open")) == "open": + if not knocked.is_empty(): + show_battle_knockout_review(guild_company) + else: + show_battle_reward_choice(guild_company) + return + # Legacy DEV-5 settlement receipts have no reward draft. Preserve their + # historical route while the observation path uses OUT-003 explicitly. + if not knocked.is_empty(): + show_battle_knockout_review(guild_company) + else: + show_battle_recovery(guild_company) func show_battle_knockout_review(company := {}) -> void: @@ -6095,7 +6127,186 @@ func show_battle_knockout_review(company := {}) -> void: func acknowledge_battle_knockout(company := {}) -> void: - show_battle_recovery(company) + var resolved: Dictionary = Dictionary(company).duplicate(true) if company is Dictionary else guild_company.duplicate(true) + var battle: Dictionary = resolved.get("active_contract", {}).get("battle_state", {}) + var reward_draft: Dictionary = battle.get("reward_draft", {}) + if not reward_draft.is_empty() and String(reward_draft.get("status", "open")) == "open": + show_battle_reward_choice(resolved) + else: + show_battle_recovery(resolved) + + +func show_battle_reward_choice(company := {}) -> void: + var resolved: Dictionary = Dictionary(company).duplicate(true) if company is Dictionary else guild_company.duplicate(true) + var battle: Dictionary = resolved.get("active_contract", {}).get("battle_state", {}) + var draft: Dictionary = battle.get("reward_draft", {}) + if draft.is_empty() or String(draft.get("status", "open")) != "open": + show_battle_recovery(resolved) + return + guild_company = resolved + current_screen = "battle_reward_choice" + _build_world_base() + var frame_safe := _framed_page_safe("BattleReward") + frame_safe.add_child(_entry_chrome("BattleRewardFrame", "initial_company_scene_frame")) + var safe := _content_page_safe("BattleReward") + var layout := VBoxContainer.new() + layout.name = "BattleRewardLayout" + layout.add_theme_constant_override("separation", 6) + safe.add_child(layout) + var header := HBoxContainer.new() + header.name = "BattleRewardHeader" + header.custom_minimum_size = Vector2(0, 48 if size.y < 500.0 else 58) + layout.add_child(header) + var heading := VBoxContainer.new() + heading.size_flags_horizontal = Control.SIZE_EXPAND_FILL + header.add_child(heading) + heading.add_child(_label(_tr("奖励选择", "REWARD CHOICE"), _scaled(21), Color("#f5ead0"))) + heading.add_child(_label(_tr("稳定选项 · 领取一次或明确跳过", "STABLE OPTIONS · CLAIM ONCE OR EXPLICITLY SKIP"), _scaled(10), Color("#e8d39d"))) + var paid := _label(_tr("战斗金币已结算 %d", "BATTLE COIN PAID %d") % int(battle.get("result", {}).get("reward_coin", 0)), _scaled(12), Color("#f0ca75")) + paid.name = "BattleRewardPaid" + paid.vertical_alignment = VERTICAL_ALIGNMENT_CENTER + header.add_child(paid) + var body := HBoxContainer.new() + body.name = "BattleRewardBody" + body.size_flags_vertical = Control.SIZE_EXPAND_FILL + body.add_theme_constant_override("separation", 14) + layout.add_child(body) + var options_box := VBoxContainer.new() + options_box.name = "BattleRewardOptions" + options_box.size_flags_horizontal = Control.SIZE_EXPAND_FILL + options_box.size_flags_stretch_ratio = 1.25 + options_box.add_theme_constant_override("separation", 6) + body.add_child(options_box) + var options_title := _label(_tr("同等决策区", "EQUAL DECISION REGIONS"), _scaled(10), Color("#e8d39d")) + options_title.name = "BattleRewardOptionsTitle" + options_box.add_child(options_title) + for raw_option in draft.get("options", []): + var option: Dictionary = raw_option + var reward_id := String(option.get("reward_id", "")) + var selected := reward_id == battle_reward_selected_id + var option_button := _company_button("BattleRewardOption_%s" % reward_id, _battle_reward_option_title(option, selected), select_battle_reward.bind(reward_id), "company_primary" if selected else "company_secondary", "confirm" if selected else "inspect") + option_button.custom_minimum_size = Vector2(0, 64 if size.y < 500.0 else 72) + option_button.tooltip_text = _battle_reward_option_detail(option) + options_box.add_child(option_button) + var context := VBoxContainer.new() + context.name = "BattleRewardContext" + context.custom_minimum_size = Vector2(minf(360.0, size.x * 0.38), 0) + context.add_theme_constant_override("separation", 5) + body.add_child(context) + context.add_child(_label(_tr("当前选项", "SELECTED OPTION"), _scaled(10), Color("#e8d39d"))) + var selected_option := _reward_option_from_draft(draft, battle_reward_selected_id) + if selected_option.is_empty(): + context.add_child(_label(_tr("选择一项以查看目标、冲突和跳过结果。", "Choose one option to read target, conflict and skip outcome."), _scaled(12), Color("#aebbc2"))) + else: + var selected_detail := _label(_battle_reward_option_detail(selected_option), _scaled(12), Color("#f5ead0")) + selected_detail.name = "BattleRewardSelectedDetail" + selected_detail.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + selected_detail.size_flags_vertical = Control.SIZE_EXPAND_FILL + context.add_child(selected_detail) + if not battle_reward_notice.is_empty(): + var notice := _label(battle_reward_notice, _scaled(10), Color("#ffd4d4")) + notice.name = "BattleRewardNotice" + notice.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + context.add_child(notice) + var footer := HBoxContainer.new() + footer.name = "BattleRewardFooter" + footer.custom_minimum_size = Vector2(0, 46 if size.y < 500.0 else 52) + footer.add_theme_constant_override("separation", 8) + layout.add_child(footer) + var back := _company_button("BattleRewardBack", _tr("返回结果", "Back to Result"), show_battle_result.bind(resolved), "company_utility", "back") + footer.add_child(back) + var spacer := Control.new() + spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL + footer.add_child(spacer) + var skip := _company_button("BattleRewardSkip", _tr("跳过奖励", "Skip Reward"), decline_battle_reward, "company_utility", "back") + skip.custom_minimum_size = Vector2(180, footer.custom_minimum_size.y) + skip.tooltip_text = _tr("保留已结算战斗金币,不添加奖励对象", "Keep the battle Coin already paid; add no reward object") + footer.add_child(skip) + var claim := _company_button("BattleRewardClaim", _tr("领取所选奖励", "Claim Selected Reward"), commit_battle_reward, "company_primary", "confirm") + claim.custom_minimum_size = Vector2(250, footer.custom_minimum_size.y) + claim.disabled = selected_option.is_empty() + claim.tooltip_text = _tr("提交一次奖励回执", "Submit one reward receipt") + footer.add_child(claim) + back.call_deferred("grab_focus") + + +func select_battle_reward(reward_id: String) -> void: + var battle: Dictionary = guild_company.get("active_contract", {}).get("battle_state", {}) + var draft: Dictionary = battle.get("reward_draft", {}) + if _reward_option_from_draft(draft, reward_id).is_empty(): + return + battle_reward_selected_id = reward_id + battle_reward_notice = "" + show_battle_reward_choice(guild_company) + + +func commit_battle_reward() -> void: + if battle_reward_selected_id.is_empty(): + battle_reward_notice = _tr("请先选择一个奖励。", "Select a reward first.") + show_battle_reward_choice(guild_company) + return + battle_service.company_path = initial_company_service.company_path + var loaded: Dictionary = battle_service.load_company() + if not loaded.ok: + battle_reward_notice = _battle_error_text(String(loaded.get("code", "REWARD_LOAD_FAILED"))) + show_battle_reward_choice(guild_company) + return + var battle: Dictionary = loaded.company.active_contract.get("battle_state", {}) + var operation_id := ("%s|reward|%s" % [String(battle.get("operation_id", "")), battle_reward_selected_id]).sha256_text().substr(0, 32) + var claimed: Dictionary = battle_service.claim_reward(operation_id, battle_reward_selected_id, String(battle.get("operation_id", ""))) + if not claimed.ok: + battle_reward_notice = _battle_error_text(String(claimed.get("code", "REWARD_FAILED"))) + show_battle_reward_choice(loaded.company) + return + guild_company = claimed.company.duplicate(true) + battle_reward_notice = "" + battle_reward_selected_id = "" + show_battle_recovery(guild_company) + + +func decline_battle_reward() -> void: + battle_service.company_path = initial_company_service.company_path + var loaded: Dictionary = battle_service.load_company() + if not loaded.ok: + battle_reward_notice = _battle_error_text(String(loaded.get("code", "REWARD_LOAD_FAILED"))) + show_battle_reward_choice(guild_company) + return + var battle: Dictionary = loaded.company.active_contract.get("battle_state", {}) + var operation_id := ("%s|reward|decline" % String(battle.get("operation_id", ""))).sha256_text().substr(0, 32) + var declined: Dictionary = battle_service.decline_reward(operation_id, String(battle.get("operation_id", ""))) + if not declined.ok: + battle_reward_notice = _battle_error_text(String(declined.get("code", "REWARD_FAILED"))) + show_battle_reward_choice(loaded.company) + return + guild_company = declined.company.duplicate(true) + battle_reward_selected_id = "" + battle_reward_notice = "" + show_battle_recovery(guild_company) + + +func _reward_option_from_draft(draft: Dictionary, reward_id: String) -> Dictionary: + if reward_id.is_empty(): + return {} + for raw_option in draft.get("options", []): + var option: Dictionary = raw_option + if String(option.get("reward_id", "")) == reward_id: + return option.duplicate(true) + return {} + + +func _battle_reward_option_title(option: Dictionary, selected: bool = false) -> String: + var marker := "◆ " if selected else "◇ " + var kind := String(option.get("kind", "")) + if kind == "equipment": + return "%s%s" % [marker, _tr("免费装备 · %s", "FREE EQUIPMENT · %s") % _content_name("equipment", String(option.get("catalog_id", "")))] + return "%s%s" % [marker, _tr("金币储备 · +%d", "COIN RESERVE · +%d") % int(option.get("amount", 0))] + + +func _battle_reward_option_detail(option: Dictionary) -> String: + return _tr( + "改变:%s\n目标:%s\n冲突:%s\n跳过:%s", + "Changes: %s\nTarget: %s\nConflict: %s\nSkip: %s" + ) % [String(option.get("next_decision", "")), String(option.get("eligible_target", "")), String(option.get("conflict", "")), String(option.get("skip_outcome", ""))] func show_battle_recovery(company := {}) -> void: @@ -7386,6 +7597,13 @@ func _battle_error_text(code: String) -> String: "BATTLE_READINESS_REQUIRED": return _tr("请先接受准备检查。", "Accept Readiness first.") "BATTLE_RISK_UNAVAILABLE": return _tr("该风险选项已失效,请返回风险板。", "That risk option is stale; return to the Risk Board.") "BATTLE_ALREADY_ACTIVE": return _tr("已有进行中的战斗;已恢复原战斗回执。", "A battle is already active; its original receipt was restored.") + "BATTLE_OUTCOME_REQUIRED", "OBSERVATION_NOT_READY_FOR_RESULT", "OBSERVATION_OUTCOME_LOCK_MISSING": return _tr("结果尚未锁定,无法进入结算。", "The outcome is not locked yet; settlement is unavailable.") + "OBSERVATION_RESULT_INVALID": return _tr("结果事件或日志校验失败;战斗保持不变。", "The result event or journal failed validation; battle state is unchanged.") + "REWARD_RESULT_REQUIRED": return _tr("请先完成结果结算。", "Complete result settlement first.") + "REWARD_DRAFT_MISSING": return _tr("本场没有可用的奖励草案。", "No reward draft is available for this battle.") + "REWARD_OPTION_UNAVAILABLE", "REWARD_ALREADY_APPLIED": return _tr("该奖励选择已失效;请读取原回执。", "That reward choice is stale; read the original receipt.") + "REWARD_OPERATION_CONFLICT": return _tr("奖励操作 ID 与原意图冲突;没有重复发放。", "The reward operation conflicts with its original intent; nothing was duplicated.") + "RECOVERY_REWARD_REQUIRED": return _tr("请先领取或跳过本场奖励。", "Claim or skip this battle's reward first.") "RECOVERY_RESULT_REQUIRED": return _tr("结果回执尚未完成,无法恢复下一阶段。", "The result receipt is not ready for next-stage recovery.") "recovery_already_applied": return _tr("恢复已提交;正在打开下一阶段市场。", "Recovery was already committed; opening the next-stage Market.") return _tr("战斗回执未能创建;远征团没有改变。", "The battle receipt could not be created; Company state is unchanged.") + " [%s]" % code diff --git a/runtime/tests/dev5/m12_runtime_test.gd b/runtime/tests/dev5/m12_runtime_test.gd new file mode 100644 index 00000000..57b30a1f --- /dev/null +++ b/runtime/tests/dev5/m12_runtime_test.gd @@ -0,0 +1,324 @@ +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://m12_runtime_draft.json" +const COMPANY_PATH := "user://m12_runtime_company.json" +const SEED := "97531eca86420bdf97531eca86420bdf" +const CONTRACT_OP := "abcdef0123456789abcdef0123456789" +const HIRE_OP_1 := "11112222333344445555666677778888" +const HIRE_OP_2 := "88887777666655554444333322221111" +const ORDER_OP := "fedcba9876543210fedcba9876543210" +const READY_OP := "0123456789abcdef0123456789abcdef" +const BATTLE_OP := "2468ace013579bdf2468ace013579bdf" + +const 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"}, +] + +var failures: Array[String] = [] +var parsed_touches := 0 +var fixture_count := 0 + + +func _initialize() -> void: + call_deferred("run_test") + + +func run_test() -> void: + _cleanup() + root.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS + root.content_scale_aspect = Window.CONTENT_SCALE_ASPECT_EXPAND + var prepared := _create_prepared_company() + _expect(not prepared.is_empty(), "fixture prepares a four-member Company") + if not prepared.is_empty(): + await _run_route(prepared) + _cleanup() + if failures.is_empty(): + print("ABG_M12_RUNTIME_OK route=BAT002,OUT001,OUT002,OUT003,OUT004 market=true restart=true fixtures=%d input=true targets=48 strict=0 locale=zh_CN,en scale=100" % fixture_count) + quit(0) + else: + for failure in failures: + push_error(failure) + quit(1) + + +func _run_route(prepared: Dictionary) -> void: + var option_id := String(prepared.active_contract.formation.risk_options[1].risk_option_id) + var service := _battle_service() + var started: Dictionary = service.begin_battle(BATTLE_OP, option_id) + _expect(started.ok, "fixture starts one battle receipt") + if not started.ok: + return + var observed: Dictionary = service.begin_observation(BATTLE_OP, String(started.battle.snapshot_hash)) + _expect(observed.ok, "BAT-002 observation opens without settlement") + if not observed.ok: + return + var shell := await _new_shell(FIXTURES[1].size, String(FIXTURES[1].locale)) + shell.show_battle(observed.company, false) + shell.battle_paused = false + shell._schedule_battle_playback(shell.battle_playback_generation) + await _frames(4) + _expect(shell.current_screen == "battle" and not shell.battle_opening_mode, "BAT-002 opens the live battlefield") + _expect(shell.find_child("BattleLiveNextBoundary", true, false) != null, "BAT-002 exposes the OUT-001 boundary") + _verify_buttons(shell, FIXTURES[1].size, "live") + + var guard := 0 + while shell.battle_live_state != "outcome_locked" and guard < 60: + if shell.battle_paused: + shell.battle_paused = false + shell.advance_battle_phase() + await _frames(1) + guard += 1 + _expect(shell.battle_live_state == "outcome_locked", "BAT-002 reaches an immutable outcome lock") + var locked_before: Dictionary = service.load_company() + _expect(locked_before.ok and String(locked_before.company.active_contract.battle_state.status) == "outcome_locked", "outcome lock persists before settlement") + var locked_observation: Dictionary = locked_before.company.active_contract.battle_state.observation if locked_before.ok else {} + var locked_hash := String(locked_observation.get("observation_hash", "")) + var next_boundary := shell.find_child("BattleLiveNextBoundary", true, false) as Button + _expect(next_boundary != null and not next_boundary.disabled, "OUT-001 boundary is actionable") + if next_boundary != null: + await _touch(next_boundary) + _expect(shell.current_screen == "battle_result", "OUT-001 opens Result after explicit settlement") + var settled: Dictionary = service.load_company() + _expect(settled.ok and String(settled.company.active_contract.battle_state.status) == "result", "settlement enters Result state") + if settled.ok: + var current_observation: Dictionary = settled.company.active_contract.battle_state.observation + _expect(String(current_observation.get("observation_hash", "")) == locked_hash, "settlement preserves the immutable observation hash") + _expect(shell.find_child("BattleResultObjective", true, false) != null, "OUT-001 binds objective") + _expect(shell.find_child("BattleResultDecisive", true, false) != null, "OUT-001 binds decisive event") + _expect(shell.find_child("BattleResultChain", true, false) != null, "OUT-001 binds causal chain") + _verify_buttons(shell, FIXTURES[1].size, "result") + var continue_button := shell.find_child("BattleResultContinue", true, false) as Button + _expect(continue_button != null and not continue_button.disabled, "Result Continue is actionable") + if continue_button != null: + await _touch(continue_button) + var battle: Dictionary = shell.guild_company.active_contract.battle_state + var knocked: Array = battle.get("result", {}).get("knocked_out_ids", []) + if not knocked.is_empty(): + _expect(shell.current_screen == "battle_knockout_review", "OUT-002 opens for a knocked-out Recruit") + _expect(shell.find_child("BattleKnockoutCurrentGap", true, false) != null, "OUT-002 explains the battle-local gap") + _expect(shell.find_child("BattleKnockoutReturnRule", true, false) != null, "OUT-002 preserves the return rule") + _verify_buttons(shell, FIXTURES[1].size, "knockout") + var acknowledge := shell.find_child("BattleKnockoutAcknowledge", true, false) as Button + _expect(acknowledge != null and not acknowledge.disabled, "OUT-002 acknowledgement is actionable") + if acknowledge != null: + await _touch(acknowledge) + else: + _expect(shell.current_screen == "battle_reward_choice", "no-knockout Result routes directly to OUT-003") + if shell.current_screen == "battle_knockout_review": + _expect(shell.current_screen == "battle_reward_choice", "OUT-002 advances to OUT-003") + _expect(shell.current_screen == "battle_reward_choice", "OUT-003 reward choice is reachable") + _expect(shell.find_child("BattleRewardOptions", true, false) != null, "OUT-003 renders equal decision options") + var reward_buttons := _reward_buttons(shell) + _expect(reward_buttons.size() >= 2, "OUT-003 provides at least two deterministic options") + _verify_buttons(shell, FIXTURES[1].size, "reward") + if not reward_buttons.is_empty(): + var first_reward := reward_buttons[0] as Button + var claim := shell.find_child("BattleRewardClaim", true, false) as Button + _expect(claim != null and claim.disabled, "Reward claim is disabled before selection") + await _touch(first_reward) + claim = shell.find_child("BattleRewardClaim", true, false) as Button + _expect(claim != null and not claim.disabled, "Reward claim enables after selection") + if claim != null: + await _touch(claim) + _expect(shell.current_screen == "battle_recovery", "OUT-003 advances to OUT-004 after one claim") + _expect(shell.find_child("BattleRecoveryRule", true, false) != null, "OUT-004 binds recovery rule") + _verify_buttons(shell, FIXTURES[1].size, "recovery") + var recovery_commit := shell.find_child("BattleRecoveryCommit", true, false) as Button + _expect(recovery_commit != null and not recovery_commit.disabled, "OUT-004 commit is actionable") + if recovery_commit != null: + await _touch(recovery_commit) + _expect(shell.current_screen == "market", "OUT-004 opens the next-stage Market") + _expect(shell.find_child("MarketMasterCanvas", true, false) != null, "next-stage Market renders its layout") + var recovered: Dictionary = service.load_company() + _expect(recovered.ok and String(recovered.company.active_contract.status) == "market", "recovery persists Market status") + _expect(recovered.ok and recovered.company.active_contract.get("recovery_receipts", []).size() == 1, "recovery writes one receipt") + var stable_ids := _recruit_ids(prepared) + _expect(recovered.ok and _recruit_ids(recovered.company) == stable_ids, "recovery preserves Recruit identities") + for fixture in FIXTURES: + await _verify_fixture_pages(recovered.company if recovered.ok else shell.guild_company, fixture) + shell.queue_free() + await _frames(5) + var resumed_host := await _new_fixture_shell(FIXTURES[0].size, String(FIXTURES[0].locale)) + var resumed: Control = resumed_host.shell + resumed._open_company_destination(recovered.company if recovered.ok else shell.guild_company) + await _frames(4) + _expect(resumed.current_screen == "market" and resumed.find_child("MarketMasterCanvas", true, false) != null, "restart resumes at the next-stage Market") + resumed.queue_free() + (resumed_host.viewport as SubViewport).queue_free() + await _frames(5) + + +func _verify_fixture_pages(company: Dictionary, fixture: Dictionary) -> void: + var size: Vector2i = fixture.size + var locale := String(fixture.locale) + var host := await _new_fixture_shell(size, locale) + var shell: Control = host.shell + var battle: Dictionary = company.active_contract.get("battle_state", {}) + shell.show_battle_result(company) + await _frames(2) + _expect(shell.current_screen == "battle_result", "%s %s Result fits" % [size, locale]) + _verify_buttons(shell, size, "result %s %s" % [size, locale]) + shell.show_battle_knockout_review(company) + await _frames(2) + _verify_buttons(shell, size, "knockout %s %s" % [size, locale]) + if String(battle.get("reward_draft", {}).get("status", "")) == "claimed": + # Use the same committed state to verify the post-claim recovery page. + shell.show_battle_recovery(company) + await _frames(2) + _verify_buttons(shell, size, "recovery %s %s" % [size, locale]) + shell.show_market(company) + await _frames(2) + _expect(shell.current_screen == "market", "%s %s Market fits" % [size, locale]) + _verify_buttons(shell, size, "market %s %s" % [size, locale]) + fixture_count += 1 + shell.queue_free() + (host.viewport as SubViewport).queue_free() + await _frames(3) + + +func _new_shell(viewport_size: Vector2i, locale: String) -> Control: + root.size = viewport_size + var shell := EntryShell.new() + shell.auto_start = false + shell.initial_company_service.path = DRAFT_PATH + shell.initial_company_service.company_path = COMPANY_PATH + root.add_child(shell) + await _frames(2) + 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 _new_fixture_shell(viewport_size: Vector2i, locale: String) -> Dictionary: + var viewport := SubViewport.new() + viewport.name = "M12FixtureViewport" + viewport.size = viewport_size + viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS + root.add_child(viewport) + var shell := EntryShell.new() + shell.auto_start = false + shell.initial_company_service.path = DRAFT_PATH + shell.initial_company_service.company_path = COMPANY_PATH + viewport.add_child(shell) + await _frames(2) + 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 {"viewport": viewport, "shell": shell} + + +func _reward_buttons(shell: Control) -> Array[Button]: + var result: Array[Button] = [] + var options := shell.find_child("BattleRewardOptions", true, false) + if options == null: + return result + for child in options.get_children(): + if child is Button and String(child.name).begins_with("BattleRewardOption_"): + result.append(child) + return result + + +func _verify_buttons(node: Node, viewport_size: Vector2i, state: String) -> void: + for child in node.get_children(): + if child is BaseButton and child.is_visible_in_tree(): + if state.begins_with("market") and String(child.name).begins_with("MarketCategory"): + _verify_buttons(child, viewport_size, state) + continue + var rect := (child as Control).get_global_rect() + _expect(rect.size.x >= 47.9 and rect.size.y >= 47.9, "%s button %s is at least 48px (rect=%s)" % [state, child.name, rect]) + _expect(rect.position.x >= -1.0 and rect.position.y >= -1.0 and rect.end.x <= viewport_size.x + 1.0 and rect.end.y <= viewport_size.y + 1.0, "%s button %s stays inside viewport (rect=%s)" % [state, child.name, rect]) + _verify_buttons(child, viewport_size, state) + + +func _create_prepared_company() -> Dictionary: + var draft_service := DraftService.new() + draft_service.path = DRAFT_PATH + draft_service.company_path = COMPANY_PATH + 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[1].offer_id)]) + if not draft_service.commit_company().ok: + return {} + var contract_service := ContractService.new() + contract_service.company_path = COMPANY_PATH + 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 formation_service := FormationService.new() + formation_service.company_path = COMPANY_PATH + 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 _battle_service() -> RefCounted: + var service := BattleService.new() + service.company_path = COMPANY_PATH + return service + + +func _recruit_ids(company: Dictionary) -> Array[String]: + var result: Array[String] = [] + for recruit in company.get("owned_recruits", []): + result.append(String(recruit.get("recruit_id", ""))) + return result + + +func _touch(control: Control) -> void: + if control == null: + _expect(false, "touch target exists") + return + var center := control.get_global_rect().get_center() + var press := InputEventScreenTouch.new() + press.position = center + press.pressed = true + press.index = 0 + Input.parse_input_event(press) + await _frames(2) + var release := InputEventScreenTouch.new() + release.position = center + release.pressed = false + release.index = 0 + Input.parse_input_event(release) + parsed_touches += 1 + await _frames(5) + + +func _frames(count: int) -> void: + for _index in range(count): + await process_frame + + +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("M12 Runtime: %s" % message)