def run(self): alliance_query = alliances.AllianceQuery() all_alliances = alliance_query.getAll() alliances_names = [item["shortname"] for item in all_alliances] users_with_alliance = users.UserQuery().find_name_by_alliances( alliances_names) query = "SELECT id FROM room_imports WHERE status LIKE 'complete' ORDER BY started_at DESC" result = db.find_one(query) self.room_import_id = result[0] self.conn = get_conn() self.start() print(self.id) for alliance in all_alliances: users_with_alliance = self.find_name_by_alliances(alliances_names) members = [ user['name'] for user in users_with_alliance if user['alliance'] == alliance['shortname'] ] filtered_members = [ user for user in members if self.get_player_room_count(user) > 0 ] # Not enough members. if len(filtered_members) < 2: continue # Not enough rooms if self.get_room_count(alliance['shortname']) < 2: continue rcl = self.getAllianceRCL(alliance['shortname']) combined_gcl = sum( self.getUserGCL(user) for user in filtered_members) control = sum( getUserControlPoints(user) for user in filtered_members) alliance_gcl = self.convertGcl(control) combined_power = sum( self.getUserPowerLevel(user) for user in filtered_members) power = sum(getUserPowerPoints(user) for user in filtered_members) alliance_power = self.convertPowerToLevel(power) spawns = self.getAllianceSpawns(alliance['shortname']) print('%s- %s, %s, %s, %s, %s, %s, %s' % (alliance['shortname'], combined_gcl, alliance_gcl, rcl, spawns, len(filtered_members), alliance_power, combined_power)) self.update(alliance['shortname'], alliance_gcl, combined_gcl, rcl, spawns, len(filtered_members), alliance_power, combined_power) self.finish() self.conn.commit()
def import_alliances(): click.echo("Start to import alliances from http://www.leagueofautomatednations.com/alliances.js") import requests as r import screeps_loan.models.alliances as alliances_model import screeps_loan.models.users as users_model import screeps_loan.auth_user import screeps_loan.services.users as users_service alliance_query = alliances_model.AllianceQuery() users_query = users_model.UserQuery() screeps = get_client() auth_user = screeps_loan.auth_user.AuthPlayer(screeps) resp = r.get('http://www.leagueofautomatednations.com/alliances.js') data = json.loads(resp.text) for shortname, info in data.items(): print(shortname) members = info['members'] fullname = info['name'] color = None if 'color' in info: color = info['color'] slack = None if 'slack' in info: slack = info['slack'] alliance = alliance_query.find_by_shortname(shortname) if (alliance is None): alliance_query.insert_alliance(shortname, fullname, color, slack) alliance = shortname existing_member = [i['name'] for i in users_query.find_name_by_alliances([shortname])] new_members = [name for name in members if name not in existing_member] for member in new_members: print(member) id = users_service.player_id_from_api(member) users_query.update_alliance_by_screeps_id(id, shortname)
def alliance_listing_json(): import screeps_loan.models.alliances as alliances import screeps_loan.models.users as users alliance_query = alliances.AllianceQuery() all_alliances = alliance_query.getAll() alliance_users = alliance_query.getMembershipData() alliance_user_data = {} for users_row in alliance_users: alliance_user_data[users_row["shortname"]] = users_row ranking_types = rankings_model.get_rankings_list() ranking_data = {} for ranking_type in ranking_types: ranking_data[ranking_type] = rankings_model.get_rankings_by_import_and_type(ranking_type) alliances_aux = {} for alliance in all_alliances: if not alliance["shortname"] in alliance_user_data: continue if alliance_user_data[alliance["shortname"]]["active_member_count"] < 2: continue if alliance_user_data[alliance["shortname"]]["room_count"] < 2: continue alliance["members"] = alliance_user_data[alliance["shortname"]]["members"] alliance['name'] = alliance['fullname'] alliance['abbreviation'] = alliance['shortname'] alliance.pop('fullname', None) alliance.pop('shortname', None) for ranking_type in ranking_types: if alliance['abbreviation'] in ranking_data[ranking_type]: data = ranking_data[ranking_type][alliance['abbreviation']] alliance[ranking_type + '_rank'] = data alliances_aux[alliance['abbreviation']] = alliance return json.dumps(alliances_aux)
def alliance_listing(): import screeps_loan.models.alliances as alliances import screeps_loan.models.users as users alliance_query = alliances.AllianceQuery() all_alliances = alliance_query.getAll() alliances_name = [item["shortname"] for item in all_alliances] users_with_alliance = users.UserQuery().find_name_by_alliances(alliances_name) display_alliances = [] for alliance in all_alliances: if not alliance['shortname']: continue if alliances_model.get_room_count(alliance['shortname']) < 2: continue alliance['users'] = [user for user in users_with_alliance if user['alliance'] == alliance['shortname']] if alliance['users']: display_alliances.append(alliance) display_alliances = sorted(display_alliances, key=lambda k: k['fullname']) return render_template("alliance_listing.html", alliances = display_alliances)
def export_to_segments(): click.echo("Exporting Alliance and Bot Data To Segment") r = requests.get( 'http://www.leagueofautomatednations.com/vk/bots/league.json') if r.status_code == requests.codes.ok: clone_data = r.text else: clone_data = False import screeps_loan.models.alliances as alliances import screeps_loan.models.users as users alliance_query = alliances.AllianceQuery() all_alliances = alliance_query.getAll() alliances_name = [item["shortname"] for item in all_alliances] users_with_alliance = users.UserQuery().find_name_by_alliances( alliances_name) alliances_aux = {} for alliance in all_alliances: members = [ user['name'] for user in users_with_alliance if user['alliance'] == alliance['shortname'] ] if len(members) < 2: continue alliances_aux[alliance['shortname']] = members alliance_json = json.dumps(alliances_aux) screeps = get_client() shards = screeps.get_shards() for shard in shards: screeps.set_segment(alliance_segment, alliance_json, shard) if clone_data: screeps.set_segment(clone_segment, clone_data, shard)
from flask import session from screeps_loan import app import screeps_loan.models.alliances as alliances_model from screeps_loan.screeps_client import get_client from screeps_loan.services.cache import cache alliance_query = alliances_model.AllianceQuery() app.jinja_env.globals.update( get_name_from_shortname=alliance_query.find_by_shortname) import screeps_loan.models.users as users app.jinja_env.globals.update(get_name_from_user_id=users.user_name_from_db_id) import screeps_loan.models.invites as invites def user_has_invites(): my_invites = invites.get_invites_by_user(session['my_id']) return len(my_invites) > 0 app.jinja_env.globals.update(has_invites=user_has_invites) @cache.cache() def get_shards(): api = get_client() shards = api.get_shards() if not shards: shards = ['shard0'] return shards