Refactor save/load functionality; improve error messages and add save data timer

This commit is contained in:
Kirill
2026-07-10 20:36:44 +03:00
parent 45cfc8c8ee
commit 5820bbc7b2
5 changed files with 83 additions and 33 deletions
+4 -17
View File
@@ -5,25 +5,14 @@ extends Node
const SETTING_SECTION := "Settings" const SETTING_SECTION := "Settings"
const SAVE_PATH = "user://settings.cfg" const SAVE_PATH = "user://settings.cfg"
#region Save Load routines
func ch():
pass
#print(Global.Owniverse)
#print('h')
#save_data('ow1', 'test', Global.Owniverse)
#var d1: Dictionary = load_data('ow1', 'test1')
#print("d1: ", d1)
#var d2: Dictionary = load_data('ow1', 'test')
#print("d2: ", d2)
#print('d')
#print(Global.Owniverse.datetime.WEEKDAYS)
func save_data(filename: String, password: String, data: Dictionary) -> void: func save_data(filename: String, password: String, data: Dictionary) -> void:
filename = "user://" + filename + ".adventure" filename = "user://" + filename + ".adventure"
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.WRITE, password) var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.WRITE, password)
if file == null: if file == null:
push_error("Не удалось открыть файл для шифрованной записи") push_error("Can't write to the file: ", filename)
return return
file.store_var(data) # бинарно + быстрее + сериализация file.store_var(data) # бинарно + быстрее + сериализация
@@ -37,17 +26,15 @@ func load_data(filename: String, password: String) -> Dictionary:
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.READ, password) var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.READ, password)
if file == null: if file == null:
push_error("Неверный пароль или повреждённый файл") push_error(filename, " couldn't be decrypted.")
return {} return {}
var data: Variant = file.get_var() var data: Variant = file.get_var()
file.close() file.close()
#print("data")
#print(data)
return data if data is Dictionary else {} return data if data is Dictionary else {}
#endregion
#region Settings #region Settings
+6
View File
@@ -93,6 +93,7 @@ func add_to_update_list(item_id: String, amount: int) -> void:
func set_owniverse_item_value() -> void: func set_owniverse_item_value() -> void:
for item_id in owniverse.items_to_update: for item_id in owniverse.items_to_update:
if owniverse_items.has(item_id):
owniverse_items[item_id].set_label(owniverse.items_to_update[item_id]) owniverse_items[item_id].set_label(owniverse.items_to_update[item_id])
owniverse.items_to_update = {} owniverse.items_to_update = {}
@@ -235,6 +236,7 @@ func _input(event: InputEvent) -> void:
func _ready() -> void: func _ready() -> void:
owniverse.init_enitities() owniverse.init_enitities()
owniverse.load_data()
_init_owniverse_items() _init_owniverse_items()
owniverse.unlock_item.connect(_unlock_item) owniverse.unlock_item.connect(_unlock_item)
@@ -298,3 +300,7 @@ func _change_speed(delta: int) -> void:
func _on_topmenu_button_pressed(ButtonName) -> void: func _on_topmenu_button_pressed(ButtonName) -> void:
print(ButtonName) print(ButtonName)
func _on_save_data() -> void:
owniverse.save_data()
+2 -2
View File
@@ -20,8 +20,8 @@ var item_type = ""
func set_label(value: float) -> void: func set_label(value: float) -> void:
#description.text = Global.format_number(value) description.text = Global.format_number(value)
description.text = str(value) #description.text = str(value)
func set_selection(flag: bool) -> void: func set_selection(flag: bool) -> void:
+64 -1
View File
@@ -8,6 +8,8 @@ var current_speed: int = 1
var current_speed_in_years:int = 1 var current_speed_in_years:int = 1
var entities: Dictionary[String, OwniverseEntity] = {} var entities: Dictionary[String, OwniverseEntity] = {}
var entity_groups: Dictionary[String, Array] = {}
var entity_event_groups: Dictionary[String, Array] = {}
var items_to_update: Dictionary[String, float] = {} var items_to_update: Dictionary[String, float] = {}
@@ -29,6 +31,8 @@ func cycle(year_delta: float) -> void:
_check_locked_items() _check_locked_items()
#print(_get_distribution(current_amount))
#region auto production and evolution #region auto production and evolution
@@ -47,6 +51,17 @@ func _auto_production(year_delta: float) -> void:
func _entity_evolution(year_delta: float) -> void: func _entity_evolution(year_delta: float) -> void:
pass pass
func _get_distribution(source: Dictionary) -> Dictionary:
var distribution: Dictionary = {}
var total: float = 0
for id in source:
if source[id] != 0:
total += source[id]
distribution[id] = source[id]
for id in distribution:
distribution[id] /= total
return distribution
#endregion #endregion
#region Production #region Production
@@ -80,7 +95,6 @@ func get_available_to_produce(item_id: String) -> float:
var amount: float = 1E50 var amount: float = 1E50
for elem in entity.cost: for elem in entity.cost:
amount = min(floorf(current_amount[elem] / entity.cost[elem]), amount) amount = min(floorf(current_amount[elem] / entity.cost[elem]), amount)
#print(elem, amount)
return amount return amount
# add to all lists # add to all lists
@@ -132,6 +146,38 @@ func change_speed(delta: int) -> int:
#endregion #endregion
#region Init save load routines
func load_data() -> bool:
var is_succesfull = false
var game_data: Dictionary = SaveManager.load_data("owniverse", "qwerty")
if game_data == {}:
return false
if !game_data.has_all(["cumulative_amount", "current_amount", "entity_batches"]):
return false
entity_batches = game_data["entity_batches"]
cumulative_amount = game_data["cumulative_amount"]
current_amount = game_data["current_amount"]
for item in current_amount:
items_to_update[item] = current_amount[item]
return true
func save_data() -> void:
var game_data: Dictionary = {}
game_data["cumulative_amount"] = cumulative_amount
game_data["current_amount"] = current_amount
game_data["entity_batches"] = entity_batches
SaveManager.save_data("owniverse", "qwerty", game_data)
func init_enitities() -> void: func init_enitities() -> void:
for entity_id in Global.OwniverseEntities: for entity_id in Global.OwniverseEntities:
var entity = OwniverseEntity.new() var entity = OwniverseEntity.new()
@@ -149,3 +195,20 @@ func init_enitities() -> void:
if entity.product_per_year_type != "": if entity.product_per_year_type != "":
auto_production.append(entity) auto_production.append(entity)
# groups
if entity.group_as != "":
if !entity_groups.has(entity.group_as):
entity_groups[entity.group_as] = []
entity_groups[entity.group_as].append(entity_id)
if entity.event_group != "":
if !entity_event_groups.has(entity.event_group):
entity_event_groups[entity.event_group] = []
entity_event_groups[entity.event_group].append(entity_id)
#if entity.event_group != "":
#entity_event_groups.append(entity.id)
print(entity_groups)
print(entity_event_groups)
#endregion
+4 -10
View File
@@ -45,14 +45,6 @@
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ifjjt"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ifjjt"]
[sub_resource type="GDScript" id="GDScript_behgl"]
script/source = "extends Node
func print_smth(smth: String):
pass
print(smth)
"
[node name="World" type="Node2D" unique_id=1392592456] [node name="World" type="Node2D" unique_id=1392592456]
[node name="Background" type="Node2D" parent="." unique_id=1988758765] [node name="Background" type="Node2D" parent="." unique_id=1988758765]
@@ -880,8 +872,9 @@ layout_mode = 2
wait_time = 0.2 wait_time = 0.2
autostart = true autostart = true
[node name="CommandProcessor" type="Node" parent="." unique_id=2069361293] [node name="SaveData" type="Timer" parent="Stage" unique_id=1208861837]
script = SubResource("GDScript_behgl") wait_time = 5.0
autostart = true
[connection signal="timeout" from="Background/Timer" to="Background" method="randomize_parallax"] [connection signal="timeout" from="Background/Timer" to="Background" method="randomize_parallax"]
[connection signal="pressed" from="Stage/StageRows/TopMenu/BurgerButton" to="Stage" method="_on_topmenu_button_pressed" binds= ["Burger"]] [connection signal="pressed" from="Stage/StageRows/TopMenu/BurgerButton" to="Stage" method="_on_topmenu_button_pressed" binds= ["Burger"]]
@@ -897,3 +890,4 @@ script = SubResource("GDScript_behgl")
[connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/AggressionIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Aggression", 4]] [connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/AggressionIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Aggression", 4]]
[connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/TrendIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Trends", 4]] [connection signal="pressed" from="Stage/StageRows/PageScroller/HBoxContainer/LifePage/LifeHeader/TrendIndicator" to="Stage" method="_on_indicator_pressed" binds= ["Trends", 4]]
[connection signal="timeout" from="Stage/ItemAmountUpdate" to="Stage" method="set_owniverse_item_value"] [connection signal="timeout" from="Stage/ItemAmountUpdate" to="Stage" method="set_owniverse_item_value"]
[connection signal="timeout" from="Stage/SaveData" to="Stage" method="_on_save_data"]