class_name SettlementStateRecord extends RefCounted const SCHEMA_VERSION := 1 var data: Dictionary func _init(record_data: Dictionary = {}) -> void: data = record_data.duplicate(true) static func create( settlement_id: StringName, location_id: StringName, display_name: String, polity_id: StringName, tier: int, founding_group_id: StringName, founded_tick: int, stockpile_ledger: Dictionary = {}, history_event_ids: Array = [] ) -> SettlementStateRecord: var normalized_stockpile: Variant = RegionalStateSupport.normalize_amount_ledger( stockpile_ledger ) var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids( history_event_ids ) if normalized_stockpile == null or normalized_history == null: return null var record := ( SettlementStateRecord . new( { "schema_version": SCHEMA_VERSION, "settlement_id": String(settlement_id), "location_id": String(location_id), "display_name": display_name, "polity_id": String(polity_id), "tier": tier, "founding_group_id": String(founding_group_id), "founded_tick": founded_tick, "stockpile_ledger": normalized_stockpile, "history_event_ids": normalized_history, } ) ) return record if record.is_valid() else null static func from_dictionary(record_data: Dictionary) -> SettlementStateRecord: if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION: return null if not ( record_data . has_all( [ "settlement_id", "location_id", "display_name", "polity_id", "tier", "founding_group_id", "founded_tick", "stockpile_ledger", "history_event_ids", ] ) ): return null if ( not record_data["stockpile_ledger"] is Dictionary or not record_data["history_event_ids"] is Array ): return null return create( StringName(record_data["settlement_id"]), StringName(record_data["location_id"]), String(record_data["display_name"]), StringName(record_data["polity_id"]), int(record_data["tier"]), StringName(record_data["founding_group_id"]), int(record_data["founded_tick"]), record_data["stockpile_ledger"], record_data["history_event_ids"] ) func is_valid() -> bool: return ( not get_settlement_id().is_empty() and not get_location_id().is_empty() and not get_display_name().is_empty() and not get_polity_id().is_empty() and not get_founding_group_id().is_empty() and get_tier() >= 0 and get_founded_tick() >= 0 ) func get_settlement_id() -> StringName: return StringName(data["settlement_id"]) func get_location_id() -> StringName: return StringName(data["location_id"]) func get_display_name() -> String: return String(data["display_name"]) func get_polity_id() -> StringName: return StringName(data["polity_id"]) func get_tier() -> int: return int(data["tier"]) func get_founding_group_id() -> StringName: return StringName(data["founding_group_id"]) func get_founded_tick() -> int: return int(data["founded_tick"]) func get_stockpile_ledger() -> Dictionary: return data["stockpile_ledger"].duplicate(true) func get_stockpile_amount(item_id: StringName) -> float: return float(data["stockpile_ledger"].get(String(item_id), 0.0)) func get_total_stockpile_amount() -> float: return RegionalStateSupport.amount_ledger_total(data["stockpile_ledger"]) func get_history_event_ids() -> Array[int]: return RegionalStateSupport.copy_int_array(data["history_event_ids"]) func with_stockpile_ledger( stockpile_ledger: Dictionary, cause_event_id: int = -1 ) -> SettlementStateRecord: var history_event_ids: Array = data["history_event_ids"].duplicate() if cause_event_id >= 0 and cause_event_id not in history_event_ids: history_event_ids.append(cause_event_id) return create( get_settlement_id(), get_location_id(), get_display_name(), get_polity_id(), get_tier(), get_founding_group_id(), get_founded_tick(), stockpile_ledger, history_event_ids ) func to_dictionary() -> Dictionary: return data.duplicate(true)