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

111 lines
2.6 KiB
GDScript

class_name LocationStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
location_id: StringName,
world_id: StringName,
location_type: StringName,
display_name: String,
address: SpatialAddress,
history_event_ids: Array = []
) -> LocationStateRecord:
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
var normalized_address: Variant = RegionalStateSupport.normalize_address(address)
if normalized_history == null or normalized_address == null:
return null
var record := (
LocationStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"location_id": String(location_id),
"world_id": String(world_id),
"location_type": String(location_type),
"display_name": display_name,
"address": normalized_address,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> LocationStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"location_id",
"world_id",
"location_type",
"display_name",
"address",
"history_event_ids",
]
)
):
return null
if not record_data["address"] is Dictionary or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["location_id"]),
StringName(record_data["world_id"]),
StringName(record_data["location_type"]),
String(record_data["display_name"]),
SpatialAddress.from_dictionary(record_data["address"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_location_id().is_empty()
and not get_world_id().is_empty()
and not get_location_type().is_empty()
and not get_display_name().is_empty()
and RegionalStateSupport.address_matches(
data.get("address"), get_world_id(), get_location_id()
)
)
func get_location_id() -> StringName:
return StringName(data["location_id"])
func get_world_id() -> StringName:
return StringName(data["world_id"])
func get_location_type() -> StringName:
return StringName(data["location_type"])
func get_display_name() -> String:
return String(data["display_name"])
func get_address() -> SpatialAddress:
return RegionalStateSupport.address_from_data(data["address"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func to_dictionary() -> Dictionary:
return data.duplicate(true)