class DailyQuestWidget(InjectComponentAdaptor, DailyQuestMeta,
                       IGlobalListener):
    lobbyContext = dependency.descriptor(ILobbyContext)
    eventsCache = dependency.descriptor(IEventsCache)
    promoController = dependency.descriptor(IPromoController)
    __layout = 0

    def updateWidgetLayout(self, value):
        self.__layout = value
        if self._injectView is not None:
            self._injectView.setLayout(self.__layout)
        return

    def onPrbEntitySwitched(self):
        if not (self._isRandomBattleSelected() or self._isMapboxSelected()):
            self.__animateHide()
        else:
            self.__showOrHide()

    def _populate(self):
        super(DailyQuestWidget, self)._populate()
        self.__addListeners()
        self.__timer = CallbackDelayer()
        self.__showOrHide()

    def _onPopulate(self):
        pass

    def _dispose(self):
        self.__timer.clearCallbacks()
        self.__removeListeners()
        super(DailyQuestWidget, self)._dispose()

    def _makeInjectView(self):
        return DailyQuestsWidgetView()

    def _isRandomBattleSelected(self):
        return self.__isQueueSelected(QUEUE_TYPE.RANDOMS)

    def _isMapboxSelected(self):
        return self.__isQueueSelected(QUEUE_TYPE.MAPBOX)

    def __isQueueSelected(self, queueType):
        return self.prbDispatcher.getFunctionalState().isQueueSelected(
            queueType) if self.prbDispatcher is not None else False

    def __show(self):
        if self._injectView is None:
            self._createInjectView()
        self._injectView.setVisible(True)
        self._injectView.viewModel.onDisappear += self.__hide
        return

    def __animateHide(self):
        if self._injectView is not None:
            self._injectView.setVisible(False)
        return

    def __hide(self):
        if self._injectView is not None:
            self._injectView.viewModel.onDisappear -= self.__hide
        self._destroyInjected()
        return

    def __delayedShowOrHide(self):
        self.__timer.delayCallback(UtilsManager.ONE_SECOND, self.__showOrHide)

    def __showOrHide(self):
        if not self.__timer.hasDelayedCallback(self.__executeShowOrHide):
            self.__timer.delayCallback(0.0, self.__executeShowOrHide)

    def __executeShowOrHide(self):
        isEnabled = False
        if self.__shouldHide():
            self.__hide()
        elif self.__hasIncompleteQuests() or self.__hasQuestStatusChanged():
            isEnabled = True
            self.__show()
        self.as_setEnabledS(isEnabled)

    def __shouldHide(self):
        return not isDailyQuestsEnable() or self.promoController.isTeaserOpen(
        ) or not (self._isRandomBattleSelected() or self._isMapboxSelected())

    def __hasIncompleteQuests(self):
        for quest in self.eventsCache.getDailyQuests().values():
            if not quest.isCompleted():
                return True

        return False

    def __hasQuestStatusChanged(self):
        for quest in self.eventsCache.getDailyQuests().values():
            if self.eventsCache.questsProgress.getQuestCompletionChanged(
                    quest.getID()):
                return True

        return False

    def __onServerSettingsChanged(self, diff):
        if DAILY_QUESTS_CONFIG in diff:
            self.__showOrHide()

    def __onTeaserClosed(self):
        self.__delayedShowOrHide()

    def __onTeaserShown(self):
        self.__timer.stopCallback(self.__showOrHide)
        self.__showOrHide()

    def __onSyncCompleted(self):
        self.__showOrHide()

    def __addListeners(self):
        self.startGlobalListening()
        self.lobbyContext.getServerSettings(
        ).onServerSettingsChange += self.__onServerSettingsChanged
        self.eventsCache.onSyncCompleted += self.__onSyncCompleted
        self.promoController.onTeaserShown += self.__onTeaserShown
        self.promoController.onTeaserClosed += self.__onTeaserClosed

    def __removeListeners(self):
        self.stopGlobalListening()
        self.lobbyContext.getServerSettings(
        ).onServerSettingsChange -= self.__onServerSettingsChanged
        self.eventsCache.onSyncCompleted -= self.__onSyncCompleted
        self.promoController.onTeaserShown -= self.__onTeaserShown
        self.promoController.onTeaserClosed -= self.__onTeaserClosed
        if self._injectView is not None:
            self._injectView.viewModel.onDisappear -= self.__hide
        return
