Ejemplo n.º 1
0
def lobby():
    form = UserSearchForm()
    if form.validate_on_submit():
        return redirect(
            url_for('profile.overview',
                    username=form.username.data).replace('%40', '@'))

    player_names = {}
    games_in_progress = Game.query.filter(((Game.player1 == current_user.id) | (Game.player2 == current_user.id)) & \
                                          (Game.status == 'started')).order_by(desc(Game.id)).all()
    for game in games_in_progress:
        if game.player1 and game.player1 not in player_names:
            player_names[game.player1] = User.query.with_entities(User.username) \
                .filter_by(id=game.player1).first_or_404()[0]
        if game.player2 and game.player2 not in player_names:
            player_names[game.player2] = User.query.with_entities(User.username) \
                .filter_by(id=game.player2).first_or_404()[0]

    # get challenges
    challenges = Game.query.filter_by(status='challenged',
                                      player2=current_user.id).all()
    for game in challenges:
        player_names[game.player1] = User.query.with_entities(User.username) \
            .filter_by(id=game.player1).first_or_404()[0]
    challenges = [r.as_dict() for r in challenges]

    # get friend requests
    friend_requests = FriendshipRequest.query.filter_by(
        receiving_user_id=current_user.id).all()
    for friend_request in friend_requests:
        if friend_request.requesting_user_id not in player_names:
            player_names[friend_request.requesting_user_id] = User.query.with_entities(User.username) \
                .filter_by(id=friend_request.requesting_user_id).first_or_404()[0]

    # get online friends
    friend_query1 = Friendship.query.with_entities(
        Friendship.user2_id).filter_by(user1_id=current_user.id)
    friend_query2 = Friendship.query.with_entities(
        Friendship.user1_id).filter_by(user2_id=current_user.id)
    online_users = User.query.with_entities(User.id).filter(
        or_(User.is_online,
            User.last_seen > datetime.utcnow() - timedelta(minutes=1)))

    online_friend_list = friend_query1.union(friend_query2).intersect(
        online_users).all()
    online_friend_list = [r for r, in online_friend_list]

    for friend in online_friend_list:
        if friend not in player_names:
            player_names[friend] = User.query.with_entities(User.username) \
                .filter_by(id=friend).first_or_404()[0]

    return render_template('generic/lobby.html',
                           games_in_progress=games_in_progress,
                           player_names=player_names,
                           friend_requests=friend_requests,
                           online_friend_list=online_friend_list,
                           form=form,
                           challenges=challenges,
                           title=lazy_gettext('Lobby'))
Ejemplo n.º 2
0
def user_not_found(username):
    form = UserSearchForm()
    if form.validate_on_submit():
        return redirect(
            url_for('profile.overview',
                    username=form.username.data).replace('%40', '@'))
    return render_template('profile/user_not_found.html',
                           username=username,
                           form=form)
Ejemplo n.º 3
0
def lobby():
    form = UserSearchForm()
    if form.validate_on_submit():
        return redirect(
            url_for('profile.overview',
                    username=form.username.data).replace('%40', '@'))

    player_names = {}
    games_in_progress = Game.query.filter(
        ((Game.player1 == current_user.id) | (Game.player2 == current_user.id))
        & (Game.status == 'started')).order_by(desc(Game.id)).all()
    cricket_games_in_progress = CricketGame.query.filter(((CricketGame.player1 == current_user.id) |
                                                          (CricketGame.player2 == current_user.id)) &
                                                         (CricketGame.status == 'started'))\
        .order_by(desc(CricketGame.id)).all()
    games_in_progress.extend(cricket_games_in_progress)
    for game in games_in_progress:
        if game.player1 and game.player1 not in player_names:
            player_names[game.player1] = User.query.with_entities(User.username) \
                .filter_by(id=game.player1).first_or_404()[0]
        if game.player2 and game.player2 not in player_names:
            player_names[game.player2] = User.query.with_entities(User.username) \
                .filter_by(id=game.player2).first_or_404()[0]

    # get challenges
    challenges = Game.query.filter_by(status='challenged',
                                      player2=current_user.id).all()
    cricket_challenges = CricketGame.query.filter_by(
        status='challenged', player2=current_user.id).all()
    challenges.extend(cricket_challenges)
    for game in challenges:
        player_names[game.player1] = User.query.with_entities(User.username) \
            .filter_by(id=game.player1).first_or_404()[0]
    challenges = [r.__dict__ for r in challenges]

    # get friend requests
    friend_requests = FriendshipRequest.query.filter_by(
        receiving_user_id=current_user.id).all()
    for friend_request in friend_requests:
        if friend_request.requesting_user_id not in player_names:
            player_names[friend_request.requesting_user_id] = User.query.with_entities(User.username) \
                .filter_by(id=friend_request.requesting_user_id).first_or_404()[0]

    # get online friends
    friend_query1 = Friendship.query.with_entities(
        Friendship.user2_id).filter_by(user1_id=current_user.id)
    friend_query2 = Friendship.query.with_entities(
        Friendship.user1_id).filter_by(user2_id=current_user.id)
    online_users = User.query.with_entities(User.id).filter(
        or_(User.is_online,
            User.last_seen > datetime.utcnow() - timedelta(minutes=1)))

    online_friend_list = friend_query1.union(friend_query2).intersect(
        online_users).all()
    online_friend_list = [r for r, in online_friend_list]

    for friend in online_friend_list:
        if friend not in player_names:
            player_names[friend] = (User.query.with_entities(
                User.username).filter_by(id=friend).first_or_404()[0])

    player_names_sorted = {
        k: v
        for k, v in sorted(player_names.items(), key=lambda x: x[1])
    }
    online_friend_list.sort(key=lambda k: player_names_sorted[k])

    mobile_follower_mode = (WebcamSettings.query.with_entities(
        WebcamSettings.mobile_follower_mode).filter_by(
            user=current_user.id).first())
    if mobile_follower_mode:
        mobile_follower_mode = mobile_follower_mode[0]
    else:
        mobile_follower_mode = False

    return render_template('generic/lobby.html',
                           games_in_progress=games_in_progress,
                           player_names=player_names,
                           friend_requests=friend_requests,
                           online_friend_list=online_friend_list,
                           form=form,
                           mobile_follower_mode=mobile_follower_mode,
                           challenges=challenges,
                           title=lazy_gettext('Lobby'))