def test_from_str_invalid_tile(self):
     data = """
         WWW
         WxW
         WWW
     """
     with self.assertRaises(Exception):
         GameConfig.from_str(data)
 def test_from_str_non_square(self):
     data = """
         WWW
         W  W
         WWW
     """
     with self.assertRaises(Exception):
         GameConfig.from_str(data)
    def test_with_direction(self):
        data = """
               WWWWWW
               W1  3W
               WD  DW
               WD  DW
               W4  2W
               WWWWWW\r\n
           """
        gc = GameConfig.from_str(data)

        self.assertEqual(
            gc.spawn_positions,
            [Position(1, 1),
             Position(4, 4),
             Position(4, 1),
             Position(1, 4)])
        self.assertEqual(
            gc.spawn_directions,
            [
                Direction(Direction.DOWN),
                Direction(Direction.UP),
                Direction(Direction.DOWN),
                Direction(Direction.UP)
            ],
        )
    def test_from_str(self):
        data = """
            WWWWW
            W!%$W
            W1 3W
            W4W2W
            WWWWW\r\n
        """
        gc = GameConfig.from_str(data)

        self.assertEqual(gc.game_map.size, 5)
        self.assertEqual(gc.game_map.get_tile(Position(1, 1)),
                         (GameMap.BLACK_HOLE, None))
        self.assertEqual(gc.game_map.get_tile(Position(2, 1)),
                         (GameMap.PLANET, None))
        self.assertEqual(gc.game_map.get_tile(Position(3, 1)),
                         (GameMap.BLITZIUM, None))
        self.assertEqual(gc.game_map.get_tile(Position(2, 2)),
                         (GameMap.EMPTY, None))
        self.assertEqual(gc.game_map.get_tile(Position(2, 3)),
                         (GameMap.ASTEROIDS, None))
        self.assertEqual(
            gc.spawn_positions,
            [Position(1, 2),
             Position(3, 3),
             Position(3, 2),
             Position(1, 3)])
Beispiel #5
0
    def test_add_player_should_respect_orientation(self):
        gc = GameConfig(GameMap(20),
                        spawn_positions=[Position(1, 1)],
                        spawn_directions=[Direction(Direction.LEFT)])
        gs = GameState(gc, players=None)
        self.assertEqual(len(gs.players), 0)
        gs.add_player("Karl Marx")

        self.assertEqual(Direction(Direction.LEFT), gs.players[0].direction)
Beispiel #6
0
    def test_add_player_with_game_config(self):
        game_config = GameConfig.from_str("WWWW\nW 2W\nW 1W\nWWWW")
        gs = GameState(game_config)
        self.assertEqual(len(gs.players), 0)

        ps0 = gs.add_player("0")
        self.assertEqual(Position(2, 2), ps0.spawn_position)

        ps1 = gs.add_player("1")
        self.assertEqual(Position(2, 1), ps1.spawn_position)

        with self.assertRaises(Exception):
            gs.add_player("2")
    def test_game_server_with_config(self):
        from pathlib import Path

        root_path = Path(__file__).parent.parent.parent.parent
        file_path = (root_path / "game_presets/0-basic_4p_21x21.txt").resolve()

        gc = GameConfig.from_file(file_path)
        server = SocketGameServer(max_nb_ticks=10,
                                  max_nb_players=100,
                                  game_config=gc,
                                  path=path,
                                  port=port)
        self.assertEqual(21, server.game_map.size)
        self.assertEqual(4, server.max_nb_players)
Beispiel #8
0
async def start() -> None:
    # Start the game
    if args.game_config is not None:
        root_path = Path(__file__).parent.parent.parent
        file_path = (root_path /
                     f"server/game_presets/{args.game_config}").resolve()
        game_config = GameConfig.from_file(file_path)

        game = SocketGameServer(
            max_nb_ticks=args.max_nb_ticks,
            min_nb_players=args.min_nb_players,
            max_nb_players=args.max_nb_players,
            start_delay_timeout=args.start_delay_timeout,
            team_names_by_token=json.loads(args.team_names_by_token)
            if args.team_names_by_token is not None else None,
            game_config=game_config,
            record_path=args.record_path,
            s3_bucket=args.s3_bucket,
            s3_path=args.s3_path,
            path=args.server_address,
            game_delay=args.game_delay,
            log_file=args.log_file,
            move_timeout=args.move_timeout,
        )
    else:
        game = SocketGameServer(
            max_nb_ticks=args.max_nb_ticks,
            min_nb_players=args.min_nb_players,
            max_nb_players=args.max_nb_players,
            start_delay_timeout=args.start_delay_timeout,
            team_names_by_token=json.loads(args.team_names_by_token)
            if args.team_names_by_token is not None else None,
            record_path=args.record_path,
            s3_bucket=args.s3_bucket,
            s3_path=args.s3_path,
            path=args.server_address,
            game_delay=args.game_delay,
            log_file=args.log_file,
            move_timeout=args.move_timeout,
        )

    await game.start()
    winner = await game.wait_for_game_to_finish()
    print(winner)
    print(
        "\nSleeping a bit, to give a chance to the participant to upload their stuff.\n"
    )
    time.sleep(2)
Beispiel #9
0
 def test_create_with_game_config(self):
     game_config = GameConfig.from_str("WWWW\nW 2W\nW 1W\nWWWW")
     gs = GameState(game_config)
     self.assertEqual(4, gs.game_map.size)
     self.assertEqual(2, len(gs.game_config.spawn_positions))
 def test_from_file(self):
     file_path = (root_path / "game_presets/0-basic_4p_21x21.txt").resolve()
     gc = GameConfig.from_file(file_path)
     self.assertEqual(21, gc.game_map.size)
     self.assertEqual(4, len(gc.spawn_positions))