130 lines
2.9 KiB
GDScript
130 lines
2.9 KiB
GDScript
class_name DiplomaticRelationRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
|
|
var data: Dictionary
|
|
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
|
|
static func create(
|
|
relation_id: StringName,
|
|
first_polity_id: StringName,
|
|
second_polity_id: StringName,
|
|
stance_id: StringName,
|
|
trust: float,
|
|
started_tick: int,
|
|
history_event_ids: Array = []
|
|
) -> DiplomaticRelationRecord:
|
|
var first := String(first_polity_id)
|
|
var second := String(second_polity_id)
|
|
if second < first:
|
|
var swapped := first
|
|
first = second
|
|
second = swapped
|
|
var normalized_history: Variant = RegionalStateSupport.normalize_history_event_ids(
|
|
history_event_ids
|
|
)
|
|
if normalized_history == null:
|
|
return null
|
|
var record := (
|
|
DiplomaticRelationRecord
|
|
. new(
|
|
{
|
|
"schema_version": SCHEMA_VERSION,
|
|
"relation_id": String(relation_id),
|
|
"first_polity_id": first,
|
|
"second_polity_id": second,
|
|
"stance_id": String(stance_id),
|
|
"trust": trust,
|
|
"started_tick": started_tick,
|
|
"history_event_ids": normalized_history,
|
|
}
|
|
)
|
|
)
|
|
return record if record.is_valid() else null
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> DiplomaticRelationRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not (
|
|
record_data
|
|
. has_all(
|
|
[
|
|
"relation_id",
|
|
"first_polity_id",
|
|
"second_polity_id",
|
|
"stance_id",
|
|
"trust",
|
|
"started_tick",
|
|
"history_event_ids",
|
|
]
|
|
)
|
|
):
|
|
return null
|
|
if not record_data["history_event_ids"] is Array:
|
|
return null
|
|
return create(
|
|
StringName(record_data["relation_id"]),
|
|
StringName(record_data["first_polity_id"]),
|
|
StringName(record_data["second_polity_id"]),
|
|
StringName(record_data["stance_id"]),
|
|
float(record_data["trust"]),
|
|
int(record_data["started_tick"]),
|
|
record_data["history_event_ids"]
|
|
)
|
|
|
|
|
|
func is_valid() -> bool:
|
|
return (
|
|
not get_relation_id().is_empty()
|
|
and not get_first_polity_id().is_empty()
|
|
and not get_second_polity_id().is_empty()
|
|
and String(get_first_polity_id()) < String(get_second_polity_id())
|
|
and not get_stance_id().is_empty()
|
|
and is_finite(get_trust())
|
|
and get_trust() >= -1.0
|
|
and get_trust() <= 1.0
|
|
and get_started_tick() >= 0
|
|
)
|
|
|
|
|
|
func get_relation_id() -> StringName:
|
|
return StringName(data["relation_id"])
|
|
|
|
|
|
func get_first_polity_id() -> StringName:
|
|
return StringName(data["first_polity_id"])
|
|
|
|
|
|
func get_second_polity_id() -> StringName:
|
|
return StringName(data["second_polity_id"])
|
|
|
|
|
|
func get_stance_id() -> StringName:
|
|
return StringName(data["stance_id"])
|
|
|
|
|
|
func get_trust() -> float:
|
|
return float(data["trust"])
|
|
|
|
|
|
func get_started_tick() -> int:
|
|
return int(data["started_tick"])
|
|
|
|
|
|
func get_history_event_ids() -> Array[int]:
|
|
return RegionalStateSupport.copy_int_array(data["history_event_ids"])
|
|
|
|
|
|
func get_pair_key() -> String:
|
|
return RegionalStateSupport.pair_key(get_first_polity_id(), get_second_polity_id())
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|