示例#1
0
def main():
    config = {
        "publish-addr": None,
        "controller-addr": None,
        "viewers": [],
        "external-viewers": []
    }

    args = parser.parse_args()
    if args.help or args.long_help:
        parser.print_help()
        sys.exit(0)

    if args.version:
        if pelita._git_version:
            print("Pelita {} (git: {})".format(pelita.__version__, pelita._git_version))
        else:
            print("Pelita {}".format(pelita.__version__))
        sys.exit(0)

    if args.list_layouts:
        layouts = pelita.layout.get_available_layouts()
        print('\n'.join(layouts))
        sys.exit(0)

    if args.viewer.startswith('tk') and not args.publish_to:
        raise ValueError("Options --tk (or --tk-no-sync) and --no-publish are mutually exclusive.")

    if args.log:
        libpelita.start_logging(args.log)

    if args.check_team:
        if not args.team_specs:
            raise ValueError("No teams specified.")
        for team_spec in args.team_specs:
            team_name = libpelita.check_team(libpelita.prepare_team(team_spec))
            print("NAME:", team_name)
        sys.exit(0)


    viewers = []
    if args.dump:
        viewers.append(pelita.viewer.DumpingViewer(open(args.dump, "w")))
    if args.viewer == 'ascii':
        viewers.append(pelita.viewer.AsciiViewer())
    if args.viewer == 'progress':
        viewers.append(pelita.viewer.ProgressViewer())
    if args.reply_to:
        viewers.append(pelita.viewer.ReplyToViewer(args.reply_to))
    if args.viewer == 'null':
        pass

    # Adding the result printer to the viewers.
    viewers.append(ResultPrinter())

    if args.replayfile:
        if not args.viewer == 'tk':
            raise RuntimeError("Can only replay with the tk viewer.")

        with libpelita.channel_setup(publish_to=args.publish_to) as channels:
            geometry = args.geometry
            delay = int(1000./args.fps)
            controller = channels["controller"]
            controller_addr = controller.socket_addr
            publisher = channels["publisher"]
            viewer = libpelita.run_external_viewer(publisher.socket_addr, controller_addr,
                                                    geometry=geometry, delay=delay, stop_after=args.stop_after)
            replay_publisher = ReplayPublisher(args.replayfile, publisher, controller)
            replay_publisher.run()
        sys.exit(0)

    # Run a normal game
    num_teams = 2
    team_specs = args.team_specs
    if len(team_specs) == 0:
        team_specs = ('0', '1')
    if len(team_specs) == 1:
        raise RuntimeError("Not enough teams given. Must be {}".format(num_teams))
    if len(team_specs) > num_teams:
        raise RuntimeError("Too many teams given. Must be < {}.".format(num_teams))

    if args.seed is None:
        seed = random.randint(0, sys.maxsize)
        args.seed = seed
        print("Replay this game with --seed {seed}".format(seed=seed))
    else:
        pass
    random.seed(args.seed)

    if args.layout or args.layoutfile:
        layout_name, layout_string = pelita.layout.load_layout(layout_name=args.layout, layout_file=args.layoutfile)
    else:
        layout_name, layout_string = pelita.layout.get_random_layout(args.filter)
    print("Using layout '%s'" % layout_name)

    game_config = {
        "rounds": args.rounds,
        "max_timeouts": args.max_timeouts,
        "timeout_length": args.timeout_length,
        "layout_name": layout_name,
        "layout_string": layout_string,
        "seed": args.seed,
        "dump": args.dump,
    }

    with libpelita.channel_setup(publish_to=args.publish_to) as channels:
        if args.viewer.startswith('tk'):
            geometry = args.geometry
            delay = int(1000./args.fps)
            controller = channels["controller"]
            controller_addr = controller.socket_addr
            publisher = channels["publisher"]
            game_config["publisher"] = publisher
            if args.viewer == 'tk-no-sync':
                controller = None
                controller_addr = None
            viewer = libpelita.run_external_viewer(publisher.socket_addr, controller_addr,
                                                   geometry=geometry, delay=delay, stop_after=args.stop_after)
        else:
            controller = None

        libpelita.run_game(team_specs=team_specs, game_config=game_config, viewers=viewers, controller=controller)
