Skip to content

Commit

Permalink
fix: Retry on too many offline players error
Browse files Browse the repository at this point in the history
The backend can believe that a player in a lobby is offline. If this
happens too many times, this error will occur. A retry after some time
should give the backend time to realize that they're online.
  • Loading branch information
Amund211 committed Jul 26, 2023
1 parent 964766b commit 228be13
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/prism/overlay/antisniper_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ def is_invalid_api_key_response(response: requests.Response) -> bool: # pragma:
return True


def is_checked_too_many_offline_players_response(
response: requests.Response,
) -> bool: # pragma: nocover
"""Return True if the response is a 403: checked too many offline players"""
if response.status_code != 403:
return False

try:
response_json = response.json()
except JSONDecodeError:
return False

if response_json.get("success", None) is not False:
return False

cause = response_json.get("cause", None)

if not isinstance(cause, str) or "too many offline" not in cause.lower():
return False

return True


def set_denick_cache(nick: str, uuid: str | None) -> str | None:
"""Set the cache entry for nick, and return the uuid"""
with DENICK_MUTEX:
Expand Down Expand Up @@ -100,6 +123,9 @@ def _make_request(
"Request to AntiSniper API failed due to an unknown error"
) from e

if is_checked_too_many_offline_players_response(response):
raise ExecutionError(f"Checked too many offline players for {url}, retrying")

if response.status_code == 429 and not last_try:
raise ExecutionError(
"Request to AntiSniper API failed due to ratelimit, retrying"
Expand Down Expand Up @@ -136,6 +162,9 @@ def get_antisniper_playerdata(
f"Assumed invalid API key. Response: {response.text}"
)

if is_checked_too_many_offline_players_response(response):
raise HypixelAPIError("Checked too many offline players for {uuid}")

if response.status_code == 429:
raise HypixelAPIThrottleError(
f"Request to Hypixel API failed with status code {response.status_code}. "
Expand Down

0 comments on commit 228be13

Please sign in to comment.