def change_avatar(): if request.method == 'POST' and 'avatar' in request.files: avatar_file = request.files['avatar'] if avatar_file.filename == '': flash(lazy_gettext('No selected file'), 'danger') return redirect(request.url) delete_avatar() filename = avatars.save(avatar_file, name='{}.'.format(current_user.id)) image = os.path.join(current_app.config['UPLOADS_DEFAULT_DEST'], 'avatars', filename) if image.split('.')[-1].lower() not in ['jpg', 'jpeg', 'gif', 'png']: flash(lazy_gettext('Wrong image format.')) return redirect(url_for('profile.change_avatar')) im = Image.open(image) rgb_im = im.convert('RGB') rgb_im.thumbnail((64, 64), Image.BICUBIC) rgb_im.save( os.path.join(current_app.config['UPLOADS_DEFAULT_DEST'], 'avatars', f'{current_user.id}_thumbnail.jpg'), "JPEG") current_user.avatar = filename current_user.avatar_version = int(time.time()) db.session.commit() flash(lazy_gettext("Avatar saved.")) return redirect(url_for('profile.change_avatar')) avatar_url = avatars.url( current_user.avatar) if current_user.avatar else avatars.url( 'default.png') return render_template('profile/change_avatar.html', avatar_url=avatar_url, title=lazy_gettext('Avatar Settings'))
def overview(username): player_names = {} user = User.query.filter(User.username.ilike(username)).first_or_404() games = Game.query.filter(((Game.player1 == user.id) | (Game.player2 == user.id)) & (Game.status != 'challenged') & (Game.status != 'declined') & (Game.status != 'aborted')) \ .order_by(desc(Game.id)).all() stats = {'darts_thrown': 0, 'double_thrown': 0, 'legs_won': 0, 'total_score': 0, 'number_of_games': 0, 'first9_scores': []} for game in games: socketio.sleep(0) 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] if not (game.type == 501 and game.out_mode == 'do' and game.in_mode == 'si'): continue player = '1' if (user.id == game.player1) else '2' stats['number_of_games'] += 1 match_json = json.loads(game.match_json) for set in match_json: for leg in match_json[set]: for score in match_json[set][leg][player]['scores'][:3]: stats['first9_scores'].append(score) stats['darts_thrown'] += len(match_json[set][leg][player]['scores']) * 3 stats['total_score'] += sum(match_json[set][leg][player]['scores']) if 'to_finish' in match_json[set][leg][player]: stats['darts_thrown'] -= (3 - match_json[set][leg][player]['to_finish']) stats['double_thrown'] += 1 stats['legs_won'] += 1 if isinstance(match_json[set][leg][player]['double_missed'], (list,)): stats['double_thrown'] += sum(match_json[set][leg][player]['double_missed']) else: # legacy: double_missed as int stats['double_thrown'] += match_json[set][leg][player]['double_missed'] 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) friend_list = friend_query1.union(friend_query2).all() friend_list = [r for (r,) in friend_list] stats['doubles'] = round((stats['legs_won'] / stats['double_thrown']), 4) * 100 if stats['double_thrown'] else 0 stats['average'] = round((stats['total_score'] / (stats['darts_thrown'])) * 3, 2) if stats['darts_thrown'] else 0 stats['first9_average'] = round((sum(stats['first9_scores']) / len(stats['first9_scores'])), 2) \ if stats['first9_scores'] else 0 avatar_url = avatars.url(user.avatar) if user.avatar else avatars.url('default.png') return render_template('profile/overview.html', user=user, games=games, player_names=player_names, friend_list=friend_list, stats=stats, avatar_url=avatar_url, title=lazy_gettext('Profile'), is_online=(user.last_seen > datetime.utcnow() - timedelta(seconds=15)))
def broadcast_online_players(broadcast=True): status_order = ['lfg', 'online', 'playing', 'busy'] ingame_count = 0 online_players_list = [] online_thresh_timestamp = datetime.utcnow() - timedelta(minutes=1) online_players = ( User.query .filter(or_(User.is_online, User.last_seen > online_thresh_timestamp)) .join(UserStatistic).add_columns(UserStatistic.average, UserStatistic.doubles) .join(UserSettings).add_columns(UserSettings.country) .all()) online_count = len(online_players) for user, average, doubles, country in online_players: socketio.sleep(0) status = user.status if user.last_seen_ingame and user.last_seen_ingame > (datetime.utcnow() - timedelta(seconds=35)): status = 'playing' ingame_count += 1 avatar = avatars.url(user.avatar) if user.avatar else avatars.url('default.png') statistics = {'average': average, 'doubles': doubles} country = country.lower() if country else None online_players_list.append( { 'id': user.id, 'username': user.username, 'status': status, 'status_prio': status_order.index(status), 'avatar': avatar, 'statistics': statistics, 'country': country, } ) online_players_list.sort(key=lambda k: (k['status_prio'], k['username'].lower())) if broadcast: emit( 'send_online_players', {'players': online_players_list, 'ingame-count': ingame_count, 'online-count': online_count}, namespace='/chat', broadcast=True ) else: emit( 'send_online_players', {'players': online_players_list, 'ingame-count': ingame_count, 'online-count': online_count}, namespace='/chat', room=request.sid )
def change_avatar(): if request.method == 'POST' and 'avatar' in request.files: delete_avatar() filename = avatars.save(request.files['avatar'], name='{}.'.format(current_user.id)) current_user.avatar = filename db.session.commit() flash(lazy_gettext("Avatar saved.")) return redirect(url_for('profile.change_avatar')) avatar_url = avatars.url(current_user.avatar) if current_user.avatar else avatars.url('default.png') return render_template('profile/change_avatar.html', avatar_url=avatar_url, title=lazy_gettext('Avatar Settings'))
def broadcast_online_players(): status_order = ['lfg', 'online', 'playing', 'busy'] ingame_count = 0 online_players_list = [] online_players = (User.query.filter( User.last_seen > (datetime.utcnow() - timedelta(seconds=15))).all()) online_count = len(online_players) for user in online_players: status = user.status if user.last_seen_ingame and user.last_seen_ingame > ( datetime.utcnow() - timedelta(seconds=10)): status = 'playing' ingame_count += 1 avatar = avatars.url( user.avatar) if user.avatar else avatars.url('default.png') user_statistic = UserStatistic.query.filter_by(user=user.id).first() if not user_statistic: user_statistic = UserStatistic(user=user.id, average=0, doubles=0) db.session.add(user_statistic) db.session.commit() statistics = { 'average': user_statistic.average, 'doubles': user_statistic.doubles } online_players_list.append({ 'id': user.id, 'username': user.username, 'status': status, 'status_prio': status_order.index(status), 'avatar': avatar, 'statistics': statistics, }) online_players_list.sort( key=lambda k: (k['status_prio'], k['username'].lower())) emit('send_online_players', { 'players': online_players_list, 'ingame-count': ingame_count, 'online-count': online_count }, broadcast=True, namespace='/chat')
def overview(username): player_names = {} user = User.query.filter(User.username.ilike(username)).first() if not user: return redirect(url_for('profile.user_not_found', username=username)) player1 = aliased(User) player2 = aliased(User) games = ( Game.query .filter( ((Game.player1 == user.id) | (Game.player2 == user.id)) & (Game.status != 'challenged') & (Game.status != 'declined') & (Game.status != 'aborted') ) .join(player1, Game.player1 == player1.id).add_columns(player1.username) .join(player2, Game.player2 == player2.id, isouter=True).add_columns(player2.username) .order_by(desc(Game.id)).limit(10).all() ) stats = UserStatistic.query.filter_by(user=user.id).first() country = UserSettings.query.with_entities(UserSettings.country).filter_by(user=user.id).first() if country: country = country[0] else: country = UserSettings(user=user.id) db.session.add(country) db.session.commit() country = None 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) friend_list = friend_query1.union(friend_query2).all() friend_list = [r for (r,) in friend_list] avatar_url = avatars.url(user.avatar) if user.avatar else avatars.url('default.png') return render_template('profile/overview.html', user=user, games=games, player_names=player_names, friend_list=friend_list, recently_online=user.recently_online(), country=country, stats=stats, avatar_url=avatar_url, title=lazy_gettext('Profile'))
def broadcast_online_players(broadcast=True, room='public_chat'): status_order = ['lfg', 'online', 'playing', 'busy', 'offline'] ingame_count = 0 online_players_list = [] online_thresh_timestamp = datetime.utcnow() - timedelta(minutes=1) online_players = User.query if room == 'public_chat': online_players = online_players.filter( or_(User.is_online, User.last_seen > online_thresh_timestamp)) else: online_players = (online_players.join( User.tournaments).filter(Tournament.hashid == room)) online_players = (online_players.join( UserStatistic, isouter=True).add_columns( UserStatistic.average, UserStatistic.doubles).join( UserSettings, isouter=True).add_columns(UserSettings.country).join( WebcamSettings, isouter=True).add_columns(WebcamSettings.activated).all()) online_count = len(online_players) for user, average, doubles, country, webcam in online_players: socketio.sleep(0) status = user.status if user.last_seen_ingame and user.last_seen_ingame > ( datetime.utcnow() - timedelta(seconds=35)): status = 'playing' ingame_count += 1 if user.last_seen and user.last_seen < (datetime.utcnow() - timedelta(seconds=60)): status = 'offline' online_count -= 1 avatar = avatars.url(f'{user.id}_thumbnail.jpg' ) if user.avatar else avatars.url('default.png') average = average if average else 0 doubles = doubles if doubles else 0 statistics = {'average': average, 'doubles': doubles} country = country.lower() if country else None webcam = webcam if webcam else False online_players_list.append({ 'id': user.id, 'username': user.username, 'status': status, 'status_prio': status_order.index(status), 'avatar': avatar, 'statistics': statistics, 'country': country, 'is_backer': user.is_backer, 'webcam': webcam }) online_players_list.sort( key=lambda k: (k['status_prio'], k['username'].lower())) if broadcast: emit( 'send_online_players', { 'players': online_players_list, 'ingame-count': ingame_count, 'online-count': online_count }, namespace='/chat', room=room, ) else: emit('send_online_players', { 'players': online_players_list, 'ingame-count': ingame_count, 'online-count': online_count }, namespace='/chat', room=request.sid)
def overview(username): player_names = {} user = User.query.filter( func.lower(User.username) == func.lower(username)).first() if not user: return redirect(url_for('profile.user_not_found', username=username)) player1 = aliased(User) player2 = aliased(User) games = (Game.query.filter( ((Game.player1 == user.id) | (Game.player2 == user.id)) & (Game.status != 'challenged') & (Game.status != 'declined') & (Game.status != 'aborted')).join( player1, Game.player1 == player1.id).add_columns(player1.username).join( player2, Game.player2 == player2.id, isouter=True).add_columns( player2.username).order_by(desc(Game.id)).limit(10).all()) cricket_games = (CricketGame.query.filter( ((CricketGame.player1 == user.id) | (CricketGame.player2 == user.id)) & (CricketGame.status != 'challenged') & (CricketGame.status != 'declined') & (CricketGame.status != 'aborted')).join( player1, CricketGame.player1 == player1.id).add_columns( player1.username).join( player2, CricketGame.player2 == player2.id, isouter=True).add_columns(player2.username).order_by( desc(CricketGame.id)).limit(10).all()) games.extend(cricket_games) games.sort(key=lambda game: game[0].begin, reverse=True) stats = UserStatistic.query.filter_by(user=user.id).first() if not stats: stats = UserStatistic(user=user.id) db.session.add(stats) db.session.commit() settings = (UserSettings.query.with_entities( UserSettings.country, UserSettings.profile_text).filter_by(user=user.id).first()) if not settings: settings = UserSettings(user=user.id) db.session.add(settings) db.session.commit() country = None profile_text = None else: country, profile_text = settings if profile_text: profile_text = re.sub( r'(\bhttps:\/\/i\.imgur\.com\/\w+.\w+)', '<img src="' + r'\1' + '" style="max-width: 100%">', profile_text, ) profile_text = linker.linkify(profile_text) profile_text = profile_text.replace('\n', '<br>') 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) friend_list = friend_query1.union(friend_query2).all() friend_list = [r for (r, ) in friend_list] avatar_url = avatars.url(f'{user.id}_thumbnail.jpg' ) if user.avatar else avatars.url('default.png') return render_template('profile/overview.html', user=user, games=games[:10], player_names=player_names, friend_list=friend_list, recently_online=user.recently_online(), country=country, profile_text=profile_text, stats=stats, avatar_url=avatar_url, title=lazy_gettext('Profile'))