class MentionsNotifications: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() p_key = core.p_parser_key p_secret = core.p_parser_secret def __init__(self): print 'Loaded ' + str(len(self.core.loaded_data['bots'])) + ' bots:' for bot in self.core.loaded_data['bots']: print '> @' + bot['name'] print self.setBot(0) def setBot(self, bot_id): self.bot_id = bot_id self.bot = self.core.loaded_data['bots'][bot_id] def postNotification(self, notification): try: auth = tweepy.OAuthHandler(self.p_key, self.p_secret) auth.set_access_token(self.bot['token1'].decode('utf-8'), self.bot['token2'].decode('utf-8')) api = tweepy.API(auth) api.update_status(notification) except Exception, e: print 'Error post', notification['name'], e
class artistsRatingsCounter: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() def countRatings(self): starttime = time.time() authors = {} likes = self.mongo.getu('items_likes', {'author_id': {'$exists': True}}) for like in likes: author_str_id = str(like['author_id']) if not author_str_id in authors: authors.update({author_str_id: 0}) authors[author_str_id] += len(like['people']) self.mongo.cleanUp('authors_likes') for author_str_id in authors: author_info = self.mongo.find('players', {'_id': ObjectId(author_str_id)}, {'_id': 1, 'name': 1, 'avatar': 1}) if author_info: self.mongo.insert('authors_likes', { 'author_id': author_info['_id'], 'author_name': author_info['name'], 'author_avatar': author_info['avatar'], 'likes': authors[author_str_id] }) message = 'Artist ratings was counted by '+str(time.time()-starttime)+' seconds' print message
class FVFCreator: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() log = logger.logger('logs/system_events.log') def __init__(self): pass def checkAndCreateFvF(self, FORCE=False): need_to_create_new = True last_fvf = self.mongo.getu('events', search={'type': 'fvf'}, sort={'create_date': -1}, limit=1) if last_fvf: if last_fvf[0][ 'create_date'] + self.balance.FVF_CREATE_TIME > time.time( ): need_to_create_new = False if need_to_create_new or self.core.debug['always_create_fvf'] or FORCE: if last_fvf: not_side = last_fvf[0]['sides'][random.randint(0, 1)] sides = [0, 1, 2] del sides[not_side] side_1, side_2 = sides else: side_1 = random.sample([0, 1, 2], 1)[0] side_2 = Set([0, 1, 2]) side_2.remove(side_1) side_2 = random.sample(side_2, 1)[0] self.createFvF(side_1, side_2) def createFvF(self, side_1, side_2): start_time = time.time() + random.randint( self.balance.FVF_START_TIME_MIN, self.balance.FVF_START_TIME_MAX) desc = '' event = { 'type': 'fvf', 'guild_run': False, 'desc': desc, 'activity': [], 'start_date': start_time, 'create_date': time.time(), 'finish_date': start_time + self.balance.LENGTH_OF_FVF, 'status': 'active', 'promoted_hashtag': [self.balance.FVF_HASHES[side_1], self.balance.FVF_HASHES[side_2]], 'sides': [side_1, side_2], 'sides_names': [self.balance.faction[side_1], self.balance.faction[side_2]], 'people': [] } self.mongo.insert('events', event) message = 'FvF event was created: ' + event['sides_names'][ 0] + ' vs. ' + event['sides_names'][0] self.log.write(message) print message
#!/usr/bin/python # -*- coding: utf-8 -*- # This is automated system to create new faction vs faction events # author: Alex Shteinikov import __init__ import settings import time from sets import Set import random tweenk_core = settings.core() tweenk_balance = settings.balance() import db import logger class FVFCreator: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() log = logger.logger('logs/system_events.log') def __init__(self): pass def checkAndCreateFvF(self, FORCE=False):
#!/usr/bin/python # -*- coding: utf-8 -*- # This is artists rating counter # author: Alex Shteinikov import __init__ import settings tweenk_core = settings.core() tweenk_balance = settings.balance() import db import time from bson import ObjectId class artistsRatingsCounter: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() def countRatings(self): starttime = time.time() authors = {} likes = self.mongo.getu('items_likes', {'author_id': {'$exists': True}}) for like in likes:
class serviceCatcher(): core_settings = settings.core() data = settings.balance() log = logger.logger() LOG_NAME = os.path.abspath( os.path.split(inspect.getfile( inspect.currentframe()))[0]) + '/logs/newparser.log' LOCK_NAME = os.path.abspath( os.path.split(inspect.getfile( inspect.currentframe()))[0]) + '/__lock__' timelines = {} p_key = core_settings.p_parser_key p_secret = core_settings.p_parser_secret mongo = db.mongoAdapter() queue = myQueue() local_queue = myQueue() missing_players = [] auths = {} apis = {} threads = {} threads_number = 0 request_time = 0 parse_time = 0 group_number = 0 graph_tweets = {} def __init__(self): self.parser = tweet_parser.tweetParser(debug=False) def _createLock(self): fp = open(self.LOCK_NAME, 'w') fp.write('YEAH') fp.close() def loadDataToQueue(self, array=False): self.queue = myQueue() self.queue.body = [] if not array: buff_players = self.mongo.getu( 'players', search={ 'banned': { '$exists': False }, 'last_login': { '$gte': time.time() - self.core_settings.MAX_TIME_TO_SLEEP } }, fields={ 'user_id': 1, 'name': 1, 'token1': 1, 'token2': 1 }, sort={'last_login': -1}) else: buff_players = array self.queue.add(buff_players) def startThread(self, name): self.threads.update({name: ThreadWithExc(self, name)}) self.threads[name].start() def startPostThread(self, name): self.threads.update({name: ThreadPostNotification(self, name)}) self.threads[name].start() def getTimelineFromTwitter(self, user): try: self.auths[user['user_id']] = tweepy.OAuthHandler( self.p_key, self.p_secret) self.auths[user['user_id']].set_access_token( user['token1'], user['token2']) self.apis[user['user_id']] = tweepy.API( self.auths[user['user_id']]) timeline = self.apis[user['user_id']].user_timeline( include_entities=1, include_rts=True) except Exception, e: timeline = False print 'Error', user['name'], e try: del self.auths[user['user_id']] del self.apis[user['user_id']] except Exception: pass return timeline
class WeeklyStats: mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() def __init__(self): pass def updateStats(self): collection = 'weekly_stats' if not self.mongo.collectionExist(collection): self.mongo.createCollection(collection) stats = self.mongo.getu('statistics', {'user_id': 12164662}) prev_weekly = self.mongo.getu(collection) if not prev_weekly: prev_weekly = [] str_prev_weekly = {} for stat in prev_weekly: str_prev_weekly.update({str(stat['user_id']): stat['stats']}) to_update = [] for stat in stats: str_id = str(stat['user_id']) if str_id in str_prev_weekly: diff = { 'monsters': stat['stats']['kills_monster'] - str_prev_weekly[str_id]['monsters'], 'players': stat['stats']['pvp_kills_player'] - str_prev_weekly[str_id]['players'], 'lvl': stat['stats']['lvl'] - str_prev_weekly[str_id]['lvl'] } record = {'user_id': stat['user_id'], 'stats': diff} if diff['monsters'] + diff['players'] + diff['lvl'] > 0: to_update.append(record) normal = { 'monsters': stat['stats']['kills_monster'], 'players': stat['stats']['pvp_kills_player'], 'lvl': stat['stats']['lvl'] } self.mongo.update(collection, {'user_id': stat['user_id']}, {'stats': normal}) else: diff = { 'monsters': stat['stats']['kills_monster'], 'players': stat['stats']['pvp_kills_player'], 'lvl': stat['stats']['lvl'] } record = {'user_id': stat['user_id'], 'stats': diff} if diff['monsters'] + diff['players'] + diff['lvl'] > 1: to_update.append(record) self.mongo.insert(collection, record) for item in to_update: print item
class ratingsCounter: K1 = 0.02 K2 = 1.5 K_kill = 100 RD = 25 mongo = db.mongoAdapter() balance = settings.balance() core = settings.core() log = logger.logger('logs/system_events.log') def __init__(self): pass def countUserRatings(self): def countTheUserRating(sort_field, result_field): self.players.sort(key=lambda x: x[sort_field]) self.players.reverse() place = 1 for player in self.players: if 'banned' in player and player['banned']: player.update({result_field: 100500}) else: player.update({result_field: place}) place += 1 starttime = time.time() for player in self.players: # Умножаем уровень на 100 млн и прибавляем опыт # чтобы два раза не сравнивать (по уровню и по опыту) # а учитывать общее значение player.update( {'rating': player['lvl'] * 100000000 + player['exp']}) # Если нет информации о том сколько твитов игрока за день получено # то считаем 0 if not 'today_parsed_tweets' in player: player.update({'today_parsed_tweets': 0}) # Если нет информации о том сколько pvp points игрок за день набрал # то считаем что все (что вчера у него было 0 очков) if not 'prev_day_pvp' in player: player.update({'pvp_per_day': player['pvp_score']}) else: player.update({ 'pvp_per_day': player['pvp_score'] - player['prev_day_pvp'] }) # Считаем рейтинг игрока по метрикам global_metric = 0 if player['lvl'] == 1: global_metric = 0 else: if 'metrics' in player: if 'monster_kill' in player['metrics']: for hour in player['metrics']['monster_kill']: global_metric += ( self.balance.max_lvl - player['metrics']['monster_kill'][hour]['lvl'] * self.K2) * self.K1 * self.K_kill * player[ 'metrics']['monster_kill'][hour]['value'] else: global_metric = 0 try: if player['ratings']['trending_position'] <= 10: if player['ratings']['trending_position'] <= 3: global_metric = global_metric * 0.7 elif player['ratings']['trending_position'] <= 7: global_metric = global_metric * 0.8 else: global_metric = global_metric * 0.9 except Exception: pass global_metric = global_metric + global_metric / 100 * random.randint( 0, self.RD) player.update({'trending_score': global_metric}) # Считаем место игрока в глобальном рейтинге игроков по опыту, # Если уровень одинаковый, то выше в рейтинге тот, у кого больше опыта countTheUserRating('rating', 'rating_by_exp') # ... в общем рейтинге игроков по pvp points countTheUserRating('pvp_score', 'rating_by_pvp') # ... в общем рейтинге игроков по achv_points countTheUserRating('achv_points', 'rating_by_achv_points') # ... trending players countTheUserRating('trending_score', 'trending_position') for player in self.players: record = { 'rating_by_exp': player['rating_by_exp'], 'rating_by_pvp': player['rating_by_pvp'], 'rating_by_achv_points': player['rating_by_achv_points'], 'trending_position': player['trending_position'], 'trending_score': player['trending_score'] } self.mongo.update('players', {'_id': player['_id']}, {'ratings': record}) message = 'Player ratings was counted by ' + str( time.time() - starttime) + ' seconds' self.log.write(message) print message def countGuildRatings(self): def countGuildRating(field): self.guilds.sort(key=lambda x: x[field]) self.guilds.reverse() place = 1 for guild in self.guilds: guild.update({field: place}) place += 1 starttime = time.time() for guild in self.guilds: guild.update({ 'buff_global_metric': 0, 'buff_rating': 0, 'buff_pvp': 0, 'pvp_score': 0, }) query = [] for id in guild['people']: query.append({'_id': id}) members = self.mongo.getu('players', search={'$or': query}, fields={ 'lvl': 1, 'pvp_score': 1, 'ratings': 1 }) for player in members: try: guild['buff_global_metric'] += player['ratings'][ 'trending_score'] guild['buff_rating'] += player['lvl'] guild['buff_pvp'] += player['pvp_score'] except Exception: pass if len(members) < 5: guild['buff_global_metric'] = 0 guild['pvp_score'] = int(guild['buff_pvp']) # Считает место гильдии в глобальном рейтинге гильдии # по сумме уровня членов гильдии countGuildRating('buff_rating') # ... sum trending members countGuildRating('buff_global_metric') # .. по общему pvp_score участников countGuildRating('buff_pvp') for guild in self.guilds: record = { 'rating_place_members_lvl': guild['buff_rating'], 'rating_place_members_pvp': guild['buff_pvp'], 'trending_position': guild['buff_global_metric'], 'pvp_score': guild['pvp_score'] } self.mongo.update('guilds', {'_id': guild['_id']}, {'ratings': record}) message = 'Guilds ratings was counted by ' + str( time.time() - starttime) + ' seconds' self.log.write(message) print message def countAll(self): self.players = self.mongo.getu('players', {'banned': { '$exists': False }}, { '_id': 1, 'lvl': 1, 'exp': 1, 'achv_points': 1, 'pvp_score': 1, 'metrics': 1, 'ratings': 1 }) self.banned_players = self.mongo.getu('players', {'banned': { '$exists': True }}, { '_id': 1, 'lvl': 1, 'exp': 1, 'achv_points': 1, 'pvp_score': 1, 'metrics': 1 }) self.guilds = self.mongo.getu('guilds', {}, { 'id': 1, 'name': 1, 'people': 1 }) self.countUserRatings() self.countGuildRatings() for player in self.banned_players: record = { 'rating_by_exp': 100500, 'rating_by_pvp': 100500, 'rating_by_achv_points': 100500, 'trending_position': 100500, 'trending_score': 0 } self.mongo.update('players', {'_id': player['_id']}, record) self.exit() def countGameStatistics(self): count_players = [] for index in range(0, len(self.balance.faction)): query = {'faction': index, '$or': [{'race': 0}, {'race': 1}]} count_players.append(self.mongo.count('players', query)) count_avg_level = [0, 0, 0] players = self.mongo.getu('players', {}, {'lvl': 1, 'faction': 1}) for player in players: count_avg_level[player['faction']] += player['lvl'] for index in range(0, len(self.balance.faction)): try: count_avg_level[index] = float( int( float(count_avg_level[index]) / count_players[index] * 10)) / 10 except Exception: count_avg_level[index] = 0.0 current_time = time.localtime() hashkey = str(current_time.tm_year) + str(current_time.tm_yday) lastday_stat = self.mongo.find('game_statistics', {'type': 'lastday_avg_level'}) if not lastday_stat or time.localtime( ).tm_hour > 20 and not lastday_stat['hashkey'] == hashkey: self.mongo.update('game_statistics', {'type': 'lastday_avg_level'}, { 'type': 'lastday_avg_level', 'data': count_avg_level, 'hashkey': hashkey }, True) self.mongo.update('game_statistics', {'type': 'lastday_count'}, { 'type': 'lastday_count', 'data': count_players, 'hashkey': hashkey }, True) self.mongo.update('game_statistics', {'type': 'players_count'}, { 'type': 'players_count', 'data': count_players }, True) self.mongo.update('game_statistics', {'type': 'players_avg_level'}, { 'type': 'players_avg_level', 'data': count_avg_level }, True) def exit(self): self.log.closeFile()
def __init__(self, connection): self.mongo = connection self.balance = settings.balance()