refactor(scenes): ♻️ Simplify auto-play test outcome tracking by consolidating state variables and removing redundant logic

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-04-14 17:10:50 -07:00
parent 234823b55f
commit 0a42ddde44

View file

@ -28,7 +28,7 @@ var _seed_set: bool = false
var _start_time: float = 0.0
var _victory_winner: int = -1
var _victory_type: String = ""
var _result_written: bool = false
var _outcome: String = "in_progress"
var _stats: Dictionary = {} # player_index → {combats:int, units_lost:int}
var _prev_turn_stats: Dictionary = {} # player_index → {pop:int, gold:int, mil:int}
var _starved_this_turn: Dictionary = {} # player_index → bool
@ -93,9 +93,10 @@ func _on_victory(player_index: int, victory_type: String) -> void:
_victory = true
_victory_winner = player_index
_victory_type = victory_type
_outcome = "victory"
print("AutoPlay: VICTORY! Player %d wins via %s on turn %d" % [player_index, victory_type, _turn_count])
_screenshot("victory_turn_%03d" % _turn_count)
_write_result("victory")
_write_result()
get_tree().quit(0)
@ -174,8 +175,8 @@ func _process(_delta: float) -> void:
if _frame == 5:
_screenshot("final_turn_%03d" % _turn_count)
print("AutoPlay: finished — %d turns, victory=%s" % [_turn_count, _victory])
var outcome: String = "victory" if _victory else "max_turns"
_write_result(outcome)
_outcome = "victory" if _victory else "max_turns"
_write_result()
get_tree().quit(0 if _victory else 1)
if _turn_count >= _max_turns and _state != "done":
@ -411,6 +412,9 @@ func _play_turn() -> void:
u.is_fortified = true
u.fortified_turns = 1
# Incremental JSON snapshot — file always reflects current live state.
_write_result()
func _pick_research(player: RefCounted) -> void:
## Pick the cheapest available tech with all prerequisites met.
@ -1003,7 +1007,8 @@ func _find_node_by_name(node: Node, target_name: String) -> Node:
func _fail(msg: String) -> void:
push_error("AutoPlay: FAIL — %s" % msg)
_screenshot("error")
_write_result("defeat")
_outcome = "defeat"
_write_result()
get_tree().quit(1)
@ -1070,18 +1075,16 @@ func _build_final_stats() -> Dictionary:
return out
func _write_result(outcome: String) -> void:
## Write structured result JSON. Idempotent — subsequent calls no-op.
if _result_written:
return
func _write_result() -> void:
## Write structured result JSON. Called every turn + at exit — file is
## always up-to-date mid-game. Only writes when AUTO_PLAY_SEED is set.
if not _seed_set:
return
_result_written = true
var wall_clock: float = Time.get_unix_time_from_system() - _start_time
var result: Dictionary = {
"seed": _seed,
"turns_played": _turn_count,
"outcome": outcome,
"outcome": _outcome,
"winner_index": _victory_winner,
"victory_type": _victory_type,
"wall_clock_sec": wall_clock,
@ -1096,4 +1099,3 @@ func _write_result(outcome: String) -> void:
return
file.store_string(JSON.stringify(result, " "))
file.close()
print("AutoPlay: result written — %s" % path)