Пример #1
0
def create_game(team_id, teams, width, height, turn_time, mode):
    if not teams or len(teams) == 0:
        raise Exception('No teams added. You need at least one...')

    # Create snakes and fetch whois for each
    snakes = [
        Snake(team_id=team.id, url=team.snake_url, name=team.teamname)
        for team in teams
    ]
    _update_snakes(snakes, ai.whois(snakes))

    # Create game
    team_ids = [team.id for team in teams]
    print team_ids
    game = Game(width=width,
                height=height,
                turn_time=turn_time,
                mode=mode,
                team_id=team_id,
                team_ids=team_ids)
    game.insert()

    # Create the first GameState
    game_state = Engine.create_game_state(game.id, game.width, game.height,
                                          game.mode)

    # Init the first GameState
    Engine.add_random_snakes_to_board(game_state, snakes)
    Engine.add_starting_food_to_board(game_state)

    # Notify snakes that we're about to start
    _update_snakes(game_state.snakes, ai.start(game, game_state))

    # Save the first GameState
    game_state.insert()

    if len(snakes) > 1:
        _update_slack(
            game.id, '%d brave snakes enter the grid: %s' %
            (len(snakes), ', '.join([s.name for s in snakes])))

    return game, game_state
Пример #2
0
def next_turn(game):
    game_states = GameState.find({'game_id': game.id})

    if len(game_states) > 0:
        game_state = game_states[0]
        moves = get_moves(game_state, game.turn_time * 5)
        next_game_state = Engine.resolve_moves(game_state, moves)
        next_game_state.insert()

        return next_game_state
    else:
        raise Exception('No GameStates found for %s' % game)
Пример #3
0
def next_turn(game):
    game_states = GameState.find({'game_id': game.id}, limit=1)

    if len(game_states) > 0:
        game_state = game_states[0]
        moves = get_moves(game_state, game.turn_time * 5)
        next_game_state = Engine.resolve_moves(game_state, moves)
        next_game_state.insert()

        return next_game_state
    else:
        raise Exception('No GameStates found for %s' % game)
Пример #4
0
def next_turn(game):
    game_states = GameState.find({'game_id': game.id}, limit=1)
    if not game_states:
        raise Exception('No GameStates found for %s' % game)

    game_state = game_states[0]

    # Update taunts and moves
    _update_snakes(game_state.snakes, ai.move(game, game_state))

    next_game_state = Engine.resolve_moves(game_state)
    next_game_state.insert()

    return next_game_state
Пример #5
0
def create_game(snake_urls, width, height, turn_time):
    game = Game(width=width, height=height, turn_time=turn_time)

    # Fetch snakes
    start_urls = [('%s/start' % url) for url in snake_urls]
    responses = AsyncCall(
        payload={
            'game_id': game.id,
            'width': width,
            'height': height
        },
        urls=start_urls,
        timeout=10  # Enough time for Heroku apps to wake up
    ).start()

    # Any errors?
    for url, response in responses.items():
        if not response:
            raise Exception('%s failed to respond' % url)

        params = ['name', 'color']
        for param in params:
            if param not in response:
                raise Exception('%s missing %s' % (url, param))

    # Build snakes
    snakes = []
    for snake_url in snake_urls:

        # Find the response for that snake
        for url, response in responses.items():

            # We matched! Now handle the response
            if url.startswith(snake_url):

                if response is None:
                    # FREAK OUT
                    raise Exception('failed to contact snake: %s' % url)

                snakes.append({
                    'url':
                    snake_url,
                    'color':
                    response['color'],
                    'head_url':
                    response.get(
                        'head_url',
                        'http://www.battlesnake.io/static/img/default_head.gif'
                    ),
                    'name':
                    response['name'],
                    'taunt':
                    response['taunt']
                })

    game.insert()

    # Create the first GameState
    game_state = Engine.create_game_state(game.id, game.width, game.height)

    # Init the first GameState
    Engine.add_random_snakes_to_board(game_state, snakes)
    Engine.add_starting_food_to_board(game_state)

    # Save the first GameState
    game_state.insert()

    return (game, game_state)
Пример #6
0
def create_game(snake_urls, width, height, turn_time):
    if not snake_urls or len(snake_urls) == 0:
        raise Exception('No snake urls added. You need at least one...')

    game = Game(width=width, height=height, turn_time=turn_time)

    # Fetch snakes
    start_urls = [('%s/start' % url) for url in snake_urls]
    responses = AsyncCall(
        payload={
            'game_id': game.id,
            'width': width,
            'height': height
        },
        urls=start_urls,
        timeout=10  # Enough time for Heroku apps to wake up
    ).start()

    # Any errors?
    for url, response in responses.items():
        if not response:
            raise Exception('%s failed to respond' % url)

        params = ['name', 'color']
        for param in params:
            if param not in response:
                raise Exception('%s missing %s' % (url, param))

    # Build snakes
    snakes = []
    for snake_url in snake_urls:

        # Find the response for that snake
        for url, response in responses.items():

            # We matched! Now handle the response
            if url.startswith(snake_url):

                if response is None:
                    # FREAK OUT
                    raise Exception('failed to contact snake: %s' % url)

                snake = {
                    'url': snake_url,
                    'color': response['color'],
                    'head_url': response.get('head_url', 'http://www.battlesnake.io/static/img/default_head.gif'),
                    'name': response['name'],
                    'taunt': response['taunt']
                }

                if snake in snakes:
                    raise Exception('cannot snake name "%s" more than once' % (snake['name']))

                snakes.append(snake)

    game.insert()

    # Create the first GameState
    game_state = Engine.create_game_state(game.id, game.width, game.height)

    # Init the first GameState
    Engine.add_random_snakes_to_board(game_state, snakes)
    Engine.add_starting_food_to_board(game_state)

    # Save the first GameState
    game_state.insert()

    if (len(snakes) > 1):
        _update_slack(game.id, '%d brave snakes enter the grid: %s' % (
            len(snakes), ', '.join([s['name'] for s in snakes]))
        )

    return (game, game_state)