示例#1
0
class PlayerMenuHandler(AbstractContextMenuHandler):
    sessionProvider = dependency.descriptor(IBattleSessionProvider)

    def __init__(self, cmProxy, ctx = None):
        self.__denunciator = BattleDenunciator()
        self.__arenaUniqueID = BattleDenunciator.getArenaUniqueID()
        g_eventBus.addListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        super(PlayerMenuHandler, self).__init__(cmProxy, ctx=ctx, handlers=_OPTIONS_HANDLERS)

    @proto_getter(PROTO_TYPE.MIGRATION)
    def proto(self):
        return None

    @storage_getter('users')
    def usersStorage(self):
        return None

    @proto_getter(PROTO_TYPE.BW_CHAT2)
    def bwProto(self):
        return None

    @property
    def arenaGuiType(self):
        return self.sessionProvider.arenaVisitor.gui

    def fini(self):
        g_eventBus.removeListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        self.__denunciator = None
        super(PlayerMenuHandler, self).fini()
        return

    def addFriend(self):
        self.proto.contacts.addFriend(self.__userInfo.databaseID, self.__userInfo.userName)

    def removeFriend(self):
        self.proto.contacts.removeFriend(self.__userInfo.databaseID)

    def setIgnored(self):
        self.proto.contacts.addIgnored(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetIgnored(self):
        self.proto.contacts.removeIgnored(self.__userInfo.databaseID)

    def disableCommunications(self):
        self.proto.contacts.addTmpIgnored(self.__userInfo.databaseID, self.__userInfo.userName)

    def enableCommunications(self):
        self.proto.contacts.removeTmpIgnored(self.__userInfo.databaseID)

    def setMuted(self):
        self.proto.contacts.setMuted(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetMuted(self):
        self.proto.contacts.unsetMuted(self.__userInfo.databaseID)

    def appealIncorrectBehavior(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.INCORRECT_BEHAVIOR, self.__arenaUniqueID)

    def appealNotFairPlay(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.NOT_FAIR_PLAY, self.__arenaUniqueID)

    def appealForbiddenNick(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.FORBIDDEN_NICK, self.__arenaUniqueID)

    def appealBot(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.BOT, self.__arenaUniqueID)

    def sendInvitation(self):
        self.sessionProvider.invitations.send(self.__userInfo.databaseID)

    def acceptInvitation(self):
        self.sessionProvider.invitations.accept(self.__userInfo.databaseID)

    def rejectInvitation(self):
        self.sessionProvider.invitations.reject(self.__userInfo.databaseID)

    def _initFlashValues(self, ctx):
        self.__vInfo = self.sessionProvider.getArenaDP().getVehicleInfo(long(ctx.vehicleID))
        player = self.__vInfo.player
        self.__userInfo = PlayerContextMenuInfo(player.accountDBID, player.name)

    def _clearFlashValues(self):
        self.__userInfo = None
        self.__vInfo = None
        return

    def _generateOptions(self, ctx = None):
        options = []
        options = self.__addDyncSquadInfo(options)
        options = self.__addFriendshipInfo(options)
        options = self.__addIgnoreInfo(options)
        options = self.__addCommunicationInfo(options)
        options = self.__addMutedInfo(options)
        options = self.__addDenunciationsInfo(options)
        return options

    @classmethod
    def _getOptionIcon(cls, optionID):
        return _OPTION_ICONS.get(optionID, '')

    @classmethod
    def _getOptionInitData(cls, optionID, isEnabled = True):
        return {'enabled': isEnabled,
         'iconType': cls._getOptionIcon(optionID)}

    def __addDyncSquadInfo(self, options):
        make = self._makeItem
        ctx = self.sessionProvider.getCtx()
        if not ctx.isInvitationEnabled() or ctx.hasSquadRestrictions():
            return options
        elif not self.__userInfo.isAlly:
            return options
        else:
            contact = self.usersStorage.getUser(self.__userInfo.databaseID)
            isIgnored = contact is not None and contact.isIgnored()
            status = self.__vInfo.invitationDeliveryStatus
            if status & _D_STATUS.FORBIDDEN_BY_RECEIVER > 0 or status & _D_STATUS.SENT_TO > 0 and not status & _D_STATUS.SENT_INACTIVE:
                optionID = DYN_SQUAD_OPTION_ID.SENT_INVITATION
                isEnabled = False
            elif status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                optionID = None
            elif self.__vInfo.isSquadMan():
                optionID = DYN_SQUAD_OPTION_ID.IN_SQUAD
                isEnabled = False
            else:
                optionID = DYN_SQUAD_OPTION_ID.SENT_INVITATION
                isEnabled = not isIgnored
            if optionID is not None:
                options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
            if status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                options.append(make(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION), optInitData=self._getOptionInitData(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION, not isIgnored)))
                options.append(make(DYN_SQUAD_OPTION_ID.REJECT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.REJECT_INVITATION), optInitData=self._getOptionInitData(DYN_SQUAD_OPTION_ID.REJECT_INVITATION, not isIgnored)))
            return options

    def __addFriendshipInfo(self, options):
        isEnabled = self.__userInfo.isSameRealm
        if self.__userInfo.isFriend:
            optionID = USER.REMOVE_FROM_FRIENDS
        else:
            optionID = USER.ADD_TO_FRIENDS
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addIgnoreInfo(self, options):
        isEnabled = self.__userInfo.isSameRealm
        if self.__userInfo.isTemporaryIgnored:
            optionID = USER.ADD_TO_IGNORED
            isEnabled = False
        elif self.__userInfo.isIgnored:
            optionID = USER.REMOVE_FROM_IGNORED
        else:
            optionID = USER.ADD_TO_IGNORED
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addCommunicationInfo(self, options):
        if self.__userInfo.isAlly and not self.arenaGuiType.isTrainingBattle():
            isEnabled = self.__userInfo.isSameRealm
            if self.__userInfo.isTemporaryIgnored:
                optionID = BATTLE_CHAT_OPTION_ID.ENABLE_COMMUNICATIONS
            elif not self.__userInfo.isIgnored:
                optionID = BATTLE_CHAT_OPTION_ID.DISABLE_COMMUNICATIONS
            else:
                optionID = BATTLE_CHAT_OPTION_ID.DISABLE_COMMUNICATIONS
                isEnabled = False
            options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addMutedInfo(self, options):
        isVisible = self.bwProto.voipController.isVOIPEnabled() and (self.__userInfo.isAlly or self.arenaGuiType.isTrainingBattle())
        isEnabled = not self.__userInfo.isIgnored or self.__userInfo.isTemporaryIgnored
        if self.__userInfo.isMuted:
            optionID = USER.UNSET_MUTED
        else:
            optionID = USER.SET_MUTED
        if isVisible:
            options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __isAppealsForTopicEnabled(self, topic):
        topicID = DENUNCIATIONS_MAP[topic]
        return self.__denunciator.isAppealsForTopicEnabled(self.__userInfo.databaseID, topicID, self.__arenaUniqueID)

    def __addDenunciationsInfo(self, options):
        make = self._makeItem
        if self.__userInfo.isAlly or self.arenaGuiType.isTrainingBattle():
            order = DENUNCIATIONS.ORDER
        else:
            order = DENUNCIATIONS.ENEMY_ORDER
        sub = [ make(denunciation, MENU.contextmenu(denunciation), optInitData={'enabled': self.__isAppealsForTopicEnabled(denunciation)}) for denunciation in order ]
        label = '{} {}/{}'.format(i18n.makeString(MENU.CONTEXTMENU_APPEAL), self.__denunciator.getDenunciationsLeft(), DENUNCIATIONS_PER_DAY)
        options.append(make(DENUNCIATIONS.APPEAL, label, optInitData={'enabled': self.__denunciator.isAppealsEnabled()}, optSubMenu=sub))
        return options

    def __handleHideCursor(self, _):
        self.onContextMenuHide()
