예제 #1
0
파일: game.py 프로젝트: ValRCS/kfchess
def replay_start():
    data = json.loads(request.data)
    history_id = data['historyId']
    print 'replay start', data

    game_history = db_service.get_game_history(history_id)
    if game_history is None:
        return json.dumps({
            'success': False,
            'message': 'Replay does not exist.',
        })

    # create game and add to game states
    replay = Replay.from_json_obj(game_history.replay)
    if replay.players[2].startswith('c'):
        level = int(replay.players[2][2:])
        campaign_level = campaign.get_level(level)
        game = Game(Speed(replay.speed),
                    replay.players,
                    board=Board.from_str(campaign_level.board),
                    is_campaign=True)
    else:
        game = Game(Speed(replay.speed), replay.players)
    for player in replay.players:
        game.mark_ready(player)

    game_id = generate_game_id()
    game_states[game_id] = GameState(game_id, game, {}, {}, replay)

    return json.dumps({
        'success': True,
        'gameId': game_id,
    })
예제 #2
0
def reset(data):
    data = json.loads(data)
    game_id = data['gameId']
    player_key = data['playerKey']
    print 'reset', data

    if game_id not in game_states:
        emit('notfound')
        return

    game_state = game_states[game_id]
    auth_player = get_auth_player(game_state, player_key)

    # can't reset an in-progress multiplayer game
    if game_state.level is None and not game_state.game.finished:
        return

    # only authenticated players can reset game
    if auth_player is not None:
        db_service.remove_active_game(context.SERVER, game_id)

        board = None
        if game_state.level is not None:
            campaign_level = campaign.get_level(game_state.level)
            board = Board.from_str(campaign_level.board)

        old_game = game_state.game
        game = Game(old_game.speed,
                    old_game.players,
                    num_players=old_game.num_players,
                    board=board,
                    is_campaign=old_game.is_campaign,
                    debug=old_game.debug)
        for player in game_state.bots:
            game.mark_ready(player)
        game_state.game = game

        emit('resetack', {
            'game': game.to_json_obj(),
        },
             room=game_id,
             json=True)
예제 #3
0
파일: game.py 프로젝트: ValRCS/kfchess
def campaign_start():
    data = json.loads(request.data)
    level = data['level']
    print 'campaign start', data

    if not current_user.is_authenticated:
        return json.dumps({
            'success': False,
            'message': 'User is not logged in.',
        })

    # check if user is already in game
    user_id = current_user.user_id
    user = db_service.get_user_by_id(user_id)
    if user.current_game:
        game_id = user.current_game['gameId']
        if game_id in game_states:
            game_state = game_states[game_id]
            if not game_state.game.started or game_state.game.finished:
                del game_states[game_id]
            else:
                return json.dumps({
                    'success': False,
                    'message': 'User already in game.',
                })

    # check that user has access to this level
    progress = db_service.get_campaign_progress(user_id)
    belt = level / 8 + 1
    if belt > 1 and not progress.belts_completed[str(belt - 1)]:
        return json.dumps({
            'success':
            False,
            'message':
            'User does not have access to this level.',
        })

    # create game and add to game states
    campaign_level = campaign.get_level(level)
    players = {1: 'u:%s' % user_id, 2: 'c:%s' % level}
    game = Game(Speed(campaign_level.speed),
                players,
                board=Board.from_str(campaign_level.board),
                is_campaign=True)
    game.mark_ready(2)

    game_id = generate_game_id()
    player_keys = {1: str(uuid.uuid4())}
    bots = {2: ai.get_bot('campaign')}
    game_states[game_id] = GameState(game_id,
                                     game,
                                     player_keys,
                                     bots,
                                     level=level)

    # update user current game
    db_service.update_user_current_game(user_id, game_id, player_keys[1])

    return json.dumps({
        'success': True,
        'gameId': game_id,
        'playerKeys': player_keys,
    })