Files
TheTree_mock/scripts/events/event_action.gd
Takeshi 5dde0c5ec2 Record original-alignment implementation and traceability checkpoint
Capture current source, content data, tests, asset licenses, and original-specification traceability. Original implementation remains incomplete. The accepted matching UI generation passed 154 suites and 12,253 checks, with eight rendered staff-contract screens reviewed. Latest native Windows and full-original acceptance remain outstanding.
2026-09-30 23:52:24 +09:00

82 lines
2.3 KiB
GDScript

class_name EventAction
extends RefCounted
enum Type {
SET_FLAG,
ADD_RELATIONSHIP_VALUE,
DAMAGE_SHIELD,
ADD_LEDGER_ENTRY,
ADD_OR_REMOVE_ITEM,
CREATE_TASK,
CREATE_INCIDENT,
SET_FACILITY_STATE,
TELEPORT_ENTITY_FOR_RECOVERY,
SCHEDULE_EVENT,
SET_ROUTE_LOCK,
CREATE_DEFENSE_RAID,
}
var action_id: StringName = &""
var type: Type = Type.SET_FLAG
var payload: Dictionary = {}
func configure(
p_action_id: StringName,
p_type: Variant,
p_payload: Dictionary = {}
) -> EventAction:
action_id = p_action_id
type = _type_from_variant(p_type)
payload = p_payload.duplicate(true)
return self
func is_valid() -> bool:
return not action_id.is_empty() and type >= Type.SET_FLAG and type <= Type.CREATE_DEFENSE_RAID
func create_snapshot() -> Dictionary:
return {
&"action_id": String(action_id),
&"type": Type.keys()[type],
&"payload": payload.duplicate(true),
}
static func from_variant(raw_value: Variant) -> EventAction:
if raw_value is EventAction:
return raw_value
if not raw_value is Dictionary:
return null
# Serialized action enums are authoritative. Keep an invalid object in the
# action list so EventDefinition/EventChoice reject it instead of silently
# dropping it, clamping it to a destructive action, or choosing SET_FLAG.
var raw_type: Variant = _value(raw_value, &"type", "SET_FLAG")
if raw_type is int:
if int(raw_type) < Type.SET_FLAG or int(raw_type) > Type.CREATE_DEFENSE_RAID:
return EventAction.new()
elif typeof(raw_type) in [TYPE_STRING, TYPE_STRING_NAME]:
if Type.keys().find(String(raw_type).to_upper()) < 0:
return EventAction.new()
else:
return EventAction.new()
return EventAction.new().configure(
StringName(_value(raw_value, &"action_id", &"")),
_value(raw_value, &"type", "SET_FLAG"),
_value(raw_value, &"payload", {}) if _value(raw_value, &"payload", {}) is Dictionary else {}
)
static func _type_from_variant(raw_value: Variant) -> Type:
if raw_value is int:
return clampi(int(raw_value), Type.SET_FLAG, Type.CREATE_DEFENSE_RAID) as Type
var index := Type.keys().find(String(raw_value).to_upper())
return (index if index >= 0 else Type.SET_FLAG) as Type
static func _value(source: Dictionary, key: StringName, fallback: Variant) -> Variant:
if source.has(key):
return source[key]
return source.get(String(key), fallback)