class PlayerMenuHandler(AbstractContextMenuHandler):

    def __init__(self, cmProxy, ctx = None):
        self.__denunciator = BattleDenunciator()
        g_eventBus.addListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        super(PlayerMenuHandler, self).__init__(cmProxy, ctx=ctx, handlers=_OPTIONS_HANDLERS)

    @proto_getter(PROTO_TYPE.MIGRATION)
    def proto(self):
        return None

    @storage_getter('users')
    def usersStorage(self):
        return None

    @proto_getter(PROTO_TYPE.BW_CHAT2)
    def bwProto(self):
        return None

    def fini(self):
        g_eventBus.removeListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        self.__denunciator = None
        super(PlayerMenuHandler, self).fini()
        return

    def addFriend(self):
        self.proto.contacts.addFriend(self.__userInfo.databaseID, self.__userInfo.userName)

    def removeFriend(self):
        self.proto.contacts.removeFriend(self.__userInfo.databaseID)

    def setIgnored(self):
        self.proto.contacts.addIgnored(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetIgnored(self):
        self.proto.contacts.removeIgnored(self.__userInfo.databaseID)

    def setMuted(self):
        self.proto.contacts.setMuted(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetMuted(self):
        self.proto.contacts.unsetMuted(self.__userInfo.databaseID)

    def appealOffend(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.OFFEND)

    def appealFlood(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.FLOOD)

    def appealBlackmail(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.BLACKMAIL)

    def appealSwindle(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.SWINDLE)

    def appealNotFairPlay(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.NOT_FAIR_PLAY)

    def appealForbiddenNick(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.FORBIDDEN_NICK)

    def appealBot(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.BOT)

    def sendInvitation(self):
        g_sessionProvider.invitations.send(self.__userInfo.databaseID)

    def acceptInvitation(self):
        g_sessionProvider.invitations.accept(self.__userInfo.databaseID)

    def rejectInvitation(self):
        g_sessionProvider.invitations.reject(self.__userInfo.databaseID)

    def _initFlashValues(self, ctx):
        self.__vInfo = g_sessionProvider.getArenaDP().getVehicleInfo(long(ctx.vehicleID))
        player = self.__vInfo.player
        self.__userInfo = UserContextMenuInfo(player.accountDBID, player.name)

    def _clearFlashValues(self):
        self.__userInfo = None
        self.__vInfo = None
        return

    def _generateOptions(self, ctx = None):
        options = []
        options = self.__addDyncSquadInfo(options)
        options = self.__addFriendshipInfo(options)
        options = self.__addIgnoreInfo(options)
        options = self.__addDenunciationsInfo(options)
        options = self.__addMutedInfo(options)
        return options

    def __addDyncSquadInfo(self, options):
        make = self._makeItem
        ctx = g_sessionProvider.getCtx()
        if not ctx.isInvitationEnabled() or ctx.hasSquadRestrictions():
            return options
        elif not g_sessionProvider.getArenaDP().isAllyTeam(self.__vInfo.team):
            return options
        else:
            contact = self.usersStorage.getUser(self.__userInfo.databaseID)
            isIgnored = contact is not None and contact.isIgnored()
            status = self.__vInfo.invitationDeliveryStatus
            if status & _D_STATUS.FORBIDDEN_BY_RECEIVER > 0 or status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE or status & _D_STATUS.SENT_TO > 0 and not status & _D_STATUS.SENT_INACTIVE:
                options.append(make(DYN_SQUAD_OPTION_ID.SENT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.SENT_INVITATION), optInitData={'enabled': False}))
            elif self.__vInfo.isSquadMan():
                options.append(make(DYN_SQUAD_OPTION_ID.IN_SQUAD, MENU.contextmenu(DYN_SQUAD_OPTION_ID.IN_SQUAD), optInitData={'enabled': False}))
            else:
                options.append(make(DYN_SQUAD_OPTION_ID.SENT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.SENT_INVITATION), optInitData={'enabled': not isIgnored}))
            if status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                options.append(make(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION), optInitData={'enabled': not isIgnored}))
                options.append(make(DYN_SQUAD_OPTION_ID.REJECT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.REJECT_INVITATION)))
            return options

    def __addFriendshipInfo(self, options):
        data = {'enabled': self.__userInfo.isSameRealm}
        if self.__userInfo.isFriend:
            optionID = USER.REMOVE_FROM_FRIENDS
        else:
            optionID = USER.ADD_TO_FRIENDS
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=data))
        return options

    def __addIgnoreInfo(self, options):
        data = {'enabled': self.__userInfo.isSameRealm}
        if self.__userInfo.isIgnored:
            optionID = USER.REMOVE_FROM_IGNORED
        else:
            optionID = USER.ADD_TO_IGNORED
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=data))
        return options

    def __addMutedInfo(self, options):
        muted = USER.UNSET_MUTED if self.__userInfo.isMuted else USER.SET_MUTED
        if not self.__userInfo.isIgnored and self.bwProto.voipController.isVOIPEnabled():
            options.append(self._makeItem(muted, MENU.contextmenu(muted)))
        return options

    def __addDenunciationsInfo(self, options):
        make = self._makeItem
        sub = [ make(value, MENU.contextmenu(value)) for value in DENUNCIATIONS.ORDER ]
        label = '{} ({})'.format(i18n.makeString(MENU.CONTEXTMENU_APPEAL), self.__denunciator.getDenunciationsLeft())
        options.append(make(DENUNCIATIONS.APPEAL, label, optInitData={'enabled': self.__denunciator.isAppealsEnabled()}, optSubMenu=sub))
        return options

    def __handleHideCursor(self, _):
        self.onContextMenuHide()
