168 lines
4.4 KiB
GDScript
168 lines
4.4 KiB
GDScript
class_name OpportunityStateRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
const NO_EVENT_ID := -1
|
|
const DEFAULT_TARGET_AMOUNT := 1.0
|
|
const STATUS_OPEN := SimulationIds.OPPORTUNITY_STATUS_OPEN
|
|
const STATUS_RESOLVED := SimulationIds.OPPORTUNITY_STATUS_RESOLVED
|
|
|
|
var data: Dictionary
|
|
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
|
|
static func create(
|
|
opportunity_id: int,
|
|
created_tick: int,
|
|
trigger_event_id: int,
|
|
interested_npc_id: int,
|
|
target_amount: float = DEFAULT_TARGET_AMOUNT
|
|
) -> OpportunityStateRecord:
|
|
return (
|
|
OpportunityStateRecord
|
|
. new(
|
|
{
|
|
"schema_version": SCHEMA_VERSION,
|
|
"opportunity_id": opportunity_id,
|
|
"opportunity_type": String(SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY),
|
|
"status": String(STATUS_OPEN),
|
|
"created_tick": created_tick,
|
|
"trigger_event_id": trigger_event_id,
|
|
"interested_npc_id": interested_npc_id,
|
|
"target_id": String(SimulationIds.STORAGE_VILLAGE_PANTRY),
|
|
"resource_id": String(SimulationIds.RESOURCE_FOOD),
|
|
"target_amount": target_amount,
|
|
"resolution_event_id": NO_EVENT_ID,
|
|
"resolved_tick": -1,
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> OpportunityStateRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not (
|
|
record_data
|
|
. has_all(
|
|
[
|
|
"opportunity_id",
|
|
"opportunity_type",
|
|
"status",
|
|
"created_tick",
|
|
"trigger_event_id",
|
|
"interested_npc_id",
|
|
"target_id",
|
|
"resource_id",
|
|
"target_amount",
|
|
"resolution_event_id",
|
|
"resolved_tick",
|
|
]
|
|
)
|
|
):
|
|
return null
|
|
var opportunity_id := int(record_data["opportunity_id"])
|
|
var opportunity_type := StringName(record_data["opportunity_type"])
|
|
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 interested_npc_id := int(record_data["interested_npc_id"])
|
|
var target_id := StringName(record_data["target_id"])
|
|
var resource_id := StringName(record_data["resource_id"])
|
|
var target_amount := float(record_data["target_amount"])
|
|
var resolution_event_id := int(record_data["resolution_event_id"])
|
|
var resolved_tick := int(record_data["resolved_tick"])
|
|
if opportunity_id < 0 or created_tick < 0 or trigger_event_id < 0 or interested_npc_id < 0:
|
|
return null
|
|
if opportunity_type != SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY:
|
|
return null
|
|
if target_id != SimulationIds.STORAGE_VILLAGE_PANTRY:
|
|
return null
|
|
if resource_id != SimulationIds.RESOURCE_FOOD:
|
|
return null
|
|
if not is_finite(target_amount) or not is_equal_approx(target_amount, DEFAULT_TARGET_AMOUNT):
|
|
return null
|
|
match status:
|
|
STATUS_OPEN:
|
|
if resolution_event_id != NO_EVENT_ID or resolved_tick != -1:
|
|
return null
|
|
STATUS_RESOLVED:
|
|
if resolution_event_id <= trigger_event_id or resolved_tick < created_tick:
|
|
return null
|
|
_:
|
|
return null
|
|
var record := create(
|
|
opportunity_id, created_tick, trigger_event_id, interested_npc_id, target_amount
|
|
)
|
|
if status == STATUS_RESOLVED:
|
|
record.resolve(resolution_event_id, resolved_tick)
|
|
return record
|
|
|
|
|
|
func resolve(resolution_event_id: int, resolved_tick: int) -> bool:
|
|
if (
|
|
not is_open()
|
|
or resolution_event_id <= get_trigger_event_id()
|
|
or resolved_tick < get_created_tick()
|
|
):
|
|
return false
|
|
data["status"] = String(STATUS_RESOLVED)
|
|
data["resolution_event_id"] = resolution_event_id
|
|
data["resolved_tick"] = resolved_tick
|
|
return true
|
|
|
|
|
|
func get_opportunity_id() -> int:
|
|
return int(data["opportunity_id"])
|
|
|
|
|
|
func get_opportunity_type() -> StringName:
|
|
return StringName(data["opportunity_type"])
|
|
|
|
|
|
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_interested_npc_id() -> int:
|
|
return int(data["interested_npc_id"])
|
|
|
|
|
|
func get_target_id() -> StringName:
|
|
return StringName(data["target_id"])
|
|
|
|
|
|
func get_resource_id() -> StringName:
|
|
return StringName(data["resource_id"])
|
|
|
|
|
|
func get_target_amount() -> float:
|
|
return float(data["target_amount"])
|
|
|
|
|
|
func get_resolution_event_id() -> int:
|
|
return int(data["resolution_event_id"])
|
|
|
|
|
|
func get_resolved_tick() -> int:
|
|
return int(data["resolved_tick"])
|
|
|
|
|
|
func is_open() -> bool:
|
|
return get_status() == STATUS_OPEN
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|