203 lines
4.8 KiB
GDScript
203 lines
4.8 KiB
GDScript
class_name CommitmentStateRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
const NO_EVENT_ID := -1
|
|
const STATUS_ACTIVE := &"active"
|
|
const STATUS_FULFILLED := &"fulfilled"
|
|
const STATUS_BROKEN := &"broken"
|
|
const STATUS_RELEASED := &"released"
|
|
const STATUS_SUPERSEDED := &"superseded"
|
|
const VALID_STATUSES := [
|
|
STATUS_ACTIVE, STATUS_FULFILLED, STATUS_BROKEN, STATUS_RELEASED, STATUS_SUPERSEDED
|
|
]
|
|
|
|
var data: Dictionary
|
|
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
|
|
static func create(
|
|
commitment_id: int,
|
|
situation_id: int,
|
|
debtor: WorldEntityRef,
|
|
creditor: WorldEntityRef,
|
|
terms: Dictionary,
|
|
created_tick: int,
|
|
deadline_tick: int = -1
|
|
) -> CommitmentStateRecord:
|
|
if (
|
|
debtor == null
|
|
or creditor == null
|
|
or not debtor.is_valid()
|
|
or not creditor.is_valid()
|
|
or debtor.equals(creditor)
|
|
or commitment_id < 0
|
|
or situation_id < 0
|
|
or terms.is_empty()
|
|
or created_tick < 0
|
|
or (deadline_tick != -1 and deadline_tick < created_tick)
|
|
):
|
|
return null
|
|
return (
|
|
CommitmentStateRecord
|
|
. new(
|
|
{
|
|
"schema_version": SCHEMA_VERSION,
|
|
"commitment_id": commitment_id,
|
|
"situation_id": situation_id,
|
|
"debtor": debtor.to_dictionary(),
|
|
"creditor": creditor.to_dictionary(),
|
|
"terms": terms.duplicate(true),
|
|
"status": String(STATUS_ACTIVE),
|
|
"created_tick": created_tick,
|
|
"deadline_tick": deadline_tick,
|
|
"closed_tick": -1,
|
|
"cause_event_id": NO_EVENT_ID,
|
|
"close_reason": "",
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> CommitmentStateRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not (
|
|
record_data
|
|
. has_all(
|
|
[
|
|
"commitment_id",
|
|
"situation_id",
|
|
"debtor",
|
|
"creditor",
|
|
"terms",
|
|
"status",
|
|
"created_tick",
|
|
"deadline_tick",
|
|
"closed_tick",
|
|
"cause_event_id",
|
|
"close_reason",
|
|
]
|
|
)
|
|
):
|
|
return null
|
|
if (
|
|
not record_data["debtor"] is Dictionary
|
|
or not record_data["creditor"] is Dictionary
|
|
or not record_data["terms"] is Dictionary
|
|
):
|
|
return null
|
|
var debtor := WorldEntityRef.from_dictionary(record_data["debtor"])
|
|
var creditor := WorldEntityRef.from_dictionary(record_data["creditor"])
|
|
var commitment_id := int(record_data["commitment_id"])
|
|
var situation_id := int(record_data["situation_id"])
|
|
var status := StringName(record_data["status"])
|
|
var created_tick := int(record_data["created_tick"])
|
|
var deadline_tick := int(record_data["deadline_tick"])
|
|
var closed_tick := int(record_data["closed_tick"])
|
|
var cause_event_id := int(record_data["cause_event_id"])
|
|
var close_reason := StringName(record_data["close_reason"])
|
|
if (
|
|
debtor == null
|
|
or creditor == null
|
|
or debtor.equals(creditor)
|
|
or commitment_id < 0
|
|
or situation_id < 0
|
|
or record_data["terms"].is_empty()
|
|
or status not in VALID_STATUSES
|
|
or created_tick < 0
|
|
or (deadline_tick != -1 and deadline_tick < created_tick)
|
|
or cause_event_id < NO_EVENT_ID
|
|
):
|
|
return null
|
|
if status == STATUS_ACTIVE:
|
|
if closed_tick != -1 or cause_event_id != NO_EVENT_ID or not close_reason.is_empty():
|
|
return null
|
|
elif closed_tick < created_tick or close_reason.is_empty():
|
|
return null
|
|
var record := create(
|
|
commitment_id,
|
|
situation_id,
|
|
debtor,
|
|
creditor,
|
|
record_data["terms"],
|
|
created_tick,
|
|
deadline_tick
|
|
)
|
|
if status != STATUS_ACTIVE:
|
|
record.close(status, closed_tick, close_reason, cause_event_id)
|
|
return record
|
|
|
|
|
|
func get_commitment_id() -> int:
|
|
return int(data["commitment_id"])
|
|
|
|
|
|
func get_situation_id() -> int:
|
|
return int(data["situation_id"])
|
|
|
|
|
|
func get_debtor() -> WorldEntityRef:
|
|
return WorldEntityRef.from_dictionary(data["debtor"])
|
|
|
|
|
|
func get_creditor() -> WorldEntityRef:
|
|
return WorldEntityRef.from_dictionary(data["creditor"])
|
|
|
|
|
|
func get_terms() -> Dictionary:
|
|
return data["terms"].duplicate(true)
|
|
|
|
|
|
func get_status() -> StringName:
|
|
return StringName(data["status"])
|
|
|
|
|
|
func get_created_tick() -> int:
|
|
return int(data["created_tick"])
|
|
|
|
|
|
func get_deadline_tick() -> int:
|
|
return int(data["deadline_tick"])
|
|
|
|
|
|
func get_closed_tick() -> int:
|
|
return int(data["closed_tick"])
|
|
|
|
|
|
func get_cause_event_id() -> int:
|
|
return int(data["cause_event_id"])
|
|
|
|
|
|
func get_close_reason() -> StringName:
|
|
return StringName(data["close_reason"])
|
|
|
|
|
|
func is_active() -> bool:
|
|
return get_status() == STATUS_ACTIVE
|
|
|
|
|
|
func close(
|
|
status: StringName, closed_tick: int, reason: StringName, cause_event_id: int = NO_EVENT_ID
|
|
) -> bool:
|
|
if (
|
|
not is_active()
|
|
or status not in [STATUS_FULFILLED, STATUS_BROKEN, STATUS_RELEASED, STATUS_SUPERSEDED]
|
|
or closed_tick < get_created_tick()
|
|
or reason.is_empty()
|
|
or cause_event_id < NO_EVENT_ID
|
|
):
|
|
return false
|
|
data["status"] = String(status)
|
|
data["closed_tick"] = closed_tick
|
|
data["cause_event_id"] = cause_event_id
|
|
data["close_reason"] = String(reason)
|
|
return true
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|