def _update_tabs(self) -> None: from ba.internal import (get_available_sale_time, get_available_purchase_count) if not self._root_widget: return for tab_name, tab_data in list( self._purchasable_count_widgets.items()): sale_time = get_available_sale_time(tab_name) if sale_time is not None: ba.textwidget(edit=tab_data['sale_title_text'], text=ba.Lstr(resource='store.saleText')) ba.textwidget(edit=tab_data['sale_time_text'], text=ba.timestring( sale_time, centi=False, timeformat=ba.TimeFormat.MILLISECONDS)) ba.imagewidget(edit=tab_data['sale_img'], opacity=1.0) count = 0 else: ba.textwidget(edit=tab_data['sale_title_text'], text='') ba.textwidget(edit=tab_data['sale_time_text'], text='') ba.imagewidget(edit=tab_data['sale_img'], opacity=0.0) count = get_available_purchase_count(tab_name) if count > 0: ba.textwidget(edit=tab_data['text'], text=str(count)) ba.imagewidget(edit=tab_data['img'], opacity=1.0) else: ba.textwidget(edit=tab_data['text'], text='') ba.imagewidget(edit=tab_data['img'], opacity=0.0)
def buy(self, item: str) -> None: """Attempt to purchase the provided item.""" from ba.internal import (get_available_sale_time, get_store_item_name_translated) from bastd.ui import account from bastd.ui.confirm import ConfirmWindow from bastd.ui import getcurrency # Prevent pressing buy within a few seconds of the last press # (gives the buttons time to disable themselves and whatnot). curtime = ba.time(ba.TimeType.REAL) if self._last_buy_time is not None and (curtime - self._last_buy_time) < 2.0: ba.playsound(ba.getsound('error')) else: if _ba.get_account_state() != 'signed_in': account.show_sign_in_prompt() else: self._last_buy_time = curtime # Pro is an actual IAP; the rest are ticket purchases. if item == 'pro': ba.playsound(ba.getsound('click01')) # Purchase either pro or pro_sale depending on whether # there is a sale going on. self._do_purchase_check('pro' if get_available_sale_time( 'extras') is None else 'pro_sale') else: price = _ba.get_account_misc_read_val( 'price.' + item, None) our_tickets = _ba.get_account_ticket_count() if price is not None and our_tickets < price: ba.playsound(ba.getsound('error')) getcurrency.show_get_tickets_prompt() else: def do_it() -> None: self._do_purchase_check(item, is_ticket_purchase=True) ba.playsound(ba.getsound('swish')) ConfirmWindow( ba.Lstr(resource='store.purchaseConfirmText', subs=[ ('${ITEM}', get_store_item_name_translated(item)) ]), width=400, height=120, action=do_it, ok_text=ba.Lstr(resource='store.purchaseText', fallback_resource='okText'))
def update_buttons(self) -> None: """Update our buttons.""" # pylint: disable=too-many-statements # pylint: disable=too-many-branches # pylint: disable=too-many-locals from ba.internal import have_pro, get_available_sale_time from ba import SpecialChar if not self._root_widget: return import datetime sales_raw = _ba.get_account_misc_read_val('sales', {}) sales = {} try: # Look at the current set of sales; filter any with time remaining. for sale_item, sale_info in list(sales_raw.items()): to_end = (datetime.datetime.utcfromtimestamp(sale_info['e']) - datetime.datetime.utcnow()).total_seconds() if to_end > 0: sales[sale_item] = { 'to_end': to_end, 'original_price': sale_info['op'] } except Exception: ba.print_exception('Error parsing sales.') assert self.button_infos is not None for b_type, b_info in self.button_infos.items(): if b_type in ['upgrades.pro', 'pro']: purchased = have_pro() else: purchased = _ba.get_purchased(b_type) sale_opacity = 0.0 sale_title_text: Union[str, ba.Lstr] = '' sale_time_text: Union[str, ba.Lstr] = '' if purchased: title_color = (0.8, 0.7, 0.9, 1.0) color = (0.63, 0.55, 0.78) extra_image_opacity = 0.5 call = ba.WeakCall(self._print_already_own, b_info['name']) price_text = '' price_text_left = '' price_text_right = '' show_purchase_check = True description_color: Sequence[float] = (0.4, 1.0, 0.4, 0.4) description_color2: Sequence[float] = (0.0, 0.0, 0.0, 0.0) price_color = (0.5, 1, 0.5, 0.3) else: title_color = (0.7, 0.9, 0.7, 1.0) color = (0.4, 0.8, 0.1) extra_image_opacity = 1.0 call = b_info['call'] if 'call' in b_info else None if b_type in ['upgrades.pro', 'pro']: sale_time = get_available_sale_time('extras') if sale_time is not None: priceraw = _ba.get_price('pro') price_text_left = (priceraw if priceraw is not None else '?') priceraw = _ba.get_price('pro_sale') price_text_right = (priceraw if priceraw is not None else '?') sale_opacity = 1.0 price_text = '' sale_title_text = ba.Lstr(resource='store.saleText') sale_time_text = ba.timestring( sale_time, centi=False, timeformat=ba.TimeFormat.MILLISECONDS) else: priceraw = _ba.get_price('pro') price_text = priceraw if priceraw is not None else '?' price_text_left = '' price_text_right = '' else: price = _ba.get_account_misc_read_val('price.' + b_type, 0) # Color the button differently if we cant afford this. if _ba.get_account_state() == 'signed_in': if _ba.get_account_ticket_count() < price: color = (0.6, 0.61, 0.6) price_text = ba.charstr(ba.SpecialChar.TICKET) + str( _ba.get_account_misc_read_val('price.' + b_type, '?')) price_text_left = '' price_text_right = '' # TESTING: if b_type in sales: sale_opacity = 1.0 price_text_left = ba.charstr(SpecialChar.TICKET) + str( sales[b_type]['original_price']) price_text_right = price_text price_text = '' sale_title_text = ba.Lstr(resource='store.saleText') sale_time_text = ba.timestring( int(sales[b_type]['to_end'] * 1000), centi=False, timeformat=ba.TimeFormat.MILLISECONDS) description_color = (0.5, 1.0, 0.5) description_color2 = (0.3, 1.0, 1.0) price_color = (0.2, 1, 0.2, 1.0) show_purchase_check = False if 'title_text' in b_info: ba.textwidget(edit=b_info['title_text'], color=title_color) if 'purchase_check' in b_info: ba.imagewidget(edit=b_info['purchase_check'], opacity=1.0 if show_purchase_check else 0.0) if 'price_widget' in b_info: ba.textwidget(edit=b_info['price_widget'], text=price_text, color=price_color) if 'price_widget_left' in b_info: ba.textwidget(edit=b_info['price_widget_left'], text=price_text_left) if 'price_widget_right' in b_info: ba.textwidget(edit=b_info['price_widget_right'], text=price_text_right) if 'price_slash_widget' in b_info: ba.imagewidget(edit=b_info['price_slash_widget'], opacity=sale_opacity) if 'sale_bg_widget' in b_info: ba.imagewidget(edit=b_info['sale_bg_widget'], opacity=sale_opacity) if 'sale_title_widget' in b_info: ba.textwidget(edit=b_info['sale_title_widget'], text=sale_title_text) if 'sale_time_widget' in b_info: ba.textwidget(edit=b_info['sale_time_widget'], text=sale_time_text) if 'button' in b_info: ba.buttonwidget(edit=b_info['button'], color=color, on_activate_call=call) if 'extra_backings' in b_info: for bck in b_info['extra_backings']: ba.imagewidget(edit=bck, color=color, opacity=extra_image_opacity) if 'extra_images' in b_info: for img in b_info['extra_images']: ba.imagewidget(edit=img, opacity=extra_image_opacity) if 'extra_texts' in b_info: for etxt in b_info['extra_texts']: ba.textwidget(edit=etxt, color=description_color) if 'extra_texts_2' in b_info: for etxt in b_info['extra_texts_2']: ba.textwidget(edit=etxt, color=description_color2) if 'descriptionText' in b_info: ba.textwidget(edit=b_info['descriptionText'], color=description_color)
def _update(self) -> None: # pylint: disable=too-many-branches from ba import SpecialChar, TimeFormat from ba.internal import (get_available_sale_time, get_available_purchase_count) if not self._button: return # Our instance may outlive our UI objects. if self._ticket_text is not None: if _ba.get_account_state() == 'signed_in': sval = ba.charstr(SpecialChar.TICKET) + str( _ba.get_account_ticket_count()) else: sval = '-' ba.textwidget(edit=self._ticket_text, text=sval) available_purchases = get_available_purchase_count() # Old pro sale stuff.. sale_time = get_available_sale_time('extras') # ..also look for new style sales. if sale_time is None: import datetime sales_raw = _ba.get_account_misc_read_val('sales', {}) sale_times = [] try: # Look at the current set of sales; filter any with time # remaining that we don't own. for sale_item, sale_info in list(sales_raw.items()): if not _ba.get_purchased(sale_item): to_end = (datetime.datetime.utcfromtimestamp( sale_info['e']) - datetime.datetime.utcnow()).total_seconds() if to_end > 0: sale_times.append(to_end) except Exception: ba.print_exception('Error parsing sales') if sale_times: sale_time = int(min(sale_times) * 1000) if sale_time is not None: ba.textwidget(edit=self._sale_title_text, text=ba.Lstr(resource='store.saleText')) ba.textwidget(edit=self._sale_time_text, text=ba.timestring( sale_time, centi=False, timeformat=TimeFormat.MILLISECONDS)) ba.imagewidget(edit=self._sale_backing, opacity=1.0) ba.imagewidget(edit=self._available_purchase_backing, opacity=1.0) ba.textwidget(edit=self._available_purchase_text, text='') ba.imagewidget(edit=self._available_purchase_backing, opacity=0.0) else: ba.imagewidget(edit=self._sale_backing, opacity=0.0) ba.textwidget(edit=self._sale_time_text, text='') ba.textwidget(edit=self._sale_title_text, text='') if available_purchases > 0: ba.textwidget(edit=self._available_purchase_text, text=str(available_purchases)) ba.imagewidget(edit=self._available_purchase_backing, opacity=1.0) else: ba.textwidget(edit=self._available_purchase_text, text='') ba.imagewidget(edit=self._available_purchase_backing, opacity=0.0)