Exemplo n.º 1
0
def __load_or_generate_config(config_file: Optional[str]) -> dict:
    ## Setting up the players using the config file

    if config_file:
        # A custom config file location given:
        try:
            with open(config_file) as f:
                config_data = jsonplus.loads(f.read())
        except:  # Failed to load config, fallback to default values
            logger.error(
                f"config file '{config_file}' not found, using default value")
            raise
    else:
        # Default config file expected:
        config_dir = user_config_dir(APP_NAME)
        config_file = os.path.join(config_dir, DEFAULT_CONFIG_FILE)

        try:
            with open(config_file) as f:
                config_data = jsonplus.loads(f.read())
        except FileNotFoundError:  # Failed to load config, fallback to default values
            logger.warning(f"No default config file found, generating...")
            config_data = {
                "headless": False,
                "interactive": False,
                "start_paused": True,
                "wait_end": 5,
                "max_iterations": ITERATION_LIMIT,
                "tick_step": TICK_STEP
            }

            os.makedirs(config_dir, exist_ok=True)
            logger.warning(f"Writing default config into: {config_file}")
            with open(config_file, "w") as f:
                f.write(jsonplus.pretty(config_data))

    config_data.setdefault('start_paused', False)
    config_data.setdefault('wait_end', 10)
    config_data.setdefault('assets', ASSET_DIRECTORY)
    config_data.setdefault('interactive', False)
    config_data.setdefault('tick_step', TICK_STEP)
    config_data.setdefault(
        'no_text', False)  # A work around Pillow (Python image library) bug
    config_data.setdefault('single_step', False)
    config_data.setdefault('endless', False)

    config_data.setdefault('rows', Game.ROW_COUNT)
    config_data.setdefault('columns', Game.COLUMN_COUNT)
    config_data.setdefault('max_iterations', ITERATION_LIMIT)

    return config_data
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description=SCREEN_TITLE)

    parser.add_argument('--headless',
                        action='store_true',
                        default=False,
                        help='run without graphics')
    parser.add_argument('--interactive',
                        action='store_true',
                        default=False,
                        help='all a user to contol a player')
    parser.add_argument('--no_text',
                        action='store_true',
                        default=False,
                        help='Graphics bug workaround - disables all text')
    parser.add_argument('--start_paused',
                        action='store_true',
                        default=False,
                        help='Start a game in pause mode, only if interactive')
    parser.add_argument('--players',
                        type=str,
                        help="Comma-separated list of player names")
    parser.add_argument('--hack',
                        action='store_true',
                        default=False,
                        help=argparse.SUPPRESS)

    parser.add_argument(
        '--submit',
        action='store_true',
        default=False,
        help=
        "Don't run the game, but submit the agent as team entry into the trournament"
    )

    parser.add_argument('--record', type=str, help='file name to record game')
    parser.add_argument('--watch',
                        action='store_true',
                        default=False,
                        help='automatically reload agents on file changes')
    parser.add_argument('--config',
                        type=str,
                        default=None,
                        help='path to the custom config file')

    parser.add_argument("agents", nargs="+", help="agent module")

    args = parser.parse_args()

    n_agents = len(args.agents)
    if args.submit:
        if n_agents > 1:
            print(
                "Error: Only a single agent entry per team is allowed.\n"
                f"You have specified {n_agents} agent modules.\n"
                "Please chose only one you wish submit and try again.\n",
                file=sys.stderr)
            sys.exit(1)

        submit(agent_module=args.agents[0])
        sys.exit(0)

    if len(args.agents) < 2 and (args.headless or not args.interactive):
        print("At least 2 agents must be provided in the match mode. Exiting",
              file=sys.stderr)
        sys.exit(1)

    if args.headless and args.interactive:
        print("Interactive play is not support in headless mode. Exiting",
              file=sys.stderr)
        sys.exit(1)
    if args.headless and args.no_text:
        print("Makes no sense to run headless and ask for no-text. Ignoring",
              file=sys.stderr)
    if not args.interactive and args.start_paused:
        print("Can not start paused in non-interactive mode. Exiting",
              file=sys.stderr)
        sys.exit(1)

    jsonplus.prefer_compat()

    players = args.players.split(',') if args.players else None
    result = run_match(agents=args.agents,
                       players=players,
                       config_name=args.config,
                       record_file=args.record,
                       watch=args.watch,
                       args=args)
    print(jsonplus.pretty(result))

    # We done here, all good.
    sys.exit(0)
Exemplo n.º 3
0
 def pretty(obj, **kw):
     return jsonplus.pretty(obj, **kw)
Exemplo n.º 4
0
 def test_basic_pretty(self):
     self.assertEqual(json.pretty(self.basic, sort_keys=True), self.basic_pretty)