feat: add structured economic events
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
class_name EconomicEventRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
|
||||
var data: Dictionary
|
||||
|
||||
func _init(record_data: Dictionary = {}) -> void:
|
||||
data = record_data.duplicate(true)
|
||||
|
||||
static func create(
|
||||
event_id: int,
|
||||
event_type: StringName,
|
||||
tick: int,
|
||||
actor_id: int,
|
||||
source_id: StringName,
|
||||
destination_id: StringName,
|
||||
item_id: StringName,
|
||||
amount: float
|
||||
) -> EconomicEventRecord:
|
||||
return EconomicEventRecord.new({
|
||||
"schema_version": SCHEMA_VERSION,
|
||||
"event_id": event_id,
|
||||
"event_type": String(event_type),
|
||||
"tick": tick,
|
||||
"actor_id": actor_id,
|
||||
"source_id": String(source_id),
|
||||
"destination_id": String(destination_id),
|
||||
"item_id": String(item_id),
|
||||
"amount": amount
|
||||
})
|
||||
|
||||
static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all([
|
||||
"event_id", "event_type", "tick", "actor_id", "source_id",
|
||||
"destination_id", "item_id", "amount"
|
||||
]):
|
||||
return null
|
||||
var normalized := record_data.duplicate(true)
|
||||
normalized["schema_version"] = SCHEMA_VERSION
|
||||
normalized["event_id"] = int(record_data["event_id"])
|
||||
normalized["event_type"] = String(record_data["event_type"])
|
||||
normalized["tick"] = int(record_data["tick"])
|
||||
normalized["actor_id"] = int(record_data["actor_id"])
|
||||
normalized["source_id"] = String(record_data["source_id"])
|
||||
normalized["destination_id"] = String(record_data["destination_id"])
|
||||
normalized["item_id"] = String(record_data["item_id"])
|
||||
normalized["amount"] = float(record_data["amount"])
|
||||
if (
|
||||
normalized["event_id"] < 0
|
||||
or normalized["tick"] < 0
|
||||
or normalized["event_type"].is_empty()
|
||||
or normalized["source_id"].is_empty()
|
||||
or normalized["destination_id"].is_empty()
|
||||
or normalized["item_id"].is_empty()
|
||||
or normalized["amount"] <= 0.0
|
||||
):
|
||||
return null
|
||||
return EconomicEventRecord.new(normalized)
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
return data.duplicate(true)
|
||||
Reference in New Issue
Block a user