bastd.ui.purchase

UI related to purchasing items.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI related to purchasing items."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import _ba
 10import ba
 11
 12if TYPE_CHECKING:
 13    from typing import Any
 14
 15
 16class PurchaseWindow(ba.Window):
 17    """Window for purchasing one or more items."""
 18
 19    def __init__(self,
 20                 items: list[str],
 21                 transition: str = 'in_right',
 22                 header_text: ba.Lstr | None = None):
 23        from ba.internal import get_store_item_display_size
 24        from bastd.ui.store import item as storeitemui
 25        if header_text is None:
 26            header_text = ba.Lstr(resource='unlockThisText',
 27                                  fallback_resource='unlockThisInTheStoreText')
 28        if len(items) != 1:
 29            raise ValueError('expected exactly 1 item')
 30        self._items = list(items)
 31        self._width = 580
 32        self._height = 520
 33        uiscale = ba.app.ui.uiscale
 34        super().__init__(root_widget=ba.containerwidget(
 35            size=(self._width, self._height),
 36            transition=transition,
 37            toolbar_visibility='menu_currency',
 38            scale=(1.2 if uiscale is ba.UIScale.SMALL else
 39                   1.1 if uiscale is ba.UIScale.MEDIUM else 1.0),
 40            stack_offset=(0, -15) if uiscale is ba.UIScale.SMALL else (0, 0)))
 41        self._is_double = False
 42        self._title_text = ba.textwidget(parent=self._root_widget,
 43                                         position=(self._width * 0.5,
 44                                                   self._height - 30),
 45                                         size=(0, 0),
 46                                         text=header_text,
 47                                         h_align='center',
 48                                         v_align='center',
 49                                         maxwidth=self._width * 0.9 - 120,
 50                                         scale=1.2,
 51                                         color=(1, 0.8, 0.3, 1))
 52        size = get_store_item_display_size(items[0])
 53        display: dict[str, Any] = {}
 54        storeitemui.instantiate_store_item_display(
 55            items[0],
 56            display,
 57            parent_widget=self._root_widget,
 58            b_pos=(self._width * 0.5 - size[0] * 0.5 + 10 -
 59                   ((size[0] * 0.5 + 30) if self._is_double else 0),
 60                   self._height * 0.5 - size[1] * 0.5 + 30 +
 61                   (20 if self._is_double else 0)),
 62            b_width=size[0],
 63            b_height=size[1],
 64            button=False)
 65
 66        # Wire up the parts we need.
 67        if self._is_double:
 68            pass  # not working
 69        else:
 70            if self._items == ['pro']:
 71                price_str = _ba.get_price(self._items[0])
 72                pyoffs = -15
 73            else:
 74                pyoffs = 0
 75                price = self._price = _ba.get_v1_account_misc_read_val(
 76                    'price.' + str(items[0]), -1)
 77                price_str = ba.charstr(ba.SpecialChar.TICKET) + str(price)
 78            self._price_text = ba.textwidget(parent=self._root_widget,
 79                                             position=(self._width * 0.5,
 80                                                       150 + pyoffs),
 81                                             size=(0, 0),
 82                                             text=price_str,
 83                                             h_align='center',
 84                                             v_align='center',
 85                                             maxwidth=self._width * 0.9,
 86                                             scale=1.4,
 87                                             color=(0.2, 1, 0.2))
 88
 89        self._update_timer = ba.Timer(1.0,
 90                                      ba.WeakCall(self._update),
 91                                      timetype=ba.TimeType.REAL,
 92                                      repeat=True)
 93
 94        self._cancel_button = ba.buttonwidget(
 95            parent=self._root_widget,
 96            position=(50, 40),
 97            size=(150, 60),
 98            scale=1.0,
 99            on_activate_call=self._cancel,
100            autoselect=True,
101            label=ba.Lstr(resource='cancelText'))
102        self._purchase_button = ba.buttonwidget(
103            parent=self._root_widget,
104            position=(self._width - 200, 40),
105            size=(150, 60),
106            scale=1.0,
107            on_activate_call=self._purchase,
108            autoselect=True,
109            label=ba.Lstr(resource='store.purchaseText'))
110
111        ba.containerwidget(edit=self._root_widget,
112                           cancel_button=self._cancel_button,
113                           start_button=self._purchase_button,
114                           selected_child=self._purchase_button)
115
116    def _update(self) -> None:
117        can_die = False
118
119        # We go away if we see that our target item is owned.
120        if self._items == ['pro']:
121            if ba.app.accounts_v1.have_pro():
122                can_die = True
123        else:
124            if _ba.get_purchased(self._items[0]):
125                can_die = True
126
127        if can_die:
128            ba.containerwidget(edit=self._root_widget, transition='out_left')
129
130    def _purchase(self) -> None:
131        from bastd.ui import getcurrency
132        if self._items == ['pro']:
133            _ba.purchase('pro')
134        else:
135            ticket_count: int | None
136            try:
137                ticket_count = _ba.get_v1_account_ticket_count()
138            except Exception:
139                ticket_count = None
140            if ticket_count is not None and ticket_count < self._price:
141                getcurrency.show_get_tickets_prompt()
142                ba.playsound(ba.getsound('error'))
143                return
144
145            def do_it() -> None:
146                _ba.in_game_purchase(self._items[0], self._price)
147
148            ba.playsound(ba.getsound('swish'))
149            do_it()
150
151    def _cancel(self) -> None:
152        ba.containerwidget(edit=self._root_widget, transition='out_right')
class PurchaseWindow(ba.ui.Window):
 17class PurchaseWindow(ba.Window):
 18    """Window for purchasing one or more items."""
 19
 20    def __init__(self,
 21                 items: list[str],
 22                 transition: str = 'in_right',
 23                 header_text: ba.Lstr | None = None):
 24        from ba.internal import get_store_item_display_size
 25        from bastd.ui.store import item as storeitemui
 26        if header_text is None:
 27            header_text = ba.Lstr(resource='unlockThisText',
 28                                  fallback_resource='unlockThisInTheStoreText')
 29        if len(items) != 1:
 30            raise ValueError('expected exactly 1 item')
 31        self._items = list(items)
 32        self._width = 580
 33        self._height = 520
 34        uiscale = ba.app.ui.uiscale
 35        super().__init__(root_widget=ba.containerwidget(
 36            size=(self._width, self._height),
 37            transition=transition,
 38            toolbar_visibility='menu_currency',
 39            scale=(1.2 if uiscale is ba.UIScale.SMALL else
 40                   1.1 if uiscale is ba.UIScale.MEDIUM else 1.0),
 41            stack_offset=(0, -15) if uiscale is ba.UIScale.SMALL else (0, 0)))
 42        self._is_double = False
 43        self._title_text = ba.textwidget(parent=self._root_widget,
 44                                         position=(self._width * 0.5,
 45                                                   self._height - 30),
 46                                         size=(0, 0),
 47                                         text=header_text,
 48                                         h_align='center',
 49                                         v_align='center',
 50                                         maxwidth=self._width * 0.9 - 120,
 51                                         scale=1.2,
 52                                         color=(1, 0.8, 0.3, 1))
 53        size = get_store_item_display_size(items[0])
 54        display: dict[str, Any] = {}
 55        storeitemui.instantiate_store_item_display(
 56            items[0],
 57            display,
 58            parent_widget=self._root_widget,
 59            b_pos=(self._width * 0.5 - size[0] * 0.5 + 10 -
 60                   ((size[0] * 0.5 + 30) if self._is_double else 0),
 61                   self._height * 0.5 - size[1] * 0.5 + 30 +
 62                   (20 if self._is_double else 0)),
 63            b_width=size[0],
 64            b_height=size[1],
 65            button=False)
 66
 67        # Wire up the parts we need.
 68        if self._is_double:
 69            pass  # not working
 70        else:
 71            if self._items == ['pro']:
 72                price_str = _ba.get_price(self._items[0])
 73                pyoffs = -15
 74            else:
 75                pyoffs = 0
 76                price = self._price = _ba.get_v1_account_misc_read_val(
 77                    'price.' + str(items[0]), -1)
 78                price_str = ba.charstr(ba.SpecialChar.TICKET) + str(price)
 79            self._price_text = ba.textwidget(parent=self._root_widget,
 80                                             position=(self._width * 0.5,
 81                                                       150 + pyoffs),
 82                                             size=(0, 0),
 83                                             text=price_str,
 84                                             h_align='center',
 85                                             v_align='center',
 86                                             maxwidth=self._width * 0.9,
 87                                             scale=1.4,
 88                                             color=(0.2, 1, 0.2))
 89
 90        self._update_timer = ba.Timer(1.0,
 91                                      ba.WeakCall(self._update),
 92                                      timetype=ba.TimeType.REAL,
 93                                      repeat=True)
 94
 95        self._cancel_button = ba.buttonwidget(
 96            parent=self._root_widget,
 97            position=(50, 40),
 98            size=(150, 60),
 99            scale=1.0,
100            on_activate_call=self._cancel,
101            autoselect=True,
102            label=ba.Lstr(resource='cancelText'))
103        self._purchase_button = ba.buttonwidget(
104            parent=self._root_widget,
105            position=(self._width - 200, 40),
106            size=(150, 60),
107            scale=1.0,
108            on_activate_call=self._purchase,
109            autoselect=True,
110            label=ba.Lstr(resource='store.purchaseText'))
111
112        ba.containerwidget(edit=self._root_widget,
113                           cancel_button=self._cancel_button,
114                           start_button=self._purchase_button,
115                           selected_child=self._purchase_button)
116
117    def _update(self) -> None:
118        can_die = False
119
120        # We go away if we see that our target item is owned.
121        if self._items == ['pro']:
122            if ba.app.accounts_v1.have_pro():
123                can_die = True
124        else:
125            if _ba.get_purchased(self._items[0]):
126                can_die = True
127
128        if can_die:
129            ba.containerwidget(edit=self._root_widget, transition='out_left')
130
131    def _purchase(self) -> None:
132        from bastd.ui import getcurrency
133        if self._items == ['pro']:
134            _ba.purchase('pro')
135        else:
136            ticket_count: int | None
137            try:
138                ticket_count = _ba.get_v1_account_ticket_count()
139            except Exception:
140                ticket_count = None
141            if ticket_count is not None and ticket_count < self._price:
142                getcurrency.show_get_tickets_prompt()
143                ba.playsound(ba.getsound('error'))
144                return
145
146            def do_it() -> None:
147                _ba.in_game_purchase(self._items[0], self._price)
148
149            ba.playsound(ba.getsound('swish'))
150            do_it()
151
152    def _cancel(self) -> None:
153        ba.containerwidget(edit=self._root_widget, transition='out_right')

