Skip to content

Commit 3f8da60

Browse files
committed
debounce player and save updates
1 parent 3cac8e7 commit 3f8da60

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

addons/talo/apis/players_api.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ signal identification_failed()
1717
## Emitted after calling clear_identity().
1818
signal identity_cleared()
1919

20+
var _update_timer := TaloDebounceTimer.new(_handle_update_timer_timeout)
21+
2022
func _ready() -> void:
2123
Talo.connection_restored.connect(_on_connection_restored)
24+
add_child(_update_timer)
25+
26+
func _handle_update_timer_timeout() -> void:
27+
await Talo.players.update()
2228

2329
func _handle_identify_success(alias: TaloPlayerAlias, socket_token: String = "") -> TaloPlayer:
2430
if not await Talo.is_offline():
@@ -57,6 +63,10 @@ func identify_steam(ticket: String, identity: String = "") -> TaloPlayer:
5763
else:
5864
return await identify("steam", "%s:%s" % [identity, ticket])
5965

66+
## Queue a debounced update to the current player. The timer will reset every time this method is called.
67+
func debounce_update() -> void:
68+
_update_timer.debounce()
69+
6070
## Flush and sync the player's current data with Talo.
6171
func update() -> TaloPlayer:
6272
if Talo.identity_check() != OK:

addons/talo/apis/saves_api.gd

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ var latest: TaloGameSave:
2828
var 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.
3241
func 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,37 @@ func register(loadable: TaloLoadable) -> void:
112121

113122
## Update the currently loaded save using the current state of the game and with the given name.
114123
func 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 the save is being renamed, sync it immediately
128+
if not new_name.is_empty():
129+
return await update_save(_saves_manager.current_save, new_name)
130+
# else, update the save locally and queue it for syncing
131+
else:
132+
_update_timer.debounce()
133+
_saves_manager.current_save.content = _saves_manager.get_save_content()
134+
return _saves_manager.current_save
116135

117136
## Update the given save using the current state of the game and with the given name.
118137
func update_save(save: TaloGameSave, new_name: String = "") -> TaloGameSave:
119-
var content := _saves_manager.get_save_content()
138+
var is_offline := await Talo.is_offline()
139+
var can_update_save := Talo.identity_check() == OK or is_offline
140+
if not can_update_save:
141+
return null
120142

121-
if await Talo.is_offline():
122-
if not new_name.is_empty():
123-
save.name = new_name
143+
if not new_name.is_empty():
144+
save.name = new_name
145+
146+
var content := _saves_manager.get_save_content()
147+
save.content = content
124148

125-
save.content = content
149+
if is_offline:
126150
save.updated_at = TaloTimeUtils.get_current_datetime_string()
127151
else:
128-
if Talo.identity_check() != OK:
129-
return
130-
131152
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
153+
name=save.name,
154+
content=save.content
134155
})
135156

136157
match res.status:

addons/talo/entities/player.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func update_from_raw_data(data: Dictionary) -> void:
2626
func set_prop(key: String, value: String, update: bool = true) -> void:
2727
super.set_prop(key, value)
2828
if update:
29-
await Talo.players.update()
29+
Talo.players.debounce_update()
3030

3131
## Delete a property by key. Optionally sync the player (default true) with Talo.
3232
func delete_prop(key: String, update: bool = true) -> void:
3333
super.delete_prop(key)
3434
if update:
35-
await Talo.players.update()
35+
Talo.players.debounce_update()
3636

3737
## Check if the player is in a group with the given ID.
3838
func is_in_talo_group_id(group_id: String) -> bool:

0 commit comments

Comments
 (0)