def __init__(self, submitterParams, key):
     self.__submitter = self._getSubmitterType()(submitterParams)
     self.onInvalidate = Event.Event()
     self.onCloseDialog = Event.Event()
     self.colorManager = ColorSchemeManager()
     super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
     self.wallet.onWalletStatusChanged += self._onStatsChanged
Exemple #2
0
 def __init__(self, typeCompactDescr, key):
     self.__typeCompactDescr = typeCompactDescr
     self.onInvalidate = Event.Event()
     self.onCloseDialog = Event.Event()
     self.colorManager = ColorSchemeManager()
     super(_ExchangeDialogMeta,
           self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
     self.wallet.onWalletStatusChanged += self._onStatsChanged
Exemple #3
0
 def __init__(self, key, typeCompactDescr):
     self.__typeCompactDescr = typeCompactDescr
     self.onInvalidate = Event.Event()
     self.onCloseDialog = Event.Event()
     self._items = g_itemsCache.items
     self.colorManager = ColorSchemeManager()
     super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
     game_control.g_instance.wallet.onWalletStatusChanged += self._onStatsChanged
 def __init__(self, key, typeCompactDescr):
     self.__typeCompactDescr = typeCompactDescr
     self.onInvalidate = Event.Event()
     self.onCloseDialog = Event.Event()
     self._items = g_itemsCache.items
     self.colorManager = ColorSchemeManager()
     super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
     game_control.g_instance.wallet.onWalletStatusChanged += self._onStatsChanged
Exemple #5
0
 def _createManagers(self):
     super(LobbyEntry, self)._createManagers()
     self._varsMgr = GlobalVarsManager()
     self._soundMgr = SoundManager()
     self._colorSchemeMgr = ColorSchemeManager()
     self._eventLogMgr = EventLogManager()
     self._contextMgr = ContextMenuManager(self.proxy)
     self._popoverManager = PopoverManager(EVENT_BUS_SCOPE.LOBBY)
     self._guiItemsMgr = GuiItemsManager()
     self._voiceChatMgr = VoiceChatManager(self.proxy)
     self._utilsMgr = UtilsManager()
     self._tweenMgr = TweenManager()
     self._gameInputMgr = GameInputMgr()
     self._cacheMgr = CacheManager()
     self._tutorialMgr = TutorialManager(True, 'gui/tutorial-lobby-gui.xml')
class _ExchangeDialogMeta(I18nConfirmDialogMeta):
    itemsCache = dependency.descriptor(IItemsCache)
    wallet = dependency.descriptor(IWalletController)

    def __init__(self, submitterParams, key):
        self.__submitter = self._getSubmitterType()(submitterParams)
        self.onInvalidate = Event.Event()
        self.onCloseDialog = Event.Event()
        self.colorManager = ColorSchemeManager()
        super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
        self.wallet.onWalletStatusChanged += self._onStatsChanged

    def destroy(self):
        self.wallet.onWalletStatusChanged -= self._onStatsChanged
        self.onInvalidate.clear()
        self.onCloseDialog.clear()
        self.__submitter.destroy()

    @async
    @process
    def submit(self, gold, valueToExchange, callback=None):
        submitter = self._getSubmitter()
        result = yield submitter.submit(gold, valueToExchange)
        if callback is not None:
            callback(result)
        return

    def getType(self):
        submitter = self._getSubmitter()
        return submitter.type

    def getEventType(self):
        return events.ShowDialogEvent.SHOW_EXCHANGE_DIALOG

    def getExchangeRate(self):
        submitter = self._getSubmitter()
        return submitter.exchangeRate

    def getTypeCompDescr(self):
        submitter = self._getSubmitter()
        return submitter.itemCD

    def makeVO(self):
        submitter = self._getSubmitter()
        item = submitter.infoItem
        resToExchange = submitter.resourceToExchange
        state, stateMsg = self._getState(resToExchange)
        return {'title': self.getTitle(),
         'exchangeBtnText': self.getButtonLabels()[0]['label'],
         'cancelBtnText': self.getButtonLabels()[1]['label'],
         'state': state,
         'lockExchangeMessage': stateMsg,
         'iconExtraInfo': item.getExtraIconInfo(),
         'iconModuleType': item.itemTypeName,
         'icon': self._getItemIcon(item),
         'iconType': self._getItemIconType(item),
         'itemName': text_styles.middleTitle(item.userName),
         'needItemsText': self._getResourceToExchangeTxt(resToExchange),
         'needGoldText': self._getGoldToExchangeTxt(resToExchange),
         'exchangeBlockData': self._getExchangeBlockData(resToExchange)}

    def _getSubmitterType(self):
        raise NotImplementedError()

    def _getSubmitter(self):
        return self.__submitter

    def _onStatsChanged(self, *args):
        self.onInvalidate()

    def _getRGB(self, colorId):
        return self.colorManager.getColorScheme(colorId).get('rgb')

    def _makeString(self, key, ctx=None):
        ctx = ctx or {}
        i18nKey = key.format(self._key)
        return super(_ExchangeDialogMeta, self)._makeString(i18nKey, ctx)

    def _getExchangeBlockData(self, resToExchange):
        submitter = self._getSubmitter()
        goldStepperTitleStr = i18n.makeString(DIALOGS.CONFIRMEXCHANGEDIALOG_GOLDITEMSSTEPPERTITLE)
        goldStepperTitleFmt = text_styles.main(goldStepperTitleStr)
        needItemsStepperTitle = text_styles.main(self._makeString(I18N_NEEDITEMSSTEPPERTITLE_KEY))
        return {'goldStepperTitle': goldStepperTitleFmt,
         'needItemsIcon': submitter.currencyIconPath,
         'needItemsStepperTitle': needItemsStepperTitle,
         'goldIcon': RES_ICONS.MAPS_ICONS_LIBRARY_GOLDICON_2,
         'defaultExchangeRate': submitter.defaultExchangeRate,
         'exchangeRate': submitter.exchangeRate,
         'defaultGoldValue': self._getGoldToExchange(resToExchange),
         'goldStepSize': STEP_SIZE,
         'maxGoldValue': submitter.maxExchangeValue,
         'goldTextColorId': TEXT_MANAGER_STYLES.GOLD_TEXT,
         'itemsTextColorId': submitter.colorScheme,
         'exchangeHeaderData': {'labelText': MENU.EXCHANGE_RATE,
                                'rateFromIcon': ICON_TEXT_FRAMES.GOLD,
                                'rateToIcon': submitter.exchangeRateItemsIcon,
                                'rateFromTextColor': self._getRGB(TEXT_COLOR_ID_XP),
                                'rateToTextColor': self._getRGB(submitter.rateToColorScheme)}}

    def _getState(self, resToExchange):
        if resToExchange <= 0:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.EXCHANGE_NOT_NEEED_STATE, text_styles.success(self._makeString(I18N_EXCHANGENONEEDTEXT_KEY)))
        if not self._isEnoughGold(resToExchange):
            goldToExchange = self._getGoldToExchange(resToExchange)
            fmtGold = ''.join((text_styles.gold(backport.getGoldFormat(goldToExchange)), icons.gold()))
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NOT_ENOUGH_GOLD_STATE, text_styles.error(self._makeString(I18N_GOLDNOTENOUGHTEXT_KEY, {'gold': fmtGold})))
        return (CONFIRM_EXCHANGE_DIALOG_TYPES.NORMAL_STATE, '')

    def _isEnoughGold(self, resToExchange):
        return self._getGoldToExchange(resToExchange) <= self.itemsCache.items.stats.gold

    def _getResourceToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            resource = backport.getIntegralFormat(resToExchange)
            submitter = self._getSubmitter()
            resStr = submitter.currencyFormat(resource) + submitter.currencyIconStr
            return text_styles.error(self._makeString(I18N_NEEDITEMSTEXT_KEY, {'value': resStr}))

    def _getGoldToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            goldToExchange = self._getGoldToExchange(resToExchange)
            fmtGold = ''.join((text_styles.gold(backport.getGoldFormat(goldToExchange)), icons.gold()))
            return text_styles.main(self._makeString(I18N_NEEDGOLDTEXT_KEY, {'gold': fmtGold}))

    def _getGoldToExchange(self, resToExchange):
        if resToExchange > 0:
            submitter = self._getSubmitter()
            return int(math.ceil(float(resToExchange) / submitter.exchangeRate))

    def _getItemIconType(self, item):
        return CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE else CONFIRM_EXCHANGE_DIALOG_TYPES.MODULE_ICON

    def _getItemIcon(self, item):
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            icon = item.type
            if item.isElite:
                icon += '_elite'
            return icon
        else:
            return item.getGUIEmblemID()
