Skip to content

Commit

Permalink
[wip] handle market_save in the_market
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Jan 3, 2025
1 parent 360c4e5 commit 3cf37d8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions theLudovyc/EventBus.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions theLudovyc/GUI/MarketContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions theLudovyc/GUI/ResourceOrder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
51 changes: 50 additions & 1 deletion theLudovyc/TheMarket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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

0 comments on commit 3cf37d8

Please sign in to comment.