def test_player_in_n_out(self): instance = EDInstance() self.assertIsNone(instance.player("LeKeno")) lekeno = MockCmdr("LeKeno") instance.player_in(lekeno) self.assertEqual(instance.player("LeKeno"), lekeno) instance.player_out("lekeno") self.assertIsNone(instance.player("LeKeno"))
def test_players_nb(self): instance = EDInstance() self.assertEqual(instance.players_nb(), 0) instance.player_in(MockCmdr("LeKeno")) self.assertEqual(instance.players_nb(), 1) instance.player_out("LeKeno") self.assertEqual(instance.players_nb(), 0) instance.player_in(MockCmdr("LeKeno")) self.assertEqual(instance.players_nb(), 1) instance.player_in(MockCmdr("Arguendo")) self.assertEqual(instance.players_nb(), 2) instance.player_in(MockCmdr("Ozram")) self.assertEqual(instance.players_nb(), 3) instance.player_out("LeKeno") self.assertEqual(instance.players_nb(), 2) instance.player_out("Ozram") self.assertEqual(instance.players_nb(), 1) instance.player_out("Ozram") self.assertEqual(instance.players_nb(), 1) instance.player_out("Dummy") self.assertEqual(instance.players_nb(), 1) instance.player_out("Arguendo") self.assertEqual(instance.players_nb(), 0) instance.player_out("Dummy") self.assertEqual(instance.players_nb(), 0)
def test_empty(self): instance = EDInstance() self.assertTrue(instance.is_empty()) instance.player_in(MockCmdr("LeKeno")) self.assertFalse(instance.is_empty()) instance.reset() self.assertTrue(instance.is_empty()) instance.player_in(MockCmdr("LeKeno")) self.assertFalse(instance.is_empty()) instance.player_out("LeKeno") self.assertTrue(instance.is_empty())
def test_player_noteworthy_changes(self): now = 1000 * calendar.timegm(time.gmtime()) instance = EDInstance() changes = instance.noteworthy_changes_json() self.assertAlmostEquals(changes["timestamp"], now) self.assertEqual(changes["players"], []) changes = instance.noteworthy_changes_json() self.assertIsNone(changes) instance.player_in(MockCmdr("LeKeno")) changes = instance.noteworthy_changes_json() self.assertAlmostEquals(changes["timestamp"], now) self.assertEqual(changes["players"], [{"cmdr": "LeKeno"}]) instance.player_out("LeKeno") changes = instance.noteworthy_changes_json() self.assertAlmostEquals(changes["timestamp"], now) self.assertEqual(changes["players"], []) instance.player_out("LeKeno") changes = instance.noteworthy_changes_json() self.assertIsNone(changes)
def test_anyone_beside(self): instance = EDInstance() self.assertFalse(instance.anyone_beside(None)) instance.player_in(MockCmdr("LeKeno")) self.assertTrue(instance.anyone_beside(None)) self.assertFalse(instance.anyone_beside(["LeKeno"])) self.assertFalse(instance.anyone_beside(["lekeno"])) instance.player_in(MockCmdr("Arguendo")) self.assertTrue(instance.anyone_beside(None)) self.assertTrue(instance.anyone_beside(["LeKeno"])) self.assertTrue(instance.anyone_beside(["lekeno"])) self.assertFalse(instance.anyone_beside(["lekeno", "arguendo"])) instance.player_in(MockCmdr("Ozram")) self.assertTrue(instance.anyone_beside(None)) self.assertTrue(instance.anyone_beside(["LeKeno"])) self.assertTrue(instance.anyone_beside(["lekeno"])) self.assertTrue(instance.anyone_beside(["lekeno", "arguendo"])) self.assertFalse( instance.anyone_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() 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) @property def target(self): return self._target @target.setter def target(self, new_target): if self._target: self._target.targeted = False self._target._touch() self._target = new_target if new_target: new_target.targeted = True new_target._touch() 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.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.target = None self.wanted = False self.mothership = EDVehicleFactory.unknown_vehicle() self.piloted_vehicle = self.mothership self.srv = None self.slf = None self.location = EDLocation() self._bounty = None self.instance.reset() self.to_normal_space() self._touch() self.reset_mining_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.target = None 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.target = None 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(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(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_empty(): # Can't PvP if there is no one. return False if not self.instance.anyone_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 targeting(self, cmdr): self.instance.player_in(cmdr) self.target = cmdr self._touch() def destroy(self, cmdr): self._touch() cmdr.killed() self.instance.player_out(cmdr.name) if self.target and self.target.name == cmdr.name: self.target = None def interdiction(self, interdicted, success): self._touch() self.to_normal_space() if success: interdicted.location = self.location self.instance.player_in(interdicted) else: self.recon_box.reset() def interdicted(self, interdictor, success): self._touch() if success: self.to_normal_space() interdictor.location = self.location self.instance.player_in(interdictor) else: self.instance.player_out(interdictor.cmdr_name) self.recon_box.reset() def is_instanced_with(self, cmdr_name): return self.instance.player(cmdr_name) != None def instanced(self, cmdr_name, ship_internal_name=None, piloted=True): self._touch() cmdr = self.instance.player(cmdr_name) if not cmdr: cmdr = EDPlayer(cmdr_name) 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(self, cmdr_name): self._touch() self.instance.player_out(cmdr_name) 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 reset_mining_stats(self): self.mining_stats.reset()