Bombsquad-Ballistica-Modded.../dist/ba_data/python/bauiv1lib/connectivity.py

151 lines
4.7 KiB
Python
Raw Permalink Normal View History

2024-05-19 18:25:43 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""UI functionality related to master-server connectivity."""
from __future__ import annotations
from typing import TYPE_CHECKING
import bauiv1 as bui
if TYPE_CHECKING:
from typing import Callable, Any
def wait_for_connectivity(
on_connected: Callable[[], Any],
on_cancel: Callable[[], Any] | None = None,
) -> None:
"""Wait for the engine to establish a master-server connection.
If need be, shows a window to keep the user informed of connectivity
2025-04-06 17:17:13 +05:30
state and allows the user to cancel the operation. Note that
canceling does not prevent the engine from continuing its attempt to
establish connectivity; it simply cancels the operation that is
waiting for connectivity.
2024-05-19 18:25:43 +05:30
"""
plus = bui.app.plus
assert plus is not None
# Quick-out: if we're already connected, don't bother with the UI.
2024-11-28 00:23:35 +05:30
# We do, however, push this call instead of calling it immediately
# so as to be consistent with the waiting path.
2024-05-19 18:25:43 +05:30
if plus.cloud.connected:
2024-11-28 00:23:35 +05:30
bui.pushcall(on_connected)
2024-05-19 18:25:43 +05:30
return
WaitForConnectivityWindow(on_connected=on_connected, on_cancel=on_cancel)
class WaitForConnectivityWindow(bui.Window):
"""Window informing the user that the game is establishing connectivity."""
def __init__(
self,
on_connected: Callable[[], Any],
on_cancel: Callable[[], Any] | None,
) -> None:
self._on_connected = on_connected
self._on_cancel = on_cancel
self._width = 650
self._height = 300
super().__init__(
root_widget=bui.containerwidget(
size=(self._width, self._height),
transition='in_scale',
parent=bui.get_special_widget('overlay_stack'),
)
)
bui.textwidget(
parent=self._root_widget,
2025-02-09 00:17:58 +05:30
position=(self._width * 0.5, self._height * 0.7),
2024-05-19 18:25:43 +05:30
size=(0, 0),
scale=1.2,
h_align='center',
v_align='center',
2025-02-09 00:17:58 +05:30
text=bui.Lstr(resource='internal.connectingToPartyText'),
2024-05-19 18:25:43 +05:30
maxwidth=self._width * 0.9,
)
2025-02-09 00:17:58 +05:30
self._spinner = bui.spinnerwidget(
parent=self._root_widget,
position=(self._width * 0.5, self._height * 0.54),
style='bomb',
size=48,
)
2024-05-19 18:25:43 +05:30
self._info_text = bui.textwidget(
parent=self._root_widget,
2025-02-09 00:17:58 +05:30
position=(self._width * 0.5, self._height * 0.4),
2024-05-19 18:25:43 +05:30
size=(0, 0),
2025-02-09 00:17:58 +05:30
color=(0.6, 0.5, 0.6),
2024-05-19 18:25:43 +05:30
flatness=1.0,
2025-02-09 00:17:58 +05:30
shadow=0.0,
scale=0.75,
2024-05-19 18:25:43 +05:30
h_align='center',
v_align='center',
2025-02-09 00:17:58 +05:30
text='',
2024-05-19 18:25:43 +05:30
maxwidth=self._width * 0.9,
)
2025-02-09 00:17:58 +05:30
self._info_text_str = ''
2024-05-19 18:25:43 +05:30
cancel_button = bui.buttonwidget(
parent=self._root_widget,
autoselect=True,
position=(50, 30),
size=(150, 50),
label=bui.Lstr(resource='cancelText'),
on_activate_call=self._cancel,
)
bui.containerwidget(edit=self._root_widget, cancel_button=cancel_button)
self._update_timer = bui.AppTimer(
0.113, bui.WeakCall(self._update), repeat=True
)
def _update(self) -> None:
plus = bui.app.plus
assert plus is not None
if plus.cloud.connected:
self._connected()
return
2025-02-09 00:17:58 +05:30
# Show what connectivity is up to if we don't have any published
# zone-pings yet (or if we do but there's no transport state to
# show yet).
if not bui.app.net.zone_pings or not bui.app.net.transport_state:
infotext = bui.app.net.connectivity_state
else:
infotext = bui.app.net.transport_state
if infotext != self._info_text_str:
self._info_text_str = infotext
bui.textwidget(edit=self._info_text, text=infotext)
2024-05-19 18:25:43 +05:30
def _connected(self) -> None:
if not self._root_widget or self._root_widget.transitioning_out:
return
2025-02-09 00:17:58 +05:30
# Show 'connected.' and kill the spinner for the brief moment
# we're visible on our way out.
bui.textwidget(
edit=self._info_text, text=bui.Lstr(resource='remote_app.connected')
)
if self._spinner:
self._spinner.delete()
2024-05-19 18:25:43 +05:30
bui.containerwidget(
edit=self._root_widget,
transition=('out_scale'),
)
bui.pushcall(self._on_connected)
def _cancel(self) -> None:
if not self._root_widget or self._root_widget.transitioning_out:
return
bui.containerwidget(
edit=self._root_widget,
transition=('out_scale'),
)
if self._on_cancel is not None:
bui.pushcall(self._on_cancel)