Beispiel #1
0
 def _connect(self):
     self.logger.info('Connecting to server %s.ogame.%s' % (self.account.universe, self.account.domain))
     self.web = WebAdapter(self)
Beispiel #2
0
class Master(object):

    def __init__(self, options, args):
        self.logger = logging.getLogger("Master")

        self.options = options
        self.args = args

        self.web = None
        self.config = {}
        
        self._load_config()
        self.i18n = Translations()

    def run(self):

        self.logger.info("Welcome, %s", self.account.username)

        # connect to server and login
        self._connect()

        self.logger.info('ServerTime: ' + get_servertime_now().strftime('%Y-%m-%d %H:%M:%S'))

        # set base planet
        planet_name = self.options.planet if self.options.planet else self.config.get('planet', self.web.home)
        if planet_name:
            self.web.go_to_planet(planet_name)
            self.logger.info("Current planet: %s" % self.web.current_planet)

        # execute tasks
        if self.options.sense:
            self.scan_galaxy(mode='sense', spy=self.options.spy)
        elif self.options.scan:
            self.scan_galaxy(mode='scan', spy=self.options.spy, expression=self.options.scan)
        elif self.options.queue:
            self.start_build_queue()
        elif self.options.message:
            
            msgs = self.web.get_messages(self.options.all)

            for m in msgs:
                try:
                    print m
                except:
                    print 'error-------', m.subject

        elif self.options.stat:
            stat.stat(self.web.statistic())

    def stop(self):
        pass

    def _connect(self):
        self.logger.info('Connecting to server %s.ogame.%s' % (self.account.universe, self.account.domain))
        self.web = WebAdapter(self)

    def scan_galaxy(self, mode, spy=False, expression=None):
        coords = []

        if mode == 'sense':
            for s in self.config['sense']:
                if not s['status']: continue
                p = self.web.planets[s['planet']]
                origin = p.coord
                (o_galaxy, o_system, o_position) = origin.tuple()

                coords.append(origin)
                for i in range(1, int(s['radius'])+1):
                    coords.append(Coord(o_galaxy, o_system + i))
                    coords.append(Coord(o_galaxy, o_system - i))

        elif mode == 'scan':
            coords = parse_coords_exp(expression)

        datas = self.web.scan_solar_systems(coords)

        if spy:
            planets_to_spy = ai.analyze_galaxy(datas)
            from progressbar import *
            spy_widgets = ['Spying: ', Percentage(), ' ', Bar('>'), ' ', ETA()]
            spy_bar = ProgressBar(widgets=spy_widgets, maxval=len(planets_to_spy)).start()
            for i,p in enumerate(planets_to_spy):
                while True:
                    if self.web.spy(p.coordinate()):
                        spy_bar.update(i)
                        break

            self.logger.info("Spy task completed")

    def start_build_queue(self):
        for p,q in self.config.queues.iteritems():
            planet = self.web.planets[p]
            build_queue = Queue()
            for b in q:
                build_queue.put(b)
            thread = BuildThread(self.web, planet, build_queue)
            thread.start()
            thread.join()

        self.logger.info('all buiding in queue is done!')
                
    def _load_config(self):
        # load basic configuration
        cf = file(os.getcwd() + '/config.yml' ,'r')
        self.config = Configuration(cf)
        of = file(os.getcwd() + '/ogame.yml', 'r')
        self.config.update(yaml.load(of.read()))
        of.close()
        
        # load profile
        self.account = Account()
        acc = self.options.account if self.options.account else self.config['default_profile']
        profile_path = os.getcwd() + '/profiles/%s/' % acc
        p = file(profile_path + 'master.yml')
        profile = yaml.load(p.read())
        self.config.update(profile)
        p.close()

        self.account.username = self.config['username']
        self.account.password = self.config['password']
        self.account.universe = self.config['universe']
        self.account.domain = self.config['domain']

        # load queue
        bq = file(profile_path + 'queue.yml')
        build_queue = yaml.load(bq)
        bq.close()
        self.config['queues'] = build_queue

        app.config = self.config