bauiv1lib.appinvite
UI functionality related to inviting people to try the game.
1# Released under the MIT License. See LICENSE for details. 2# 3"""UI functionality related to inviting people to try the game.""" 4 5from __future__ import annotations 6 7import copy 8import time 9from typing import TYPE_CHECKING 10 11import bauiv1 as bui 12 13if TYPE_CHECKING: 14 from typing import Any 15 16 17class ShowFriendCodeWindow(bui.Window): 18 """Window showing a code for sharing with friends.""" 19 20 def __init__(self, data: dict[str, Any]): 21 bui.set_analytics_screen('Friend Promo Code') 22 self._width = 650 23 self._height = 400 24 assert bui.app.classic is not None 25 uiscale = bui.app.ui_v1.uiscale 26 super().__init__( 27 root_widget=bui.containerwidget( 28 size=(self._width, self._height), 29 color=(0.45, 0.63, 0.15), 30 transition='in_scale', 31 scale=( 32 1.7 33 if uiscale is bui.UIScale.SMALL 34 else 1.35 35 if uiscale is bui.UIScale.MEDIUM 36 else 1.0 37 ), 38 ) 39 ) 40 self._data = copy.deepcopy(data) 41 bui.getsound('cashRegister').play() 42 bui.getsound('swish').play() 43 44 self._cancel_button = bui.buttonwidget( 45 parent=self._root_widget, 46 scale=0.7, 47 position=(50, self._height - 50), 48 size=(60, 60), 49 label='', 50 on_activate_call=self.close, 51 autoselect=True, 52 color=(0.45, 0.63, 0.15), 53 icon=bui.gettexture('crossOut'), 54 iconscale=1.2, 55 ) 56 bui.containerwidget( 57 edit=self._root_widget, cancel_button=self._cancel_button 58 ) 59 60 bui.textwidget( 61 parent=self._root_widget, 62 position=(self._width * 0.5, self._height * 0.8), 63 size=(0, 0), 64 color=bui.app.ui_v1.infotextcolor, 65 scale=1.0, 66 flatness=1.0, 67 h_align='center', 68 v_align='center', 69 text=bui.Lstr(resource='gatherWindow.shareThisCodeWithFriendsText'), 70 maxwidth=self._width * 0.85, 71 ) 72 73 bui.textwidget( 74 parent=self._root_widget, 75 position=(self._width * 0.5, self._height * 0.645), 76 size=(0, 0), 77 color=(1.0, 3.0, 1.0), 78 scale=2.0, 79 h_align='center', 80 v_align='center', 81 text=data['code'], 82 maxwidth=self._width * 0.85, 83 ) 84 85 award_str: str | bui.Lstr | None 86 if self._data['awardTickets'] != 0: 87 award_str = bui.Lstr( 88 resource='gatherWindow.friendPromoCodeAwardText', 89 subs=[('${COUNT}', str(self._data['awardTickets']))], 90 ) 91 else: 92 award_str = '' 93 bui.textwidget( 94 parent=self._root_widget, 95 position=(self._width * 0.5, self._height * 0.37), 96 size=(0, 0), 97 color=bui.app.ui_v1.infotextcolor, 98 scale=1.0, 99 flatness=1.0, 100 h_align='center', 101 v_align='center', 102 text=bui.Lstr( 103 value='${A}\n${B}\n${C}\n${D}', 104 subs=[ 105 ( 106 '${A}', 107 bui.Lstr( 108 resource=( 109 'gatherWindow.friendPromoCodeRedeemLongText' 110 ), 111 subs=[ 112 ('${COUNT}', str(self._data['tickets'])), 113 ( 114 '${MAX_USES}', 115 str(self._data['usesRemaining']), 116 ), 117 ], 118 ), 119 ), 120 ( 121 '${B}', 122 bui.Lstr( 123 resource=( 124 'gatherWindow.friendPromoCodeWhereToEnterText' 125 ) 126 ), 127 ), 128 ('${C}', award_str), 129 ( 130 '${D}', 131 bui.Lstr( 132 resource='gatherWindow.friendPromoCodeExpireText', 133 subs=[ 134 ( 135 '${EXPIRE_HOURS}', 136 str(self._data['expireHours']), 137 ) 138 ], 139 ), 140 ), 141 ], 142 ), 143 maxwidth=self._width * 0.9, 144 max_height=self._height * 0.35, 145 ) 146 147 if bui.is_browser_likely_available(): 148 xoffs = 0 149 bui.buttonwidget( 150 parent=self._root_widget, 151 size=(200, 40), 152 position=(self._width * 0.5 - 100 + xoffs, 39), 153 autoselect=True, 154 label=bui.Lstr(resource='gatherWindow.emailItText'), 155 on_activate_call=bui.WeakCall(self._email), 156 ) 157 158 def _email(self) -> None: 159 import urllib.parse 160 161 plus = bui.app.plus 162 assert plus is not None 163 164 # If somehow we got signed out. 165 if plus.get_v1_account_state() != 'signed_in': 166 bui.screenmessage( 167 bui.Lstr(resource='notSignedInText'), color=(1, 0, 0) 168 ) 169 bui.getsound('error').play() 170 return 171 172 bui.set_analytics_screen('Email Friend Code') 173 subject = ( 174 bui.Lstr(resource='gatherWindow.friendHasSentPromoCodeText') 175 .evaluate() 176 .replace('${NAME}', plus.get_v1_account_name()) 177 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 178 .replace('${COUNT}', str(self._data['tickets'])) 179 ) 180 body = ( 181 bui.Lstr(resource='gatherWindow.youHaveBeenSentAPromoCodeText') 182 .evaluate() 183 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 184 + '\n\n' 185 + str(self._data['code']) 186 + '\n\n' 187 ) 188 body += ( 189 ( 190 bui.Lstr(resource='gatherWindow.friendPromoCodeRedeemShortText') 191 .evaluate() 192 .replace('${COUNT}', str(self._data['tickets'])) 193 ) 194 + '\n\n' 195 + bui.Lstr(resource='gatherWindow.friendPromoCodeInstructionsText') 196 .evaluate() 197 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 198 + '\n' 199 + bui.Lstr(resource='gatherWindow.friendPromoCodeExpireText') 200 .evaluate() 201 .replace('${EXPIRE_HOURS}', str(self._data['expireHours'])) 202 + '\n' 203 + bui.Lstr(resource='enjoyText').evaluate() 204 ) 205 bui.open_url( 206 'mailto:?subject=' 207 + urllib.parse.quote(subject) 208 + '&body=' 209 + urllib.parse.quote(body) 210 ) 211 212 def close(self) -> None: 213 """Close the window.""" 214 bui.containerwidget(edit=self._root_widget, transition='out_scale') 215 216 217def handle_app_invites_press() -> None: 218 """(internal)""" 219 app = bui.app 220 plus = app.plus 221 assert plus is not None 222 223 bui.screenmessage( 224 bui.Lstr(resource='gatherWindow.requestingAPromoCodeText'), 225 color=(0, 1, 0), 226 ) 227 228 def handle_result(result: dict[str, Any] | None) -> None: 229 if result is None: 230 bui.screenmessage(bui.Lstr(resource='errorText'), color=(1, 0, 0)) 231 bui.getsound('error').play() 232 else: 233 ShowFriendCodeWindow(result) 234 235 plus.add_v1_account_transaction( 236 { 237 'type': 'FRIEND_PROMO_CODE_REQUEST', 238 'ali': False, 239 'expire_time': time.time() + 10, 240 }, 241 callback=handle_result, 242 ) 243 plus.run_v1_account_transactions()
class
ShowFriendCodeWindow(bauiv1._uitypes.Window):
18class ShowFriendCodeWindow(bui.Window): 19 """Window showing a code for sharing with friends.""" 20 21 def __init__(self, data: dict[str, Any]): 22 bui.set_analytics_screen('Friend Promo Code') 23 self._width = 650 24 self._height = 400 25 assert bui.app.classic is not None 26 uiscale = bui.app.ui_v1.uiscale 27 super().__init__( 28 root_widget=bui.containerwidget( 29 size=(self._width, self._height), 30 color=(0.45, 0.63, 0.15), 31 transition='in_scale', 32 scale=( 33 1.7 34 if uiscale is bui.UIScale.SMALL 35 else 1.35 36 if uiscale is bui.UIScale.MEDIUM 37 else 1.0 38 ), 39 ) 40 ) 41 self._data = copy.deepcopy(data) 42 bui.getsound('cashRegister').play() 43 bui.getsound('swish').play() 44 45 self._cancel_button = bui.buttonwidget( 46 parent=self._root_widget, 47 scale=0.7, 48 position=(50, self._height - 50), 49 size=(60, 60), 50 label='', 51 on_activate_call=self.close, 52 autoselect=True, 53 color=(0.45, 0.63, 0.15), 54 icon=bui.gettexture('crossOut'), 55 iconscale=1.2, 56 ) 57 bui.containerwidget( 58 edit=self._root_widget, cancel_button=self._cancel_button 59 ) 60 61 bui.textwidget( 62 parent=self._root_widget, 63 position=(self._width * 0.5, self._height * 0.8), 64 size=(0, 0), 65 color=bui.app.ui_v1.infotextcolor, 66 scale=1.0, 67 flatness=1.0, 68 h_align='center', 69 v_align='center', 70 text=bui.Lstr(resource='gatherWindow.shareThisCodeWithFriendsText'), 71 maxwidth=self._width * 0.85, 72 ) 73 74 bui.textwidget( 75 parent=self._root_widget, 76 position=(self._width * 0.5, self._height * 0.645), 77 size=(0, 0), 78 color=(1.0, 3.0, 1.0), 79 scale=2.0, 80 h_align='center', 81 v_align='center', 82 text=data['code'], 83 maxwidth=self._width * 0.85, 84 ) 85 86 award_str: str | bui.Lstr | None 87 if self._data['awardTickets'] != 0: 88 award_str = bui.Lstr( 89 resource='gatherWindow.friendPromoCodeAwardText', 90 subs=[('${COUNT}', str(self._data['awardTickets']))], 91 ) 92 else: 93 award_str = '' 94 bui.textwidget( 95 parent=self._root_widget, 96 position=(self._width * 0.5, self._height * 0.37), 97 size=(0, 0), 98 color=bui.app.ui_v1.infotextcolor, 99 scale=1.0, 100 flatness=1.0, 101 h_align='center', 102 v_align='center', 103 text=bui.Lstr( 104 value='${A}\n${B}\n${C}\n${D}', 105 subs=[ 106 ( 107 '${A}', 108 bui.Lstr( 109 resource=( 110 'gatherWindow.friendPromoCodeRedeemLongText' 111 ), 112 subs=[ 113 ('${COUNT}', str(self._data['tickets'])), 114 ( 115 '${MAX_USES}', 116 str(self._data['usesRemaining']), 117 ), 118 ], 119 ), 120 ), 121 ( 122 '${B}', 123 bui.Lstr( 124 resource=( 125 'gatherWindow.friendPromoCodeWhereToEnterText' 126 ) 127 ), 128 ), 129 ('${C}', award_str), 130 ( 131 '${D}', 132 bui.Lstr( 133 resource='gatherWindow.friendPromoCodeExpireText', 134 subs=[ 135 ( 136 '${EXPIRE_HOURS}', 137 str(self._data['expireHours']), 138 ) 139 ], 140 ), 141 ), 142 ], 143 ), 144 maxwidth=self._width * 0.9, 145 max_height=self._height * 0.35, 146 ) 147 148 if bui.is_browser_likely_available(): 149 xoffs = 0 150 bui.buttonwidget( 151 parent=self._root_widget, 152 size=(200, 40), 153 position=(self._width * 0.5 - 100 + xoffs, 39), 154 autoselect=True, 155 label=bui.Lstr(resource='gatherWindow.emailItText'), 156 on_activate_call=bui.WeakCall(self._email), 157 ) 158 159 def _email(self) -> None: 160 import urllib.parse 161 162 plus = bui.app.plus 163 assert plus is not None 164 165 # If somehow we got signed out. 166 if plus.get_v1_account_state() != 'signed_in': 167 bui.screenmessage( 168 bui.Lstr(resource='notSignedInText'), color=(1, 0, 0) 169 ) 170 bui.getsound('error').play() 171 return 172 173 bui.set_analytics_screen('Email Friend Code') 174 subject = ( 175 bui.Lstr(resource='gatherWindow.friendHasSentPromoCodeText') 176 .evaluate() 177 .replace('${NAME}', plus.get_v1_account_name()) 178 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 179 .replace('${COUNT}', str(self._data['tickets'])) 180 ) 181 body = ( 182 bui.Lstr(resource='gatherWindow.youHaveBeenSentAPromoCodeText') 183 .evaluate() 184 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 185 + '\n\n' 186 + str(self._data['code']) 187 + '\n\n' 188 ) 189 body += ( 190 ( 191 bui.Lstr(resource='gatherWindow.friendPromoCodeRedeemShortText') 192 .evaluate() 193 .replace('${COUNT}', str(self._data['tickets'])) 194 ) 195 + '\n\n' 196 + bui.Lstr(resource='gatherWindow.friendPromoCodeInstructionsText') 197 .evaluate() 198 .replace('${APP_NAME}', bui.Lstr(resource='titleText').evaluate()) 199 + '\n' 200 + bui.Lstr(resource='gatherWindow.friendPromoCodeExpireText') 201 .evaluate() 202 .replace('${EXPIRE_HOURS}', str(self._data['expireHours'])) 203 + '\n' 204 + bui.Lstr(resource='enjoyText').evaluate() 205 ) 206 bui.open_url( 207 'mailto:?subject=' 208 + urllib.parse.quote(subject) 209 + '&body=' 210 + urllib.parse.quote(body) 211 ) 212 213 def close(self) -> None: 214 """Close the window.""" 215 bui.containerwidget(edit=self._root_widget, transition='out_scale')
Window showing a code for sharing with friends.
ShowFriendCodeWindow(data: dict[str, typing.Any])
21 def __init__(self, data: dict[str, Any]): 22 bui.set_analytics_screen('Friend Promo Code') 23 self._width = 650 24 self._height = 400 25 assert bui.app.classic is not None 26 uiscale = bui.app.ui_v1.uiscale 27 super().__init__( 28 root_widget=bui.containerwidget( 29 size=(self._width, self._height), 30 color=(0.45, 0.63, 0.15), 31 transition='in_scale', 32 scale=( 33 1.7 34 if uiscale is bui.UIScale.SMALL 35 else 1.35 36 if uiscale is bui.UIScale.MEDIUM 37 else 1.0 38 ), 39 ) 40 ) 41 self._data = copy.deepcopy(data) 42 bui.getsound('cashRegister').play() 43 bui.getsound('swish').play() 44 45 self._cancel_button = bui.buttonwidget( 46 parent=self._root_widget, 47 scale=0.7, 48 position=(50, self._height - 50), 49 size=(60, 60), 50 label='', 51 on_activate_call=self.close, 52 autoselect=True, 53 color=(0.45, 0.63, 0.15), 54 icon=bui.gettexture('crossOut'), 55 iconscale=1.2, 56 ) 57 bui.containerwidget( 58 edit=self._root_widget, cancel_button=self._cancel_button 59 ) 60 61 bui.textwidget( 62 parent=self._root_widget, 63 position=(self._width * 0.5, self._height * 0.8), 64 size=(0, 0), 65 color=bui.app.ui_v1.infotextcolor, 66 scale=1.0, 67 flatness=1.0, 68 h_align='center', 69 v_align='center', 70 text=bui.Lstr(resource='gatherWindow.shareThisCodeWithFriendsText'), 71 maxwidth=self._width * 0.85, 72 ) 73 74 bui.textwidget( 75 parent=self._root_widget, 76 position=(self._width * 0.5, self._height * 0.645), 77 size=(0, 0), 78 color=(1.0, 3.0, 1.0), 79 scale=2.0, 80 h_align='center', 81 v_align='center', 82 text=data['code'], 83 maxwidth=self._width * 0.85, 84 ) 85 86 award_str: str | bui.Lstr | None 87 if self._data['awardTickets'] != 0: 88 award_str = bui.Lstr( 89 resource='gatherWindow.friendPromoCodeAwardText', 90 subs=[('${COUNT}', str(self._data['awardTickets']))], 91 ) 92 else: 93 award_str = '' 94 bui.textwidget( 95 parent=self._root_widget, 96 position=(self._width * 0.5, self._height * 0.37), 97 size=(0, 0), 98 color=bui.app.ui_v1.infotextcolor, 99 scale=1.0, 100 flatness=1.0, 101 h_align='center', 102 v_align='center', 103 text=bui.Lstr( 104 value='${A}\n${B}\n${C}\n${D}', 105 subs=[ 106 ( 107 '${A}', 108 bui.Lstr( 109 resource=( 110 'gatherWindow.friendPromoCodeRedeemLongText' 111 ), 112 subs=[ 113 ('${COUNT}', str(self._data['tickets'])), 114 ( 115 '${MAX_USES}', 116 str(self._data['usesRemaining']), 117 ), 118 ], 119 ), 120 ), 121 ( 122 '${B}', 123 bui.Lstr( 124 resource=( 125 'gatherWindow.friendPromoCodeWhereToEnterText' 126 ) 127 ), 128 ), 129 ('${C}', award_str), 130 ( 131 '${D}', 132 bui.Lstr( 133 resource='gatherWindow.friendPromoCodeExpireText', 134 subs=[ 135 ( 136 '${EXPIRE_HOURS}', 137 str(self._data['expireHours']), 138 ) 139 ], 140 ), 141 ), 142 ], 143 ), 144 maxwidth=self._width * 0.9, 145 max_height=self._height * 0.35, 146 ) 147 148 if bui.is_browser_likely_available(): 149 xoffs = 0 150 bui.buttonwidget( 151 parent=self._root_widget, 152 size=(200, 40), 153 position=(self._width * 0.5 - 100 + xoffs, 39), 154 autoselect=True, 155 label=bui.Lstr(resource='gatherWindow.emailItText'), 156 on_activate_call=bui.WeakCall(self._email), 157 )
def
close(self) -> None:
213 def close(self) -> None: 214 """Close the window.""" 215 bui.containerwidget(edit=self._root_widget, transition='out_scale')
Close the window.
Inherited Members
- bauiv1._uitypes.Window
- get_root_widget