Beispiel #1
0
def private_messages():
    form = ChatmessageForm()
    messages_sent = Privatemessage.query.filter_by(sender=current_user.id)
    messages_received = Privatemessage.query.filter_by(
        receiver=current_user.id)
    messages = messages_sent.union(messages_received).order_by(
        Privatemessage.id.asc()).all()
    user_names = {current_user.id: current_user.username}
    messages_dict = defaultdict(list)
    status = {}

    for message in messages:
        other_user = message.sender if message.sender != current_user.id else message.receiver
        messages_dict[other_user].append({
            'sender': message.sender,
            'receiver': message.receiver,
            'message': message.message,
            'timestamp': message.timestamp
        })

        if other_user not in user_names:
            other_user = User.query.filter_by(id=other_user).first_or_404()
            user_names[other_user.id] = other_user.username
            status[other_user.id] = get_user_status(other_user)

    return render_template('generic/inbox.html',
                           form=form,
                           messages=messages_dict,
                           user_names=user_names,
                           status=status,
                           title=lazy_gettext('Private Messages'))
Beispiel #2
0
def chat():
    form = ChatmessageForm()
    messages = (Chatmessage.query.filter_by(tournament_hashid=None).filter(
        Chatmessage.timestamp > (
            datetime.utcnow() - timedelta(hours=3))).order_by(
                Chatmessage.id.desc()).join(User).add_columns(
                    User.username).join(UserStatistic).add_columns(
                        UserStatistic.average).join(UserSettings).add_columns(
                            UserSettings.country).limit(100).all())

    player1 = aliased(User)
    player2 = aliased(User)
    recent_results = (Game.query.filter_by(status='completed').filter(
        Game.end != None).filter(Game.player2 != None).join(
            player1,
            Game.player1 == player1.id).add_columns(player1.username).join(
                player2, Game.player2 == player2.id,
                isouter=True).add_columns(player2.username).order_by(
                    Game.end.desc()).limit(10).all())

    new_games = (Game.query.filter_by(
        status='started').filter(Game.player1 != Game.player2).join(
            player1,
            Game.player1 == player1.id).add_columns(player1.username).join(
                player2, Game.player2 == player2.id, isouter=True).add_columns(
                    player2.username).order_by(Game.id.desc()).limit(10).all())

    return render_template(
        'generic/chat.html',
        form=form,
        messages=messages,
        recent_results=recent_results,
        new_games=new_games,
        title=lazy_gettext('Chat'),
    )
Beispiel #3
0
def chat():
    form = ChatmessageForm()
    messages = (Chatmessage.query.filter(
        Chatmessage.timestamp > (datetime.utcnow() -
                                 timedelta(hours=12))).order_by(
                                     Chatmessage.id.desc()).limit(200).all())
    messages = messages[::-1]
    user_names = {}
    statistics = {}

    for message in messages:
        if message.author in user_names:
            continue
        user_names[message.author] = (User.query.with_entities(
            User.username).filter_by(id=message.author).first_or_404()[0])
        user_statistic = UserStatistic.query.filter_by(
            user=message.author).first()
        if not user_statistic:
            user_statistic = UserStatistic(user=message.author,
                                           average=0,
                                           doubles=0)
            db.session.add(user_statistic)
            db.session.commit()
        statistics[message.author] = {
            'average': user_statistic.average,
            'doubles': user_statistic.doubles
        }

    return render_template('generic/chat.html',
                           form=form,
                           messages=messages,
                           statistics=statistics,
                           user_names=user_names,
                           title=lazy_gettext('Chat'))
Beispiel #4
0
def chat():
    form = ChatmessageForm()
    messages = (Chatmessage.query.filter_by(tournament_hashid=None).filter(
        Chatmessage.timestamp > (datetime.utcnow() - timedelta(hours=3))
    ).order_by(
        Chatmessage.id.desc()).limit(100).from_self().join(User).add_columns(
            User.username).join(UserStatistic).add_columns(
                UserStatistic.average).join(UserSettings).add_columns(
                    UserSettings.country).all())

    player1 = aliased(User)
    player2 = aliased(User)
    recent_results = (GameBase.query.options(
        load_only('id', 'player1', 'player2', 'status', 'end')).filter_by(
            status='completed').filter(GameBase.end != None).filter(
                GameBase.player2 != None).order_by(
                    GameBase.end.desc()).limit(10).from_self().join(
                        player1, GameBase.player1 == player1.id).add_columns(
                            player1.username).join(
                                player2,
                                GameBase.player2 == player2.id,
                                isouter=True).add_columns(
                                    player2.username).all())

    for game, _, _ in recent_results:
        if game.bo_sets > 1:
            game.p1_final_score = game.p1_sets
            game.p2_final_score = game.p2_sets
        else:
            game.p1_final_score = game.p1_legs
            game.p2_final_score = game.p2_legs

    new_games = (GameBase.query.filter_by(status='started').filter(
        GameBase.player1 != GameBase.player2).order_by(
            GameBase.id.desc()).limit(10).from_self().join(
                player1, GameBase.player1 == player1.id).add_columns(
                    player1.username).join(player2,
                                           GameBase.player2 == player2.id,
                                           isouter=True).add_columns(
                                               player2.username).all())

    settings = UserSettings.query.filter_by(user=current_user.id).first()
    if not settings:
        settings = UserSettings(user=current_user.id)
        db.session.add(settings)
        db.session.commit()

    return render_template(
        'generic/chat.html',
        form=form,
        messages=messages,
        recent_results=recent_results,
        new_games=new_games,
        show_average_in_chat_list=settings.show_average_in_chat_list,
        country=settings.country,
        title=lazy_gettext('Chat'),
    )