示例#2
0
def main():
    config = {
        "publish-addr": None,
        "controller-addr": None,
        "viewers": [],
        "external-viewers": []
    }

    args = parser.parse_args()
    if args.help:
        parser.print_help()
        sys.exit(0)
    if args.version:
        if pelita._git_version:
            print("Pelita {} (git: {})".format(pelita.__version__,
                                               pelita._git_version))
        else:
            print("Pelita {}".format(pelita.__version__))

        sys.exit(0)
    if args.layout == 'list':
        layouts = pelita.layout.get_available_layouts()
        print('\n'.join(layouts))
        sys.exit(0)

    if args.list_teams:
        for player in default_players():
            print(player.__name__)
        sys.exit(0)

    if args.seed is None:
        seed = random.randint(0, sys.maxsize)
        args.seed = seed
        print("Replay this game with --seed {seed}".format(seed=seed))
    else:
        pass
    random.seed(args.seed)

    if args.viewer.startswith('tk') and not args.publish_to:
        raise ValueError(
            "Options --tk (or --tk-no-sync) and --no-publish are mutually exclusive."
        )

    try:
        pelita.utils.start_logging(args.log)
    except AttributeError:
        pass

    if args.check_team:
        if not args.team_specs:
            raise ValueError("No teams specified.")
        for team_spec in args.team_specs:
            team_name = libpelita.check_team(libpelita.prepare_team(team_spec))
            print("NAME:", team_name)
        sys.exit(0)

    try:
        # TODO: Re-include the dump.
        dump = args.dump or 'pelita.dump'
    except AttributeError:
        dump = None

    try:
        replayfile = args.replay or 'pelita.dump'
    except AttributeError:
        replayfile = None

    if replayfile:
        replay_publisher = ReplayPublisher(args.publish_to, replayfile)
        config["publish-addr"] = replay_publisher.publisher.socket_addr
        subscribe_sock = replay_publisher.publisher.socket_addr.replace(
            '*', 'localhost')

        if args.viewer.startswith("tk"):
            args.viewer = "tk-no-sync"
        runner = replay_publisher.run
    else:
        if args.layout or args.layoutfile:
            layout_name, layout_string = pelita.layout.load_layout(
                layout_name=args.layout, layout_file=args.layoutfile)
        else:
            layout_name, layout_string = pelita.layout.get_random_layout(
                args.filter)
        print("Using layout '%s'" % layout_name)

        num_teams = 2
        team_specs = args.team_specs
        while len(team_specs) < num_teams:
            team_specs.append("random")
        if len(team_specs) > num_teams:
            raise RuntimeError(
                "Too many teams given. Must be < {}.".format(num_teams))

        if args.dry_run:
            sys.exit(0)

        if args.viewer == 'tk-no-sync':
            # only use delay when not synced.
            initial_delay = 0.5
        else:
            initial_delay = 0.0

        game_config = {
            "rounds": args.rounds,
            "initial_delay": initial_delay,
            "max_timeouts": args.max_timeouts,
            "timeout_length": args.timeout_length,
            "layout_name": layout_name,
            "layout_string": layout_string,
            "seed": args.seed,
        }

        viewers = []
        if dump:
            viewers.append(pelita.viewer.DumpingViewer(open(dump, "w")))
        if args.viewer == 'ascii':
            viewers.append(pelita.viewer.AsciiViewer())
        if args.viewer == 'progress':
            viewers.append(pelita.viewer.ProgressViewer())
        if args.reply_to:
            viewers.append(pelita.viewer.ReplyToViewer(args.reply_to))
        if args.viewer == 'null':
            pass

        # Adding the result printer to the viewers.
        viewers.append(ResultPrinter())

        with libpelita.channel_setup(publish_to=args.publish_to) as channels:
            if args.viewer.startswith('tk'):
                geometry = args.geometry
                delay = int(1000. / args.fps)
                controller = channels["controller"]
                publisher = channels["publisher"]
                game_config["publisher"] = publisher
                viewer = libpelita.run_external_viewer(publisher.socket_addr,
                                                       controller.socket_addr,
                                                       geometry=geometry,
                                                       delay=delay)
                libpelita.run_game(team_specs=team_specs,
                                   game_config=game_config,
                                   viewers=viewers,
                                   controller=controller)
            else:
                libpelita.run_game(team_specs=team_specs,
                                   game_config=game_config,
                                   viewers=viewers)