class PlayerMenuHandler(AbstractContextMenuHandler):

    def __init__(self, cmProxy, ctx = None):
        self.__denunciator = BattleDenunciator()
        self.__arenaUniqueID = BattleDenunciator.getArenaUniqueID()
        g_eventBus.addListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        super(PlayerMenuHandler, self).__init__(cmProxy, ctx=ctx, handlers=_OPTIONS_HANDLERS)

    @proto_getter(PROTO_TYPE.MIGRATION)
    def proto(self):
        return None

    @storage_getter('users')
    def usersStorage(self):
        return None

    @proto_getter(PROTO_TYPE.BW_CHAT2)
    def bwProto(self):
        return None

    @property
    def arenaGuiType(self):
        return g_sessionProvider.arenaVisitor.gui

    def fini(self):
        g_eventBus.removeListener(events.GameEvent.HIDE_CURSOR, self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        self.__denunciator = None
        super(PlayerMenuHandler, self).fini()
        return

    def addFriend(self):
        self.proto.contacts.addFriend(self.__userInfo.databaseID, self.__userInfo.userName)

    def removeFriend(self):
        self.proto.contacts.removeFriend(self.__userInfo.databaseID)

    def setIgnored(self):
        self.proto.contacts.addIgnored(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetIgnored(self):
        self.proto.contacts.removeIgnored(self.__userInfo.databaseID)

    def disableCommunications(self):
        self.proto.contacts.addTmpIgnored(self.__userInfo.databaseID, self.__userInfo.userName)

    def enableCommunications(self):
        self.proto.contacts.removeTmpIgnored(self.__userInfo.databaseID)

    def setMuted(self):
        self.proto.contacts.setMuted(self.__userInfo.databaseID, self.__userInfo.userName)

    def unsetMuted(self):
        self.proto.contacts.unsetMuted(self.__userInfo.databaseID)

    def appealIncorrectBehavior(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.INCORRECT_BEHAVIOR, self.__arenaUniqueID)

    def appealNotFairPlay(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.NOT_FAIR_PLAY, self.__arenaUniqueID)

    def appealForbiddenNick(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.FORBIDDEN_NICK, self.__arenaUniqueID)

    def appealBot(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID, self.__userInfo.userName, DENUNCIATIONS.BOT, self.__arenaUniqueID)

    def sendInvitation(self):
        g_sessionProvider.invitations.send(self.__userInfo.databaseID)

    def acceptInvitation(self):
        g_sessionProvider.invitations.accept(self.__userInfo.databaseID)

    def rejectInvitation(self):
        g_sessionProvider.invitations.reject(self.__userInfo.databaseID)

    def _initFlashValues(self, ctx):
        self.__vInfo = g_sessionProvider.getArenaDP().getVehicleInfo(long(ctx.vehicleID))
        player = self.__vInfo.player
        self.__userInfo = PlayerContextMenuInfo(player.accountDBID, player.name)

    def _clearFlashValues(self):
        self.__userInfo = None
        self.__vInfo = None
        return

    def _generateOptions(self, ctx = None):
        options = []
        options = self.__addDyncSquadInfo(options)
        options = self.__addFriendshipInfo(options)
        options = self.__addIgnoreInfo(options)
        options = self.__addCommunicationInfo(options)
        options = self.__addMutedInfo(options)
        options = self.__addDenunciationsInfo(options)
        return options

    @classmethod
    def _getOptionIcon(cls, optionID):
        return _OPTION_ICONS.get(optionID, '')

    @classmethod
    def _getOptionInitData(cls, optionID, isEnabled = True):
        return {'enabled': isEnabled,
         'iconType': cls._getOptionIcon(optionID)}

    def __addDyncSquadInfo(self, options):
        make = self._makeItem
        ctx = g_sessionProvider.getCtx()
        if not ctx.isInvitationEnabled() or ctx.hasSquadRestrictions():
            return options
        elif not self.__userInfo.isAlly:
            return options
        else:
            contact = self.usersStorage.getUser(self.__userInfo.databaseID)
            isIgnored = contact is not None and contact.isIgnored()
            status = self.__vInfo.invitationDeliveryStatus
            if status & _D_STATUS.FORBIDDEN_BY_RECEIVER > 0 or status & _D_STATUS.SENT_TO > 0 and not status & _D_STATUS.SENT_INACTIVE:
                optionID = DYN_SQUAD_OPTION_ID.SENT_INVITATION
                isEnabled = False
            elif status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                optionID = None
            elif self.__vInfo.isSquadMan():
                optionID = DYN_SQUAD_OPTION_ID.IN_SQUAD
                isEnabled = False
            else:
                optionID = DYN_SQUAD_OPTION_ID.SENT_INVITATION
                isEnabled = not isIgnored
            if optionID is not None:
                options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
            if status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                options.append(make(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION), optInitData=self._getOptionInitData(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION, not isIgnored)))
                options.append(make(DYN_SQUAD_OPTION_ID.REJECT_INVITATION, MENU.contextmenu(DYN_SQUAD_OPTION_ID.REJECT_INVITATION), optInitData=self._getOptionInitData(DYN_SQUAD_OPTION_ID.REJECT_INVITATION, not isIgnored)))
            return options

    def __addFriendshipInfo(self, options):
        isEnabled = self.__userInfo.isSameRealm
        if self.__userInfo.isFriend:
            optionID = USER.REMOVE_FROM_FRIENDS
        else:
            optionID = USER.ADD_TO_FRIENDS
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addIgnoreInfo(self, options):
        isEnabled = self.__userInfo.isSameRealm
        if self.__userInfo.isTemporaryIgnored:
            optionID = USER.ADD_TO_IGNORED
            isEnabled = False
        elif self.__userInfo.isIgnored:
            optionID = USER.REMOVE_FROM_IGNORED
        else:
            optionID = USER.ADD_TO_IGNORED
        options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addCommunicationInfo(self, options):
        if self.__userInfo.isAlly and not self.arenaGuiType.isTrainingBattle():
            isEnabled = self.__userInfo.isSameRealm
            if self.__userInfo.isTemporaryIgnored:
                optionID = BATTLE_CHAT_OPTION_ID.ENABLE_COMMUNICATIONS
            elif not self.__userInfo.isIgnored:
                optionID = BATTLE_CHAT_OPTION_ID.DISABLE_COMMUNICATIONS
            else:
                optionID = BATTLE_CHAT_OPTION_ID.DISABLE_COMMUNICATIONS
                isEnabled = False
            options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __addMutedInfo(self, options):
        isVisible = self.bwProto.voipController.isVOIPEnabled() and (self.__userInfo.isAlly or self.arenaGuiType.isTrainingBattle())
        isEnabled = not self.__userInfo.isIgnored or self.__userInfo.isTemporaryIgnored
        if self.__userInfo.isMuted:
            optionID = USER.UNSET_MUTED
        else:
            optionID = USER.SET_MUTED
        if isVisible:
            options.append(self._makeItem(optionID, MENU.contextmenu(optionID), optInitData=self._getOptionInitData(optionID, isEnabled)))
        return options

    def __isAppealsForTopicEnabled(self, topic):
        topicID = DENUNCIATIONS_MAP[topic]
        return self.__denunciator.isAppealsForTopicEnabled(self.__userInfo.databaseID, topicID, self.__arenaUniqueID)

    def __addDenunciationsInfo(self, options):
        make = self._makeItem
        if self.__userInfo.isAlly or self.arenaGuiType.isTrainingBattle():
            order = DENUNCIATIONS.ORDER
        else:
            order = DENUNCIATIONS.ENEMY_ORDER
        sub = [ make(denunciation, MENU.contextmenu(denunciation), optInitData={'enabled': self.__isAppealsForTopicEnabled(denunciation)}) for denunciation in order ]
        label = '{} {}/{}'.format(i18n.makeString(MENU.CONTEXTMENU_APPEAL), self.__denunciator.getDenunciationsLeft(), DENUNCIATIONS_PER_DAY)
        options.append(make(DENUNCIATIONS.APPEAL, label, optInitData={'enabled': self.__denunciator.isAppealsEnabled()}, optSubMenu=sub))
        return options

    def __handleHideCursor(self, _):
        self.onContextMenuHide()
示例#4
0
class PlayerMenuHandler(AbstractContextMenuHandler):
    def __init__(self, cmProxy, ctx=None):
        self.__denunciator = BattleDenunciator()
        g_eventBus.addListener(events.GameEvent.HIDE_CURSOR,
                               self.__handleHideCursor, EVENT_BUS_SCOPE.GLOBAL)
        super(PlayerMenuHandler, self).__init__(cmProxy,
                                                ctx=ctx,
                                                handlers=_OPTIONS_HANDLERS)

    @proto_getter(PROTO_TYPE.MIGRATION)
    def proto(self):
        return None

    @storage_getter('users')
    def usersStorage(self):
        return None

    @proto_getter(PROTO_TYPE.BW_CHAT2)
    def bwProto(self):
        return None

    def fini(self):
        g_eventBus.removeListener(events.GameEvent.HIDE_CURSOR,
                                  self.__handleHideCursor,
                                  EVENT_BUS_SCOPE.GLOBAL)
        self.__denunciator = None
        super(PlayerMenuHandler, self).fini()
        return

    def addFriend(self):
        self.proto.contacts.addFriend(self.__userInfo.databaseID,
                                      self.__userInfo.userName)

    def removeFriend(self):
        self.proto.contacts.removeFriend(self.__userInfo.databaseID)

    def setIgnored(self):
        self.proto.contacts.addIgnored(self.__userInfo.databaseID,
                                       self.__userInfo.userName)

    def unsetIgnored(self):
        self.proto.contacts.removeIgnored(self.__userInfo.databaseID)

    def setMuted(self):
        self.proto.contacts.setMuted(self.__userInfo.databaseID,
                                     self.__userInfo.userName)

    def unsetMuted(self):
        self.proto.contacts.unsetMuted(self.__userInfo.databaseID)

    def appealOffend(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.OFFEND)

    def appealFlood(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.FLOOD)

    def appealBlackmail(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.BLACKMAIL)

    def appealSwindle(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.SWINDLE)

    def appealNotFairPlay(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.NOT_FAIR_PLAY)

    def appealForbiddenNick(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.FORBIDDEN_NICK)

    def appealBot(self):
        self.__denunciator.makeAppeal(self.__userInfo.databaseID,
                                      self.__userInfo.userName,
                                      DENUNCIATIONS.BOT)

    def sendInvitation(self):
        g_sessionProvider.invitations.send(self.__userInfo.databaseID)

    def acceptInvitation(self):
        g_sessionProvider.invitations.accept(self.__userInfo.databaseID)

    def rejectInvitation(self):
        g_sessionProvider.invitations.reject(self.__userInfo.databaseID)

    def _initFlashValues(self, ctx):
        self.__vInfo = g_sessionProvider.getArenaDP().getVehicleInfo(
            long(ctx.vehicleID))
        player = self.__vInfo.player
        self.__userInfo = UserContextMenuInfo(player.accountDBID, player.name)

    def _clearFlashValues(self):
        self.__userInfo = None
        self.__vInfo = None
        return

    def _generateOptions(self, ctx=None):
        options = []
        options = self.__addDyncSquadInfo(options)
        options = self.__addFriendshipInfo(options)
        options = self.__addIgnoreInfo(options)
        options = self.__addDenunciationsInfo(options)
        options = self.__addMutedInfo(options)
        return options

    def __addDyncSquadInfo(self, options):
        make = self._makeItem
        ctx = g_sessionProvider.getCtx()
        if not ctx.isInvitationEnabled() or ctx.hasSquadRestrictions():
            return options
        elif not g_sessionProvider.getArenaDP().isAllyTeam(self.__vInfo.team):
            return options
        else:
            contact = self.usersStorage.getUser(self.__userInfo.databaseID)
            isIgnored = contact is not None and contact.isIgnored()
            status = self.__vInfo.invitationDeliveryStatus
            if status & _D_STATUS.FORBIDDEN_BY_RECEIVER > 0 or status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE or status & _D_STATUS.SENT_TO > 0 and not status & _D_STATUS.SENT_INACTIVE:
                options.append(
                    make(DYN_SQUAD_OPTION_ID.SENT_INVITATION,
                         MENU.contextmenu(DYN_SQUAD_OPTION_ID.SENT_INVITATION),
                         optInitData={'enabled': False}))
            elif self.__vInfo.isSquadMan():
                options.append(
                    make(DYN_SQUAD_OPTION_ID.IN_SQUAD,
                         MENU.contextmenu(DYN_SQUAD_OPTION_ID.IN_SQUAD),
                         optInitData={'enabled': False}))
            else:
                options.append(
                    make(DYN_SQUAD_OPTION_ID.SENT_INVITATION,
                         MENU.contextmenu(DYN_SQUAD_OPTION_ID.SENT_INVITATION),
                         optInitData={'enabled': not isIgnored}))
            if status & _D_STATUS.RECEIVED_FROM > 0 and not status & _D_STATUS.RECEIVED_INACTIVE:
                options.append(
                    make(DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION,
                         MENU.contextmenu(
                             DYN_SQUAD_OPTION_ID.ACCEPT_INVITATION),
                         optInitData={'enabled': not isIgnored}))
                options.append(
                    make(
                        DYN_SQUAD_OPTION_ID.REJECT_INVITATION,
                        MENU.contextmenu(
                            DYN_SQUAD_OPTION_ID.REJECT_INVITATION)))
            return options

    def __addFriendshipInfo(self, options):
        data = {'enabled': self.__userInfo.isSameRealm}
        if self.__userInfo.isFriend:
            optionID = USER.REMOVE_FROM_FRIENDS
        else:
            optionID = USER.ADD_TO_FRIENDS
        options.append(
            self._makeItem(optionID,
                           MENU.contextmenu(optionID),
                           optInitData=data))
        return options

    def __addIgnoreInfo(self, options):
        data = {'enabled': self.__userInfo.isSameRealm}
        if self.__userInfo.isIgnored:
            optionID = USER.REMOVE_FROM_IGNORED
        else:
            optionID = USER.ADD_TO_IGNORED
        options.append(
            self._makeItem(optionID,
                           MENU.contextmenu(optionID),
                           optInitData=data))
        return options

    def __addMutedInfo(self, options):
        muted = USER.UNSET_MUTED if self.__userInfo.isMuted else USER.SET_MUTED
        if not self.__userInfo.isIgnored and self.bwProto.voipController.isVOIPEnabled(
        ):
            options.append(self._makeItem(muted, MENU.contextmenu(muted)))
        return options

    def __addDenunciationsInfo(self, options):
        make = self._makeItem
        sub = [
            make(value, MENU.contextmenu(value))
            for value in DENUNCIATIONS.ORDER
        ]
        label = '{} ({})'.format(i18n.makeString(MENU.CONTEXTMENU_APPEAL),
                                 self.__denunciator.getDenunciationsLeft())
        options.append(
            make(
                DENUNCIATIONS.APPEAL,
                label,
                optInitData={'enabled': self.__denunciator.isAppealsEnabled()},
                optSubMenu=sub))
        return options

    def __handleHideCursor(self, _):
        self.onContextMenuHide()