Beispiel #5
0
def details(hashid):
    tournament, creator_name = (Tournament.query.filter_by(hashid=hashid).join(
        User, User.id == Tournament.creator).add_columns(
            User.username).first_or_404())

    if current_user in tournament.banned_players:
        flash(lazy_gettext('You were banned from this tournament.'), 'danger')
        return redirect(url_for('generic.lobby'))

    form = ChatmessageForm()
    messages = (Chatmessage.query.filter_by(tournament_hashid=hashid).filter(
        Chatmessage.timestamp > (datetime.utcnow() - timedelta(hours=24))
    ).order_by(
        Chatmessage.id.desc()).limit(100).from_self().join(User).add_columns(
            User.username).join(UserStatistic).add_columns(
                UserStatistic.average).join(UserSettings).add_columns(
                    UserSettings.country).all())

    in_tournament = tournament in current_user.tournaments

    player1 = aliased(User)
    player2 = aliased(User)
    recent_results = (GameBase.query.filter_by(tournament=hashid).filter_by(
        status='completed').order_by(
            GameBase.end.desc()).limit(10).from_self().join(
                player1, GameBase.player1 == player1.id).add_columns(
                    player1.username).join(player2,
                                           GameBase.player2 == player2.id,
                                           isouter=True).add_columns(
                                               player2.username).all())

    settings = UserSettings.query.filter_by(user=current_user.id).first()
    if not settings:
        settings = UserSettings(user=current_user.id)
        db.session.add(settings)
        db.session.commit()

    for game, _, _ in recent_results:
        if game.bo_sets > 1:
            game.p1_final_score = game.p1_sets
            game.p2_final_score = game.p2_sets
        else:
            game.p1_final_score = game.p1_legs
            game.p2_final_score = game.p2_legs

    return render_template(
        'tournament/details.html',
        tournament=tournament,
        form=form,
        messages=messages,
        in_tournament=in_tournament,
        recent_results=recent_results,
        creator_name=creator_name,
        show_average_in_chat_list=settings.show_average_in_chat_list,
        country=settings.country,
        title=lazy_gettext('Tournament details'),
    )
Beispiel #6
0
def details(hashid):
    tournament = Tournament.query.filter_by(hashid=hashid).first_or_404()

    form = ChatmessageForm()
    messages = (
        Chatmessage.query
        .filter_by(tournament_hashid=hashid)
        .filter(Chatmessage.timestamp > (datetime.utcnow() - timedelta(hours=24)))
        .order_by(Chatmessage.id.desc())
        .join(User).add_columns(User.username)
        .join(UserStatistic).add_columns(UserStatistic.average)
        .join(UserSettings).add_columns(UserSettings.country)
        .limit(100)
        .all()
    )

    tournament = Tournament.query.filter_by(hashid=hashid).first()
    in_tournament = tournament in current_user.tournaments

    player1 = aliased(User)
    player2 = aliased(User)
    recent_results = (
        Game.query
        .filter_by(tournament=hashid)
        .filter_by(status='completed')
        .join(player1, Game.player1 == player1.id).add_columns(player1.username)
        .join(player2, Game.player2 == player2.id, isouter=True).add_columns(player2.username)
        .order_by(Game.end.desc())
        .limit(10)
        .all()
    )

    return render_template(
        'tournament/details.html',
        tournament=tournament,
        form=form,
        messages=messages,
        in_tournament=in_tournament,
        recent_results=recent_results,
        title=lazy_gettext('Tournament details'),
    )
Beispiel #7
0
def chat():
    form = ChatmessageForm()
    messages = (Chatmessage.query.filter(Chatmessage.timestamp > (
        datetime.utcnow() - timedelta(hours=3))).order_by(
            Chatmessage.id.desc()).join(User).add_columns(
                User.username).join(UserStatistic).add_columns(
                    UserStatistic.average).join(UserSettings).add_columns(
                        UserSettings.country).limit(100).all())

    match_alert = UserSettings.query.with_entities(
        UserSettings.match_alerts).filter_by(user=current_user.id).first()
    if not match_alert:
        match_alert = UserSettings(user=current_user.id)
        db.session.add(match_alert)
        db.session.commit()
        match_alert = True
    else:
        match_alert = match_alert[0]

    return render_template('generic/chat.html',
                           form=form,
                           messages=messages,
                           match_alert=match_alert,
                           title=lazy_gettext('Chat'))
Beispiel #8
0
def validate_chat_message():
    # validating the score input from users
    form = ChatmessageForm(request.form)
    result = form.validate()
    return jsonify(form.errors)