def autoplay(self): """ Creates a new ClientActor, and connects it with the Server. This method only returns when the ClientActor finishes. """ client_actor = ClientActor(self.team_name) client_actor.register_team(self.team) if self.port is None: address = "%s" % self.main_actor connect = lambda: client_actor.connect_local(self.main_actor) else: address = "%s on %s:%s" % (self.main_actor, self.host, self.port) connect = lambda: client_actor.connect(self.main_actor, self.host, self.port) # Try 3 times to connect for i in range(3): if connect(): break else: print "%s: No connection to %s." % (self.team_name, address), if i < 2: print " Waiting 3 seconds. (%d/3)" % (i + 1) time.sleep(3) else: print "Giving up." return try: while client_actor.actor_ref.is_alive: client_actor.actor_ref.join(1) except KeyboardInterrupt: print "%s: Client received CTRL+C. Exiting." % self.team_name finally: client_actor.actor_ref.stop()
# -*- coding: utf-8 -*- from pelita.player import RandomPlayer, BFSPlayer, SimpleTeam from pelita.actors import ClientActor import logging from pelita.utils.colorama_wrapper import colorama FORMAT = '[%(asctime)s,%(msecs)03d][%(name)s][%(levelname)s][%(funcName)s]' + colorama.Fore.MAGENTA + ' %(message)s' + colorama.Fore.RESET #logging.basicConfig(format=FORMAT, datefmt="%H:%M:%S", level=logging.WARNING) clientActor = ClientActor("the good ones") clientActor.register_team(SimpleTeam(BFSPlayer(), BFSPlayer())) clientActor.connect("pelita-main", host="", port=50007) clientActor2 = ClientActor("the bad ones") clientActor2.register_team(SimpleTeam(RandomPlayer(), RandomPlayer())) clientActor2.connect("pelita-main", host="", port=50007) try: while clientActor.actor_ref.is_alive: clientActor.actor_ref.join(1) except KeyboardInterrupt: pass finally: clientActor.actor_ref.stop() clientActor2.actor_ref.stop()