messenger.py (10847B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\sovereignty\skyhook\client\messenger.py 2 import datetime 3 from six.moves import http_client as httplib 4 import eveProto.generated.eve_public.sovereignty.skyhook.api.requests_pb2 as SkyhookAPI 5 import eveProto.generated.eve_public.sovereignty.skyhook.api.admin.admin_pb2 as SkyhookAdminAPI 6 from eveProto import blue_to_timestamp 7 from publicGateway.grpc.backoff import backoff_if_error 8 from publicGateway.grpc.exceptions import GenericException 9 from semantic_version import SemanticVersion 10 from sovereignty.resource.client.data_types import ReagentProductionSplitDynamicData, ReagentDefinition 11 from sovereignty.skyhook.data_type import Skyhook, get_skyhook_object, PlanetAndSkyhookTheftVulnerabilityData, SkyhookTheftVulnerabilityData 12 import sovereignty.skyhook.client.errors as errors 13 TIMEOUT_SECONDS = 10 14 import logging 15 logger = logging.getLogger('skyhook') 16 17 class SkyhookMessenger(object): 18 19 def __init__(self, public_gateway): 20 super(SkyhookMessenger, self).__init__() 21 self.public_gateway = public_gateway 22 23 @backoff_if_error(whitelist_errors=[errors.SkyhookAccessForbiddenError]) 24 def get(self, skyhook_id): 25 logger.info('get %s', skyhook_id) 26 request = SkyhookAPI.GetRequest() 27 request.skyhook.sequential = skyhook_id 28 try: 29 _, _, response_payload = self._send_request(request, SkyhookAPI.GetResponse) 30 except GenericException as e: 31 if e.response_primitive.status_code == httplib.FORBIDDEN: 32 raise errors.SkyhookAccessForbiddenError() 33 else: 34 raise 35 36 vulnerability_data = None 37 if response_payload.WhichOneof('vulnerability') == 'theft_vulnerability': 38 vulnerability_data = response_payload.theft_vulnerability 39 workforce = None 40 if response_payload.workforce.WhichOneof('available') == 'amount': 41 workforce = response_payload.workforce.amount 42 resource_version = SemanticVersion.from_proto(response_payload.planet_resources_definitions_version) 43 return get_skyhook_object(response_payload.skyhook.sequential, response_payload.active, response_payload.reagent_simulations, response_payload.reagent_definitions, vulnerability_data, workforce, resource_version) 44 45 @backoff_if_error() 46 def get_all_local(self): 47 logger.info('get_all_local') 48 request = SkyhookAPI.GetAllLocalRequest() 49 _, _, response_payload = self._send_request(request, SkyhookAPI.GetAllLocalResponse) 50 skyhooks = [] 51 for skyhook in response_payload.skyhooks: 52 vulnerability_data = None 53 if skyhook.WhichOneof('vulnerability') == 'theft_vulnerability': 54 vulnerability_data = skyhook.theft_vulnerability 55 workforce = None 56 if skyhook.workforce.WhichOneof('available') == 'amount': 57 workforce = skyhook.workforce.amount 58 resource_version = SemanticVersion.from_proto(skyhook.planet_resources_definitions_version) 59 skyhooks.append(get_skyhook_object(skyhook.skyhook.sequential, skyhook.active, skyhook.reagent_simulations, skyhook.reagent_definitions, vulnerability_data, workforce, resource_version)) 60 61 return (response_payload.solar_system.sequential, skyhooks) 62 63 @backoff_if_error() 64 def get_all_corporation(self, corporation_id): 65 logger.info('get_all_corporation %s', corporation_id) 66 request = SkyhookAPI.GetAllByCorporationRequest() 67 request.corporation.sequential = corporation_id 68 _, _, response_payload = self._send_request(request, SkyhookAPI.GetAllByCorporationResponse) 69 skyhooks = {} 70 for skyhook in response_payload.skyhooks: 71 vulnerability_data = None 72 if skyhook.WhichOneof('vulnerability') == 'theft_vulnerability': 73 vulnerability_data = skyhook.theft_vulnerability 74 workforce = None 75 if skyhook.workforce.WhichOneof('available') == 'amount': 76 workforce = skyhook.workforce.amount 77 resource_version = SemanticVersion.from_proto(skyhook.planet_resources_definitions_version) 78 skyhooks[skyhook.skyhook.sequential] = get_skyhook_object(skyhook.skyhook.sequential, skyhook.active, skyhook.reagent_simulations, skyhook.reagent_definitions, vulnerability_data, workforce, resource_version) 79 80 return skyhooks 81 82 @backoff_if_error() 83 def activate(self, skyhook_id): 84 request = SkyhookAPI.ActivateRequest() 85 request.skyhook.sequential = skyhook_id 86 try: 87 _, _, _ = self._send_request(request, SkyhookAPI.ActivateResponse) 88 except GenericException as e: 89 if e.response_primitive.status_code == httplib.NOT_FOUND: 90 raise errors.SkyhookNotFoundError() 91 elif e.response_primitive.status_code == httplib.FORBIDDEN: 92 raise errors.SkyhookActivateForbiddenError 93 else: 94 raise 95 96 return True 97 98 @backoff_if_error() 99 def deactivate(self, skyhook_id): 100 request = SkyhookAPI.DeactivateRequest() 101 request.skyhook.sequential = skyhook_id 102 try: 103 _, _, _ = self._send_request(request, SkyhookAPI.DeactivateResponse) 104 except GenericException as e: 105 if e.response_primitive.status_code == httplib.NOT_FOUND: 106 raise errors.SkyhookNotFoundError() 107 elif e.response_primitive.status_code == httplib.FORBIDDEN: 108 raise errors.SkyhookDeactivateForbiddenError 109 elif e.response_primitive.status_code == httplib.INTERNAL_SERVER_ERROR: 110 raise errors.SkyhookDeactivateInternalError 111 else: 112 raise 113 114 return True 115 116 @backoff_if_error() 117 def get_solar_systems_with_theft_vulnerable_skyhooks(self): 118 request = SkyhookAPI.GetSolarSystemsWithTheftVulnerableSkyhooksRequest() 119 _, _, response_payload = self._send_request(request, SkyhookAPI.GetSolarSystemsWithTheftVulnerableSkyhooksResponse) 120 solar_systems = [ solar_system.sequential for solar_system in response_payload.solar_systems ] 121 return solar_systems 122 123 @backoff_if_error() 124 def get_theft_vulnerable_skyhooks_in_solar_system(self, solar_system_id): 125 request = SkyhookAPI.GetTheftVulnerableSkyhooksInSolarSystemRequest() 126 request.solar_system.sequential = solar_system_id 127 _, _, response_payload = self._send_request(request, SkyhookAPI.GetTheftVulnerableSkyhooksInSolarSystemResponse) 128 skyhooks = [] 129 for skyhook_data in response_payload.skyhooks: 130 skyhooks.append(PlanetAndSkyhookTheftVulnerabilityData(skyhook_data.skyhook.sequential, skyhook_data.planet.sequential, skyhook_data.start.ToDatetime(), skyhook_data.expiry.ToDatetime())) 131 132 return skyhooks 133 134 @backoff_if_error() 135 def admin_modify_reagents(self, skyhook_id, reagent_type, secure_amount, insecure_amount, timestamp = None): 136 request = SkyhookAdminAPI.ModifyReagentsRequest() 137 request.skyhook.sequential = skyhook_id 138 reagent = SkyhookAdminAPI.ModifyReagentsRequest.ReagentsSimulationState() 139 reagent.reagent.sequential = reagent_type 140 reagent.secured_stock = secure_amount 141 reagent.unsecured_stock = insecure_amount 142 if timestamp is None: 143 reagent.now = True 144 else: 145 blue_to_timestamp(timestamp, reagent.timestamp) 146 request.reagents.append(reagent) 147 try: 148 _, _, _ = self._send_request(request, SkyhookAdminAPI.ModifyReagentsResponse) 149 except GenericException as e: 150 if e.response_primitive.status_code == httplib.NOT_FOUND: 151 raise errors.SkyhookNotFoundError() 152 elif e.response_primitive.status_code == httplib.FORBIDDEN: 153 raise errors.SkyhookDeactivateForbiddenError 154 elif e.response_primitive.status_code == httplib.INTERNAL_SERVER_ERROR: 155 raise errors.SkyhookDeactivateInternalError 156 elif e.response_primitive.status_code == httplib.BAD_REQUEST: 157 raise errors.SkyhookBadRequestError 158 else: 159 raise 160 161 return True 162 163 def _send_request(self, request, response_class): 164 request_primitive, response_channel = self.public_gateway.send_character_request(request, response_class, TIMEOUT_SECONDS) 165 response_primitive, payload = response_channel.receive() 166 if response_primitive.status_code != httplib.OK: 167 raise GenericException(request_primitive=request_primitive, response_primitive=response_primitive) 168 return (request_primitive, response_primitive, payload) 169 170 171 class FakeSkyhookMessenger(object): 172 173 def get(self, skyhook_id): 174 from sovereignty.resource.client.messenger import FakeResourceDataForPlanets 175 slimItem = sm.GetService('michelle').GetBallpark().GetInvItem(skyhook_id) 176 d = FakeResourceDataForPlanets().GetData(slimItem.planetID) 177 reagent_simulation_list = [] 178 reagent_configuration_list = [] 179 if d and d.staticReagents: 180 from sovereignty.resource.client.data_types import HarvestSplitSimulationState 181 import gametime 182 reagent_simulation_list.append(ReagentProductionSplitDynamicData(reagent_type_id=d.reagent_type_id, split_function_dynamic=HarvestSplitSimulationState(d.secure_amount, d.insecure_amount, gametime.GetWallclockTime()))) 183 reagent_configuration_list.append(ReagentDefinition(type_id=d.reagent_type_id, split_configuration=d.staticReagents.split_configuration)) 184 theft_vulnerability = SkyhookTheftVulnerabilityData(False, datetime.datetime.utcnow() + datetime.timedelta(hours=1), datetime.datetime.utcnow() + datetime.timedelta(hours=1)) 185 skyhook = Skyhook(skyhook_id, True, reagent_simulation_list, reagent_configuration_list, theft_vulnerability) 186 return skyhook 187 188 def get_all_local(self): 189 allSkyhooks = [] 190 for _, slimItem in sm.GetService('michelle').GetBallpark().GetBallsAndItems(): 191 if slimItem.typeID == 81080: 192 allSkyhooks.append(self.get(slimItem.itemID)) 193 194 return (session.solarsystemid2, allSkyhooks) 195 196 def get_solar_systems_with_theft_vulnerable_skyhooks(self): 197 return [30004045, 198 30000872, 199 30004664, 200 30004956, 201 30000208] 202 203 def get_theft_vulnerable_skyhooks_in_solar_system(self, solar_system_id): 204 return [PlanetAndSkyhookTheftVulnerabilityData(1000000012512L, 40013231, datetime.datetime.utcnow() - datetime.timedelta(hours=1), datetime.datetime.utcnow() + datetime.timedelta(hours=1))] 205 206 def activate(self, skyhook_id): 207 return True 208 209 def deactivate(self, skyhook_id): 210 return False