示例#1
0
 def __init__(self):
     super(BWChatProvider, self).__init__()
     self.__handlers = defaultdict(set)
     self.__msgFilters = None
     self.__coolDown = _ChatCooldownManager()
     self.__idGen = SequenceIDGenerator()
     self.__isEnabled = False
     self.__queue = []
示例#2
0
 def __init__(self, controller):
     self.__controller = weakref.proxy(controller)
     self.__idGen = SequenceIDGenerator()
     self.__cache = {}
     self.__idToIndex = {}
     self.__indexToID = {}
     self.__selectedUnit = None
     self.__isRequestInProcess = False
示例#3
0
 def __init__(self, proxy):
     super(BrowserController, self).__init__(proxy)
     self.__browsers = {}
     self.__browsersCallbacks = {}
     self.__browserIDGenerator = SequenceIDGenerator()
     self.__eventMgr = Event.EventManager()
     self.onBrowserAdded = Event.Event(self.__eventMgr)
     self.onBrowserDeleted = Event.Event(self.__eventMgr)
     self.__urlMacros = URLMarcos()
示例#4
0
 def __init__(self, ctx, *args, **kwargs):
     settings = ViewSettings(R.views.lobby.marathon.RewardWindow())
     settings.flags = ViewFlags.VIEW
     settings.model = MarathonPrizeRewardModel()
     super(MarathonRewardWindowView, self).__init__(settings, *args, **kwargs)
     self.__rewards = []
     self.__convertRewards(ctx)
     self.__marathon = self.marathonController.getMarathon(ctx.get('marathonPrefix'))
     self.__idGen = SequenceIDGenerator()
     self.__bonusCache = {}
示例#5
0
 def __init__(self):
     from gui.prb_control.formatters.invites import PrbInviteLinkFormatter
     self.__linkFormatter = PrbInviteLinkFormatter()
     self._IDGen = SequenceIDGenerator()
     self._IDMap = {'inviteIDs': {}, 'prbIDs': {}}
     self.__receivedInvites = {}
     self.__unreadInvitesCount = 0
     self.__eventManager = Event.EventManager()
     self.__acceptChain = None
     self.onReceivedInviteListInited = Event.Event(self.__eventManager)
     self.onReceivedInviteListModified = Event.Event(self.__eventManager)
     return
 def __init__(self):
     super(BrowserController, self).__init__()
     self.__browsers = {}
     self.__browsersCallbacks = {}
     self.__browserIDGenerator = SequenceIDGenerator()
     self.__eventMgr = Event.EventManager()
     self.onBrowserAdded = Event.Event(self.__eventMgr)
     self.onBrowserDeleted = Event.Event(self.__eventMgr)
     self.__urlMacros = URLMarcos()
     self.__pendingBrowsers = {}
     self.__creatingBrowserID = None
     self.__filters = _getGlobalFilters()
     return
示例#7
0
 def __init__(self, loader):
     super(InvitesManager, self).__init__()
     self.__loader = loader
     self._IDGen = SequenceIDGenerator()
     self._IDMap = {}
     self.__invites = {}
     self.__unreadInvitesCount = 0
     self.__eventManager = Event.EventManager()
     self.__acceptChain = None
     self.onInvitesListInited = Event.Event(self.__eventManager)
     self.onReceivedInviteListModified = Event.Event(self.__eventManager)
     self.onSentInviteListModified = Event.Event(self.__eventManager)
     self.__isInBattle = False
示例#8
0
 def __init__(self):
     super(RibbonsAggregator, self).__init__()
     self.__feedbackProvider = None
     self.__vehicleStateCtrl = None
     self.__cache = _RibbonsCache()
     self.__accumulatedRibbons = _RibbonsCache()
     self.__rules = {}
     self.__idGenerator = SequenceIDGenerator()
     self.onRibbonAdded = Event.Event()
     self.onRibbonUpdated = Event.Event()
     self.__isStarted = False
     self.__isSuspended = False
     self.__isInPostmortemMode = False
     return
示例#9
0
 def __init__(self):
     self.__client = BigWorld.XmppClient()
     self.__currentUser = None
     self.__xmppRoster = None
     self.__usersMgr = None
     self.__model = None
     self.__bwRoster = None
     self.__reconnectCount = 0
     self.__reconnectCallbackId = None
     self.__bwConnected = False
     self.__isEnabled = False
     self.__idsGen = SequenceIDGenerator()
     self.__pendingBattleMode = None
     self.__databaseID = None
     return
示例#10
0
class BattleBoosterSlotContextMenu(BaseSlotContextMenu):
    __sqGen = SequenceIDGenerator()

    @option(__sqGen.next(), CMLabel.BUY_MORE)
    def buyMore(self):
        shop.showBattleBoosterOverlay(itemId=self._intCD,
                                      source=shop.Source.EXTERNAL,
                                      origin=shop.Origin.BATTLE_BOOSTERS,
                                      alias=VIEW_ALIAS.BROWSER_LOBBY_TOP_SUB)

    @option(__sqGen.next(), TankSetupCMLabel.UNLOAD)
    def unload(self):
        self._sendSlotAction(BaseSetupModel.SELECT_SLOT_ACTION,
                             intCD=None,
                             currentSlotId=self._installedSlotId)
        return

    @option(__sqGen.next(), TankSetupCMLabel.TAKE_OFF)
    def takeOff(self):
        self._sendSlotAction(BaseSetupModel.REVERT_SLOT_ACTION)

    def _isVisible(self, label):
        return not self._itemsCache.items.getItemByCD(
            self._intCD).isHidden if label == CMLabel.BUY_MORE else super(
                BattleBoosterSlotContextMenu, self)._isVisible(label)

    def _getVehicleItems(self):
        return self._getVehicle().battleBoosters
class BattleBoostersCMHandler(ContextMenu):
    __sqGen = SequenceIDGenerator()
    _itemsCache = dependency.descriptor(IItemsCache)

    def _initFlashValues(self, ctx):
        super(BattleBoostersCMHandler, self)._initFlashValues(ctx)
        self._enabled = bool(ctx.enabled)

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        shared_events.showStorageModuleInfo(self._id)

    @option(__sqGen.next(), CMLabel.SELL)
    def sell(self):
        showBattleBoosterSellDialog(self._id)

    @option(__sqGen.next(), CMLabel.BUY_MORE)
    def buy(self):
        shop.showBattleBoosterOverlay(self._id,
                                      source=_SOURCE,
                                      origin=_ORIGIN,
                                      alias=VIEW_ALIAS.BROWSER_LOBBY_TOP_SUB)

    def _getOptionCustomData(self, label):
        optionData = super(BattleBoostersCMHandler,
                           self)._getOptionCustomData(label)
        if label == CMLabel.INFORMATION:
            optionData.enabled = False
        elif label == CMLabel.SELL:
            optionData.enabled = self._enabled
        elif label == CMLabel.BUY_MORE:
            optionData.textColor = CM_BUY_COLOR
        return optionData
class OptionalDeviceCMHandler(EquipmentCMHandler):
    _sqGen = SequenceIDGenerator()
    _itemsCache = dependency.descriptor(IItemsCache)
    __lobbyContext = dependency.descriptor(ILobbyContext)

    @option(_sqGen.next(), CMLabel.UPGRADE)
    def upgrade(self):
        module = self._itemsCache.items.getItemByCD(int(self._id))
        ItemsActionsFactory.doAction(ItemsActionsFactory.UPGRADE_OPT_DEVICE,
                                     module, None, None)
        return

    def _generateOptions(self, ctx=None):
        options = super(OptionalDeviceCMHandler, self)._generateOptions(ctx)
        module = self._itemsCache.items.getItemByCD(int(self._id))
        if not module.isUpgradable or not self.__lobbyContext.getServerSettings(
        ).isTrophyDevicesEnabled():
            options = [
                item for item in options if item['id'] != CMLabel.UPGRADE
            ]
        return options

    def _getOptionCustomData(self, label):
        optionData = super(OptionalDeviceCMHandler,
                           self)._getOptionCustomData(label)
        if label == CMLabel.BUY_MORE:
            optionData.textColor = CM_BUY_COLOR
        return optionData
示例#13
0
class EquipmentsPlugin(common.IntervalPlugin):
    __slots__ = ('__generator',)

    def __init__(self, parent):
        super(EquipmentsPlugin, self).__init__(parent)
        self.__generator = SequenceIDGenerator()

    def start(self):
        super(EquipmentsPlugin, self).start()
        ctrl = self.sessionProvider.shared.equipments
        if ctrl is not None:
            ctrl.onEquipmentMarkerShown += self.__onEquipmentMarkerShown
        return

    def stop(self):
        ctrl = self.sessionProvider.shared.equipments
        if ctrl is not None:
            ctrl.onEquipmentMarkerShown -= self.__onEquipmentMarkerShown
        super(EquipmentsPlugin, self).stop()
        return

    def __onEquipmentMarkerShown(self, equipment, position, _, interval):
        uniqueID = self.__generator.next()
        marker = equipment.getMarker()
        if marker in settings.EQ_MARKER_TO_SYMBOL:
            symbol = settings.EQ_MARKER_TO_SYMBOL[marker]
        else:
            LOG_ERROR('Symbol is not found for equipment', equipment)
            return
        matrix = minimap_utils.makePositionMatrix(position)
        model = self._addEntryEx(uniqueID, symbol, _C_NAME.EQUIPMENTS, matrix=matrix, active=True)
        if model is not None:
            self._setCallback(uniqueID, int(interval))
        return
示例#14
0
class EquipmentsPlugin(common.IntervalPlugin):
    __slots__ = ('__generator',)

    def __init__(self, parent):
        super(EquipmentsPlugin, self).__init__(parent)
        self.__generator = SequenceIDGenerator()

    def start(self):
        super(EquipmentsPlugin, self).start()
        ctrl = g_sessionProvider.shared.equipments
        if ctrl is not None:
            ctrl.onEquipmentMarkerShown += self.__onEquipmentMarkerShown
        return

    def stop(self):
        ctrl = g_sessionProvider.shared.equipments
        if ctrl is not None:
            ctrl.onEquipmentMarkerShown -= self.__onEquipmentMarkerShown
        super(EquipmentsPlugin, self).stop()
        return

    def __onEquipmentMarkerShown(self, equipment, position, _, interval):
        uniqueID = self.__generator.next()
        marker = equipment.getMarker()
        if marker in settings.EQ_MARKER_TO_SYMBOL:
            symbol = settings.EQ_MARKER_TO_SYMBOL[marker]
        else:
            LOG_ERROR('Symbol is not found for equipment', equipment)
            return
        matrix = minimap_utils.makePositionMatrix(position)
        model = self._addEntryEx(uniqueID, symbol, _C_NAME.EQUIPMENTS, matrix=matrix, active=True)
        if model is not None:
            self._setCallback(uniqueID, int(interval))
        return
