def test_simple_game(self): layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer( layout_string=layout, rounds=5, players=2, bind_addrs=( "ipc:///tmp/pelita-testplayer1-%s" % uuid.uuid4(), "ipc:///tmp/pelita-testplayer2-%s" % uuid.uuid4(), ), ) for bind_address in server.bind_addresses: self.assertTrue(bind_address.startswith("ipc://")) client1_address = server.bind_addresses[0] client2_address = server.bind_addresses[1] client1 = SimpleClient(SimpleTeam("team1", RandomPlayer()), address=client1_address) client2 = SimpleClient(SimpleTeam("team2", RandomPlayer()), address=client2_address) client1.autoplay_process() client2.autoplay_process() server.run() server.shutdown()
def test_simple_failing_bots(self): layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2) for bind_address in server.bind_addresses: assert bind_address.startswith("tcp://") client1_address = server.bind_addresses[0].replace("*", "localhost") client2_address = server.bind_addresses[1].replace("*", "localhost") class FailingPlayer: def _set_initial(self, dummy, dummy2): pass def _set_index(self, dummy): pass def _get_move(self, universe, game_state): pass client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")), address=client1_address) client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()), address=client2_address) client1.autoplay_process() client2.autoplay_process() server.run() server.shutdown()
def test_failing_bots_do_not_crash_server(self): layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2, timeout_length=0.3) for bind_address in server.bind_addresses: assert bind_address.startswith("tcp://") client1_address = server.bind_addresses[0].replace("*", "localhost") client2_address = server.bind_addresses[1].replace("*", "localhost") class ThisIsAnExpectedException(Exception): pass class FailingPlayer(AbstractPlayer): def get_move(self): raise ThisIsAnExpectedException() old_timeout = pelita.simplesetup.DEAD_CONNECTION_TIMEOUT pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = 0.3 client1 = SimpleClient(SimpleTeam("team1", FailingPlayer()), address=client1_address) client2 = SimpleClient(SimpleTeam("team2", FailingPlayer()), address=client2_address) client1.autoplay_process() client2.autoplay_process() server.run() server.shutdown() pelita.simplesetup.DEAD_CONNECTION_TIMEOUT = old_timeout
def test_simple_remote_game(self): layout = """ ########## # # #0 1# ########## """ client1 = SimpleClient(SimpleTeam("team1", RandomPlayer()), local=False) client2 = SimpleClient(SimpleTeam("team2", RandomPlayer()), local=False) server = SimpleServer(layout_string=layout, rounds=5, players=2, local=False) self.assertEqual(server.host, "") self.assertEqual(server.port, 50007) self.assertTrue(server.server.is_alive) client1.autoplay_background() client2.autoplay_background() server.run_simple(AsciiViewer) self.assertFalse(server.server.is_alive)
def test_simple_remote_game(self): layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2) for bind_address in server.bind_addresses: assert bind_address.startswith("tcp://") client1_address = server.bind_addresses[0].replace("*", "localhost") client2_address = server.bind_addresses[1].replace("*", "localhost") client1 = SimpleClient(SimpleTeam("team1", SteppingPlayer("^>>v<")), address=client1_address) client2 = SimpleClient(SimpleTeam("team2", SteppingPlayer("^<<v>")), address=client2_address) client1.autoplay_process() client2.autoplay_process() server.run() server.shutdown()
def test_simple_remote_game(self): layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2) for bind_address in server.bind_addresses: self.assertTrue(bind_address.startswith("tcp://")) client1_address = server.bind_addresses[0].replace("*", "localhost") client2_address = server.bind_addresses[1].replace("*", "localhost") client1 = SimpleClient(SimpleTeam("team1", TestPlayer("^>>v<")), address=client1_address) client2 = SimpleClient(SimpleTeam("team2", TestPlayer("^<<v>")), address=client2_address) client1.autoplay_process() client2.autoplay_process() server.run() server.shutdown()
# ## # ## #### # ##. . . .# #.. . . #. . #. # # ## ##### # # ## #### #.## # # . . . ..# #.. .. # # # # ##### ##### ##### ##### # # # # .. ..# #.. . . . # # ##.# #### ## # # ##### ## # # .# . .# . . ..# #. . . .## # #### ## # ## # ######### # . # . # #.# # ## # . # # # .# #0##### # # ### # # ## # #2 # # #. # ...# ################################## """ server = SimpleServer(layout_string=layout, rounds=3000) def star_to_localhost(str): # server might publish to tcp://* in which case we simply try localhost for the clients return str.replace("*", "localhost") client = SimpleClient(SimpleTeam("the good ones", NQRandomPlayer(), BFSPlayer()), address=star_to_localhost(server.bind_addresses[0])) client.autoplay_process() client2 = SimpleClient(SimpleTeam("the bad ones", BFSPlayer(), BasicDefensePlayer()), address=star_to_localhost(server.bind_addresses[1])) client2.autoplay_process() server.run() print(server.game_master.universe.pretty) print(server.game_master.game_state)
client2 = SimpleClient(SimpleTeam("the bad ones", BFSPlayer(), BasicDefensePlayer()), address="ipc:///tmp/pelita-client2") client2.autoplay_process() layout = """ ################################## #... # .# # # 3# # ## # # ### # # #####1# #. # # # . # ## # #.# # . # . # ######### # ## # ## #### # ##. . . .# #.. . . #. . #. # # ## ##### # # ## #### #.## # # . . . ..# #.. .. # # # # ##### ##### ##### ##### # # # # .. ..# #.. . . . # # ##.# #### ## # # ##### ## # # .# . .# . . ..# #. . . .## # #### ## # ## # ######### # . # . # #.# # ## # . # # # .# #0##### # # ### # # ## # #2 # # #. # ...# ################################## """ server = SimpleServer(layout_string=layout, rounds=3000, bind_addrs=(client.address, client2.address)) server.run()
def test_remote_game(): remote = [ libpelita.get_python_process(), '-m', 'pelita.scripts.pelita_player', '--remote' ] remote_stopping = remote + [ 'pelita/player/StoppingPlayer', 'tcp://127.0.0.1:52301' ] remote_food_eater = remote + [ 'pelita/player/FoodEatingPlayer', 'tcp://127.0.0.1:52302' ] remote_procs = [ Popen_autokill(args) for args in [remote_stopping, remote_food_eater] ] layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=[ 'remote:tcp://127.0.0.1:52301', 'remote:tcp://127.0.0.1:52302' ]) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 1 server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=[ 'remote:tcp://127.0.0.1:52302', 'remote:tcp://127.0.0.1:52301' ]) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 0 server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=[ 'remote:tcp://127.0.0.1:52301', 'remote:tcp://127.0.0.1:52302' ]) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 1 # terminate processes [p.terminate() for p in remote_procs] for p in remote_procs: assert p.wait(2) is not None
py_proc = sys.executable if not py_proc: raise RuntimeError("Cannot retrieve current Python executable.") return py_proc FORMAT = '[%(asctime)s,%(msecs)03d][%(name)s][%(levelname)s][%(funcName)s]' + MAGENTA + ' %(message)s' + RESET logging.basicConfig(format=FORMAT, datefmt="%H:%M:%S", level=logging.INFO) layout = ( """ ################## #0#. . 2# . 3 # # ##### ##### # # . # . .#1# ################## """) server = SimpleServer(layout_string=layout, rounds=200, bind_addrs=("tcp://*:50007", "tcp://*:50008")) publisher = SimplePublisher("tcp://*:50012") server.game_master.register_viewer(publisher) subscribe_sock = server tk_open = "TkViewer(%r, %r).run()" % ("tcp://localhost:50012", "tcp://localhost:50013") tkprocess = subprocess.Popen([get_python_process(), "-c", "from pelita.ui.tk_viewer import TkViewer\n" + tk_open]) try: print(server.bind_addresses) server.register_teams() controller = SimpleController(server.game_master, "tcp://*:50013") controller.run()
if not py_proc: raise RuntimeError("Cannot retrieve current Python executable.") return py_proc 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.INFO) layout = (""" ################## #0#. . 2# . 3 # # ##### ##### # # . # . .#1# ################## """) server = SimpleServer(layout_string=layout, rounds=200, bind_addrs=("tcp://*:50007", "tcp://*:50008")) publisher = SimplePublisher("tcp://*:50012") server.game_master.register_viewer(publisher) subscribe_sock = server tk_open = "TkViewer(%r, %r).run()" % ("tcp://localhost:50012", "tcp://localhost:50013") tkprocess = subprocess.Popen([ get_python_process(), "-c", "from pelita.ui.tk_viewer import TkViewer\n" + tk_open ]) try: print server.bind_addresses
""" We start a server using the SimpleServer class and a layout given by the specified layout file. """ from pelita.simplesetup import SimpleServer from pelita.layout import get_random_layout layout_name, layout_string = get_random_layout() server = SimpleServer(layout_string=layout_string, layout_name=layout_name) # For more control, we could also define a game with 8 Bots, # run 10000 rounds and want to receive connections on port # # server = SimpleServer(layout_string=layout_string, players=8, rounds=10000, port=61009, layout_name=layout_name) server.run()
def test_remote_game(): remote = [libpelita.get_python_process(), '-m', 'pelita.scripts.pelita_player', '--remote'] remote_stopping = remote + ['pelita/player/StoppingPlayer', 'tcp://127.0.0.1:52301'] remote_food_eater = remote + ['pelita/player/FoodEatingPlayer', 'tcp://127.0.0.1:52302'] remote_procs = [Popen_autokill(args) for args in [remote_stopping, remote_food_eater]] layout = """ ########## # # #0 .. 1# ########## """ server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=['remote:tcp://127.0.0.1:52301', 'remote:tcp://127.0.0.1:52302']) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 1 server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=['remote:tcp://127.0.0.1:52302', 'remote:tcp://127.0.0.1:52301']) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 0 server = SimpleServer(layout_string=layout, rounds=5, players=2, bind_addrs=['remote:tcp://127.0.0.1:52301', 'remote:tcp://127.0.0.1:52302']) server.run() server.shutdown() assert server.game_master.game_state['team_wins'] == 1 # terminate processes [p.terminate() for p in remote_procs] for p in remote_procs: assert p.wait(2) is not None
# -*- coding: utf-8 -*- """ We start a server using the SimpleServer class and a layout given by the specified layout file. """ from pelita.simplesetup import SimpleServer server = SimpleServer(layoutfile="layouts/02.layout") # For more control, we could also define a game with 8 Bots, # run 10000 rounds and want to receive connections on port # # server = SimpleServer(layoutfile="layouts/02.layout", players=8, rounds=10000, port=61009) # Now we run it with a Tk interface. # This will only ever return if interrupted or if Tk is closed. server.run_tk()