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