class WebController(WebListeners, IWebController):
    lobbyContext = dependency.descriptor(ILobbyContext)

    def __init__(self):
        super(WebController, self).__init__()
        self.__cache = {}
        self.__searchDataCache = {}
        self.__state = None
        self.__profile = None
        self.__userCache = None
        self.__clanCache = None
        self.__simWGCGEnabled = True
        return

    def getRequesterConfig(self):
        return self.lobbyContext.getServerSettings().wgcg

    def simEnableWGCG(self, enable):
        self.__simWGCGEnabled = enable
        if self.__profile:
            self.__profile.resync(force=True)

    def simEnableClan(self, enable):
        settings = self.lobbyContext.getServerSettings()
        clanSettings = {
            'wgcgProfile': {
                'isEnabled': enable,
                'gateUrl': settings.wgcg.getGateUrl()
            }
        }
        settings.update(clanSettings)
        g_clientUpdateManager.update({'serverSettings': clanSettings})

    def simWGCGEnabled(self):
        return self.__simWGCGEnabled

    def init(self):
        self.__userCache = UserCache(self)
        self.__state = UndefinedState(self)
        self.__state.init()

    def fini(self):
        self.stop()
        self.__state.fini()
        self.__state = None
        self.__userCache = None
        return

    def start(self):
        self.__profile = MyClanAccountProfile(self)
        self.__clanCache = ClanCache(self.__profile.getDbID())
        self.__clanCache.onRead += self._onClanCacheRead
        self.__clanCache.read()
        self.invalidate()
        g_clientUpdateManager.addCallbacks({
            'stats.clanInfo':
            self.__onClanInfoChanged,
            'serverSettings.wgcg.isEnabled':
            self.__onServerSettingChanged,
            'serverSettings.clanProfile.isEnabled':
            self.__onClanEnableChanged
        })
        g_wgncEvents.onProxyDataItemShowByDefault += self._onProxyDataItemShowByDefault
        g_playerEvents.onClanMembersListChanged += self._onClanMembersListChanged

    def stop(self, logout=True):
        g_playerEvents.onClanMembersListChanged -= self._onClanMembersListChanged
        g_wgncEvents.onProxyDataItemShowByDefault -= self._onProxyDataItemShowByDefault
        g_clientUpdateManager.removeObjectCallbacks(self)
        if self.__clanCache is not None:
            self.__clanCache.onRead -= self._onClanCacheRead
            self.__profile.updateClanCache(self.__clanCache)
            self.__clanCache.write()
            self.__clanCache = None
        if self.__profile is not None:
            self.__profile.fini()
            self.__profile = None
        if logout:
            self.__state.logout()
        self.__cleanDossiers()
        return

    def invalidate(self):
        self.__state.update()

    def getClanDossier(self, clanDbID=None):
        if clanDbID in self.__cache:
            dossier = self.__cache[clanDbID]
        else:
            dossier = self.__cache[clanDbID] = _ClanDossier(
                clanDbID, self, isMy=clanDbID == self.__profile.getClanDbID())
        return dossier

    @async
    @process
    def login(self, callback):
        yield self.__state.loginAsync()
        callback(True)

    def resyncLogin(self, forceLogin=False):
        perms = self.__profile.getMyClanPermissions()
        if forceLogin or perms.canHandleClanInvites() and perms.canTrade(
        ) and perms.canExchangeMoney():
            self.__state.login()

    @async
    @process
    def sendRequest(self, ctx, callback=None, allowDelay=None):
        result = yield self.__state.sendRequest(ctx, allowDelay=allowDelay)
        if self.__profile is not None:
            self.__profile.processRequestResponse(ctx, result)
        if not result.isSuccess():
            _showError(result, ctx)
        callback(result)
        return

    def getStateID(self):
        return self.__state.getStateID()

    def isEnabled(self):
        settings = self.lobbyContext.getServerSettings()
        return settings.wgcg.isEnabled() if settings is not None else True

    def compareStates(self, state):
        return self.__state.compare(state)

    def isAvailable(self):
        return self.__state.isAvailable()

    def getWebRequester(self):
        return self.__state.getWebRequester()

    def getAccountProfile(self):
        return self.__profile

    def getLimits(self):
        return self.__state.getLimits(self.__profile)

    def getClanDbID(self):
        return self.__profile.getClanDbID() if self.__profile else None

    def getClanInfo(self):
        return {
            'id': self.__profile.getClanDbID(),
            'tag': self.__profile.getClanAbbrev(),
            'joined_at': self.__profile.getJoinedAt(),
            'name': self.__profile.getClanName(),
            'full_name': self.__profile.getClanFullName()
        }

    def changeState(self, state):
        self.__state = state

    def onStateUpdated(self):
        if self.__profile and self.__state.isLoggedOn():
            self.__profile.resync()

    def isLoggedOn(self):
        return self.__state.isLoggedOn()

    @async
    @process
    def getAccessTokenData(self, force, callback=None):
        accessToken = yield self.__state.getAccessTokenData(force)
        callback(accessToken)

    def updateClanCommonDataCache(self, cache):
        for item in cache or {}:
            self.__searchDataCache[item.getDbID()] = item

    def clearClanCommonDataCache(self):
        self.__searchDataCache = {}

    def getClanCommonData(self, clanDbID):
        return self.__searchDataCache.get(clanDbID, None)

    @async
    @process
    def requestUsers(self, dbIDs, callback):
        result = yield self.__userCache.requestUsers(dbIDs)
        callback(result)

    def _onProxyDataItemShowByDefault(self, notifID, item):
        if item.getType() in _CLAN_WGNC_NOTIFICATION_TYPES:
            if self.__profile is not None:
                self.__profile.processWgncNotification(notifID, item)
            self.notify('onWgncNotificationReceived', notifID, item)
        return

    def _onClanMembersListChanged(self):
        memberIDs = getattr(BigWorld.player(), 'clanMembers', {}).keys()
        if self.__profile is not None:
            self.__profile.processClanMembersListChange(memberIDs)
        self.notify('onMembersListChanged', memberIDs)
        return

    def _onClanCacheRead(self):
        if self.__profile is not None and self.__clanCache is not None:
            self.__profile.processClanCache(self.__clanCache)
        return

    def __onClanInfoChanged(self, _):
        self.__profile.resyncBwInfo()
        self.resyncLogin()

    def __onServerSettingChanged(self, *args):
        self.invalidate()

    def __onClanEnableChanged(self, *args):
        settings = self.lobbyContext.getServerSettings()
        self.notify('onClanEnableChanged', settings.clanProfile.isEnabled())

    def __cleanDossiers(self):
        for _, v in self.__cache.iteritems():
            v.fini()

        self.__cache.clear()
