예제 #1
0
def maybe_run_game():
    game_to_run = Game.find_one({'state': Game.STATE_READY})

    if game_to_run:
        _log("running game: %s" % game_to_run.id)
        controller.run_game(game_to_run)
        _log("finished game: %s" % game_to_run.id)
예제 #2
0
def games_list():
    games = Game.find({'is_live': True}, limit=20)
    data = []
    for game in games:
        obj = game.to_dict()
        data.append(obj)

    return _json_response(data)
예제 #3
0
def rematch_game(game_id):
    game = Game.find_one({'_id': game_id})
    game_state = GameState.find({'game_id': game.id}, limit=1)[0]

    snake_urls = []
    for snake in game_state.snakes + game_state.dead_snakes:
        snake_urls.append(snake['url'])

    return create_game(snake_urls, game.width, game.height, game.turn_time)[0]
예제 #4
0
def rematch_game(game_id):
    game = Game.find_one({'_id': game_id})
    game_state = GameState.find({'game_id': game.id}, limit=1)[0]

    snake_urls = []
    for snake in game_state.snakes + game_state.dead_snakes:
        snake_urls.append(snake['url'])

    return create_game(snake_urls, game.width, game.height, game.turn_time)[0]
예제 #5
0
def start_game(game_id, manual):
    game = Game.find_one({'_id': game_id})

    if not game:
        raise Exception('Could not find game %s' % game_id)

    if manual:
        game.state = Game.STATE_MANUAL
        game.save()
    else:
        game.state = Game.STATE_READY
        game.save()

    return game
예제 #6
0
def start_game(game_id, manual):
    game = Game.find_one({'_id': game_id})

    if not game:
        raise Exception('Could not find game %s' % game_id)

    if manual:
        game.state = Game.STATE_MANUAL
        game.save()
    else:
        game.state = Game.STATE_READY
        game.save()

    return game
예제 #7
0
def games_list():
    games = Game.find(
        {
            'is_live': True,
            'state': {
                '$in': [Game.STATE_PLAYING, Game.STATE_DONE]
            }
        },
        limit=50)
    data = []
    for game in games:
        obj = game.to_dict()
        data.append(obj)

    return _json_response(data)
예제 #8
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)
예제 #9
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)
예제 #10
0
def game_resume(game_id):
    game = Game.find_one({'_id': game_id})
    game.state = Game.STATE_READY
    game.save()
    return _json_response(game.to_dict())
예제 #11
0
def game_pause(game_id):
    game = Game.find_one({'_id': game_id})
    game.state = Game.STATE_PAUSED
    game.save()
    return _json_response(game.to_dict())
예제 #12
0
def game_details(game_id):
    game = Game.find_one({'_id': game_id})
    return _json_response(game.to_dict())
예제 #13
0
def game_turn(game_id):
    game = Game.find_one({'_id': game_id})
    game_state = controller.next_turn(game)

    return _json_response(game_state.to_dict())