def game(db_session, user, user2): match_json = json.dumps({1: {1: {1: [], 2: []}}}) game = Game(player1=user.id, player2=user2.id, type=170, bo_sets=3, bo_legs=3, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=170, p2_score=170, in_mode='si', out_mode='do', begin=datetime.utcnow(), match_json=match_json, status='started') game.p1_next_turn = True db_session.add(user) db_session.add(user2) db_session.add(game) db_session.commit() game.set_hashid() db_session.commit() return game
def create_rematch(hashid): game = Game.query.filter_by(hashid=hashid).first_or_404() match_json = json.dumps({ 1: { 1: { 1: { 'scores': [], 'double_missed': [] }, 2: { 'scores': [], 'double_missed': [] } } } }) rematch = Game( player1=game.player1, player2=game.player2, type=game.type, bo_sets=game.bo_sets, bo_legs=game.bo_legs, two_clear_legs=game.two_clear_legs, p1_sets=0, p2_sets=0, p1_legs=0, p2_legs=0, p1_score=int(game.type), p2_score=int(game.type), in_mode=game.in_mode, out_mode=game.out_mode, begin=datetime.utcnow(), match_json=match_json, status='started', opponent_type=game.opponent_type, public_challenge=False, tournament=game.tournament, score_input_delay=game.score_input_delay, webcam=game.webcam, jitsi_hashid=secrets.token_urlsafe(8)[:8], variant='x01', ) rematch.p1_next_turn = True if game.closest_to_bull_json: closest_to_bull_json = json.dumps({1: [], 2: []}) rematch.closest_to_bull_json = closest_to_bull_json rematch.closest_to_bull = True db.session.add(rematch) db.session.commit() # needed to get a game id for the hashid rematch.set_hashid() db.session.commit() return rematch.hashid
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': 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): 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'))