Пример #1
0
def create_game(args):
    '''
    Create all the semi-permanent game structures (i.e. sockets and dockers and
    stuff
    '''

    # Load the Game state info
    game = server.Game(logging_level=logging.ERROR,
                       game_map=args['map'],
                       time_pool=args['time_pool'],
                       time_additional=args['time_additional'],
                       terminal_viewer=args['terminal_viewer'],
                       extra_delay=args['extra_delay'],
                       map_name=args['map_name'])

    working_dir = abspath("working_dir")
    prepare_working_directory(working_dir)

    # pick server location
    if 'USE_TCP' in os.environ or sys.platform == 'win32':
        print('Running game server on port tcp://localhost:16148')
        # int indicates tcp
        sock_file = ('localhost', 16148)
    else:
        # Find a good filename to use as socket file
        for index in range(10000):
            sock_file = "/tmp/battlecode-" + str(index)
            if not os.path.exists(sock_file):
                break
        else:
            raise Exception(
                "Do you really have 10000 /tmp/battlecode sockets???")
        print('Running game server on socket unix://{}'.format(sock_file))

    # Assign the docker instances client ids
    dockers = {}
    for index in range(len(game.players)):
        key = [player['id'] for player in game.players][index]
        local_dir = args['dir_p1' if index % 2 == 0 else 'dir_p2']
        if args['docker']:
            # Note, importing a module multiple times is ok in python.
            # It might take a bit of time to verify that it is already imported though, but that should be negligable.
            import docker
            docker_instance = docker.from_env()
            dockers[key] = SandboxedPlayer(sock_file,
                                           working_dir=working_dir,
                                           docker_client=docker_instance,
                                           player_key=key,
                                           local_dir=local_dir)
        else:
            dockers[key] = PlainPlayer(sock_file,
                                       working_dir=working_dir,
                                       player_key=key,
                                       local_dir=local_dir)

    return (game, dockers, sock_file)
Пример #2
0
def create_scrimmage_game(args):
    '''
    Create all the semi-permanent game structures (i.e. sockets and dockers and
    stuff
    '''
    args['replay_filename'] = 'k'
    # Load the Game state info
    game = server.Game(logging_level=logging.ERROR,
                       game_map=args['map'],
                       time_pool=int(os.environ['TIME_POOL']),
                       time_additional=int(os.environ['TIME_ADDITIONAL']),
                       terminal_viewer=False,
                       extra_delay=0)

    working_dir = abspath("working_dir")
    prepare_working_directory(working_dir)

    # Find a good filename to use as socket file
    for index in range(10000):
        sock_file = "/tmp/battlecode-" + str(index)
        if not os.path.exists(sock_file):
            break

    # Assign the docker instances client ids
    import docker
    docker_instance = docker.from_env()
    dockers = {}
    for index in range(len(game.players)):
        key = [player['id'] for player in game.players][index]
        dockers[key] = SandboxedPlayer(sock_file,
                                       player_key=key,
                                       s3_bucket=args['s3_bucket'],
                                       s3_key=args['red_key' if index %
                                                   2 == 0 else 'blue_key'],
                                       docker_client=docker_instance,
                                       working_dir=working_dir)

    return (game, dockers, sock_file)