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()
# . # . .#1# ################## """) # Notify the ServerActor that we’d like to run a game # with our preferred layout, 4 bots and 200 rounds. server.notify("initialize_game", [layout, 4, 200]) # Initialise a TkViewer and register it with the ServerActor. viewer = TkViewer() server.notify("register_viewer", [viewer]) # Our two PlayerTeams must be defined in the same Python # process (because this is local game). # Create their ClientActors, register the Teams, and connect # to the ServerActor. clientActor = ClientActor("the good ones") clientActor.register_team(SimpleTeam(BFSPlayer(), BFSPlayer())) clientActor.connect_local("pelita-main") clientActor2 = ClientActor("the bad ones") clientActor2.register_team(SimpleTeam(RandomPlayer(), RandomPlayer())) clientActor2.connect_local("pelita-main") # Now follows the boilerplate which is needed to run the game. # As this uses a TkViewer, we need to give Tk the control # over our main thread. # Since everything else runs in threaded actors, this is not # much of a problem. The queue needed to exchange data between # different threads is handled by our TkViewer. try: viewer.app.mainloop()