Window for purchasing one or more items.

PurchaseWindow( items: list[str], transition: str = 'in_right', header_text: ba._language.Lstr | None = None)
 20    def __init__(self,
 21                 items: list[str],
 22                 transition: str = 'in_right',
 23                 header_text: ba.Lstr | None = None):
 24        from ba.internal import get_store_item_display_size
 25        from bastd.ui.store import item as storeitemui
 26        if header_text is None:
 27            header_text = ba.Lstr(resource='unlockThisText',
 28                                  fallback_resource='unlockThisInTheStoreText')
 29        if len(items) != 1:
 30            raise ValueError('expected exactly 1 item')
 31        self._items = list(items)
 32        self._width = 580
 33        self._height = 520
 34        uiscale = ba.app.ui.uiscale
 35        super().__init__(root_widget=ba.containerwidget(
 36            size=(self._width, self._height),
 37            transition=transition,
 38            toolbar_visibility='menu_currency',
 39            scale=(1.2 if uiscale is ba.UIScale.SMALL else
 40                   1.1 if uiscale is ba.UIScale.MEDIUM else 1.0),
 41            stack_offset=(0, -15) if uiscale is ba.UIScale.SMALL else (0, 0)))
 42        self._is_double = False
 43        self._title_text = ba.textwidget(parent=self._root_widget,
 44                                         position=(self._width * 0.5,
 45                                                   self._height - 30),
 46                                         size=(0, 0),
 47                                         text=header_text,
 48                                         h_align='center',
 49                                         v_align='center',
 50                                         maxwidth=self._width * 0.9 - 120,
 51                                         scale=1.2,
 52                                         color=(1, 0.8, 0.3, 1))
 53        size = get_store_item_display_size(items[0])
 54        display: dict[str, Any] = {}
 55        storeitemui.instantiate_store_item_display(
 56            items[0],
 57            display,
 58            parent_widget=self._root_widget,
 59            b_pos=(self._width * 0.5 - size[0] * 0.5 + 10 -
 60                   ((size[0] * 0.5 + 30) if self._is_double else 0),
 61                   self._height * 0.5 - size[1] * 0.5 + 30 +
 62                   (20 if self._is_double else 0)),
 63            b_width=size[0],
 64            b_height=size[1],
 65            button=False)
 66
 67        # Wire up the parts we need.
 68        if self._is_double:
 69            pass  # not working
 70        else:
 71            if self._items == ['pro']:
 72                price_str = _ba.get_price(self._items[0])
 73                pyoffs = -15
 74            else:
 75                pyoffs = 0
 76                price = self._price = _ba.get_v1_account_misc_read_val(
 77                    'price.' + str(items[0]), -1)
 78                price_str = ba.charstr(ba.SpecialChar.TICKET) + str(price)
 79            self._price_text = ba.textwidget(parent=self._root_widget,
 80                                             position=(self._width * 0.5,
 81                                                       150 + pyoffs),
 82                                             size=(0, 0),
 83                                             text=price_str,
 84                                             h_align='center',
 85                                             v_align='center',
 86                                             maxwidth=self._width * 0.9,
 87                                             scale=1.4,
 88                                             color=(0.2, 1, 0.2))
 89
 90        self._update_timer = ba.Timer(1.0,
 91                                      ba.WeakCall(self._update),
 92                                      timetype=ba.TimeType.REAL,
 93                                      repeat=True)
 94
 95        self._cancel_button = ba.buttonwidget(
 96            parent=self._root_widget,
 97            position=(50, 40),
 98            size=(150, 60),
 99            scale=1.0,
100            on_activate_call=self._cancel,
101            autoselect=True,
102            label=ba.Lstr(resource='cancelText'))
103        self._purchase_button = ba.buttonwidget(
104            parent=self._root_widget,
105            position=(self._width - 200, 40),
106            size=(150, 60),
107            scale=1.0,
108            on_activate_call=self._purchase,
109            autoselect=True,
110            label=ba.Lstr(resource='store.purchaseText'))
111
112        ba.containerwidget(edit=self._root_widget,
113                           cancel_button=self._cancel_button,
114                           start_button=self._purchase_button,
115                           selected_child=self._purchase_button)
Inherited Members
ba.ui.Window
get_root_widget