예제 #2
0
class PreBattleHintPlugin(HintPanelPlugin):
    sessionProvider = dependency.descriptor(IBattleSessionProvider)
    lobbyContext = dependency.descriptor(ILobbyContext)

    def __init__(self, parentObj):
        super(PreBattleHintPlugin, self).__init__(parentObj)
        self.__isActive = False
        self.__hintInQueue = None
        self.__callbackDelayer = CallbackDelayer()
        self.__questHintSettings = {}
        self.__helpHintSettings = {}
        self.__battleComHintSettings = {}
        self.__isInDisplayPeriod = False
        self.__haveReqLevel = False
        self.__vehicleId = None
        return

    @classmethod
    def isSuitable(cls):
        guiType = cls.sessionProvider.arenaVisitor.getArenaGuiType()
        return guiType != ARENA_GUI_TYPE.RANKED and guiType != ARENA_GUI_TYPE.BATTLE_ROYALE

    def start(self):
        prbSettings = dict(
            AccountSettings.getSettings(PRE_BATTLE_HINT_SECTION))
        self.__questHintSettings = prbSettings[QUEST_PROGRESS_HINT_SECTION]
        self.__helpHintSettings = prbSettings[HELP_SCREEN_HINT_SECTION]
        self.__battleComHintSettings = prbSettings[IBC_HINT_SECTION]
        HintPanelPlugin._updateCounterOnStart(
            self.__questHintSettings, PRBSettings.HINT_DAY_COOLDOWN,
            PRBSettings.HINT_BATTLES_COOLDOWN)
        self.__isActive = True
        g_eventBus.addListener(GameEvent.SHOW_BTN_HINT,
                               self.__handleShowBtnHint,
                               scope=EVENT_BUS_SCOPE.GLOBAL)
        g_eventBus.addListener(GameEvent.HELP_DETAILED,
                               self.__handlePressHelpBtn,
                               scope=EVENT_BUS_SCOPE.BATTLE)
        g_eventBus.addListener(GameEvent.FULL_STATS_QUEST_PROGRESS,
                               self.__handlePressQuestBtn,
                               scope=EVENT_BUS_SCOPE.BATTLE)
        vStateCtrl = self.sessionProvider.shared.vehicleState
        if vStateCtrl is not None:
            vStateCtrl.onVehicleControlling += self.__onVehicleControlling
        return

    def stop(self):
        if not self.isActive():
            return
        else:
            g_eventBus.removeListener(GameEvent.SHOW_BTN_HINT,
                                      self.__handleShowBtnHint,
                                      scope=EVENT_BUS_SCOPE.GLOBAL)
            g_eventBus.removeListener(GameEvent.HELP_DETAILED,
                                      self.__handlePressHelpBtn,
                                      scope=EVENT_BUS_SCOPE.BATTLE)
            g_eventBus.removeListener(GameEvent.FULL_STATS_QUEST_PROGRESS,
                                      self.__handlePressQuestBtn,
                                      scope=EVENT_BUS_SCOPE.BATTLE)
            vStateCtrl = self.sessionProvider.shared.vehicleState
            if vStateCtrl is not None:
                vStateCtrl.onVehicleControlling -= self.__onVehicleControlling
            self.__callbackDelayer.destroy()
            self.__isActive = False
            prbHintSettings = dict()
            prbHintSettings[
                QUEST_PROGRESS_HINT_SECTION] = self.__questHintSettings
            prbHintSettings[HELP_SCREEN_HINT_SECTION] = self.__helpHintSettings
            prbHintSettings[IBC_HINT_SECTION] = self.__battleComHintSettings
            AccountSettings.setSettings(PRE_BATTLE_HINT_SECTION,
                                        prbHintSettings)
            return

    def isActive(self):
        return self.__isActive

    def setPeriod(self, period):
        self.__isInDisplayPeriod = period in (ARENA_PERIOD.PREBATTLE,
                                              ARENA_PERIOD.WAITING)
        if period is ARENA_PERIOD.BATTLE:
            self._updateCounterOnBattle(self.__questHintSettings)

    def updateMapping(self):
        if self.__hintInQueue is not None:
            self._parentObj.setBtnHint(self.__hintInQueue, self._getHint())
        return

    def _getHint(self):
        if self.__hintInQueue is CommandMapping.CMD_SHOW_HELP:
            keyName = getReadableKey(CommandMapping.CMD_SHOW_HELP)
            pressText = backport.text(
                R.strings.ingame_gui.helpScreen.hint.press())
            hintText = backport.text(
                R.strings.ingame_gui.helpScreen.hint.description())
            return HintData(keyName, pressText, hintText, 0, 0,
                            HintPriority.HELP)
        if self.__hintInQueue is CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND:
            keyName = getReadableKey(
                CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND)
            pressText = backport.text(
                R.strings.ingame_gui.battleCommunication.hint.press())
            hintText = backport.text(
                R.strings.ingame_gui.battleCommunication.hint.description())
            return HintData(keyName, pressText, hintText, 0, 0,
                            HintPriority.BATTLE_COMMUNICATION)
        if self.__hintInQueue is CommandMapping.CMD_QUEST_PROGRESS_SHOW:
            if self.lobbyContext.getServerSettings().isPersonalMissionsEnabled(
            ):
                keyName = getReadableKey(
                    CommandMapping.CMD_QUEST_PROGRESS_SHOW)
                pressText = ''
                hintText = backport.text(
                    R.strings.ingame_gui.battleProgress.hint.noBindingKey())
                if keyName:
                    pressText = backport.text(
                        R.strings.ingame_gui.battleProgress.hint.press())
                    hintText = backport.text(
                        R.strings.ingame_gui.battleProgress.hint.description())
                return HintData(keyName, pressText, hintText, 0, 0,
                                HintPriority.QUESTS)

    def __onVehicleControlling(self, vehicle):
        if not self.isActive():
            return
        else:
            vTypeDesc = vehicle.typeDescriptor
            vehicleType = vTypeDesc.type.id
            self.__vehicleId = makeIntCompactDescrByID('vehicle',
                                                       vehicleType[0],
                                                       vehicleType[1])
            self.__haveReqLevel = vTypeDesc.level >= _HINT_MIN_VEHICLE_LEVEL
            if vTypeDesc.isWheeledVehicle or vTypeDesc.type.isDualgunVehicleType or vTypeDesc.hasTurboshaftEngine:
                self.__updateHintCounterOnStart(self.__vehicleId, vehicle,
                                                self.__helpHintSettings)
            if self.__canDisplayHelpHint(vTypeDesc):
                self.__displayHint(CommandMapping.CMD_SHOW_HELP)
                return
            if self.__canDisplayBattleCommunicationHint():
                isDisplayed = self.__displayHint(
                    CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND)
                if isDisplayed:
                    self.__battleComHintSettings = self._updateBattleCounterOnUsed(
                        self.__battleComHintSettings)
                return
            if self.__canDisplayQuestHint():
                self.__displayHint(CommandMapping.CMD_QUEST_PROGRESS_SHOW)
                return
            if self.__hintInQueue is not None:
                self._parentObj.removeBtnHint(CommandMapping.CMD_SHOW_HELP)
                self._parentObj.removeBtnHint(
                    CommandMapping.CMD_QUEST_PROGRESS_SHOW)
                self._parentObj.removeBtnHint(
                    CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND)
                self.__callbackDelayer.destroy()
            return

    def __updateHintCounterOnStart(self, vehicleId, vehicle, setting):
        if vehicleId not in setting:
            setting[vehicleId] = {
                HINTS_LEFT: 1,
                LAST_DISPLAY_DAY: 0,
                NUM_BATTLES: 0
            }
        if vehicle.isAlive() and vehicle.isPlayerVehicle:
            self._updateCounterOnBattle(setting[vehicleId])
            HintPanelPlugin._updateCounterOnStart(
                setting[vehicleId], PRBSettings.HINT_DAY_COOLDOWN,
                PRBSettings.HINT_BATTLES_COOLDOWN)

    def __onHintTimeOut(self):
        self._parentObj.removeBtnHint(self.__hintInQueue)
        if self.__hintInQueue is CommandMapping.CMD_SHOW_HELP or self.__hintInQueue is CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND:
            self.__callbackDelayer.delayCallback(_HINT_COOLDOWN,
                                                 self.__onHintTimeCooldown,
                                                 self.__hintInQueue)
        self.__hintInQueue = None
        return

    def __canDisplayHelpHint(self, typeDescriptor):
        return (typeDescriptor.isWheeledVehicle
                or typeDescriptor.type.isDualgunVehicleType
                or typeDescriptor.hasTurboshaftEngine
                ) and self.__isInDisplayPeriod and self._haveHintsLeft(
                    self.__helpHintSettings[self.__vehicleId])

    def __canDisplayBattleCommunicationHint(self):
        settingsCore = dependency.instance(ISettingsCore)
        battleCommunicationIsEnabled = bool(
            settingsCore.getSetting(
                BattleCommStorageKeys.ENABLE_BATTLE_COMMUNICATION))
        return self.__isInDisplayPeriod and self._haveHintsLeft(
            self.__battleComHintSettings
        ) and self.sessionProvider.arenaVisitor.getArenaGuiType(
        ) != ARENA_GUI_TYPE.BOOTCAMP and battleCommunicationIsEnabled

    def __canDisplayQuestHint(self):
        return self.__isInDisplayPeriod and self._haveHintsLeft(
            self.__questHintSettings
        ) and self.__haveReqLevel and self.sessionProvider.arenaVisitor.getArenaGuiType(
        ) in ARENA_GUI_TYPE.RANDOM_RANGE and self.lobbyContext.getServerSettings(
        ).isPersonalMissionsEnabled()

    def __onHintTimeCooldown(self, lastHint):
        if lastHint == CommandMapping.CMD_SHOW_HELP and self.__canDisplayBattleCommunicationHint(
        ):
            isDisplayed = self.__displayHint(
                CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND)
            if isDisplayed:
                self.__battleComHintSettings = self._updateBattleCounterOnUsed(
                    self.__battleComHintSettings)
        elif (lastHint == CommandMapping.CMD_SHOW_HELP
              or lastHint == CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND
              ) and self.__canDisplayQuestHint():
            self.__displayHint(CommandMapping.CMD_QUEST_PROGRESS_SHOW)

    def __displayHint(self, hintType):
        if self.__isInDisplayPeriod:
            self.__hintInQueue = hintType
            self._parentObj.setBtnHint(hintType, self._getHint())
            return True
        return False

    def __handleShowBtnHint(self, event):
        if event.ctx.get(
                'btnID') == CommandMapping.CMD_SHOW_HELP or event.ctx.get(
                    'btnID'
                ) == CommandMapping.CMD_QUEST_PROGRESS_SHOW or event.ctx.get(
                    'btnID'
                ) == CommandMapping.CMD_CHAT_SHORTCUT_CONTEXT_COMMAND:
            self.__callbackDelayer.delayCallback(_HINT_TIMEOUT,
                                                 self.__onHintTimeOut)
        elif self.__callbackDelayer.hasDelayedCallback(self.__onHintTimeOut):
            self.__callbackDelayer.stopCallback(self.__onHintTimeOut)

    def __handlePressHelpBtn(self, event):
        if self.__hintInQueue == CommandMapping.CMD_SHOW_HELP:
            self._parentObj.removeBtnHint(CommandMapping.CMD_SHOW_HELP)
            self.__callbackDelayer.delayCallback(_HINT_COOLDOWN,
                                                 self.__onHintTimeCooldown,
                                                 self.__hintInQueue)
            self.__hintInQueue = None
        hintStats = self.__helpHintSettings[self.__vehicleId]
        self.__helpHintSettings[self.__vehicleId] = self._updateCounterOnUsed(
            hintStats)
        return

    def __handlePressQuestBtn(self, event):
        if self.__hintInQueue == CommandMapping.CMD_QUEST_PROGRESS_SHOW:
            self._parentObj.removeBtnHint(
                CommandMapping.CMD_QUEST_PROGRESS_SHOW)
            self.__hintInQueue = None
        self.__questHintSettings = self._updateCounterOnUsed(
            self.__questHintSettings)
        return