class HangarShellItemContextMenu(BaseHangarEquipmentSlotContextMenu):
    _sqGen = SequenceIDGenerator(
        BaseHangarEquipmentSlotContextMenu._sqGen.currSequenceID)

    def _initFlashValues(self, ctx):
        super(HangarShellItemContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicleItems().getShellsCount()

    def _putOnAction(self, onId):
        copyVehicle = self._getCopyVehicle()
        layout = copyVehicle.shells.layout
        self._makePutOnAction(TankSetupConstants.SHELLS, onId, copyVehicle,
                              layout)

    def _isVisible(self, label):
        return False if label == CMLabel.UPGRADE else super(
            HangarShellItemContextMenu, self)._isVisible(label)

    @async
    @process
    def _doPutOnAction(self, vehicle, callback):
        action = ActionsFactory.getAction(
            ActionsFactory.BUY_AND_INSTALL_SHELLS,
            vehicle,
            confirmOnlyExchange=True)
        result = yield ActionsFactory.asyncDoAction(action)
        callback(result)

    def _getVehicleItems(self):
        return self._getVehicle().shells
class ConsumableSlotContextMenu(BaseEquipmentSlotContextMenu):
    _sqGen = SequenceIDGenerator(
        BaseEquipmentSlotContextMenu._sqGen.currSequenceID)

    @option(_sqGen.next(), CMLabel.BUY_MORE)
    def buyMore(self):
        shop.showBuyEquipmentOverlay(itemId=self._intCD,
                                     source=shop.Source.EXTERNAL,
                                     origin=shop.Origin.CONSUMABLES,
                                     alias=VIEW_ALIAS.BROWSER_LOBBY_TOP_SUB)

    @option(_sqGen.next(), TankSetupCMLabel.UNLOAD)
    def unload(self):
        self._sendSlotAction(BaseSetupModel.SELECT_SLOT_ACTION,
                             intCD=None,
                             currentSlotId=self._installedSlotId)
        return

    @option(_sqGen.next(), TankSetupCMLabel.TAKE_OFF)
    def takeOff(self):
        self._sendSlotAction(BaseSetupModel.REVERT_SLOT_ACTION)

    def _initFlashValues(self, ctx):
        super(ConsumableSlotContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicleItems().installed.getCapacity()

    def _getVehicleItems(self):
        return self._getVehicle().consumables
示例#17
0
class BattleAbilitySlotContextMenu(BaseEquipmentSlotContextMenu):
    _sqGen = SequenceIDGenerator(
        BaseEquipmentSlotContextMenu._sqGen.currSequenceID)

    @option(_sqGen.next(), TankSetupCMLabel.TAKE_OFF)
    def takeOff(self):
        if self._isMounted:
            self._sendSlotAction(BaseSetupModel.SELECT_SLOT_ACTION,
                                 intCD=None,
                                 currentSlotId=self._installedSlotId)
        else:
            self._sendSlotAction(BaseSetupModel.REVERT_SLOT_ACTION)
        return

    def _initFlashValues(self, ctx):
        super(BattleAbilitySlotContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicleItems().installed.getCapacity()

    def _isVisible(self, label):
        if label == CMLabel.INFORMATION:
            return False
        return False if label == TankSetupCMLabel.TAKE_OFF else super(
            BattleAbilitySlotContextMenu, self)._isVisible(label)

    def _getVehicleItems(self):
        return self._getVehicle().battleAbilities
class ConsumableItemContextMenu(BaseEquipmentItemContextMenu):
    _sqGen = SequenceIDGenerator(
        BaseEquipmentItemContextMenu._sqGen.currSequenceID)

    @option(_sqGen.next(), CMLabel.BUY_MORE)
    def buyMore(self):
        shop.showBuyEquipmentOverlay(itemId=self._intCD,
                                     source=shop.Source.EXTERNAL,
                                     origin=shop.Origin.CONSUMABLES,
                                     alias=VIEW_ALIAS.BROWSER_LOBBY_TOP_SUB)

    def _getCopyVehicle(self):
        copyVehicle = super(ConsumableItemContextMenu, self)._getCopyVehicle()
        copyVehicle.consumables.setInstalled(*self._getVehicleItems().layout)
        return copyVehicle

    def _getOptionCustomData(self, label):
        optionData = super(ConsumableItemContextMenu,
                           self)._getOptionCustomData(label)
        for slotID, slotLabel in enumerate(TankSetupCMLabel.PUT_ON_LIST):
            if slotLabel == label:
                consumable = self._getVehicleItems().layout[slotID]
                optionData.enabled = optionData.enabled and (
                    consumable is None or not consumable.isBuiltIn)
                break

        return optionData

    def _initFlashValues(self, ctx):
        super(ConsumableItemContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicleItems().installed.getCapacity()

    def _getVehicleItems(self):
        return self._getVehicle().consumables
class _ArmingCMHandler(ContextMenu):
    _sqGen = SequenceIDGenerator()
    _itemsCache = dependency.descriptor(IItemsCache)

    @option(_sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        shared_events.showStorageModuleInfo(self._id)

    @option(_sqGen.next(), CMLabel.SELL)
    @process
    def sell(self):
        yield DialogsInterface.showDialog(SellModuleMeta(self._id))

    def buy(self):
        shared_events.showShop()

    def _getHighlightedLabels(self):
        return tuple()

    def _generateOptions(self, ctx=None):
        options = super(_ArmingCMHandler, self)._generateOptions(ctx)
        module = self._itemsCache.items.getItemByCD(int(self._id))
        if module.isHidden:
            return [item for item in options if item['id'] != CMLabel.BUY_MORE]
        return options

    def _getOptionCustomData(self, label):
        optionData = super(_ArmingCMHandler, self)._getOptionCustomData(label)
        if label in self._getHighlightedLabels():
            optionData.textColor = CM_BUY_COLOR
        return optionData
class ShellItemContextMenu(BaseItemContextMenu):
    __sqGen = SequenceIDGenerator()

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        self._sendSlotAction(BaseSetupModel.SHOW_INFO_SLOT_ACTION)

    @option(__sqGen.next(), TankSetupCMLabel.PUT_ON_FIRST)
    def putOnFirst(self):
        self._sendPutOnSlotAction(onId=FIRST_SLOT)

    @option(__sqGen.next(), TankSetupCMLabel.PUT_ON_SECOND)
    def putOnSecond(self):
        self._sendPutOnSlotAction(onId=SECOND_SLOT)

    @option(__sqGen.next(), TankSetupCMLabel.PUT_ON_THIRD)
    def putOnThird(self):
        self._sendPutOnSlotAction(onId=THIRD_SLOT)

    def _sendPutOnSlotAction(self, onId):
        view = self._getEmitterView()
        if view is None or self._slotType != view.getSelectedSetup():
            return
        else:
            view.sendSlotAction({
                'actionType': BaseSetupModel.SWAP_SLOTS_ACTION,
                'intCD': self._intCD,
                'leftID': min(onId, self._installedSlotId),
                'rightID': max(onId, self._installedSlotId)
            })
            return

    def _initFlashValues(self, ctx):
        super(ShellItemContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicle().shells.getShellsCount()
class PersonalReservesCMHandler(ContextMenu):
    __sqGen = SequenceIDGenerator()
    __goodiesCache = dependency.descriptor(IGoodiesCache)

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        shared_events.showStorageBoosterInfo(self._id)

    @option(__sqGen.next(), CMLabel.ACTIVATE)
    @process
    def activate(self):
        _ = yield shared_events.showBoosterActivateDialog(self._id)

    @option(__sqGen.next(), CMLabel.BUY_MORE)
    def buy(self):
        shop.showBuyPersonalReservesOverlay(self._id, _SOURCE, _ORIGIN)

    def _getOptionCustomData(self, label):
        optionData = super(PersonalReservesCMHandler,
                           self)._getOptionCustomData(label)
        if label == CMLabel.ACTIVATE:
            booster = self.__goodiesCache.getBooster(self._id)
            optionData.enabled = booster is not None and booster.isReadyToActivate
        elif label == CMLabel.BUY_MORE:
            booster = self.__goodiesCache.getBooster(self._id)
            if booster is not None and not booster.isHidden:
                optionData.textColor = CM_BUY_COLOR
            else:
                optionData.enabled = False
        return optionData
class ForSellCMHandler(ContextMenu, EventSystemEntity):
    __sqGen = SequenceIDGenerator()

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        shared_events.showStorageModuleInfo(self._id)

    @option(__sqGen.next(), CMLabel.SELL)
    @process
    def sell(self):
        yield DialogsInterface.showDialog(SellModuleMeta(self._id))

    @option(__sqGen.next(), CMLabel.SALE_OPTION)
    def switchSaleOption(self):
        self.fireEvent(events.StorageEvent(events.StorageEvent.SELECT_MODULE_FOR_SELL, ctx={'intCD': self._id}), scope=EVENT_BUS_SCOPE.LOBBY)

    def _getOptionCustomData(self, label):
        optionData = super(ForSellCMHandler, self)._getOptionCustomData(label)
        if label == CMLabel.SALE_OPTION:
            optionData.label = 'prohibitSale' if self._selected else 'allowSale'
        return optionData

    def _initFlashValues(self, ctx):
        super(ForSellCMHandler, self)._initFlashValues(ctx)
        self._selected = ctx.selected
class BaseEquipmentSlotContextMenu(BaseSlotContextMenu):
    __lobbyContext = dependency.descriptor(ILobbyContext)
    _sqGen = SequenceIDGenerator()

    @option(_sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        self._sendSlotAction(BaseSetupModel.SHOW_INFO_SLOT_ACTION)

    @option(_sqGen.next(), CMLabel.UPGRADE)
    def upgrade(self):
        self._upgradeEquipment()

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_FIRST)
    def putOnFirst(self):
        self._sendPutOnSlotAction(onId=FIRST_SLOT)

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_SECOND)
    def putOnSecond(self):
        self._sendPutOnSlotAction(onId=SECOND_SLOT)

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_THIRD)
    def putOnThird(self):
        self._sendPutOnSlotAction(onId=THIRD_SLOT)

    def _isVisible(self, label):
        return self._getItem().isUpgradable and self.__lobbyContext.getServerSettings().isTrophyDevicesEnabled() if label == CMLabel.UPGRADE else super(BaseEquipmentSlotContextMenu, self)._isVisible(label)

    def _upgradeEquipment(self):
        pass
示例#24
0
class BaseHangarEquipmentSlotContextMenu(BaseSlotContextMenu):
    __lobbyContext = dependency.descriptor(ILobbyContext)
    _sqGen = SequenceIDGenerator()

    @option(_sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        self._showInfo()

    @option(_sqGen.next(), CMLabel.UPGRADE)
    def upgrade(self):
        self._upgradeEquipment()

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_FIRST)
    def putOnFirst(self):
        self._putOnAction(onId=FIRST_SLOT)

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_SECOND)
    def putOnSecond(self):
        self._putOnAction(onId=SECOND_SLOT)

    @option(_sqGen.next(), TankSetupCMLabel.PUT_ON_THIRD)
    def putOnThird(self):
        self._putOnAction(onId=THIRD_SLOT)

    def _getVehicle(self):
        return g_currentVehicle.item

    def _showInfo(self):
        showModuleInfo(self._intCD, self._getVehicle().descriptor)

    def _putOnAction(self, onId):
        pass

    @process
    def _makePutOnAction(self, setupName, onId, copyVehicle, layout):
        leftID, rightID = sorted((onId, self._installedSlotId))
        leftItem, rightItem = layout[leftID], layout[rightID]
        layout[leftID], layout[rightID] = layout[rightID], layout[leftID]
        result = yield self._doPutOnAction(copyVehicle)
        if result:
            self._sendLastSlotAction(
                setupName, BaseSetupModel.SWAP_SLOTS_ACTION, {
                    'leftID': leftID,
                    'rightID': rightID,
                    'leftIntCD': leftItem.intCD if leftItem else NONE_ID,
                    'rightIntCD': rightItem.intCD if rightItem else NONE_ID
                })

    @async
    def _doPutOnAction(self, vehicle, callback):
        callback(False)

    def _isVisible(self, label):
        return self._getItem(
        ).isUpgradable and self.__lobbyContext.getServerSettings(
        ).isTrophyDevicesEnabled() if label == CMLabel.UPGRADE else super(
            BaseHangarEquipmentSlotContextMenu, self)._isVisible(label)

    def _upgradeEquipment(self):
        pass
示例#25
0
 def __init__(self):
     super(BWChatProvider, self).__init__()
     self.__handlers = defaultdict(set)
     self.__msgFilters = None
     self.__coolDown = _ChatCooldownManager()
     self.__idGen = SequenceIDGenerator()
     self.__isEnabled = False
     self.__queue = []
示例#26
0
 def __init__(self, controller):
     self.__controller = weakref.proxy(controller)
     self.__idGen = SequenceIDGenerator()
     self.__cache = {}
     self.__idToIndex = {}
     self.__indexToID = {}
     self.__selectedUnit = None
     self.__isRequestInProcess = False
示例#27
0
 def __init__(self, proxy):
     super(BrowserController, self).__init__(proxy)
     self.__browsers = {}
     self.__browsersCallbacks = {}
     self.__browserIDGenerator = SequenceIDGenerator()
     self.__eventMgr = Event.EventManager()
     self.onBrowserAdded = Event.Event(self.__eventMgr)
     self.onBrowserDeleted = Event.Event(self.__eventMgr)
     self.__urlMacros = URLMarcos()
示例#28
0
class SequenceIDLoader(EventSystemEntity, AppRef):
    __counter = SequenceIDGenerator()

    def __init__(self):
        super(SequenceIDLoader, self).__init__()

    def _loadView(self, alias, *args, **kwargs):
        self.app.loadView(alias, 'rw{0}'.format(self.__counter.next()), *args,
                          **kwargs)
示例#29
0
class BattleAbilityItemContextMenu(BaseEquipmentItemContextMenu):
    _sqGen = SequenceIDGenerator(BaseEquipmentItemContextMenu._sqGen.currSequenceID)

    def _initFlashValues(self, ctx):
        super(BattleAbilityItemContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicle().battleAbilities.installed.getCapacity()

    def _isVisible(self, label):
        return False if label == CMLabel.INFORMATION else super(BattleAbilityItemContextMenu, self)._isVisible(label)
示例#30
0
class VehModuleItemContextMenu(BaseItemContextMenu):
    __sqGen = SequenceIDGenerator()

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        self._sendSlotAction(BaseSetupModel.SHOW_INFO_SLOT_ACTION)

    @option(__sqGen.next(), TankSetupCMLabel.SELECT)
    def select(self):
        self._sendSlotAction(BaseSetupModel.SELECT_SLOT_ACTION)
class HangarConsumableSlotContextMenu(BaseHangarEquipmentSlotContextMenu):
    _sqGen = SequenceIDGenerator(
        BaseHangarEquipmentSlotContextMenu._sqGen.currSequenceID)

    @option(_sqGen.next(), CMLabel.BUY_MORE)
    def buyMore(self):
        shop.showBuyEquipmentOverlay(itemId=self._intCD,
                                     source=shop.Source.EXTERNAL,
                                     origin=shop.Origin.CONSUMABLES,
                                     alias=VIEW_ALIAS.BROWSER_LOBBY_TOP_SUB)

    @option(_sqGen.next(), TankSetupCMLabel.UNLOAD)
    def unload(self):
        self.__unloadAction()

    @option(_sqGen.next(), TankSetupCMLabel.TAKE_OFF)
    def takeOffFromSlot(self):
        self.__unloadAction()

    def _initFlashValues(self, ctx):
        super(HangarConsumableSlotContextMenu, self)._initFlashValues(ctx)
        self._slotsCount = self._getVehicleItems().installed.getCapacity()

    def _putOnAction(self, onId):
        copyVehicle = self._getCopyVehicle()
        copyVehicle.consumables.setLayout(*copyVehicle.consumables.installed)
        layout = copyVehicle.consumables.layout
        self._makePutOnAction(TankSetupConstants.CONSUMABLES, onId,
                              copyVehicle, layout)

    @async
    @process
    def _doPutOnAction(self, vehicle, callback):
        action = ActionsFactory.getAction(
            ActionsFactory.BUY_AND_INSTALL_CONSUMABLES,
            vehicle,
            confirmOnlyExchange=True)
        result = yield ActionsFactory.asyncDoAction(action)
        callback(result)

    def _getVehicleItems(self):
        return self._getVehicle().consumables

    @process
    def __unloadAction(self):
        copyVehicle = self._getCopyVehicle()
        copyVehicle.consumables.setLayout(*copyVehicle.consumables.installed)
        copyVehicle.consumables.layout[self._installedSlotId] = None
        result = yield self._doPutOnAction(copyVehicle)
        if result:
            self._sendLastSlotAction(TankSetupConstants.CONSUMABLES,
                                     BaseSetupModel.REVERT_SLOT_ACTION,
                                     {'slotID': self._installedSlotId})
        return
示例#32
0
class _SquadInvitationsRecorder(_SquadInvitationsHandler):
    """ This class wraps _SquadInvitationsHandler in order to record player's
    actions with dyn squads during replay recording."""

    __slots__ = ("__idGen",)

    def __init__(self, setup):
        super(_SquadInvitationsRecorder, self).__init__(setup)
        self.__idGen = SequenceIDGenerator()

    def send(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(_SEND_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).send(playerID)

    def accept(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(_ACCEPT_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).accept(playerID)

    def reject(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(_REJECT_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).reject(playerID)
示例#33
0
文件: invites.py 项目: webiumsk/WoT
 def __init__(self, loader):
     self.__loader = loader
     self._IDGen = SequenceIDGenerator()
     self._IDMap = {'inviteIDs': {},
      'prbIDs': {}}
     self.__receivedInvites = {}
     self.__unreadInvitesCount = 0
     self.__eventManager = Event.EventManager()
     self.__acceptChain = None
     self.onReceivedInviteListInited = Event.Event(self.__eventManager)
     self.onReceivedInviteListModified = Event.Event(self.__eventManager)
     return
示例#34
0
class _SquadInvitationsRecorder(_SquadInvitationsHandler):
    __slots__ = ('__idGen', )

    def __init__(self, setup):
        super(_SquadInvitationsRecorder, self).__init__(setup)
        self.__idGen = SequenceIDGenerator()

    def send(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(
            _SEND_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).send(playerID)

    def accept(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(
            _ACCEPT_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).accept(playerID)

    def reject(self, playerID):
        BattleReplay.g_replayCtrl.serializeCallbackData(
            _REJECT_ACTION_NAME, (self.__idGen.next(), playerID))
        super(_SquadInvitationsRecorder, self).reject(playerID)
示例#35
0
class VehiclesRentedCMHandler(ContextMenu):
    __sqGen = SequenceIDGenerator()
    __itemsCache = dependency.descriptor(IItemsCache)
    __comparisonBasket = dependency.descriptor(IVehicleComparisonBasket)
    __epicController = dependency.descriptor(IEpicBattleMetaGameController)

    @option(__sqGen.next(), CMLabel.INFORMATION)
    def showInfo(self):
        shared_events.showVehicleInfo(self._id)

    @option(__sqGen.next(), CMLabel.STATS)
    def goToStats(self):
        shared_events.showVehicleStats(self._id)

    @option(__sqGen.next(), CMLabel.BUY)
    def buy(self):
        ItemsActionsFactory.doAction(ItemsActionsFactory.BUY_VEHICLE, self._id)

    @option(__sqGen.next(), CMLabel.REMOVE)
    def remove(self):
        shared_events.showVehicleSellDialog(
            self.__itemsCache.items.getItemByCD(self._id).invID)

    @option(__sqGen.next(), CMLabel.ADD_TO_COMPARE)
    def addToCompare(self):
        self.__comparisonBasket.addVehicle(self._id)

    @option(__sqGen.next(), CMLabel.SHOW_IN_HANGAR)
    def showInHangar(self):
        shared_events.selectVehicleInHangar(self._id)

    def _getOptionCustomData(self, label):
        optionData = super(VehiclesRentedCMHandler,
                           self)._getOptionCustomData(label)
        if label == CMLabel.STATS:
            optionData.enabled = _canGoToStats(self._id)
        elif label == CMLabel.ADD_TO_COMPARE:
            optionData.enabled = _canAddToComparisonBasket(self._id)
        elif label == CMLabel.BUY:
            optionData.enabled = self.__canBuy()
        elif label == CMLabel.REMOVE:
            optionData.enabled = self.__canRemove()
        return optionData

    def __canBuy(self):
        items = self.__itemsCache.items
        vehicle = self.__itemsCache.items.getItemByCD(self._id)
        return vehicle is not None and vehicle.mayObtainWithMoneyExchange(
            items.stats.money, items.shop.exchangeRate)

    def __canRemove(self):
        vehicle = self.__itemsCache.items.getItemByCD(self._id)
        return vehicle is not None and vehicle.canSell and vehicle.rentalIsOver and not vehicle.isTelecomRent
示例#36
0
class _IMarkerComponentBase(object):
    _idGen = SequenceIDGenerator()

    def __init__(self, data):
        super(_IMarkerComponentBase, self).__init__()
        self._componentID = self._idGen.next()
        self._initData = data
        self._matrixProduct = data.get('matrixProduct')
        self._isVisible = data.get('visible', True)

    @property
    def isVisible(self):
        return self._isVisible

    @property
    def componentID(self):
        return self._componentID

    @property
    def position(self):
        return Math.Matrix(self._matrixProduct.a).translation

    @property
    def positionWithOffset(self):
        return Math.Matrix(self._matrixProduct).translation

    @property
    def maskType(self):
        raise NotImplementedError

    def update(self, *args, **kwargs):
        pass

    def clear(self):
        pass

    def setVisible(self, isVisible):
        pass

    def attachGUI(self, guiProvider):
        pass

    def detachGUI(self):
        pass

    def setMarkerMatrix(self, matrix):
        self._matrixProduct.a = matrix

    def setMarkerPosition(self, position):
        matrix = Math.Matrix()
        matrix.setTranslate(position)
        self.setMarkerMatrix(matrix)
示例#37
0
 def __init__(self):
     from gui.prb_control.formatters.invites import PrbInviteLinkFormatter
     self.__linkFormatter = PrbInviteLinkFormatter()
     self._IDGen = SequenceIDGenerator()
     self._IDMap = {'inviteIDs': {},
      'prbIDs': {}}
     self.__receivedInvites = {}
     self.__unreadInvitesCount = 0
     self.__eventManager = Event.EventManager()
     self.__acceptChain = None
     self.onReceivedInviteListInited = Event.Event(self.__eventManager)
     self.onReceivedInviteListModified = Event.Event(self.__eventManager)
     return
示例#38
0
 def __init__(self, loader):
     super(InvitesManager, self).__init__()
     self.__loader = loader
     self._IDGen = SequenceIDGenerator()
     self._IDMap = {}
     self.__invites = {}
     self.__unreadInvitesCount = 0
     self.__eventManager = Event.EventManager()
     self.__acceptChain = None
     self.onInvitesListInited = Event.Event(self.__eventManager)
     self.onReceivedInviteListModified = Event.Event(self.__eventManager)
     self.onSentInviteListModified = Event.Event(self.__eventManager)
     self.__isInBattle = False
 def __init__(self, proxy):
     super(BrowserController, self).__init__(proxy)
     self.__browsers = {}
     self.__browsersCallbacks = {}
     self.__browserIDGenerator = SequenceIDGenerator()
     self.__eventMgr = Event.EventManager()
     self.onBrowserAdded = Event.Event(self.__eventMgr)
     self.onBrowserDeleted = Event.Event(self.__eventMgr)
     self.__urlMacros = URLMarcos()
     self.__pendingBrowsers = {}
     self.__creatingBrowserID = None
     self.__filters = _getGlobalFilters()
     return
示例#40
0
 def __init__(self):
     self.__client = BigWorld.XmppClient()
     self.__currentUser = None
     self.__xmppRoster = None
     self.__usersMgr = None
     self.__model = None
     self.__bwRoster = None
     self.__reconnectCount = 0
     self.__reconnectCallbackId = None
     self.__bwConnected = False
     self.__isEnabled = False
     self.__idsGen = SequenceIDGenerator()
     self.__pendingBattleMode = None
     self.__databaseID = None
     return
示例#41
0
class _InvitesIDsManager(object):

    def __init__(self):
        self.clear()

    def clear(self):
        self.__idsGen = SequenceIDGenerator()
        self.__idsMap = {'inviteIDs': {},
         'clubIDs': {}}

    def getInviteID(self, clubDbID, userDbID, creationTime):
        uniqueID = (clubDbID, userDbID, creationTime)
        if uniqueID not in self.__idsMap['clubIDs']:
            inviteID, showAt = self.__idsGen.next(), None
            self.__idsMap['inviteIDs'][inviteID] = uniqueID
            self.__idsMap['clubIDs'][uniqueID] = (inviteID, showAt)
        else:
            inviteID, showAt = self.__idsMap['clubIDs'][uniqueID]
        return (inviteID, showAt)

    def setShowTime(self, inviteID, showAt):
        uniqueID = self.__idsMap['inviteIDs'][inviteID]
        self.__idsMap['clubIDs'][uniqueID] = (inviteID, showAt)
示例#42
0
class XmppPlugin(IProtoPlugin):
    DISCONNECT_BY_REQUEST = 0
    DISCONNECT_AUTHENTICATION = 1
    DISCONNECT_OTHER_ERROR = 2
    USE_MOCK_DATA = False
    ALLOWED_ROSTER_ACTIONS = [USER_ROSTER_ACTION.AddToFriend,
     USER_ROSTER_ACTION.AddToIgnored,
     USER_ROSTER_ACTION.RemoveFromFriend,
     USER_ROSTER_ACTION.RemoveFromIgnored]

    def __init__(self):
        self.__client = BigWorld.XmppClient()
        self.__currentUser = None
        self.__xmppRoster = None
        self.__usersMgr = None
        self.__model = None
        self.__bwRoster = None
        self.__reconnectCount = 0
        self.__reconnectCallbackId = None
        self.__bwConnected = False
        self.__isEnabled = False
        self.__idsGen = SequenceIDGenerator()
        self.__pendingBattleMode = None
        self.__databaseID = None
        return

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

    @property
    def isConnected(self):
        """
        Shows current XMPP connection status
        """
        if self.__model is not None:
            return self.__model.isConnected
        else:
            return False

    def logState(self):
        if self.__isEnabled:
            if self.__model is not None and self.__model.isConnected:
                itemsStrList = ['XMPP:logState - XMPP is connected. Logging XMPP roster state:']
                itemsStrList.append('Curr.User JID: {0}  Status: {1}'.format(self.__currentUser.bareJid, self.__currentUser.getPresenceStr(self.__currentUser.presence)))
                for jid, item in self.__xmppRoster.items():
                    itemsStrList.append('Contact   JID: {0}  Status: {1}  Name: {2}'.format(jid, item.getPresenceStr(item.presence), item.name))

                LOG_DEBUG('\n'.join(itemsStrList))
            else:
                LOG_DEBUG('XMPP:logState - XMPP is not connected yet. Try to run your command later')
        else:
            LOG_DEBUG('XMPP:logState - You are not logged in or connection to XMPP is disabled by server settings')
        return

    def connect(self, scope = None):
        if scope is not None:
            self._setBattleMode(scope)
        if self.isConnected:
            return
        else:
            self.__bwConnected = True
            self._cancelReconnectCallback()
            settings = self._getServerSettings()
            if settings.get('xmpp_enabled', False):
                self.__isEnabled = bool(int(settings.get('jdCutouts', 0)))
                if self.__isEnabled:
                    self._subscribeToActions()
                    LOG_DEBUG('XMPP:connect - XMPP functionality is enabled. Starting connection to XMPP server')
                    if self.__model is None:
                        self.__model = XmppConnectionModel(settings.get('xmpp_host'), settings.get('xmpp_port'), settings.get('xmpp_resource'), False, None, None)
                    else:
                        self.__model = self.__model._replace(host=settings.get('xmpp_host'), port=settings.get('xmpp_port'), resourceName=settings.get('xmpp_resource'), isConnected=False)
                    if self.__model.host is None:
                        raise Exception, 'XMPP: server xmpp_host is not defined'
                    connections = settings.get('xmpp_connections', [])
                    if len(connections) > 0:
                        self.__model = self.__model._replace(connection=random.choice(connections))
                    else:
                        self.__model = self.__model._replace(connection=(self.__model.host, self.__model.port))
                        LOG_DEBUG('XMPP:connect - no xmpp_connections are passed - using default connection', self.__model.connection)
                    if self.__model.token is None:
                        Account.g_accountRepository and Account.g_accountRepository.onChatTokenReceived.clear()
                        Account.g_accountRepository.onChatTokenReceived += self._tokenCallback
                    BigWorld.player().requestChatToken(self.__idsGen.next())
                else:
                    LOG_DEBUG('XMPP:connect - trying to connect using token from previous connection')
                    self._tokenCallback(cPickle.dumps({'error': None,
                     'token': self.__model.token,
                     'databaseID': self.__databaseID}))
            else:
                LOG_DEBUG('XMPP:connect - XMPP functionality is disabled. Stopping execution')
            return

    def disconnect(self):
        LOG_DEBUG('XMPP:disconnect')
        if Account.g_accountRepository:
            Account.g_accountRepository.onChatTokenReceived.clear()
        self.__bwConnected = False
        if self.isConnected:
            self.__client.disconnect()
        else:
            self.onDisconnect()

    def onConnect(self):
        """
        Called after successful connection, encryption setup and authentication.
        """
        LOG_DEBUG('XMPP:onConnect - Successfully connected to XMPP server')
        self.__model = self.__model._replace(isConnected=True)
        self.__reconnectCount = 0
        self._cancelReconnectCallback()
        if self.__pendingBattleMode is not None:
            self._setBattleMode(self.__pendingBattleMode)
            self.__pendingBattleMode = None
        self._doRostersSync()
        return

    def onDisconnect(self, reason = DISCONNECT_BY_REQUEST, description = None):
        """
        Called if connection is closed or attempt to connect is failed.
        Additional description (in English) of disconnect reason is given. If
        description is empty then disconnect was caused by user (i.e. by
        explicit call to disconnect()).
        """
        LOG_DEBUG('XMPP:onDisconnect - Disconnected', reason, description)
        self._cancelReconnectCallback()
        if reason == self.DISCONNECT_AUTHENTICATION:
            self.__model = self.__model._replace(token=None)
        if self.__bwConnected:
            if self.__isEnabled:
                self.__model = self.__model._replace(isConnected=False)
                self.__reconnectCount += 1
                reconnectDelay = max(random.random() * 2 * self.__reconnectCount, 10.0)
                self.__reconnectCallbackId = BigWorld.callback(reconnectDelay, self.connect)
                LOG_DEBUG('XMPP:onDisconnect - will try to reconnect in {0} seconds'.format(reconnectDelay), description)
        else:
            if self.__isEnabled:
                self._unsubscribeFromActions()
            self.__client.xmppHandler = None
            self.__currentUser = None
            self.__xmppRoster = None
            self.__model = None
            self.__bwRoster = None
            self.__isEnabled = False
            self.__reconnectCount = 0
            self.__databaseID = None
        return

    def onNewRosterItem(self, bareJid, name, groups, subscriptionTo, subscriptionFrom):
        """
        Called when the contact is changed or added.
        
        name is custom contact name chosen by user.
        
        groups is set of groups (strings) which contact belongs to.
        
        subscriptionTo is presence subscription state in direction "contact
        TO user". One of SUBSCRIPTION_* values is used.
        
        subscriptionFrom is presence subscription state in direction "FROM user
        to contact". Either SUBSCRIPTION_ON or SUBSCRIPTION_OFF is used here.
        See onSubscribe() event.
        """
        LOG_DEBUG('XMPP:onNewRosterItem - New roster item added', bareJid, name)
        if self.__bwRoster is not None:
            if bareJid in self.__bwRoster:
                self._addToLocalXmppRoster(bareJid, name, groups, subscriptionTo, subscriptionFrom)
            else:
                self._doXmppRosterAction(bareJid, USER_ROSTER_ACTION.RemoveFromFriend)
        else:
            self._addToLocalXmppRoster(bareJid, name, groups, subscriptionTo, subscriptionFrom)
        return

    def onRosterItemRemove(self, bareJid):
        """
        Called when the contact is removed.
        """
        LOG_DEBUG('XMPP:onRosterItemRemove', bareJid)
        if self.__bwRoster is not None:
            if bareJid not in self.__bwRoster:
                self._removeFromLocalXmppRoster(bareJid)
            else:
                user = self.__bwRoster[bareJid]
                action = USER_ROSTER_ACTION.AddToFriend if bool(user.getRoster() & USERS_ROSTER_FRIEND) else USER_ROSTER_ACTION.AddToIgnored
                self._doXmppRosterAction(bareJid, action, user.getName())
        else:
            self._removeFromLocalXmppRoster(bareJid)
        return

    def onNewRosterResource(self, fullJid, priority, status, presence):
        """
        Called when contact resource is changed or added.
        
        You should compare fullJid of resource with fullJid of client to
        determine if this resource change is actually server acknowledgement of
        user presence change.
        
        Exact value of resource priority depends solely on contact's choice.
        
        status can be arbitrary text.
        
        Resource presence is defined by one of PRESENCE_* values.
        """
        LOG_DEBUG('XMPP:onNewRosterResource', fullJid, priority, status, presence)
        fullJidParts = fullJid.split('/')
        if len(fullJidParts) > 1:
            bareJid, resourceName = fullJidParts[0:2]
            if fullJid != self.__client.fullJid:
                if bareJid not in self.__xmppRoster:
                    self.__xmppRoster[bareJid] = RosterItem(bareJid)
                self.__xmppRoster[bareJid].updateResource(resourceName, priority, status, presence)
            else:
                self.__currentUser.updateResource(resourceName, priority, status, presence)

    def onRosterResourceRemove(self, fullJid):
        """
        Called when contact resource is removed (i.e. contact has gone offline).
        """
        LOG_DEBUG('XMPP:onRosterResourceRemove', fullJid)
        fullJidParts = fullJid.split('/')
        if len(fullJidParts) > 1:
            bareJid, resourceName = fullJidParts[0:2]
            if fullJid != self.__client.fullJid:
                if bareJid in self.__xmppRoster:
                    self.__xmppRoster[bareJid].removeResource(resourceName)
            else:
                self.__currentUser.removeResource(resourceName)

    def onSubscribe(self, bareJid, message):
        """
        Called when a contact (from roster or not) asks for subscription to user
        status updates.
        
        Call setSubscribed() or setUnsubscribed() to allow or disallow sending
        user status updates to the contact. Roster item will be automatically
        updated by onNewRosterItem event.
        
        Note that transition of subscriptionFrom attribute of contact from
        SUBSCRIPTION_OFF state to SUBSCRIPTION_PENDING state should be done
        manually in this callback. This event shouldn't be generated if
        subscriptionFrom is SUBSCRIPTION_ON, however it's possible.
        """
        LOG_DEBUG('XMPP:onSubscribe - Subscription request received', bareJid, message)
        self.__client.setSubscribed(bareJid)

    def _subscribeToActions(self):
        """
        Adds subscription to UsersManager events
        """
        self.__usersMgr = g_messengerEvents.users
        self.__usersMgr.onUsersRosterReceived += self._onRosterReceived
        self.__usersMgr.onUserRosterChanged += self._onRosterUpdate

    def _unsubscribeFromActions(self):
        """
        Removes subscription to UsersManager events
        """
        if self.__usersMgr:
            self.__usersMgr.onUsersRosterReceived -= self._onRosterReceived
            self.__usersMgr.onUserRosterChanged -= self._onRosterUpdate
            self.__usersMgr = None
        return

    def _getServerSettings(self):
        """
        Gets server setting values
        """
        if self.USE_MOCK_DATA:
            settings = {'jdCutouts': 1,
             'xmpp_connections': [('jbr-wowpkis11.pershastudia.org', 5222)],
             'xmpp_host': 'jbr-wowpkis11.pershastudia.org',
             'xmpp_port': 5222,
             'xmpp_resource': 'wot',
             'xmpp_enabled': True}
        elif Account.g_accountRepository:
            settings = Account.g_accountRepository.serverSettings
        else:
            settings = {'jdCutouts': 0,
             'xmpp_enabled': False}
        return settings

    def _tokenCallback(self, data):
        """
        Callback for PlayerAccount.requestChatToken method call
        """
        data = cPickle.loads(data)
        errorStr = data.get('error', None)
        if errorStr is None:
            self.__model = self.__model._replace(token=data.get('token'))
            self.__databaseID = data.get('databaseID')
            host, port = self.__model.connection
            if self.USE_MOCK_DATA:
                bareJid = '{0}@{1}'.format('admin1', self.__model.host)
            else:
                bareJid = '{0}@{1}'.format(self.__databaseID, self.__model.host)
            fullJid = '{0}/{1}'.format(bareJid, self.__model.resourceName)
            self.__currentUser = RosterItem(bareJid, 'Self', [], BigWorld.XmppClient.SUBSCRIPTION_OFF, BigWorld.XmppClient.SUBSCRIPTION_OFF)
            LOG_DEBUG('XMPP:_tokenCallback - Token received - connecting to XMPP server', fullJid, self.__model.token, host, port)
            self.__client.xmppHandler = self
            self.__xmppRoster = {}
            self.__client.connect(fullJid, str(self.__model.token), host, port)
        else:
            self.onDisconnect()
            LOG_ERROR('XMPP:_tokenCallback - Error while getting XMPP connection token', errorStr)
        return

    def _cancelReconnectCallback(self):
        try:
            if self.__reconnectCallbackId is not None:
                BigWorld.cancelCallback(self.__reconnectCallbackId)
        except:
            pass
        finally:
            self.__reconnectCallbackId = None

        return

    def _onRosterReceived(self):
        """
        Listener for UsersManager.onUsersRosterReceived
        """
        self.__bwRoster = {}
        contacts = self.usersStorage.all()
        LOG_DEBUG('XMPP:_onRosterReceived - BW rooster received', contacts)
        for user in contacts:
            if user.isCurrentPlayer():
                continue
            if self.USE_MOCK_DATA:
                bareJid = '{0}@{1}'.format('admin2', self.__model.host)
            else:
                bareJid = '{0}@{1}'.format(user.getID(), self.__model.host)
            self.__bwRoster[bareJid] = user

        self._doRostersSync()

    def _onRosterUpdate(self, action, user):
        """
        Listener for UsersManager.onUsersRosterUpdate
        """
        if action in self.ALLOWED_ROSTER_ACTIONS:
            if self.USE_MOCK_DATA:
                bareJid = '{0}@{1}'.format('admin2', self.__model.host)
            else:
                bareJid = '{0}@{1}'.format(user.getID(), self.__model.host)
            LOG_DEBUG('XMPP:_onRosterUpdate - BW rooster update', action, user)
            if action in [USER_ROSTER_ACTION.AddToFriend, USER_ROSTER_ACTION.AddToIgnored]:
                self.__bwRoster[bareJid] = user
            elif bareJid in self.__bwRoster:
                del self.__bwRoster[bareJid]
            self._doXmppRosterAction(bareJid, action, user.getName())

    def _doRostersSync(self):
        """
        Performs XMPP roster synchronization with BW roster (BW is Master)
        """
        if self.__bwRoster is not None and self.isConnected:
            bwJidSet = set(self.__bwRoster)
            xmppJidSet = set(self.__xmppRoster)
            LOG_DEBUG('XMPP:_doRostersSync - Syncing BW and XMPP rosters')
            LOG_DEBUG('XMPP:_doRostersSync - BW roster', bwJidSet)
            LOG_DEBUG('XMPP:_doRostersSync - XMPP roster', xmppJidSet)
            toRemove = xmppJidSet - bwJidSet
            toAdd = bwJidSet - xmppJidSet
            for jid in toRemove:
                self._doXmppRosterAction(jid, USER_ROSTER_ACTION.RemoveFromFriend)

            for jid in toAdd:
                user = self.__bwRoster[jid]
                action = USER_ROSTER_ACTION.AddToFriend if bool(user.getRoster() & USERS_ROSTER_FRIEND) else USER_ROSTER_ACTION.AddToIgnored
                self._doXmppRosterAction(jid, action, user.getName())

        return

    def _addToLocalXmppRoster(self, bareJid, name, groups, subscriptionTo, subscriptionFrom):
        """
        Adds new item to local XMPP roster
        """
        if bareJid in self.__xmppRoster:
            LOG_DEBUG('XMPP:_addToLocalXmppRoster - Updating item in local XMPP roster', bareJid, name, groups, subscriptionTo, subscriptionFrom)
            item = self.__xmppRoster.get(bareJid)
            item.name, item.groups, item.subscriptionTo, item.subscriptionFrom = (name,
             groups,
             subscriptionTo,
             subscriptionFrom)
        else:
            LOG_DEBUG('XMPP:_addToLocalXmppRoster - Adding item to local XMPP roster', bareJid, name, groups, subscriptionTo, subscriptionFrom)
            self.__xmppRoster[bareJid] = RosterItem(bareJid, name, groups, subscriptionTo, subscriptionFrom)

    def _removeFromLocalXmppRoster(self, bareJid):
        """
        Removes item from local XMPP roster
        """
        LOG_DEBUG('XMPP:_removeFromLocalXmppRoster - Roster item is removed from local XMPP roster', bareJid, self.__xmppRoster[bareJid].name)
        del self.__xmppRoster[bareJid]

    def _doXmppRosterAction(self, bareJid, action, userName = '******'):
        """
        Triggers needed roster action with XMPP chat server basing on passes action type
        """
        if action in [USER_ROSTER_ACTION.AddToFriend, USER_ROSTER_ACTION.AddToIgnored]:
            LOG_DEBUG('XMPP:_doXmppRosterAction - adding user from BW rooster to XMPP roster', bareJid, userName)
            self.__client.add(bareJid, userName)
            self.__client.subscribe(bareJid)
        elif action in [USER_ROSTER_ACTION.RemoveFromFriend, USER_ROSTER_ACTION.RemoveFromIgnored]:
            LOG_DEBUG('XMPP:_doXmppRosterAction - user is removed from BW rooster. Removing from XMPP roster', bareJid)
            self.__client.remove(bareJid)
            self.__client.unsubscribe(bareJid)

    def _setBattleMode(self, mode):
        LOG_DEBUG('XMPP:_setBattleMode', mode)
        if self.isConnected:
            self.__client.presence = BigWorld.XmppClient.PRESENCE_DND if mode == MESSENGER_SCOPE.BATTLE else BigWorld.XmppClient.PRESENCE_AVAILABLE
        else:
            self.__pendingBattleMode = mode
            self.onDisconnect()
示例#43
0
class InvitesManager(UsersInfoHelper):
    __clanInfo = None

    def __init__(self, loader):
        super(InvitesManager, self).__init__()
        self.__loader = loader
        self._IDGen = SequenceIDGenerator()
        self._IDMap = {}
        self.__invites = {}
        self.__unreadInvitesCount = 0
        self.__eventManager = Event.EventManager()
        self.__acceptChain = None
        self.onInvitesListInited = Event.Event(self.__eventManager)
        self.onReceivedInviteListModified = Event.Event(self.__eventManager)
        self.onSentInviteListModified = Event.Event(self.__eventManager)
        self.__isInBattle = False
        return

    def __del__(self):
        LOG_DEBUG('InvitesManager deleted')
        super(InvitesManager, self).__del__()

    def init(self):
        self.__inited = PRB_INVITES_INIT_STEP.UNDEFINED
        g_messengerEvents.users.onUsersListReceived += self.__me_onUsersListReceived
        g_playerEvents.onPrebattleInvitesChanged += self.__pe_onPrebattleInvitesChanged
        g_playerEvents.onPrebattleInvitationsChanged += self.__pe_onPrebattleInvitationsChanged
        g_playerEvents.onPrebattleInvitesStatus += self.__pe_onPrebattleInvitesStatus

    def fini(self):
        self.__clearAcceptChain()
        self.__inited = PRB_INVITES_INIT_STEP.UNDEFINED
        self.__loader = None
        g_messengerEvents.users.onUsersListReceived -= self.__me_onUsersListReceived
        g_playerEvents.onPrebattleInvitationsChanged -= self.__pe_onPrebattleInvitationsChanged
        g_playerEvents.onPrebattleInvitesChanged -= self.__pe_onPrebattleInvitesChanged
        g_playerEvents.onPrebattleInvitesStatus -= self.__pe_onPrebattleInvitesStatus
        self.clear()
        return

    def start(self):
        self.__isInBattle = False
        if self.__inited & PRB_INVITES_INIT_STEP.STARTED == 0:
            self.__inited |= PRB_INVITES_INIT_STEP.STARTED
            if self.__inited == PRB_INVITES_INIT_STEP.INITED:
                self.onInvitesListInited()

    def clear(self):
        self.__inited = PRB_INVITES_INIT_STEP.UNDEFINED
        self.__clearInvites()
        self._IDMap = {}
        self.__eventManager.clear()

    def onAvatarBecomePlayer(self):
        self.__isInBattle = True
        self.__clearAcceptChain()

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

    def isInited(self):
        return self.__inited == PRB_INVITES_INIT_STEP.INITED

    def acceptInvite(self, inviteID, postActions = None):
        try:
            invite = self.__invites[inviteID]
        except KeyError:
            LOG_ERROR('Invite ID is invalid', inviteID, self._IDMap)
            return

        self.__clearAcceptChain()
        if not postActions:
            invite.accept()
        else:
            self.__acceptChain = _AcceptInvitesPostActions(invite, postActions)
            self.__acceptChain.onStopped += self.__accept_onPostActionsStopped
            self.__acceptChain.start()
        if self.__unreadInvitesCount > 0:
            self.__unreadInvitesCount -= 1

    def declineInvite(self, inviteID):
        try:
            invite = self.__invites[inviteID]
        except KeyError:
            LOG_ERROR('Invite ID is invalid', inviteID, self._IDMap)
            return

        invite.decline()
        if self.__unreadInvitesCount > 0:
            self.__unreadInvitesCount -= 1

    def revokeInvite(self, inviteID):
        try:
            invite = self.__invites[inviteID]
        except KeyError:
            LOG_ERROR('Invite ID is invalid', inviteID, self._IDMap)
            return

        invite.revoke()
        if self.__unreadInvitesCount > 0:
            self.__unreadInvitesCount -= 1

    def canAcceptInvite(self, invite):
        result = False
        if invite.alwaysAvailable is True:
            result = True
        elif invite.clientID in self.__invites:
            dispatcher = self.__loader.getDispatcher()
            if dispatcher:
                prbFunctional = dispatcher.getPrbFunctional()
                unitFunctional = dispatcher.getUnitFunctional()
                preQueueFunctional = dispatcher.getPreQueueFunctional()
                if invite.alreadyJoined:
                    return False
                if prbFunctional and prbFunctional.hasLockedState() or unitFunctional and unitFunctional.hasLockedState() or preQueueFunctional and preQueueFunctional.hasLockedState():
                    return False
            another = invite.anotherPeriphery
            if another:
                if g_preDefinedHosts.periphery(invite.peripheryID) is None:
                    LOG_ERROR('Periphery not found')
                    result = False
                elif g_lobbyContext.getCredentials() is None:
                    LOG_ERROR('Login info not found')
                    result = False
                elif g_preDefinedHosts.isRoamingPeriphery(invite.peripheryID) and not isRoamingEnabled(g_itemsCache.items.stats.attributes):
                    LOG_ERROR('Roaming is not supported')
                    result = False
                else:
                    result = invite.clientID > 0 and invite.isActive()
            else:
                result = invite.clientID > 0 and invite.isActive()
        return result

    def canDeclineInvite(self, invite):
        result = False
        if invite.clientID in self.__invites:
            result = invite.clientID > 0 and invite.isActive()
        return result

    def canRevokeInvite(self, invite):
        result = False
        if invite.clientID in self.__invites:
            result = invite.clientID > 0 and invite.isActive() and invite.creatorDBID == getPlayerDatabaseID()
        return result

    def getInvites(self, incoming = None, version = None, onlyActive = None):
        result = self.__invites.values()
        if incoming is not None:
            result = filter(lambda item: item.isIncoming() is incoming, result)
        if version is not None:
            result = filter(lambda item: item.getVersion() == version, result)
        if onlyActive is not None:
            result = filter(lambda item: item.isActive() is onlyActive, result)
        return result

    def getInvite(self, inviteID):
        invite = None
        if inviteID in self.__invites:
            invite = self.__invites[inviteID]
        return invite

    def getReceivedInviteCount(self):
        return len(self.getReceivedInvites())

    def getReceivedInvites(self, IDs = None):
        result = self.getInvites(incoming=True)
        if IDs is not None:
            result = filter(lambda item: item.clientID in IDs, result)
        return result

    def getSentInvites(self, IDs = None):
        result = self.getInvites(incoming=False)
        if IDs is not None:
            result = filter(lambda item: item.clientID in IDs, result)
        return result

    def getSentInviteCount(self):
        return len(self.getSentInvites())

    def getUnreadCount(self):
        return self.__unreadInvitesCount

    def resetUnreadCount(self):
        self.__unreadInvitesCount = 0

    def onUserNamesReceived(self, names):
        updated = defaultdict(list)
        rosterGetter = self.users.getUser
        inviteMaker = self._getNewInviteMaker(rosterGetter)
        prebattleInvitations = _getNewInvites()
        for invite in self.getInvites(version=_INVITE_VERSION.NEW):
            if invite.creatorDBID in names or invite.receiverDBID in names:
                inviteData = prebattleInvitations.get(invite.id)
                inviteID, invite = inviteMaker(inviteData)
                if inviteData and self._updateInvite(invite, rosterGetter):
                    updated[invite.isIncoming()].append(inviteID)

        for isIncoming, event in ((True, self.onReceivedInviteListModified), (False, self.onSentInviteListModified)):
            if updated[isIncoming]:
                event([], updated[isIncoming], [])

    def _makeInviteID(self, prebattleID, peripheryID, senderDBID, receiverDBID):
        inviteKey = (prebattleID,
         peripheryID,
         senderDBID,
         receiverDBID)
        inviteID = self._IDMap.get(inviteKey)
        if inviteID is None:
            inviteID = self._IDGen.next()
            self._IDMap[inviteKey] = inviteID
        return inviteID

    def _addInvite(self, invite, userGetter):
        if self.__isInviteSenderIgnoredInBattle(invite, userGetter):
            return False
        self.__invites[invite.clientID] = invite
        if invite.isActive():
            self.__unreadInvitesCount += 1
        return True

    def _updateInvite(self, other, userGetter):
        inviteID = other.clientID
        invite = self.__invites[inviteID]
        if invite == other or self.__isInviteSenderIgnoredInBattle(invite, userGetter):
            return False
        prevCount = invite.count
        invite = invite._merge(other)
        self.__invites[inviteID] = invite
        if invite.isActive() and prevCount < invite.count:
            self.__unreadInvitesCount += 1
        return True

    def _delInvite(self, inviteID):
        result = inviteID in self.__invites
        if result:
            self.__invites.pop(inviteID)
        return result

    def _buildReceivedInvitesList(self, invitesLists):
        if self.__inited & PRB_INVITES_INIT_STEP.DATA_BUILD == 0:
            self.__inited |= PRB_INVITES_INIT_STEP.DATA_BUILD
        self.__clearInvites()
        userGetter = self.users.getUser
        for invitesData, maker in invitesLists:
            for item in invitesData:
                _, invite = maker(item)
                if invite:
                    self._addInvite(invite, userGetter)

        if g_appLoader.getSpaceID() != GUI_GLOBAL_SPACE_ID.BATTLE:
            self.syncUsersInfo()

    def _rebuildInvitesLists(self):
        rosterGetter = self.users.getUser
        self._buildReceivedInvitesList([(_getOldInvites().items(), self._getOldInviteMaker(rosterGetter)), (_getNewInvites().values(), self._getNewInviteMaker(rosterGetter))])

    def _getOldInviteMaker(self, rosterGetter):
        receiver = getPlayerName()
        receiverDBID = getPlayerDatabaseID()
        receiverClanAbbrev = g_lobbyContext.getClanAbbrev(self.__clanInfo)

        def _inviteMaker(item):
            (prebattleID, peripheryID), data = item
            inviteID = self._makeInviteID(prebattleID, peripheryID, data['creatorDBID'], receiverDBID)
            if data is not None:
                invite = PrbInviteWrapper(clientID=inviteID, receiver=receiver, receiverDBID=receiverDBID, receiverClanAbbrev=receiverClanAbbrev, peripheryID=peripheryID, prebattleID=prebattleID, **data)
            else:
                invite = None
            return (inviteID, invite)

        return _inviteMaker

    def _getNewInviteMaker(self, rosterGetter):

        def _getUserName(userDBID):
            name, abbrev = ('', None)
            if userDBID:
                if g_appLoader.getSpaceID() == GUI_GLOBAL_SPACE_ID.BATTLE:
                    name, abbrev = g_battleCtrl.getCtx().getPlayerFullNameParts(accID=userDBID, showVehShortName=False)[1:3]
                if not name:
                    userName = self.getUserName(userDBID)
                    userClanAbbrev = self.getUserClanAbbrev(userDBID)
                    user = rosterGetter(userDBID)
                    if user and user.hasValidName():
                        name, abbrev = userName, userClanAbbrev
            return (name, abbrev)

        def _inviteMaker(item):
            peripheryID, prebattleID = PrbInvitationWrapper.getPrbInfo(item.get('info', {}))
            senderDBID = item.get('senderDBID', 0)
            receiverDBID = item.get('receiverDBID', 0)
            inviteID = self._makeInviteID(prebattleID, peripheryID, senderDBID, receiverDBID)
            senderName, senderClanAbbrev = _getUserName(senderDBID)
            receiverName, receiverClanAbbrev = _getUserName(receiverDBID)
            return (inviteID, PrbInvitationWrapper(inviteID, sender=senderName, senderClanAbbrev=senderClanAbbrev, receiver=receiverName, receiverClanAbbrev=receiverClanAbbrev, **item))

        return _inviteMaker

    def __initReceivedInvites(self):
        step = PRB_INVITES_INIT_STEP.CONTACTS_RECEIVED
        if self.__inited & step != step:
            return
        self._rebuildInvitesLists()
        if self.__inited == PRB_INVITES_INIT_STEP.INITED:
            self.onInvitesListInited()

    def __clearAcceptChain(self):
        if self.__acceptChain is not None:
            self.__acceptChain.onStopped -= self.__accept_onPostActionsStopped
            self.__acceptChain.stop()
            self.__acceptChain = None
        return

    def __me_onUsersListReceived(self, tags):
        doInit = False
        if USER_TAG.FRIEND in tags:
            doInit = True
            step = PRB_INVITES_INIT_STEP.FRIEND_RECEIVED
            if self.__inited & step == 0:
                self.__inited |= step
        if USER_TAG.IGNORED in tags:
            doInit = True
            step = PRB_INVITES_INIT_STEP.IGNORED_RECEIVED
            if self.__inited & step == 0:
                self.__inited |= step
        if doInit:
            self.__initReceivedInvites()

    def __pe_onPrebattleInvitesChanged(self, diff):
        step = PRB_INVITES_INIT_STEP.CONTACTS_RECEIVED
        if self.__inited & step != step:
            LOG_DEBUG('Received invites are ignored. Manager waits for client will receive contacts')
            return
        if ('prebattleInvites', '_r') in diff:
            self._rebuildInvitesLists()
        if 'prebattleInvites' in diff:
            self.__updateOldPrebattleInvites(_getOldInvites())

    def __pe_onPrebattleInvitationsChanged(self, invitations):
        step = PRB_INVITES_INIT_STEP.CONTACTS_RECEIVED
        if self.__inited & step != step:
            LOG_DEBUG('Received invites are ignored. Manager waits for client will receive contacts')
            return
        self.__updateNewPrebattleInvites(invitations)

    def __pe_onPrebattleInvitesStatus(self, dbID, name, status):
        if status != PREBATTLE_INVITE_STATUS.OK:
            statusName = PREBATTLE_INVITE_STATUS_NAMES[status]
            SystemMessages.pushI18nMessage('#system_messages:invite/status/%s' % statusName, name=name, type=SystemMessages.SM_TYPE.Warning)

    def __updateOldPrebattleInvites(self, prbInvites):
        added = []
        changed = []
        deleted = []
        modified = False
        rosterGetter = self.users.getUser
        inviteMaker = self._getOldInviteMaker(rosterGetter)
        for item in prbInvites.iteritems():
            inviteID, invite = inviteMaker(item)
            if invite is None:
                if self._delInvite(inviteID):
                    modified = True
                    deleted.append(inviteID)
                continue
            inList = inviteID in self.__invites
            if not inList:
                if self._addInvite(invite, rosterGetter):
                    modified = True
                    added.append(inviteID)
            elif self._updateInvite(invite, rosterGetter):
                modified = True
                changed.append(inviteID)

        if modified:
            self.onReceivedInviteListModified(added, changed, deleted)
        return

    def __updateNewPrebattleInvites(self, prbInvites):
        added = defaultdict(list)
        changed = defaultdict(list)
        deleted = defaultdict(list)
        modified = dict(((v, False) for v in (True, False)))
        rosterGetter = self.users.getUser
        inviteMaker = self._getNewInviteMaker(rosterGetter)
        newInvites = {}
        for data in prbInvites.itervalues():
            inviteID, invite = inviteMaker(data)
            if inviteID not in newInvites or invite.createTime > newInvites[inviteID].createTime:
                newInvites[inviteID] = invite

        for invite in self.getInvites(version=_INVITE_VERSION.NEW):
            inviteID = invite.clientID
            if inviteID not in newInvites and self._delInvite(inviteID):
                isIncoming = invite.isIncoming()
                modified[isIncoming] = True
                deleted[isIncoming].append(inviteID)
            else:
                continue

        for inviteID, invite in newInvites.iteritems():
            isIncoming = invite.isIncoming()
            if inviteID not in self.__invites:
                if self._addInvite(invite, rosterGetter):
                    modified[isIncoming] = True
                    added[isIncoming].append(inviteID)
            elif self._updateInvite(invite, rosterGetter):
                modified[isIncoming] = True
                changed[isIncoming].append(inviteID)

        for isIncoming, event in ((True, self.onReceivedInviteListModified), (False, self.onSentInviteListModified)):
            if modified[isIncoming]:
                event(added[isIncoming], changed[isIncoming], deleted[isIncoming])

        self.syncUsersInfo()

    def __accept_onPostActionsStopped(self, isCompleted):
        if not isCompleted:
            return
        invite = self.__acceptChain.invite
        invite.accept()
        if self.__unreadInvitesCount > 0:
            self.__unreadInvitesCount -= 1

    def __clearInvites(self):
        self.__invites.clear()
        self.__unreadInvitesCount = 0

    def __isInviteSenderIgnoredInBattle(self, invite, userGetter):
        return isInviteSenderIgnoredInBattle(userGetter(invite.creatorDBID), g_settings.userPrefs.invitesFromFriendsOnly, invite.isCreatedInBattle())
示例#44
0
 def __init__(self):
     raise isinstance(self, BigWorld.Entity) or AssertionError
     self.__chatActionCallbacks = {}
     self._idGen = SequenceIDGenerator()
示例#45
0
class ClientChat(object):
    __dataProcessors = ['_ClientChat__dataTimeProcessor', '_ClientChat__inviteDataTimeProcessor', '_ClientChat__systemMessageTimeProcessor']
    __actionHandlers = {CHAT_ACTIONS.receiveInvite.index(): '_ClientChat__onReceiveInvite'}

    def __init__(self):
        raise isinstance(self, BigWorld.Entity) or AssertionError
        self.__chatActionCallbacks = {}
        self._idGen = SequenceIDGenerator()

    def acquireRequestID(self):
        return self._idGen.next()

    def requestSystemChatChannels(self):
        self.__baseChatCommand(CHAT_COMMANDS.requestSystemChatChannels)

    def findChatChannels(self, sample, requestID = None):
        if requestID is None:
            requestID = self.acquireRequestID()
        try:
            self.__baseChatCommand(CHAT_COMMANDS.findChatChannels, stringArg1=sample, ignoreCooldown=False, requestID=requestID)
        except ChatError as ex:
            self._processChatError(CHAT_ACTIONS.requestChannels, 0, ex, requestID=requestID)

        return

    def getChannelInfoById(self, channelId):
        self.__baseChatCommand(CHAT_COMMANDS.getChannelInfoById, int64Arg=channelId)

    def requestChatChannelMembers(self, channelId):
        self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.requestChatChannelMembers)

    def requestChatChannelMembersCount(self, channelId):
        self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.getMembersCount)

    def createChatChannel(self, channelName, password = None):
        try:
            self.__baseChatCommand(CHAT_COMMANDS.createChatChannel, stringArg1=channelName, stringArg2=password if password is not None else '', ignoreCooldown=False)
        except ChatError as ex:
            self._processChatError(CHAT_COMMANDS.createChatChannel, 0, ex)

        return

    def destroyChatChannel(self, channelId):
        self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.destroyChatChannel)

    def enterChat(self, channelId, password = None):
        self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.enterChatChannel, stringArg1=password if password is not None else '')
        return

    def broadcast(self, channelId, message):
        if not len(message) or message.isspace():
            return
        message = message.rstrip()
        if not isCommandMessage(message):
            try:
                self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.broadcast, stringArg1=message, ignoreCooldown=False)
            except ChatError as ex:
                self._processChatError(CHAT_ACTIONS.broadcast, channelId, ex)

        else:
            try:
                command, int64Arg, int16Arg, stringArg1, stringArg2 = parseCommandMessage(message)
                self.__baseChannelChatCommand(channelId, command, int64Arg, int16Arg, stringArg1, stringArg2, ignoreCooldown=False)
            except ChatCommandError as ex:
                self._processChatError(CHAT_ACTIONS.userChatCommand, channelId, ex)

    def leaveChat(self, channelId):
        self.__baseChannelChatCommand(channelId, CHAT_COMMANDS.leaveChatChannel)

    def onChatActionFailure(self, actionData):
        MessengerEntry.g_instance.protos.BW.onChatActionFailure(actionData)

    def onChatAction(self, chatActionData):
        global g_replayCtrl
        if g_replayCtrl is None:
            import BattleReplay
            g_replayCtrl = BattleReplay.g_replayCtrl
        if g_replayCtrl.isRecording:
            g_replayCtrl.cancelSaveCurrMessage()
        elif g_replayCtrl.isPlaying:
            g_replayCtrl.onChatAction(chatActionData)
            return
        for processor in self.__dataProcessors:
            getattr(self, processor)(chatActionData)

        if CHAT_RESPONSES[chatActionData['actionResponse']] != CHAT_RESPONSES.success:
            self.onChatActionFailure(chatActionData)
        else:
            hanlerName = self.__actionHandlers.get(chatActionData['action'], None)
            if hanlerName:
                getattr(self, hanlerName)(chatActionData)
            chId = chatActionData['channel']
            commonCallbacks = self.__getChatActionCallbacks(CHAT_ACTIONS[chatActionData['action']], 0)
            commonCallbacks(chatActionData)
            if chId != 0:
                channelCallbacks = self.__getChatActionCallbacks(CHAT_ACTIONS[chatActionData['action']], chId)
                channelCallbacks(chatActionData)
        return

    def requestLastSysMessages(self):
        self.__baseChatCommand(CHAT_COMMANDS.requestLastSysMessages)

    def findUsers(self, userNamePattern, onlineMode = None, requestID = None):
        if onlineMode is None:
            searchMode = USER_SEARCH_MODE.ALL
        elif onlineMode:
            searchMode = USER_SEARCH_MODE.ONLINE
        else:
            searchMode = USER_SEARCH_MODE.OFFLINE
        self.__baseChatCommand(CHAT_COMMANDS.findUser, int16Arg=searchMode, stringArg1=userNamePattern, requestID=requestID)
        return

    def requestUsersRoster(self, flags = 0):
        self.__baseChatCommand(CHAT_COMMANDS.requestUsersRoster, int16Arg=flags)

    def logVivoxLogin(self):
        self.__baseChatCommand(CHAT_COMMANDS.logVivoxLogin)

    def requestFriendStatus(self, friendID = -1):
        self.__baseChatCommand(CHAT_COMMANDS.requestFriendStatus, int64Arg=friendID)

    def addFriend(self, friendID, friendName):
        self.__baseChatCommand(CHAT_COMMANDS.addFriend, int64Arg=friendID, stringArg1=friendName)

    def createPrivate(self, friendID, friendName):
        self.__baseChatCommand(CHAT_COMMANDS.createPrivate, int64Arg=friendID, stringArg1=friendName)

    def removeFriend(self, friendID):
        self.__baseChatCommand(CHAT_COMMANDS.removeFriend, int64Arg=friendID)

    def addIgnored(self, ignoredID, ignoredName):
        self.__baseChatCommand(CHAT_COMMANDS.addIgnored, int64Arg=ignoredID, stringArg1=ignoredName)

    def removeIgnored(self, ignoredID):
        self.__baseChatCommand(CHAT_COMMANDS.removeIgnored, int64Arg=ignoredID)

    def setMuted(self, mutedID, mutedName):
        self.__baseChatCommand(CHAT_COMMANDS.setMuted, int64Arg=mutedID, stringArg1=mutedName)

    def unsetMuted(self, mutedID):
        self.__baseChatCommand(CHAT_COMMANDS.unsetMuted, int64Arg=mutedID)

    def createPrebattleInvite(self, receiverName, auxText, prebattleID, prebattleType, requestID = None):
        self.__baseInviteCommand(CHAT_COMMANDS.createInvite, INVITE_TYPES.PREBATTLE, receiverName, prebattleID, prebattleType, stringArg2=auxText, requestID=requestID)

    def createBarterInvite(self, receiverName, auxText, itemID, requestID = None):
        self.__baseInviteCommand(CHAT_COMMANDS.createInvite, INVITE_TYPES.BARTER, receiverName, itemID, stringArg2=auxText, requestID=requestID)

    def acceptPrebattleInvite(self, inviteID, requestID = None):
        if requestID is None:
            requestID = self.acquireRequestID()
        self.base.ackCommand(requestID, CHAT_COMMANDS.acceptInvite.index(), time.time(), inviteID, -1)
        return

    def rejectInvite(self, inviteID, requestID = None):
        if requestID is None:
            requestID = self.acquireRequestID()
        self.base.ackCommand(requestID, CHAT_COMMANDS.rejectInvite.index(), time.time(), inviteID, -1)
        return

    def getActiveInvites(self):
        self.__baseInviteCommand(CHAT_COMMANDS.getActiveInvites)

    def getArchiveInvites(self):
        self.__baseInviteCommand(CHAT_COMMANDS.getArchiveInvites)

    def requestVOIPCredentials(self, changePwd = 0):
        self.__baseChatCommand(CHAT_COMMANDS.requestVOIPCredentials, int16Arg=changePwd)

    def subscribeChatAction(self, callback, action, channelId = None):
        cbs = self.__getChatActionCallbacks(action, channelId)
        cbs += callback

    def unsubscribeChatAction(self, callback, action, channelId = None):
        cbs = self.__getChatActionCallbacks(action, channelId)
        cbs -= callback

    def setChatActionsCallbacks(self, callbacks):
        self.__chatActionCallbacks = callbacks

    def sendChannelChatCommand(self, channelID, command, int64Arg = 0, int16Arg = 0, stringArg1 = '', stringArg2 = ''):
        self.__baseChannelChatCommand(channelID, command, int64Arg, int16Arg, stringArg1, stringArg2)

    def _processChatError(self, action, channelId, chatError, requestID = -1):
        if isinstance(chatError, ChatError):
            actionData = chatError.messageArgs if chatError.messageArgs is not None else chatError.message
        else:
            actionData = ''
        chatAction = buildChatActionData(action=action, requestID=requestID, channelId=channelId, originatorNickName=self.name, data=actionData, actionResponse=chatError.response if isinstance(chatError, ChatError) else CHAT_RESPONSES.internalError)
        self.onChatAction(chatAction)
        return

    def __getChatActionCallbacks(self, action, channelId):
        channelId = channelId if channelId is not None else 0
        key = (action, channelId)
        if key not in self.__chatActionCallbacks:
            handlers = self.__chatActionCallbacks[key] = Event.Event()
        else:
            handlers = self.__chatActionCallbacks[key]
        return handlers

    def __receiveStreamedData(self, streamID, data):
        failed = False
        try:
            data = zlib.decompress(data)
            chatMessages = cPickle.loads(data)
        except:
            LOG_CURRENT_EXCEPTION()
            failed = True

        if not failed:
            chIds = sorted(chatMessages.keys(), cmp=lambda x, y: cmp(abs(x), abs(y)))
            for chId in chIds:
                channelQueue = chatMessages.get(chId, deque())
                while True:
                    try:
                        actionData = channelQueue.popleft()
                        self.onChatAction(actionData)
                    except IndexError:
                        break

        self.__baseChatCommand(CHAT_COMMANDS.initAck, int64Arg=streamID, int16Arg=failed)

    def __baseChannelChatCommand(self, channelID, command, int64Arg = 0, int16Arg = 0, stringArg1 = '', stringArg2 = '', ignoreCooldown = True):
        if 0 == channelID:
            LOG_ERROR('Can`t execute chat channel command for channelId: %s' % (channelID,))
        else:
            if chat_shared.isOperationInCooldown(chat_shared.g_chatCooldownData, command):
                if ignoreCooldown:
                    return
                raise ChatCommandInCooldown(command)
            self.__baseChatCommand(command, channelID, int64Arg, int16Arg, stringArg1, stringArg2)

    def __baseChatCommand(self, command, channelID = 0, int64Arg = 0, int16Arg = 0, stringArg1 = '', stringArg2 = '', ignoreCooldown = True, requestID = None):
        if requestID is None:
            requestID = self.acquireRequestID()
        if chat_shared.isOperationInCooldown(chat_shared.g_chatCooldownData, command):
            if not ignoreCooldown:
                raise ChatCommandInCooldown(command)
        self.base.chatCommandFromClient(requestID, command.index(), channelID, int64Arg, int16Arg, stringArg1, stringArg2)
        return

    def __baseInviteCommand(self, command, inviteType = None, receiverName = '', int64Arg = 0, int16Arg = 0, stringArg1 = '', stringArg2 = '', requestID = None):
        if requestID is None:
            requestID = self.acquireRequestID()
        self.base.inviteCommand(requestID, command.index(), inviteType.index() if inviteType is not None else -1, receiverName, int64Arg, int16Arg, stringArg1, stringArg2)
        return

    def __onReceiveInvite(self, chatActionData):
        inviteID = chatActionData['data'].get('id', None)
        receivedAt = chatActionData['data'].get('received_at', None)
        if inviteID is not None and receivedAt is None:
            requestID = self.acquireRequestID()
            self.base.ackCommand(requestID, CHAT_COMMANDS.inviteReceived.index(), time.time(), inviteID, -1)
        return

    def __dataTimeProcessor(self, actionData):
        actionData['time'] = tm.makeLocalServerTime(actionData['time'])
        actionData['sentTime'] = tm.makeLocalServerTime(actionData['sentTime'])

    def __inviteDataTimeProcessor(self, actionData):
        isInviteAction = CHAT_ACTIONS[actionData['action']] in (CHAT_ACTIONS.createInvite, CHAT_ACTIONS.receiveInvite, CHAT_ACTIONS.receiveArchiveInvite)
        if isInviteAction:
            if actionData.has_key('data'):
                inviteData = actionData['data']
                if 'sent_at' in inviteData:
                    inviteData['sent_at'] = tm.utcToLocalDatetime(tm.makeLocalServerDatetime(inviteData['sent_at']))
                if 'received_at' in inviteData:
                    inviteData['received_at'] = tm.utcToLocalDatetime(tm.makeLocalServerDatetime(inviteData['received_at']))
                if 'processed_at' in inviteData:
                    inviteData['processed_at'] = tm.utcToLocalDatetime(tm.makeLocalServerDatetime(inviteData['processed_at']))

    def __systemMessageTimeProcessor(self, actionData):
        isSystemMessage = CHAT_ACTIONS[actionData['action']] in (CHAT_ACTIONS.personalSysMessage, CHAT_ACTIONS.sysMessage)
        if isSystemMessage:
            if actionData.has_key('data'):
                messageData = actionData['data']
                messageType = messageData['type']
                if 'created_at' in messageData:
                    messageData['created_at'] = tm.makeLocalServerDatetime(messageData['created_at'])
                if 'finished_at' in messageData:
                    messageData['finished_at'] = tm.makeLocalServerDatetime(messageData['finished_at'])
                if 'started_at' in messageData:
                    messageData['started_at'] = tm.makeLocalServerDatetime(messageData['started_at'])
                if messageType == SYS_MESSAGE_TYPE.serverReboot.index():
                    messageData['data'] = tm.makeLocalServerDatetime(messageData['data'])
                elif messageType == SYS_MESSAGE_TYPE.battleResults.index():
                    messageData['data']['arenaCreateTime'] = tm.makeLocalServerTime(messageData['data']['arenaCreateTime'])
                elif messageType == SYS_MESSAGE_TYPE.goldReceived.index():
                    messageData['data']['date'] = tm.makeLocalServerTime(messageData['data']['date'])
                elif messageType in (SYS_MESSAGE_TYPE.accountTypeChanged.index(),
                 SYS_MESSAGE_TYPE.premiumBought.index(),
                 SYS_MESSAGE_TYPE.premiumExtended.index(),
                 SYS_MESSAGE_TYPE.premiumExpired.index()):
                    if 'expiryTime' in messageData['data']:
                        messageData['data']['expiryTime'] = tm.makeLocalServerTime(messageData['data']['expiryTime'])
示例#46
0
 def clear(self):
     self.__idsGen = SequenceIDGenerator()
     self.__idsMap = {'inviteIDs': {},
      'clubIDs': {}}
示例#47
0
class BWChatProvider(object):
    __slots__ = ('__weakref__', '__handlers', '__msgFilters', '__coolDown', '__idGen', '__isEnabled', '__queue')

    def __init__(self):
        super(BWChatProvider, self).__init__()
        self.__handlers = defaultdict(set)
        self.__msgFilters = None
        self.__coolDown = _ChatCooldownManager()
        self.__idGen = SequenceIDGenerator()
        self.__isEnabled = False
        self.__queue = []
        return

    def clear(self):
        self.__handlers.clear()

    def setEnable(self, value):
        if self.__isEnabled == value:
            return
        self.__isEnabled = value
        if self.__isEnabled:
            self.__sendActionsFromQueue()
        else:
            self.__queue = []

    def doAction(self, actionID, args = None, response = False, skipCoolDown = False):
        success, reqID = False, 0
        if self.__coolDown.isInProcess(actionID):
            if not skipCoolDown:
                g_messengerEvents.onErrorReceived(createCoolDownError(actionID))
        else:
            if response:
                reqID = self.__idGen.next()
            if self.__isEnabled:
                success = self.__sendAction(actionID, reqID, args)
            else:
                success = self.__addActionToQueue(actionID, reqID, args)
        return (success, reqID)

    def onActionReceived(self, actionID, reqID, args):
        handlers = self.__handlers[actionID]
        for handler in handlers:
            try:
                handler((actionID, reqID), args)
            except TypeError:
                LOG_ERROR('Handler has been invoked with error', handler)
                LOG_CURRENT_EXCEPTION()

    def setFilters(self, msgFilterChain):
        self.__msgFilters = msgFilterChain

    def filterInMessage(self, message):
        text = self.__msgFilters.chainIn(message.accountDBID, message.text)
        if not text:
            result = None
        else:
            message.text = text
            result = message
        return result

    def filterOutMessage(self, text, limits):
        return self.__msgFilters.chainOut(text, limits)

    def setActionCoolDown(self, actionID, coolDown):
        self.__coolDown.process(actionID, coolDown)

    def isActionInCoolDown(self, actionID):
        return self.__coolDown.isInProcess(actionID)

    def getActionCoolDown(self, actionID):
        return self.__coolDown.getTime(actionID)

    def clearActionCoolDown(self, actionID):
        self.__coolDown.reset(actionID)

    def registerHandler(self, actionID, handler):
        handlers = self.__handlers[actionID]
        if handler in handlers:
            LOG_WARNING('Handler already is exist', actionID, handler)
        else:
            if not hasattr(handler, '__self__') or not isinstance(handler.__self__, ActionsHandler):
                LOG_ERROR('Class of handler is not subclass of ActionsHandler', handler)
                return
            if callable(handler):
                handlers.add(handler)
            else:
                LOG_ERROR('Handler is invalid', handler)

    def unregisterHandler(self, actionID, handler):
        handlers = self.__handlers[actionID]
        if handler in handlers:
            handlers.remove(handler)

    def __sendAction(self, actionID, reqID, args = None):
        player = BigWorld.player()
        if player:
            player.base.messenger_onActionByClient_chat2(actionID, reqID, args or messageArgs())
            return True
        else:
            LOG_ERROR('Player is not defined')
            return False

    def __addActionToQueue(self, actionID, reqID, args = None):
        self.__queue.append((actionID, reqID, args))
        return True

    def __sendActionsFromQueue(self):
        invokedIDs = set()
        while self.__queue:
            actionID, reqID, args = self.__queue.pop()
            if actionID in invokedIDs:
                LOG_WARNING('Action is ignored, your must send action after event "showGUI" is invoked', actionID, reqID, args)
                continue
            self.__sendAction(actionID, reqID, args)
            invokedIDs.add(actionID)
class BrowserController(Controller):
    _BROWSER_TEXTURE = 'BrowserBg'
    _ALT_BROWSER_TEXTURE = 'AltBrowserBg'

    def __init__(self, proxy):
        super(BrowserController, self).__init__(proxy)
        self.__browsers = {}
        self.__browsersCallbacks = {}
        self.__browserIDGenerator = SequenceIDGenerator()
        self.__eventMgr = Event.EventManager()
        self.onBrowserAdded = Event.Event(self.__eventMgr)
        self.onBrowserDeleted = Event.Event(self.__eventMgr)
        self.__urlMacros = URLMarcos()
        self.__pendingBrowsers = {}
        self.__creatingBrowserID = None
        self.__filters = _getGlobalFilters()
        return

    def fini(self):
        self.__filters = None
        self.__eventMgr.clear()
        self.__eventMgr = None
        self.__urlMacros.clear()
        self.__urlMacros = None
        self.__browserIDGenerator = None
        super(BrowserController, self).fini()
        return

    def onAvatarBecomePlayer(self):
        self.__stop()

    def onDisconnected(self):
        self.__stop()

    def addFilterHandler(self, handler):
        """ Adds given @handler to the browser urls filter chain. Calls
        it if there is a @tag in url onto opened browser page.
        Handler should receive url and list of client-specific tags,
        return bool as flag that routine have to stop
        """
        self.__filters.add(handler)

    def removeFilterHandler(self, handler):
        """ Remove given @handler from filtering chain. Handler description
        can be seen in addFilterhandler method doc-string
        """
        self.__filters.discard(handler)

    @async
    @process
    def load(self, url = None, title = None, showActionBtn = True, showWaiting = True, browserID = None, isAsync = False, browserSize = None, isDefault = True, callback = None, showCloseBtn = False, useBrowserWindow = True):
        url = yield self.__urlMacros.parse(url or GUI_SETTINGS.browser.url)
        suffix = yield self.__urlMacros.parse(GUI_SETTINGS.browser.params)
        concatenator = '&' if '?' in url else '?'
        if suffix not in url:
            url = concatenator.join([url, suffix])
        size = browserSize or BROWSER.SIZE
        webBrowserID = browserID
        if browserID is None:
            browserID = self.__browserIDGenerator.next()
            webBrowserID = browserID
        elif type(browserID) is not int:
            webBrowserID = self.__browserIDGenerator.next()
        ctx = {'url': url,
         'title': title,
         'showActionBtn': showActionBtn,
         'showWaiting': showWaiting,
         'browserID': browserID,
         'size': size,
         'isAsync': isAsync,
         'showCloseBtn': showCloseBtn,
         'showWindow': useBrowserWindow}
        texture = browserID not in self.__browsers and browserID not in self.__pendingBrowsers and self._BROWSER_TEXTURE
        app = g_appLoader.getApp()
        if not app:
            raise AssertionError('Application can not be None')
            browser = WebBrowser(webBrowserID, app, texture, size, url, handlers=self.__filters)
            self.__browsers[browserID] = browser
            if self.__isCreatingBrowser():
                self.__pendingBrowsers[browserID] = ctx
            else:
                self.__createBrowser(ctx)
        elif browserID in self.__pendingBrowsers:
            self.__pendingBrowsers[browserID] = ctx
        elif browserID in self.__browsers:
            self.__browsers[browserID].navigate(url)
        callback(browserID)
        return

    def getBrowser(self, browserID):
        return self.__browsers.get(browserID)

    def delBrowser(self, browserID):
        if browserID in self.__browsers:
            browser = self.__browsers.pop(browserID)
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()
            if self.__creatingBrowserID == browserID:
                self.__creatingBrowserID = None
                self.__tryCreateNextPendingBrowser()
        self.onBrowserDeleted(browserID)
        return

    def __isCreatingBrowser(self):
        return self.__creatingBrowserID is not None

    def __createDone(self, ctx):
        self.__creatingBrowserID = None
        g_eventBus.handleEvent(BrowserEvent(BrowserEvent.BROWSER_CREATED, ctx=ctx))
        self.__tryCreateNextPendingBrowser()
        return

    def __tryCreateNextPendingBrowser(self):
        if len(self.__pendingBrowsers) > 0:
            nextCtx = self.__pendingBrowsers.popitem()[1]
            self.__createBrowser(nextCtx)

    def __createBrowser(self, ctx):
        browserID = ctx['browserID']
        self.__creatingBrowserID = browserID
        self.__browsers[browserID].create()
        self.onBrowserAdded(browserID)

        def browserCallback(url, isLoaded):
            self.__clearCallback(browserID)
            if isLoaded:
                self.__showBrowser(browserID, ctx)
            else:
                LOG_WARNING('Browser request url was not loaded!', url)
            self.__createDone(browserID)

        if ctx['isAsync']:
            self.__browsersCallbacks[browserID] = (None, browserCallback)
            self.__browsers[browserID].onLoadEnd += browserCallback
        else:
            self.__browsersCallbacks[browserID] = (browserCallback, None)
            self.__browsers[browserID].onReady += browserCallback
        return

    def __stop(self):
        while self.__browsers:
            browserID, browser = self.__browsers.popitem()
            ready, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if ready is not None:
                browser.onReady -= ready
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()

        return

    def __clearCallback(self, browserID):
        ready, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
        if ready is not None:
            self.__browsers[browserID].onReady -= ready
        if loadEnd is not None:
            self.__browsers[browserID].onLoadEnd -= loadEnd
        return

    def __showBrowser(self, browserID, ctx):
        if ctx.get('showWindow'):
            g_eventBus.handleEvent(LoadViewEvent(VIEW_ALIAS.BROWSER_WINDOW, getViewName(VIEW_ALIAS.BROWSER_WINDOW, browserID), ctx=ctx), EVENT_BUS_SCOPE.LOBBY)
示例#49
0
class BrowserController(Controller):
    _BROWSER_TEXTURE = 'BrowserBg'
    _ALT_BROWSER_TEXTURE = 'AltBrowserBg'

    def __init__(self, proxy):
        super(BrowserController, self).__init__(proxy)
        self.__browsers = {}
        self.__browsersCallbacks = {}
        self.__browserIDGenerator = SequenceIDGenerator()
        self.__eventMgr = Event.EventManager()
        self.onBrowserAdded = Event.Event(self.__eventMgr)
        self.onBrowserDeleted = Event.Event(self.__eventMgr)
        self.__urlMacros = URLMarcos()

    def fini(self):
        self.__eventMgr.clear()
        self.__eventMgr = None
        self.__urlMacros.clear()
        self.__urlMacros = None
        self.__browserIDGenerator = None
        super(BrowserController, self).fini()
        return

    def onAvatarBecomePlayer(self):
        self.__stop()

    def onDisconnected(self):
        self.__stop()

    @async
    @process
    def load(self, url = None, title = None, showActionBtn = True, showWaiting = True, browserID = None, isAsync = False, browserSize = None, isDefault = True, callback = None, showCloseBtn = False):
        url = yield self.__urlMacros.parse(url or GUI_SETTINGS.browser.url)
        suffix = yield self.__urlMacros.parse(GUI_SETTINGS.browser.params)
        concatenator = '&' if '?' in url else '?'
        if suffix not in url:
            url = concatenator.join([url, suffix])
        size = browserSize or BROWSER.SIZE
        if browserID is None:
            browserID = self.__browserIDGenerator.next()
        if browserID not in self.__browsers:
            texture = self._BROWSER_TEXTURE if isDefault else self._ALT_BROWSER_TEXTURE
            app = g_appLoader.getApp()
            if not app:
                raise AssertionError('Application can not be None')
                self.__browsers[browserID] = WebBrowser(browserID, app, texture, size, url)
                self.onBrowserAdded(browserID)
            ctx = {'url': url,
             'title': title,
             'showActionBtn': showActionBtn,
             'showWaiting': showWaiting,
             'browserID': browserID,
             'size': size,
             'isDefault': isDefault,
             'isAsync': isAsync,
             'showCloseBtn': showCloseBtn}

            def browserCallback(*args):
                self.__clearCallback(browserID)
                self.__showBrowser(browserID, ctx)

            def browserAsyncCallback(url, isLoaded):
                self.__clearCallback(browserID)
                if isLoaded:
                    self.__showBrowser(browserID, ctx)
                else:
                    LOG_WARNING('Browser async request url was not loaded!', url)

            self.__browsersCallbacks[browserID] = isAsync and (None, browserCallback)
            self.__browsers[browserID].onLoadEnd += browserAsyncCallback
        else:
            self.__browsersCallbacks[browserID] = (browserCallback, None)
            self.__browsers[browserID].onLoadStart += browserCallback
        callback(browserID)
        return

    def getBrowser(self, browserID):
        return self.__browsers.get(browserID)

    def delBrowser(self, browserID):
        if browserID in self.__browsers:
            browser = self.__browsers.pop(browserID)
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()
        self.onBrowserDeleted(browserID)
        return

    def __stop(self):
        while self.__browsers:
            browserID, browser = self.__browsers.popitem()
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()

        return

    def __clearCallback(self, browserID):
        if browserID in self.__browsersCallbacks:
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                self.__browsers[browserID].onLoadStart -= loadStart
            if loadEnd is not None:
                self.__browsers[browserID].onLoadEnd -= loadEnd
        return

    def __showBrowser(self, browserID, ctx):
        g_eventBus.handleEvent(LoadViewEvent(VIEW_ALIAS.BROWSER_WINDOW, getViewName(VIEW_ALIAS.BROWSER_WINDOW, browserID), ctx=ctx), EVENT_BUS_SCOPE.LOBBY)
示例#50
0
class BrowserController(Controller):
    _BROWSER_TEXTURE = "BrowserBg"
    _ALT_BROWSER_TEXTURE = "AltBrowserBg"

    def __init__(self, proxy):
        super(BrowserController, self).__init__(proxy)
        self.__browsers = {}
        self.__browsersCallbacks = {}
        self.__browserIDGenerator = SequenceIDGenerator()
        self.__eventMgr = Event.EventManager()
        self.onBrowserAdded = Event.Event(self.__eventMgr)
        self.onBrowserDeleted = Event.Event(self.__eventMgr)
        self.__urlMacros = URLMarcos()

    def fini(self):
        self.__eventMgr.clear()
        self.__eventMgr = None
        self.__urlMacros.clear()
        self.__urlMacros = None
        self.__browserIDGenerator = None
        super(BrowserController, self).fini()
        return

    def onAvatarBecomePlayer(self):
        self.__stop()

    def onDisconnected(self):
        self.__stop()

    @async
    @process
    def load(
        self,
        url=None,
        title=None,
        showActionBtn=True,
        showWaiting=True,
        browserID=None,
        isAsync=False,
        browserSize=None,
        background=None,
        isDefault=True,
        callback=None,
        showCloseBtn=False,
    ):
        url = url or GUI_SETTINGS.browser.url
        suffix = yield self.__urlMacros.parse(GUI_SETTINGS.browser.params)
        concatenator = "&" if "?" in url else "?"
        if suffix not in url:
            url = concatenator.join([url, suffix])
        size = browserSize or BROWSER.SIZE
        background = background or BROWSER.BACKGROUND
        if browserID is None:
            browserID = self.__browserIDGenerator.next()
        if browserID not in self.__browsers:
            texture = self._BROWSER_TEXTURE if isDefault else self._ALT_BROWSER_TEXTURE
            app = g_appLoader.getApp()
            if not app:
                raise AssertionError("Application can not be None")
                self.__browsers[browserID] = WebBrowser(browserID, app, texture, size, url, backgroundUrl=background)
                self.onBrowserAdded(browserID)
            ctx = {
                "url": url,
                "title": title,
                "showActionBtn": showActionBtn,
                "showWaiting": showWaiting,
                "browserID": browserID,
                "size": size,
                "isDefault": isDefault,
                "isAsync": isAsync,
                "showCloseBtn": showCloseBtn,
            }

            def browserCallback(*args):
                self.__clearCallback(browserID)
                self.__showBrowser(browserID, ctx)

            self.__browsersCallbacks[browserID] = isAsync and (None, browserCallback)
            self.__browsers[browserID].onLoadEnd += browserCallback
        else:
            self.__browsersCallbacks[browserID] = (browserCallback, None)
            self.__browsers[browserID].onLoadStart += browserCallback
        callback(browserID)
        return

    def getBrowser(self, browserID):
        return self.__browsers.get(browserID)

    def delBrowser(self, browserID):
        if browserID in self.__browsers:
            browser = self.__browsers.pop(browserID)
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()
        self.onBrowserDeleted(browserID)
        return

    def __stop(self):
        while self.__browsers:
            browserID, browser = self.__browsers.popitem()
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()

        return

    def __clearCallback(self, browserID):
        if browserID in self.__browsersCallbacks:
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                self.__browsers[browserID].onLoadStart -= loadStart
            if loadEnd is not None:
                self.__browsers[browserID].onLoadEnd -= loadEnd
        return

    def __showBrowser(self, browserID, ctx):
        g_eventBus.handleEvent(
            LoadViewEvent(VIEW_ALIAS.BROWSER_WINDOW, getViewName(VIEW_ALIAS.BROWSER_WINDOW, browserID), ctx=ctx),
            EVENT_BUS_SCOPE.LOBBY,
        )
示例#51
0
 def __init__(self, setup):
     super(_SquadInvitationsRecorder, self).__init__(setup)
     self.__idGen = SequenceIDGenerator()
示例#52
0
 def __init__(self):
     assert isinstance(self, BigWorld.Entity)
     self.__chatActionCallbacks = {}
     self._idGen = SequenceIDGenerator()
示例#53
0
class FortBattlesCache(object):
    __selectedID = 0

    def __init__(self, controller):
        self.__controller = weakref.proxy(controller)
        self.__idGen = SequenceIDGenerator()
        self.__cache = {}
        self.__idToIndex = {}
        self.__indexToID = {}
        self.__selectedUnit = None
        self.__isRequestInProcess = False
        return

    def __del__(self):
        LOG_DEBUG('Fort battles cache deleted:', self)

    def clear(self):
        self.__controller = None
        self.__cache.clear()
        self.__idToIndex.clear()
        self.__indexToID.clear()
        self.__selectedUnit = None
        return

    def start(self):
        fort = self.__controller.getFort()
        if fort:
            fort.onFortBattleChanged += self.__fort_onFortBattleChanged
            fort.onFortBattleRemoved += self.__fort_onFortBattleRemoved
            fort.onFortBattleUnitReceived += self.__fort_onFortBattleUnitReceived
            fort.onEnemyStateChanged += self.__fort_onEnemyStateChanged
            self.__cache = self.__buildCache()
        else:
            LOG_ERROR('Client fort is not found')

    def stop(self):
        fort = self.__controller.getFort()
        if fort:
            fort.onFortBattleChanged -= self.__fort_onFortBattleChanged
            fort.onFortBattleRemoved -= self.__fort_onFortBattleRemoved
            fort.onFortBattleUnitReceived -= self.__fort_onFortBattleUnitReceived
            fort.onEnemyStateChanged -= self.__fort_onEnemyStateChanged
        self.clear()

    @property
    def isRequestInProcess(self):
        return self.__isRequestInProcess

    @classmethod
    def getSelectedID(cls):
        return cls.__selectedID

    def getSelectedIdx(self):
        return self.__getClientIdx(self.getSelectedID())

    def clearSelectedID(self):
        self.__selectedUnit = None
        self._setSelectedID(0)
        return

    def setSelectedID(self, selectedID):
        if selectedID not in self.__cache:
            LOG_WARNING('Item is not found in cache', selectedID)
            return False
        else:
            self.__selectedUnit = None
            return self._setSelectedID(selectedID)

    def getItem(self, battleID):
        try:
            item, fortBattle = self.__cache[battleID]
        except KeyError:
            LOG_ERROR('Item not found in cache', battleID)
            item = None
            fortBattle = None

        return (item, fortBattle)

    def getUnitByIndex(self, index):
        unit = None
        if index in self.__indexToID:
            battleID = self.__indexToID[index]
            unit = self.__getUnit(battleID)
        return unit

    def getSelectedUnit(self):
        return self.__getUnit(self.getSelectedID())

    def getIterator(self):
        for item, battleItem in self.__cache.itervalues():
            if item.filter():
                yield (item, battleItem)

    @classmethod
    def _setSelectedID(cls, selectedID):
        result = False
        if selectedID != cls.__selectedID:
            cls.__selectedID = selectedID
            result = True
        return result

    @classmethod
    def _removeStoredData(cls):
        cls.__selectedID = (0, 0)

    def __buildCache(self):
        cache = {}
        fort = self.__controller.getFort()
        if not fort:
            LOG_WARNING('Client fort is not found')
            return cache
        fortBattles = fort.getAttacksAndDefencesIn(timePeriod=2 * time_utils.ONE_WEEK)
        selectedID = self.getSelectedID()
        found = False
        for item in fortBattles:
            battleID = item.getBattleID()
            if not found and battleID == selectedID:
                found = True
            battleItem = fort.getBattle(battleID)
            cache[battleID] = (item, battleItem)

        if not found:
            self.clearSelectedID()
        return cache

    def __updateItem(self, battleID, fort):
        item = fort.getBattleItemByBattleID(battleID)
        if item is None:
            LOG_ERROR('Fort battle is not found', battleID, fort.attacks, fort.defences)
            return (None, None)
        else:
            fortBattle = fort.getBattle(battleID)
            self.__cache[battleID] = (item, fortBattle)
            if self.getSelectedID() == battleID:
                self.__selectedUnit = None
            return (item, fortBattle)

    def __requestCallback(self, _):
        self.__isRequestInProcess = False

    def __removeItem(self, battleID):
        result = False
        if battleID in self.__cache:
            self.__cache.pop(battleID)
            result = True
        if self.getSelectedID() == battleID:
            self.clearSelectedID()
        clientIdx = self.__idToIndex.pop(battleID, None)
        if clientIdx is not None:
            self.__indexToID.pop(clientIdx, None)
        return result

    def __getClientIdx(self, battleID):
        if battleID == 0:
            return 0
        if battleID not in self.__idToIndex:
            clientIdx = self.__idGen.next()
            self.__idToIndex[battleID] = clientIdx
            self.__indexToID[clientIdx] = battleID
        else:
            clientIdx = self.__idToIndex[battleID]
        return clientIdx

    def __getUnit(self, battleID):
        fort = self.__controller.getFort()
        if not fort:
            LOG_WARNING('Client fort is not found')
            return
        else:
            isSelected = battleID == self.getSelectedID()
            if isSelected and self.__selectedUnit is not None:
                return self.__selectedUnit
            unit = fort.getFortBattleUnit(battleID)
            if isSelected:
                self.__selectedUnit = unit
            return unit

    def __fort_onEnemyStateChanged(self, battleID, isReady):
        fort = self.__controller.getFort()
        if fort:
            item, battleItem = self.__updateItem(battleID, fort)
            if item:
                self.__controller._listeners.notify('onFortBattleChanged', self, item, battleItem)

    def __fort_onFortBattleChanged(self, battleID):
        fort = self.__controller.getFort()
        if fort:
            item, battleItem = self.__updateItem(battleID, fort)
            if item:
                self.__controller._listeners.notify('onFortBattleChanged', self, item, battleItem)

    def __fort_onFortBattleRemoved(self, battleID):
        if self.__removeItem(battleID):
            self.__controller._listeners.notify('onFortBattleRemoved', self, battleID)

    def __fort_onFortBattleUnitReceived(self, battleID):
        fort = self.__controller.getFort()
        if fort:
            if self.__selectedID == battleID:
                self.__selectedUnit = None
            self.__controller._listeners.notify('onFortBattleUnitReceived', self.__getClientIdx(battleID))
        return
示例#54
0
class SortiesCache(object):
    __selectedID = (0, 0)
    __rosterTypeID = 0

    def __init__(self, controller):
        self.__controller = weakref.proxy(controller)
        self.__idGen = SequenceIDGenerator()
        self.__cache = {}
        self.__idToIndex = {}
        self.__indexToID = {}
        self.__selectedUnit = None
        self.__isRequestInProcess = False
        self.__cooldownRequest = None
        return

    def __del__(self):
        LOG_DEBUG('Sortie cache deleted:', self)

    def clear(self):
        self.__controller = None
        self.__cache.clear()
        self.__idToIndex.clear()
        self.__indexToID.clear()
        self.__selectedUnit = None
        if self.__cooldownRequest is not None:
            BigWorld.cancelCallback(self.__cooldownRequest)
            self.__cooldownRequest = None
        return

    def start(self):
        fort = self.__controller.getFort()
        if fort:
            fort.onSortieChanged += self.__fort_onSortieChanged
            fort.onSortieRemoved += self.__fort_onSortieRemoved
            fort.onSortieUnitReceived += self.__fort_onSortieUnitReceived
            self.__cache = self.__buildCache()
        else:
            LOG_ERROR('Client fort is not found')

    def stop(self):
        fort = self.__controller.getFort()
        if fort:
            fort.onSortieChanged -= self.__fort_onSortieChanged
            fort.onSortieRemoved -= self.__fort_onSortieRemoved
            fort.onSortieUnitReceived -= self.__fort_onSortieUnitReceived
        self.clear()

    def setController(self, controller):
        self.__controller = weakref.proxy(controller)

    @property
    def isRequestInProcess(self):
        return self.__isRequestInProcess

    @classmethod
    def getSelectedID(cls):
        return cls.__selectedID

    def clearSelectedID(self):
        self.__selectedUnit = None
        self._setSelectedID((0, 0))
        return

    def setSelectedID(self, selectedID):
        if selectedID not in self.__cache:
            LOG_WARNING('Item is not found in cache', selectedID)
            return False
        else:
            self.__selectedUnit = None
            self._setSelectedID(selectedID)
            if BigWorld.player().isLongDisconnectedFromCenter:
                self.__controller._listeners.notify('onSortieUnitReceived', self.__getClientIdx(selectedID))
                return True
            unit = self.getSelectedUnit()
            if unit and not self.__cache[selectedID]._isDirty:
                self.__controller._listeners.notify('onSortieUnitReceived', self.__getClientIdx(selectedID))
            else:
                self._requestSortieUnit(selectedID)
            return True

    @classmethod
    def getRosterTypeID(cls):
        return cls.__rosterTypeID

    def setRosterTypeID(self, rosterTypeID):
        result = self._setRosterTypeID(rosterTypeID)
        if result:
            self.__cache = self.__buildCache()
        return result

    def getItem(self, sortieID):
        try:
            item = self.__cache[sortieID]
        except KeyError:
            LOG_ERROR('Item not found in cache', sortieID)
            item = None

        return item

    def getUnitByIndex(self, index):
        unit = None
        if index in self.__indexToID:
            sortieID = self.__indexToID[index]
            unit = self.__getUnit(sortieID)
        return unit

    def getSelectedUnit(self):
        return self.__getUnit(self.getSelectedID())

    def getIterator(self):
        for item in self.__cache.itervalues():
            if item.filter(self.__rosterTypeID):
                yield item

    def _requestSortieUnit(self, selectedID):
        Waiting.show('fort/sortie/get')
        if self.__cooldownRequest is not None:
            Waiting.hide('fort/sortie/get')
            BigWorld.cancelCallback(self.__cooldownRequest)
            self.__cooldownRequest = None
        ctx = RequestSortieUnitCtx(waitingID='', *selectedID)

        def requester():
            self.__cooldownRequest = None
            self.__isRequestInProcess = True
            self.__controller.request(ctx, self.__requestCallback)
            return

        if self.__controller._cooldown.isInProcess(ctx.getRequestType()):
            self.__cooldownRequest = BigWorld.callback(self.__controller._cooldown.getTime(ctx.getRequestType()), requester)
        else:
            requester()
        return

    @classmethod
    def _setSelectedID(cls, selectedID):
        result = False
        if selectedID != cls.__selectedID and len(selectedID) == 2:
            cls.__selectedID = selectedID
            result = True
        return result

    @classmethod
    def _setRosterTypeID(cls, rosterTypeID):
        result = False
        if rosterTypeID != cls.__rosterTypeID:
            cls.__rosterTypeID = rosterTypeID
            result = True
        return result

    @classmethod
    def _removeStoredData(cls):
        cls.__selectedID = (0, 0)
        cls.__rosterTypeID = 0

    def __buildCache(self):
        cache = {}
        fort = self.__controller.getFort()
        if not fort:
            LOG_WARNING('Client fort is not found')
            return cache
        sorties = fort.getSorties()
        selectedID = self.getSelectedID()
        found = False
        for sortieID, sortie in sorties.iteritems():
            item = SortieItem(sortieID, sortie)
            if not found and item.getID() == selectedID:
                found = True
            cache[sortieID] = item

        if not found:
            self.clearSelectedID()
        return cache

    def __updateItem(self, sortieID, fort):
        sortie = fort.getSortieShortData(*sortieID)
        if sortie is None:
            LOG_ERROR('Sortie is not found', sortieID, fort.sorties)
            return
        else:
            if sortieID in self.__cache:
                item = self.__cache[sortieID]
                item._updateItemData(sortie)
                if item._isDirty and self.__selectedID == item.getID() and item.filter(self.getRosterTypeID()):
                    self.__selectedUnit = None
                    self._requestSortieUnit(sortieID)
            else:
                item = SortieItem(sortieID, sortie)
                self.__cache[sortieID] = item
            return item

    def __requestCallback(self, _):
        Waiting.hide('fort/sortie/get')
        self.__isRequestInProcess = False

    def __removeItem(self, sortieID):
        result = False
        if sortieID in self.__cache:
            self.__cache.pop(sortieID)
            result = True
        if self.getSelectedID() == sortieID:
            self.clearSelectedID()
        clientIdx = self.__idToIndex.pop(sortieID, None)
        if clientIdx is not None:
            self.__indexToID.pop(clientIdx, None)
        return result

    def __getClientIdx(self, sortieID):
        if sortieID == (0, 0):
            return 0
        if sortieID not in self.__idToIndex:
            clientIdx = self.__idGen.next()
            self.__idToIndex[sortieID] = clientIdx
            self.__indexToID[clientIdx] = sortieID
        else:
            clientIdx = self.__idToIndex[sortieID]
        return clientIdx

    def __getUnit(self, sortieID):
        fort = self.__controller.getFort()
        if not fort:
            LOG_WARNING('Client fort is not found')
            return
        else:
            isSelected = self.getSelectedID() == sortieID
            if isSelected and self.__selectedUnit is not None:
                return self.__selectedUnit
            unit = fort.getSortieUnit(*sortieID)
            if isSelected:
                self.__selectedUnit = unit
            return unit

    def __fort_onSortieChanged(self, unitMgrID, peripheryID):
        fort = self.__controller.getFort()
        sortieID = (unitMgrID, peripheryID)
        if fort:
            item = self.__updateItem(sortieID, fort)
            if item:
                self.__controller._listeners.notify('onSortieChanged', self, item)

    def __fort_onSortieRemoved(self, unitMgrID, peripheryID):
        sortieID = (unitMgrID, peripheryID)
        if self.__removeItem(sortieID):
            self.__controller._listeners.notify('onSortieRemoved', self, sortieID)

    def __fort_onSortieUnitReceived(self, unitMgrID, peripheryID):
        fort = self.__controller.getFort()
        sortieID = (unitMgrID, peripheryID)
        if fort:
            if unitMgrID in self.__cache:
                self.__cache[sortieID]._isDirty = False
            if self.getSelectedID() == sortieID:
                self.__selectedUnit = None
            self.__controller._listeners.notify('onSortieUnitReceived', self.__getClientIdx(sortieID))
        return
class ServiceChannelManager(ChatActionsListener):
    def __init__(self):
        ChatActionsListener.__init__(self)
        self.__idGenerator = SequenceIDGenerator()
        self.__messages = deque([], SCH_MSGS_MAX_LENGTH)
        self.__unreadMessagesCount = 0

    def addListeners(self):
        self.addListener(self.onReceiveSysMessage, CHAT_ACTIONS.sysMessage)
        self.addListener(self.onReceivePersonalSysMessage, CHAT_ACTIONS.personalSysMessage)

    def clear(self):
        self.__messages.clear()
        self.__unreadMessagesCount = 0

    def switch(self, scope):
        if scope is MESSENGER_SCOPE.LOBBY:
            self.requestLastServiceMessages()

    def requestLastServiceMessages(self):
        BigWorld.player().requestLastSysMessages()

    def onReceiveSysMessage(self, chatAction):
        message = ServiceChannelMessage.fromChatAction(chatAction)
        self.__addServerMessage(message)

    def onReceivePersonalSysMessage(self, chatAction):
        message = ServiceChannelMessage.fromChatAction(chatAction, personal=True)
        self.__addServerMessage(message)

    def pushClientSysMessage(self, message, msgType, isAlert=False, priority=None):
        return self.__addClientMessage(
            message, SCH_CLIENT_MSG_TYPE.SYS_MSG_TYPE, isAlert=isAlert, auxData=[msgType.name(), priority]
        )

    def pushClientMessage(self, message, msgType, isAlert=False, auxData=None):
        return self.__addClientMessage(message, msgType, isAlert=isAlert, auxData=auxData)

    def getReadMessages(self):
        if self.__unreadMessagesCount > 0:
            messages = list(self.__messages)[: -self.__unreadMessagesCount]
        else:
            messages = self.__messages
        for clientID, message in messages:
            yield (clientID, message)

    def getMessage(self, clientID):
        mapping = dict(self.__messages)
        message = (False, None, None)
        if clientID in mapping:
            message = mapping[clientID]
        return message

    def getUnreadCount(self):
        return self.__unreadMessagesCount

    def resetUnreadCount(self):
        self.__unreadMessagesCount = 0

    def handleUnreadMessages(self):
        if not self.__unreadMessagesCount:
            return
        unread = list(self.__messages)[-self.__unreadMessagesCount :]
        serviceChannel = g_messengerEvents.serviceChannel
        for clientID, (isServerMsg, formatted, settings) in unread:
            if isServerMsg:
                serviceChannel.onServerMessageReceived(clientID, formatted, settings)
            else:
                serviceChannel.onClientMessageReceived(clientID, formatted, settings)

    @process
    def __addServerMessage(self, message):
        yield lambda callback: callback(True)
        formatter = collections_by_type.SERVER_FORMATTERS.get(message.type)
        serviceChannel = g_messengerEvents.serviceChannel
        serviceChannel.onChatMessageReceived(self.__idGenerator.next(), message)
        LOG_DEBUG("Server message received", message, formatter)
        if formatter:
            try:
                if formatter.isAsync():
                    formatted, settings = yield formatter.format(message)
                else:
                    formatted, settings = formatter.format(message)
            except:
                LOG_CURRENT_EXCEPTION()
                return

            if formatted:
                clientID = self.__idGenerator.next()
                self.__messages.append((clientID, (True, formatted, settings)))
                self.__unreadMessagesCount += 1
                serviceChannel.onServerMessageReceived(clientID, formatted, settings)
                customEvent = settings.getCustomEvent()
                if customEvent is not None:
                    serviceChannel.onCustomMessageDataReceived(clientID, customEvent)
            elif IS_DEVELOPMENT:
                LOG_WARNING("Not enough data to format. Action data : ", message)
        elif IS_DEVELOPMENT:
            LOG_WARNING("Formatter not found. Action data : ", message)
        return

    def __addClientMessage(self, message, msgType, isAlert=False, auxData=None):
        if auxData is None:
            auxData = []
        clientID = 0
        formatter = collections_by_type.CLIENT_FORMATTERS.get(msgType)
        if formatter:
            try:
                formatted, settings = formatter.format(message, auxData)
            except:
                LOG_CURRENT_EXCEPTION()
                return

            if formatted:
                clientID = self.__idGenerator.next()
                if not settings.isAlert:
                    settings.isAlert = isAlert
                self.__messages.append((clientID, (False, formatted, settings)))
                self.__unreadMessagesCount += 1
                g_messengerEvents.serviceChannel.onClientMessageReceived(clientID, formatted, settings)
            elif IS_DEVELOPMENT:
                LOG_WARNING("Not enough data to format. Action data : ", message)
        elif IS_DEVELOPMENT:
            LOG_WARNING("Formatter not found:", msgType, message)
        return clientID
 def __init__(self):
     ChatActionsListener.__init__(self)
     self.__idGenerator = SequenceIDGenerator()
     self.__messages = deque([], SCH_MSGS_MAX_LENGTH)
     self.__unreadMessagesCount = 0
示例#57
0
def _generateUserName():
    global _g_namesGenerator
    if _g_namesGenerator is None:
        _g_namesGenerator = SequenceIDGenerator()
    return '%s %d' % (USER_DEFAULT_NAME_PREFIX, _g_namesGenerator.next())
示例#58
0
class BrowserController(Controller, AppRef):
    _BROWSER_TEXTURE = 'BrowserBg'
    _ALT_BROWSER_TEXTURE = 'AltBrowserBg'

    def __init__(self, proxy):
        super(BrowserController, self).__init__(proxy)
        self.__browsers = {}
        self.__browsersCallbacks = {}
        self.__browserIDGenerator = SequenceIDGenerator()
        self.__eventMgr = Event.EventManager()
        self.onBrowserAdded = Event.Event(self.__eventMgr)
        self.onBrowserDeleted = Event.Event(self.__eventMgr)
        self.__urlMacros = URLMarcos()

    def fini(self):
        self.__eventMgr.clear()
        self.__eventMgr = None
        self.__urlMacros.clear()
        self.__urlMacros = None
        self.__browserIDGenerator = None
        super(BrowserController, self).fini()
        return

    def onBattleStarted(self):
        self.__stop()

    def onDisconnected(self):
        self.__stop()

    @async
    @process
    def load(self, url = None, title = None, showActionBtn = True, showWaiting = True, browserID = None, isAsync = False, browserSize = None, background = None, isDefault = True, callback = None):
        url = url or GUI_SETTINGS.browser.url
        suffix = yield self.__urlMacros.parse(GUI_SETTINGS.browser.params)
        concatenator = '&' if '?' in url else '?'
        if suffix not in url:
            url = concatenator.join([url, suffix])
        size = browserSize or BROWSER.SIZE
        background = background or BROWSER.BACKGROUND
        if browserID not in self.__browsers:
            browserID = self.__browserIDGenerator.next()
            texture = self._BROWSER_TEXTURE if isDefault else self._ALT_BROWSER_TEXTURE
            self.__browsers[browserID] = WebBrowser(browserID, self.app, texture, size, url, backgroundUrl=background)
            self.onBrowserAdded(browserID)
        ctx = {'url': url,
         'title': title,
         'showActionBtn': showActionBtn,
         'showWaiting': showWaiting,
         'browserID': browserID,
         'size': size,
         'isDefault': isDefault,
         'isAsync': isAsync}

        def browserCallback(*args):
            self.__clearCallback(browserID)
            self.__showBrowser(browserID, ctx)

        if isAsync:
            self.__browsersCallbacks[browserID] = (None, browserCallback)
            self.__browsers[browserID].onLoadEnd += browserCallback
        else:
            self.__browsersCallbacks[browserID] = (browserCallback, None)
            self.__browsers[browserID].onLoadStart += browserCallback
        callback(browserID)
        return

    def getBrowser(self, browserID):
        return self.__browsers.get(browserID)

    def delBrowser(self, browserID):
        if browserID in self.__browsers:
            browser = self.__browsers.pop(browserID)
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()
        self.onBrowserDeleted(browserID)
        return

    def __stop(self):
        while self.__browsers:
            browserID, browser = self.__browsers.popitem()
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                browser.onLoadStart -= loadStart
            if loadEnd is not None:
                browser.onLoadEnd -= loadEnd
            browser.destroy()

        return

    def __clearCallback(self, browserID):
        if browserID in self.__browsersCallbacks:
            loadStart, loadEnd = self.__browsersCallbacks.pop(browserID, (None, None))
            if loadStart is not None:
                self.__browsers[browserID].onLoadStart -= loadStart
            if loadEnd is not None:
                self.__browsers[browserID].onLoadEnd -= loadEnd
        return

    def __showBrowser(self, browserID, ctx):
        self.app.fireEvent(LoadViewEvent(VIEW_ALIAS.BROWSER_WINDOW, getViewName(VIEW_ALIAS.BROWSER_WINDOW, browserID), ctx=ctx), EVENT_BUS_SCOPE.LOBBY)
示例#59
0
 def __init__(self, parent):
     super(EquipmentsPlugin, self).__init__(parent)
     self.__generator = SequenceIDGenerator()
示例#60
0
class BWChatProvider(object):

    def __init__(self):
        super(BWChatProvider, self).__init__()
        self.__handlers = defaultdict(set)
        self.__msgFilters = None
        self.__coolDown = _ChatCooldownManager()
        self.__idGen = SequenceIDGenerator()



    def clear(self):
        self.__handlers.clear()



    def doAction(self, actionID, args = None, response = False, skipCoolDown = False):
        player = BigWorld.player()
        (success, reqID,) = (False, 0)
        if player:
            if self.__coolDown.isInProcess(actionID):
                if not skipCoolDown:
                    g_messengerEvents.onServerErrorReceived(createCoolDownError(actionID))
            elif response:
                reqID = self.__idGen.next()
            player.base.messenger_onActionByClient_chat2(actionID, reqID, args or messageArgs())
            success = True
        else:
            LOG_ERROR('Player is not defined')
        return (success, reqID)



    def onActionReceived(self, actionID, reqID, args):
        handlers = self.__handlers[actionID]
        for handler in handlers:
            try:
                handler((actionID, reqID), args)
            except TypeError:
                LOG_ERROR('Handler has been invoked with error', handler)
                LOG_CURRENT_EXCEPTION()




    def setFilters(self, msgFilterChain):
        self.__msgFilters = msgFilterChain



    def filterInMessage(self, message):
        text = self.__msgFilters.chainIn(message.accountDBID, message.text)
        if not text:
            result = None
        else:
            message.text = text
            result = message
        return result



    def filterOutMessage(self, text, limits):
        return self.__msgFilters.chainOut(text, limits)



    def setActionCoolDown(self, actionID, coolDown):
        self.__coolDown.process(actionID, coolDown)



    def isActionInCoolDown(self, actionID):
        return self.__coolDown.isInProcess(actionID)



    def getActionCoolDown(self, actionID):
        return self.__coolDown.getTime(actionID)



    def clearActionCoolDown(self, actionID):
        self.__coolDown.reset(actionID)



    def registerHandler(self, actionID, handler):
        handlers = self.__handlers[actionID]
        if handler in handlers:
            LOG_WARNING('Handler already is exist', actionID, handler)
        elif not hasattr(handler, '__self__') or not isinstance(handler.__self__, ActionsHandler):
            LOG_ERROR('Class of handler is not subclass of ActionsHandler', handler)
            return 
        if callable(handler):
            handlers.add(handler)
        else:
            LOG_ERROR('Handler is invalid', handler)



    def unregisterHandler(self, actionID, handler):
        handlers = self.__handlers[actionID]
        if handler in handlers:
            handlers.remove(handler)