feat: add location-based food storage loop
This commit is contained in:
@@ -2,12 +2,14 @@ class_name SimulationStateRecord
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_NAME := "the_steward.simulation"
|
||||
const SCHEMA_VERSION := 1
|
||||
const SCHEMA_VERSION := 2
|
||||
const LEGACY_SCHEMA_VERSION := 1
|
||||
|
||||
var simulation: Dictionary
|
||||
var village: VillageStateRecord
|
||||
var npcs: Array[NPCStateRecord] = []
|
||||
var resources: Array[ResourceStateRecord] = []
|
||||
var storages: Array[StorageStateRecord] = []
|
||||
|
||||
func to_dictionary() -> Dictionary:
|
||||
var npc_data: Array[Dictionary] = []
|
||||
@@ -17,6 +19,9 @@ func to_dictionary() -> Dictionary:
|
||||
var resource_data: Array[Dictionary] = []
|
||||
for resource_record in resources:
|
||||
resource_data.append(resource_record.to_dictionary())
|
||||
var storage_data: Array[Dictionary] = []
|
||||
for storage_record in storages:
|
||||
storage_data.append(storage_record.to_dictionary())
|
||||
|
||||
return {
|
||||
"schema": SCHEMA_NAME,
|
||||
@@ -24,7 +29,8 @@ func to_dictionary() -> Dictionary:
|
||||
"simulation": simulation.duplicate(true),
|
||||
"village": village.to_dictionary(),
|
||||
"npcs": npc_data,
|
||||
"resources": resource_data
|
||||
"resources": resource_data,
|
||||
"storages": storage_data
|
||||
}
|
||||
|
||||
func to_json() -> String:
|
||||
@@ -39,10 +45,13 @@ static func from_json(json_text: String) -> SimulationStateRecord:
|
||||
static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
if record_data.get("schema", "") != SCHEMA_NAME:
|
||||
return null
|
||||
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
||||
var version := int(record_data.get("schema_version", -1))
|
||||
if version == LEGACY_SCHEMA_VERSION:
|
||||
record_data = _migrate_v1(record_data)
|
||||
elif version != SCHEMA_VERSION:
|
||||
return null
|
||||
if not record_data.has_all([
|
||||
"simulation", "village", "npcs", "resources"
|
||||
"simulation", "village", "npcs", "resources", "storages"
|
||||
]):
|
||||
return null
|
||||
|
||||
@@ -72,7 +81,12 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
|
||||
var npc_data = record_data["npcs"]
|
||||
var resource_data = record_data["resources"]
|
||||
if not npc_data is Array or not resource_data is Array:
|
||||
var storage_data = record_data["storages"]
|
||||
if (
|
||||
not npc_data is Array
|
||||
or not resource_data is Array
|
||||
or not storage_data is Array
|
||||
):
|
||||
return null
|
||||
|
||||
var record := SimulationStateRecord.new()
|
||||
@@ -105,4 +119,30 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
|
||||
resource_ids[resource_id] = true
|
||||
record.resources.append(resource_record)
|
||||
|
||||
var storage_ids := {}
|
||||
for item in storage_data:
|
||||
if not item is Dictionary:
|
||||
return null
|
||||
var storage_record := StorageStateRecord.from_dictionary(item)
|
||||
if storage_record == null:
|
||||
return null
|
||||
var storage_id := storage_record.get_storage_id()
|
||||
if storage_ids.has(storage_id):
|
||||
return null
|
||||
storage_ids[storage_id] = true
|
||||
record.storages.append(storage_record)
|
||||
|
||||
return record
|
||||
|
||||
static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
|
||||
var migrated := legacy_data.duplicate(true)
|
||||
migrated["schema_version"] = SCHEMA_VERSION
|
||||
var village_data: Dictionary = legacy_data.get("village", {})
|
||||
var initial_food := float(village_data.get("food", 0.0))
|
||||
migrated["storages"] = [
|
||||
StorageStateRecord.create(
|
||||
SimulationIds.STORAGE_VILLAGE_PANTRY,
|
||||
{String(SimulationIds.RESOURCE_FOOD): initial_food}
|
||||
).to_dictionary()
|
||||
]
|
||||
return migrated
|
||||
|
||||
Reference in New Issue
Block a user