示例#3
0
def main():
    config = {
        "publish-addr": None,
        "controller-addr": None,
        "viewers": [],
        "external-viewers": []
    }

    args = parser.parse_args()
    if args.help or args.long_help:
        parser.print_help()
        sys.exit(0)

    if args.version:
        if pelita._git_version:
            print("Pelita {} (git: {})".format(pelita.__version__,
                                               pelita._git_version))
        else:
            print("Pelita {}".format(pelita.__version__))
        sys.exit(0)

    if args.list_layouts:
        layouts = pelita.layout.get_available_layouts()
        print('\n'.join(layouts))
        sys.exit(0)

    if args.viewer.startswith('tk') and not args.publish_to:
        raise ValueError(
            "Options --tk (or --tk-no-sync) and --no-publish are mutually exclusive."
        )

    if args.log:
        libpelita.start_logging(args.log)

    if args.check_team:
        if not args.team_specs:
            raise ValueError("No teams specified.")
        for team_spec in args.team_specs:
            team_name = libpelita.check_team(libpelita.prepare_team(team_spec))
            print("NAME:", team_name)
        sys.exit(0)

    viewers = []
    if args.dump:
        viewers.append(pelita.viewer.DumpingViewer(open(args.dump, "w")))
    if args.viewer == 'ascii':
        viewers.append(pelita.viewer.AsciiViewer())
    if args.viewer == 'progress':
        viewers.append(pelita.viewer.ProgressViewer())
    if args.reply_to:
        viewers.append(pelita.viewer.ReplyToViewer(args.reply_to))
    if args.viewer == 'null':
        pass

    # Adding the result printer to the viewers.
    viewers.append(ResultPrinter())

    if args.replayfile:
        if not args.viewer == 'tk':
            raise RuntimeError("Can only replay with the tk viewer.")

        with libpelita.channel_setup(publish_to=args.publish_to) as channels:
            geometry = args.geometry
            delay = int(1000. / args.fps)
            controller = channels["controller"]
            controller_addr = controller.socket_addr
            publisher = channels["publisher"]
            viewer = libpelita.run_external_viewer(publisher.socket_addr,
                                                   controller_addr,
                                                   geometry=geometry,
                                                   delay=delay,
                                                   stop_after=args.stop_after)
            replay_publisher = ReplayPublisher(args.replayfile, publisher,
                                               controller)
            replay_publisher.run()
        sys.exit(0)

    # Run a normal game
    num_teams = 2
    team_specs = args.team_specs
    if len(team_specs) == 0:
        team_specs = ('0', '1')
    if len(team_specs) == 1:
        raise RuntimeError(
            "Not enough teams given. Must be {}".format(num_teams))
    if len(team_specs) > num_teams:
        raise RuntimeError(
            "Too many teams given. Must be < {}.".format(num_teams))

    if args.seed is None:
        seed = random.randint(0, sys.maxsize)
        args.seed = seed
        print("Replay this game with --seed {seed}".format(seed=seed))
    else:
        pass
    random.seed(args.seed)

    if args.layout or args.layoutfile:
        layout_name, layout_string = pelita.layout.load_layout(
            layout_name=args.layout, layout_file=args.layoutfile)
    else:
        layout_name, layout_string = pelita.layout.get_random_layout(
            args.filter)

    print("Using layout '%s'" % layout_name)

    with libpelita.channel_setup(publish_to=args.publish_to) as channels:
        if args.viewer.startswith('tk'):
            geometry = args.geometry
            delay = int(1000. / args.fps)
            controller = channels["controller"]
            controller_addr = controller.socket_addr
            publisher = channels["publisher"]
            if args.viewer == 'tk-no-sync':
                controller = None
                controller_addr = None
            viewer = libpelita.run_external_viewer(publisher.socket_addr,
                                                   controller_addr,
                                                   geometry=geometry,
                                                   delay=delay,
                                                   stop_after=args.stop_after)
        else:
            controller = None
            publisher = None

        libpelita.run_game(team_specs=team_specs,
                           rounds=args.rounds,
                           layout=layout_string,
                           layout_name=layout_name,
                           seed=args.seed,
                           dump=args.dump,
                           max_timeouts=args.max_timeouts,
                           timeout_length=args.timeout_length,
                           viewers=viewers,
                           controller=controller,
                           publisher=publisher)