Files
TheTree_mock/scripts/core/domain_event.gd

76 lines
2.2 KiB
GDScript

class_name SimulationDomainEvent
extends RefCounted
var event_id: StringName = &""
var event_type: StringName = &""
var absolute_minute := 0
var source_service: StringName = &""
var entity_ids: Array[StringName] = []
var payload: Dictionary = {}
var causation_command_id := 0
func _init(
p_event_id: StringName = &"",
p_event_type: StringName = &"",
p_absolute_minute := 0,
p_source_service: StringName = &"",
p_entity_ids: Array[StringName] = [],
p_payload: Dictionary = {},
p_causation_command_id := 0
) -> void:
event_id = p_event_id
event_type = p_event_type
absolute_minute = p_absolute_minute
source_service = p_source_service
entity_ids = p_entity_ids.duplicate()
payload = p_payload.duplicate(true)
causation_command_id = p_causation_command_id
func is_valid() -> bool:
return not event_id.is_empty() \
and not event_type.is_empty() \
and not source_service.is_empty() \
and absolute_minute >= 0 \
and causation_command_id >= 0
func copy() -> SimulationDomainEvent:
return SimulationDomainEvent.new(
event_id,
event_type,
absolute_minute,
source_service,
entity_ids,
payload,
causation_command_id
)
func to_dictionary() -> Dictionary:
var saved_entity_ids: Array[String] = []
for entity_id in entity_ids:
saved_entity_ids.append(String(entity_id))
return {
"event_id": String(event_id),
"event_type": String(event_type),
"absolute_minute": absolute_minute,
"source_service": String(source_service),
"entity_ids": saved_entity_ids,
"payload": payload.duplicate(true),
"causation_command_id": causation_command_id,
}
static func from_dictionary(data: Dictionary) -> SimulationDomainEvent:
var restored_entity_ids: Array[StringName] = []
var saved_entity_ids: Variant = data.get("entity_ids", [])
if saved_entity_ids is Array:
for entity_id: Variant in saved_entity_ids:
restored_entity_ids.append(StringName(entity_id))
return SimulationDomainEvent.new(
StringName(data.get("event_id", "")),
StringName(data.get("event_type", "")),
int(data.get("absolute_minute", -1)),
StringName(data.get("source_service", "")),
restored_entity_ids,
data.get("payload", {}) as Dictionary,
int(data.get("causation_command_id", 0))
)