bauiv1lib.connectivity

UI functionality related to master-server connectivity.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality related to master-server connectivity."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import bauiv1 as bui
 10
 11if TYPE_CHECKING:
 12    from typing import Callable, Any
 13
 14
 15def wait_for_connectivity(
 16    on_connected: Callable[[], Any],
 17    on_cancel: Callable[[], Any] | None = None,
 18) -> None:
 19    """Wait for the engine to establish a master-server connection.
 20
 21    If need be, shows a window to keep the user informed of connectivity
 22    state and allows the user to cancel the operation. Note that canceling
 23    does not prevent the engine from continuing its attempt to establish
 24    connectivity; it simply cancels the operation that depends on it.
 25    """
 26    plus = bui.app.plus
 27    assert plus is not None
 28
 29    # Quick-out: if we're already connected, don't bother with the UI.
 30    # We do, however, push this call instead of calling it immediately
 31    # so as to be consistent with the waiting path.
 32    if plus.cloud.connected:
 33        bui.pushcall(on_connected)
 34        return
 35
 36    WaitForConnectivityWindow(on_connected=on_connected, on_cancel=on_cancel)
 37
 38
 39class WaitForConnectivityWindow(bui.Window):
 40    """Window informing the user that the game is establishing connectivity."""
 41
 42    def __init__(
 43        self,
 44        on_connected: Callable[[], Any],
 45        on_cancel: Callable[[], Any] | None,
 46    ) -> None:
 47        self._on_connected = on_connected
 48        self._on_cancel = on_cancel
 49        self._width = 650
 50        self._height = 300
 51        super().__init__(
 52            root_widget=bui.containerwidget(
 53                size=(self._width, self._height),
 54                transition='in_scale',
 55                parent=bui.get_special_widget('overlay_stack'),
 56            )
 57        )
 58        bui.textwidget(
 59            parent=self._root_widget,
 60            position=(self._width * 0.5, self._height * 0.65),
 61            size=(0, 0),
 62            scale=1.2,
 63            h_align='center',
 64            v_align='center',
 65            text=bui.Lstr(resource='internal.connectingToPartyText'),
 66            maxwidth=self._width * 0.9,
 67        )
 68        self._info_text = bui.textwidget(
 69            parent=self._root_widget,
 70            position=(self._width * 0.5, self._height * 0.45),
 71            size=(0, 0),
 72            color=(0.6, 0.5, 0.6),
 73            flatness=1.0,
 74            shadow=0.0,
 75            scale=0.75,
 76            h_align='center',
 77            v_align='center',
 78            text='',
 79            maxwidth=self._width * 0.9,
 80        )
 81        self._info_text_str = ''
 82        cancel_button = bui.buttonwidget(
 83            parent=self._root_widget,
 84            autoselect=True,
 85            position=(50, 30),
 86            size=(150, 50),
 87            label=bui.Lstr(resource='cancelText'),
 88            on_activate_call=self._cancel,
 89        )
 90        bui.containerwidget(edit=self._root_widget, cancel_button=cancel_button)
 91        self._update_timer = bui.AppTimer(
 92            0.113, bui.WeakCall(self._update), repeat=True
 93        )
 94
 95    def _update(self) -> None:
 96
 97        plus = bui.app.plus
 98        assert plus is not None
 99
100        if plus.cloud.connected:
101            self._connected()
102            return
103
104        # Show what connectivity is up to if we don't have any published
105        # zone-pings yet (or if we do but there's no transport state to
106        # show yet).
107        if not bui.app.net.zone_pings or not bui.app.net.transport_state:
108            infotext = bui.app.net.connectivity_state
109        else:
110            infotext = bui.app.net.transport_state
111        if infotext != self._info_text_str:
112            self._info_text_str = infotext
113            bui.textwidget(edit=self._info_text, text=infotext)
114
115    def _connected(self) -> None:
116        if not self._root_widget or self._root_widget.transitioning_out:
117            return
118        bui.containerwidget(
119            edit=self._root_widget,
120            transition=('out_scale'),
121        )
122        bui.pushcall(self._on_connected)
123
124    def _cancel(self) -> None:
125        if not self._root_widget or self._root_widget.transitioning_out:
126            return
127        bui.containerwidget(
128            edit=self._root_widget,
129            transition=('out_scale'),
130        )
131        if self._on_cancel is not None:
132            bui.pushcall(self._on_cancel)
def wait_for_connectivity( on_connected: Callable[[], Any], on_cancel: Optional[Callable[[], Any]] = None) -> None:
16def wait_for_connectivity(
17    on_connected: Callable[[], Any],
18    on_cancel: Callable[[], Any] | None = None,
19) -> None:
20    """Wait for the engine to establish a master-server connection.
21
22    If need be, shows a window to keep the user informed of connectivity
23    state and allows the user to cancel the operation. Note that canceling
24    does not prevent the engine from continuing its attempt to establish
25    connectivity; it simply cancels the operation that depends on it.
26    """
27    plus = bui.app.plus
28    assert plus is not None
29
30    # Quick-out: if we're already connected, don't bother with the UI.
31    # We do, however, push this call instead of calling it immediately
32    # so as to be consistent with the waiting path.
33    if plus.cloud.connected:
34        bui.pushcall(on_connected)
35        return
36
37    WaitForConnectivityWindow(on_connected=on_connected, on_cancel=on_cancel)

