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

118 lines
2.7 KiB
GDScript

class_name RouteStateRecord
extends RefCounted
const SCHEMA_VERSION := 1
var data: Dictionary
func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true)
static func create(
route_id: StringName,
from_location_id: StringName,
to_location_id: StringName,
travel_ticks: int,
bidirectional: bool = true,
history_event_ids: Array = []
) -> RouteStateRecord:
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
history_event_ids
)
if normalized_history == null:
return null
var record := (
RouteStateRecord
. new(
{
"schema_version": SCHEMA_VERSION,
"route_id": String(route_id),
"from_location_id": String(from_location_id),
"to_location_id": String(to_location_id),
"travel_ticks": travel_ticks,
"bidirectional": bidirectional,
"history_event_ids": normalized_history,
}
)
)
return record if record.is_valid() else null
static func from_dictionary(record_data: Dictionary) -> RouteStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null
if not (
record_data
. has_all(
[
"route_id",
"from_location_id",
"to_location_id",
"travel_ticks",
"bidirectional",
"history_event_ids",
]
)
):
return null
if not record_data["bidirectional"] is bool or not record_data["history_event_ids"] is Array:
return null
return create(
StringName(record_data["route_id"]),
StringName(record_data["from_location_id"]),
StringName(record_data["to_location_id"]),
int(record_data["travel_ticks"]),
bool(record_data["bidirectional"]),
record_data["history_event_ids"]
)
func is_valid() -> bool:
return (
not get_route_id().is_empty()
and not get_from_location_id().is_empty()
and not get_to_location_id().is_empty()
and get_from_location_id() != get_to_location_id()
and get_travel_ticks() > 0
)
func get_route_id() -> StringName:
return StringName(data["route_id"])
func get_from_location_id() -> StringName:
return StringName(data["from_location_id"])
func get_to_location_id() -> StringName:
return StringName(data["to_location_id"])
func get_travel_ticks() -> int:
return int(data["travel_ticks"])
func is_bidirectional() -> bool:
return bool(data["bidirectional"])
func get_history_event_ids() -> Array[int]:
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
func connects(first_location_id: StringName, second_location_id: StringName) -> bool:
if first_location_id == get_from_location_id() and second_location_id == get_to_location_id():
return true
return (
is_bidirectional()
and first_location_id == get_to_location_id()
and second_location_id == get_from_location_id()
)
func to_dictionary() -> Dictionary:
return data.duplicate(true)