Save The Tree implementation and acceptance work in progress
Preserve production code, regression fixtures, audio assets and provenance, test infrastructure, and the original-completion plan and progress ledger. Final game and Windows acceptance remain pending; excluded local artifacts and native experiments are not part of this snapshot.
This commit is contained in:
@@ -1,10 +1,45 @@
|
||||
class_name SupplyManagementService
|
||||
extends RefCounted
|
||||
|
||||
const PurchaseQuoteV2 := preload("res://scripts/supply/supply_purchase_quote_v2.gd")
|
||||
|
||||
## Player commands and deterministic replenishment. Vendor purchases arrive in
|
||||
## the selected warehouse; all transfers inside the tree use real porter tasks.
|
||||
|
||||
func preview_purchase(runtime: Variant, warehouse_id: StringName, item_id: StringName, quantity: int) -> Dictionary:
|
||||
# Keep the entire ordinary branch literal; only placed UNIQUE Mimi
|
||||
# selects the explicit total-order quote. No attendance/role/floor condition.
|
||||
if not PurchaseQuoteV2.eligible(runtime):
|
||||
return _preview_purchase_v1(runtime, warehouse_id, item_id, quantity)
|
||||
var quote := _preview_purchase_v1(runtime, warehouse_id, item_id, quantity)
|
||||
var item := SupplyCatalog.get_item(item_id)
|
||||
var facility: FacilityInstance = runtime.facility_service.get_instance(warehouse_id)
|
||||
if facility == null or facility.definition_id != &"warehouse" or item.is_empty() \
|
||||
or not bool(item.get(&"purchasable", false)) or quantity <= 0 or quantity > 10000: return quote
|
||||
var frozen := PurchaseQuoteV2.make(item_id, quantity, int(item[&"unit_price"]))
|
||||
if frozen.is_empty(): return quote
|
||||
var cash: int = runtime.economy_service.get_balance(&"PLAYER_CASH")
|
||||
var space: int = maxi(SupplyCatalog.storage_capacity(facility.definition_id) - SupplyCatalog.inventory_weight(runtime.inventory_service, facility.local_inventory_id) - _incoming_weight(runtime, facility.local_inventory_id), 0)
|
||||
@warning_ignore("integer_division")
|
||||
var slots := space / int(item[&"unit_weight"])
|
||||
quote[&"purchase_quote"] = frozen
|
||||
quote[&"total_cost"] = frozen[&"effective_total"]
|
||||
quote[&"cash_after"] = cash - int(frozen[&"effective_total"])
|
||||
quote[&"max_quantity"] = PurchaseQuoteV2.affordable_quantity(cash, slots, int(item[&"unit_price"]))
|
||||
quote[&"ok"] = false
|
||||
quote[&"reason"] = &""
|
||||
if not facility.allows_logistics():
|
||||
quote[&"reason"] = &"FACILITY_CLOSED"
|
||||
elif int(frozen[&"effective_total"]) > cash:
|
||||
quote[&"reason"] = &"INSUFFICIENT_FUNDS"
|
||||
elif quantity * int(item[&"unit_weight"]) > space:
|
||||
quote[&"reason"] = &"STORAGE_FULL"
|
||||
else:
|
||||
quote[&"ok"] = true
|
||||
return quote
|
||||
|
||||
|
||||
func _preview_purchase_v1(runtime: Variant, warehouse_id: StringName, item_id: StringName, quantity: int) -> Dictionary:
|
||||
var cash: int = runtime.economy_service.get_balance(&"PLAYER_CASH")
|
||||
var item := SupplyCatalog.get_item(item_id)
|
||||
var facility: FacilityInstance = runtime.facility_service.get_instance(warehouse_id)
|
||||
@@ -62,12 +97,20 @@ func _purchase_paid(runtime: Variant, warehouse_id: StringName, item_id: StringN
|
||||
runtime.economy_service.restore_snapshot(economy_before)
|
||||
return _purchase_failure(command_id, &"INVENTORY_ERROR")
|
||||
var memo: StringName = SupplyAutoPurchasePlans.receipt_memo(item_id, quantity, int(item[&"unit_price"]), int(quote[&"total_cost"])) if record_auto_attempt else &"supply_purchase"
|
||||
if quote.has(&"purchase_quote"):
|
||||
memo = PurchaseQuoteV2.receipt_memo(quote[&"purchase_quote"])
|
||||
var paid: CommandResult = runtime.economy_service.transfer(&"PLAYER_CASH", runtime.OPERATING_EXPENSE_ACCOUNT, int(quote[&"total_cost"]), minute, &"OPERATING_EXPENSE", warehouse_id, ledger_key, memo, &"", false)
|
||||
if not paid.is_success():
|
||||
runtime.inventory_service.restore_snapshot(inventory_before)
|
||||
runtime.economy_service.restore_snapshot(economy_before)
|
||||
return _purchase_failure(command_id, &"PAYMENT_FAILED")
|
||||
if record_auto_attempt and not runtime.auto_purchase_plans.record_attempt(warehouse_id, item_id, minute, CommandResult.succeeded(0, {&"transaction_id": paid.details[&"transaction_id"], &"quantity": quantity, &"unit_price": item[&"unit_price"], &"total_cost": quote[&"total_cost"]}), runtime.economy_service):
|
||||
var recorded := true
|
||||
if record_auto_attempt:
|
||||
if quote.has(&"purchase_quote"):
|
||||
recorded = runtime.auto_purchase_plans.record_attempt(warehouse_id, item_id, minute, CommandResult.succeeded(0, {&"transaction_id": paid.details[&"transaction_id"], &"quantity": quantity, &"purchase_quote": quote[&"purchase_quote"], &"total_cost": quote[&"total_cost"]}), runtime.economy_service)
|
||||
else:
|
||||
recorded = runtime.auto_purchase_plans.record_attempt(warehouse_id, item_id, minute, CommandResult.succeeded(0, {&"transaction_id": paid.details[&"transaction_id"], &"quantity": quantity, &"unit_price": item[&"unit_price"], &"total_cost": quote[&"total_cost"]}), runtime.economy_service)
|
||||
if not recorded:
|
||||
runtime.inventory_service.restore_snapshot(inventory_before)
|
||||
runtime.economy_service.restore_snapshot(economy_before)
|
||||
return _purchase_failure(command_id, &"PAYMENT_FAILED")
|
||||
@@ -153,7 +196,11 @@ func advance_auto_purchases(runtime: Variant, minute: int) -> Array[Dictionary]:
|
||||
# today's quote is unaffordable. It never stocks, charges or notifies.
|
||||
var receipt := SupplyAutoPurchasePlans.receipt_from_ledger(runtime.economy_service, warehouse_id, item_id, minute, prior_id)
|
||||
if receipt.is_empty():
|
||||
result = _purchase_failure(0, &"REPLAY_MISMATCH")
|
||||
var frozen := PurchaseQuoteV2.receipt_from_ledger(runtime.economy_service, warehouse_id, item_id, minute, prior_id)
|
||||
if frozen.is_empty():
|
||||
result = _purchase_failure(0, &"REPLAY_MISMATCH")
|
||||
else:
|
||||
result = CommandResult.succeeded(0, {&"transaction_id": prior_id, &"quantity": frozen[&"quantity"], &"purchase_quote": frozen, &"total_cost": frozen[&"effective_total"], &"replayed": true})
|
||||
else:
|
||||
result = CommandResult.succeeded(0, {&"transaction_id": prior_id, &"quantity": receipt[&"quantity"], &"unit_price": receipt[&"unit_price"], &"total_cost": receipt[&"total_cost"], &"replayed": true})
|
||||
if (not result.is_success() or not prior_id.is_empty()) and not runtime.auto_purchase_plans.record_attempt(warehouse_id, item_id, minute, result, runtime.economy_service):
|
||||
|
||||
Reference in New Issue
Block a user