def start(hashid, theme=None): game = Game.query.filter_by(hashid=hashid).first_or_404() # check if we found an opponent, logged in users only if game.status == 'challenged' and current_user.is_authenticated \ and current_user.id != game.player1: if not game.player2 or game.player2 == current_user.id: game.player2 = current_user.id game.status = 'started' db.session.commit() # send message to global chat broadcast_new_game(game) # signal the waiting player and spectators start_game(hashid) game_dict = game.as_dict() player_names = get_player_names(game) game_dict['player1_name'] = player_names[0] game_dict['player2_name'] = player_names[1] match_json = json.loads(game.match_json) # for player1 and spectators while waiting if game.status == 'challenged': p2_name = get_name_by_id(game.player2) if game.player2 else None return render_template('game/wait_for_opponent.html', game=game_dict, p2_name=p2_name, title=lazy_gettext("Waiting...")) # for everyone if the game is completed if game.status in ('completed', 'aborted', 'declined'): statistics = collect_statistics(game, match_json) return render_template('game/X01_completed.html', game=game_dict, match_json=match_json, stats=statistics, title=lazy_gettext('Match overview')) # for running games else: form = ScoreForm() chat_form = GameChatmessageForm() chat_form_small = GameChatmessageForm() caller = current_user.caller if current_user.is_authenticated else 'default' cpu_delay = current_user.cpu_delay if current_user.is_authenticated else 0 user = current_user.id if current_user.is_authenticated else None if user == game.player1 or user == game.player2: messages = ChatmessageIngame.query.filter_by( game_hashid=game.hashid).order_by( ChatmessageIngame.id.asc()).all() else: messages = [] user_names = {} for message in messages: user_names[message.author] = User.query.with_entities(User.username) \ .filter_by(id=message.author).first_or_404()[0] if theme: return render_template('game/X01_stream.html', game=game_dict, form=form, match_json=match_json, caller=caller, cpu_delay=cpu_delay, title=lazy_gettext('Stream overlay'), chat_form=chat_form, chat_form_small=chat_form_small, messages=messages, user_names=user_names) return render_template('game/X01.html', game=game_dict, form=form, match_json=match_json, caller=caller, cpu_delay=cpu_delay, title=lazy_gettext('Live Match'), chat_form=chat_form, chat_form_small=chat_form_small, messages=messages, user_names=user_names)
def validate_game_chat_message(): # validating the score input from users form = GameChatmessageForm(request.form) result = form.validate() return jsonify(form.errors)
def start(hashid, theme=None): game = GameBase.query.filter_by(hashid=hashid).first_or_404() # check if we found an opponent, logged in users only if game.status == 'challenged' and current_user.is_authenticated and current_user.id != game.player1: # Check webcam consent if game.webcam: webcam_settings = WebcamSettings.query.filter_by( user=current_user.id).first() if not webcam_settings or not webcam_settings.activated: return redirect(url_for('game.webcam_consent')) if not game.player2 or game.player2 == current_user.id: game.player2 = current_user.id game.status = 'started' db.session.commit() # send message to global chat broadcast_new_game(game) # signal the waiting player and spectators start_game(hashid) broadcast_public_challenges() game_dict = game.__dict__ player_names = get_player_names(game) game_dict['player1_name'] = player_names[0] game_dict['player2_name'] = player_names[1] match_json = json.loads(game.match_json) # for player1 and spectators while waiting if game.status == 'challenged': if game.public_challenge: if not current_user.is_authenticated: return redirect(url_for('generic.index')) return render_template( 'game/wait_for_opponent_public_challenge.html', game=game_dict, title=lazy_gettext('Waiting...'), ) p2_name = get_name_by_id(game.player2) if game.player2 else None return render_template( 'game/wait_for_opponent.html', game=game_dict, p2_name=p2_name, title=lazy_gettext('Waiting...'), ) # for everyone if the game is completed if game.status in {'completed', 'aborted', 'declined'}: statistics = collect_statistics(game, match_json) player_countries = [None, None] if game.player1: p1_country = UserSettings.query.with_entities( UserSettings.country).filter_by(user=game.player1).first() if not p1_country: p1_country = UserSettings(user=game.player1) db.session.add(p1_country) db.session.commit() p1_country = None else: p1_country = p1_country[0] player_countries[0] = p1_country if game.player2 and game.player1 != game.player2: p2_country = UserSettings.query.with_entities( UserSettings.country).filter_by(user=game.player2).first() if not p2_country: p2_country = UserSettings(user=game.player2) db.session.add(p2_country) db.session.commit() p2_country = None else: p2_country = p2_country[0] player_countries[1] = p2_country return render_template( f'game/{game.variant}/completed.html', game=game_dict, match_json=match_json, stats=statistics, countries=player_countries, title=lazy_gettext('Match overview'), ) # for running games form = ScoreForm() chat_form = GameChatmessageForm() chat_form_small = GameChatmessageForm() caller = 'default' cpu_delay = current_user.cpu_delay if current_user.is_authenticated else 0 user = current_user.id if current_user.is_authenticated else None if user in {game.player1, game.player2}: messages = ChatmessageIngame.query.filter_by( game_hashid=game.hashid).order_by( ChatmessageIngame.id.asc()).all() else: messages = [] if user: settings = UserSettings.query.filter_by(user=user).first() if not settings: settings = UserSettings(user=user) db.session.add(settings) db.session.commit(settings) caller = settings.caller else: settings = {'checkout_suggestions': False} user_names = {} for message in messages: user_names[message.author] = (User.query.with_entities( User.username).filter_by(id=message.author).first_or_404()[0]) if theme: title = lazy_gettext('Stream overlay') else: title = lazy_gettext('Live Match') stream_consent = True channel_ids = [None, None] jitsi_force_public_server = False if 'JITSI_FORCE_PUBLIC_SERVER' in current_app.config: jitsi_force_public_server = current_app.config[ 'JITSI_FORCE_PUBLIC_SERVER'] if game.webcam and current_user.is_authenticated and current_user.id in ( game.player1, game.player2): # webcam base template template = 'webcam' webcam_settings = WebcamSettings.query.filter_by( user=current_user.id).first() if webcam_settings and webcam_settings.force_scoreboard_page: # force normal game template template = 'game' p1_webcam_settings = WebcamSettings.query.filter_by( user=game.player1).first() p2_webcam_settings = WebcamSettings.query.filter_by( user=game.player2).first() if game.player2 else None if not p1_webcam_settings.stream_consent or ( p2_webcam_settings and not p2_webcam_settings.stream_consent): stream_consent = False else: if game.webcam: p1_webcam_settings = WebcamSettings.query.filter_by( user=game.player1).first() p2_webcam_settings = WebcamSettings.query.filter_by( user=game.player2).first() if game.player2 else None p1_user_settings = UserSettings.query.filter_by( user=game.player1).first() p2_user_settings = UserSettings.query.filter_by( user=game.player2).first() if game.player2 else None if (p1_webcam_settings.stream_consent and p2_webcam_settings and p2_webcam_settings.stream_consent and (p1_user_settings.channel_id or p1_user_settings.channel_id)): # consent was given by both players, render watch page template = 'watch_webcam' channel_ids[0] = p1_user_settings.channel_id channel_ids[1] = p2_user_settings.channel_id else: # no consent for spectators template = 'game' webcam_settings = None else: template = 'game' webcam_settings = None return render_template( f'game/{game.variant}/{template}.html', game=game_dict, form=form, match_json=match_json, caller=caller, cpu_delay=cpu_delay, title=title, chat_form=chat_form, chat_form_small=chat_form_small, messages=messages, user_names=user_names, stream=theme, channel_ids=channel_ids, settings=settings, webcam_settings=webcam_settings, stream_consent=stream_consent, jitsi_force_public_server=jitsi_force_public_server, )