From 3cf37d8879101f0bb59f6b2e088bb00226b8cf6e Mon Sep 17 00:00:00 2001 From: theludovyc Date: Fri, 3 Jan 2025 16:19:45 +0100 Subject: [PATCH] [wip] handle market_save in the_market --- theLudovyc/EventBus.gd | 1 + theLudovyc/GUI/MarketContainer.gd | 12 ++++++++ theLudovyc/GUI/ResourceOrder.gd | 3 ++ theLudovyc/Game2D.gd | 3 ++ theLudovyc/TheMarket.gd | 51 ++++++++++++++++++++++++++++++- 5 files changed, 69 insertions(+), 1 deletion(-) diff --git a/theLudovyc/EventBus.gd b/theLudovyc/EventBus.gd index 1316c5e0..2539cdfc 100644 --- a/theLudovyc/EventBus.gd +++ b/theLudovyc/EventBus.gd @@ -32,6 +32,7 @@ signal money_production_rate_updated(money_production_rate) ## ORDER signal ask_create_new_order(resource_type) signal send_create_new_order(resource_type) +signal send_create_new_order_with_values(resource_type, buy_amount, sell_amount) signal ask_remove_order(resource_type) signal send_remove_order(resource_type) diff --git a/theLudovyc/GUI/MarketContainer.gd b/theLudovyc/GUI/MarketContainer.gd index ee6627fe..c73233be 100644 --- a/theLudovyc/GUI/MarketContainer.gd +++ b/theLudovyc/GUI/MarketContainer.gd @@ -20,6 +20,7 @@ func _ready(): event_bus = current_scene.get_node("EventBus") as EventBus event_bus.send_create_new_order.connect(_on_receive_create_new_order) + event_bus.send_create_new_order_with_values.connect(_on_receive_create_new_order_with_values) event_bus.send_remove_order.connect(_on_receive_remove_order) event_bus.send_update_order_buy.connect(_on_receive_update_order_buy) event_bus.money_production_rate_updated.connect(_on_receive_money_production_rate_updated) @@ -44,6 +45,17 @@ func _on_receive_create_new_order(resource_type: Resources.Types): order_nodes[resource_type] = resource_order +func _on_receive_create_new_order_with_values(resource_type: Resources.Types, + buy_amount:int, sell_amount:int): + var resource_order = resource_order_scene.instantiate() + + order_container.add_child(resource_order) + + resource_order._resource_type = resource_type + resource_order.force_buy_value(buy_amount) + resource_order.force_sell_value(sell_amount) + + order_nodes[resource_type] = resource_order func _on_receive_remove_order(resource_type: Resources.Types): order_nodes[resource_type].queue_free() diff --git a/theLudovyc/GUI/ResourceOrder.gd b/theLudovyc/GUI/ResourceOrder.gd index 9e7befa2..92333bd0 100644 --- a/theLudovyc/GUI/ResourceOrder.gd +++ b/theLudovyc/GUI/ResourceOrder.gd @@ -5,6 +5,7 @@ var event_bus: EventBus = null @onready var _resource_texture = $VBoxContainer/TextureRect @onready var buy_spin_box = $BuySpinBox +@onready var sell_spin_box = $SellSpinBox @onready var delete_button = $VBoxContainer/DeleteButton @@ -41,6 +42,8 @@ func _on_SellSpinBox_value_changed(value): if event_bus != null: event_bus.ask_update_order_sell.emit(_resource_type, value) +func force_sell_value(sell_amount:int): + sell_spin_box.set_value_no_signal(sell_amount) func _on_DeleteButton_pressed(): delete_button.disabled = true diff --git a/theLudovyc/Game2D.gd b/theLudovyc/Game2D.gd index 35405db2..c6a0feb0 100644 --- a/theLudovyc/Game2D.gd +++ b/theLudovyc/Game2D.gd @@ -14,6 +14,7 @@ class_name Game2D @onready var the_storage := $TheStorage @onready var the_bank := $TheBank @onready var the_factory := $TheFactory +@onready var the_market := $TheMarket @onready var the_builder := $TheBuilder @onready var gui := $GUI @@ -70,6 +71,7 @@ func _ready(): the_storage.load_storage_save() the_bank.load_bank_save() the_factory.load_factory_save() + the_market.load_market_save() the_builder.load_buildings_save() else: # save cannot be loaded @@ -264,6 +266,7 @@ func _on_PauseMenu_ask_to_save() -> void: dicoToSave.merge(the_storage.get_storage_save()) dicoToSave.merge(the_bank.get_bank_save()) dicoToSave.merge(the_factory.get_factory_save()) + dicoToSave.merge(the_market.get_market_save()) dicoToSave.merge(the_builder.get_buildings_save()) pause_menu.save_this_please(dicoToSave) diff --git a/theLudovyc/TheMarket.gd b/theLudovyc/TheMarket.gd index 0311ba23..77c2c156 100644 --- a/theLudovyc/TheMarket.gd +++ b/theLudovyc/TheMarket.gd @@ -9,8 +9,14 @@ class Order: var buy_amount := 0 var sell_amount := 0 + func _to_json() -> Array: + return [buy_amount, sell_amount] + + func _from_json(data:Array): + buy_amount = data[0] + sell_amount = data[1] -var orders = {} +var orders := {} func _ready(): @@ -99,3 +105,46 @@ func _on_TheTicker_cycle(): if the_storage.try_to_sell_resource(order_key, order.sell_amount): the_bank.conclude_sale(order_key, order.sell_amount) + +func get_market_save() -> Dictionary: + var data_to_save := {} + + for resource_type in orders: + var order = orders[resource_type] + + data_to_save[resource_type] = order._to_json() + + return {"Market": data_to_save} + +func load_market_save() -> Error: + if SaveHelper.last_loaded_data.is_empty(): + return FAILED + + var market_data:Dictionary = SaveHelper.last_loaded_data.get("Market", {}) + + if market_data.is_empty(): + return FAILED + + for resource_type_str in market_data: + var resource_type:int = resource_type_str.to_int() + + if not Resources.Types.values().has(resource_type): + push_error("Cannot create a production line \ + from an unknow resource type: " + resource_type_str) + + continue + + var order = Order.new() + order._from_json(market_data[resource_type_str]) + + orders[resource_type] = order + + if event_bus != null: + event_bus.send_create_new_order_with_values.emit( + resource_type, order.buy_amount, order.sell_amount) + + the_storage.update_global_production_rate(resource_type) + + the_bank.recalculate_orders_cost() + + return OK