Exemplo n.º 1
0
def authorized(data):
    next_url = request.args.get('state') or url_for('index')
    print 'oauth authorized', request.args

    if current_user.is_authenticated:
        return redirect(next_url)

    # store access token in the session
    access_token = data['access_token']
    session['access_token'] = access_token, ''

    # get user's email from google
    headers = {'Authorization': 'OAuth ' + access_token}
    response = requests.get('https://www.googleapis.com/plus/v1/people/me', headers=headers)
    if response.status_code == 200:
        user_data = response.json()

        print 'user data', user_data

        email = [e for e in user_data['emails'] if e['type'] == 'ACCOUNT'][0]['value']
        user = db_service.get_user_by_email(email)
        if user is None:
            # create user with random username
            username = random_username()
            while db_service.get_user_by_username(username) is not None:
                username = random_username()

            user = db_service.create_user(email, username, None, {})

        login_user(user)
    else:
        print 'error getting google info', response.status_code, response.text

    return redirect(next_url)
Exemplo n.º 2
0
def new():
    data = json.loads(request.data)
    speed = data['speed']
    bots = data.get('bots', {})
    bots = {int(player): ai.get_bot(difficulty) for player, difficulty in bots.iteritems()}
    username = data.get('username')
    print 'new game', data

    # generate game ID and player keys
    game_id = generate_game_id()
    player_keys = {i: str(uuid.uuid4()) for i in xrange(1, 3) if i not in bots}

    # if logged in, add current user to game
    players = {i: 'b:%s' % bot.difficulty for i, bot in bots.iteritems()}
    if current_user.is_authenticated:
        players[1] = 'u:%s' % current_user.user_id
        db_service.update_user_current_game(current_user.user_id, game_id, player_keys[1])

    # check opponent
    if username is not None:
        user = db_service.get_user_by_username(username)
        if user is None:
            return json.dumps({
                'success': False,
                'message': 'User to invite does not exist.',
            })

        if user.current_game is not None:
            return json.dumps({
                'success': False,
                'message': 'User to invite is already in a game.',
            })

        players[2] = 'u:%s' % user.user_id
        db_service.update_user_current_game(user.user_id, game_id, player_keys[2])

        socketio.emit('invite', '', room=str(user.user_id))

    for i in xrange(1, 3):
        if i not in players:
            players[i] = 'o'

    # create game and add to game states
    game = Game(Speed(speed), players)
    for player in bots:
        game.mark_ready(player)

    game_states[game_id] = GameState(game_id, game, player_keys, bots)

    return json.dumps({
        'success': True,
        'gameId': game_id,
        'playerKeys': player_keys,
    })
Exemplo n.º 3
0
def invite():
    data = json.loads(request.data)
    game_id = data['gameId']
    player = data['player']
    username = data['username']
    print 'invite', data

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

    if game_id not in game_states:
        return json.dumps({
            'success': False,
            'message': 'Game does not exist.',
        })

    game_state = game_states[game_id]
    game = game_state.game
    if game.players[player] != 'o':
        return json.dumps({
            'success': False,
            'message': 'Player position is already filled.',
        })

    if ('u:%s' % current_user.user_id) not in game.players.values():
        return json.dumps({
            'success': False,
            'message': 'User is not in the game.',
        })

    user = db_service.get_user_by_username(username)
    if user is None:
        return json.dumps({
            'success': False,
            'message': 'User to invite does not exist.',
        })

    if user.current_game is not None:
        return json.dumps({
            'success': False,
            'message': 'User to invite is already in a game.',
        })

    # generate a new player key (for security)
    new_key = str(uuid.uuid4())
    game_state.player_keys[player] = new_key
    game.players[player] = 'u:%s' % user.user_id
    db_service.update_user_current_game(user.user_id, game_id, new_key)

    socketio.emit('invite', '', room=str(user.user_id))

    socketio.emit('inviteack', {
        'game': game.to_json_obj(),
    },
                  room=game_id,
                  json=True)

    return json.dumps({
        'success': True,
    })