Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions addons/talo/apis/players_api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ signal identification_failed()
## Emitted after calling clear_identity().
signal identity_cleared()

var _update_timer := TaloDebounceTimer.new(_handle_update_timer_timeout)

func _ready() -> void:
Talo.connection_restored.connect(_on_connection_restored)
add_child(_update_timer)

func _handle_update_timer_timeout() -> void:
await Talo.players.update()

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

## Queue a debounced update to the current player. The timer will reset every time this method is called.
func debounce_update() -> void:
_update_timer.debounce()

## Flush and sync the player's current data with Talo.
func update() -> TaloPlayer:
if Talo.identity_check() != OK:
Expand Down
43 changes: 32 additions & 11 deletions addons/talo/apis/saves_api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ var latest: TaloGameSave:
var current: TaloGameSave:
get: return _saves_manager.current_save

var _update_timer := TaloDebounceTimer.new(_handle_update_timer_timeout)

func _ready() -> void:
add_child(_update_timer)

func _handle_update_timer_timeout() -> void:
if _saves_manager.current_save:
await update_save(_saves_manager.current_save)

## Sync an offline save with an online save using the offline save data.
func replace_save_with_offline_save(offline_save: TaloGameSave) -> TaloGameSave:
var res := await client.make_request(HTTPClient.METHOD_PATCH, "/%s" % offline_save.id, {
Expand Down Expand Up @@ -112,25 +121,37 @@ func register(loadable: TaloLoadable) -> void:

## Update the currently loaded save using the current state of the game and with the given name.
func update_current_save(new_name: String = "") -> TaloGameSave:
return await update_save(_saves_manager.current_save, new_name)
if not _saves_manager.current_save:
return null

# if the save is being renamed, sync it immediately
if not new_name.is_empty():
return await update_save(_saves_manager.current_save, new_name)
# else, update the save locally and queue it for syncing
else:
_update_timer.debounce()
_saves_manager.current_save.content = _saves_manager.get_save_content()
return _saves_manager.current_save

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

if await Talo.is_offline():
if not new_name.is_empty():
save.name = new_name
if not new_name.is_empty():
save.name = new_name

var content := _saves_manager.get_save_content()
save.content = content

save.content = content
if is_offline:
save.updated_at = TaloTimeUtils.get_current_datetime_string()
else:
if Talo.identity_check() != OK:
return

var res := await client.make_request(HTTPClient.METHOD_PATCH, "/%s" % save.id, {
name=save.name if new_name.is_empty() else new_name,
content=content
name=save.name,
content=save.content
})

match res.status:
Expand Down
4 changes: 2 additions & 2 deletions addons/talo/entities/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func update_from_raw_data(data: Dictionary) -> void:
func set_prop(key: String, value: String, update: bool = true) -> void:
super.set_prop(key, value)
if update:
await Talo.players.update()
Talo.players.debounce_update()

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

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