Skip to content

Commit

Permalink
Let some apis in PlayersAPI return TaloPlayer directly, and let `up…
Browse files Browse the repository at this point in the history
…date()` try to update current player instead of creating a new instance.
  • Loading branch information
Daylily-Zeleen committed Feb 25, 2025
1 parent 6f66437 commit ebaf251
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 14 additions & 6 deletions addons/talo/apis/players_api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@ class_name PlayersAPI extends TaloAPI
signal identified(player: TaloPlayer)

## Identify a player using a service (e.g. "username") and identifier (e.g. "bob").
func identify(service: String, identifier: String) -> void:
func identify(service: String, identifier: String) -> TaloPlayer:
var res = await client.make_request(HTTPClient.METHOD_GET, "/identify?service=%s&identifier=%s" % [service, identifier])
match (res.status):
200:
Talo.current_alias = TaloPlayerAlias.new(res.body.alias)
Talo.socket.set_socket_token(res.body.socketToken)
identified.emit(Talo.current_player)
return Talo.current_player
_:
if not await Talo.is_offline():
Talo.player_auth.session_manager.clear_session()
return null

## Identify a player using a Steam ticket.
func identify_steam(ticket: String, identity: String = "") -> void:
func identify_steam(ticket: String, identity: String = "") -> TaloPlayer:
if identity.is_empty():
identify("steam", ticket)
return await identify("steam", ticket)
else:
identify("steam", "%s:%s" % [identity, ticket])
return await identify("steam", "%s:%s" % [identity, ticket])

## Flush and sync the player's current data with Talo.
func update() -> void:
func update() -> TaloPlayer:
var res = await client.make_request(HTTPClient.METHOD_PATCH, "/%s" % Talo.current_player.id, { props = Talo.current_player.get_serialized_props() })
match (res.status):
200:
Talo.current_alias.player = TaloPlayer.new(res.body.player)
if is_instance_valid(Talo.current_alias.player):
Talo.current_alias.player.update_from_raw_data(res.body.player)
else:
Talo.current_alias.player = TaloPlayer.new(res.body.player)
return Talo.current_player
_:
return null

## Merge all of the data from player_id2 into player_id1 and delete player_id2.
func merge(player_id1: String, player_id2: String) -> TaloPlayer:
Expand Down
7 changes: 6 additions & 1 deletion addons/talo/entities/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ var id: String
var groups: Array[TaloPlayerGroupStub] = []

func _init(data: Dictionary):
super._init(data.props.map(func (prop): return TaloProp.new(prop.key, prop.value)))
super._init([]) # Will be initialized correctly in update_from_raw_data().
update_from_raw_data(data)

## Update the player from raw data.
func update_from_raw_data(data: Dictionary) -> void:
props.assign(data.props.map(func (prop): return TaloProp.new(prop.key, prop.value)))

id = data.id
groups.assign(data.groups.map(func (group): return TaloPlayerGroupStub.new(group.id, group.name)))
Expand Down

0 comments on commit ebaf251

Please sign in to comment.