From b2e1f7755c8f65b20a27cb767e2acb4346002f99 Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Sun, 15 Aug 2021 12:52:59 +0100 Subject: [PATCH] SteamUser: add refresh() method and rework logic --- steam/client/builtins/user.py | 2 +- steam/client/user.py | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/steam/client/builtins/user.py b/steam/client/builtins/user.py index dbffe50..16a0b7b 100644 --- a/steam/client/builtins/user.py +++ b/steam/client/builtins/user.py @@ -114,7 +114,7 @@ class User(object): self._user_cache[steam_id] = suser if fetch_persona_state: - self.request_persona_state([steam_id]) + suser.refresh(wait=False) return suser diff --git a/steam/client/user.py b/steam/client/user.py index d504c8f..1eebeea 100644 --- a/steam/client/user.py +++ b/steam/client/user.py @@ -14,6 +14,7 @@ class SteamUser(object): This is an internal object that can be obtained by :meth:`SteamClient.get_user` """ _pstate = None + _pstate_requested = False steam_id = SteamID() #: steam id relationship = EFriendRelationship.NONE #: friendship status @@ -30,18 +31,31 @@ class SteamUser(object): self.state, ) + def refresh(self, wait=True): + if self._pstate_requested and self._pstate_ready.is_set(): + self._pstate_requested = False + + if not self._pstate_requested: + self._steam.request_persona_state([self.steam_id]) + self._pstate_ready.clear() + self._pstate_requested = True + + if wait: + self._pstate_ready.wait(timeout=5) + self._pstate_requested = False + def get_ps(self, field_name, wait_pstate=True): """Get property from PersonaState `See full list of available fields_names `_ """ - if not wait_pstate or self._pstate_ready.wait(timeout=5): - if self._pstate is None and wait_pstate: - self._steam.request_persona_state([self.steam_id]) - self._pstate_ready.wait(timeout=5) + if not self._pstate_ready.is_set() and wait_pstate: + self.refresh() + if self._pstate is not None: return getattr(self._pstate, field_name) - return None + else: + return None @property def last_logon(self):