Wait for the engine to establish a master-server connection.

If need be, shows a window to keep the user informed of connectivity 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 depends on it.

class WaitForConnectivityWindow(bauiv1._uitypes.Window):
 40class WaitForConnectivityWindow(bui.Window):
 41    """Window informing the user that the game is establishing connectivity."""
 42
 43    def __init__(
 44        self,
 45        on_connected: Callable[[], Any],
 46        on_cancel: Callable[[], Any] | None,
 47    ) -> None:
 48        self._on_connected = on_connected
 49        self._on_cancel = on_cancel
 50        self._width = 650
 51        self._height = 300
 52        super().__init__(
 53            root_widget=bui.containerwidget(
 54                size=(self._width, self._height),
 55                transition='in_scale',
 56                parent=bui.get_special_widget('overlay_stack'),
 57            )
 58        )
 59        bui.textwidget(
 60            parent=self._root_widget,
 61            position=(self._width * 0.5, self._height * 0.65),
 62            size=(0, 0),
 63            scale=1.2,
 64            h_align='center',
 65            v_align='center',
 66            text=bui.Lstr(resource='internal.connectingToPartyText'),
 67            maxwidth=self._width * 0.9,
 68        )
 69        self._info_text = bui.textwidget(
 70            parent=self._root_widget,
 71            position=(self._width * 0.5, self._height * 0.45),
 72            size=(0, 0),
 73            color=(0.6, 0.5, 0.6),
 74            flatness=1.0,
 75            shadow=0.0,
 76            scale=0.75,
 77            h_align='center',
 78            v_align='center',
 79            text='',
 80            maxwidth=self._width * 0.9,
 81        )
 82        self._info_text_str = ''
 83        cancel_button = bui.buttonwidget(
 84            parent=self._root_widget,
 85            autoselect=True,
 86            position=(50, 30),
 87            size=(150, 50),
 88            label=bui.Lstr(resource='cancelText'),
 89            on_activate_call=self._cancel,
 90        )
 91        bui.containerwidget(edit=self._root_widget, cancel_button=cancel_button)
 92        self._update_timer = bui.AppTimer(
 93            0.113, bui.WeakCall(self._update), repeat=True
 94        )
 95
 96    def _update(self) -> None:
 97
 98        plus = bui.app.plus
 99        assert plus is not None
100
101        if plus.cloud.connected:
102            self._connected()
103            return
104
105        # Show what connectivity is up to if we don't have any published
106        # zone-pings yet (or if we do but there's no transport state to
107        # show yet).
108        if not bui.app.net.zone_pings or not bui.app.net.transport_state:
109            infotext = bui.app.net.connectivity_state
110        else:
111            infotext = bui.app.net.transport_state
112        if infotext != self._info_text_str:
113            self._info_text_str = infotext
114            bui.textwidget(edit=self._info_text, text=infotext)
115
116    def _connected(self) -> None:
117        if not self._root_widget or self._root_widget.transitioning_out:
118            return
119        bui.containerwidget(
120            edit=self._root_widget,
121            transition=('out_scale'),
122        )
123        bui.pushcall(self._on_connected)
124
125    def _cancel(self) -> None:
126        if not self._root_widget or self._root_widget.transitioning_out:
127            return
128        bui.containerwidget(
129            edit=self._root_widget,
130            transition=('out_scale'),
131        )
132        if self._on_cancel is not None:
133            bui.pushcall(self._on_cancel)

Window informing the user that the game is establishing connectivity.

WaitForConnectivityWindow( on_connected: Callable[[], Any], on_cancel: Optional[Callable[[], Any]])
43    def __init__(
44        self,
45        on_connected: Callable[[], Any],
46        on_cancel: Callable[[], Any] | None,
47    ) -> None:
48        self._on_connected = on_connected
49        self._on_cancel = on_cancel
50        self._width = 650
51        self._height = 300
52        super().__init__(
53            root_widget=bui.containerwidget(
54                size=(self._width, self._height),
55                transition='in_scale',
56                parent=bui.get_special_widget('overlay_stack'),
57            )
58        )
59        bui.textwidget(
60            parent=self._root_widget,
61            position=(self._width * 0.5, self._height * 0.65),
62            size=(0, 0),
63            scale=1.2,
64            h_align='center',
65            v_align='center',
66            text=bui.Lstr(resource='internal.connectingToPartyText'),
67            maxwidth=self._width * 0.9,
68        )
69        self._info_text = bui.textwidget(
70            parent=self._root_widget,
71            position=(self._width * 0.5, self._height * 0.45),
72            size=(0, 0),
73            color=(0.6, 0.5, 0.6),
74            flatness=1.0,
75            shadow=0.0,
76            scale=0.75,
77            h_align='center',
78            v_align='center',
79            text='',
80            maxwidth=self._width * 0.9,
81        )
82        self._info_text_str = ''
83        cancel_button = bui.buttonwidget(
84            parent=self._root_widget,
85            autoselect=True,
86            position=(50, 30),
87            size=(150, 50),
88            label=bui.Lstr(resource='cancelText'),
89            on_activate_call=self._cancel,
90        )
91        bui.containerwidget(edit=self._root_widget, cancel_button=cancel_button)
92        self._update_timer = bui.AppTimer(
93            0.113, bui.WeakCall(self._update), repeat=True
94        )