feat: generate world-backed situations and commitments
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://fthjvjr6x2ia
|
||||
@@ -0,0 +1,174 @@
|
||||
class_name QuestJournalEntryStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
const STATUS_ACTIVE := &"active"
|
||||
const STATUS_COMPLETED := &"completed"
|
||||
const STATUS_FAILED := &"failed"
|
||||
const STATUS_ARCHIVED := &"archived"
|
||||
const VALID_STATUSES := [STATUS_ACTIVE, STATUS_COMPLETED, STATUS_FAILED, STATUS_ARCHIVED]
|
||||
const TRACKING_AUTO := &"auto"
|
||||
const TRACKING_TRACKED := &"tracked"
|
||||
const TRACKING_UNTRACKED := &"untracked"
|
||||
const VALID_TRACKING_PREFERENCES := [TRACKING_AUTO, TRACKING_TRACKED, TRACKING_UNTRACKED]
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
|
||||
static func create(
|
||||
entry_id: int,
|
||||
situation_id: int,
|
||||
definition_id: StringName,
|
||||
discovered_tick: int,
|
||||
selected_alternative_id: StringName = &"",
|
||||
tracking_preference: StringName = TRACKING_AUTO
|
||||
) -> QuestJournalEntryStateRecord:
|
||||
if (
|
||||
entry_id < 0
|
||||
or situation_id < 0
|
||||
or definition_id.is_empty()
|
||||
or discovered_tick < 0
|
||||
or tracking_preference not in VALID_TRACKING_PREFERENCES
|
||||
):
|
||||
return null
|
||||
return (
|
||||
QuestJournalEntryStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"entry_id": entry_id,
|
||||
"situation_id": situation_id,
|
||||
"definition_id": String(definition_id),
|
||||
"status": String(STATUS_ACTIVE),
|
||||
"discovered_tick": discovered_tick,
|
||||
"selected_alternative_id": String(selected_alternative_id),
|
||||
"tracking_preference": String(tracking_preference),
|
||||
"closed_tick": -1,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> QuestJournalEntryStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not (
|
||||
record_data
|
||||
. has_all(
|
||||
[
|
||||
"entry_id",
|
||||
"situation_id",
|
||||
"definition_id",
|
||||
"status",
|
||||
"discovered_tick",
|
||||
"selected_alternative_id",
|
||||
"tracking_preference",
|
||||
"closed_tick",
|
||||
]
|
||||
)
|
||||
):
|
||||
return null
|
||||
var entry_id := int(record_data["entry_id"])
|
||||
var situation_id := int(record_data["situation_id"])
|
||||
var definition_id := StringName(record_data["definition_id"])
|
||||
var status := StringName(record_data["status"])
|
||||
var discovered_tick := int(record_data["discovered_tick"])
|
||||
var selected_alternative_id := StringName(record_data["selected_alternative_id"])
|
||||
var tracking_preference := StringName(record_data["tracking_preference"])
|
||||
var closed_tick := int(record_data["closed_tick"])
|
||||
if (
|
||||
entry_id < 0
|
||||
or situation_id < 0
|
||||
or definition_id.is_empty()
|
||||
or status not in VALID_STATUSES
|
||||
or discovered_tick < 0
|
||||
or tracking_preference not in VALID_TRACKING_PREFERENCES
|
||||
):
|
||||
return null
|
||||
if (
|
||||
(status == STATUS_ACTIVE and closed_tick != -1)
|
||||
or (status != STATUS_ACTIVE and closed_tick < discovered_tick)
|
||||
):
|
||||
return null
|
||||
var record := create(
|
||||
entry_id,
|
||||
situation_id,
|
||||
definition_id,
|
||||
discovered_tick,
|
||||
selected_alternative_id,
|
||||
tracking_preference
|
||||
)
|
||||
if status != STATUS_ACTIVE:
|
||||
record.close(status, closed_tick)
|
||||
return record
|
||||
|
||||
|
||||
func get_entry_id() -> int:
|
||||
return int(data["entry_id"])
|
||||
|
||||
|
||||
func get_situation_id() -> int:
|
||||
return int(data["situation_id"])
|
||||
|
||||
|
||||
func get_definition_id() -> StringName:
|
||||
return StringName(data["definition_id"])
|
||||
|
||||
|
||||
func get_status() -> StringName:
|
||||
return StringName(data["status"])
|
||||
|
||||
|
||||
func get_discovered_tick() -> int:
|
||||
return int(data["discovered_tick"])
|
||||
|
||||
|
||||
func get_selected_alternative_id() -> StringName:
|
||||
return StringName(data["selected_alternative_id"])
|
||||
|
||||
|
||||
func get_tracking_preference() -> StringName:
|
||||
return StringName(data["tracking_preference"])
|
||||
|
||||
|
||||
func get_closed_tick() -> int:
|
||||
return int(data["closed_tick"])
|
||||
|
||||
|
||||
func is_active() -> bool:
|
||||
return get_status() == STATUS_ACTIVE
|
||||
|
||||
|
||||
func is_archived() -> bool:
|
||||
return get_status() == STATUS_ARCHIVED
|
||||
|
||||
|
||||
func set_tracking_preference(preference: StringName) -> bool:
|
||||
if preference not in VALID_TRACKING_PREFERENCES:
|
||||
return false
|
||||
data["tracking_preference"] = String(preference)
|
||||
return true
|
||||
|
||||
|
||||
func archive(closed_tick: int) -> bool:
|
||||
return close(STATUS_ARCHIVED, closed_tick)
|
||||
|
||||
|
||||
func close(status: StringName, closed_tick: int) -> bool:
|
||||
if (
|
||||
not is_active()
|
||||
or status not in [STATUS_COMPLETED, STATUS_FAILED, STATUS_ARCHIVED]
|
||||
or closed_tick < get_discovered_tick()
|
||||
):
|
||||
return false
|
||||
data["status"] = String(status)
|
||||
data["closed_tick"] = closed_tick
|
||||
return true
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c5i631jie8rtt
|
||||
@@ -0,0 +1,233 @@
|
||||
class_name SituationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
const NO_EVENT_ID := -1
|
||||
const STATUS_ACTIVE := &"active"
|
||||
const STATUS_RESOLVED := &"resolved"
|
||||
const STATUS_EXPIRED := &"expired"
|
||||
const STATUS_SUPERSEDED := &"superseded"
|
||||
const VALID_STATUSES := [STATUS_ACTIVE, STATUS_RESOLVED, STATUS_EXPIRED, STATUS_SUPERSEDED]
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
|
||||
static func create(
|
||||
situation_id: int,
|
||||
definition_id: StringName,
|
||||
dedupe_key: String,
|
||||
created_tick: int,
|
||||
trigger_event_id: int = NO_EVENT_ID,
|
||||
context: Dictionary = {}
|
||||
) -> SituationStateRecord:
|
||||
if situation_id < 0 or definition_id.is_empty() or dedupe_key.is_empty() or created_tick < 0:
|
||||
return null
|
||||
if trigger_event_id < NO_EVENT_ID:
|
||||
return null
|
||||
return (
|
||||
SituationStateRecord
|
||||
. new(
|
||||
{
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"situation_id": situation_id,
|
||||
"definition_id": String(definition_id),
|
||||
"dedupe_key": dedupe_key,
|
||||
"status": String(STATUS_ACTIVE),
|
||||
"created_tick": created_tick,
|
||||
"trigger_event_id": trigger_event_id,
|
||||
"selected_alternative_id": "",
|
||||
"resolution_event_id": NO_EVENT_ID,
|
||||
"closed_tick": -1,
|
||||
"close_reason": "",
|
||||
"context": context.duplicate(true),
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> SituationStateRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not (
|
||||
record_data
|
||||
. has_all(
|
||||
[
|
||||
"situation_id",
|
||||
"definition_id",
|
||||
"dedupe_key",
|
||||
"status",
|
||||
"created_tick",
|
||||
"trigger_event_id",
|
||||
"selected_alternative_id",
|
||||
"resolution_event_id",
|
||||
"closed_tick",
|
||||
"close_reason",
|
||||
"context",
|
||||
]
|
||||
)
|
||||
):
|
||||
return null
|
||||
var situation_id := int(record_data["situation_id"])
|
||||
var definition_id := StringName(record_data["definition_id"])
|
||||
var dedupe_key := String(record_data["dedupe_key"])
|
||||
var status := StringName(record_data["status"])
|
||||
var created_tick := int(record_data["created_tick"])
|
||||
var trigger_event_id := int(record_data["trigger_event_id"])
|
||||
var selected_alternative_id := StringName(record_data["selected_alternative_id"])
|
||||
var resolution_event_id := int(record_data["resolution_event_id"])
|
||||
var closed_tick := int(record_data["closed_tick"])
|
||||
var close_reason := StringName(record_data["close_reason"])
|
||||
if (
|
||||
situation_id < 0
|
||||
or definition_id.is_empty()
|
||||
or dedupe_key.is_empty()
|
||||
or status not in VALID_STATUSES
|
||||
or created_tick < 0
|
||||
or trigger_event_id < NO_EVENT_ID
|
||||
or not record_data["context"] is Dictionary
|
||||
):
|
||||
return null
|
||||
if status == STATUS_ACTIVE:
|
||||
if resolution_event_id != NO_EVENT_ID or closed_tick != -1 or not close_reason.is_empty():
|
||||
return null
|
||||
else:
|
||||
if closed_tick < created_tick or close_reason.is_empty():
|
||||
return null
|
||||
if status != STATUS_RESOLVED and resolution_event_id != NO_EVENT_ID:
|
||||
return null
|
||||
var record := create(
|
||||
situation_id,
|
||||
definition_id,
|
||||
dedupe_key,
|
||||
created_tick,
|
||||
trigger_event_id,
|
||||
record_data["context"]
|
||||
)
|
||||
if not selected_alternative_id.is_empty():
|
||||
record.select_alternative(selected_alternative_id)
|
||||
if status != STATUS_ACTIVE:
|
||||
record.close(status, closed_tick, close_reason, resolution_event_id)
|
||||
return record
|
||||
|
||||
|
||||
static func from_opportunity(
|
||||
opportunity: OpportunityStateRecord, mapped_definition_id: StringName = &""
|
||||
) -> SituationStateRecord:
|
||||
if opportunity == null:
|
||||
return null
|
||||
var context := {
|
||||
"opportunity_id": opportunity.get_opportunity_id(),
|
||||
"interested_npc_id": opportunity.get_interested_npc_id(),
|
||||
"target_id": String(opportunity.get_target_id()),
|
||||
"resource_id": String(opportunity.get_resource_id()),
|
||||
"target_amount": opportunity.get_target_amount(),
|
||||
}
|
||||
var dedupe_key := "opportunity:%d" % opportunity.get_opportunity_id()
|
||||
var definition_id := mapped_definition_id
|
||||
if definition_id.is_empty():
|
||||
definition_id = opportunity.get_opportunity_type()
|
||||
var record := create(
|
||||
opportunity.get_opportunity_id(),
|
||||
definition_id,
|
||||
dedupe_key,
|
||||
opportunity.get_created_tick(),
|
||||
opportunity.get_trigger_event_id(),
|
||||
context
|
||||
)
|
||||
if opportunity.get_status() == OpportunityStateRecord.STATUS_RESOLVED:
|
||||
record.close(
|
||||
STATUS_RESOLVED,
|
||||
opportunity.get_closed_tick(),
|
||||
&"opportunity_resolved",
|
||||
opportunity.get_resolution_event_id()
|
||||
)
|
||||
elif opportunity.get_status() == OpportunityStateRecord.STATUS_INVALIDATED:
|
||||
record.close(
|
||||
STATUS_EXPIRED, opportunity.get_closed_tick(), opportunity.get_invalidation_reason()
|
||||
)
|
||||
return record
|
||||
|
||||
|
||||
func get_situation_id() -> int:
|
||||
return int(data["situation_id"])
|
||||
|
||||
|
||||
func get_definition_id() -> StringName:
|
||||
return StringName(data["definition_id"])
|
||||
|
||||
|
||||
func get_dedupe_key() -> String:
|
||||
return String(data["dedupe_key"])
|
||||
|
||||
|
||||
func get_status() -> StringName:
|
||||
return StringName(data["status"])
|
||||
|
||||
|
||||
func get_created_tick() -> int:
|
||||
return int(data["created_tick"])
|
||||
|
||||
|
||||
func get_trigger_event_id() -> int:
|
||||
return int(data["trigger_event_id"])
|
||||
|
||||
|
||||
func get_selected_alternative_id() -> StringName:
|
||||
return StringName(data["selected_alternative_id"])
|
||||
|
||||
|
||||
func get_resolution_event_id() -> int:
|
||||
return int(data["resolution_event_id"])
|
||||
|
||||
|
||||
func get_closed_tick() -> int:
|
||||
return int(data["closed_tick"])
|
||||
|
||||
|
||||
func get_close_reason() -> StringName:
|
||||
return StringName(data["close_reason"])
|
||||
|
||||
|
||||
func get_context() -> Dictionary:
|
||||
return data["context"].duplicate(true)
|
||||
|
||||
|
||||
func is_active() -> bool:
|
||||
return get_status() == STATUS_ACTIVE
|
||||
|
||||
|
||||
func select_alternative(alternative_id: StringName) -> bool:
|
||||
if not is_active() or alternative_id.is_empty():
|
||||
return false
|
||||
var selected := get_selected_alternative_id()
|
||||
if not selected.is_empty() and selected != alternative_id:
|
||||
return false
|
||||
data["selected_alternative_id"] = String(alternative_id)
|
||||
return true
|
||||
|
||||
|
||||
func close(
|
||||
status: StringName, closed_tick: int, reason: StringName, resolution_event_id: int = NO_EVENT_ID
|
||||
) -> bool:
|
||||
if (
|
||||
not is_active()
|
||||
or status not in [STATUS_RESOLVED, STATUS_EXPIRED, STATUS_SUPERSEDED]
|
||||
or closed_tick < get_created_tick()
|
||||
or reason.is_empty()
|
||||
):
|
||||
return false
|
||||
if status != STATUS_RESOLVED and resolution_event_id != NO_EVENT_ID:
|
||||
return false
|
||||
data["status"] = String(status)
|
||||
data["resolution_event_id"] = resolution_event_id
|
||||
data["closed_tick"] = closed_tick
|
||||
data["close_reason"] = String(reason)
|
||||
return true
|
||||
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
@@ -0,0 +1 @@
|
||||
uid://c3x0msm0gmlpk
|
||||
Reference in New Issue
Block a user