示例#2
0
class _ClanController(ClansListeners):

    def __init__(self):
        super(_ClanController, self).__init__()
        self.__cache = {}
        self.__searchDataCache = {}
        self.__state = None
        self.__profile = None
        self.__userCache = None
        self.__clanCache = None
        self.__simWGCGEnabled = True
        return

    def simEnableWGCG(self, enable):
        self.__simWGCGEnabled = enable
        if self.__profile:
            self.__profile.resync(force=True)

    def simEnableClan(self, enable):
        settings = g_lobbyContext.getServerSettings()
        clanSettings = {'clanProfile': {'isEnabled': enable,
                         'gateUrl': settings.clanProfile.getGateUrl()}}
        settings.update(clanSettings)
        g_clientUpdateManager.update({'serverSettings': clanSettings})

    def simWGCGEnabled(self):
        return self.__simWGCGEnabled

    def init(self):
        self.__userCache = UserCache(self)
        self.__state = ClanUndefinedState(self)
        self.__state.init()

    def fini(self):
        self.stop()
        self.__state.fini()
        self.__state = None
        self.__userCache = None
        self.__cleanDossiers()
        return

    def start(self):
        self.__profile = MyClanAccountProfile(self)
        self.__clanCache = ClanCache(self.__profile.getDbID())
        self.__clanCache.onRead += self._onClanCacheRead
        self.__clanCache.read()
        self.invalidate()
        g_clientUpdateManager.addCallbacks({'stats.clanInfo': self.__onClanInfoChanged,
         'serverSettings.clanProfile.isEnabled': self.__onServerSettingChanged})
        g_wgncEvents.onProxyDataItemShowByDefault += self._onProxyDataItemShowByDefault

    def stop(self):
        g_wgncEvents.onProxyDataItemShowByDefault -= self._onProxyDataItemShowByDefault
        g_clientUpdateManager.removeObjectCallbacks(self)
        if self.__clanCache is not None:
            self.__clanCache.onRead -= self._onClanCacheRead
            self.__profile.updateClanCache(self.__clanCache)
            self.__clanCache.write()
            self.__clanCache = None
        if self.__profile is not None:
            self.__profile.fini()
            self.__profile = None
        self.__state.logout()
        return

    def invalidate(self):
        self.__state.update()

    def getClanDossier(self, clanDbID = None, useCached = True):
        if useCached and clanDbID in self.__cache:
            dossier = self.__cache[clanDbID]
        else:
            dossier = self.__cache[clanDbID] = _ClanDossier(clanDbID, self, isMy=clanDbID == self.__profile.getClanDbID())
        return dossier

    def resyncLogin(self, forceLogin = False):
        perms = self.__profile.getMyClanPermissions()
        if forceLogin or perms.canHandleClanInvites() and perms.canTrade() and perms.canExchangeMoney():
            self.__state.login()

    @async
    @process
    def sendRequest(self, ctx, callback, allowDelay = None):
        result = yield self.__state.sendRequest(ctx, allowDelay=allowDelay)
        if self.__profile is not None:
            self.__profile.processRequestResponse(ctx, result)
        if not result.isSuccess():
            _showError(result, ctx)
        callback(result)
        return

    def getStateID(self):
        return self.__state.getStateID()

    def isEnabled(self):
        settings = g_lobbyContext.getServerSettings()
        if settings is not None:
            return settings.clanProfile.isEnabled()
        else:
            return True

    def isAvailable(self):
        return self.__state.isAvailable()

    def getWebRequester(self):
        return self.__state.getWebRequester()

    def getAccountProfile(self):
        return self.__profile

    def getLimits(self):
        return self.__state.getLimits(self.__profile)

    def getClanDbID(self):
        if self.__profile:
            return self.__profile.getClanDbID()
        else:
            return None

    def changeState(self, state):
        oldState = self.__state
        self.__state = state
        self.notify('onClanStateChanged', oldState.getStateID(), state.getStateID())

    def onStateUpdated(self):
        if self.__state.isLoggedOn():
            self.__profile.resync()

    def isLoggedOn(self):
        return self.__state.isLoggedOn()

    def updateClanCommonDataCache(self, cache):
        for item in cache or {}:
            self.__searchDataCache[item.getDbID()] = item

    def clearClanCommonDataCache(self):
        self.__searchDataCache = {}

    def getClanCommonData(self, clanDbID):
        return self.__searchDataCache.get(clanDbID, None)

    @async
    @process
    def requestUsers(self, dbIDs, callback):
        result = yield self.__userCache.requestUsers(dbIDs)
        callback(result)

    def _onProxyDataItemShowByDefault(self, notifID, item):
        """
        Method handles notifications received trough WGNC
        :param notifID:
        :param item: instance of gui.wgnc.proxy_data._ProxyDataItem
        """
        if item.getType() in _CLAN_WGNC_NOTOFICATION_TYPES:
            if self.__profile is not None:
                self.__profile.processWgncNotification(notifID, item)
            self.notify('onWgncNotificationReceived', notifID, item)
        return

    def _onClanCacheRead(self):
        if self.__profile is not None and self.__clanCache is not None:
            self.__profile.processClanCache(self.__clanCache)
        return

    def __onClanInfoChanged(self, _):
        self.__profile.resyncBwInfo()
        self.resyncLogin()

    def __onServerSettingChanged(self, *args):
        self.invalidate()

    def __cleanDossiers(self):
        for k, v in self.__cache.iteritems():
            v.fini()

        self.__cache.clear()

    def __repr__(self):
        return 'ClanCtrl(state = %s, profile = %s)' % (str(self.__state), self.__profile)
