Esempio n. 1
0
class _FortController(IFortController):
    _TIME_OUT = 45

    def __init__(self, handlers):
        super(_FortController, self).__init__()
        self._requester = None
        self._limits = None
        self._validators = None
        self._sortiesCache = None
        self._sortiesCurfewCtrl = None
        self._fortBattlesCache = None
        self._publicInfoCache = None
        self._handlers = handlers
        self._cooldown = FortCooldownManager()
        self.clear()
        return

    def clear(self):
        self._clan = None
        self._listeners = None
        self._waiters = None
        return

    def init(self, clan, listeners, prevController = None):
        self._requester = PlayerFortRequester()
        self._requester.init()
        self._setLimits()
        self._setValidators()
        self._clan = clan
        self._listeners = listeners
        self._addFortListeners()
        self._waiters = {}

    def fini(self, clearCache = True):
        self._removeFortListeners()
        self.stopProcessing()
        if self._requester:
            self._requester.fini()
            self._requester = None
        if self._limits:
            self._limits = None
        if self._validators:
            self._validators.fini()
            self._validators = None
        self.clear()
        self._handlers.clear()
        return

    def stopProcessing(self):
        self._clearWaiters()
        if self._requester is not None:
            self._requester.stopProcessing()
        return

    def getFort(self):
        return getClientFort()

    def getPermissions(self):
        if self._clan:
            roles = self._clan.clanRole
        else:
            roles = 0
        return FortPermissions(roles)

    def getLimits(self):
        return self._limits

    def getValidators(self):
        return self._validators

    def getSortiesCache(self):
        return self._sortiesCache

    def getFortBattlesCache(self):
        return self._fortBattlesCache

    def getSortiesCurfewCtrl(self):
        return self._sortiesCurfewCtrl

    def getPublicInfoCache(self):
        return self._publicInfoCache

    def removeSortiesCache(self):
        SortiesCache._removeStoredData()

    def removeFortBattlesCache(self):
        FortBattlesCache._removeStoredData()

    def request(self, ctx, callback = None):
        if self._clan is None:
            return self._failChecking('Clan is not defined', ctx, callback)
        else:
            requestType = ctx.getRequestType()
            if requestType in self._handlers:
                cooldown = ctx.getCooldown()
                if self._cooldown.validate(requestType, cooldown):
                    if callback:
                        callback(False)
                else:
                    LOG_DEBUG('Fort request', ctx)
                    if self._handlers[requestType](ctx, callback=partial(self._callbackWrapper, requestType, callback, cooldown)):
                        self._waiters[requestType] = BigWorld.callback(self._TIME_OUT, self._onTimeout)
                        self._cooldown.process(requestType, cooldown)
            else:
                self._failChecking('Handler not found', ctx, callback)
            return

    def subscribe(self, callback = None):

        def _doRequest():
            LOG_DEBUG('Fort request to subscribe')
            result = self._requester.doRequestEx(FortRequestCtx(), callback, 'subscribe')
            if result:
                self._waiters[FORT_REQUEST_TYPE.SUBSCRIBE] = BigWorld.callback(self._TIME_OUT, self._onTimeout)
                self._cooldown.process(FORT_REQUEST_TYPE.SUBSCRIBE)

        if self._cooldown.validate(FORT_REQUEST_TYPE.SUBSCRIBE):
            BigWorld.callback(self._cooldown.getTime(FORT_REQUEST_TYPE.SUBSCRIBE), _doRequest)
        else:
            _doRequest()

    def unsubscribe(self, callback = None):
        LOG_DEBUG('Fort request to unsubscribe')
        self._requester.doRequestEx(FortRequestCtx(), callback, 'unsubscribe')
        return False

    def _failChecking(self, ctx, msg, callback = None):
        if callback:
            callback(False)
        LOG_ERROR(msg, ctx)
        return False

    def _addFortListeners(self):
        pass

    def _removeFortListeners(self):
        pass

    def _setLimits(self):
        self._limits = NoFortLimits()

    def _setValidators(self):
        self._validators = NoFortValidators()

    def _callbackWrapper(self, requestType, callback, cooldown, *args):
        callbackID = self._waiters.pop(requestType, None)
        if callbackID is not None:
            BigWorld.cancelCallback(callbackID)
        self._cooldown.adjust(requestType, cooldown)
        callback(*args)
        return

    def _clearWaiters(self):
        if self._waiters is not None:
            while len(self._waiters):
                _, callbackID = self._waiters.popitem()
                BigWorld.cancelCallback(callbackID)

        return

    def _onTimeout(self):
        LOG_ERROR('Fort request time out!')
        self.stopProcessing()
        g_eventBus.handleEvent(events.FortEvent(events.FortEvent.REQUEST_TIMEOUT), scope=EVENT_BUS_SCOPE.FORT)
