def create(): #Getting habit_id from the hidden form habit_id = request.form.get('habit-id') current_habit = Habit.get_or_none(Habit.id == habit_id) game_id = current_habit.game_id game = Game.get_or_none(Game.id == game_id) user = User.get_or_none(User.id == current_habit.user_id) username = user.username sender_id = current_habit.user_id if sender_id == game.player_1_id: receiver_id = game.player_2_id else: receiver_id = game.player_1_id # if current_user == user: image_file = request.files[f'image-for-{habit_id}'] output = upload_file_to_s3(image_file, Config.S3_BUCKET) latest_round = Round.select(pw.fn.MAX(Round.id)).where(Round.game_id == game_id).scalar() new_habit_logged = LogHabit(habit_id = habit_id, sender_id = current_habit.user_id, receiver_id = receiver_id, approved = False, image_path = str(output), game_round_id = latest_round) new_habit_logged.save() flash('Habit submitted. Waiting for your opponent\'s approval. Keep up the good work!', 'success') return redirect(url_for('games.show', game_id = game_id, username = username ))
def index(game_id, user_id): rounds = Round.select().where(Round.game_id == game_id) game = Game.get_by_id(game_id) return render_template('/rounds/index.html', rounds=rounds, game=game)
def show(username, game_id): game = Game.get_or_none(Game.id == game_id) #int value for latest round_id for the game latest_round = Round.select(pw.fn.MAX( Round.id)).where(Round.game_id == game_id).scalar() current_round_object = Round.get_or_none(Round.id == latest_round) # get length of all rounds for game to send round number into anchor link to show latest round len_round = len(Round.select().where(Round.game_id == game_id)) # Active player - could be either player 1 or 2 user = User.get_or_none(User.username == username) #Set up habit info user_habits = Habit.select().where((Habit.game_id == game_id) & (Habit.user_id == user.id)) length_habit_list = len(user_habits) #progress bars progress = [] user_more_to_go = [] for habit in user_habits: approved_logs = LogHabit.select().where( (LogHabit.sender_id == user.id) & (LogHabit.habit_id == habit.id) & (LogHabit.approved == True) & (LogHabit.game_round_id == latest_round)) logged_habits = len(approved_logs) percentage = logged_habits / habit.frequency * 100 progress.append(percentage) leftover = habit.frequency - logged_habits user_more_to_go.append(leftover) rounded_progress = [round(freq, 0) for freq in progress] # Opponent - could be either player 1 or player 2 # check is active user is player_1 or player_2 for the game if user.id == game.player_1_id: active_user = 1 opponent = User.get_or_none(User.id == game.player_2_id) else: active_user = 2 opponent = User.get_or_none(User.id == game.player_1_id) opponent_username = opponent.username opponent_habits = Habit.select().where((Habit.game_id == game_id) & (Habit.user_id == opponent.id)) opponent_habit_length = len(opponent_habits) print(opponent_habit_length) # check if game has been accepted by player_2 or not game_accepted = game.accepted #Progress bars opponent_progress = [] for habit in opponent_habits: approved_logs = LogHabit.select().where( (LogHabit.sender_id == opponent.id) & (LogHabit.habit_id == habit.id) & (LogHabit.approved == True) & (LogHabit.game_round_id == latest_round)) logged_habits = len(approved_logs) percentage = logged_habits / habit.frequency * 100 opponent_progress.append(percentage) print(opponent_progress) rounded_opponent_progress = [round(freq, 0) for freq in opponent_progress] habits = Habit.select().where(Habit.game_id == game_id) game_habits = [] for habit in habits: game_habits.append(habit.id) to_approve = LogHabit.select().where((LogHabit.approved == False) & (LogHabit.receiver_id == user.id) & (LogHabit.habit_id in game_habits)) to_approve_length = len(to_approve) return render_template('games/show.html', username=user.username, user_habits=user_habits, length_habit_list=length_habit_list, rounded_progress=rounded_progress, opponent_username=opponent_username, opponent_habits=opponent_habits, opponent_habit_length=opponent_habit_length, rounded_opponent_progress=rounded_opponent_progress, user_more_to_go=user_more_to_go, game_id=game.id, game_accepted=game_accepted, active_user=active_user, to_approve_length=to_approve_length, current_round_object=current_round_object, len_round=len_round)