def test_any_player_beside(self): instance = EDInstance() self.assertFalse(instance.any_player_beside(None)) instance.player_in(MockCmdr("LeKeno")) self.assertTrue(instance.any_player_beside(None)) self.assertFalse(instance.any_player_beside(["LeKeno"])) self.assertFalse(instance.any_player_beside(["lekeno"])) instance.player_in(MockCmdr("Arguendo")) self.assertTrue(instance.any_player_beside(None)) self.assertTrue(instance.any_player_beside(["LeKeno"])) self.assertTrue(instance.any_player_beside(["lekeno"])) self.assertFalse(instance.any_player_beside(["lekeno", "arguendo"])) instance.player_in(MockCmdr("Ozram")) self.assertTrue(instance.any_player_beside(None)) self.assertTrue(instance.any_player_beside(["LeKeno"])) self.assertTrue(instance.any_player_beside(["lekeno"])) self.assertTrue(instance.any_player_beside(["lekeno", "arguendo"])) self.assertFalse( instance.any_player_beside(["lekeno", "arguendo", "ozram"]))
class EDPlayerOne(EDPlayer): EDR_FLEET_CARRIER_CACHE = utils2to3.abspathmaker(__file__, 'cache', 'fleet_carrier.v1.p') def __init__(self, name=None): super(EDPlayerOne, self).__init__(name) self.powerplay = None self.game_mode = None self.private_group = None self.previous_mode = None self.previous_wing = set() self.from_genesis = False self.wing = EDWing() self.friends = set() self.crew = None self._target = None self.instance = EDInstance() self.planetary_destination = None self.recon_box = EDReconBox() self.inventory = EDRInventory() self.fleet = edrfleet.EDRFleet() try: with open(self.EDR_FLEET_CARRIER_CACHE, 'rb') as handle: self.fleet_carrier = pickle.load(handle) except: self.fleet_carrier = edrfleetcarrier.EDRFleetCarrier() self.mining_stats = edrminingstats.EDRMiningStats() self.bounty_hunting_stats = edrbountyhuntingstats.EDRBountyHuntingStats() def __repr__(self): return str(self.__dict__) def persist(self): self.inventory.persist() with open(self.EDR_FLEET_CARRIER_CACHE, 'wb') as handle: pickle.dump(self.fleet_carrier, handle, protocol=pickle.HIGHEST_PROTOCOL) def target_pilot(self): return self._target def target_vehicle(self): if not self._target: return None return self._target.targeted_vehicle def targeting(self, pilot, ship_internal_name=None): if pilot.is_human(): self.instance.player_in(pilot) else: self.instance.npc_in(pilot) if self._target: self._target.untargeted() self._target._touch() self._target = pilot mothership = True slf = False srv = False if ship_internal_name: vehicle = EDVehicleFactory.from_internal_name(ship_internal_name) slf = EDVehicleFactory.is_ship_launched_fighter(vehicle) srv = EDVehicleFactory.is_surface_vehicle(vehicle) mothership = not(slf or srv) pilot.targeted(mothership, slf, srv) pilot._touch() self._touch() def untarget(self): if self._target: self._target.untargeted() self._target._touch() self._target = None self._touch() def lowish_fuel(self): if self.mothership.fuel_level is None or self.mothership.fuel_capacity is None: return True # Better safe than sorry return (self.mothership.fuel_level / self.mothership.fuel_capacity) <= 0.3 def heavily_damaged(self): if self.mothership.hull_health is None: return True # Better safe than sorry return self.mothership.hull_health <= 50 def json(self, fuel_info=False, with_target=False): result = { u"cmdr": self.name, u"timestamp": self.timestamp * 1000, u"wanted": self.wanted, u"bounty": self.bounty, u"starSystem": self.star_system, u"place": self.place, u"wingof": len(self.wing.wingmates), u"wing": self.wing.noteworthy_changes_json(self.instance), u"byPledge": self.powerplay.canonicalize() if self.powerplay else u'', u"ship": self.piloted_vehicle.json(fuel_info=fuel_info), u"mode": self.game_mode, u"group": self.private_group } if with_target: result[u"target"] = self.target_pilot().json() if self._target else {} result[u"crew"] = [] if self.crew: result[u"crew"] = [ {u"cmdr": crew_member} for crew_member in self.crew.all_members()] return result def force_new_name(self, new_name): self._name = new_name def in_solo_or_private(self): return self.game_mode in [u"Solo", u"Group"] def in_solo(self): return self.game_mode == u"Solo" def in_open(self): return self.game_mode == u"Open" def inception(self, genesis=False): if genesis: self.from_genesis = True self.previous_mode = None self.previous_wing = set() self.wing = EDWing() self.crew = None self.destroyed = False self.untarget() self.wanted = False self.mothership = EDVehicleFactory.unknown_vehicle() self.piloted_vehicle = self.mothership self.targeted_vehicle = None self.srv = None self.slf = None self.location = EDLocation() self._bounty = None self.instance.reset() self.to_normal_space() self._touch() self.reset_stats() def killed(self): super(EDPlayerOne, self).killed() self.previous_mode = self.game_mode self.previous_private_group = self.private_group self.previous_wing = self.wing.wingmates.copy() self.game_mode = None self.private_group = None self.wing = EDWing() self.crew = None self.untarget() self.instance.reset() self.recon_box.reset() self._touch() def resurrect(self, rebought=True): self.game_mode = self.previous_mode self.private_group = self.previous_private_group self.wing = EDWing(self.previous_wing) self.previous_mode = None self.previous_wing = set() self.destroyed = False self.untarget() self.to_normal_space() self.instance.reset() self._touch() if rebought: self.mothership.reset() if self.slf: self.slf.reset() if self.srv: self.srv.reset() else: self.mothership = EDVehicleFactory.unknown_vehicle() self.piloted_vehicle = self.mothership self.slf = None self.srv = None def is_crew_member(self): if not self.crew: return False return self.crew.captain != self.name def in_a_crew(self): return self.crew is not None def leave_wing(self): self.wing.leave() self._touch() def join_wing(self, others): self.wing.join(others) self.crew = None self._touch() def add_to_wing(self, other): self.wing.add(other) self._touch() def in_a_wing(self): return self.wing.formed() def leave_crew(self): self._touch() if not self.crew: return self.crew = None self.instance.reset() def disband_crew(self): self._touch() if not self.crew: return for member in self.crew.members: self.instance.player_out(member) self.crew.disband() def join_crew(self, captain): self.wing = EDWing() self.instance.reset() self.crew = EDRCrew(captain) self.crew.add(self.name) self.instanced_player(captain) self.mothership = EDVehicleFactory.unknown_vehicle() self.piloted_vehicle = self.mothership self.slf = None self.srv = None self._touch() def add_to_crew(self, member): self._touch() if not self.crew: self.crew = EDRCrew(self.name) self.wing = EDWing() self.instance.reset() self.instanced_player(member) return self.crew.add(member) def remove_from_crew(self, member): self._touch() if not self.crew: self.crew = EDRCrew(self.name) self.wing = EDWing() self.instance.reset() self.instance.player_out(member) return self.crew.remove(member) def crew_time_elapsed(self, member): if not self.crew: return 0 return self.crew.duration(member) def is_captain(self, member=None): if not self.crew: return False if not member: member = self.name return self.crew.is_captain(member) def is_friend(self, cmdr_name): return cmdr_name in self.friends def is_wingmate(self, cmdr_name): return cmdr_name in self.wing.wingmates def is_crewmate(self, cmdr_name): if not self.crew: return False return cmdr_name in self.crew.all_members() def is_enemy_with(self, power): if self.is_independent() or not power: return False return self.powerplay.is_enemy(power) def to_normal_space(self): if self.in_normal_space(): return super(EDPlayerOne, self).to_normal_space() self.instance.reset() def to_super_space(self): if self.in_supercruise(): return super(EDPlayerOne, self).to_super_space() self.instance.reset() self.recon_box.reset() def to_hyper_space(self): if self.in_hyper_space(): return super(EDPlayerOne, self).to_hyper_space() self.instance.reset() self.recon_box.reset() def wing_and_crew(self): wing_and_crew = self.wing.wingmates.copy() if self.crew: wing_and_crew.update(self.crew.all_members() ) return wing_and_crew def maybe_in_a_pvp_fight(self): if not self.in_a_fight(): return False if self.instance.is_void_of_player(): # Can't PvP if there is no other player. return False if not self.instance.any_player_beside(self.wing_and_crew()): return False return True def leave_vehicle(self): self.mothership = EDVehicleFactory.unknown_vehicle() self.piloted_vehicle = self.mothership self.slf = None self.srv = None self.instance.reset() self.recon_box.reset() self._touch() def destroy(self, cmdr): self._touch() cmdr.killed() self.instance.player_out(cmdr.name) if self.target_pilot() and self.target_pilot().name == cmdr.name: self.untarget() def interdiction(self, interdicted, success): self._touch() self.to_normal_space() if success and interdicted: interdicted.location = self.location if interdicted.is_human(): self.instance.player_in(interdicted) else: self.instance.npc_in(interdicted) else: self.recon_box.reset() def interdicted(self, interdictor, success): self._touch() if success: self.to_normal_space() if interdictor: interdictor.location = self.location if interdictor.is_human(): self.instance.player_in(interdictor) else: self.instance.npc_in(interdictor) else: if interdictor: if interdictor.is_human(): self.instance.player_out(interdictor.name) else: self.instance.npc_out(interdictor.name) self.recon_box.reset() def is_instanced_with_player(self, cmdr_name): return self.instance.player(cmdr_name) != None def instanced_player(self, cmdr_name, rank=None, ship_internal_name=None, piloted=True): self._touch() cmdr = self.instance.player(cmdr_name) if not cmdr: cmdr = EDPlayer(cmdr_name, rank) cmdr.location = self.location if ship_internal_name: vehicle = EDVehicleFactory.from_internal_name(ship_internal_name) cmdr.update_vehicle_if_obsolete(vehicle, piloted) self.instance.player_in(cmdr) return cmdr def deinstanced_player(self, cmdr_name): self._touch() self.instance.player_out(cmdr_name) def instanced_npc(self, name, rank=None, ship_internal_name=None, piloted=True): self._touch() npc = self.instance.npc(name, rank, ship_internal_name) if not npc: npc = EDPilot(name, rank) npc.location = self.location if ship_internal_name: vehicle = EDVehicleFactory.from_internal_name(ship_internal_name) npc.update_vehicle_if_obsolete(vehicle, piloted) self.instance.npc_in(npc) return npc def attacked(self, target): self._touch() if target == u"Mothership": self.mothership.attacked() elif target == u"Fighter": if self.slf: self.slf.attacked() else: EDRLOG.log(u"SLF attacked but player had none", u"WARNING") elif target == u"You": self.piloted_vehicle.attacked() def update_fleet(self, stored_ships_entry): self.fleet.update(stored_ships_entry) def prospected(self, entry): self.mining_stats.prospected(entry) def refined(self, entry): self.mining_stats.refined(entry) def bounty_scanned(self, entry): self.bounty_hunting_stats.scanned(entry) def bounty_awarded(self, entry): self.bounty_hunting_stats.awarded(entry) def reset_stats(self): self.mining_stats.reset() self.bounty_hunting_stats.reset()