示例#3
0
class _ClanController(ClansListeners):
    def __init__(self):
        super(_ClanController, self).__init__()
        self.__cache = {}
        self.__searchDataCache = {}
        self.__state = None
        self.__profile = None
        self.__userCache = None
        self.__clanCache = None
        self.__simWGCGEnabled = True
        return

    def simEnableWGCG(self, enable):
        self.__simWGCGEnabled = enable
        if self.__profile:
            self.__profile.resync(force=True)

    def simEnableClan(self, enable):
        settings = g_lobbyContext.getServerSettings()
        clanSettings = {
            'clanProfile': {
                'isEnabled': enable,
                'gateUrl': settings.clanProfile.getGateUrl()
            }
        }
        settings.update(clanSettings)
        g_clientUpdateManager.update({'serverSettings': clanSettings})

    def simWGCGEnabled(self):
        return self.__simWGCGEnabled

    def init(self):
        self.__userCache = UserCache(self)
        self.__state = ClanUndefinedState(self)
        self.__state.init()

    def fini(self):
        self.stop()
        self.__state.fini()
        self.__state = None
        self.__userCache = None
        return

    def start(self):
        self.__profile = MyClanAccountProfile(self)
        self.__clanCache = ClanCache(self.__profile.getDbID())
        self.__clanCache.onRead += self._onClanCacheRead
        self.__clanCache.read()
        self.invalidate()
        g_clientUpdateManager.addCallbacks({
            'stats.clanInfo':
            self.__onClanInfoChanged,
            'serverSettings.clanProfile.isEnabled':
            self.__onServerSettingChanged
        })
        g_wgncEvents.onProxyDataItemShowByDefault += self._onProxyDataItemShowByDefault
        g_playerEvents.onClanMembersListChanged += self._onClanMembersListChanged

    def stop(self):
        g_playerEvents.onClanMembersListChanged -= self._onClanMembersListChanged
        g_wgncEvents.onProxyDataItemShowByDefault -= self._onProxyDataItemShowByDefault
        g_clientUpdateManager.removeObjectCallbacks(self)
        if self.__clanCache is not None:
            self.__clanCache.onRead -= self._onClanCacheRead
            self.__profile.updateClanCache(self.__clanCache)
            self.__clanCache.write()
            self.__clanCache = None
        if self.__profile is not None:
            self.__profile.fini()
            self.__profile = None
        self.__state.logout()
        self.__cleanDossiers()
        return

    def invalidate(self):
        self.__state.update()

    def getClanDossier(self, clanDbID=None):
        if clanDbID in self.__cache:
            dossier = self.__cache[clanDbID]
        else:
            dossier = self.__cache[clanDbID] = _ClanDossier(
                clanDbID, self, isMy=clanDbID == self.__profile.getClanDbID())
        return dossier

    def resyncLogin(self, forceLogin=False):
        perms = self.__profile.getMyClanPermissions()
        if forceLogin or perms.canHandleClanInvites() and perms.canTrade(
        ) and perms.canExchangeMoney():
            self.__state.login()

    @async
    @process
    def sendRequest(self, ctx, callback, allowDelay=None):
        result = yield self.__state.sendRequest(ctx, allowDelay=allowDelay)
        if self.__profile is not None:
            self.__profile.processRequestResponse(ctx, result)
        if not result.isSuccess():
            _showError(result, ctx)
        callback(result)
        return

    def getStateID(self):
        return self.__state.getStateID()

    def isEnabled(self):
        settings = g_lobbyContext.getServerSettings()
        if settings is not None:
            return settings.clanProfile.isEnabled()
        else:
            return True

    def isAvailable(self):
        return self.__state.isAvailable()

    def getWebRequester(self):
        return self.__state.getWebRequester()

    def getAccountProfile(self):
        return self.__profile

    def getLimits(self):
        return self.__state.getLimits(self.__profile)

    def getClanDbID(self):
        if self.__profile:
            return self.__profile.getClanDbID()
        else:
            return None

    def changeState(self, state):
        oldState = self.__state
        self.__state = state
        self.notify('onClanStateChanged', oldState.getStateID(),
                    state.getStateID())

    def onStateUpdated(self):
        if self.__state.isLoggedOn():
            self.__profile.resync()

    def isLoggedOn(self):
        return self.__state.isLoggedOn()

    def updateClanCommonDataCache(self, cache):
        for item in cache or {}:
            self.__searchDataCache[item.getDbID()] = item

    def clearClanCommonDataCache(self):
        self.__searchDataCache = {}

    def getClanCommonData(self, clanDbID):
        return self.__searchDataCache.get(clanDbID, None)

    @async
    @process
    def requestUsers(self, dbIDs, callback):
        result = yield self.__userCache.requestUsers(dbIDs)
        callback(result)

    def _onProxyDataItemShowByDefault(self, notifID, item):
        """
        Method handles notifications received trough WGNC
        :param notifID:
        :param item: instance of gui.wgnc.proxy_data._ProxyDataItem
        """
        if item.getType() in _CLAN_WGNC_NOTOFICATION_TYPES:
            if self.__profile is not None:
                self.__profile.processWgncNotification(notifID, item)
            self.notify('onWgncNotificationReceived', notifID, item)
        return

    def _onClanMembersListChanged(self):
        memberIDs = getattr(BigWorld.player(), 'clanMembers', {}).keys()
        if self.__profile is not None:
            self.__profile.processClanMembersListChange(memberIDs)
        self.notify('onMembersListChanged', memberIDs)
        return

    def _onClanCacheRead(self):
        if self.__profile is not None and self.__clanCache is not None:
            self.__profile.processClanCache(self.__clanCache)
        return

    def __onClanInfoChanged(self, _):
        self.__profile.resyncBwInfo()
        self.resyncLogin()

    def __onServerSettingChanged(self, *args):
        self.invalidate()

    def __cleanDossiers(self):
        for k, v in self.__cache.iteritems():
            v.fini()

        self.__cache.clear()

    def __repr__(self):
        return 'ClanCtrl(state = %s, profile = %s)' % (str(
            self.__state), self.__profile)