def connect_client(): if not current_user.is_authenticated: disconnect() return join_room(current_user.username) notifications = Notification.query.filter_by(user=current_user.id).all() for notification in notifications: send_notification(current_user.username, notification.message, notification.author, notification.type)
def send_private_message(message): message['message'] = bleach.clean(message['message']) message['message'] = bleach.linkify(message['message']) receiver = message['receiver'] receiver_settings = UserSettings.query.filter_by(user=receiver).first() if receiver_settings and not receiver_settings.allow_private_messages: emit('receiver_PMs_disabled', room=request.sid) return new_message = Privatemessage(message=message['message'], sender=current_user.id, receiver=receiver, timestamp=datetime.utcnow()) sender_name = current_user.username receiver_name = User.query.with_entities( User.username).filter_by(id=receiver).first_or_404()[0] notification = Notification(user=receiver, message=message['message'], author=sender_name, type='message') db.session.add(new_message) db.session.add(notification) db.session.commit() send_notification(receiver_name, message['message'], sender_name, 'message') emit('broadcast_private_message', dict(message=message['message'], sender=current_user.id, sender_name=sender_name, receiver_name=receiver_name, receiver=message['receiver'], timestamp=str(datetime.utcnow()) + 'Z'), room=receiver_name, broadcast=True) emit('broadcast_private_message', dict(message=message['message'], sender=current_user.id, sender_name=sender_name, receiver_name=receiver_name, receiver=message['receiver'], timestamp=str(datetime.utcnow()) + 'Z'), room=sender_name, broadcast=True)
def connect_client(): if not current_user.is_authenticated: return # current_user.ping() current_app.redis_client.sadd('last_seen_bulk_user_ids', current_user.id) join_room(current_user.username) notifications = Notification.query.filter_by(user=current_user.id).all() for notification in notifications: send_notification(current_user.username, notification.message, notification.author, notification.type, silent=True) emit('status_reply', {'status': current_user.status})
def send_private_message(message): message['message'] = bleach.clean(message['message']) receiver = message['receiver'] new_message = Privatemessage(message=message['message'], sender=current_user.id, receiver=receiver, timestamp=datetime.utcnow()) sender_name = current_user.username receiver_name = User.query.with_entities( User.username).filter_by(id=receiver).first_or_404()[0] notification = Notification(user=receiver, message=message['message'], author=sender_name, type='message') db.session.add(new_message) db.session.add(notification) db.session.commit() send_notification(receiver_name, message['message'], sender_name, 'message') emit('broadcast_private_message', dict(message=message['message'], sender=current_user.id, sender_name=sender_name, receiver_name=receiver_name, receiver=message['receiver'], timestamp=str(datetime.utcnow()) + 'Z'), room=receiver_name, broadcast=True) emit('broadcast_private_message', dict(message=message['message'], sender=current_user.id, sender_name=sender_name, receiver_name=receiver_name, receiver=message['receiver'], timestamp=str(datetime.utcnow()) + 'Z'), room=sender_name, broadcast=True)
def connect_client(): connections = SocketConnections.query.first() connections.active += 1 connections.total += 1 if not current_user.is_authenticated: db.session.commit() return current_user.ping() join_room(current_user.username) notifications = Notification.query.filter_by(user=current_user.id).all() for notification in notifications: send_notification(current_user.username, notification.message, notification.author, notification.type, silent=True) emit('status_reply', {'status': current_user.status})
def create(mode='x01', opponent_name=None): if mode == 'x01': x01_type = request.args.get('type') x01_type = x01_type if x01_type in ['170', '301', '501', '1001' ] else '170' starter = request.args.get('starter') starter_short = { '1': 'me', '2': 'opponent', 'bull': 'closest_to_bull', } starter = starter_short[starter] if starter in starter_short else 'me' bo_sets = request.args.get('sets') try: bo_sets = bo_sets if int(bo_sets) < 30 else 1 except (ValueError, TypeError): bo_sets = 1 bo_legs = request.args.get('legs') try: bo_legs = bo_legs if int(bo_legs) < 30 else 1 except (ValueError, TypeError): bo_legs = 1 two_clear_legs = request.args.get('2cl') form = CreateX01GameForm( opponent_name=opponent_name, type=x01_type, starter=starter, bo_sets=bo_sets, bo_legs=bo_legs, two_clear_legs=two_clear_legs, ) else: pass # no other game modes yet if form.validate_on_submit(): player1 = current_user.id if current_user.is_authenticated else None if player1 and form.opponent.data == 'local': player2 = current_user.id status = 'started' elif player1 and form.opponent.data == 'online': if form.opponent_name.data: player2 = User.query.with_entities(User.id).filter_by( username=form.opponent_name.data).first_or_404() message = gettext('New challenge') notification = Notification(user=player2, message=message, author=current_user.username, type='challenge') db.session.add(notification) send_notification(form.opponent_name.data, message, current_user.username, 'challenge') else: player2 = None status = 'challenged' else: # computer as opponent player2 = None status = 'started' match_json = json.dumps({ 1: { 1: { 1: { 'scores': [], 'double_missed': [] }, 2: { 'scores': [], 'double_missed': [] } } } }) game = Game(player1=player1, player2=player2, type=form.type.data, bo_sets=form.bo_sets.data, bo_legs=form.bo_legs.data, two_clear_legs=form.two_clear_legs.data, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=int(form.type.data), p2_score=int(form.type.data), in_mode=form.in_mode.data, out_mode=form.out_mode.data, begin=datetime.utcnow(), match_json=match_json, status=status, opponent_type=form.opponent.data) if game.opponent_type.startswith('computer'): game.opponent_type += form.level.data game.p1_next_turn = form.starter.data == 'me' if form.starter.data == 'closest_to_bull': game.p1_next_turn = True closest_to_bull_json = json.dumps({1: [], 2: []}) game.closest_to_bull_json = closest_to_bull_json game.closest_to_bull = True db.session.add(game) db.session.commit() # needed to get a game id for the hashid game.set_hashid() db.session.commit() return redirect(url_for('game.start', hashid=game.hashid)) return render_template('game/create_X01.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game'))
def create(mode='x01', opponent_name=None, tournament_hashid=None): if mode == 'x01': form = prepare_x01_form(opponent_name, tournament_hashid) else: form = prepare_cricket_form(opponent_name, tournament_hashid) webcam_query = WebcamSettings.query.filter_by(user=current_user.id).first() if not webcam_query or not webcam_query.activated: if form.webcam.data: return redirect(url_for('game.webcam_consent')) form.webcam.render_kw = {'disabled': 'disabled'} if form.validate_on_submit(): player1 = current_user.id if current_user.is_authenticated else None if player1 and form.opponent.data == 'local': player2 = current_user.id status = 'started' elif player1 and form.opponent.data == 'online': if form.opponent_name.data: if form.public_challenge.data: # Public challenge flag and player name specified is not allowed flash( gettext( 'Public challenges must be created without an opponent name.' ), 'danger') return render_template('game/create_game.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game')) player2 = (User.query.with_entities(User.id).filter( func.lower(User.username) == func.lower( form.opponent_name.data)).first_or_404()) player2_settings = UserSettings.query.filter_by( user=player2.id).first() if player2_settings and not player2_settings.allow_challenges: # player 2 does not allow challenge requests flash(gettext('Player does not allow challenges'), 'danger') return render_template('game/create_game.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game')) message = gettext('New challenge') notification = Notification(user=player2, message=message, author=current_user.username, type='challenge') db.session.add(notification) send_notification(form.opponent_name.data, message, current_user.username, 'challenge', webcam=form.webcam.data) else: player2 = None status = 'challenged' else: # computer as opponent player2 = None status = 'started' if form.webcam.data: if player2: webcam_player2 = WebcamSettings.query.filter_by( user=player2).first() if not webcam_player2 or not webcam_player2.activated: flash( gettext( 'Player 2 does not have webcam games enabled.'), 'danger') return render_template('game/create_game.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game')) else: webcam_player2 = None jitsi_hashid = secrets.token_urlsafe(8)[:8] webcam_player1 = WebcamSettings.query.filter_by( user=player1).first() jitsi_public_server = webcam_player1.jitsi_public_server or ( webcam_player2 and webcam_player2.jitsi_public_server) else: jitsi_hashid = None jitsi_public_server = False tournament = form.tournament.data if form.tournament.data != '-' else None try: player2_id = player2.id except AttributeError: player2_id = player2 if mode == 'x01': match_json = json.dumps({ 1: { 1: { 1: { 'scores': [], 'double_missed': [] }, 2: { 'scores': [], 'double_missed': [] } } } }) game = Game( player1=player1, player2=player2_id, type=form.type.data, variant='x01', bo_sets=form.bo_sets.data, bo_legs=form.bo_legs.data, two_clear_legs=form.two_clear_legs.data, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=int(form.type.data), p2_score=int(form.type.data), in_mode=form.in_mode.data, out_mode=form.out_mode.data, begin=datetime.utcnow(), match_json=match_json, status=status, opponent_type=form.opponent.data, public_challenge=form.public_challenge.data, tournament=tournament, score_input_delay=form.score_input_delay.data, webcam=form.webcam.data, jitsi_hashid=jitsi_hashid, jitsi_public_server=jitsi_public_server, ) elif mode == 'cricket': match_json = json.dumps({ 1: { 1: cricket_leg_default.copy(), }, }, ) game = CricketGame( player1=player1, player2=player2_id, variant='cricket', bo_sets=form.bo_sets.data, bo_legs=form.bo_legs.data, two_clear_legs=form.two_clear_legs.data, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=0, p2_score=0, begin=datetime.utcnow(), match_json=match_json, status=status, opponent_type=form.opponent.data, public_challenge=form.public_challenge.data, tournament=tournament, score_input_delay=form.score_input_delay.data, webcam=form.webcam.data, jitsi_hashid=jitsi_hashid, jitsi_public_server=jitsi_public_server, ) else: pass # Preset saving if form.save_preset.data: if mode == 'x01': save_x01_preset(form) elif mode == 'cricket': save_cricket_preset(form) else: pass if game.opponent_type.startswith('computer'): game.opponent_type += form.level.data game.p1_next_turn = form.starter.data == 'me' if form.starter.data == 'closest_to_bull': game.p1_next_turn = True closest_to_bull_json = json.dumps({1: [], 2: []}) game.closest_to_bull_json = closest_to_bull_json game.closest_to_bull = True db.session.add(game) db.session.commit() # needed to get a game id for the hashid game.set_hashid() db.session.commit() return redirect(url_for('game.start', hashid=game.hashid)) return render_template('game/create_game.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game'))
def create(mode='x01', opponent_name=None): if mode == 'x01': preset = X01Presetting.query.filter_by(user=current_user.id).first() if not preset: preset = X01Presetting(user=current_user.id) db.session.add(preset) db.session.commit() if request.args.get('type'): x01_type = request.args.get('type') if request.args.get( 'type') in ['170', '301', '501', '1001'] else '170' elif preset.type: x01_type = preset.type else: x01_type = 501 if request.args.get('starter'): starter_short = { '1': 'me', '2': 'opponent', 'bull': 'closest_to_bull', } starter = starter_short[request.args.get( 'starter')] if request.args.get( 'starter') in starter_short else 'me' elif preset.starter: starter = preset.starter else: starter = 'me' if request.args.get('sets'): bo_sets = request.args.get('sets') try: bo_sets = bo_sets if int(bo_sets) < 30 else 1 except (ValueError, TypeError): bo_sets = 1 elif preset.bo_sets: bo_sets = preset.bo_sets else: bo_sets = 1 if request.args.get('legs'): bo_legs = request.args.get('legs') try: bo_legs = bo_legs if int(bo_legs) < 30 else 1 except (ValueError, TypeError): bo_legs = 1 elif preset.bo_legs: bo_legs = preset.bo_legs else: bo_legs = 5 if request.args.get('2cl'): two_clear_legs = request.args.get('2cl') elif preset.two_clear_legs: two_clear_legs = preset.two_clear_legs else: two_clear_legs = False level = preset.level if preset.level else 1 opponent = preset.opponent_type if preset.opponent_type else 'online' in_mode = preset.in_mode if preset.in_mode else 'si' out_mode = preset.out_mode if preset.out_mode else 'do' public_challenge = preset.public_challenge if preset.public_challenge else False form = CreateX01GameForm( opponent_name=opponent_name, opponent=opponent, type=x01_type, starter=starter, bo_sets=bo_sets, bo_legs=bo_legs, two_clear_legs=two_clear_legs, level=level, in_mode=in_mode, out_mode=out_mode, public_challenge=public_challenge, ) else: pass # no other game modes yet if form.validate_on_submit(): player1 = current_user.id if current_user.is_authenticated else None if player1 and form.opponent.data == 'local': player2 = current_user.id status = 'started' elif player1 and form.opponent.data == 'online': if form.opponent_name.data: if form.public_challenge.data: # Public challenge flag and player name specified is not allowed flash( gettext( 'Public challenges must be created without an opponent name.' ), 'danger') return render_template('game/create_X01.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game')) player2 = User.query.with_entities(User.id).filter( User.username.ilike( form.opponent_name.data)).first_or_404() player2_settings = UserSettings.query.filter_by( user=player2.id).first() if player2_settings and not player2_settings.allow_challenges: # player 2 does not allow challenge requests flash(gettext('Player does not allow challenges'), 'danger') return render_template('game/create_X01.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game')) message = gettext('New challenge') notification = Notification(user=player2, message=message, author=current_user.username, type='challenge') db.session.add(notification) send_notification(form.opponent_name.data, message, current_user.username, 'challenge') else: player2 = None status = 'challenged' else: # computer as opponent player2 = None status = 'started' match_json = json.dumps({ 1: { 1: { 1: { 'scores': [], 'double_missed': [] }, 2: { 'scores': [], 'double_missed': [] } } } }) game = Game( player1=player1, player2=player2, type=form.type.data, bo_sets=form.bo_sets.data, bo_legs=form.bo_legs.data, two_clear_legs=form.two_clear_legs.data, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=int(form.type.data), p2_score=int(form.type.data), in_mode=form.in_mode.data, out_mode=form.out_mode.data, begin=datetime.utcnow(), match_json=match_json, status=status, opponent_type=form.opponent.data, public_challenge=form.public_challenge.data, ) # Preset saving if form.save_preset.data: preset = X01Presetting.query.filter_by( user=current_user.id).first() if not preset: preset = X01Presetting(user=current_user.id) db.session.add(preset) preset.bo_sets = form.bo_sets.data preset.bo_legs = form.bo_legs.data preset.two_clear_legs = form.two_clear_legs.data preset.starter = form.starter.data preset.type = form.type.data preset.in_mode = form.in_mode.data preset.out_mode = form.out_mode.data preset.opponent_type = form.opponent.data preset.level = form.level.data preset.public_challenge = form.public_challenge.data db.session.commit() if game.opponent_type.startswith('computer'): game.opponent_type += form.level.data game.p1_next_turn = form.starter.data == 'me' if form.starter.data == 'closest_to_bull': game.p1_next_turn = True closest_to_bull_json = json.dumps({1: [], 2: []}) game.closest_to_bull_json = closest_to_bull_json game.closest_to_bull = True db.session.add(game) db.session.commit() # needed to get a game id for the hashid game.set_hashid() db.session.commit() return redirect(url_for('game.start', hashid=game.hashid)) return render_template('game/create_X01.html', form=form, opponent_name=opponent_name, title=lazy_gettext('Create Game'))