Files
2026-08-12 21:22:04 +02:00

99 lines
2.3 KiB
GDScript

class_name PolityStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
polity_id: StringName,
display_name: String,
capital_location_id: StringName,
settlement_ids: Array = [],
history_event_ids: Array = []
) -> PolityStateRecord:
var normalized_settlements: Variant = RegionalStateSupport.normalize_id_array(settlement_ids)
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_settlements == null or normalized_history == null:
return null
var record := (
PolityStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"polity_id": String(polity_id),
"display_name": display_name,
"capital_location_id": String(capital_location_id),
"settlement_ids": normalized_settlements,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> PolityStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"polity_id",
"display_name",
"capital_location_id",
"settlement_ids",
"history_event_ids",
]
)
):
return null
if not record_data["settlement_ids"] is Array or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["polity_id"]),
String(record_data["display_name"]),
StringName(record_data["capital_location_id"]),
record_data["settlement_ids"],
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_polity_id().is_empty()
and not get_display_name().is_empty()
and not get_capital_location_id().is_empty()
)
func get_polity_id() -> StringName:
return StringName(data["polity_id"])
func get_display_name() -> String:
return String(data["display_name"])
func get_capital_location_id() -> StringName:
return StringName(data["capital_location_id"])
func get_settlement_ids() -> Array[StringName]:
return RegionalStateSupport.copy_string_array(data["settlement_ids"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)