148 lines
3.3 KiB
GDScript
148 lines
3.3 KiB
GDScript
class_name PopulationCohortRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
|
|
var data: Dictionary
|
|
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
|
|
static func create_at_location(
|
|
cohort_id: StringName,
|
|
cohort_type: StringName,
|
|
polity_id: StringName,
|
|
count: int,
|
|
location_id: StringName,
|
|
history_event_ids: Array = []
|
|
) -> PopulationCohortRecord:
|
|
return _create(cohort_id, cohort_type, polity_id, count, location_id, &"", history_event_ids)
|
|
|
|
|
|
static func create_in_group(
|
|
cohort_id: StringName,
|
|
cohort_type: StringName,
|
|
polity_id: StringName,
|
|
count: int,
|
|
mobile_group_id: StringName,
|
|
history_event_ids: Array = []
|
|
) -> PopulationCohortRecord:
|
|
return _create(
|
|
cohort_id, cohort_type, polity_id, count, &"", mobile_group_id, history_event_ids
|
|
)
|
|
|
|
|
|
static func _create(
|
|
cohort_id: StringName,
|
|
cohort_type: StringName,
|
|
polity_id: StringName,
|
|
count: int,
|
|
location_id: StringName,
|
|
mobile_group_id: StringName,
|
|
history_event_ids: Array
|
|
) -> PopulationCohortRecord:
|
|
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
|
|
history_event_ids
|
|
)
|
|
if normalized_history == null:
|
|
return null
|
|
var record := (
|
|
PopulationCohortRecord
|
|
. new(
|
|
{
|
|
"schema_version": SCHEMA_VERSION,
|
|
"cohort_id": String(cohort_id),
|
|
"cohort_type": String(cohort_type),
|
|
"polity_id": String(polity_id),
|
|
"count": count,
|
|
"location_id": String(location_id),
|
|
"mobile_group_id": String(mobile_group_id),
|
|
"history_event_ids": normalized_history,
|
|
}
|
|
)
|
|
)
|
|
return record if record.is_valid() else null
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> PopulationCohortRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not (
|
|
record_data
|
|
. has_all(
|
|
[
|
|
"cohort_id",
|
|
"cohort_type",
|
|
"polity_id",
|
|
"count",
|
|
"location_id",
|
|
"mobile_group_id",
|
|
"history_event_ids",
|
|
]
|
|
)
|
|
):
|
|
return null
|
|
if not record_data["history_event_ids"] is Array:
|
|
return null
|
|
return _create(
|
|
StringName(record_data["cohort_id"]),
|
|
StringName(record_data["cohort_type"]),
|
|
StringName(record_data["polity_id"]),
|
|
int(record_data["count"]),
|
|
StringName(record_data["location_id"]),
|
|
StringName(record_data["mobile_group_id"]),
|
|
record_data["history_event_ids"]
|
|
)
|
|
|
|
|
|
func is_valid() -> bool:
|
|
return (
|
|
not get_cohort_id().is_empty()
|
|
and not get_cohort_type().is_empty()
|
|
and not get_polity_id().is_empty()
|
|
and get_count() > 0
|
|
and (is_at_location() != is_in_mobile_group())
|
|
)
|
|
|
|
|
|
func get_cohort_id() -> StringName:
|
|
return StringName(data["cohort_id"])
|
|
|
|
|
|
func get_cohort_type() -> StringName:
|
|
return StringName(data["cohort_type"])
|
|
|
|
|
|
func get_polity_id() -> StringName:
|
|
return StringName(data["polity_id"])
|
|
|
|
|
|
func get_count() -> int:
|
|
return int(data["count"])
|
|
|
|
|
|
func is_at_location() -> bool:
|
|
return not get_location_id().is_empty() and get_mobile_group_id().is_empty()
|
|
|
|
|
|
func is_in_mobile_group() -> bool:
|
|
return get_location_id().is_empty() and not get_mobile_group_id().is_empty()
|
|
|
|
|
|
func get_location_id() -> StringName:
|
|
return StringName(data["location_id"])
|
|
|
|
|
|
func get_mobile_group_id() -> StringName:
|
|
return StringName(data["mobile_group_id"])
|
|
|
|
|
|
func get_history_event_ids() -> Array[int]:
|
|
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|