def init(self): self.__error = TOKEN_ERRORS.OK self.__userToken = '' self.__accountDBID = self.__getAccountDBID() if self.__accountDBID: self.__saveAccountDBID() self.__tokensBase = self.__getTokensBase() #V1. Сheck for a new token on the server stats = loadJsonUrl(XVM_GETTOKEN.format(TOKEN='-', ID=self.__accountDBID)) if stats is None: self.__error = TOKEN_ERRORS.NOT_CONNECTION return elif stats: self.__userToken = stats.get('token','') if self.__userToken: self.__saveLocalToken(stats) #V2. Search and check the token from the cache if not self.__userToken: self.__userToken = self.__getLocalToken() if self.__userToken: stats = loadJsonUrl(XVM_GETTOKEN.format(TOKEN=self.__userToken, ID=self.__accountDBID)) if stats is None: self.__userToken = '' self.__error = TOKEN_ERRORS.NOT_CONNECTION return elif not stats or stats.get('status', None) in (None, 'inactive'): if self.__accountDBID in self.__tokensBase: self.__tokensBase.pop(self.__accountDBID) self.__userToken = '' #V3. Search and check the token in the local database for use without activation on the website if not self.__userToken: self.__userToken = self.__tokensBase.get(self.__accountDBID, '') if self.__userToken: stats = loadJsonUrl(XVM_GETTOKEN.format(TOKEN=self.__userToken, ID=self.__accountDBID)) if stats is None: self.__userToken = '' self.__error = TOKEN_ERRORS.NOT_CONNECTION return elif not stats or stats.get('status', None) in (None, 'inactive'): self.__tokensBase.pop(self.__accountDBID) self.__userToken = '' self.__error = TOKEN_ERRORS.NEED_ACTIVATION else: self.__saveLocalToken(stats) else: self.__error = TOKEN_ERRORS.NEED_ACTIVATION #Save the token in the local database if self.__error == TOKEN_ERRORS.OK and self.__userToken: self.__tokensBase[self.__accountDBID] = self.__userToken self.__saveTokensBase() else: self.__error = TOKEN_ERRORS.NEED_LOGIN
def __sendQuery(self, query, players, timeout): answer = None while not answer and timeout >= 0: answer = loadJsonUrl(query) if not answer: timeout -= self.__timeDelay sleep(self.__timeDelay) if answer: if 'v' in answer and answer['v']: vehicle = answer['v'] for id in vehicle: vehicle[id]['id'] = id players['players'].append(answer)
def __sendTanksQuery(self, query, stats, timeout): answer = None while not answer and timeout >= 0: answer = loadJsonUrl(query) if not answer: timeout -= self.__timeDelay sleep(self.__timeDelay) if answer: meta = answer.get('meta', {}) if meta and meta['count'] > 0: accountDBID = answer['data'].keys()[0] for statistics in answer['data'][accountDBID]: if statistics and 'all' in statistics: statistics.update(statistics.pop('all')) stats['vehicles'][statistics['tank_id']] = statistics
#Sending typical requests to the XVM-server class _XVMConsole(object): def __init__(self): self.OnAsyncReports = Event() self.__timeDelay = 0.5 def __prepareRequest(self, async, url, onAsyncReport=None): if async: thread = threading.Thread(target=self.__sendRequest, args=[async, url, onAsyncReport]) thread.setDaemon(True) thread.start() else: return self.__sendRequest(async, url, None) def __sendRequest(self, async, url, onAsyncReport): answer = loadJsonUrl(url) if async: if onAsyncReport: onAsyncReport(answer) else: if len(self.OnAsyncReports._delegates) > 1: for delegate in self.OnAsyncReports._delegates[0:-1]: delegate(deepcopy(answer)) self.OnAsyncReports._delegates[-1](answer) else: self.OnAsyncReports(answer) return answer def getVersion(self): if g_UserToken.accountDBID and g_UserToken.userToken: return self.__prepareRequest(False, XVM_GETVERSION.format(TOKEN=g_UserToken.userToken, ID=g_UserToken.accountDBID))
#Sending typical requests to the WG-server class _WGConsole(object): def __init__(self): self.OnAsyncReports = Event() self.__timeDelay = 0.5 def __prepareRequest(self, async, url, onAsyncReport=None): if async: thread = threading.Thread(target=self.__sendRequest, args=[async, url, onAsyncReport]) thread.setDaemon(True) thread.start() else: return self.__sendRequest(async, url, None) def __sendRequest(self, async, url, onAsyncReport): answer = loadJsonUrl(url) if answer: meta = answer.get('meta', {}) answer = answer['data'] if not meta or meta['count'] > 0 else {} if async: if onAsyncReport: onAsyncReport(answer) else: if len(self.OnAsyncReports._delegates) > 1: for delegate in self.OnAsyncReports._delegates[0:-1]: delegate(deepcopy(answer)) self.OnAsyncReports._delegates[-1](answer) else: self.OnAsyncReports(answer) return answer