class Account: """The bot contains an instance to the main page of the app""" # ogame logic ogame = None # data dictionary data = {} # array of planets and moons planets = [] # persistance file file = "ogame.json" def __init__(self): print("Account.__init__") def login(self, universe, user, password): pc.yellow("Logging in with %s" % user, end=" ") try: self.ogame = OGame(universe, user, password) except Exception as e: pp.red("Failed to login %s. Exception: " % user + str(e)) else: if self.ogame.is_logged(): pc.green("Success!") for p in self.planets: p.ogame = self.ogame # # Fetching functions (they display only status messages, not the data fetched) # def fetch_general(self): pc.yellow("Updating general user info") self.ogame.general_get_user_infos() def fetch_planets(self): if self.ogame is None: pp.red("Not logged in") return # read from the server pc.yellow("Updating all planet ids...") planets = self.ogame.get_planet_ids() + self.ogame.get_moon_ids() # convert to dict and save in data planets_dict = {} for p in planets: pc.yellow("Reading " + p, end="") planet_overview = self.ogame.get_planet_infos(p) planets_dict[planet_overview["planet_name"]] = planet_overview pc.green(" OK for " + planet_overview["planet_name"]) # update the class member if "planets" not in self.data: self.data["planets"] = {} self.data["planets"].update(planets_dict) def fetch_account(self): #TODO: implement this if self.ogame is None: pp.red("Not logged in") return # # Print general account informations (rank, name, research queue, messages) # def pp(self): for planet in self.planets: planet.pp() # # Persistance functions # def load(self, file_name=None): """Reads the data from a json file""" if file_name is not None and self.file != file_name: print("Setting file to %s" % file_name) self.file = file_name file_name = self.file pc.bold("Loading %s" % file_name, end=" ") try: f = open(file_name, "r") data = f.read() f.close() # data = '{"planet": {"mines": "none"}, "moon": {"produciton": "tbd"}}' self.data = json.loads(data) except Exception as e: pp.red("JSON format error: " + str(e)) else: self.init_planets() pc.green("Done.") def save(self, file_name=None): if file_name is not None and self.file != file_name: pp("Setting file to %s", file_name) file_name = self.file pc.bold("Saving to %s" % file_name, end=" ") # updated data for p in self.planets: if p.name() not in self.data["planets"]: self.data["planets"][p.name()] = {} self.data["planets"][p.name()].update(p.data) # write to file try: f = open(file_name, "w") f.write(json.dumps(self.data, indent=4)) f.close() except Exception as e: pp.red("Something went wrong: " + str(e)) else: pc.green("Done.") # # Data manipulation # def planet_data(self, planet_id): """Given an ID returns the planet data source (aka data)""" for planet in self.planets: if planet.id() == planet_id: return planet.data def init_planets(self): """Given an existing data, gets the `planets` key and creates attributes on the `Account` class""" if 'planets' not in self.data: pc.red( "Unable to initialize `planets`. No `planets` key found in `self.data`" ) self.planets = [] for k, v in self.data['planets'].items(): planet = Planet(self, v) self.planets.append(planet) self.__setattr__(k.lower(), planet)
class BOT: def __init__(self, universe, username, password, server, delay=60, gap=60): self.ogame = OGame(universe, username, password, server) self.planet_ids = self.ogame.get_planet_ids() self.delay = delay self.gap = gap self.resources = {} self.resources_buildings = {} self.research = {} @staticmethod def pretty_dict(name, d): print " " + "\033[91m" + name + "\033[0m" for key, value in d.iteritems(): print " {}: {}".format(key, value) def pretty_info(self): for planet_id in planet_ids: print '\033[92m' + str(datetime.datetime.now()) + '\033[0m' self.pretty_dict("resource", self.resources[planet_id]) self.pretty_dict("resource level", self.resources_buildings[planet_id]) self.pretty_dict("research", self.research) def get_info(self, planet_id): for planet_id in self.planet_ids: self.resources[planet_id] = self.ogame.get_resources(planet_id) self.resources_buildings[ planet_id] = self.ogame.get_resources_buildings(planet_id) self.research = self.ogame.get_research() def auto_escape(self): if not self.ogame.is_under_attack(): return print '\033[31m' + "under attack!! auto escape on!!" + '\033[0m' ships = [] where = {'galaxy': 1, 'system': 161, 'position': 16} resources = { 'metal': 10000000, 'crystal': 10000000, 'deuterium': 10000000 } for value in Ships.values(): ships.append((value, 10000)) fleet_id = self.ogame.send_fleet(self.planet_ids[0], ships, Speed['100%'], where, Missions['Expedition'], resources) if fleet_id: print '\033[31m' + "successfully depart away from battle field!!" + '\033[0m' def auto_merge_resources(self, where): ships = [] resources = { 'metal': 10000000, 'crystal': 10000000, 'deuterium': 10000000 } for value in Ships.values(): ships.append((value, 10000)) # assume mother planet at the first planet for planet_id in self.planet_ids[1:]: fleet_id = self.ogame.send_fleet(planet_id, ships, Speed['100%'], where, Missions['Transport'], resources) if fleet_id: print '\033[36m' + "send fleet from " + str( planet_id) + '\033[0m' def auto_upgrade(self, positive_energy=True): if positive_energy and resource["energy"] < 0: ogame.build(planet_id, Buildings["SolarPlant"]) ogame.build(planet_id, Buildings["FusionReactor"]) if resource["energy"] < -60: ogame.build(planet_id, (Ships["SolarSatellite"], 1)) else: for value in Buildings.values(): ogame.build(planet_id, value) for value in Facilities.values(): ogame.build(planet_id, value) for value in Research.values(): ogame.build(planet_id, value) def run(self): while True: self.get_info() self.pretty_info() self.auto_escape() self.auto_merge_resources() time.sleep(random.randint(self.delay, self.delay + self.gap))