Bombsquad-Ballistica-Modded.../dist/ba_root/mods/tools/account.py

88 lines
3.3 KiB
Python
Raw Normal View History

2023-03-24 12:20:38 +05:30
# ba_meta require api 6
from __future__ import annotations
import ba
2023-05-28 14:07:54 +05:30
import _ba
import bacommon.cloud
import logging
from efro.error import CommunicationError
2023-05-28 14:07:54 +05:30
from playersData import pdata
STATUS_CHECK_INTERVAL_SECONDS = 2.0
2023-05-28 14:07:54 +05:30
class AccountUtil:
def __init__(self):
self._proxyid: str | None = None
self._proxykey: str | None = None
2022-10-02 21:04:29 +05:30
ba.internal.sign_out_v1()
ba.app.cloud.send_message_cb(bacommon.cloud.LoginProxyRequestMessage(),
2023-05-28 14:07:54 +05:30
on_response=ba.Call(self._on_proxy_request_response))
2022-10-02 21:04:29 +05:30
def _on_proxy_request_response(self, response: bacommon.cloud.LoginProxyRequestResponse | Exception) -> None:
if isinstance(response, Exception):
logging.error("error occured")
logging.critical("Falling back to V1 account")
ba.internal.sign_in_v1('Local')
return
address = ba.internal.get_master_server_address(
version=2) + response.url
2023-05-28 14:07:54 +05:30
logging.debug("Copy this URL to your browser : " + address)
self._proxyid = response.proxyid
self._proxykey = response.proxykey
ba.timer(STATUS_CHECK_INTERVAL_SECONDS,
ba.Call(self._ask_for_status))
def _ask_for_status(self) -> None:
assert self._proxyid is not None
assert self._proxykey is not None
ba.app.cloud.send_message_cb(
bacommon.cloud.LoginProxyStateQueryMessage(
proxyid=self._proxyid, proxykey=self._proxykey),
on_response=ba.Call(self._got_status))
def _got_status(
self, response: bacommon.cloud.LoginProxyStateQueryResponse | Exception
) -> None:
# For now, if anything goes wrong on the server-side, just abort
# with a vague error message. Can be more verbose later if need be.
if (isinstance(response, bacommon.cloud.LoginProxyStateQueryResponse)
and response.state is response.State.FAIL):
2023-05-28 14:07:54 +05:30
logging.error("error occured ..terminating login request")
logging.critical("Falling back to V1 account")
ba.internal.sign_in_v1('Local')
# If we got a token, set ourself as signed in. Hooray!
if (isinstance(response, bacommon.cloud.LoginProxyStateQueryResponse)
and response.state is response.State.SUCCESS):
assert response.credentials is not None
ba.app.accounts_v2.set_primary_credentials(response.credentials)
2022-10-02 21:04:29 +05:30
2023-05-28 14:07:54 +05:30
ba.timer(3, self._logged_in)
2022-10-02 21:04:29 +05:30
return
# If we're still waiting, ask again soon.
if (isinstance(response, Exception)
or response.state is response.State.WAITING):
ba.timer(STATUS_CHECK_INTERVAL_SECONDS,
ba.Call(self._ask_for_status))
2023-05-28 14:07:54 +05:30
2022-10-02 21:04:29 +05:30
def _logged_in(self):
2023-05-28 14:07:54 +05:30
logging.info("Logged in as: " +
ba.internal.get_v1_account_display_string())
def updateOwnerIps():
if "owner" in pdata.get_roles():
accountIds = pdata.get_roles()["owner"]["ids"]
profiles = pdata.get_profiles()
for account_id in accountIds:
if account_id in profiles:
profile = profiles[account_id]
if "lastIP" in profile:
ip = profile["lastIP"]
_ba.append_owner_ip(ip)