Esempio n. 2
0
class _FortController(IFortController):
    _TIME_OUT = 45

    def __init__(self, handlers):
        super(_FortController, self).__init__()
        self._requester = None
        self._limits = None
        self._validators = None
        self._sortiesCache = None
        self._sortiesCurfewCtrl = None
        self._fortBattlesCache = None
        self._publicInfoCache = None
        self._handlers = handlers
        self._cooldown = FortCooldownManager()
        self.clear()
        return

    def clear(self):
        self._clan = None
        self._listeners = None
        self._waiters = None
        return

    def init(self, clan, listeners, prevController=None):
        self._requester = PlayerFortRequester()
        self._requester.init()
        self._setLimits()
        self._setValidators()
        self._clan = clan
        self._listeners = listeners
        self._addFortListeners()
        self._waiters = {}

    def fini(self, clearCache=True):
        self._removeFortListeners()
        self.stopProcessing()
        if self._requester:
            self._requester.fini()
            self._requester = None
        if self._limits:
            self._limits = None
        if self._validators:
            self._validators.fini()
            self._validators = None
        self.clear()
        self._handlers.clear()
        return

    def stopProcessing(self):
        self._clearWaiters()
        if self._requester is not None:
            self._requester.stopProcessing()
        return

    def getFort(self):
        return getClientFort()

    def getPermissions(self):
        if self._clan:
            roles = self._clan.clanRole
        else:
            roles = 0
        return FortPermissions(roles)

    def getLimits(self):
        return self._limits

    def getValidators(self):
        return self._validators

    def getSortiesCache(self):
        return self._sortiesCache

    def getFortBattlesCache(self):
        return self._fortBattlesCache

    def getSortiesCurfewCtrl(self):
        return self._sortiesCurfewCtrl

    def getPublicInfoCache(self):
        return self._publicInfoCache

    def removeSortiesCache(self):
        SortiesCache._removeStoredData()

    def removeFortBattlesCache(self):
        FortBattlesCache._removeStoredData()

    def request(self, ctx, callback=None):
        if self._clan is None:
            return self._failChecking('Clan is not defined', ctx, callback)
        else:
            requestType = ctx.getRequestType()
            if requestType in self._handlers:
                cooldown = ctx.getCooldown()
                if self._cooldown.validate(requestType, cooldown):
                    if callback:
                        callback(False)
                else:
                    LOG_DEBUG('Fort request', ctx)
                    if self._handlers[requestType](
                            ctx,
                            callback=partial(self._callbackWrapper,
                                             requestType, callback, cooldown)):
                        self._waiters[requestType] = BigWorld.callback(
                            self._TIME_OUT, self._onTimeout)
                        self._cooldown.process(requestType, cooldown)
            else:
                self._failChecking('Handler not found', ctx, callback)
            return

    def subscribe(self, callback=None):
        def _doRequest():
            LOG_DEBUG('Fort request to subscribe')
            result = self._requester.doRequestEx(FortRequestCtx(), callback,
                                                 'subscribe')
            if result:
                self._waiters[FORT_REQUEST_TYPE.SUBSCRIBE] = BigWorld.callback(
                    self._TIME_OUT, self._onTimeout)
                self._cooldown.process(FORT_REQUEST_TYPE.SUBSCRIBE)

        if self._cooldown.validate(FORT_REQUEST_TYPE.SUBSCRIBE):
            BigWorld.callback(
                self._cooldown.getTime(FORT_REQUEST_TYPE.SUBSCRIBE),
                _doRequest)
        else:
            _doRequest()

    def unsubscribe(self, callback=None):
        LOG_DEBUG('Fort request to unsubscribe')
        self._requester.doRequestEx(FortRequestCtx(), callback, 'unsubscribe')
        return False

    def _failChecking(self, ctx, msg, callback=None):
        if callback:
            callback(False)
        LOG_ERROR(msg, ctx)
        return False

    def _addFortListeners(self):
        pass

    def _removeFortListeners(self):
        pass

    def _setLimits(self):
        self._limits = NoFortLimits()

    def _setValidators(self):
        self._validators = NoFortValidators()

    def _callbackWrapper(self, requestType, callback, cooldown, *args):
        callbackID = self._waiters.pop(requestType, None)
        if callbackID is not None:
            BigWorld.cancelCallback(callbackID)
        self._cooldown.adjust(requestType, cooldown)
        callback(*args)
        return

    def _clearWaiters(self):
        if self._waiters is not None:
            while len(self._waiters):
                _, callbackID = self._waiters.popitem()
                BigWorld.cancelCallback(callbackID)

        return

    def _onTimeout(self):
        LOG_ERROR('Fort request time out!')
        self.stopProcessing()
        g_eventBus.handleEvent(events.FortEvent(
            events.FortEvent.REQUEST_TIMEOUT),
                               scope=EVENT_BUS_SCOPE.FORT)