class_name AudienceState extends RefCounted # Audience state belongs to one run. Spectacle is cumulative and thresholds are # crossed once, so replaying panel refreshes can never manufacture followers. const DEFAULT_FOLLOWER_THRESHOLDS: Array[Dictionary] = [ {"spectacle": 10, "followers": 50}, {"spectacle": 30, "followers": 200}, {"spectacle": 60, "followers": 350}, ] const DEFAULT_SPONSORS: Array[Dictionary] = [ {"id": "forge_du_phenix", "name": "Forge du Phénix", "followers_required": 250}, {"id": "brasserie_du_donjon", "name": "Brasserie du Donjon", "followers_required": 600}, ] var follower_thresholds: Array = DEFAULT_FOLLOWER_THRESHOLDS.duplicate(true) var sponsor_definitions: Array = DEFAULT_SPONSORS.duplicate(true) var episode_interval_turns := 5 var _state: Dictionary func _init(state: Dictionary = {}) -> void: _state = state _ensure_state() func spectacle_score() -> int: return int(_state["spectacle_score"]) func followers() -> int: return int(_state["followers"]) func unlocked_sponsors() -> Array[StringName]: var sponsors: Array[StringName] = [] for sponsor_id in _state["unlocked_sponsors"]: sponsors.append(StringName(sponsor_id)) return sponsors func add_spectacle(amount: int) -> Dictionary: var gained := maxi(0, amount) var before := spectacle_score() _state["spectacle_score"] = before + gained var followers_gained := 0 for threshold: Dictionary in follower_thresholds: var score := int(threshold.get("spectacle", 0)) if before < score and spectacle_score() >= score: followers_gained += int(threshold.get("followers", 0)) _state["followers"] = followers() + followers_gained var unlocked: Array[StringName] = [] for definition: Dictionary in sponsor_definitions: var sponsor_id := StringName(definition.get("id", "")) if sponsor_id.is_empty() or sponsor_id in _state["unlocked_sponsors"]: continue if followers() >= int(definition.get("followers_required", 0)): _state["unlocked_sponsors"].append(sponsor_id) unlocked.append(sponsor_id) return {"spectacle_gained": gained, "followers_gained": followers_gained, "unlocked_sponsors": unlocked} func sponsor_definition(sponsor_id: StringName) -> Dictionary: for definition: Dictionary in sponsor_definitions: if StringName(definition.get("id", "")) == sponsor_id: return definition.duplicate(true) return {} func consume_episode_due(turn: int) -> bool: if episode_interval_turns <= 0 or turn <= 0 or turn % episode_interval_turns != 0: return false if int(_state["last_episode_turn"]) >= turn: return false _state["last_episode_turn"] = turn _state["episode_count"] = episode_count() + 1 return true func episode_count() -> int: return int(_state["episode_count"]) func _ensure_state() -> void: if not _state.has("spectacle_score"): _state["spectacle_score"] = 0 if not _state.has("followers"): _state["followers"] = 0 if not _state.has("unlocked_sponsors"): _state["unlocked_sponsors"] = [] if not _state.has("last_episode_turn"): _state["last_episode_turn"] = 0 if not _state.has("episode_count"): _state["episode_count"] = 0