Exemple #7
0
 def _createColorSchemeManager(self):
     return ColorSchemeManager()
Exemple #8
0
class _ExchangeDialogMeta(I18nConfirmDialogMeta):
    itemsCache = dependency.descriptor(IItemsCache)
    wallet = dependency.descriptor(IWalletController)

    def __init__(self, typeCompactDescr, key):
        self.__typeCompactDescr = typeCompactDescr
        self.onInvalidate = Event.Event()
        self.onCloseDialog = Event.Event()
        self.colorManager = ColorSchemeManager()
        super(_ExchangeDialogMeta,
              self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
        self.wallet.onWalletStatusChanged += self._onStatsChanged

    def destroy(self):
        """
        destroy operation after exchange dialog close
        """
        self.wallet.onWalletStatusChanged -= self._onStatsChanged
        self.onInvalidate.clear()
        self.onCloseDialog.clear()

    def getEventType(self):
        """
        EventType to open ConfirmExchangeDialog
        :return:<str> eventType "showExchangeDialog"
        """
        return events.ShowDialogEvent.SHOW_EXCHANGE_DIALOG

    def getTypeCompDescr(self):
        """
        Gets item compact descriptor
        :return: <int> item compact descriptor
        """
        return self.__typeCompactDescr

    def submit(self, gold, valueToExchange, callback=None):
        """
        Submit exchange operation, send request to exchange and call callback after response received
        :param gold: <int> gold value
        :param valueToExchange: <int> credits or xp value to exchange
        :param callback: <function>
        """
        raise NotImplementedError()

    def getExchangeRate(self):
        """
        Get exchange rate rate for defined currency
        May return shop.exchangeRate for gold exchange
        or shop.freeXPConversion[0] for xp exchange
        :return: <int> exchange rate for defined currency
        """
        raise NotImplementedError()

    def getType(self):
        """
        :return: <str> confirm exchange dialog type for unique name for _ExchangeDialogBusinessHandler
        """
        raise NotImplementedError()

    def makeVO(self):
        """
        Makes VO for ConfirmExchangeDialog
        :return: <obj>
        """
        item = self.itemsCache.items.getItemByCD(self.getTypeCompDescr())
        resToExchange = self._getResourceToExchange()
        extraData = None
        if item.itemTypeID == GUI_ITEM_TYPE.GUN and item.isClipGun():
            extraData = CLIP_ICON_PATH
        state, stateMsg = self.__getState(resToExchange)
        return {
            'title': self.getTitle(),
            'exchangeBtnText': self.getButtonLabels()[0]['label'],
            'cancelBtnText': self.getButtonLabels()[1]['label'],
            'state': state,
            'lockExchangeMessage': stateMsg,
            'iconExtraInfo': extraData,
            'iconModuleType': item.itemTypeName,
            'icon': self.__getItemIcon(item),
            'iconType': self.__getItemIconType(item),
            'itemName': text_styles.middleTitle(item.userName),
            'needItemsText': self.__getResourceToExchangeTxt(resToExchange),
            'needGoldText': self.__getGoldToExchangeTxt(resToExchange),
            'exchangeBlockData': self.__getExchangeBlockData(resToExchange)
        }

    def _onStatsChanged(self, *args):
        """
        call onInvalidate Event on account stats changes
        """
        self.onInvalidate()

    def _getDefaultExchangeRate(self):
        """
        Gets shop default exchange rate for defined currency
        :return: <int>
        """
        raise NotImplementedError()

    def _getResourceToExchange(self):
        """
        # calculate necessary resource for exchange (credits, xp, ...)
        :return: <int>
        """
        raise NotImplementedError()

    def _getCurrencyIconStr(self):
        """
        Gets html currency icon
        :return: <str>
        """
        raise NotImplementedError()

    def _getCurrencyIconPath(self):
        """
        Gets currency icon path
        :return: <str>
        """
        raise NotImplementedError()

    def _getCurrencyFormat(self):
        """
        Gets text_style format for resource to exchange text
        :return: <function>
        """
        raise NotImplementedError()

    def _getColorScheme(self):
        """
        Gets color scheme ID for itemsText
        :return: <str>
        """
        raise NotImplementedError()

    def _getRateToColorScheme(self):
        """
        Gets color scheme ID for rateToTextColor field
        :return: <str>
        """
        raise NotImplementedError()

    def _getExchangeRateItemsIcon(self):
        """
        :return: <str> frame for  GUI rateToIcon field
        """
        raise NotImplementedError()

    def _getMaxExchangeValue(self):
        """
        Calculates max available resource value for exchange
        :return: <int>
        """
        raise NotImplementedError()

    def _getRGB(self, colorId):
        """
        :param colorId:  <str>
        :return: <obj> RGB color scheme
        """
        return self.colorManager.getColorScheme(colorId).get('rgb')

    def _makeString(self, key, ctx=None):
        """
        :param key: <str> localization key
        :param ctx: <obj> lcalisation context
        :return: <str> localized value
        """
        ctx = ctx or {}
        i18nKey = key.format(self._key)
        return super(_ExchangeDialogMeta, self)._makeString(i18nKey, ctx)

    def __getExchangeBlockData(self, resToExchange):
        """
        Makes VO for exchangeBlockData field
        :param resToExchange: <int> resource value for exchange
        :return: <obj>
        """
        goldStepperTitleStr = i18n.makeString(
            DIALOGS.CONFIRMEXCHANGEDIALOG_GOLDITEMSSTEPPERTITLE)
        goldStepperTitleFmt = text_styles.main(goldStepperTitleStr)
        exchangeHeaderData = {
            'labelText': i18n.makeString(MENU.EXCHANGE_RATE),
            'rateFromIcon': ICON_TEXT_FRAMES.GOLD,
            'rateToIcon': self._getExchangeRateItemsIcon(),
            'rateFromTextColor': self._getRGB(TEXT_COLOR_ID_XP),
            'rateToTextColor': self._getRGB(self._getRateToColorScheme())
        }
        needItemsStepperTitle = text_styles.main(
            self._makeString(I18N_NEEDITEMSSTEPPERTITLE_KEY))
        return {
            'goldStepperTitle': goldStepperTitleFmt,
            'needItemsIcon': self._getCurrencyIconPath(),
            'needItemsStepperTitle': needItemsStepperTitle,
            'goldIcon': RES_ICONS.MAPS_ICONS_LIBRARY_GOLDICON_2,
            'defaultExchangeRate': self._getDefaultExchangeRate(),
            'exchangeRate': self.getExchangeRate(),
            'defaultGoldValue': self.__getGoldToExchange(resToExchange),
            'goldStepSize': STEP_SIZE,
            'maxGoldValue': self._getMaxExchangeValue(),
            'goldTextColorId': TEXT_MANAGER_STYLES.GOLD_TEXT,
            'itemsTextColorId': self._getColorScheme(),
            'exchangeHeaderData': {
                'labelText': '',
                'rateFromIcon': ICON_TEXT_FRAMES.GOLD,
                'rateToIcon': self._getExchangeRateItemsIcon(),
                'rateFromTextColor': self._getRGB(TEXT_COLOR_ID_XP),
                'rateToTextColor': self._getRGB(self._getRateToColorScheme())
            }
        }

    def __getState(self, resToExchange):
        """
        Gets state and reason for exchange possibility
        :param resToExchange: <int> resource for exchange
        :return: <tuple(state:<int>, reason<str>)>
        """
        if resToExchange <= 0:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.EXCHANGE_NOT_NEEED_STATE,
                    text_styles.success(
                        self._makeString(I18N_EXCHANGENONEEDTEXT_KEY)))
        if not self.__isEnoughGold(resToExchange):
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = ''.join(
                (text_styles.gold(BigWorld.wg_getGoldFormat(goldToExchange)),
                 icons.gold()))
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NOT_ENOUGH_GOLD_STATE,
                    text_styles.error(
                        self._makeString(I18N_GOLDNOTENOUGHTEXT_KEY,
                                         {'gold': fmtGold})))
        return (CONFIRM_EXCHANGE_DIALOG_TYPES.NORMAL_STATE, '')

    def __isEnoughGold(self, resToExchange):
        """
        Checks if enough gold for exchange operation
        :param resToExchange: <int> resource for exchange
        :return: <bool>
        """
        return self.__getGoldToExchange(
            resToExchange) <= self.itemsCache.items.stats.gold

    def __getResourceToExchangeTxt(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: <str> formatted needed resource to exchange text
        """
        if resToExchange > 0:
            resource = BigWorld.wg_getIntegralFormat(resToExchange)
            resStr = self._getCurrencyFormat()(
                resource) + self._getCurrencyIconStr()
            return text_styles.error(
                self._makeString(I18N_NEEDITEMSTEXT_KEY, {'value': resStr}))
        return ''

    def __getGoldToExchangeTxt(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: formatted needed gold to exchange text
        """
        if resToExchange > 0:
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = ''.join(
                (text_styles.gold(BigWorld.wg_getGoldFormat(goldToExchange)),
                 icons.gold()))
            return text_styles.main(
                self._makeString(I18N_NEEDGOLDTEXT_KEY, {'gold': fmtGold}))
        return ''

    def __getGoldToExchange(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: <int> gold for exchange
        """
        if resToExchange > 0:
            return int(math.ceil(
                float(resToExchange) / self.getExchangeRate()))
        return 0

    def __getItemIconType(self, item):
        """
        Gets item icon type for Flash component
        :param item: <GUIItem>
        :return: <int> icon type
        """
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            return CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON
        return CONFIRM_EXCHANGE_DIALOG_TYPES.MODULE_ICON

    def __getItemIcon(self, item):
        """
        Gets item icon frame for Flash component
        :param item: <GUIItem>
        :return:
        """
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            icon = item.type
            if item.isElite:
                icon += '_elite'
            return icon
        else:
            return item.getGUIEmblemID()
class _ExchangeDialogMeta(I18nConfirmDialogMeta):

    def __init__(self, key, typeCompactDescr):
        self.__typeCompactDescr = typeCompactDescr
        self.onInvalidate = Event.Event()
        self.onCloseDialog = Event.Event()
        self._items = g_itemsCache.items
        self.colorManager = ColorSchemeManager()
        super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
        game_control.g_instance.wallet.onWalletStatusChanged += self._onStatsChanged

    def destroy(self):
        self._items = None
        game_control.g_instance.wallet.onWalletStatusChanged -= self._onStatsChanged
        self.onInvalidate.clear()
        self.onCloseDialog.clear()
        return

    def getEventType(self):
        return events.ShowDialogEvent.SHOW_EXCHANGE_DIALOG

    def getTypeCompDescr(self):
        return self.__typeCompactDescr

    def submit(self, gold, valueToExchange, callback = None):
        raise NotImplementedError()

    def getExchangeRate(self):
        raise NotImplementedError()

    def getType(self):
        raise NotImplementedError()

    def makeVO(self):
        item = self._items.getItemByCD(self.getTypeCompDescr())
        resToExchange = self._getResourceToExchange()
        extraData = None
        if item.itemTypeID == GUI_ITEM_TYPE.GUN and item.isClipGun():
            extraData = CLIP_ICON_PATH
        state, stateMsg = self.__getState(resToExchange)
        icon = self.__getIcon(item) if self.__getIconType(item) == CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON else item.getGUIEmblemID()
        return {'title': self.getTitle(),
         'exchangeBtnText': self.getButtonLabels()[0]['label'],
         'cancelBtnText': self.getButtonLabels()[1]['label'],
         'state': state,
         'lockExchangeMessage': stateMsg,
         'iconExtraInfo': extraData,
         'iconModuleType': item.itemTypeName,
         'icon': icon,
         'iconType': self.__getIconType(item),
         'itemName': text_styles.middleTitle(item.userName),
         'needItemsText': self.__getResourceToExchangeTxt(resToExchange),
         'needGoldText': self.__getGoldToExchangeTxt(resToExchange),
         'exchangeBlockData': self.__getExchangeBlockData(resToExchange)}

    def _getCurrencyTxt(self, strResource):
        return text_styles.error(self._makeString(I18N_NEEDITEMSTEXT_KEY.format(self._key), {'value': strResource}))

    def _getExchangeTxt(self, strGold):
        return text_styles.main(self._makeString(I18N_NEEDGOLDTEXT_KEY.format(self._key), {'gold': strGold}))

    def _getResourceStepperTxt(self):
        return text_styles.main(self._makeString(I18N_NEEDITEMSSTEPPERTITLE_KEY.format(self._key), {}))

    def _getNoNeedExchangeStateTxt(self):
        return text_styles.success(self._makeString(I18N_EXCHANGENONEEDTEXT_KEY.format(self._key), {}))

    def _getNotEnoughGoldStateTxt(self, strGold):
        return text_styles.error(self._makeString(I18N_GOLDNOTENOUGHTEXT_KEY.format(self._key), {'gold': strGold}))

    def _onStatsChanged(self, *args):
        self.onInvalidate()

    def _getStepSize(self):
        return STEP_SIZE

    def _getDefaultExchangeRate(self):
        raise NotImplementedError()

    def _getResourceToExchange(self):
        raise NotImplementedError()

    def _getCurrencyIconStr(self):
        raise NotImplementedError()

    def _getCurrencyIconPath(self):
        raise NotImplementedError()

    def _getCurrencyFormat(self):
        raise NotImplementedError()

    def _getColorScheme(self):
        raise NotImplementedError()

    def _getRateToColorScheme(self):
        raise NotImplementedError()

    def _getExchangeRateItemsIcon(self):
        raise NotImplementedError()

    def _getMaxExchangeValue(self):
        raise NotImplementedError()

    def _getRGB(self, colorId):
        return self.colorManager.getColorScheme(colorId).get('rgb')

    def __getExchangeBlockData(self, resToExchange):
        goldStepperTitleStr = i18n.makeString(DIALOGS.CONFIRMEXCHANGEDIALOG_GOLDITEMSSTEPPERTITLE)
        goldStepperTitleFmt = text_styles.main(goldStepperTitleStr)
        exchangeHeaderData = {'labelText': '',
         'rateFromIcon': ICON_TEXT_FRAMES.GOLD,
         'rateToIcon': self._getExchangeRateItemsIcon(),
         'rateFromTextColor': self._getRGB(TEXT_COLOR_ID_XP),
         'rateToTextColor': self._getRGB(self._getRateToColorScheme())}
        return {'goldStepperTitle': goldStepperTitleFmt,
         'needItemsIcon': self._getCurrencyIconPath(),
         'needItemsStepperTitle': self._getResourceStepperTxt(),
         'goldIcon': RES_ICONS.MAPS_ICONS_LIBRARY_GOLDICON_2,
         'defaultExchangeRate': self._getDefaultExchangeRate(),
         'exchangeRate': self.getExchangeRate(),
         'defaultGoldValue': self.__getGoldToExchange(resToExchange),
         'goldStepSize': self._getStepSize(),
         'maxGoldValue': self._getMaxExchangeValue(),
         'goldTextColorId': TEXT_MANAGER_STYLES.GOLD_TEXT,
         'itemsTextColorId': self._getColorScheme(),
         'exchangeHeaderData': exchangeHeaderData}

    def __getState(self, resToExchange):
        if resToExchange <= 0:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.EXCHANGE_NOT_NEEED_STATE, self._getNoNeedExchangeStateTxt())
        elif not self.__isEnoughGold(resToExchange):
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = self.__getGoldValueWithIcon(goldToExchange)
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NOT_ENOUGH_GOLD_STATE, self._getNotEnoughGoldStateTxt(fmtGold))
        else:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NORMAL_STATE, '')

    def __isEnoughGold(self, resToExchange):
        return self.__getGoldToExchange(resToExchange) <= self._items.stats.gold

    def __getResourceToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            resource = BigWorld.wg_getIntegralFormat(resToExchange)
            resStr = self._getCurrencyFormat()(resource) + self._getCurrencyIconStr()
            return self._getCurrencyTxt(resStr)
        return ''

    def __getGoldToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = self.__getGoldValueWithIcon(goldToExchange)
            return self._getExchangeTxt(fmtGold)
        return ''

    def __getGoldValueWithIcon(self, gold):
        return ''.join(text_styles.gold(BigWorld.wg_getGoldFormat(gold)) + icons.gold())

    def __getGoldToExchange(self, resToExchange):
        if resToExchange > 0:
            return int(math.ceil(float(resToExchange) / self.getExchangeRate()))
        return 0

    def __getIconType(self, item):
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            return CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON
        return CONFIRM_EXCHANGE_DIALOG_TYPES.MODULE_ICON

    def __getIcon(self, item):
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            icon = item.type
            if item.isElite:
                icon += '_elite'
            return icon
        if item.itemTypeID not in (GUI_ITEM_TYPE.OPTIONALDEVICE, GUI_ITEM_TYPE.SHELL, GUI_ITEM_TYPE.EQUIPMENT):
            return str(item.level)
        return item.icon
Exemple #10
0
class _ExchangeDialogMeta(I18nConfirmDialogMeta):

    def __init__(self, key, typeCompactDescr):
        self.__typeCompactDescr = typeCompactDescr
        self.onInvalidate = Event.Event()
        self.onCloseDialog = Event.Event()
        self._items = g_itemsCache.items
        self.colorManager = ColorSchemeManager()
        super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
        game_control.g_instance.wallet.onWalletStatusChanged += self._onStatsChanged

    def destroy(self):
        self._items = None
        game_control.g_instance.wallet.onWalletStatusChanged -= self._onStatsChanged
        self.onInvalidate.clear()
        self.onCloseDialog.clear()
        return

    def getEventType(self):
        return events.ShowDialogEvent.SHOW_EXCHANGE_DIALOG

    def getTypeCompDescr(self):
        return self.__typeCompactDescr

    def submit(self, gold, valueToExchange, callback = None):
        raise NotImplementedError()

    def getExchangeRate(self):
        raise NotImplementedError()

    def getType(self):
        raise NotImplementedError()

    def makeVO(self):
        item = self._items.getItemByCD(self.getTypeCompDescr())
        resToExchange = self._getResourceToExchange()
        extraData = None
        if item.itemTypeID == GUI_ITEM_TYPE.GUN and item.isClipGun():
            extraData = CLIP_ICON_PATH
        state, stateMsg = self.__getState(resToExchange)
        icon = self.__getIcon(item) if self.__getIconType(item) == CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON else item.getGUIEmblemID()
        return {'title': self.getTitle(),
         'exchangeBtnText': self.getButtonLabels()[0]['label'],
         'cancelBtnText': self.getButtonLabels()[1]['label'],
         'state': state,
         'lockExchangeMessage': stateMsg,
         'iconExtraInfo': extraData,
         'iconModuleType': item.itemTypeName,
         'icon': icon,
         'iconType': self.__getIconType(item),
         'itemName': text_styles.middleTitle(item.userName),
         'needItemsText': self.__getResourceToExchangeTxt(resToExchange),
         'needGoldText': self.__getGoldToExchangeTxt(resToExchange),
         'exchangeBlockData': self.__getExchangeBlockData(resToExchange)}

    def _getCurrencyTxt(self, strResource):
        return text_styles.error(self._makeString(I18N_NEEDITEMSTEXT_KEY.format(self._key), {'value': strResource}))

    def _getExchangeTxt(self, strGold):
        return text_styles.main(self._makeString(I18N_NEEDGOLDTEXT_KEY.format(self._key), {'gold': strGold}))

    def _getResourceStepperTxt(self):
        return text_styles.main(self._makeString(I18N_NEEDITEMSSTEPPERTITLE_KEY.format(self._key), {}))

    def _getNoNeedExchangeStateTxt(self):
        return text_styles.success(self._makeString(I18N_EXCHANGENONEEDTEXT_KEY.format(self._key), {}))

    def _getNotEnoughGoldStateTxt(self, strGold):
        return text_styles.error(self._makeString(I18N_GOLDNOTENOUGHTEXT_KEY.format(self._key), {'gold': strGold}))

    def _onStatsChanged(self, *args):
        self.onInvalidate()

    def _getStepSize(self):
        return STEP_SIZE

    def _getDefaultExchangeRate(self):
        raise NotImplementedError()

    def _getResourceToExchange(self):
        raise NotImplementedError()

    def _getCurrencyIconStr(self):
        raise NotImplementedError()

    def _getCurrencyIconPath(self):
        raise NotImplementedError()

    def _getCurrencyFormat(self):
        raise NotImplementedError()

    def _getColorScheme(self):
        raise NotImplementedError()

    def _getRateToColorScheme(self):
        raise NotImplementedError()

    def _getExchangeRateItemsIcon(self):
        raise NotImplementedError()

    def _getMaxExchangeValue(self):
        raise NotImplementedError()

    def _getRGB(self, colorId):
        return self.colorManager.getColorScheme(colorId).get('rgb')

    def __getExchangeBlockData(self, resToExchange):
        goldStepperTitleStr = i18n.makeString(DIALOGS.CONFIRMEXCHANGEDIALOG_GOLDITEMSSTEPPERTITLE)
        goldStepperTitleFmt = text_styles.main(goldStepperTitleStr)
        exchangeHeaderData = {'labelText': '',
         'rateFromIcon': ICON_TEXT_FRAMES.GOLD,
         'rateToIcon': self._getExchangeRateItemsIcon(),
         'rateFromTextColor': self._getRGB(TEXT_COLOR_ID_XP),
         'rateToTextColor': self._getRGB(self._getRateToColorScheme())}
        return {'goldStepperTitle': goldStepperTitleFmt,
         'needItemsIcon': self._getCurrencyIconPath(),
         'needItemsStepperTitle': self._getResourceStepperTxt(),
         'goldIcon': RES_ICONS.MAPS_ICONS_LIBRARY_GOLDICON_2,
         'defaultExchangeRate': self._getDefaultExchangeRate(),
         'exchangeRate': self.getExchangeRate(),
         'defaultGoldValue': self.__getGoldToExchange(resToExchange),
         'goldStepSize': self._getStepSize(),
         'maxGoldValue': self._getMaxExchangeValue(),
         'goldTextColorId': TEXT_MANAGER_STYLES.GOLD_TEXT,
         'itemsTextColorId': self._getColorScheme(),
         'exchangeHeaderData': exchangeHeaderData}

    def __getState(self, resToExchange):
        if resToExchange <= 0:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.EXCHANGE_NOT_NEEED_STATE, self._getNoNeedExchangeStateTxt())
        elif not self.__isEnoughGold(resToExchange):
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = self.__getGoldValueWithIcon(goldToExchange)
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NOT_ENOUGH_GOLD_STATE, self._getNotEnoughGoldStateTxt(fmtGold))
        else:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NORMAL_STATE, '')

    def __isEnoughGold(self, resToExchange):
        return self.__getGoldToExchange(resToExchange) <= self._items.stats.gold

    def __getResourceToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            resource = BigWorld.wg_getIntegralFormat(resToExchange)
            resStr = self._getCurrencyFormat()(resource) + self._getCurrencyIconStr()
            return self._getCurrencyTxt(resStr)
        return ''

    def __getGoldToExchangeTxt(self, resToExchange):
        if resToExchange > 0:
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = self.__getGoldValueWithIcon(goldToExchange)
            return self._getExchangeTxt(fmtGold)
        return ''

    def __getGoldValueWithIcon(self, gold):
        return ''.join(text_styles.gold(BigWorld.wg_getGoldFormat(gold)) + icons.gold())

    def __getGoldToExchange(self, resToExchange):
        if resToExchange > 0:
            return int(math.ceil(float(resToExchange) / self.getExchangeRate()))
        return 0

    def __getIconType(self, item):
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            return CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON
        return CONFIRM_EXCHANGE_DIALOG_TYPES.MODULE_ICON

    def __getIcon(self, item):
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            icon = item.type
            if item.isElite:
                icon += '_elite'
            return icon
        if item.itemTypeID not in (GUI_ITEM_TYPE.OPTIONALDEVICE, GUI_ITEM_TYPE.SHELL, GUI_ITEM_TYPE.EQUIPMENT):
            return str(item.level)
        return item.icon
class _ExchangeDialogMeta(I18nConfirmDialogMeta):
    """
    Base meta for ConfirmExchangeDialog
    allows to exchange gold for other currency
    :param typeCompactDescr: <int> item compact descriptor
    :param key: <str> localization key
    """

    def __init__(self, typeCompactDescr, key):
        self.__typeCompactDescr = typeCompactDescr
        self.onInvalidate = Event.Event()
        self.onCloseDialog = Event.Event()
        self._items = g_itemsCache.items
        self.colorManager = ColorSchemeManager()
        super(_ExchangeDialogMeta, self).__init__(key, scope=ScopeTemplates.LOBBY_SUB_SCOPE)
        game_control.g_instance.wallet.onWalletStatusChanged += self._onStatsChanged

    def destroy(self):
        """
        destroy operation after exchange dialog close
        """
        self._items = None
        game_control.g_instance.wallet.onWalletStatusChanged -= self._onStatsChanged
        self.onInvalidate.clear()
        self.onCloseDialog.clear()
        return

    def getEventType(self):
        """
        EventType to open ConfirmExchangeDialog
        :return:<str> eventType "showExchangeDialog"
        """
        return events.ShowDialogEvent.SHOW_EXCHANGE_DIALOG

    def getTypeCompDescr(self):
        """
        Gets item compact descriptor
        :return: <int> item compact descriptor
        """
        return self.__typeCompactDescr

    def submit(self, gold, valueToExchange, callback=None):
        """
        Submit exchange operation, send request to exchange and call callback after response received
        :param gold: <int> gold value
        :param valueToExchange: <int> credits or xp value to exchange
        :param callback: <function>
        """
        raise NotImplementedError()

    def getExchangeRate(self):
        """
        Get exchange rate rate for defined currency
        May return shop.exchangeRate for gold exchange
        or shop.freeXPConversion[0] for xp exchange
        :return: <int> exchange rate for defined currency
        """
        raise NotImplementedError()

    def getType(self):
        """
        :return: <str> confirm exchange dialog type for unique name for _ExchangeDialogBusinessHandler
        """
        raise NotImplementedError()

    def makeVO(self):
        """
        Makes VO for ConfirmExchangeDialog
        :return: <obj>
        """
        item = self._items.getItemByCD(self.getTypeCompDescr())
        resToExchange = self._getResourceToExchange()
        extraData = None
        if item.itemTypeID == GUI_ITEM_TYPE.GUN and item.isClipGun():
            extraData = CLIP_ICON_PATH
        state, stateMsg = self.__getState(resToExchange)
        return {
            "title": self.getTitle(),
            "exchangeBtnText": self.getButtonLabels()[0]["label"],
            "cancelBtnText": self.getButtonLabels()[1]["label"],
            "state": state,
            "lockExchangeMessage": stateMsg,
            "iconExtraInfo": extraData,
            "iconModuleType": item.itemTypeName,
            "icon": self.__getItemIcon(item),
            "iconType": self.__getItemIconType(item),
            "itemName": text_styles.middleTitle(item.userName),
            "needItemsText": self.__getResourceToExchangeTxt(resToExchange),
            "needGoldText": self.__getGoldToExchangeTxt(resToExchange),
            "exchangeBlockData": self.__getExchangeBlockData(resToExchange),
        }

    def _onStatsChanged(self, *args):
        """
        call onInvalidate Event on account stats changes
        """
        self.onInvalidate()

    def _getDefaultExchangeRate(self):
        """
        Gets shop default exchange rate for defined currency
        :return: <int>
        """
        raise NotImplementedError()

    def _getResourceToExchange(self):
        """
        # calculate necessary resource for exchange (credits, xp, ...)
        :return: <int>
        """
        raise NotImplementedError()

    def _getCurrencyIconStr(self):
        """
        Gets html currency icon
        :return: <str>
        """
        raise NotImplementedError()

    def _getCurrencyIconPath(self):
        """
        Gets currency icon path
        :return: <str>
        """
        raise NotImplementedError()

    def _getCurrencyFormat(self):
        """
        Gets text_style format for resource to exchange text
        :return: <function>
        """
        raise NotImplementedError()

    def _getColorScheme(self):
        """
        Gets color scheme ID for itemsText
        :return: <str>
        """
        raise NotImplementedError()

    def _getRateToColorScheme(self):
        """
        Gets color scheme ID for rateToTextColor field
        :return: <str>
        """
        raise NotImplementedError()

    def _getExchangeRateItemsIcon(self):
        """
        :return: <str> frame for  GUI rateToIcon field
        """
        raise NotImplementedError()

    def _getMaxExchangeValue(self):
        """
        Calculates max available resource value for exchange
        :return: <int>
        """
        raise NotImplementedError()

    def _getRGB(self, colorId):
        """
        :param colorId:  <str>
        :return: <obj> RGB color scheme
        """
        return self.colorManager.getColorScheme(colorId).get("rgb")

    def _makeString(self, key, ctx=None):
        """
        :param key: <str> localization key
        :param ctx: <obj> lcalisation context
        :return: <str> localized value
        """
        ctx = ctx or {}
        i18nKey = key.format(self._key)
        return super(_ExchangeDialogMeta, self)._makeString(i18nKey, ctx)

    def __getExchangeBlockData(self, resToExchange):
        """
        Makes VO for exchangeBlockData field
        :param resToExchange: <int> resource value for exchange
        :return: <obj>
        """
        goldStepperTitleStr = i18n.makeString(DIALOGS.CONFIRMEXCHANGEDIALOG_GOLDITEMSSTEPPERTITLE)
        goldStepperTitleFmt = text_styles.main(goldStepperTitleStr)
        exchangeHeaderData = {
            "labelText": i18n.makeString(MENU.EXCHANGE_RATE),
            "rateFromIcon": ICON_TEXT_FRAMES.GOLD,
            "rateToIcon": self._getExchangeRateItemsIcon(),
            "rateFromTextColor": self._getRGB(TEXT_COLOR_ID_XP),
            "rateToTextColor": self._getRGB(self._getRateToColorScheme()),
        }
        needItemsStepperTitle = text_styles.main(self._makeString(I18N_NEEDITEMSSTEPPERTITLE_KEY))
        return {
            "goldStepperTitle": goldStepperTitleFmt,
            "needItemsIcon": self._getCurrencyIconPath(),
            "needItemsStepperTitle": needItemsStepperTitle,
            "goldIcon": RES_ICONS.MAPS_ICONS_LIBRARY_GOLDICON_2,
            "defaultExchangeRate": self._getDefaultExchangeRate(),
            "exchangeRate": self.getExchangeRate(),
            "defaultGoldValue": self.__getGoldToExchange(resToExchange),
            "goldStepSize": STEP_SIZE,
            "maxGoldValue": self._getMaxExchangeValue(),
            "goldTextColorId": TEXT_MANAGER_STYLES.GOLD_TEXT,
            "itemsTextColorId": self._getColorScheme(),
            "exchangeHeaderData": {
                "labelText": "",
                "rateFromIcon": ICON_TEXT_FRAMES.GOLD,
                "rateToIcon": self._getExchangeRateItemsIcon(),
                "rateFromTextColor": self._getRGB(TEXT_COLOR_ID_XP),
                "rateToTextColor": self._getRGB(self._getRateToColorScheme()),
            },
        }

    def __getState(self, resToExchange):
        """
        Gets state and reason for exchange possibility
        :param resToExchange: <int> resource for exchange
        :return: <tuple(state:<int>, reason<str>)>
        """
        if resToExchange <= 0:
            return (
                CONFIRM_EXCHANGE_DIALOG_TYPES.EXCHANGE_NOT_NEEED_STATE,
                text_styles.success(self._makeString(I18N_EXCHANGENONEEDTEXT_KEY)),
            )
        elif not self.__isEnoughGold(resToExchange):
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = "".join((text_styles.gold(BigWorld.wg_getGoldFormat(goldToExchange)), icons.gold()))
            return (
                CONFIRM_EXCHANGE_DIALOG_TYPES.NOT_ENOUGH_GOLD_STATE,
                text_styles.error(self._makeString(I18N_GOLDNOTENOUGHTEXT_KEY, {"gold": fmtGold})),
            )
        else:
            return (CONFIRM_EXCHANGE_DIALOG_TYPES.NORMAL_STATE, "")

    def __isEnoughGold(self, resToExchange):
        """
        Checks if enough gold for exchange operation
        :param resToExchange: <int> resource for exchange
        :return: <bool>
        """
        return self.__getGoldToExchange(resToExchange) <= self._items.stats.gold

    def __getResourceToExchangeTxt(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: <str> formatted needed resource to exchange text
        """
        if resToExchange > 0:
            resource = BigWorld.wg_getIntegralFormat(resToExchange)
            resStr = self._getCurrencyFormat()(resource) + self._getCurrencyIconStr()
            return text_styles.error(self._makeString(I18N_NEEDITEMSTEXT_KEY, {"value": resStr}))
        return ""

    def __getGoldToExchangeTxt(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: formatted needed gold to exchange text
        """
        if resToExchange > 0:
            goldToExchange = self.__getGoldToExchange(resToExchange)
            fmtGold = "".join((text_styles.gold(BigWorld.wg_getGoldFormat(goldToExchange)), icons.gold()))
            return text_styles.main(self._makeString(I18N_NEEDGOLDTEXT_KEY, {"gold": fmtGold}))
        return ""

    def __getGoldToExchange(self, resToExchange):
        """
        :param resToExchange: <int> resource for exchange
        :return: <int> gold for exchange
        """
        if resToExchange > 0:
            return int(math.ceil(float(resToExchange) / self.getExchangeRate()))
        return 0

    def __getItemIconType(self, item):
        """
        Gets item icon type for Flash component
        :param item: <GUIItem>
        :return: <int> icon type
        """
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            return CONFIRM_EXCHANGE_DIALOG_TYPES.VEHICLE_ICON
        return CONFIRM_EXCHANGE_DIALOG_TYPES.MODULE_ICON

    def __getItemIcon(self, item):
        """
        Gets item icon frame for Flash component
        :param item: <GUIItem>
        :return:
        """
        if item.itemTypeID == GUI_ITEM_TYPE.VEHICLE:
            icon = item.type
            if item.isElite:
                icon += "_elite"
            return icon
        else:
            return item.getGUIEmblemID()