def decode_party_id(party_id): """ function to handle 'unknown' party_ids. The search endpoint could also be used but there are only a few party types """ if party_id <= 2: return id_filter = {'id': party_id} result = db.entities.find_one(id_filter) if result is not None: return result op = esiapp.op['get_characters_character_id']( character_id=party_id ) result = esiclient.request(op) if result.status == 200: return user_update(party_id, result) op = esiapp.op['get_corporations_corporation_id']( corporation_id=party_id ) result = esiclient.request(op) if result.status == 200: return corp_update(party_id, result) op = esiapp.op['get_alliances_alliance_id']( alliance_id=party_id ) result = esiclient.request(op) if result.status == 200: return alliance_update(party_id, result) # sometimes the party_id is a '''''''special''''''' value, so most of the time this is no big deal (but could mean error) logger.info('No character/corp/alliance found for: ' + str(party_id))
def user_update(character_id, public_data=None): """ adds or updates a DB entry for a user (character) and returns it """ data_to_update = {} data_to_update['id'] = character_id data_to_update['type'] = 'character' data_to_remove = None if not public_data: op = esiapp.op['get_characters_character_id']( character_id=character_id ) public_data = esiclient.request(op) if public_data.status != 200: logger.error('status: ' + str(public_data.status) + ' error with getting public data: ' + str(public_data.data)) logger.error('headers: ' + str(public_data.header)) logger.error('character with issue: ' + str(character_id)) return data_to_update['name'] = public_data.data['name'] data_to_update['birthday'] = public_data.data['birthday'].v.replace(tzinfo=timezone.utc).strftime("%Y-%m-%d %X") data_to_update['corporation_id'] = public_data.data['corporation_id'] if 'alliance_id' in public_data.data: data_to_update['alliance_id'] = public_data.data['alliance_id'] else: data_to_remove = {'alliance_id': 1} character_filter = {'id': character_id} update = {"$set": data_to_update} if data_to_remove: update['$unset'] = data_to_remove new_user_doc = db.entities.find_one_and_update(character_filter, update, upsert=True, return_document=ReturnDocument.AFTER) decode_party_id(data_to_update['corporation_id']) if 'alliance_id' in public_data.data: decode_party_id(data_to_update['alliance_id']) return new_user_doc
def get_fleet_wings(current_user): update_token(current_user) op = esiapp.op['get_fleets_fleet_id_wings'](fleet_id=current_user.fleet_id) fleet = esiclient.request(op) if fleet.status >= 400 and fleet.status < 500: error_string = fleet.data['error'] if fleet.data else str(fleet.status) logging.error('error getting fleet wings for: %s', current_user.get_id()) logging.error('error is: %s', error_string) if fleet.status == 404: if error_string == "Not found": raise EsiError(error_string) raise EsiException('not fleet boss') else: raise EsiError(error_string) elif fleet.status >= 500: error_string = fleet.data['error'] if fleet.data else str(fleet.status) logging.error('error getting fleet wings for: %s', current_user.get_id()) logging.error('error is: %s', error_string) raise EsiError(error_string) fleet.data.sort(key=lambda x: x['id']) for wing in fleet.data: wing['squads'].sort(key=lambda x: x['id']) return fleet.data
def decode_ship_id(_id): id_filter = {'id': _id} result = mongo.db.entities.find_one(id_filter) if result is not None: return result['name'] op = esiapp.op['get_universe_types_type_id'](type_id=_id) request = esiclient.request(op) esi_error_check_basic(request, 'ship data', str(_id)) add_db_entity(_id, request.data['name']) return request.data['name']
def decode_character_id(_id): id_filter = {'id': _id} result = mongo.db.entities.find_one(id_filter) if result is not None: return result['name'] op = esiapp.op['get_characters_character_id'](character_id=_id) request = esiclient.request(op) esi_error_check_basic(request, 'public character data', str(_id)) add_db_entity(_id, request.data['name']) return request.data['name']
def get_fleet_members(current_user, fleet_doc): update_token(current_user) op = esiapp.op['get_fleets_fleet_id_members']( fleet_id=current_user.fleet_id) fleet = esiclient.request(op) if fleet.status >= 400 and fleet.status < 500: error_string = fleet.data['error'] if fleet.data else str(fleet.status) logging.error('error getting fleet members for: %s', current_user.get_id()) logging.error('error is: %s', error_string) if fleet.status == 404: if error_string == "Not found": raise EsiError(error_string) raise EsiException('not fleet boss') else: raise EsiError(error_string) elif fleet.status >= 500: error_string = fleet.data['error'] if fleet.data else str(fleet.status) logging.error('error getting fleet members for: %s', current_user.get_id()) logging.error('error is: %s', error_string) raise EsiError(error_string) new_members = [member['character_id'] for member in fleet.data] data_to_update = {} # audit conncted sockets to ensure they are still in fleet for member in fleet_doc.get('connected_clients', []): if member not in new_members: fleet_doc['connected_clients'].remove(member) data_to_update['connected_clients'] = fleet_doc.get( 'connected_clients', []) for member in fleet_doc.get('connected_webapps', []): if member not in new_members: fleet_doc['connected_webapps'].remove(member) emit_to_char('error', 'You are no longer in the fleet', char_id=member) data_to_update['connected_webapps'] = fleet_doc.get( 'connected_webapps', []) data_to_update['id'] = current_user.fleet_id data_to_update['fc_id'] = current_user.character_id data_to_update['members'] = new_members update = { '$set': data_to_update, '$currentDate': { 'updated_time': { '$type': 'date' } } } _filter = {'id': current_user.fleet_id} mongo.db.fleets.update_one(_filter, update, upsert=True) return fleet.data, data_to_update['connected_clients']
def alliance_update(alliance_id, public_data=None): """ adds or updates a DB entry for an alliance and returns it """ data_to_update = {} data_to_update['id'] = alliance_id data_to_update['type'] = 'alliance' if not public_data: op = esiapp.op['get_alliances_alliance_id']( alliance_id=alliance_id ) public_data = esiclient.request(op) if public_data.status != 200: logger.error('status: ' + str(public_data.status) + ' error with getting public data: ' + str(public_data.data)) logger.error('headers: ' + str(public_data.header)) logger.error('alliance with issue: ' + str(alliance_id)) return data_to_update['date_founded'] = public_data.data['date_founded'].v.replace(tzinfo=timezone.utc).strftime("%Y-%m-%d %X") data_to_update['name'] = public_data.data['name'] data_to_update['ticker'] = public_data.data['ticker'] if 'executor_corporation_id' in public_data.data: data_to_update['executor_corporation_id'] = public_data.data['executor_corporation_id'] op = esiapp.op['get_alliances_alliance_id_corporations']( alliance_id=alliance_id ) public_data = esiclient.request(op) if public_data.status != 200: logger.error('status: ' + str(public_data.status) + ' error with getting public data: ' + str(public_data.data)) logger.error('headers: ' + str(public_data.header)) logger.error('alliance with issue: ' + str(alliance_id)) return data_to_update['corps'] = public_data.data alliance_filter = {'id': alliance_id} update = {"$set": data_to_update} new_alliance_doc = db.entities.find_one_and_update(alliance_filter, update, upsert=True, return_document=ReturnDocument.AFTER) if 'executor_corporation_id' in public_data.data: decode_party_id(data_to_update['executor_corporation_id']) for corp in public_data.data: decode_party_id(corp) return new_alliance_doc
def handle_kick(_id): update_token(current_user) op = esiapp.op['delete_fleets_fleet_id_members_member_id']( member_id=_id, fleet_id=current_user.get_fleet_id()) request = esiclient.request(op) if request.status != 204: error_string = request.data['error'] if request.data else str( request.status) logging.error('error performing kick for %s', _id) logging.error('error is: %s', error_string) emit('error', error_string) return emit('info', 'member kicked')
def id_from_name(_name): id_filter = {'name': _name} result = mongo.db.entities.find_one(id_filter) if result is not None: return result['id'] op = esiapp.op['post_universe_ids'](names=[_name]) request = esiclient.request(op) try: esi_error_check_basic(request, 'ship data', str(_name)) except EsiError: return 0 if ('inventory_types' in request.data): add_db_entity(request.data['inventory_types'][0]['id'], _name) return request.data['inventory_types'][0]['id'] else: add_db_entity(-1, _name) return -1
def handle_move(info): movement = {'role': info['role']} if info['squad'] > 0: movement['squad_id'] = info['squad'] if info['wing'] > 0: movement['wing_id'] = info['wing'] update_token(current_user) op = esiapp.op['put_fleets_fleet_id_members_member_id']( member_id=info['id'], fleet_id=current_user.get_fleet_id(), movement=movement) request = esiclient.request(op) if request.status != 204: error_string = request.data['error'] if request.data else str( request.status) logging.error('error performing move for %s', info['id']) logging.error('error is: %s', error_string) emit('error', error_string) return emit('info', info['name'] + ' moved to ' + info['role'])
def update_fleet_metadata(current_user, client=False): if client: connectedType = 'connected_clients' else: connectedType = 'connected_webapps' data_to_update = {} update_token(current_user) op = esiapp.op['get_characters_character_id_fleet']( character_id=current_user.get_id()) fleet = esiclient.request(op) esi_error_check_basic(fleet, 'fleet', str(current_user.get_id())) data_to_update['id'] = fleet.data['fleet_id'] data_to_update['fc_id'] = fleet.data['fleet_boss_id'] current_user.set_fleet_id(fleet.data['fleet_id']) current_user.set_fleet_role(fleet.data['role']) # remove from old fleets docs = mongo.db.fleets.find({connectedType: current_user.character_id}) if docs is not None: for fleet in docs: if fleet[ 'id'] != current_user.fleet_id and current_user.character_id in fleet[ connectedType]: fleet[connectedType].remove(current_user.character_id) update = {'$set': {connectedType: fleet[connectedType]}} mongo.db.fleets.update_one({'id': fleet['id']}, update) _filter = {'id': current_user.fleet_id} doc = mongo.db.fleets.find_one(_filter) if doc is not None: if current_user.character_id not in doc[connectedType]: doc[connectedType].append(current_user.character_id) data_to_update[connectedType] = doc[connectedType] else: data_to_update[connectedType] = [current_user.character_id] if client: data_to_update['connected_webapps'] = [] else: data_to_update['connected_clients'] = [] data_to_update['members'] = [] data_to_update['client_access'] = True data_to_update['fleet_access'] = { 'fleet_commander': False, 'wing_commander': False, 'squad_commander': False, 'squad_member': False } update = { '$set': data_to_update, '$currentDate': { 'updated_time': { '$type': 'date' } } } return mongo.db.fleets.find_one_and_update( _filter, update, upsert=True, return_document=ReturnDocument.AFTER)