@@ -28,6 +28,15 @@ var latest: TaloGameSave:
2828var current : TaloGameSave :
2929 get : return _saves_manager .current_save
3030
31+ var _update_timer := TaloDebounceTimer .new (_handle_update_timer_timeout )
32+
33+ func _ready () -> void :
34+ add_child (_update_timer )
35+
36+ func _handle_update_timer_timeout () -> void :
37+ if _saves_manager .current_save :
38+ await update_save (_saves_manager .current_save )
39+
3140## Sync an offline save with an online save using the offline save data.
3241func replace_save_with_offline_save (offline_save : TaloGameSave ) -> TaloGameSave :
3342 var res := await client .make_request (HTTPClient .METHOD_PATCH , "/%s " % offline_save .id , {
@@ -112,25 +121,35 @@ func register(loadable: TaloLoadable) -> void:
112121
113122## Update the currently loaded save using the current state of the game and with the given name.
114123func update_current_save (new_name : String = "" ) -> TaloGameSave :
115- return await update_save (_saves_manager .current_save , new_name )
124+ if not _saves_manager .current_save :
125+ return null
126+
127+ if not new_name .is_empty ():
128+ return await update_save (_saves_manager .current_save , new_name )
129+ else :
130+ _update_timer .debounce ()
131+ _saves_manager .current_save .content = _saves_manager .get_save_content ()
132+ return _saves_manager .current_save
116133
117134## Update the given save using the current state of the game and with the given name.
118135func update_save (save : TaloGameSave , new_name : String = "" ) -> TaloGameSave :
119- var content := _saves_manager .get_save_content ()
136+ var is_offline := await Talo .is_offline ()
137+ var can_update_save := Talo .identity_check () == OK or is_offline
138+ if not can_update_save :
139+ return null
120140
121- if await Talo .is_offline ():
122- if not new_name .is_empty ():
123- save .name = new_name
141+ if not new_name .is_empty ():
142+ save .name = new_name
143+
144+ var content := _saves_manager .get_save_content ()
145+ save .content = content
124146
125- save . content = content
147+ if is_offline :
126148 save .updated_at = TaloTimeUtils .get_current_datetime_string ()
127149 else :
128- if Talo .identity_check () != OK :
129- return
130-
131150 var res := await client .make_request (HTTPClient .METHOD_PATCH , "/%s " % save .id , {
132- name = save .name if new_name . is_empty () else new_name ,
133- content = content
151+ name = save .name ,
152+ content = save . content
134153 })
135154
136155 match res .status :
0 commit comments